Still problem with directional lighting, please help!

Ok, the problem is old, but still existing, so I post it again. I simply don’t get this solved.

In this little testprogram all I want is the quad to have a normal of 0,0,1 and an infinite directional light lighting this quad. In my opinion it should be absolutely bright, after all both vectors are totally even, but instead it’s dark. If I change both vectors (the one of the light and the poly) to 1,0,0 it is lighted… what’s wrong on the code? Please help, thanks:

BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
{
hInst = hInstance; // Instanzzugriffsnummer in unserer globalen Variable speichern
hWnd = CreateWindow((char*)WinClass, “lp”, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if( !hWnd )
{
return FALSE;
}

SetWindowPos(hWnd,NULL,0,0,640,480,0);

// -------------------- set up OpenGL -------------------------

WinDC=GetDC(hWnd);
memset(&Pfd,0,sizeof(Pfd));

Pfd.nSize=sizeof(Pfd); Pfd.nVersion=1; Pfd.dwFlags=PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
Pfd.iPixelType=PFD_TYPE_RGBA; Pfd.cColorBits=16; Pfd.cDepthBits=16; Pfd.iLayerType=PFD_MAIN_PLANE;
PixelFormat = ChoosePixelFormat(WinDC, &Pfd);
SetPixelFormat(WinDC, PixelFormat, &Pfd);

RC = wglCreateContext(WinDC);
wglMakeCurrent(WinDC,RC);
glClearColor(0.f,0.f,0.f,1.f);
glDepthFunc(GL_LEQUAL);
glDepthMask(true);
glViewport(0,0,640,480);
glEnable(GL_DEPTH_TEST);

// --------------------------------------------------------------

ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );

return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch( message )
{
case WM_PAINT:

  	{
  		wglMakeCurrent(WinDC,RC);
  		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  		glLoadIdentity();

  		glMatrixMode(GL_MODELVIEW);

  		float mfFrTop,mfFrRight,mfFrBottom,mfFrLeft;
  		#define pi 3.141592653f

  		//calculate and set the viewcone
  		mfFrTop    = 10/*nearclip*/*(float)tan((60/*v. viewa.*//2.f)*(pi/180.f));
  		mfFrRight  = mfFrTop*(90/*h. viewa.*//60/*v. viewa.*/);
  		mfFrBottom = -mfFrTop;
  		mfFrLeft   = -mfFrRight;
  		glFrustum(mfFrLeft,mfFrRight,mfFrBottom,mfFrTop,10/*nearclip*/,1000/*farclip*/);		

  		glEnable(GL_LIGHTING); //enable lighting
  		glEnable(GL_LIGHT0); //enable light 0
  		glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE); //local viewer shall not matter
  		glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE); //two sided lighting plz

  		//setup infinite directional light
  		float lightpos[4] = {0.f,0.f,1.f,0.f};
  		glLightfv(GL_LIGHT0,GL_POSITION,lightpos);

  		glColor3f(1,1,1); //shared color for all vertices
  		glNormal3f(0,0,1); //shared normal for all vertices
  		glBegin(GL_QUADS);
  			glVertex3f(-20,-20,-100);
  			glVertex3f(20,-20,-100);
  			glVertex3f(20,20,-100);
  			glVertex3f(-20,20,-100);
  		glEnd();

  		SwapBuffers(WinDC);
  	}
  	break;
  case WM_DESTROY:
  	PostQuitMessage( 0 );
  	break;
  default:
  	return DefWindowProc( hWnd, message, wParam, lParam );

}
return 0;
}

I also uploaded the zip with the source, so if anyone of you has some time, please check it in your compiler yourself… I have this problem since months and well… I slowly need to get it solved, thanks:
http://www.lyxion.com/lightprobs.zip

Michael

Never use the modelview matrix for projection when using lighting. Load the frustum into GL_RPOJECTION and set modelview to identity ( then you’ll see a bright quad ).

Aaaaaaaaaaah, finally . Thanks a lot.

BlackJack

Corrected code

  		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  		float mfFrTop,mfFrRight,mfFrBottom,mfFrLeft;
  		#define pi 3.141592653f

  		glMatrixMode(GL_PROJECTION);
  		glLoadIdentity();

  		//calculate and set the viewcone
  		mfFrTop    = 10/*nearclip*/*(float)tan((60/*v. viewa.*//2.f)*(pi/180.f));
  		mfFrRight  = mfFrTop*(90/*h. viewa.*//60/*v. viewa.*/);
  		mfFrBottom = -mfFrTop;
  		mfFrLeft   = -mfFrRight;
  		glFrustum(mfFrLeft,mfFrRight,mfFrBottom,mfFrTop,10/*nearclip*/,1000/*farclip*/);		

  		glEnable(GL_LIGHTING); //enable lighting
  		glEnable(GL_LIGHT0); //enable light 0
  		glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE); //local viewer shall not matter
  		glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE); //two sided lighting plz

  		//setup infinite directional light
  		float lightpos[4] = {0.f,0.f,1.f,0.f};
  		glLightfv(GL_LIGHT0,GL_POSITION,lightpos);

  		glMatrixMode(GL_MODELVIEW);
  		glLoadIdentity();

  		int deg = (GetTickCount()/5) % 360;

  		glColor3f(1,1,1); //shared color for all vertices
  		glNormal3f(cos(deg*pi/180.f),0,sin(deg*pi/180.f)); //shared normal for all vertices
  		glBegin(GL_QUADS);
  			glVertex3f(-20,-20,-100);
  			glVertex3f(20,-20,-100);
  			glVertex3f(20,20,-100);
  			glVertex3f(-20,20,-100);
  		glEnd();				

  		SwapBuffers(WinDC);

also read the faq (the whole thing, right hand side of the www.opengl.org site)
itll only take an hours but can potentiually save time in the future

Well, have all in all readen days of GL doku already and am working with GL since more than 7 years, but… never needed it’s lighting, hehe. GLs lighting was compared to my own far slower, but well, now in the times of t&l of course not anymore ), so i started half a year ago a new engine from scratch to put the old big one… as soon as possible… onto a disc which will then “sleep” forever there . I think I even read that FAQ anytime, but well, my memory is no book… unfortunately , in any case thanks for the help.

Michael