Jump to content


OpenGL Alpha Mask / Diffuse Lighting??


1 reply to this topic

#1 SmokingRope

    Valued Member

  • Members
  • PipPipPip
  • 210 posts

Posted 07 June 2007 - 01:01 AM

I've setup per vertex texture blending on my terrain. It does extra passes over the quads of my terrain with the textures of adjacent quads. It uses weights based on the textures of the adjacent quads to specify the alpha at each vertex. The alphas at each vertex are interpolated across the rest of the quad.

I'm rendering the entire scene with ambient and diffuse light. My problem lies with the fact that the diffuse component of the lighting is not automatically using the alpha value specified with my calls to glColor4f().

I can work around it by changing the alpha value of the diffuse component of the material at each vertex as i do the rendering.

// Before i Turned The Lights On

glBindTexture(); // Pick Texture

foreach vertex
{
    glColor4f(1.0f, 1.0f, 1.0f, l_alpha); // Set Alpha
    glVertex3fv(l_vertex); // Draw Vertex
    // .. repeat
}

// After The Lights Were Turned On

glBindTexture() // pick texture
GLfloat l_diffuse[4] = {1.0f, 1.0f, 1.0f, 0.0f}; // array for diffuse color

foreach vertex
{
    l_diffuse[3] = l_alpha; // Change Value In Diffuse Color Array
    glColor4fv(l_diffuse); // Set Alpha
    glMaterialfv(GL_FRONT,GL_DIFFUSE,l_diffuse); // Change Materials Diffuse
    glVertex3fv(l_vertex); // Draw Vertex
    // .. repeat
}

I'm actually doing all my vertex rendering with vertex arrays and this material changing turned my texture blending code into an ugly mess. Is there any way to get around changing the materials diffuse component at each vertex while still getting the same affect?

#2 SmokingRope

    Valued Member

  • Members
  • PipPipPip
  • 210 posts

Posted 16 June 2007 - 06:09 PM

Well, as one might expect, i found the answer to my own question. OpenGL provides a rendering option called GL_COLOR_MATERIAL which causes the material color to inherit it's value from the color values specified using both glColor*() and Vertex Buffers. Along with enabling and disabling this material color tracking it is also necessary to specify which faces (front or back) and which component(s) should be tracked. A typical use of this feature might look as follows


glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

glEnable(GL_COLOR_MATERIAL);

glColor3f(0.75f, 0.75f, 0.75f);

// .. DRAW

glDisable(GL_COLOR_MATERIAL);


One important thing to remember is that you need to glColorMaterial() before you glEnable(GL_COLOR_MATERIAL).

Performance wise, although it is possible to change multiple material components through repeated calls to glColorMaterial(), the performance gains of changing multiple components in this way is questionable. In addition, one may find that even changing a single component using glColorMaterial() as opposed to glMaterialfv() yields a questionable speed improvement.

http://www.opengl.or...ormaterial.html





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users