Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: How to render normals to texture via FBO?

  1. #1
    Intern Newbie
    Join Date
    Mar 2013
    Posts
    42

    Question 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:
    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:
    Code :
    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...

  2. #2
    Member Regular Contributor malexander's Avatar
    Join Date
    Aug 2009
    Location
    Ontario
    Posts
    317
    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.

  3. #3
    Intern Newbie
    Join Date
    Mar 2013
    Posts
    42
    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)

  4. #4
    Intern Newbie
    Join Date
    Mar 2013
    Posts
    42
    Anyone here who can reference me to a normal render tutorial?

  5. #5
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    Code :
    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.

  6. #6
    Intern Newbie
    Join Date
    Mar 2013
    Posts
    42
    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?

  7. #7
    Intern Newbie
    Join Date
    Mar 2013
    Posts
    42
    Ok, i got it. It was really pretty simple
    It seams to look right:
    Click image for larger version. 

Name:	Normals.jpg 
Views:	126 
Size:	8.5 KB 
ID:	1052

    My Vertex Shader:
    Code :
    #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:
    Code :
    #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;
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •