yesbutmaybeno
yesbutmaybeno

Reputation: 1148

Using deferred rendering, getting blank output

So I'm working with Java/LibGDX and I'm trying to set up the very basics of deferred rendering, namely rendering the actual game art to one color buffer of an FBO and the corresponding normals to another color buffer of the FBO.

(So essentially, I'm wanting to create this and this by using multiple-render-targets.)

My problem is my end output is blank, as if nothing is being rendered or is being rendered incorrectly.


My shader (vertex and fragment)

I'm sort of sure this works, since if I just render some sprites completely normally with it enabled to the screen (not an FBO), they do render.

#version 150

in vec4 a_position;
in vec4 a_color;
in vec2 a_texCoord0;

uniform mat4 u_projTrans;

out vec4 v_color;
out vec2 v_texCoords;

void main()
{
   v_color = a_color;
   v_color.a = v_color.a * (255.0/254.0);
   v_texCoords = a_texCoord0;
   gl_Position =  u_projTrans * a_position;
}



#version 150

#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif

in LOWP vec4 v_color;
in vec2 v_texCoords;

uniform sampler2D u_texture;
uniform sampler2D u_normal;

out vec4 fragColor;

void main()
{
    gl_FragData[0] = v_color * texture(u_texture, v_texCoords);
    gl_FragData[1] = texture(u_normal, v_texCoords);
}

The following code is in the render loop.

The basic idea is I'm doing binding the two color buffers of the FBO with glDrawBuffers. I then bind two texture units, game art and normal'd game art. My shader above is supposed to take this and output game art to one color buffer and the corresponding normal art to the other.

// Position the camera.
_cameraRef.position.set(_stage.getWidth() * 0.5f, _stage.getHeight() * 0.5f, 0);

// Update the camera, SpriteBatch, and map renderer.
_cameraRef.update();
_spriteBatch.setProjectionMatrix(_cameraRef.combined);
_mapRenderer.setView(_cameraRef);       

// Bind the color texture units of the FBO (multiple-render-targets).
Gdx.gl30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, _gBufferFBOHandle);
IntBuffer intBuffer = BufferUtils.newIntBuffer(2);
intBuffer.put(GL30.GL_COLOR_ATTACHMENT0);
intBuffer.put(GL30.GL_COLOR_ATTACHMENT1);
Gdx.gl30.glDrawBuffers(2, intBuffer);

// Draw!
Gdx.gl30.glClearColor(0, 0, 0, 1);
Gdx.gl30.glClear(GL30.GL_COLOR_BUFFER_BIT);

_spriteBatch.setShader(_shaderGBuffer);
_spriteBatch.begin();

_tilesAtlasNormal.bind(1); // Using multiple texture units to draw art and normals at same time to the two color buffers of the FBO.
_tilesAtlas.bind(0);
_mapRenderer.renderTileLayer(_mapLayerBackground);

_spriteBatch.end();
_spriteBatch.setShader(null);

// Bind the default FBO.
Gdx.gl30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);

// Draw the contents of the FBO onto the screen to see what it looks like.
Gdx.gl30.glClearColor(0, 0, 0, 1);
Gdx.gl30.glClear(GL30.GL_COLOR_BUFFER_BIT);

_spriteBatch.begin();
_spriteBatch.draw(_gBufferTexture1, 0.0f, 0.0f);  // <-- This texture is blank? It's the texture that was just rendered to above.
_spriteBatch.end();

So like I said, the end output is blank. I'm sure the FBO I create is valid since I check with glCheckFramebufferStatus.

It's just when I take the color texture(s) from that FBO and draw them to the screen, they're blank. I don't know where I'm going wrong.

Appreciate any input.

Upvotes: 1

Views: 630

Answers (2)

Ecumene
Ecumene

Reputation: 97

Don't forget to rewind your intbuffer:

IntBuffer intBuffer = BufferUtils.newIntBuffer(2);
intBuffer.put(GL30.GL_COLOR_ATTACHMENT0);
intBuffer.put(GL30.GL_COLOR_ATTACHMENT1);

intBuffer.rewind();

Upvotes: 0

PiotrJ
PiotrJ

Reputation: 151

Last Ive checked the gl30 wasnt really ready for prime time in LibGDX.

That said, Ive achieved exactly what you want, but in gl20. Even if you have other reasons to use gl30, perhaps you will find my implementation useful source, video.

Upvotes: 0

Related Questions