Lighting problem with rotations

BlitzMax Forums/OpenGL Module/Lighting problem with rotations

carl read(Posted 2010) [#1]
Hi,

I'm new to Blitz and OpenGL and am having trouble getting lighting to behave with OpenGL. Here's an example of the problem...

GLGraphics 800,600

Function InitGL()
	glClearColor(0.0, 0.0, 0.0, 0.0)
	glClearDepth 1.0
	glDepthFunc(GL_LESS)
	glEnable(GL_DEPTH_TEST)
	glFrontFace(GL_CW)
	glShadeModel(GL_SMOOTH)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	gluPerspective(45.0, 800.0 / 600.0, 1.0, 100.0)
	glMatrixMode(GL_MODELVIEW)
	
	' Add light.
	glEnable(GL_LIGHTING)
	glEnable(GL_LIGHT0)
	glEnable(GL_COLOR_MATERIAL)	
End Function

Function DrawScene()
	glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT

	glLightfv(GL_LIGHT0, GL_POSITION, [0.0, 0.0, -6.0, 1.0])

	glTranslatef -1.5, 0.0, -7.0

	glBegin GL_POLYGON
		glColor3f 0.2, 0.2, 1.0
		glVertex3f  0.0, 1.0, 0.0
		glVertex3f -1.0,-1.0, 1.0
		glVertex3f  1.0,-1.0, 1.0
		glVertex3f  0.0, 1.0, 0.0
		glVertex3f  1.0,-1.0, 1.0
		glVertex3f  1.0,-1.0, -1.0
		glVertex3f  0.0, 1.0, 0.0
		glVertex3f  1.0,-1.0, -1.0
		glVertex3f -1.0,-1.0, -1.0
		glVertex3f  0.0, 1.0, 0.0
		glVertex3f -1.0,-1.0,-1.0
		glVertex3f -1.0,-1.0, 1.0
	glEnd

	glTranslatef 3.0, 0.0, 0.0
	
	glRotatef rotation, 0.0, 1.0, 0.0
	
	glBegin GL_POLYGON
		glColor3f 0.2, 0.2, 1.0
		glVertex3f  0.0, 1.0, 0.0
		glVertex3f -1.0,-1.0, 1.0
		glVertex3f  1.0,-1.0, 1.0
		glVertex3f  0.0, 1.0, 0.0
		glVertex3f  1.0,-1.0, 1.0
		glVertex3f  1.0,-1.0, -1.0
		glVertex3f  0.0, 1.0, 0.0
		glVertex3f  1.0,-1.0, -1.0
		glVertex3f -1.0,-1.0, -1.0
		glVertex3f  0.0, 1.0, 0.0
		glVertex3f -1.0,-1.0,-1.0
		glVertex3f -1.0,-1.0, 1.0
	glEnd
	
	' Restore changes.
	glRotatef -rotation, 0.0, 1.0, 0.0
	glTranslatef -1.5, 0.0, 7.0

	Flip
End Function

InitGL()

Global rotation:Float=0.0

While Not KeyHit( KEY_ESCAPE )
	DrawScene
	rotation = rotation + 1.0
	Flip
Wend

End


Two blue pyramids, one of them rotating. The lighting's correct for the static pyramid, but not with the rotating one, where the light also seems to be rotating. I'm assuming the light needs to be repositioned for the rotating pyramid, but many hours of seeking a way to do so have so far failed!

Any help appreciated.


ImaginaryHuman(Posted 2010) [#2]
No idea... BUT.. I did read somewhere that if you rotate the camera in OpenGL the lighting calculation my not work properly because the camera matrix isn't factored into the lighting equasion, or something like that.... and that it would be better to rotate the modelview matrix by the inverse of how you want the camera to move.... ie move the model. I have no idea if that's what you're doing already.


ImaginaryHuman(Posted 2010) [#3]
Also for lighting you must define normals with glNormal()!


carl read(Posted 2010) [#4]
Aha - glNormal() put me on the right track!

Add a...

glNormal3d(0, 1, 0)

after the two...

glBegin GL_POLYGON

lines in the above code and the shading behaves. And to see it better, change the light position line to...

glLightfv(GL_LIGHT0, GL_POSITION, [-0.5, 0.0, -8.0, 1.0])

Many thanks!


ImaginaryHuman(Posted 2010) [#5]
There you go. You have to have normals for lighting calculations because it needs to know the angle from the surface so that it can bounce the light off it into the camera.