PDA

View Full Version : How to render normals to texture via FBO?



Alphaomega86
06-05-2013, 07:09 AM
Hallo!

One question. How can i render normals to a texture with a FBO?
I currently use this code:

normaltexture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, normaltexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Display.getWidth(), Display.getHeight(), 0, GL_RGBA, GL_FLOAT, (ByteBuffer) null);

glBindTexture(GL_TEXTURE_2D, 0);

And thats the way how i bind the texture to the FBO:

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normaltexture, 0);

But it does not seem to work. If i render the result texture to a quad like a simple post process, the screen gets black.
The default color texture and the depth texture work well, but, yeah, as i said, the normals texture won't work...

malexander
06-05-2013, 07:50 AM
Are you writing the normals to the second attachment in your fragment shader, and using glDrawBuffers() to draw to both color attachments beforehand? A look at your fragment shader would also help diagnose the problem.

Alphaomega86
06-06-2013, 11:42 AM
Thanks for your answer!

1.) I use "GL_COLOR_ATTACHMENT1" and not "GL_COLOR_ATTACHMENT0". i need "GL_COLOR_ATTACHMENT0" for the color texture.
2.) I use glDrawBuffers in the following way:
Pseudo Code:

Make DrawBufferArray from data [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_DEPTH_ATTACHMENT]
Set the draw buffers to DrawBufferArray
In LWJGL:

IntBuffer buffers = BufferUtilities.createIntBuffer(3);

buffers.put(GL_COLOR_ATTACHMENT0);
buffers.put(GL_COLOR_ATTACHMENT1);
buffers.put(GL_DEPTH_ATTACHMENT);

buffers.flip();
glDrawBuffers(buffers);
3.) I do not use a fragment shader. (Maybe this is the problem)

Alphaomega86
06-08-2013, 08:22 AM
Anyone here who can reference me to a normal render tutorial?

Alfonse Reinheart
06-08-2013, 09:37 AM
buffers.put(GL_DEPTH_ATTACHMENT);

glDrawBuffers is for color buffers. `GL_DEPTH_ATTACHMENT` is not a color buffer. So you're probably getting an error on this call.


Anyone here who can reference me to a normal render tutorial?

There generally aren't tutorials specifically for "writing a value that just so happens to be a normal." There are tutorials that do that, but it's nothing special or functionally different from writing any other value to a framebuffer.

Alphaomega86
06-09-2013, 07:19 AM
glDrawBuffers is for color buffers. `GL_DEPTH_ATTACHMENT` is not a color buffer. So you're probably getting an error on this call.
Ok, i removed the 'GL_DEPTH_ATTACHMENT'.

The problem is, all i know is, that i need to render the normal data into a framebuffer. But i have no idea if this is a post processing fragment shader, or not...
Anyone who can teach me something about rendering normals to a texture?

Alphaomega86
06-09-2013, 07:53 AM
Ok, i got it. It was really pretty simple :)
It seams to look right:
1052

My Vertex Shader:

#version 120

varying vec3 position;
varying vec3 normal;

void main(void) {
gl_FrontColor = gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;

position = vec3(gl_ModelViewMatrix * gl_Vertex);
normal = normalize(gl_NormalMatrix * gl_Normal);
}

My Fragment Shader:

#version 120

varying vec3 position;
varying vec3 normal;

void main(void) {
gl_FragData[0] = gl_Color;
gl_FragData[1] = vec4(normal, 1.0);
gl_FragData[2] = gl_Color;
}