PDA

View Full Version : glMaterial & GL_LIGHT



scubasteve
10-15-2014, 12:29 PM
Hello everyone,

I am trying to obtain lighting on some spheres like the one shown here using glMaterial and lighting functions:
1485

This is what I have (I am having trouble with the green and yellow spheres):
1486

Here is the code for the materials I am using (for green and yellow):

void draw_solid_sphere_green(void)
{
// enable Light1 for this sphere
glEnable(GL_LIGHT3);

// create the matrix stack
glPushMatrix();
glTranslatef(-1.1, 0.0, 0.0);

// create a material for this sphere
GLfloat color[] = {0.0, 1.0, 0.0, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);

GLfloat white[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0f);

// draw a solid sphere
glutSolidSphere(1.0f, 1000, 1000);

// Remove matrix from stack
glPopMatrix();

// Disable the light
glDisable(GL_LIGHT3);
}


void draw_solid_sphere_yellow(void)
{
// enable Light1 for this sphere
glEnable(GL_LIGHT4);

// create the matrix stack
glPushMatrix();
glTranslatef(-3.4, 0.0, 0.0);

// create a material for this sphere
GLfloat color[] = {1.0, 1.0, 0.0, 1.0};
glMaterialfv(GL_FRONT, GL_DIFFUSE, color);

GLfloat ambient[] = {1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);

GLfloat white[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0f);

// draw a solid sphere
glutSolidSphere(1.0f, 100, 100);

// Remove matrix from stack
glPopMatrix();

// Disable the light
glDisable(GL_LIGHT4);
}


and lastly the lighting code:

/**GL_LIGHT# FLOATS**/
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_diffuse3[] = {0.3, 0.3, 0.3, 1.0};
GLfloat light_position[] = {0.0, 2.0, -8.0, 0.0}; //relative
GLfloat light_dir[] = {0.0, 0.0, 1.0, 1.0}; //relative
GLfloat light_white[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_position34[] = {0.0, 2.0, -2.0, 0.0}; //test
GLfloat light_dir34[] = {0.0, -1.0, 1.0, 1.0}; //test

/**GL_LIGHT1**/
//glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_POSITION, light_position);

glLightfv(GL_LIGHT1, GL_SPECULAR, light_white);

/**GL_LIGHT2**/
glLightfv(GL_LIGHT2, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT2, GL_POSITION, light_position);

/**GL_LIGHT3**/
//glLightfv(GL_LIGHT3, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT3, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT3, GL_POSITION, light_position34);

glLightfv(GL_LIGHT3, GL_SPECULAR, light_white);

glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, light_dir34);
glLightf(GL_LIGHT3, GL_SPOT_CUTOFF, 15.0f);
glLightf(GL_LIGHT3, GL_SPOT_EXPONENT, 1.0f);

/**GL_LIGHT4**/
glLightfv(GL_LIGHT4, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT4, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT4, GL_POSITION, light_position34);

glLightfv(GL_LIGHT4, GL_SPECULAR, light_white);

glLightfv(GL_LIGHT4, GL_SPOT_DIRECTION, light_dir34);
glLightf(GL_LIGHT4, GL_SPOT_CUTOFF, 15.0f);
glLightf(GL_LIGHT4, GL_SPOT_EXPONENT, 30.0f);


Do you have any suggestions?

Thanks!