Advertisement
patryk

Obracające się obrączki

Mar 31st, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <windows.h> //Wymagane dla implementacji OpenGL w Visual Studio.
  2. #include "gl\gl.h"
  3. #include "gl\glut.h"
  4. #include "stdio.h" //Przydatne do wypisywania komunikatów na konsoli
  5. #include "glm\glm.hpp"
  6. #include "glm\gtc\matrix_transform.hpp"
  7. #include "glm\gtc\type_ptr.hpp"
  8.  
  9. using namespace glm;
  10.  
  11. float speed=400; //60 stopni/s
  12. int lastTime=0;
  13. float angle;
  14. int direction;
  15.  
  16.  
  17.  
  18. void displayFrame(void) {
  19.     glClearColor(0,0,0,1);
  20.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  21.  
  22.     mat4 P=perspective(100.0f,1.0f,1.0f,50.0f);
  23.     mat4 V=lookAt(vec3(0.0f,0.0f,-5.0f),vec3(0.0f,0.0f,0.0f),vec3(0.0f,1.0f,0.0f));
  24.     mat4 M1 = mat4(1.0);
  25.     mat4 M2 = mat4(1.0);
  26.  
  27.     glMatrixMode(GL_PROJECTION);
  28.     glLoadMatrixf(value_ptr(P));
  29.     glMatrixMode(GL_MODELVIEW);
  30.  
  31.     M1=translate(M1,vec3(-2,0,0))*rotate(M1,angle,vec3(1.0f,0.0f,1.0f));
  32.     glLoadMatrixf(value_ptr(V*M1));
  33.     glColor3d(1,0.3,0);
  34.     glutSolidTorus(0.5,1.5,20,50);
  35.  
  36.     M2=translate(M2,vec3(2,0,0))*rotate(M2,-angle,vec3(0.3f,0.5f,0.0f));
  37.     glLoadMatrixf(value_ptr(V*M2));
  38.     glColor3d(1,0,0);
  39.     glutSolidTorus(0.5,1.5,20,50);
  40.  
  41.     glutSwapBuffers();
  42.  
  43. }
  44.  
  45. void nextFrame(void) {
  46.     int actTime=glutGet(GLUT_ELAPSED_TIME);
  47.     int interval=actTime-lastTime;
  48.     lastTime=actTime;
  49.     if (direction == 1) angle-=speed*interval/1000.0;
  50.     if (direction == -1) angle+=speed*interval/1000.0;
  51.     if (angle>360) angle-=360;
  52.     glutPostRedisplay();
  53. }
  54.  
  55. void keyDown(int c, int x, int y) {
  56.     if (c == GLUT_KEY_LEFT)  direction = 1;
  57.     if (c == GLUT_KEY_RIGHT)  direction = -1;
  58. }
  59.  
  60. void keyUp(int c, int x, int y) {
  61.     if (c == GLUT_KEY_LEFT)  direction = 0;
  62.     if (c == GLUT_KEY_RIGHT)  direction = 0;
  63. }
  64.  
  65. int main(int argc, char* argv[]) {
  66.     glutInit(&argc, argv);
  67.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  68.     glutInitWindowSize(800,800);
  69.     glutInitWindowPosition(0,0);
  70.     glutCreateWindow("Program OpenGL");        
  71.     glutDisplayFunc(displayFrame);
  72.        
  73.     //Tutaj kod inicjujący
  74.     glutIdleFunc(nextFrame);
  75.  
  76.     glEnable(GL_LIGHTING);
  77.     glEnable(GL_LIGHT0);
  78.     glEnable(GL_DEPTH_TEST);
  79.     glEnable(GL_COLOR_MATERIAL);
  80.  
  81.     glutSpecialFunc(keyDown);
  82.     glutSpecialUpFunc(keyUp);
  83.  
  84.     glutMainLoop();
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement