Advertisement
patryk

Obrączki + ptaszek

Mar 31st, 2015
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 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.  
  19. void displayFrame(void) {
  20.     glClearColor(0,0,0,1);
  21.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  22.  
  23.     mat4 P=perspective(100.0f,1.0f,1.0f,50.0f);
  24.     mat4 V=lookAt(vec3(0.0f,0.0f,-5.0f),vec3(0.0f,0.0f,0.0f),vec3(0.0f,1.0f,0.0f));
  25.     mat4 M1 = mat4(1.0);
  26.     mat4 M2 = mat4(1.0);
  27.  
  28.     glMatrixMode(GL_PROJECTION);
  29.     glLoadMatrixf(value_ptr(P));
  30.     glMatrixMode(GL_MODELVIEW);
  31.  
  32.     M1=translate(M1,vec3(-2,0,0))*rotate(M1,angle,vec3(1.0f,0.0f,1.0f));
  33.     glLoadMatrixf(value_ptr(V*M1));
  34.     glColor3d(1,0.3,0);
  35.     //glutSolidTorus(0.5,1.5,20,50);
  36.     float tri1[]={
  37.     1,2,0,
  38.     -1,0 ,1,
  39.     1,1,0
  40. };
  41.  
  42.     float tri1Colors[] = {
  43.         1, 0, 0,
  44.         1, 0, 0,
  45.         1,0, 0
  46.     };
  47.  
  48.    
  49.     float tri2[]={
  50.     1,2,0,
  51.     2,0 ,1,
  52.     1,1,0
  53.     };
  54.  
  55.     float tri2Colors[] = {
  56.         1, 0, 0,
  57.         1, 0, 0,
  58.         1,0, 0
  59.     };
  60.  
  61.  
  62.     int tri1VertexCount = 3;
  63.     int tri2VertexCount = 3;
  64.  
  65.     glEnableClientState(GL_VERTEX_ARRAY);
  66.     glVertexPointer(3, GL_FLOAT, 0, tri1);
  67.     glColorPointer(3, GL_FLOAT, 0, tri1Colors);
  68.     glDrawArrays(GL_TRIANGLES, 0, tri1VertexCount);
  69.     glDisableClientState(GL_VERTEX_ARRAY);
  70.  
  71.     glEnableClientState(GL_VERTEX_ARRAY);
  72.     glVertexPointer(3, GL_FLOAT, 0, tri2);
  73.     glColorPointer(3, GL_FLOAT, 0, tri2Colors);
  74.     glDrawArrays(GL_TRIANGLES, 0, tri2VertexCount);
  75.     glDisableClientState(GL_VERTEX_ARRAY);
  76.  
  77.     M2=translate(M2,vec3(2,0,0))*rotate(M2,-angle,vec3(0.3f,0.5f,0.0f));
  78.     glLoadMatrixf(value_ptr(V*M2));
  79.     glColor3d(1,0,0);
  80.     glutSolidTorus(0.5,1.5,20,50);
  81.  
  82.     glutSwapBuffers();
  83.  
  84. }
  85.  
  86. void nextFrame(void) {
  87.     int actTime=glutGet(GLUT_ELAPSED_TIME);
  88.     int interval=actTime-lastTime;
  89.     lastTime=actTime;
  90.     if (direction == 1) angle-=speed*interval/1000.0;
  91.     if (direction == -1) angle+=speed*interval/1000.0;
  92.     if (angle>360) angle-=360;
  93.     glutPostRedisplay();
  94. }
  95.  
  96. void keyDown(int c, int x, int y) {
  97.     if (c == GLUT_KEY_LEFT)  direction = 1;
  98.     if (c == GLUT_KEY_RIGHT)  direction = -1;
  99. }
  100.  
  101. void keyUp(int c, int x, int y) {
  102.     if (c == GLUT_KEY_LEFT)  direction = 0;
  103.     if (c == GLUT_KEY_RIGHT)  direction = 0;
  104. }
  105.  
  106. int main(int argc, char* argv[]) {
  107.     glutInit(&argc, argv);
  108.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  109.     glutInitWindowSize(800,800);
  110.     glutInitWindowPosition(0,0);
  111.     glutCreateWindow("Program OpenGL");        
  112.     glutDisplayFunc(displayFrame);
  113.        
  114.     //Tutaj kod inicjujący
  115.     glutIdleFunc(nextFrame);
  116.  
  117.     //glEnable(GL_LIGHTING);
  118.     glEnable(GL_LIGHT0);
  119.     glEnable(GL_DEPTH_TEST);
  120.     glEnable(GL_COLOR_MATERIAL);
  121.  
  122.     glutSpecialFunc(keyDown);
  123.     glutSpecialUpFunc(keyUp);
  124.  
  125.     glutMainLoop();
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement