viewing depth texture

seems im having issues copy the depth texture, or anything for that matter from the frame buffer to a texture…

  

void Draw2DQuad(float screenwidth, float screenheight, float xsize, float ysize)
{
	// some code to draw a quad with a texture so we can check... stuff
	glViewport(0, 0, screenwidth,screenheight);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0,screenwidth,screenheight,0,1,-1);

	glDisable(GL_CULL_FACE);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glDisable(GL_DEPTH_TEST);
	//glDisable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
	glNormal3f(0.0f,0.0f,1.0f);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	glTexCoord2f(0.0f,1.0f); glVertex2f(0.0f,0.0f);     
	glTexCoord2f(0.0f,0.0f); glVertex2f(0.0f,ysize);    
	glTexCoord2f(1.0f,0.0f); glVertex2f(xsize,ysize);  
	glTexCoord2f(1.0f,1.0f); glVertex2f(xsize,0.0f);    
	glEnd();
	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_DEPTH_TEST);

	glEnable(GL_CULL_FACE);
}

int main(int argc, char** argv) 
{
	//http://www.paulsprojects.net/tutorials/smt/smt.html

	Engine engine;

	Device* custom = new Device();
	custom->Create(512.0f, 512.0f);
	engine.SetDevice(custom);

	engine.Create();

	int windowWidth, windowHeight;
	engine.GetDevice()->GetDeviceSize(windowWidth, windowHeight);

	//load a model
	Mesh mesh;
	mesh.Load("data/scene.mdl");

	int shadowMapSize = 512;
	GLuint shadowTexture;
	Vector3 lightPos(5.0f, 5.0f, 0.0f);

	glEnable(GL_TEXTURE_2D);
	//Create the shadow map texture
	glGenTextures(1, &shadowTexture);
	glBindTexture(GL_TEXTURE_2D, shadowTexture);
	glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapSize, shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, shadowMapSize, shadowMapSize, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
	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);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	Camera light;
	light.SetLookAt(Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1));
	light.SetPosition(-10.0f, 0, 0);
	light.Transform();
    
	Matrix4 projection;
	projection.LoadCurrent(GL_PROJECTION_MATRIX);

	Matrix4 transform;
	double rotation = 0.0f;

	Camera camera;
	camera.SetLookAt(Vector3(0, 0, 1), Vector3(0, 1, 0), Vector3(1, 0, 0));
	camera.SetPosition(0, 0, 10.0f);
	camera.Transform();

	bool exit = false;
	while(!exit)
	{
		engine.Update();
		if (engine.GetInput()->KeyDown(SDLK_ESCAPE))
			exit = true;


		glEnable(GL_CULL_FACE);

		//We use glScale when drawing the scene
		glEnable(GL_NORMALIZE);

		engine.GetDevice()->BeginRender();

		rotation += 0.0000001f;
		transform.RotateY(rotation);

		
		//viewport need to be same size as shadow texture
		glViewport(0, 0, shadowMapSize, shadowMapSize);
		
		//Draw back faces into the shadow map
		glCullFace(GL_FRONT);

		//Disable color writes, and use flat shading for speed
		glShadeModel(GL_FLAT);
		glColorMask(1, 1, 1, 1);

		// render from light
		//light.LookAt();

		camera.LookAt();
		transform.SetM();
		mesh.RenderNoMat();

		//copy from frame buffer
		//Read the depth buffer into the shadow map texture
		glBindTexture(GL_TEXTURE_2D, shadowTexture);
		
	//	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, shadowMapSize, shadowMapSize, 0);
		glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, shadowMapSize, shadowMapSize, 0);

		//glBindTexture(GL_TEXTURE_2D, shadowTexture);
		Draw2DQuad(windowWidth, windowHeight, 100, 100);

		engine.GetDevice()->EndRender();
	}
	return 0;
}

i’ve tried just copying everything from frame buffer using GL_RGB instead of depth texture, but even that doesnt work, all i get is a white texture, no screen grab of the screen as expected :-/

It is possible that your texture (when you render it as RGB or whatever) is not all white, but rather that the range of values simply are not large enough to be visible. Try moving your near/far planes closer to your object before rendering it.

However, one thing that looks odd to me is calling glTexImage2D() first for the Depth Component then immediately for GL_RGB. That to me looks like you’re erasing what you originally setup. I could be wrong, maybe a texture can be both a Depth buffer and an RGB buffer, but it looks wrong to me.

oh i was exprimenting, same results with the RGB line commented out, i moved the model much closer, and it seems to be showing up now, but how can i change it so the texture ends up with depths the same as my scene? make if GL_FLOAT texture maybe ???

GL_FLOAT might (should?) help (I haven’t looked at using them). If you are doing shadow buffers then the greater the range of values you can cram into your depth texture the better. So when you are setting up the light frustum to render the depth buffer, it’s a good idea to adjust the near/far planes to maximize precision of those buffers.

Essentially GL_SHADOW_ARB will “see” exactly what you see. If there isn’t the precision to be able to make out your objects, then shadows won’t work (or at least they won’t be accurate - you’d get some serious artifacts). GL_FLOAT would give you a much greater range of values, and much better precision so I would expect that you’d get much better results, without having to move your near/far planes around. That’s assuming you can use them as depth buffers (Like I said, I haven’t looked at them).