PDA

View Full Version : problem : gl_lighting mystery



HamsterofDeath
02-09-2003, 04:04 AM
working : i'm using 3 textures (unit 0,1,2), blending them together with coverage factors stored in texture unit 5, combining with a noise texture (unit 4) and a basetex (unit 3).

all objects are depth-sorted (nearest first)

all near objects, those which use the first 3 textures, are 100% bright (lighting doesn't affect them).
the rest is fine.

if i disable the 3 detailtextures at the beginning, the light affects NOTHING...everything bright.

it seems that it's nessesary to draw something, then disable some texture units, and then, the lighting works...

maybe i'm not seeing a stupid bug :-|

here's the code (pretty long, but pretty simple)



private void setCoverMode() {
float TexCoordX[] = {0.5f, 0.0f, 0, 0.0f};
float TexCoordY[] = {0, 0.0f, 0.5f, 0.0f};
float TexCoordX2[] = {0.5f, 0.0f, 0, 0.0f};
float TexCoordY2[] = {0, 0.0f, 0.5f, 0.0f};
float TexCoordX3[] = {0.4f, 0.0f, 0, 0.0f};
float TexCoordY3[] = {0, 0.0f, 0.4f, 0.0f};
float TexCoordX4[] = {1f / (mapSize * mapScaleFactor), 0, 0f, 0.0f};
float TexCoordY4[] = {0, 0f, 1f / (mapSize * mapScaleFactor), 0.0f};
float TexCoordX5[] = {0.03f, 0.0f, 0, 0.0f};
float TexCoordY5[] = {0, 0.0f, 0.03f, 0.0f};
//landscape

//tex2 - stonedetail
gl.glActiveTextureARB(GL_TEXTURE2_ARB);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[earthDetailTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);
gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX3);
gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY3);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_COMBINE_ARB);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_COMBINE_RGB_ARB, gl.GL_INTERPOLATE_ARB);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE0_RGB_ARB, gl.GL_PREVIOUS_ARB);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_OPERAND0_RGB_ARB, gl.GL_SRC_COLOR);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE1_RGB_ARB, gl.GL_TEXTURE);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_OPERAND1_RGB_ARB, gl.GL_SRC_COLOR);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE2_RGB_ARB, gl.GL_TEXTURE5_ARB);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_OPERAND2_RGB_ARB, gl.GL_SRC_ALPHA); //gl.glCallList(displayList[0]);

//tex1 - grasdetail
gl.glActiveTextureARB(GL_TEXTURE1_ARB);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[grasDetailTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);
gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX2);
gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY2);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_COMBINE_ARB);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_COMBINE_RGB_ARB, gl.GL_INTERPOLATE_ARB);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE0_RGB_ARB, gl.GL_PREVIOUS_ARB);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_OPERAND0_RGB_ARB, gl.GL_SRC_COLOR);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE1_RGB_ARB, gl.GL_TEXTURE);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_OPERAND1_RGB_ARB, gl.GL_SRC_COLOR);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE2_RGB_ARB, gl.GL_TEXTURE5_ARB);
gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_OPERAND2_RGB_ARB, gl.GL_SRC_COLOR); //gl.glCallList(displayList[0]);

//tex0 - earthdetail
gl.glActiveTextureARB(GL_TEXTURE0_ARB);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[stoneDetailTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX);

gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

//basistextur
gl.glActiveTextureARB(GL_TEXTURE3_ARB);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[worldTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX4);

gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY4);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

//tex4 - noise texture
gl.glActiveTextureARB(GL_TEXTURE4_ARB);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[randomnoiseTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX5);

gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY5);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, 2);

//tex5 - covertex
gl.glActiveTextureARB(GL_TEXTURE5_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[coverTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX4);

gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY4);
}

private void setLowDetailLandscape() {

float TexCoordX4[] = {1f / (mapSize * mapScaleFactor), 0, 0f, 0.0f};
float TexCoordY4[] = {0, 0f, 1f / (mapSize * mapScaleFactor), 0.0f};
float TexCoordX5[] = {0.03f, 0.0f, 0, 0.0f};
float TexCoordY5[] = {0, 0.0f, 0.03f, 0.0f};
gl.glActiveTextureARB(GL_TEXTURE0_ARB);
gl.glEnable(GL_LIGHTING);
gl.glDisable(GL_BLEND);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[worldTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX4);

gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY4);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

//tex4 - noise texture
gl.glActiveTextureARB(GL_TEXTURE1_ARB);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[randomnoiseTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX5);

gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY5);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, 2);

}

private void setUglyMode() {

float TexCoordX4[] = {1f / (mapSize * mapScaleFactor), 0, 0f, 0.0f};
float TexCoordY4[] = {0, 0f, 1f / (mapSize * mapScaleFactor), 0.0f};
gl.glActiveTextureARB(GL_TEXTURE0_ARB);
gl.glEnable(GL_LIGHTING);
gl.glDisable(GL_BLEND);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[worldTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX4);

gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY4);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}

private void buildList(SinglePolygon[] polySoup, boolean build) {

if (build) {

} else {


gl.glPushMatrix();
switch (quality) {
//1 texturunit - voodoo 1 etc.
case 0 : {
setUglyMode();
gl.glEnable(GL_LIGHTING);

buff = octree.getVisibles(frustum, playerView);
for (int i = 0; i < buff.length; i++) {
GLDraw.executeStrips(buff[i].getStrips());
}
break;
}
//2 units - voodoo 2, riva tnt usw
case 1 : {
setLowDetailLandscape();
gl.glEnable(GL_LIGHTING);
buff = octree.getVisibles(frustum, playerView);
for (int i = 0; i < buff.length; i++) {
GLDraw.executeStrips(buff[i].getStrips());
}
break;
}
//alle details - radeon 8500 ++
case 2 : {
gl.glEnable(GL_LIGHTING);
setCoverMode();
int nearestNodeIndex=0;
int maxDst = viewDistance * viewDistance / 50;
buff = octree.getVisibles(frustum, playerView);
boolean detailTex = true;
for (int i = 0; i < buff.length; i++) {
if (detailTex) {
GLDraw.executeStrips(buff[i].getStrips());
if (buff[i].getDst() > maxDst) {
nearestNodeIndex = i;
detailTex = false;
gl.glActiveTextureARB(GL_TEXTURE0_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glActiveTextureARB(GL_TEXTURE1_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glActiveTextureARB(GL_TEXTURE2_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glEnable(GL_LIGHTING);
}
} else {
GLDraw.executeStrips(buff[i].getStrips());
}
}

break;
}


}
gl.glActiveTextureARB(GL_TEXTURE3_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glActiveTextureARB(GL_TEXTURE4_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glActiveTextureARB(GL_TEXTURE5_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glActiveTextureARB(GL_TEXTURE2_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glActiveTextureARB(GL_TEXTURE1_ARB);
gl.glDisable(GL_TEXTURE_2D);
gl.glActiveTextureARB(GL_TEXTURE0_ARB);
gl.glDisable(GL_TEXTURE_2D);

gl.glPopMatrix();





[This message has been edited by HamsterofDeath (edited 02-09-2003).]

HamsterofDeath
02-09-2003, 07:42 AM
what could be the reason that lighting is disabled, no matter if i enable lighting ?

JustHanging
02-09-2003, 07:48 AM
Hello again http://www.opengl.org/discussion_boards/ubb/smile.gif

Without having given a single look at the code I'd say you don't understand how the lighting fits into the combiner system. If you use opengl lighting, the light intensities will go to the primary color. If you want it to affect your textures, you must modulate the blended textures with these values.

In practice, setup an additional combiner stage after the ones you are using, and use it to modulate previous color with primary color.

-Ilkka

HamsterofDeath
02-09-2003, 08:14 AM
i try to reduce the code as much as possible :



private void setUglyMode() {

float TexCoordX4[] = {1f / (mapSize * mapScaleFactor), 0, 0f, 0.0f};
float TexCoordY4[] = {0, 0f, 1f / (mapSize * mapScaleFactor), 0.0f};
gl.glActiveTextureARB(GL_TEXTURE0_ARB);
gl.glEnable(GL_LIGHTING);
gl.glDisable(GL_BLEND);
gl.glEnable(GL_TEXTURE_2D);
gl.glBindTexture(GL_TEXTURE_2D, texture[worldTex]);
gl.glEnable(GL_TEXTURE_GEN_S);
gl.glEnable(GL_TEXTURE_GEN_T);

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_S, GL_OBJECT_PLANE, TexCoordX4);

gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
gl.glTexGenfv(GL_T, GL_OBJECT_PLANE, TexCoordY4);
gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}

here i just bind a single texture



setUglyMode();
buff = octree.getVisibles(frustum, playerView);
for (int i = 0; i < buff.length; i++) {
gl.glEnable(GL_LIGHTING);
GLDraw.executeStrips(buff[i].getStrips());
}

and here i simply draw triangles

lighting is disabled. what could be the reasons ? there are no coverage factors, nothing...

if i disable texturing
gl_disable(gl_texture_2d) in the code above, lighting works, but everythign is grey (as it should be)

[This message has been edited by HamsterofDeath (edited 02-09-2003).]

PH
02-09-2003, 08:19 AM
This line,

gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

REPLACE should be MODULATE ( LightValue*Texture ).

HamsterofDeath
02-09-2003, 08:20 AM
ah, i got it :
opengl draws only the lightvalue, if theres no texture.
if there is one, i have to set the texture to combine or modulate to combine it with the "lightmap"

HamsterofDeath
02-09-2003, 08:44 AM
ok, lighting works.
next (and last) problem :
how to make my coverage factors work with reading the rgb & alpha-values from a textureunit ?
i have never done this before, and i haven't found any code that does this, so my code posted above (setCoverMode) is just a guess

the main problem is that the first detail texture uses gl_replace in the delphi3d-arcticle. i don't know what happens if i set it to modulate...