The light source rotates around an object and the position of the light is rotated when the left mouse button is pressed.
#include <GL/glut.h>
static int spin = 0;
static gldouble x_1 = 0.0;
static gldouble y_1 = 0.0;
static gldouble z_1 = 0.0;
void init (void)
{
Glclearcolor (0.0,0.0,0.0,0.0);
Glshademodel (Gl_smooth);
Glenable (gl_lighting);
Glenable (GL_LIGHT0);
Glenable (gl_depth_test);
}
void display (void)
{
Glfloat position[] = {0.0,0.0,1.5,1.0};
Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);
Glpushmatrix ();
Gltranslatef (0.0,0.0,-5.0);
Glpushmatrix ();
Glrotated ((gldouble) spin,x_1,y_1,z_1);
GLLIGHTFV (gl_light0,gl_position,position);
Gltranslated (0.0,0.0,1.5);
Gldisable (gl_lighting);
glcolor3f (0.0,1.0,1.0);
Glutwirecube (1);
Glenable (gl_lighting);
Glpopmatrix ();
Glutsolidtorus (0.275,0.85,8,15);
Glpopmatrix ();
Glflush ();
}
void reshape (int w, int h)
{
Glviewport (0,0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (40.0, (glfloat) w/(glfloat) h,1.0,20.0);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
}
void Mouse (int button, int state, int x, int y)
{
Switch (Button)
{
Case Glut_left_button:
if (state = = Glut_down)
{
X_1 = 1.0;
Y_1 = 0.0;
z_1 = 0.0;
Spin = (spin + 30)% 360;
Glutpostredisplay ();
}
Break
Case Glut_right_button:
if (state = = Glut_down)
{
x_1 = 0.0;
Y_1 = 1.0;
z_1 = 0.0;
Spin = (spin-20)% 360;
Glutpostredisplay ();
}
Break
Default
Break
}
}
int main (int argc,char** argv)
{
Glutinit (&ARGC,ARGV);
Glutinitdisplaymode (glut_single| glut_rgb| Glut_depth);
Glutinitwindowsize (500,500);
Glutinitwindowposition (100,100);
Glutcreatewindow (Argv[0]);
Init ();
Glutdisplayfunc (display);
Glutreshapefunc (reshape);
Glutmousefunc (mouse);
Glutmainloop ();
return 0;
}
OpenGL uses model view to transform moving light