#include <windows.h>
#include
<gl.h>
#include
<glu.h>
#include
<glut.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<math.h>
GLfloat
LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat
LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat
LightPosition[]= { 5.0f, 25.0f, 15.0f, 1.0f };
GLfloat
mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
static int
view_state = 0, light_state = 0;
int spin;
void Sprint(
int x, int y, char *st)
{
int l,i;
l=strlen( st );
glRasterPos2i( x, y);
for( i=0; i < l; i++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,
st[i]);
}
}
static void
TimeEvent(int te)
{
spin++; // increase cube rotation
by 1
if (spin > 360) spin = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
void
init(void)
{
glClearColor
(0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
// Lighting
is added to scene
glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
}
void
display(void)
{
glClear
(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode
(GL_PROJECTION);
glLoadIdentity();
glOrtho(-8.0,
8.0, -8.0, 8.0, 0.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Clear the model matrix
if
(view_state == 1)
{
glColor3f(
1.0, 1.0, 1.0);
Sprint(-2,
4, "Perspective view");
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 30);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}else
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2,
4, "Ortho view");
}
// Lighting
on/off
if
(light_state == 1)
{
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
}else
{
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}
gluLookAt( 0,
0, 20, 0, 0, 0, 0, 1, 0);
glColor3f(
0.0, 0.0, 1.0); // Cube color
glRotatef( 45,
1.0, 1.0, 1.0); // rotate cube
glRotatef(
spin++, 0.0, 1.0, 0.0); // spin cube
glTranslatef(0.0,0.0, -10.0);
glutWireCone(5.0, 10.0, 8, 8); // Draw a cube
glRotatef(290,
0.0, 1.0, 0.0);
glutSwapBuffers();
}
void reshape
(int w, int h)
{
glViewport
(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}
void keyboard
(unsigned char key, int x, int y)
{
switch (key)
{
case 'v':
case 'V':
view_state = abs(view_state -1);
break;
case 'l':
case 'L':
light_state = abs(light_state -1);
break;
case 27:
exit(0); // exit program when [ESC] key presseed
break;
default:
break;
}
}
int main(int
argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (10, 10);
glutCreateWindow (argv[0]);
glutSetWindowTitle("GlutWindow");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}
No comments:
Post a Comment