The light isn't where I want it to be.

BlitzMax Forums/OpenGL Module/The light isn't where I want it to be.

deps(Posted 2005) [#1]
Fooling around with OpenGL a bit. I got a problem with the lights.
I place the lights with a call to glLightfv(lnum, GL_POSITION, Position) and I also place a small cube there so I can see where the light currently is, or at least should be. But it looks like the light is somewhere next to the sube and not inside it.

Inside my little "engine", everything is an extension to the type TSceneNode. Lights too. Before and after an object is drawn the engine call these two (They are a part of TSceneNode)
	Method pre_render()
		glPushMatrix()
		gltranslatef(x_pos,y_pos,z_pos)
		glscalef( x_scale, y_scale, z_scale )		
		glrotatef(x_rot, 1.0,0.0,0.0)
		glrotatef(y_rot, 0.0,1.0,0.0)
		glrotatef(z_rot, 0.0,0.0,1.0)
	EndMethod
	
	Method post_render()
		glPopMatrix()
	EndMethod

All objects also got a render() method. Lights too, and it looks like this:
	Method Render()
		Position[0] = x_pos
		Position[1] = y_pos
		Position[2] = z_pos
		glLightfv(lnum, GL_AMBIENT, Ambient)
		glLightfv(lnum, GL_DIFFUSE, Diffuse)		
		glLightfv(lnum, GL_POSITION, Position)
	EndMethod

Can anyone spot a nasty bug or something? Should glTranslate/glrotate/glscale not be used when placing lights?
As I said before, a small cube is being drawn at the lights center and it looks like it is drawn where I want my light, but the light is shining on my scene somewhere else. Looks like it's some GL units along the positive X axis and some units along the negative Z axis from where the cube is, if that makes things any clearer. I have no clue at all what's wrong...


Odds On(Posted 2005) [#2]
You don't need to use glTranslate, glScale, glRotate etc for lights.. that's what glLightfv(lnum, GL_POSITION, Position) is for (Rotation is handled for spot lights only via cone angles).

You need to reposition your lights every frame, and you don't need to push or pop the matrix.

Regards,
Chris


deps(Posted 2005) [#3]
Uncommenting the code inside pre_render and post_render did it. The light appears to be at the correct position. Creepy, I was so sure I tried that!
Thanks for the help! :)