Projective textures questions

Started by
4 comments, last by celestialVoid 20 years, 8 months ago
Hello, I've been trying to implement projective texture mapping recently, yet I don't know if I coded it correctly. You can see my code below. I have many questions now: 1.) Is the code even right? I mean the steps to project a texture. 2.) Please take a look at this (commented as "question 2" in the full code):

glMatrixMode (GL_TEXTURE);
glLoadIdentity ();
	glTranslatef (0.5f, 0.5f, 0.0f);
	glScalef (2.5f, 2.5f, 2.5f);
	gluPerspective (90.0f, 1.0f, 0.1f, 1.0f);
	glPushMatrix ();
glMatrixMode (GL_MODELVIEW);
I get VERY different results if I change values in glTranslate(), glScale() and gluPerspective(). And why texture gets bigger when values in glScale() are lower (and vice versa)? How do these values even affect the scene? Any advice how they should be set? 3.) Next piece of the code (commented as "question 3 in the full code):
       
glMatrixMode(GL_TEXTURE);
	glPopMatrix ();
	glTranslatef(0, 0, -1);
glMatrixMode(GL_MODELVIEW);
Again, results are very different if I change values in glTranslate (). Anyone who could explain? With different values I get everything from correct-looking projective texture mapping (but usually with too big textures) to situations where projected texture doesn't even fit on object it is casted (always looks the same). I don't really know why. I hope you know enough details. Any help would be highly appreciated. ---------------------------------------------------------------- void Render () { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); // bind texture which is projected glBindTexture (GL_TEXTURE_2D, tex2_proj->Tex); glMatrixMode (GL_TEXTURE); // question 2 glLoadIdentity (); glTranslatef (0.5f, 0.5f, 0.0f); glScalef (2.5f, 2.5f, 2.5f); gluPerspective (90.0f, 1.0f, 0.1f, 1.0f); glPushMatrix (); glMatrixMode (GL_MODELVIEW); glEnable (GL_TEXTURE_GEN_S); glEnable (GL_TEXTURE_GEN_T); glEnable (GL_TEXTURE_GEN_R); glEnable (GL_TEXTURE_GEN_Q); glTexGenf (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenf (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenf (GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenf (GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glDisable (GL_TEXTURE_GEN_S); glDisable (GL_TEXTURE_GEN_T); glDisable (GL_TEXTURE_GEN_R); glDisable (GL_TEXTURE_GEN_Q); glTexGenfv (GL_S, GL_EYE_PLANE, ps); glTexGenfv (GL_T, GL_EYE_PLANE, pt); glTexGenfv (GL_R, GL_EYE_PLANE, pr); glTexGenfv (GL_Q, GL_EYE_PLANE, pq); glMatrixMode (GL_TEXTURE); glLoadIdentity (); glMatrixMode (GL_MODELVIEW); // first pass: draw everything needed glBindTexture (GL_TEXTURE_2D, tex1->Tex); DrawWorld (); glEnable (GL_TEXTURE_GEN_S); glEnable (GL_TEXTURE_GEN_T); glEnable (GL_TEXTURE_GEN_R); glEnable (GL_TEXTURE_GEN_Q); glEnable (GL_BLEND); glBlendFunc (GL_ZERO, GL_SRC_COLOR); glDepthFunc (GL_EQUAL); glDepthMask (GL_FALSE); glEnable (GL_ALPHA_TEST); glAlphaFunc (GL_GREATER, 0.0f); glMatrixMode(GL_TEXTURE); // question 3 glPopMatrix (); glTranslatef(0, 0, -1); glMatrixMode(GL_MODELVIEW); // second pass: draw everything needed with projected texture glBindTexture (GL_TEXTURE_2D, tex2_proj->Tex); DrawWorld (); glDisable (GL_BLEND); glDepthFunc (GL_LESS); glDepthMask (GL_TRUE); glDisable (GL_ALPHA_TEST); } ---------------------------------------------------------------- [edited by - Wingman on August 6, 2003 6:51:02 AM]
Advertisement
First off your Translate and Scale are off. You are trying to start with a Matrix like this:
[ .5 0 0 .5]
[ 0 .5 0 .5]
[ 0 0 .5 .5]
[ 0 0 0 1]

after you have that, you need to multiply it by the light''s projection matrix. And finally you need to multiply it again by the light''s modelview matrix. I would just use gluLookAt though. So your code could end up looking like this:


glMatrixMode (GL_TEXTURE);	glLoadIdentity ();	glTranslatef (0.5f, 0.5f, 0.5f);	glScalef (0.5f, 0.5f, 0.5f);	glMultMatrixf(projectionMatrix.data);	gluLookAt(Position.data[0], Position.data[1], Position.data[2],                          front.data[0], front.data[1], front.data[2], 			  up.data[0], up.data[1], up.data[2]);glMatrixMode (GL_MODELVIEW);


I dont'' understand what you are trying to accomplish with glTranslatef(0, 0, -1); If you are trying to do the polygonOffset, then use glPolygonOffset(). You should use this before you render to the depthbuffer.

Hope that helps
Tnx, but I still need more info. I''d like to see an example (with all relevant code), ''cause I''m more and more confused.
Here are some places might help you out:

http://www.opengl.org/developers/code/sig99/advanced99/notes/node79.html
http://www.csie.nctu.edu.tw/~hhliao/slides/projtex/slides.cgi?page=1
http://users.ox.ac.uk/~univ1234/opengl/shadowmap/shadowmap.htm
http://developer.nvidia.com/object/Projective_Texture_Mapping.html
Thanks.
Have you tried cubemapping? Very easy to setup and use. See my pics.

Forged3D world editor

This topic is closed to new replies.

Advertisement