I also think that somehow the light is not moving as I want. I call the glLightfv just before I draw the model so there aren’t any glTranslate(s) between them.
Steps: the model is in a array/vector, I recalculate some stuff (actually add triangles) and then from the coordinates of these triangles I calculate the average coordinates (will be more or less in the middle of the model) and then use these coordinates to initialize the light.
code exactly as it appears in file:
…
//triangles have been calculated thus light can be initialized
float lightX = 150.0f;//totalX/totalCounter + 150.0f;
float lightY = 0.0f;//totalY/totalCounter + 0.0f;
float lightZ = 0.0f;//totalZ/totalCounter + 0.0f;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float noAmbient[] = {0.0f, 0.0f, 0.0f, 1.0f};
float whiteDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
float global_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
float position[] = {lightX, lightY, lightZ, 1.0f};
float ambPos[] = {150.0f,0.0f,0.0f,1.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, noAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
//get the ‘color’ of the face to ‘apply’ to texture
GLdouble pos3D_x, pos3D_y, pos3D_z;
GLdouble model_view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluProject(totalX/totalCounter,totalY/totalCounter,totalZ/totalCounter,model_view,projection,viewport,&pos3D_x,&pos3D_y,&pos3D_z);
int nWidth = 1;
int nHeight = 1;
GLubyte pRGB[3];
glReadPixels( pos3D_x, pos3D_y, nWidth, nHeight, GL_RGB, GL_UNSIGNED_BYTE, (void *)pRGB);
float skinRed = pRGB[ 0 ]/255.0;
float skinGreen = pRGB[ 1 ]/255.0;
float skinBlue = pRGB[ 2 ]/255.0;
glColor4f(skinRed,skinGreen,skinBlue,1.0f);
glBegin(GL_TRIANGLES);
for(int i=0;i<triangles2.size();i++)
{//three vertices
int a = triangles2[i].x;
int b = triangles2[i].y;
int c = triangles2[i].z;
//vertice one
glNormal3f(vertices2[a].x+normals2[a].x,vertices2[a].y+normals2[a].y,vertices2[a].z+normals2[a].z);
glTexCoord2d(texCoords2[a].x,texCoords2[a].y);
glVertex3f(vertices2[a].x,vertices2[a].y,vertices2[a].z);
//vertice two glNormal3f(vertices2[b].x+normals2[b].x,vertices2[b].y+normals2[b].y,vertices2[b].z+normals2[b].z);
glTexCoord2d(texCoords2[b].x,texCoords2[b].y);
glVertex3f(vertices2[b].x,vertices2[b].y,vertices2[b].z);
//vertice three
glNormal3f(vertices2[c].x+normals2[c].x,vertices2[c].y+normals2[c].y,vertices2[c].z+normals2[c].z);
glTexCoord2d(texCoords2[c].x,texCoords2[c].y);
glVertex3f(vertices2[c].x,vertices2[c].y,vertices2[c].z);
}
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0.0,0.0,1.0);
//draw circle at light position
glEnd();
I have left out the code where the triangles are calculated and where the circle for the light is drawn.
As you see there are no glTranslate(s) between the light initialization and the drawing of the model (I’m not even sure if glTranslate is used to calculate the new position).