OGL Lighting Newbie - transform with camera wont work?

Started by
9 comments, last by jswill100 16 years, 9 months ago
Hi all. I posted this question a while ago but I didnt post very clear code or explanation so im having another go, because its REALLY frustrating me and the simple way it works well....just wont work. I have posted a bump after a few replies here, so please take it that any advise before that bump is old. Basically I want one light in my scene, that acts like a headlamp - a spotlight that has its position and direction moving around with the camera. I have my scene setup with a working camera. As you can see, it is initialised in Init() and updated in my display function (the camera update function calls glLookAt). According to numerous sources, to get this headlight affect is VERY easy, and should be as simple as setting up the position as the origin, and the direction in the negative Z axis, before any kind of translations take place (in err, eye space right?). You can see I have tried this - my doLighting function is called in Init(), which sets up this position and direction. I have tried ALL kinds of setups with no success. Im hoping someone can really point me in the right direction with some specific instruction, because right now its very frustrating for such a simple thing. In terms of the behaviour I am seeing, it varies with the different setups I am trying, but the typical behaviour is a light whos position does move with the cameras position, but is actually offset from the camera centre quite considerably and inexplicably. As I say, with the different setups I try I see a wide variety of behaviours, just none of them correct at all. This particular setup shows a light that points in the negative Z direction, its position moves with the camera, but is offset considerably. The direction of thee light does not change at all, and always points in that direction. If I set a spotlight area, that cone DOES move with the camera, but will only illuminate a spot that is in the lite zone, which is offset in the negative Z direction. So it kind of seems that things are basicaly working, just with a weird offset problem. Anyone have some thoughts that could help me here? The light it setup as positional (with w=1) rather than direction, because to my mind that is what is needed here? Please help! argh
[Source] code goes here 

int camX =-1700, camZ = -1480, camY=20;

Camera* Cam;


int screenWidth = 640, screenHeight = 480;


void myGlutInit()
{
	doLighting();
	//This sets up a new camera, with position of camX/Y/Z, and looking at a slight offset from that point.
	Cam = new Camera(camX, camY, camZ, camX-10, camY, camZ+10);
}


void myGlutDisplayCallback(void) 
{	

  	// Clear our depth buffer and color bit
  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  	// Reset our view
  	glMatrixMode(GL_MODELVIEW);
  	glLoadIdentity();


	Cam->cameraUpdate();//THIS CALLS glLookAt to change the camera position and direction!!!!!!!!!!

	
	glPushMatrix();
		// Draw the models
	glPopMatrix();


	// flush all of the rendering to the color buffer and swap the buffers
	glFlush();

	glutSwapBuffers();

}



void myGlutIdleCallback( void ) 
{

	//this updates the position and look at points depending on mouse movements, for the glLookAt function.
	Cam->CamMouseMove(screenWidth, screenHeight);

	glutPostRedisplay();

}


void myGlutReshapeCallback(int x, int y)
{
	screenWidth = x; screenHeight = y;

	glMatrixMode(GL_PROJECTION);

	glLoadIdentity();

	glViewport (0, 0, screenWidth, screenHeight);

	gluPerspective(angle, (GLfloat)screenWidth/(GLfloat)screenHeight, nearD, farD);

}


void doLighting()
{

	//I have omitted all the other light setup, since its unrelated to     //position and direction.

	GLfloat dirLight1[] = {0, 0, -1};
	GLfloat light_position2[] = { 0, 0, 0, 1.0 };


	//glMatrixMode(GL_MODELVIEW); commented out, but have tried this too.
	//glLoadIdentity();

	glLightfv(GL_LIGHT1, GL_POSITION, light_position2);

	/////////define the direction for Light1
	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, dirLight1);

	//////define the cutoff angle for Light1
	glLightf(GL_LIGHT1, GL_SPOT_CUTOFF,35.0);

	 
	glEnable(GL_LIGHTING);

	glEnable(GL_LIGHT1);							
}


[/Source]
[/source][/source][/source][/source] [Edited by - jswill100 on July 17, 2007 8:35:16 AM]
Advertisement
Hi,

How are you trying to move the light in the first place. All you should really have to do is to move the light in front of the camera is to do the same transformation on the light as well as on the camera

And Use "
[Source] code goes here [/Source]
" to put the code box thingy.
The more applications I write, more I find out how less I know
Thanks for the reply. Well thats kind of what I believe im doing. My camera is moved using glLookAt, but is essensially moving around based on its position, which here is CamPos.x/.y/.x. In all the lighting movement tutorialss ive seen the below is roughly how its done - applying a translation to the

glLightfv(GL_LIGHT1, GL_POSITION....

call. I assume im either being dense or there are problems with things getting jumbled up in terms of viewing matrices etc? (again, being dense!)

Cheers
John

[Source] Test of my first code box thingy! [/Source]
I think the trick here is even more simple?

try this:

assuming this is the first light (i.e. has default values e.g. position[well direction really](0,0,1,0), and its a directional light->

just create the light at initialisation time (before any tranformations take place and do nothing further (i.e. do not update it any further in the displayCallback).

Try this....and you should see light behave as if attached to your camera.

Thanks for the reply. I have tried this and I get the same weird behaviour. In my init function I set up the camera and its variables, and set its position to (0,0,0,1). As you say, it does translate autoatmically around with the camera, but still with this weird offset I spoke about above.


argh
(0,0,0,1) makes the light positional == (x,y,z,w) where w = 1
w = 0 for directional light.

try (0,0,1,0) [not (0,0,0,1)]

this info and more can be found at e.g.

http://fly.srk.fer.hr/~unreal/theredbook/chapter06.html

[Edited by - steven katic on July 11, 2007 4:46:19 PM]
This is a bump. I have modified the original question considerably but am still basically having the same problem. Please take it that I have tried to advise above, and I still cant seem to get away from my problems. heeeelp!
Could you post a screenshot of the "offset" problem so that we might get a better understanding?
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Whats the command for posting a screen again? :) havent done it here before.
Load up the image somewhere then use <img src="url"/>

For more info have a look to the faq.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

This topic is closed to new replies.

Advertisement