OpenGL uses model view to transform moving light

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.