1
\$\begingroup\$

I have a code that draws a sphere. Without lighting it is white, but if I enable lighting, it's drawn in gray.

I don't know why the sphere changed it's color

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void init(void) 
{
    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_shininess[] = { 50.0 };
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_SMOOTH);

    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
}

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidSphere (1.0, 20, 16);
    glFlush ();
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
                 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
    else
        glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
                 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display); 
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Um, well, it's probably not facing the light, so the color is darker. That's by design, I'd say. \$\endgroup\$ Commented Jan 23, 2013 at 13:25

1 Answer 1

7
\$\begingroup\$

You haven't set a diffuse colour for your light (via glMatrialfv (GL_DIFFUSE, ...) and - according to the GL spec - the default diffuse colour is {0.8, 0.8, 0.8, 1.0} - i.e. light grey. You also haven't set an ambient colour (default {0.2, 0.2, 0.2, 1.0} - dark grey).

The default behaviour of lighting is that it multiplies by the current colour, so with an object that's coloured white, that's going to result in grey.

In other words, it's grey because you told it to be grey.

\$\endgroup\$
4
  • \$\begingroup\$ do you main that I can change object properties by glMatrialfv to reflect the color that has been chosen so the color will be the chosen color ? in my example the chosen color is gray which defult value so sphere will reflect the gray color \$\endgroup\$
    – user25406
    Commented Jan 23, 2013 at 14:12
  • 3
    \$\begingroup\$ Since you copy-and-pasted your code from the Red Book lighting chapter, you really should refer there for further explanation - glprogramming.com/red/chapter05.html \$\endgroup\$ Commented Jan 23, 2013 at 14:18
  • \$\begingroup\$ The light position is {1, 1, 1, 0} ... note the last 0. That makes it a directional light rather than a point light. \$\endgroup\$
    – ccxvii
    Commented Jan 23, 2013 at 14:41
  • \$\begingroup\$ @ccxvii - "The light position is {1, 1, 1, 0} ... note the last 0" - correct, my bad. Answer edited accordingly. \$\endgroup\$ Commented Jan 23, 2013 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .