Blending not working with lighting on!!

Started by
5 comments, last by esr_ever 17 years, 4 months ago
Hello, I've got a silly problem with an application I'm making, when I enable lighting, blending does not work, how can a glEnable/glDisable (GL_LIGHTING) affect blending?? while in blending mode, the only thing worth mentioning is that I use alpha testing for displaying alpha textures, does anybody have an idea what may be wrong?? If you need any more specific information I would be happy to send!!
Advertisement
At a guess, it's because you're using vertex colours to affect the alpha of your textures. When you switch on lighting vertex colours are ignored and material colours are used instead. Personally I think thats annoying, and the easiest way around it is to use GL_COLOR_MATERIAL so that material colours are picked up from vertex colours. Typical usage is something like:

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
I actually use blending to alpha fade between alpha textures and in the transition phase e.g. I use glColor(1,1,1,alpha1) for lod1 & glColor(1,1,1,alpha2) for lod2 in a GL_MODULATE environment. I don't want to use GL_COLOR_MATERIAL in the application,better change materials..So with lighting on, by changing the material color, the alpha fade would work in this case??
Yep. But because which material attribute you change - there are 4 colour factors and they all technically include the alpha channel =)

If you are using TexEnv stuff, im pretty sure you can still use the vertex colour channel with lighting enabled by using it as a source parameter. Not 100% sure on that though, i havent touched that stuff since fragment shaders were introduced.
I changed the material's alpha & still doesn't work..Any other ideas for what the problem might be?? & thanks for your responses so far!!
Can you post a bit of code?

Where are you calling the glMaterialf, and what are you changing?
ok, I've kept the most important part & trimmed it a little, here it goes :
(I use premultiplied alpha textures)

/************* Part of display func ***********/
glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
for(i=0;i<(uint)forest_treenum_val;i++)
{
dir = cam - CVert(for_pos[0],for_pos[1],for_pos[2]);
dirnorm = dir;
dirnorm.Normalize();
if(Dot(dirnorm,camnorm)>frcos)
{
dirsqlen = dir.SqLength();
SelectLOD(dirsqlen,forest_lodchange,forest_lodzones,perc,lval);
glPushMatrix();
glTranslatef(for_pos[0],for_pos[1],for_pos[2]);
//lod 1
if((lval[0] < config.lod_levels) && (lval[0] >= 0))
{
alphafades[0] = perc[0];
alphalod = 0;
glCallList(drawLODList[lval[0]]);
}
//lod 2
if((lval[1] < config.lod_levels) && (lval[1] >= 0))
{
alphafades[1] = perc[1];
alphalod = 1;
glCallList(drawLODList[lval[1]]);
}
glPopMatrix();
}
else
{
failed++;
}
}
glDisable(GL_BLEND);


/************** Part of display list *****************/

void drawTreeLOD_setup(int *lod_val)
{
glNewList(drawLODList[*lod_val],GL_COMPILE);
uint i,j,k,start=0,size=0;
glEnable(GL_TEXTURE_2D);
// For every Tree level
for(i=0;i<rep[*lod_val].repreNum;i++)
{
switch((int)rep[*lod_val].levelRepre)
{
case (int)geometry :
// Set texture & materials
glBindTexture(GL_TEXTURE_2D,BarkTex[(*(Tree.node_ID))]);
SetMaterialsAlpha(fdatapool.mat[3*(*(Tree.node_ID))],alphafades[alphalod]);
start = fdatapool.geom_lvl2lod_index[i*MAX_TREE_LVL + rep[*lod_val].geomLevel];
size = fdatapool.geom_lvl2lod_size[i*MAX_TREE_LVL + rep[*lod_val].geomLevel];
break;

case (int)texture :
if(Tree.nodetype == leaf)
{
glBindTexture(GL_TEXTURE_2D,LeafTex);
SetMaterials(fdatapool.mat[3*(fdatapool.matmax-2)]);
}
else
{
glBindTexture(GL_TEXTURE_2D,Releasetex);
SetMaterialsAlpha(fdatapool.mat[3*(fdatapool.matmax-1)],alphafades[alphalod]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
}
start = fdatapool.tex_lvl_index;
size = fdatapool.tex_lvl_size;
break;
}
for(j=start;j<(start+size);j++)
{

glBegin(GL_TRIANGLES);
for(k=0;k<fdatapool.indiv_data2_size[j];k++)
{
glNormal3fv((GLfloat *)fdatapool.indiv_data2[j].normals[k]);
glTexCoord2fv((GLfloat *)fdatapool.indiv_data2[j].st[k]);
glVertex3fv((GLfloat *)fdatapool.indiv_data2[j].mesh[k]);
}
glEnd();
}
}
glEndList();
if((*lod_val) == (int)(config.lod_levels - 1))
{
glNewList(drawLODList[config.lod_levels],GL_COMPILE);
glEndList();
}
}


/******************* Materials function ***********************/

void SetMaterialsAlpha(float *mat,float alpha)

{
float tmat[12] = {mat[0],mat[1],mat[2],alpha,
mat[4],mat[5],mat[6],alpha,
mat[8],mat[9],mat[10],alpha};

glMaterialfv(GL_FRONT, GL_AMBIENT, tmat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, &tmat[4]);
glMaterialfv(GL_FRONT, GL_SPECULAR, &tmat[8]);
}

This topic is closed to new replies.

Advertisement