Sign in to follow this  
esr_ever

Blending not working with lighting on!!

Recommended Posts

esr_ever    130
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!!

Share this post


Link to post
Share on other sites
OrangyTang    1298
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);

Share this post


Link to post
Share on other sites
esr_ever    130
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??

Share this post


Link to post
Share on other sites
Digitalfragment    1504
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.

Share this post


Link to post
Share on other sites
esr_ever    130
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[i][0],for_pos[i][1],for_pos[i][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[i][0],for_pos[i][1],for_pos[i][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[i])
{
case (int)geometry :
// Set texture & materials
glBindTexture(GL_TEXTURE_2D,BarkTex[(*(Tree[i].node_ID))]);
SetMaterialsAlpha(fdatapool.mat[3*(*(Tree[i].node_ID))],alphafades[alphalod]);
start = fdatapool.geom_lvl2lod_index[i*MAX_TREE_LVL + rep[*lod_val].geomLevel[i]];
size = fdatapool.geom_lvl2lod_size[i*MAX_TREE_LVL + rep[*lod_val].geomLevel[i]];
break;

case (int)texture :
if(Tree[i].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[i];
size = fdatapool.tex_lvl_size[i];
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]);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this