Advertisement
Queses

Untitled

Oct 20th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include <GL/glut.h>
  2.  
  3. #include <math.h>
  4.  
  5. float angle = 0.0f;
  6.  
  7.  
  8. void changeSize(int w, int h)
  9. {
  10. if (h == 0) h = 1;
  11. float ratio = w * 1.0 / h;
  12. glMatrixMode(GL_PROJECTION);
  13. glLoadIdentity();
  14. glViewport(0, 0, w, h);
  15. gluPerspective(45.0f, ratio, 0.1f, 100.0f);
  16.  
  17. glMatrixMode(GL_MODELVIEW);
  18. }
  19.  
  20. void myRotatef(GLfloat a, GLfloat x, GLfloat y, GLfloat z)
  21. {
  22. a = a * 3.14 / 18.0; // convert to radians
  23. GLfloat s = sin(a);
  24. GLfloat c = cos(a);
  25. GLfloat t = 1.0 - c;
  26.  
  27. GLfloat tx = t * x;
  28. GLfloat ty = t * y;
  29. GLfloat tz = t * z;
  30.  
  31. GLfloat sz = s * z;
  32. GLfloat sy = s * y;
  33. GLfloat sx = s * x;
  34.  
  35. float matrix[16];
  36.  
  37. matrix[0] = tx * x + c;
  38. matrix[1] = tx * y + sz;
  39. matrix[2] = tx * z - sy;
  40. matrix[3] = 0;
  41.  
  42. matrix[4] = tx * y - sz;
  43. matrix[5] = ty * y + c;
  44. matrix[6] = ty * z + sx;
  45. matrix[7] = 0;
  46.  
  47. matrix[8] = tx * z + sy;
  48. matrix[9] = ty * z - sx;
  49. matrix[10] = tz * z + c;
  50. matrix[11] = 0;
  51.  
  52. matrix[12] = 0;
  53. matrix[13] = 0;
  54. matrix[14] = 0;
  55. matrix[15] = 1;
  56.  
  57. glMultMatrixf(&matrix[0]);
  58. }
  59.  
  60. void myDrawLetter()
  61. {
  62. glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
  63. // Top face (y = 1.0f)
  64. // Define vertices in counter-clockwise (CCW) order with normal pointing out
  65. glColor3f(0.0f, 1.0f, 0.0f); // Green
  66. glVertex3f( 1.0f, 1.0f, -1.0f);
  67. glVertex3f(-1.0f, 1.0f, -1.0f);
  68. glVertex3f(-1.0f, 1.0f, 1.0f);
  69. glVertex3f( 1.0f, 1.0f, 1.0f);
  70.  
  71. // Bottom face (y = -1.0f)
  72. glColor3f(1.0f, 0.5f, 0.0f); // Orange
  73. glVertex3f( 1.0f, -1.0f, 1.0f);
  74. glVertex3f(-1.0f, -1.0f, 1.0f);
  75. glVertex3f(-1.0f, -1.0f, -1.0f);
  76. glVertex3f( 1.0f, -1.0f, -1.0f);
  77.  
  78. // Front face (z = 1.0f)
  79. glColor3f(1.0f, 0.0f, 0.0f); // Red
  80. glVertex3f( 1.0f, 1.0f, 1.0f);
  81. glVertex3f(-1.0f, 1.0f, 1.0f);
  82. glVertex3f(-1.0f, -1.0f, 1.0f);
  83. glVertex3f( 1.0f, -1.0f, 1.0f);
  84.  
  85. // Back face (z = -1.0f)
  86. glColor3f(1.0f, 1.0f, 0.0f); // Yellow
  87. glVertex3f( 1.0f, -1.0f, -1.0f);
  88. glVertex3f(-1.0f, -1.0f, -1.0f);
  89. glVertex3f(-1.0f, 1.0f, -1.0f);
  90. glVertex3f( 1.0f, 1.0f, -1.0f);
  91.  
  92. // Left face (x = -1.0f)
  93. glColor3f(0.0f, 0.0f, 1.0f); // Blue
  94. glVertex3f(-1.0f, 1.0f, 1.0f);
  95. glVertex3f(-1.0f, 1.0f, -1.0f);
  96. glVertex3f(-1.0f, -1.0f, -1.0f);
  97. glVertex3f(-1.0f, -1.0f, 1.0f);
  98.  
  99. // Right face (x = 1.0f)
  100. glColor3f(1.0f, 0.0f, 1.0f); // Magenta
  101. glVertex3f(1.0f, 1.0f, -1.0f);
  102. glVertex3f(1.0f, 1.0f, 1.0f);
  103. glVertex3f(1.0f, -1.0f, 1.0f);
  104. glVertex3f(1.0f, -1.0f, -1.0f);
  105. glEnd(); // End of drawing color-cube
  106. }
  107.  
  108. void renderScene()
  109. {
  110. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  111. glEnable(GL_DEPTH_TEST);
  112.  
  113. glMatrixMode(GL_MODELVIEW);
  114.  
  115. glLoadIdentity();
  116. gluLookAt( 10.0f, 10.0f, 10.0f,
  117. 0.0f, 0.0f, 0.0f,
  118. 5.0f, 5.0f, 0.0f);
  119.  
  120. myRotatef(angle*10, 0.0f, 1.0f, 0.0f);
  121. //glRotatef(angle*10, 0.0f, 1.0f, 0.0f);
  122. myDrawLetter();
  123.  
  124. angle+=0.05f;
  125.  
  126. glutSwapBuffers();
  127. }
  128.  
  129. int main(int argc, char **argv)
  130. {
  131. glutInit(&argc, argv);
  132. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  133. glutInitWindowPosition(100,100);
  134. glutInitWindowSize(400,400);
  135. glutCreateWindow("Quartenion");
  136.  
  137. glutDisplayFunc(renderScene);
  138. glutReshapeFunc(changeSize);
  139. glutIdleFunc(renderScene);
  140.  
  141. glutMainLoop();
  142.  
  143. return 1;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement