Archived

This topic is now archived and is closed to further replies.

depthmap texture

This topic is 5298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in order to make shadowmapping i have to generate a depthmap texture (near objects are darker) i take the light point of view and do a glReadPixels, then i bind this image to a texture... but it doesn''t work the depthmap texture is empty here is the code : /////////////////////////////////////////////////////////////// UINT depthMapTexture; glGenTextures(1,&depthMapTexture); void updateDepthMap(void) { static GLubyte *depthBuffer = NULL; int depthBufferSize = 0; static int lastDepthBufferSize = 0, lastWidth = 0, lastHeight = 0; static int bytesPerDepthValue = 1; static const GLfloat swBorderColor[] = {1.0,0.0,0.0,1.0}; glDisable(GL_CULL_FACE); //no collision bugs glColorMask(0,0,0,0); // colors off glDisable(GL_LIGHTING); //light off glEnable(GL_POLYGON_OFFSET_FILL); //If enabled, and if the polygon is rendered in GL_FILL mode, an offset is added to depth values of a polygon''s fragments before the depth comparison is performed glViewport(0,0,width,height); setupInvLightView(); //light point of view drawScene(); depthBufferSize = width*height*bytesPerDepthValue; if (depthBufferSize > lastDepthBufferSize) { depthBuffer=(GLubyte *)malloc(depthBufferSize); if (depthBuffer == NULL) { fprintf(stderr, "shadowcast: depth readback buffer realloc failed\n"); exit(0); } lastDepthBufferSize = depthBufferSize; } glPixelStorei(GL_UNPACK_ALIGNMENT,1); glPixelStorei(GL_PACK_ALIGNMENT,1); glReadPixels(0,0,width,height,GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE,depthBuffer); glBindTexture(GL_TEXTURE_2D,depthMapTexture); if ((width != lastWidth) || (height != lastHeight)){ //glTexImage2D(GL_TEXTURE_2D,0,GL_INTENSITY8,width,height,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,depthBuffer); glTexImage2D(GL_TEXTURE_2D,0,GL_ALPHA,width,height,0,GL_ALPHA,GL_UNSIGNED_BYTE,depthBuffer); glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR, swBorderColor); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //GL_LINEAR ou GL_NEAREST glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); lastWidth = width; lastHeight = height; } else { //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, depthBuffer); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_ALPHA, GL_UNSIGNED_BYTE, depthBuffer); } //delete [] depthBuffer; //on remet les parametres d''avant glDisable(GL_POLYGON_OFFSET_FILL); glEnable(GL_LIGHTING); glColorMask(1,1,1,1); glEnable(GL_CULL_FACE); } /////////////////////////////////////////////////////////////// ps : GL_DEPTH_TEST is enabled Do you find my errors ?? thx. (sorry for bad english)

Share this post


Link to post
Share on other sites
this could be because the depth buffer doesn''t grow linearly and your objects are near the back of your view frustrum (so all your rendered objects depth will be near one, hence the white texture) try moving you near buffer further and further back towards he far plane, that might help. you could also try placing an object at the near view frusturm (as close as you can infront of the camera) Aslo contributing to this problem is that the precision of the depth buffer is only 1 byte so many values in your texture that should have different values all end up with the same value. If you still dont get an image then it''s probably something in your code, but it realy sounds like a precision problem

Share this post


Link to post
Share on other sites
" try moving you near buffer further and further back towards he far plane"

oops i ment near plane (not near buffer)

----


cool let me know if it helps, i'm curious if this is your problem as well. If so there are some extensions that can help, if your graphics card supports them.

[edited by - _walrus on June 6, 2003 4:37:40 PM]

Share this post


Link to post
Share on other sites
i've moved the planes but it still doesn't work

http://perso.wanadoo.fr/jio/bug_texture.jpg

in the top-right corner of the picture u can see the "depth texture" corresponding to the 3d scene (in bottom-left corner).... it's all gray

the near plane is at 0.5 and the far plane is at 10.0, i.e. just behind the farest object... j'comprend pooooOOO ouiiiin :'(

[edited by - jio on June 11, 2003 8:45:09 AM]

Share this post


Link to post
Share on other sites
the only difference i noticed with ur code and some code i threw together was that i was reading into floats array as opposed to an byte array:

glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, fbytes);


and i''m using GL_LUMINANCE as the internal format, (GL_ALPHA gave be a black texture; this is because 0 is copyed into the rgb component of the texture and the elements of fbytes will be copyed into the alpha channel.)

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_LUMINANCE, GL_FLOAT, fbytes);

Other than that i cant notice anything wrong, if the above dosent get you an image then also make sure your reading from the correct buffer (front, back..etc) and that the block with glTexImage2D gets called.

Also should the line:
if (depthBufferSize > lastDepthBufferSize) {
be
if (depthBufferSize != lastDepthBufferSize) {

hope this can help you

Share this post


Link to post
Share on other sites
on a side note, this will be a very slow method since ur reading back from the gpu (glReadPixels) into main memory and then resending to texture memory on gpu. There are some extesions that should help you speed things up, if your graphics cards supports them

Share this post


Link to post
Share on other sites
thx a lot
it works now

but i still have a little problem..


UINT depthMapTexture;
glGenTextures(1,&depthMapTexture);
.
.
.
glReadPixels(0,0,width,height,GL_DEPTH_COMPONENT,GL_FLOAT,depthBuffer);
//depthbuffer contains the image
glBindTexture(GL_TEXTURE_2D,depthMapTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height,0, GL_LUMINANCE, GL_FLOAT, depthBuffer);
.
.
.
glBindTexture(GL_TEXTURE_2D, depthMapTexture);
glBegin(GL_QUADS);
...
glEnd();

//the quad is not textured

ps : i have to make a program that runs with any graphic card.. so i can''t use SGIX or NV extensions ^^

Share this post


Link to post
Share on other sites