OpenGL: DepthTest at FBO

Mar 21, 2016 at 8:44pm
Hi There,

i need some help with OpenGL offscreen-rendering.
I have a simple test-program, rendering a textured cube to a textture, which is used for a cube again. Unfortuately the depth-test works fine on cube rendered directly to the output, but not on the cube rendered offcreen.

Here ist the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    int w, h;           GetClientSize(&w, &h);

    glDepthFunc     (GL_LESS);                   // The Type Of Depth Test To Do
    glDepthMask     (GL_TRUE);
    glEnable        (GL_DEPTH_TEST);


    glBindFramebuffer(GL_FRAMEBUFFER, FBO);
    
    
    glClearColor    (1, 1, 1, 1);
    glClear         (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
    glMatrixMode    (GL_MODELVIEW);         glLoadIdentity();
    gluPerspective  (45.0, (float)w / (float)h, 1.0, 100.0);

    glPushMatrix    ();
    
    glTranslatef    (-2.0f, 0, -18.0f);
    glRotatef       (-RotX, 0.0, 1.0, 0.0);
    glRotatef       (RotY, 1.0, 0.0, 0.0);


    glBindTexture   (GL_TEXTURE_2D, texture[1]);
    
    glBegin(GL_QUADS);

[...RENDER CUBE TO TEXTURE...]

    glEnd();
    glPopMatrix();
    glFlush();              // Flush the GL Pipeline
      
    

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    

    glActiveTexture(GL_TEXTURE0);


    glViewport      (0, 0, w, h);


    glClearColor(0, 0, 0, 1);
    glClear         (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    
    glMatrixMode    (GL_MODELVIEW);         glLoadIdentity();
    gluPerspective  (45.0, (float)w / (float)h, 1.0, 100.0);

     //glPushMatrix();

    glTranslated    (0.0, 0.0, Depth);

    glRotatef       (RotY, 0.0, 1.0, 0.0);
    glRotatef       (RotX, 1.0, 0.0, 0.0);

      

    glBindTexture   (GL_TEXTURE_2D, texture[0]);
    glMaterialf     (GL_FRONT_AND_BACK, GL_SHININESS, 0);


    glBegin(GL_QUADS);

[...RENDER CUBE TO OUTPUT...]

    glEnd();


    glFlush();              // Flush the GL Pipeline
    SwapBuffers();          // Swap Buffers 



Any idea what is worng?

Regards,
Frank
Last edited on Mar 21, 2016 at 8:56pm
Mar 22, 2016 at 9:53am
Depth testing on a FBO works slightly differently - you need to add a depth attachment to your buffer: https://www.opengl.org/wiki/Framebuffer_Object#Framebuffer_Object_Structure

As a side note, this looks like some pretty old (i.e. fixed-function pipeline) OpenGL code. I'd recommend looking at some more modern OpenGL tutorials (programmable pipelines have been around since about 2004).
Mar 22, 2016 at 10:49am
Thanks for the help!
I inserted
1
2
3
4
glGenRenderbuffers(1, &renderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);

with renderBuffer as GLuint*, into the function setting up the frambuffer, and it workd.


Concerning 'pretty old': you're probably right. Also i'm a bit confused about all the library-versions etc., so also my librarys might be old.
Last week i tried to get some help, in setting up my IDE on a new computer, but after some days of waiting for answers, i figured out an own setup by trial and error. This was on sunday and what you see is my first OpenGL-program after 2 years pause - and the first one with working FBO-rendering.
So i'm quite happy with my progresses ;)

Mar 22, 2016 at 11:52am
No worries, glad you got it sorted.

With regards to the libraries, you're possibly using a more modern version than you think. Even if you're not, chances are you still have a version that supports the likes of shaders (they came in v2.0). You can query this with glGetString.

For example:
glGetString(GL_VERSION);

To be honest, the only reason I really mentioned it is because I find modern OpenGL to be a lot more:
a) powerful
b) easy
c) fun

And you should absolutely be happy with your progress. :-)

Topic archived. No new replies allowed.