How to render normals to texture via FBO?

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…

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.

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)

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

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.

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?

Ok, i got it. It was really pretty simple :slight_smile:
It seams to look right:
[ATTACH=CONFIG]442[/ATTACH]

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