0
votes

I have a problem with opengl lighting, I have an object let say this one.

enter image description here

It's an original object without applying light effect, So the problem is when I try to add light to this object it hides the object colors and turn the object into this color

enter image description here

diffuse and specular component I used :

GLfloat diffu[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat spec[] = {0.5f, 0.5f, 0.5f, 0.5f};
GLfloat shinnes [] = {50};
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffu);
glLightfv(GL_LIGHT1, GL_SPECULAR, spec);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,shinnes);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);

So I need to understand what's the problem is ???

3

3 Answers

3
votes

From this (http://www.glprogramming.com/red/chapter05.html) link:

Define Material Properties for the Objects in the Scene

An object's material properties determine how it reflects light and therefore what material it seems to be made of. Because the interaction between an object's material surface and incident light is complex, specifying material properties so that an object has a certain desired appearance is an art. You can specify a material's ambient, diffuse, and specular colors and how shiny it is. In this example, only these last two material properties - the specular material color and shininess - are explicitly specified (with the glMaterialfv() calls). (See "Defining Material Properties" for a description and examples of all the material-property parameters.)

As soon as you start using lighting, the objects material properties (its color) are specified by the specular/diffuse... properties passed in by glMaterialfv(). So if you where using glColor() you now need to specify the material properties with glMaterialfv().

In your code, as well as setting the light specular and diffuse colour, you need to set the material specular and diffuse colour:

// Set light properties
GLfloat diffu[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat spec[] = {0.5f, 0.5f, 0.5f, 0.5f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffu);
glLightfv(GL_LIGHT1, GL_SPECULAR, spec);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);

// Set material properties
GLfloat shinnes [] = {50};
GLfloat matdiffu[] = {1.0f, 0.f, 0.f, 1.0f};
GLfloat matspec[] = {1.0f, 0.f, 0.f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,matdiffu);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,matspec);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,shinnes);

// Draw object
1
votes

the diffu and spec define the color of the material as well; use {0f, 1f, 0f, 1f} and {0f, 1f, 0f, 0.5f} for a pure blue color (assuming RGBA)

1
votes
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseColor);

along with GL_SHININESS, GL_SPECULAR to set the material properties of your geometry. Then the lighting will interact with the geometry material properly.