PDA

View Full Version : OpenGL/ Light (part1)



NeedsOpenGL
05-21-2011, 09:13 AM
Hello.
I have to create a small car's game. Until now, I've been viewing all the scene just using [i]glColor3d(colorR,colorG, colorB)[i], but now I'd like to deal with light sources. I'd like to have two game-modes, one for the day-sun-light, and the other for the night-moon-light. That's a directional light. Firstly I just want to have a sun, with white light.

When I use [i]glEnable(GL_LIGHTING);[i] that turns all my scene components (pieces of road) darkgreyback. Since then, I defined my light source, enabled it, and set all that I found about material reacting to light. I tried to define normals. But nothing seems to work, that is, to make the materials react to light.

What am I doing wrong? What is missing? I've already read many sites about light and materials... Here follows the copy-pasted piece of code in which

I'm making the experience. It's a rectangular and horizontal piece of

road.

Thanks to all the helpers.

void Line::draw(){
sizeX = 640;
sizeY = 480;
if(_one){
glPushMatrix();
glTranslated(_lpoint[0],_lpoint[1],1.1);
glutSolidSphere( 10 , 10 ,10 );
glPopMatrix();

glPushMatrix();
glTranslated(_lpoint[0],_lpoint[1],1.1);
glTranslated(sizeX/6,sizeY/2,0);
////////////// I'm working on the light between these bars

GLfloat position[4] = { 1.0, 1.0, 1.0, 0.0};
GLfloat ambientLight[4] = { 0, 0, 0, 1.0};
GLfloat diffuseLight[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specularLight[4] = { 1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);

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

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
glEnable(GL_COLOR_MATERIAL);
//glColor3d(0.329412,0.329412,0.329412); DDD
glBegin(GL_POLYGON);
glNormal3fv (0,0,-1);/////////////////////////
glVertex3fv(-sizeX/6, -sizeY/2, -1);
glVertex3fv(sizeX/6, -sizeY/2, -1);
glVertex3fv(sizeX/6, sizeY/2, -1);
glVertex3fv(-sizeX/6, sizeY/2, -1);
glEnd();
glPopMatrix();
}

carsten neumann
05-21-2011, 10:12 AM
please use [ code],[ /code] around source snippets for better readability.

you are using



glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);


which tells OpenGL to ignore your materials ambient and diffuse values and instead use the vertex colour attribute for those. You then never call glColor*() to actually set a vertex colour.