PDA

View Full Version : Light working correctly with negative normals why ??



AceEmbler
06-23-2005, 04:06 AM
I was testing it with more complicated object also but for purpose of this post i added QUAD and removed some trash.

My problem is. I have got light just before my "eyes" (i can se it's position becouse i have drawn cross in this point) and there is a quad after the light pos "eyepos">>"light_pos">>"object"
Normals points from the object toward eye cordinates. But the object is not enlightened until i move it toward eye and pass the light_pos so it looks like that eye>>object>>light. But when i change Normal's xyz to negative (-x,-y,-z) i can see the object properly shading.Why do i have to flip the normals direction (toward object) to make it correct ??

///////////////////////THE codE////////////
void TestRendering()
{
int IndV;
int IndN;
float px,py,pz;
float LPos[]={0.0f,0.0f,0.0f};
float LDif[]={0.6f,0.6f,0.6f};
float LSpc[]={0.4f,0.4f,0.4f};
float Amb[]={0.5f,0.5f,0.5f};
float MAmbDif[]={0.75f,0.75f,0.75f};
float MSpc[]={1.0f,1.0f,1.0f};
//-----------------Test Rendering
glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);

glShadeModel(GL_SMOOTH);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,1,0.01,100);
gluLookAt(0,0,3.5,0,0,0,0,1,0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Turning and positioning Light
glLightfv(GL_LIGHT0,GL_DIFFUSE,LDif);
glLightfv(GL_LIGHT0,GL_POSITION,LPos);
glEnable(GL_LIGHT0);

#define DCROSS //drawing cross where light is
#if defined (DCROSS)
glDisable(GL_LIGHTING);
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_LINES);
glVertex3f(LPos[0]+0.3,LPos[1],LPos[2]); glVertex3f(LPos[0]-0.3,LPos[1],LPos[2]); glVertex3f(LPos[0],LPos[1]+0.3,LPos[2]); glVertex3f(LPos[0],LPos[1]-0.3,LPos[2]); glVertex3f(LPos[0],LPos[1],LPos[2]+0.3); glVertex3f(LPos[0],LPos[1],LPos[2]-0.3);
glEnd();
glEnable(GL_LIGHTING);
#endif

//rotating object and setting distance
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,Dist-1.5);
glRotatef(CamRotX,0,1,0);
glRotatef(CamRotY,1,0,0);
//End Turning and drawing Light
//--------------- //one dimensional quad to lighten//
glBegin( GL_QUADS );
glNormal3f( 0.0f, 0.0f, 1.0f);// Normal Pointing Towards Viewer
glVertex3f(-1.0f, -1.0f, 0.0f); // Point 1 (Front)
glVertex3f( 1.0f, -1.0f, 0.0f); // Point 2 (Front)
glVertex3f( 1.0f, 1.0f, 0.0f); // Point 3 (Front)
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
//---------------

//DRAWING TEXT
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(-1,1,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glColor3f(1.0f,1.0f,1.0f);
GLScreen->Write2DText("Ilosc Wierzcholkow: ",(int)VectVertex3f.size(),-1.0f,0.9f);
GLScreen->Write2DText("Ilosc Trojkatow: ",(int)VectFace.size()/3,-1.0f,0.8f);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
//DRAWING TExt
//----------------ENDOF IT
}

Relic
06-23-2005, 05:34 AM
Geometry code looks ok, the quad is front facing and the normal points to the viewer. (You should put the single glNormal3f call outside the glBegin-glEnd.)

>>(i can se it's position because i have drawn cross in this point)<<

You think so? The glLightfv parameters must be float[4]! Your light position is probably completely wrong or using the 0.6 from the next array as w.

You should also not enable color material before you specified glColorMaterial or you fall into pit #14 http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

AceEmbler
06-23-2005, 06:30 AM
That was the problem. Yep Light pos should be float[4]. I will take notice of this :p .
Thanks

Relic
06-23-2005, 07:04 AM
Mind, not only the position, all colors, too.