Projective Texturing

Started by
3 comments, last by Boondock_Saint 13 years, 5 months ago
Following several tutorials I'm trying to implement Projective Texture mapping. First I tried the shader approach, which unfortunately did not work out. Now I'm trying it via the fixed functionality in OpenGL, following a tutorial on shadow mapping:

http://www.cc.gatech.edu/~jtan34/shadowMappingTutorial.html

The idea is to project a texture onto a simple cube. Where in my code below do I have to bind the actual texture? Because when the shadow map is bound, I cannot bind the texture anymore.

Also, I suspect that in my case I only have to render the scene twice: once for creating the depth map from the perspective of the projector and once from the perspective of the camera. As opposed to the tutorial where the scene is rendered three times. Currently the code follows the tutorial exactly.

I did find some info about projective texturing, however not a lot and also not very concrete. That is why I am using this shadow map tutorial.

void RenWin::drawGL() {	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	/* Step 1: Place camera at projector position and determine visibility */	// set projector viewport	glViewport(0, 0, m_pcProjector->getWidth(), m_pcProjector->getHeight());	// set projector projection matrix	Matrix4x4 cProjProjectionMat;	glMatrixMode(GL_PROJECTION);	glPushMatrix();	glLoadIdentity();	gluPerspective(30.0f, (GLfloat) m_pcProjector->getWidth() / (GLfloat) m_pcProjector->getHeight(), 0.1f, 400.0f);	glGetFloatv(GL_PROJECTION_MATRIX, cProjProjectionMat);	// set projector view matrix	Matrix4x4 cProjViewMat;	glMatrixMode(GL_MODELVIEW);	glPushMatrix();	glLoadIdentity();	gluLookAt(0.0f, 20.0f, -50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);	glGetFloatv(GL_MODELVIEW_MATRIX, cProjViewMat);	// draw scene	glShadeModel(GL_FLAT);	glColorMask(0, 0, 0, 0);	glutSolidCube(10.0f);	// pop state	glViewport(0, 0, m_uiWidth, m_uiHeight);	glMatrixMode(GL_PROJECTION);	glPopMatrix();	glMatrixMode(GL_MODELVIEW);	glPopMatrix();	/* Step 2: Create depth map*/	// create depth map	GLuint gluiDepthMapId;	glGenTextures(1, &gluiDepthMapId);	glBindTexture(GL_TEXTURE_2D, gluiDepthMapId);	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, m_pcProjector->getWidth(), m_pcProjector->getHeight(), 0);	// place camera	gluLookAt(0.0f, 0.0f, -50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);	// render again TODO: Necessary?	glShadeModel(GL_SMOOTH);	glColorMask(1, 1, 1, 1);	glClear(GL_DEPTH_BUFFER);	glEnable(GL_LIGHT1);	glEnable(GL_LIGHTING);	glutSolidCube(10.0f);	/* Step 3: Render illuminated part i.e. with texture */	// Set up texture matrix	Matrix4x4 cBiasMat(			0.5f, 0.0f, 0.0f, 0.0f,			0.0f, 0.5f, 0.0f, 0.0f,			0.0f, 0.0f, 0.5f, 0.0f,			0.5f, 0.5f, 0.5f, 1.0f	);	Matrix4x4 cTextureMat = cBiasMat * cProjProjectionMat * cProjViewMat;	// Set up OpenGL texture generation	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);	glTexGenfv(GL_S, GL_EYE_PLANE, cTextureMat[0]);	glEnable(GL_TEXTURE_GEN_S);	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);	glTexGenfv(GL_T, GL_EYE_PLANE, cTextureMat[1]);	glEnable(GL_TEXTURE_GEN_T);	glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);	glTexGenfv(GL_R, GL_EYE_PLANE, cTextureMat[2]);	glEnable(GL_TEXTURE_GEN_R);	glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);	glTexGenfv(GL_Q, GL_EYE_PLANE, cTextureMat[3]);	glEnable(GL_TEXTURE_GEN_Q);	glEnable(GL_TEXTURE_2D);	// Set up depth comparison	glBindTexture(GL_TEXTURE_2D, gluiDepthMapId);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);	glAlphaFunc(GL_GEQUAL, 0.99f);	glEnable(GL_ALPHA_TEST);	glutSolidCube(10.0f);	glutSwapBuffers();}
Advertisement
Something like this?
If you have it setup for shaders, that is the easier way to go. Your bias matrix is wrong, the .5 translation should be on the far right column not the bottom.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Quote:Original post by GenPFault
Something like this?


Thanks! I can't believe I missed that one :)

Quote:Original post by dpadam450
If you have it setup for shaders, that is the easier way to go. Your bias matrix is wrong, the .5 translation should be on the far right column not the bottom.


The bias matrix is actually correct because the matrix class is in column-major order. You are right, though: it looks confusing. I should rewrite that constructor :)

Concerning shaders: I only found this tutorial:

http://www.ozone3d.net/tutorials/glsl_texturing_p08.php

But I cannot get it to work. The first version I had actually worked (without using any tutorial). That communicated the projector matrices directly to the fragment shader. However, it needs to be projected on a model with a lot more polygons (600K) and suffered from severe aliasing effects. Which, I think, were in part also caused by the lack of depth clipping (it colored all pixels the camera could see, regardless whether the projector could 'see' them as well) and because I should have communicated the matrices to the vertex shader and let the GPU do the interpolation.

So what is unclear to me about the shader approach in the ozone3d tutorial: Does it do depth mapping? Do I only have to render it once?

My shader code according to the ozone3D tutorial can be found below. This doesn't work, i.e. the cube is green. The cube is only rendered once (that code is not shown, but basically just glutSolidCube()).

void Projector::setProjector(Matrix4x4& cCamExtrinsicMat) {	/* Setting Inverse Camera View Matrix */	cCamExtrinsicMat.invert();	GLint loc = glGetUniformLocation(m_gluiShaderProgram, "InvCamExtrinsicMat");	glUniformMatrix4fv(loc, 1, 0, cCamExtrinsicMat);	/* Projector Extrinsic Matrix */	glPushMatrix();	glLoadIdentity();	gluLookAt(0.0f, 20.0f, -50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);	Matrix4x4 cProjExtrinsicMat;	glGetFloatv(GL_MODELVIEW_MATRIX, cProjExtrinsicMat);	glPopMatrix();	/* Setting TexGenMatrix */	Matrix4x4 cTexGenMat = m_cBiasMat * m_cProjIntrinsicMat * cProjExtrinsicMat;	loc = glGetUniformLocation(m_gluiShaderProgram, "TexGenMat");	glUniformMatrix4fv(loc, 1, 0, cTexGenMat);}const GLchar* Projector::m_pglcaVertexSrc[] = {		"uniform mat4 TexGenMat;"		"uniform mat4 InvCamExtrinsicMat;"		"void main()"		"{"			"vec4 posEye = gl_ModelViewMatrix * gl_Vertex;"			"vec4 posWorld = InvCamExtrinsicMat * posEye;"			"gl_TexCoord[0] = TexGenMat * posWorld;"			"gl_Position = ftransform();"		"}"};const GLchar* Projector::m_pglcaFragmentSrc[] = {		"uniform sampler2D Image;"		"void main()"		"{"			// see if pixel is on the screen			"if( gl_TexCoord[0].q == 0.0 ) {"				"gl_FragColor = texture2DProj(Image, gl_TexCoord[0]);"			"}"			"else {"				"gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);"			"}"		"}"};
So what do you guys think concerning the shader code? Is it conceptually right and just needs some tweaking or is it fundamentally wrong?

Do I have to render it once when using shaders? I could really use that extra performance...

This topic is closed to new replies.

Advertisement