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]