View Full Version : GLSL create and return a Normal map?

12-09-2010, 03:10 AM
Hi there,

I would like to be able to use the interpolated normal in the fragment shader and create a normal map, which can then be read back on the program side.

I've considered the indirect method of picking (Get Normal and TextureCoordinate by mouse by ray picking (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=168930)) and reconstruction of normals using adjacent pixels and depth map (glReadPixels (...GL_DEPTH_COMPONENT, GL_FLOAT, z_buffer)). These have their own problems.

I would just like to know what mechanisms there are for reading back data from the GPU using GLSL. From Buffer Object (http://www.opengl.org/wiki/Buffer_Object):

The GL_TRANSFORM_FEEDBACK_BUFFER (core in versions 3.0 and above) binding is used to implement storing the results of the vertex (and geometry, where available) shader in a buffer object. This binding point is the buffer that gets written into. I just don't see how to find a link between these two API's GLSL and OpenGL to describe a scenario like:

OGL (once)

Create buffer ( size = width * height * 3 * GL_FLOAT, GLSL_WRITE_ONLY | OGL_READ_ONLY )


normal = normalize(gl_NormalMatrix * gl_Normal);
pixel_index = y * width + x;
normalmap( pixel_index ) = normal;


do stuff with normalmap

Note: asynchronous transfers are good enough.

So can anyone make a suggestion on ways to go with GLSL and readback?

Thanks in advance

Alfonse Reinheart
12-09-2010, 03:30 AM
If you want to render a normal map, then bind a floating-point render target, write normals in your fragment shader, and then read the pixel values back as floating-point values.

12-09-2010, 04:00 AM
Hi Alfonse, thanks for the response.

I felt so n00b, because i had just thought of re-rendering the scene using a different shader with:
gl_FragColor = vec4(normal, 1.0);
I believe you are suggesting this can be done in a single rendering pass using two render targets? how would one go about doing that these days?