Skip to main content
gamedev.net
🔒 Locked

Simple Question

Started by franchela 8 replies 1k views
Original Post
franchela
franchela
Hi im new to opengl and i followed a tutorial to make a cube rotate with the mouse. The thing is that i added a point light and i wanted to rotate the light but in the inverse direction of the cube so that it is always lighting the front face. I rotate the cube with this: glRotatef( g_fRot[0], 1.0f, 0.0f, 0.0f ); glRotatef( g_fRot[1], 0.0f, 1.0f, 0.0f ); glRotatef( g_fRot[2], 0.0f, 0.0f, 1.0f ); And i have my light position with a vector g_fLight[] I really dont know how to do it :(
Brother Bob
Brother Bob
To transform the light with the object, set the light's position after making the object's transformations.
franchela
franchela
but im rotating the world not the cube, so if i rotate the world to the left i need the light to rotate to the right so its always in front of the camera.
reoxthen
reoxthen
to understand how transformations are done in opengl, you should clearly understand how matrices of opengl work.

here is a simple article showing opengl transformation matrices:

http://nehe.gamedev.net/data/articles/article.asp?article=02

and answer to your question:

to transform different objects independently you can always push your matrices, load identity matrix, do something else with this matrix and pop back the old one after you're done with the current.

opengl is a state machine. so;

Quote:
Original post by franchela
but im rotating the world not the cube, so if i rotate the world to the left i need the light to rotate to the right so its always in front of the camera.


no you don't rotate the world. opengl actually just multiplies your inputs with the current transformation matrix to set their position. so if you set your matrix to rotation when you draw your cube and set it back to identity when you set your light, the cube will rotate and the light will stand there.
franchela
franchela
void GL_View::Render( void )
{

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

// Translate view
glTranslatef ( -GL_View::g_fEye[0], -GL_View::g_fEye[1],-GL_View::g_fEye[2]);

// Rotates the screen by the angles
glRotatef( GL_View::g_fRot[0], 1.0f, 0.0f, 0.0f );
glRotatef( GL_View::g_fRot[1], 0.0f, 1.0f, 0.0f );
glRotatef( GL_View::g_fRot[2], 0.0f, 0.0f, 1.0f );

glLightfv( GL_LIGHT0, GL_POSITION, GL_View::g_light_position );

// Draw the environment
if( GL_View::g_bDrawSkybox && GL_View::g_cubeLoaded)GL_View::Draw_CubeMap();

if(GL_View::g_cgLoaded)GL_View::Draw_Cg();

if(GL_View::g_modelLoaded && GL_View::g_bDrawModel)GL_View::Draw_Model();

if(GL_View::g_cgLoaded)
{
GL_View::Disable_cg_Textures();
// Disable profiles
cgGLDisableProfile( GL_View::cgVertexProfile );
cgGLDisableProfile( GL_View::cgFragmentProfile );
}

SwapBuffers( GL_View::g_hDC );

if( GL_View::g_bRotate )GL_View::g_fRot[1] += 0.05f;

GL_View::cgErrorCallback("render");

}

This is my code, when the model and the cubemap rotate, the light rotates too, ¿What is the thing im missing?
Brother Bob
Brother Bob
But isn't that what you wanted, to have the light always on the the front of the cube? If you do like that, as the cube rotates, as so its front also rotates, the light will rotate with it.
_Unicron_
_Unicron_
I think when you say you light the front face of the cube, you mean the face that is closest towards you. At the moment you set the light position after you have set the translate and rotation transformations. This means that the position of the light source will be moved according to these transformations. Hence, the light rotating with your cube.

If you set the light position after you have set the modelview matrix to identity before any transformations the light will remain in the same position while your cube rotates independently of it. Here is the small modification to your code.

   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );   glMatrixMode( GL_MODELVIEW );   glLoadIdentity();   glLightfv( GL_LIGHT0, GL_POSITION, GL_View::g_light_position );   // Translate view   glTranslatef ( -GL_View::g_fEye[0], -GL_View::g_fEye[1],-GL_View::g_fEye[2]);   // Rotates the screen by the angles   glRotatef( GL_View::g_fRot[0], 1.0f, 0.0f, 0.0f );   glRotatef( GL_View::g_fRot[1], 0.0f, 1.0f, 0.0f );   glRotatef( GL_View::g_fRot[2], 0.0f, 0.0f, 1.0f );   // Draw the environment   if( GL_View::g_bDrawSkybox && GL_View::g_cubeLoaded)GL_View::Draw_CubeMap();   if(GL_View::g_cgLoaded)GL_View::Draw_Cg();   if(GL_View::g_modelLoaded && GL_View::g_bDrawModel)GL_View::Draw_Model();


Note, depending on your setup, you might have to swap around:
glLightfv( GL_LIGHT0, GL_POSITION, GL_View::g_light_position );
and
glTranslatef ( -GL_View::g_fEye[0], -GL_View::g_fEye[1],-GL_View::g_fEye[2]);
To get the exact results you desire. Either way will give you a statically positioned light source though.
franchela
franchela
Ive tried changing the position after rotating but the light rotates with the model, i want the light to illuminate the face of the model that is facing the camera, like if the model is rotating to the left and the light to the right.
Brother Bob
Brother Bob
Either I'm confused what you want, or you are confusd about your descriptions here. The object rotates, but what do you want with the light? Apparently you don't want it to rotate with the object, but you provide two other descriptions also. Either you want the light to not rotate at all, but you also say you want the light to rotate in the opposite direction.

If you don't want the light to rotate at all, then do as _Unicron_ said and rotate after setting the light. If you want the light to rotate in the opposite direction, then perform the inverse rotations (reverse order and with negative angles), set the light position, reset the transformations and do the object rotations.
_Unicron_
_Unicron_
I think your getting a little confused. Objects are only affected by the current matrix state. You don't rotate the world, any objects that have been processed are unaffected by subsequent transformations. In your system you are keeping your camera in a fixed position and rotating a cube.

Therefore you need to keep your light position stationary with respect to the camera. To do this you don't have to rotate the camera in the opposite direction of the cube. If you set your light position before updating the current modelview matrix with your rotations it won't be affected by them, i.e., it will stay with your camera.

So you don't have to rotate the light position with the cube and then unrotate it, which is what it sounds like you are trying to do.

Check my code segment I posted earlier, the important part is where glLightfv( GL_LIGHT0, GL_POSITION, GL_View::g_light_position ); has been moved to, (i.e before any rotation transformations are applied).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.