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.

Topics - tetra

Pages: [1]
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.

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,
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);

    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?

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?

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));
        if (!Shader.isAvailable()) {
            System.err.println("Hardware does not support shaders!");
        greyscale = new Shader();
        try {
            greyscale.loadFromFile(Paths.get("shaders/screen.frag"), Shader.Type.FRAGMENT);
        } catch (IOException | ShaderSourceException ex) {
        rtex = new RenderTexture();
        try {
            rtex.create(rw.getSize().x, rw.getSize().y);
        } catch (TextureCreationException ex) {
        shaderSprite = new Sprite(rtex.getTexture());

    public void display() {
        while (rw.isOpen()) {
            Screen s = State.getCurrentScreen();
            rw.draw(shaderSprite, new RenderStates(greyscale));

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);

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);
        } else {
            view.setSize(1280, 960);
            view.setCenter(640, 480);
where 'rt' is the render target, first block is zoomed in, second block is zoomed out.


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

Java / Rotate a sprite around its center using a Transform
« on: May 04, 2015, 08:31:32 pm »
I have an object that needs to be able to be rotated around its center, but keeping the same origin (for setting position, etc). So, I figured I needed to use a Transform.

I have a class, DoorTile, that extends Tile, that extends Drawable (just for context, not really relevant):

    private Transform t;

    public DoorTile() {
        t = new Transform();
    public void rotateAroundCenter(float degrees) {
        Vector2f center = SpriteUtils.getTextureCenter(TileTextures.DOORTEXTURE);
        t = Transform.rotate(t, degrees, center.x, center.y);
    public void draw(RenderTarget rt, RenderStates states) {
        RenderStates newStates = new RenderStates(
                Transform.combine(states.transform, t),
        sprite.draw(rt, newStates);

getTextureCenter(Texture t) returns the center coordinates of t, and sprite is the sprite of the Tile.

Now, when I apply this rotation, my sprites have a weird behavior. At first, using 45 degrees as a test, they completely disappeared from the screen. So then I tried 1 degree. The sprites affected seemed to have pieces "cut off" and translated downward. Here's a picture, using 1 degree as an argument:

Keep in mind, that I'm applying this rotation before setting their position on the map.

For context, I'm trying to rotate those white sprites (will be 'doors') so they fit properly in the corridor. E.g. horizontal on vertical cooridors, vertical on horizontal corridors. (The white part of the sprite is a placeholder for transparency, that's just there for debugging purposes.)

What should I do to be able to make this work properly? That is, how can I:

- Rotate a sprite around its center
- Keep the original origin (top left corner) for drawing


Pages: [1]