Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include<math.h>
- #ifdef __APPLE__
- #include <OpenGL/OpenGL.h>
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- using namespace std;
- //Called when a key is pressed
- void handleKeypress(unsigned char key, int x, int y) {
- switch (key) {
- case 27: //Escape key
- exit(0);
- }
- }
- //Initializes 3D rendering
- void initRendering() {
- glEnable(GL_DEPTH_TEST);
- }
- //Called when the window is resized
- void handleResize(int w, int h) {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
- }
- float _angle1 = 40.0f;
- //float _angle2 = 30.0f;
- //float _angle3 = 30.0f;
- float _cameraAngle = 0.0f;
- float x=0.0f;
- float y=-1.0f;
- float z=0.0f;
- //Draws the 3D scene
- void drawScene() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
- glLoadIdentity(); //Reset the drawing perspective
- glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); //Rotate the camera
- glTranslatef(0.0f, 1.0f, -5.0f); //Move forward 5 units
- glPushMatrix(); //Save the transformations performed thus far
- glBegin(GL_LINES);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(1.0, 0.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, -1.0f, 0.0f);
- glVertex3f(0.0, -1.0f, 0.0f);
- glVertex3f(1.0, -1.0f, 0.0f);
- glVertex3f(1.0f, 0.0f, 0.0f);
- glVertex3f(1.0f, -1.0f, 0.0f);
- glEnd();
- glBegin(GL_QUADS);
- glVertex3f(x-0.2f, y-0.2f, z);
- glVertex3f(x-0.2f, y+0.2f, z);
- glVertex3f(x+0.2f, y+0.2f, z);
- glVertex3f(x+0.2f, y-0.2f, z);
- glEnd();
- glPopMatrix(); //Undo the move to the center of the triangle
- glutSwapBuffers();
- }
- void update(int value) {
- x+=.01;
- if(x>1.0f && y<0.0f)
- {
- x=1.0f;
- y+=0.01;
- if(x==1.0f && y>0.0f)
- {
- x-=0.1;
- y=0.0f;
- // x=0.01;
- if(x<0.0f && y==0.0f)
- {
- x=0.0f;
- y-=0.01;
- }
- }}
- glutPostRedisplay(); //Tell GLUT that the display has changed
- //Tell GLUT to call update again in 25 milliseconds
- glutTimerFunc(25, update, 0);
- }
- int main(int argc, char** argv) {
- //Initialize GLUT
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(800, 800);
- //Create the window
- glutCreateWindow("Transformations and Timers ");
- initRendering();
- //Set handler functions
- glutDisplayFunc(drawScene);
- glutKeyboardFunc(handleKeypress);
- glutReshapeFunc(handleResize);
- glutTimerFunc(25, update, 0); //Add a timer
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement