Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - tetra

Pages: [1] 2
1
Graphics / Re: How to handle a Geometry Buffer with SFML?
« on: May 27, 2015, 09:19:38 pm »
If you're saying that I really should go through all the tutorials beforehand (using LWJGL instead of JSFML) to actually understand the process, and finally use SFML when I actually need to integrate it into my game, then I'll do that. But I feel that there is some SFML functionality that I'm not finding that will enable me to store the textures, and write to them.

2
Graphics / Re: How to handle a Geometry Buffer with SFML?
« on: May 27, 2015, 09:16:16 pm »
I think I've been using Geometry Buffer in place of GBuffer, when they are not interchanegable. I am sorry.

Laurent said that the equivalent of an FBO in SFML is a RenderTexture, so that's why I'm using that.

The reason I'm asking about that in the thread title, is because that's what this is: a buffer to store multiple textures, each with its own vertex attribute. Those attributes are separated and written to the textures using the DrawBuffer.

Sorry if I was unclear, I have an idea of how the process works, I'm just not great at expressing exactly what I need to accomplish.

3
Java / Using LWJGL and JSFML together?
« on: May 27, 2015, 08:55:51 pm »
Is there anyway you can mix LWJGL and JSFML like http://www.sfml-dev.org/tutorials/2.0/window-opengl.php?

Specifically, I'm wondering how to draw a scene (built in JSFML Drawables) to a framebuffer that's supplied by LWJGL.

4
Graphics / Re: How to handle a Geometry Buffer with SFML?
« on: May 27, 2015, 08:43:25 pm »
I know what the code does, I was just wondering if I could replicate it in SFML. I understand exactly what it does.

I would rather keep using JSFML; is there any way I could mix opengl with it? I know you can with C++ SFML, but what about JSFML? Or is it all or none? By that, I mean, I want to keep the opengl abstraction for ease of use, but be able to use pure opengl when I need to. Is this possible?

I know this is suited for the other subforum, I'll look there.

5
Graphics / How to handle a GBuffer with SFML?
« on: May 27, 2015, 07:08:55 pm »
Hi, I'm working on implementing a deferred renderer in my SFML project.

However, there are some things that I am confused about. Specifically, I don't understand how to translate some OpenGL code I found on a tutorial (http://ogldev.atspace.co.uk/www/tutorial35/tutorial35.html) to SFML.

I think the main thing is the interaction between the draw buffer and the fragment shader.

In the tutorial, the init() function of the GBuffer is as follows:

bool GBuffer::Init(unsigned int WindowWidth, unsigned int WindowHeight)
{
    // Create the FBO
    glGenFramebuffers(1, &m_fbo);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);

    // Create the gbuffer textures
    glGenTextures(ARRAY_SIZE_IN_ELEMENTS(m_textures), m_textures);
    glGenTextures(1, &m_depthTexture);

    for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_textures) ; i++) {
       glBindTexture(GL_TEXTURE_2D, m_textures[i]);
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
       glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, m_textures[i], 0);
    }

    // depth
    glBindTexture(GL_TEXTURE_2D, m_depthTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WindowWidth, WindowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
                  NULL);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);

    GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
    glDrawBuffers(ARRAY_SIZE_IN_ELEMENTS(DrawBuffers), DrawBuffers);

    GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

    if (Status != GL_FRAMEBUFFER_COMPLETE) {
        printf("FB error, status: 0x%x\n", Status);
        return false;
    }

    // restore default FBO
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

    return true;
}

Now, I think I have most of that down, except for the DrawBuffer thing (where it attaches the draw buffers so that the fragment shader can output those textures to the buffer.)

Here is my implementation in JSFML, where fbo is a RenderTexture, and the other variables are Textures:

    public boolean init(int windowWidth, int windowHeight) {

        // create the FBO
        try {
            fbo.create(windowWidth, windowHeight);
        } catch (TextureCreationException ex) {
            System.err.println("Error creating F!");
            return false;
        }

        // create gbuffer textures
        for (int i = 0; i < textures.length; i++) {
            try {
                textures[i].create(windowWidth, windowHeight);
            } catch (TextureCreationException ex) {
                System.err.println("Error creating GBuffer textures!");
                return false;
            }
        }

        // depth
        try {
            depthTexture.create(windowWidth, windowHeight);
        } catch (TextureCreationException ex) {
            System.err.println("Error creating depth texture!");
            return false;        
        }
       
        return true;
    }

As you can see, I stop at the depth texture creation, because I do not understand how to initialize the draw buffer. Also, I feel like I need to properly attach those textures to the framebuffer. How would I do that?

Can anyone help me translate that code, or point out any errors I have already?

6
Alright! Would I use two sprites for this since there are two render textures?

7
Thank you for replying, I understand my problem better now.
Could you please tell me how to render those render textures to the window?

8
Here's the code in its entirety, for more context:

private static final int RTSIZE = 1024;
    private static float radius = 3;
    private static float MAX_BLUR = 3;

    public static void main(String[] args)  {
        RenderWindow rw = new RenderWindow();
        rw.create(new VideoMode(800, 600), "Gaussian Blur!");
       
        Texture t = new Texture();
        try {
            t.loadFromFile(Paths.get("textures/bg.jpg"));
        } catch (IOException ex) {
        }
        Sprite bgSprite = new Sprite(t);

        RenderTexture blurTargetA = new RenderTexture();
        try {
            blurTargetA.create(RTSIZE, RTSIZE);
        } catch (TextureCreationException ex) {
        }
       
        RenderTexture blurTargetB = new RenderTexture();
        try {
            blurTargetB.create(RTSIZE, RTSIZE);
        } catch (TextureCreationException ex) {
        }
       
        Shader blurShader = new Shader();
        try {
            blurShader.loadFromFile(Paths.get("src/shaderpractice/frag.glsl"), Shader.Type.FRAGMENT);
        } catch (IOException | ShaderSourceException ex) {
        }
       
        blurShader.setParameter("u_texture", t);
        blurShader.setParameter("dir", new Vector2f(0, 0));
        blurShader.setParameter("resolution", RTSIZE);
        blurShader.setParameter("radius", radius);
       
       
                while (rw.isOpen()) {
           
            Vector2f mousePos = rw.mapPixelToCoords(Mouse.getPosition());
           
            blurTargetA.clear();
            blurTargetA.draw(bgSprite);
            blurTargetA.display();
           
            blurShader.setParameter("dir", new Vector2f(1, 0));
           
            float mouseXAmt = mousePos.x / rw.getSize().x;
            blurShader.setParameter("radius", mouseXAmt * MAX_BLUR);
           
            blurTargetB.clear();
            blurTargetB.draw(bgSprite, new RenderStates(blurShader));
            blurTargetB.display();
           
            blurShader.setParameter("dir", new Vector2f(0, 1));
           
            float mouseYAmt = (rw.getSize().y-mousePos.y-1) / rw.getSize().y;
            blurShader.setParameter("radius", mouseYAmt * MAX_BLUR);
           
            rw.draw(bgSprite, new RenderStates(blurShader));
           
            rw.display();

            for (Event event : rw.pollEvents()) {
                if (event.type == Event.Type.CLOSED) {
                    rw.close();
                }
            }
        }
    }

9
I'm sorry, I forgot to include a few lines  before the rendering code, that I initalize the variables first:

Code: [Select]
                blurShader.setParameter("dir", new Vector2f(0, 0));
        blurShader.setParameter("resolution", RTSIZE);
        blurShader.setParameter("radius", radius);
       

where RTSIZE is 1024 and radius is 3.

10
I'm using this tutorial: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5#Overview

However, it's using LWJGL, and I'm using (J)SFML.

I know RenderTextures are FBO's, so that's not the problem, but I am having a little bit of a problem translating the code to SFML.

The code I have now:

~see below post for edited code~

All my shaders and textures are being loaded correctly, and I'm calling create() after I initialize them, with a size of 1024 (my screen size is 800x600).

Here's my fragment shader:

Code: [Select]
uniform sampler2D u_texture;
uniform vec2 dir;
uniform float resolution;
uniform float radius;

void main() {
   
    // RGBA sum
    vec4 sum = vec4(0.0);

    vec2 tc = gl_TexCoord[0].xy;

    float blur = radius/resolution;

    float hstep = dir.x;
    float vstep = dir.y;

    //apply blurring, using a 9-tap filter with predefined gaussian weights

    sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
    sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
    sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
    sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;

    sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.2270270270;

    sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
    sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
    sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
    sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;

    gl_FragColor = vec4(sum.rgb, 1.0);
}

When I launch the program, it just displays the background sprite. What am I doing wrong here?

11
Graphics / Re: Shader Filling Screen With White?
« on: May 17, 2015, 11:47:47 pm »
Sorry, I don't quite understand what to do with the shader.

Here's my changed code:

        greyscale.setParameter("texture", rtex.getTexture());

However, what do I change in the fragment shader to change the pixels to grey?

What do I need to add to this?

Code: [Select]
uniform sampler2D texture;

void main()
{
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);

    //Compute level of gray
    float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114));

    //Write destination color
    gl_FragColor = vec4(gray, gray, gray, gl_Color.a);
}

Edit:

Solved by changing the pixel line to this:

Code: [Select]
float gray = dot(pixel.rgb, vec3(0.299, 0.587, 0.114));

12
Graphics / Re: Shader Filling Screen With White?
« on: May 17, 2015, 10:48:51 pm »
The color isn't the problem, the problem is that the sprite isn't setting the correct texture (or the shader is wrong). Or I'm doing something terribly wrong.

13
Graphics / Shader Filling Screen With White?
« on: May 17, 2015, 09:48:13 pm »
This is in JSFML, but the syntax is quite similar and I know the chances of getting a response here are better.

I'm trying to apply a simple greyscale filter to my screen:

public class Window {

    private final RenderWindow rw;
    private Shader greyscale;
    private RenderTexture rtex;
    private Sprite shaderSprite;

    public Window(String title) {
        rw = new RenderWindow(new VideoMode(WindowConfig.WINDOW_WIDTH, WindowConfig.WINDOW_HEIGHT), title, WindowStyle.DEFAULT, new ContextSettings(2));
       
        State.setCurrentScreen(ScreenType.MAIN_MENU);
       
        if (!Shader.isAvailable()) {
            System.err.println("Hardware does not support shaders!");
            return;
        }
       
        greyscale = new Shader();
        try {
            greyscale.loadFromFile(Paths.get("shaders/screen.frag"), Shader.Type.FRAGMENT);
        } catch (IOException | ShaderSourceException ex) {
            ex.printStackTrace();
            return;
        }
       
        rtex = new RenderTexture();
        try {
            rtex.create(rw.getSize().x, rw.getSize().y);
        } catch (TextureCreationException ex) {
            ex.printStackTrace();
            return;
        }
       
        shaderSprite = new Sprite(rtex.getTexture());
    }

   
    public void display() {
        while (rw.isOpen()) {
            Screen s = State.getCurrentScreen();
            s.update();
           
            rtex.clear();
            rtex.draw(s);
            rtex.display();
           
            rw.clear();
            rw.draw(shaderSprite, new RenderStates(greyscale));
            rw.display();
           
            InputHandler.handleEvents(rw);
        }
    }

However, my shaderSprite is just a white image. I know this, because if I change the color of the sprite to black, the screen displays a black image. What am I doing wrong?

Here's my fragment shader:

Code: [Select]
void main()
{
    //Compute level of gray
    float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114));

    //Write destination color
    gl_FragColor = vec4(gray, gray, gray, gl_Color.a);
}

14
Graphics / Zooming in with a View - 'Corrupting' sprites?
« on: May 09, 2015, 04:15:11 am »
Hi, I have a curious problem.

In my game, I'm generating a 40 x 30 grid of sprites (as a 'dungeon'), each sized 32 x 32.

My window size is 640 x 480.

So, the view to see the entire dungeon would be 1280 x 960, with the center as 640 x 480.

That's fine, and that view works, nothing is corrupted.

However, when I try zooming in so I can see a part of the dungeon (in this case, changing the view size to 480 x 360, and the center to the player's position), my sprite's details are 'corrupted', where some lines are thicker than others.

Here's a few pictures to better illustrate my point.

Zoomed out (1280 x 960):



Zoomed in (480 x 360):



Can anyone explain what is happening here?

My code, for reference.

        if (viewToggle) {
            view.setSize(480, 360);
            view.setCenter(game.getPlayer().getPosition());
        } else {
            view.setSize(1280, 960);
            view.setCenter(640, 480);
        }
       
        rt.setView(view);
where 'rt' is the render target, first block is zoomed in, second block is zoomed out.

Edit:

Fixed by scaling to an integer value, e.g. 2x zoom as opposed to 2.67 zoom. Fractional pixels are bad.

15
Java / Re: Rotate a sprite around its center using a Transform
« on: May 04, 2015, 09:05:44 pm »
I assume Transform.rotate in Java follows the Immutable pattern and doesn't modify the argument. But you should double-check the documentation in any case. Same for the Transform default constructor, make sure it's the identity. Also check if you're not mixing coordinate systems (for example, the center is in local coordinates)...

Same for Transform.combine(t, states.transform), might it be that it applies the transforms in the wrong order? Because in C++, the states transform is multiplied with the current object's, see here for example.

Furthermore, you're throwing away the whole render states except for the transform. Even if it may not be relevant here, you should preserve the other attributes (texture, shader, blend mode).

In the JSFML documentation, Transform has many static methods that return a new Transform, using another Transform as an argument (see my code: t = Transform.rotate(...))

A new Transform is initialized to the identity, yes.

How would I get the center of the texture in global coordinates?

I'll try to make a proper constructor to preserve other rendering characteristics, then I'll get back to you.

Thanks so much for your help!

Edit:

Changed code in the OP to reflect current state.

Pages: [1] 2