0
votes

I want to render the depth buffer to a texture, and draw a quad with the DepthTexture. But it doesn't work.

First i make DepthTexture:

glGenTextures(1, &_iTexDepth);
glBindTexture(GL_TEXTURE_2D, _iTexDepth);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, _w, _h, 0, 
    GL_DEPTH_COMPONENT,  GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

and prepare to render:

glViewport(0, 0, _w, _h);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(camera_proj);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(camera_modelview);
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_PIXEL_MODE_BIT); 
    glDrawBuffer(GL_BACK);
    glReadBuffer(GL_BACK);
glClearColor(0, 0, 0, 1);
glClearDepth(1.0f); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

and render something, and copy to the texture:

glBindTexture(GL_TEXTURE_2D, _iTexDepth);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, _w, _h);
glBindTexture(GL_TEXTURE_2D, 0);

and last, i bind _iTexDepth and draw a Quad

This code works with GL_RGBA8 Texture well, but does not work with GL_DEPTH_COMPONENT (white only with DEPTH_COMPONENT)

If you have some idea about this problem, i want some help

P.S. I don't want to use FBO

1
Why don't you want to use an FBO? It would be the easiest way to go. - K_Finlay
this is part of my graduation project, and i have to present this.but present room's PC is not support FBO. so i want make both(FBO, CopyTexture) - YeomHyeonSeop

1 Answers

0
votes

If your texture turns out all white, it is indeed being rendered correctly.

The problem, I think, is due to the setting of the far and near planes in your projection matrix. The depth buffer isn't linear, and if your near plane is very close, like 0.1 and your far plane far like 500.0 then a lot of the possible depth values are VERY close to your near plane.

It's better to set your near plane to something like 2.0 or even higher. Experiment with different near/far plane values and let me know if you're still having problems.

As for your English, it's fine! As an ex-private English teacher for people in Japan your standard is pretty good.