views:

268

answers:

1

How i can disable lighting for only one of the textures in this multitexturing scheme? I tried to use glDisable(GL_LIGHTING) and glEnable(GL_LIGHTING) but it doesnt remember the settings when i render the quad.

Here is the code snippet:

glDisable(GL_LIGHTING);
glEnable(GL_BLEND);

glDisable(GL_TEXTURE_RECTANGLE_ARB);
glDisable(GL_TEXTURE_2D);

//------------------------
glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, LIGHT_MAP); // texture with black/white

//------------------------
glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, MY_TEXTURE); // normal texture
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD);

glColor4f(1, 1, 1, 1);

glBegin(GL_QUADS);
    glNormal3f(0,0,1);

    glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 0.0f);
    glMultiTexCoord2fARB(GL_TEXTURE1, 0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);

    glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 1.0f);
    glMultiTexCoord2fARB(GL_TEXTURE1, 0.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 0.0f);

    glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 1.0f);
    glMultiTexCoord2fARB(GL_TEXTURE1, 1.0f, 1.0f);
    glVertex3f(1.0f,  1.0f, 0.0f);

    glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 0.0f);
    glMultiTexCoord2fARB(GL_TEXTURE1, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();

glActiveTextureARB(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);

glActiveTextureARB(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D);

Edit: the texture loading:

glGenTextures(1, &LIGHT_MAP);
glBindTexture(GL_TEXTURE_2D, LIGHT_MAP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

Weird thing is that when i enable lighting at the first line of code, i cant see the light_map texture rendered at all in the multitexture quad. Also the lighting doesnt seem to affect that quad at all, it works on other quads (which arent multitextured) in my app.

Any help appreciated.

+1  A: 

Your first sentence does not make sense. GL Lighting happens at the vertex level. texturing, be it single texture or multitexture. happens way after. In more details:

The lighting that got computed per vertex is interpolated to each fragment, and that value is input to the texturing pipeline stage 0.

So... The lit color L comes in and gets combined with the texture on the texture stage 0 (your light map here), according to the texture environment of stage 0. You don't show how you set up the Texture environment for texture stage 0, so I can't tell you how it will pass down.

Then your output from stage 0 (computed from lighting and texture 0) get sent to the texture stage 1 input (where your "normal" texture is bound. That's called a diffuse texture). You use an environment of COMBINE with ADD on both RGB and ALPHA here, which is not typical of lightmapping: You're adding your lighting+lightmap to the diffuse color. Usually you want to multiply them (GL_TEXTURE_ENV_MODE=GL_MODULATE rather than GL_COMBINE).

Also, you did not set the SOURCE0_ALPHA and SOURCE1_ALPHA, so I'm not sure what exactly you add to generate the stage 1 ALPHA output.

All in all, I'd advise to write down exactly what you want as a result first (as math), and try to map that to the GL multitexturing pipeline.

Edit: Following your comments, if you don't specify stage 0 TexEnv, then you get the default:

GL_TEXTURE_ENV_MODE defaults to GL_MODULATE and GL_TEXTURE_ENV_COLOR defaults to (0, 0, 0, 0).

So your color coming from the lighting is modulated with your lightmap (which may make sense depending on what you're trying to achieve). That gets added to your diffuse (where, as I said, it should probably get modulated instead).

None of these change what I said earlier: write down exactly what math you would like for your result. From that we can help find which texture environment you may need. But you did not say what result you want to achieve.

Bahbar
so it is possible to make lightmap with this multitexturing scheme, just needs correct values to the glTexEnvi() function? but when i enable lighting it does not have effect... updated my first post with the code where i load the textures, not sure is that the "texture environment" you asked for
glTexEnv == Texture Environment. Each stage has a texture environment that defines how the stage combines with the previous color.
Bahbar
i dont use other glTexEnv() codes than what i already pasted there
"But you did not say what result you want to achieve", i just want to make lights on/off inside some building window texture. i guess the modulate is the right keyword then?
So, did you try replacing the Texture Environment for stage 1 with modulate rather than combine ?
Bahbar
yeah i think its working now, i can see other texture getting darker by lighting, thats pretty much everything i need... hmm, what are the defaults for gltexenv() functions, or is there some single function to reset them to default?
the defaults are described on the man page. > GL_TEXTURE_ENV_MODE defaults to GL_MODULATE and GL_TEXTURE_ENV_COLOR defaults to (0, 0, 0, 0).http://www.manpagez.com/man/3/glTexEnv/
Bahbar