Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Material colors problem

  1. #1
    Member Contributor
    Join Date
    Apr 2002
    Location
    raanan israel
    Posts
    54

    Material colors problem

    Hi all,
    I have a really stupid problem !! it is so stupid I am a little embraced to ask about it, but it is driving me crazy so I will any how.....(-:
    my problem is:
    I am drawing a triangle strip, and I am switching colors(using glMaterialfv) between triangles, BUT the intire strip is always painted by the material color of the last vertex in the strip !!!
    The code is very basic, the init is :

    // Perspective
    ::glMatrixMode(GL_PROJECTION);
    ::glLoadIdentity();
    double aspect = (rect.Height() == 0) ? rect.Width() :
    (double)rect.Width()/(double)rect.Height();
    ::gluPerspective(45, aspect, 0.1, 1000.0);
    setSize(rect.Width(), rect.Height());
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // Lights properties
    float ambientProperties[] = { 0.2f, 0.2f, 0.2f, 1.0f};
    float diffuseProperties[] = { 0.5f, 0.5f, 0.5f, 1.0f};
    float specularProperties[] = { 0.1f, 0.1f, 0.1f, 1.0f};

    //enable lighting.
    ::glEnable(GL_LIGHTING);
    ::glEnable(GL_LIGHT0);
    ::glEnable(GL_LIGHT1);

    ::glLightfv(GL_LIGHT0, GL_AMBIENT, ambientProperties);
    ::glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
    ::glLightfv(GL_LIGHT0, GL_SPECULAR, specularProperties);

    ::glLightfv(GL_LIGHT1, GL_AMBIENT, ambientProperties);
    ::glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseProperties);
    ::glLightfv(GL_LIGHT1, GL_SPECULAR, specularProperties);
    //set the lighting position
    setLightPosition();

    ::glShadeModel(GL_SMOOTH);
    ::glEnable(GL_NORMALIZE);
    //set the black default clear color
    float black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
    setBcgColor(black);
    //enable the Z buffer
    ::glEnable(GL_DEPTH_TEST);

    and the function I fill the display list is:


    GLfloat SurfaceColor[4];
    //make sure that there is content to draw
    if(!isValid())
    return false;
    //create/replace the list located at listNumber
    ::glNewList(listNumber,GL_COMPILE);
    glColor3f (1.0, 1.0, 1.0);
    //check according to the transparency level of the object
    //if to enable a blending function
    if(getAlphaTransparencyLevel() < 1.0f)
    {
    ::glBlendFunc(GL_SRC_ALPHA,
    GL_ONE_MINUS_SRC_ALPHA);
    ::glEnable(GL_BLEND);
    ::glDepthMask (GL_FALSE);
    }
    //render the trinagle strip
    ::glBegin(GL_TRIANGLE_STRIP);
    for(int i=0 ; i<getVerticesArraySize() ; i++)
    {
    CVertex& v = getVertex(i);
    //set the vertex normal
    ::glNormal3f(v.getNormal().getX(), v.getNormal
    ().getY(), v.getNormal().getZ());
    SurfaceColor[0] = v.getColor().getRedValue();
    SurfaceColor[1] = v.getColor().getGreenValue();
    SurfaceColor[2] = v.getColor().getBlueValue();
    SurfaceColor[3] = v.getColor().getAlphaValue();
    //set the vertex color (ambient diffuse and
    //specular properties
    ::glMaterialfv(GL_FRONT_AND_BACK,
    GL_AMBIENT_AND_DIFFUSE , SurfaceColor);
    ::glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
    SurfaceColor);
    //set the vertex geometry.
    ::glVertex3f(v.getX(), v.getY(), v.getZ());
    }
    ::glEnd();
    //if we were drawing a transparent object turn off the
    //blending function and turn back on the Z buffer
    if(getAlphaTransparencyLevel() < 1.0f)
    {
    ::glDepthMask (GL_TRUE);
    ::glDisable(GL_BLEND);
    }

    ::glEndList();
    return true;


    I have tried all the trivial tests like switching off the lights and making sure that the glMaterialfv gets the proper material colors in SurfaceColor, I also tried changing the glMaterialfv calls to
    ::glColor4fv( SurfaceColor); calls, but for some reason then the strip becomes black and white……
    what basic thing am I missing ? (just when I thought I was turning from an opengl beginner to an opengl intermediate….)-
    thanks in advance.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jul 2001
    Posts
    352

    Re: Material colors problem

    Maybe it's just impossible to change the material properties inside of a strip. Anyway, when you'll worry about speed, you'll use a vertex array for this strip, and it's impossible to charge the materials inside of a vertex array.
    But using glColor4f was a good idea ! Because it's allowed to call it inside of a strip and even inside of a vertex array. Now, why do you get only black and white ?
    Here are two ideas :
    1) Before beginning drawing your strip, call :
    glEnable (GL_COLOR_MATERIAL);
    glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    2) Before beginning drawing your strip, call :
    glMaterialf (GL_SHININESS, 100.0);

    Hope this helps
    Morglum

  3. #3
    Member Contributor
    Join Date
    Apr 2002
    Location
    raanan israel
    Posts
    54

    Re: Material colors problem

    Morglum,
    it is possible to change the material in a strip i have done it before.
    regarding your suggestions:
    1) it does make it turn from black and white to color, and i get the same effect (last color only)
    2)what does the exponent factor of the color specular component has got to do with it, it only controls the reflection teta angle spread of the surface.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jul 2001
    Posts
    352

    Re: Material colors problem

    For 2), that's simple : if the specular exponent is too low, you don't see the diffuse color and in many cases, it results in a nearly-black-and white display : black for the ambient color and white for the specular highlight.

    Now, I've seen that you were compiling a display list. I think here's the problem : Try removing the calls to glNewList/glEndList. Does it work ?

  5. #5
    Member Contributor
    Join Date
    Apr 2002
    Location
    raanan israel
    Posts
    54

    Re: Material colors problem

    >>Now, I've seen that you were compiling a display list. I think here's the problem : Try removing the calls to glNewList/glEndList. Does it work ?

    I did it without the display list, same results.
    it shouldn't make any difference, the opengl just runs the compiles display list as if the commands where provoked just in amore efficient way....

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Material colors problem

    Are you by any chance using glShadeModel(GL_FLAT)?

    Nevermind... just looked at your code a bit closer. (Using code tags would make it more readable, btw.) You're using GL_SMOOTH. Guess that's not it then.

    [This message has been edited by Deiussum (edited 05-09-2002).]
    Deiussum
    Software Engineer and OpenGL enthusiast

  7. #7
    Senior Member Regular Contributor
    Join Date
    Jul 2001
    Posts
    352

    Re: Material colors problem

    I give up. The code you've shown seems to be perfectly correct.

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: Material colors problem

    I had a similar problem with glColorMaterial, in the fact that I only wanted to change the material properties and not the color. But what would happen is that after the first glColorMaterial call, other calls to glColorMaterial did not have any effect. After looking at the spec’s on glColorMaterial, in order for the material properties to change you had to call glColor after the glColorMaterial!!!!

    I not sure but you may have to have a glColor call after glMaterialfv also to make the new material properties take effect???




    Originally posted by snow_master:
    Hi all,
    I have a really stupid problem !! it is so stupid I am a little embraced to ask about it, but it is driving me crazy so I will any how.....(-:
    my problem is:
    I am drawing a triangle strip, and I am switching colors(using glMaterialfv) between triangles, BUT the intire strip is always painted by the material color of the last vertex in the strip !!!
    The code is very basic, the init is :


  9. #9
    Member Contributor
    Join Date
    Apr 2002
    Location
    raanan israel
    Posts
    54

    Re: Material colors problem

    nexusone,
    nope, nor before or after or each one of them alone works.....the same effect....the last vertex color is the color of the entire strip....

  10. #10
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: Material colors problem

    Now with glColorMaterial it is stated to be prefered over using glMaterial. And glColorMaterial can not be used between the glBegin and glEnd.
    Now with glMaterial the spec states that you can only change one parameter per vertex between a glBegin/glEnd. You have two changes to material between the glBegin/glEnd. Try putting the material changes before the glbegin.


    Here is what the spec on glMaterial states:

    NOTES

    The material parameters can be updated at any time. In
    particular, glMaterial can be called between a call to
    glBegin and the corresponding call to glEnd. If only a
    single material parameter is to be changed per vertex,
    however, glColorMaterial is preferred over glMaterial (see
    glColorMaterial).

    Originally posted by snow_master:
    nexusone,
    nope, nor before or after or each one of them alone works.....the same effect....the last vertex color is the color of the entire strip....

Page 1 of 3 123 LastLast

Similar Threads

  1. Material Problem
    By Kieran in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-22-2012, 08:43 AM
  2. material and light problem
    By v8671 in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 09-02-2008, 03:18 AM
  3. I want normal maps, but I also want material colors
    By halo in forum OpenGL: Advanced Coding
    Replies: 32
    Last Post: 08-22-2006, 06:03 AM
  4. Material-Light problem
    By Maxmagnus in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-06-2002, 04:20 PM
  5. clor \ material problem
    By snow_master in forum OpenGL: Advanced Coding
    Replies: 11
    Last Post: 05-14-2002, 10:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean