Jump to content
  • Advertisement
Sign in to follow this  
Gorgi

OpenGL Projective Texture Mapping with two textures

This topic is 4604 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

Hi there , this is my first post. :) I just started working with OpenGL and have problems with projecting two different textures on the same surface. Everything is ok when I project only one texture , but as soon as I try to project another one (which should partly overlap the first) on the same surface , it seems like only the second texture is applied.The textures are clamped to border color , and contain only luminance and alpha values (they are NOT RGBA). Here's the code: void display(void){ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); gluLookAt (Cpx, Cpy, Cpz, Cox, Coy, Coz, 0.0, 1.0, 0.0); glMatrixMode (GL_MODELVIEW); glTranslatef(0.0,0.0,-d); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL); glBindTexture(GL_TEXTURE_2D,texName1); glMatrixMode (GL_TEXTURE); glLoadIdentity (); glTranslatef(.5f, .5f, 0.f); glScalef(.5f, .5f, 1.f); gluPerspective (PRfov,Pratio , 0.95, 10.05); gluLookAt (PRx, Cpy, Cpz, PRox, PRoy, PRoz, 0.0, 1.0, 0.0); glMatrixMode (GL_MODELVIEW); glBindTexture(GL_TEXTURE_2D,texName2); glMatrixMode (GL_TEXTURE); glLoadIdentity (); glTranslatef(.5f, .5f, 0.f); glScalef(.5f, .5f, 1.f); gluPerspective (PLfov, Pratio , 0.95, 10.05); gluLookAt (PLx, Cpy, Cpz, PLox, PLoy, PLoz, 0.0, 1.0, 0.0); glMatrixMode (GL_MODELVIEW); glColor3f(1.0 , 1.0 , 1.0); glBegin(GL_QUADS); glVertex3f(-20 , 20, 0 ); glVertex3f( 20 , 20, 0 ); glVertex3f( 20 ,-20, 0 ); glVertex3f(-20 ,-20, 0 ); glEnd(); glFlush (); glDisable(GL_TEXTURE_2D); } Any clues?

Share this post


Link to post
Share on other sites
Advertisement
You forgot to switch the active texture unit between the texture setup calls.
use glActiveTexture(GL_TEXTURE0); and glActiveTexture(GL_TEXTURE1);
for it to work.

Share this post


Link to post
Share on other sites
Thanx for ur reply Niwak :) . I Googled it a bit and I changed my code according to what u said like this:







void display(void){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (Cpx, Cpy, Cpz, Cox, Coy, Coz, 0.0, 1.0, 0.0);
glMatrixMode (GL_MODELVIEW);
glTranslatef(0.0,0.0,-d);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);

PFNGLACTIVETEXTUREPROC glActiveTexture = (PFNGLACTIVETEXTUREPROC) wglGetProcAddress ("glActiveTexture");

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texNames[0]);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_BLEND);
glMatrixMode (GL_TEXTURE);
glLoadIdentity ();
glTranslatef(.5f, .5f, 0.f);
glScalef(.5f, .5f, 1.f);
gluPerspective (PRfov,Pratio , 0.95, 10.05);
gluLookAt (PRx, Cpy, Cpz, PRox, PRoy, PRoz, 0.0, 1.0, 0.0);
glMatrixMode (GL_MODELVIEW);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texNames[1]);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_BLEND);
glMatrixMode (GL_TEXTURE);
glLoadIdentity ();
glTranslatef(.5f, .5f, 0.f);
glScalef(.5f, .5f, 1.f);
gluPerspective (PLfov, Pratio , 0.95, 10.05);
gluLookAt (PLx, Cpy, Cpz, PLox, PLoy, PLoz, 0.0, 1.0, 0.0);
glMatrixMode (GL_MODELVIEW);

glColor3f(1.0 , 1.0 , 1.0);
glBegin(GL_QUADS);
glVertex3f(-20 , 20, 0 );
glVertex3f( 20 , 20, 0 );
glVertex3f( 20 ,-20, 0 );
glVertex3f(-20 ,-20, 0 );
glEnd();
glFlush ();
glDisable(GL_TEXTURE_2D);
}



The resuult is a black screen. :( If I comment the last part , everything is fine and the first texture maps ok, but as soon as I switch to GL_TEXTURE1 nothing works. A bit frustrating isn't it? Any further clues?

[Edited by - Gorgi on April 7, 2006 8:11:06 AM]

Share this post


Link to post
Share on other sites
Try this, replace glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL); with glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_ADD); (BTW GL_ADD = 0x0104) and enable blending (glEnable(GL_BLEND); glBlendFunc(GL_DST_COLOR,GL_SRC_COLOR);).
From what I know that should make things work fine.

Share this post


Link to post
Share on other sites
AxoDosS thanx for your reply too. :)

I do not think that the problem is the texture environment, even with the simple GL_DECAL enabled the second texture should just overwrite the first, assuming they are mapped to the same surface area (which they do). But instead it just blacks out my window. (BTW Clear color is set to (1,1,1,1) , not black... ).

Even so , just out of curiosity I tried your suggestions and the result was exactly what I awaited , the first texture blended in such a way , like it would do if it blended with a black texture.

It seems to me that the second texture unit (GL_TEXTURE1) doesn't work at all. I double checked if the textures are actually loaded into memory , and did also swap the order of the texture units (first GL_TEXTURE1 and then GL_TEXTURE0)which resulted again in the same black screen. It only works for one texture (the first or the second, which one doesn't matter) with only GL_TEXTURE0 !!!

I really don't understand , everyone is doing just the same and it works fine for them , why not for me? Is there any possibility of driver problem? My GPU is an 7800GT , I don't think it's outdated...

Someone help!

Share this post


Link to post
Share on other sites
If it is projective texture mapping that you are trying to do then you can take a look at this code that I wrote a while back, perhaps it will give you some clues to what your problem could be.

Share this post


Link to post
Share on other sites
am i blind or are you missing texture coordinates?, especially the little bit that for multitexturing you use glMultiTexCoord and not glTexCoord... but then i really dont see why you set the texturematrix in your example... and IMHO it shouldnt work that way at all (without texcoords or generating them that is)


T2k

Share this post


Link to post
Share on other sites
Quote:
Original post by T2k
am i blind or are you missing texture coordinates?, especially the little bit that for multitexturing you use glMultiTexCoord and not glTexCoord... but then i really dont see why you set the texturematrix in your example... and IMHO it shouldnt work that way at all (without texcoords or generating them that is)


T2k


Well , it does work , but only for the first Texture :) glMultiTexCoords are not needed as I'm doing projective texture mapping. They are automatically generated in another part of the code:

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); // set up automatic s texture coordinate generation
glTexGenfv(GL_S, GL_EYE_PLANE, Splane);
glEnable(GL_TEXTURE_GEN_S);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); // set up automatic t texture coordinate generation
glTexGenfv(GL_T, GL_EYE_PLANE, Tplane);
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); // set up automatic r texture coordinate generation
glTexGenfv(GL_R, GL_EYE_PLANE,Rplane);
glEnable(GL_TEXTURE_GEN_R);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); // set up automatic q texture coordinate generation
glTexGenfv(GL_Q, GL_EYE_PLANE, Qplane);
glEnable(GL_TEXTURE_GEN_Q);


As long as there is only one texture everything works fine , but as soon as I switch to the second texture unit nothing really works.

AxoDosS thanx for your code , It is really great but unfortunately it didn't help me find any answers because you are also using only one texture. Have u ever tried Projective texturing with more than one textures? Something like that would be most helpful...

Share this post


Link to post
Share on other sites
Nevermind People , Error found , and it was very stupid :P

Instead of declaring automatic Texture Coordinate Generation for every Texture unit seperately I did it only once, assuming that it is valid for all texture units. Instead the compiler assumed that I was refering to GL_TEXTURE0 only , that's why only the second texture wouldn't show up. The correct is:

glActiveTexture(GL_TEXTURE0)

..Generate your first texture coords here...

glActiveTexture(GL_TEXTURE1)

..Generate your second texture coords here...

and then do the rest as mentioned in the above posts. Thanx all anyway! :)

Share this post


Link to post
Share on other sites
Hi,

I am pretty much trying to do the same thing, but projecting shadow maps instead of normal textures. I have been rendering the scene each time I apply a new shadow map which is having a dramatic effect on performance. What exactly is required for:

PFNGLACTIVETEXTUREPROC glActiveTexture = (PFNGLACTIVETEXTUREPROC) wglGetProcAddress ("glActiveTexture");

I am getting an undeclared identifier on PFNGLACTIVETEXTUREPROC.

Thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!