Sign in to follow this  

Lighting with camera class

This topic is 4088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I've got a verrry big problem. My camera is controlled by the glRotatef and the glTranslatef functions. I have one Light, and I call glLightfv(GL_POSITION, <Light_position>) after my camera's update, and the light's position is at the right position, but when I'm rotating up, down, left, right, or anywhere else, the light is rotating with me too. I've already tried correcting it with shaders, but the result was same. Please, anyone could help me? (I'm sorry if my english is not too good.. [or terrible XD])

Share this post


Link to post
Share on other sites
Quote:
Original post by sonka
Hello! I've got a verrry big problem. My camera is controlled by the glRotatef and the glTranslatef functions. I have one Light, and I call glLightfv(GL_POSITION, <Light_position>) after my camera's update, and the light's position is at the right position, but when I'm rotating up, down, left, right, or anywhere else, the light is rotating with me too. I've already tried correcting it with shaders, but the result was same. Please, anyone could help me?
(I'm sorry if my english is not too good.. [or terrible XD])


why are u using glRotatef and the glTranslatef? u should use gluLookAt instead...

heres an example of a camera class:


#ifndef CAMERA_H
#define CAMERA_H

#include "Vector.h"

class CCAMERA
{
public:

CVECTOR Position;
CVECTOR LookAt;

CVECTOR Forward;
CVECTOR Up;
CVECTOR Side;

float Yaw;
float Pitch;

int size;

CCAMERA()
{
Up.Set( 0.0f, 1.0f, 0.0f );
Side.Set( 1.0f, 0.0f, 0.0f );
}

CCAMERA(float px, float py, float pz, float lx, float ly, float lz, float ux, float uy, float uz, int s)
{

size = s;

Position.Set( px, py, pz );
LookAt.Set( lx, ly, lz );
Up.Set( ux, uy, uz );

Side.Set( 1.0f, 0.0f, 0.0f );
}

void SetHeight(float y)
{
Position.y = y;
}
void Update()
{
if( ( Yaw>=360.0f) || ( Yaw<=-360.0f ) )
Yaw= 0.0f;

if( Pitch>80.0f )
Pitch= 80.0f;
if( Pitch<-80.0f )
Pitch= -80.0f;

float cosYaw = cosf( DEG_TO_RAD( Yaw ) );
float sinYaw = sinf( DEG_TO_RAD( Yaw ) );
float sinPitch= sinf( DEG_TO_RAD( Pitch ) );
float cosPitch= cosf( DEG_TO_RAD( Pitch ) );

Forward.x= sinYaw * cosPitch;
Forward.y= sinPitch;
Forward.z= cosPitch * -cosYaw;

LookAt = Position + Forward;

Side= Forward.Cross( Up );

gluLookAt(Position.x, Position.y, Position.z,
LookAt.x, LookAt.y, LookAt.z,
Up.x, Up.y, Up.z);
}

void SetPosition(float x, float y, float z)
{
Position.x = x; Position.y = y; Position.z = z;
}

void SetLookAt(float x, float y, float z)
{
LookAt.x = x; LookAt.y = y; LookAt.z = z;
}

void SetForward(float x, float y, float z)
{
Forward.x = x; Forward.y = y; Forward.z = z;
}


private:
};
#endif

Share this post


Link to post
Share on other sites
i guess u need to update light_position each frame cause i guess LookAt matrix affect the ModelView matrix so it'll be something like this
glLoadIdentity();
glLightfv(GL_POSITION, <Light_position>)
... rest code

i hope i am correct ^^ try and see
take care.

Share this post


Link to post
Share on other sites
No, wait... by loading the identity matrix, aren't you automatically sticking the light relative to exactly where you are and not the origin? Anyways...

The only way I think that could happen is if you were somehow positioning the light relative to the camera, which would mean that you're missing a transformation call before setting the light each frame. Also, I'm not so sure about this, but, if you're only setting the light's position once, and it's being multiplied into the current matrix, then, if you don't constantly reset the light's position, wouldn't you end up having the light in the exact same position relative to your camera every time? Or am I just misguided at the moment?

Share this post


Link to post
Share on other sites
If I call Lightfv after my camera.update (that is rotating, and translating the world to -cameraposition and -camerarotation) then the light will be at the right place. The problem is in the rotation, if I rotate left, the light rotates right, if I rotate down, it rotates up..
Calling Lightfv after glLoadIdentity did not work. :-(
..But.. Everybody use gluLookAt in their cameraclass?? What about everybody's lighting? Everybody have lighting in their games, and it works, so why i can't do this? :\

Share this post


Link to post
Share on other sites
u treat light as a normal object that would be affect by matrix changing so u need to keep it in mind like rest of objects
u can make ur own vertex-light class tho
i hope i am right ^^;
take care.

Share this post


Link to post
Share on other sites

This topic is 4088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this