Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #pragma comment (lib,"glut32.lib")
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <GL/glut.h>
- float lx=0.0f,lz=-1.0f;
- float x=0.0f,z=5.0f;
- float angle = 0.0f;
- void change(int w, int h)
- {
- float ratio = w * 1.0 / h;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glViewport(0, 0, w, h);
- gluPerspective(45.0f, ratio, 0.1f, 100.0f);
- glMatrixMode(GL_MODELVIEW);
- }
- void draw(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt( x, 1.0f, z,
- x+lx, 1.0f, z+lz,
- 0.0f, 1.0f, 0.0f);
- // Draw ground
- glColor3f(0.9f, 0.9f, 0.9f);
- glBegin(GL_QUADS);
- glVertex3f(-10.0f, 0.0f, -10.0f);
- glVertex3f(-10.0f, 0.0f, 10.0f);
- glVertex3f( 10.0f, 0.0f, 10.0f);
- glVertex3f( 10.0f, 0.0f, -10.0f);
- glEnd();
- glutSwapBuffers();
- }
- void processNormalKeys(unsigned char key, int x, int y)
- {
- if (key == 27)
- exit(0);
- }
- void processSpecialKeys(int key, int xx, int yy)
- {
- float fraction = 0.1f;
- switch (key) {
- case GLUT_KEY_LEFT :
- angle -= 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case GLUT_KEY_RIGHT :
- angle += 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case GLUT_KEY_UP :
- x += lx * fraction;
- z += lz * fraction;
- break;
- case GLUT_KEY_DOWN :
- x -= lx * fraction;
- z -= lz * fraction;
- break;
- }
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(320,320);
- glutCreateWindow("Lighthouse3D - GLUT Tutorial");
- glutDisplayFunc(draw);
- glutReshapeFunc(change);
- glutIdleFunc(draw);
- glutKeyboardFunc(processNormalKeys);
- glutSpecialFunc(processSpecialKeys);
- glEnable(GL_DEPTH_TEST);
- glutMainLoop();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment