Advertisement
didito33

Untitled

Nov 13th, 2023
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. /*
  2. * GLUT Shapes Demo
  3. *
  4. * Written by Nigel Stewart November 2003
  5. *
  6. * This program is test harness for the sphere, cone
  7. * and torus shapes in GLUT.
  8. *
  9. * Spinning wireframe and smooth shaded shapes are
  10. * displayed until the ESC or q key is pressed. The
  11. * number of geometry stacks and slices can be adjusted
  12. * using the + and - keys.
  13. */
  14.  
  15. #ifdef __APPLE__
  16. #include <GLUT/glut.h>
  17. #else
  18. #include <GL/glut.h>
  19. #endif
  20.  
  21. #include <stdlib.h>
  22. #include <math.h>
  23.  
  24. void drawTriangle(int R, int G, int B, float x1, float y1, float x2, float y2, float x3, float y3)
  25. {
  26. glBegin(GL_TRIANGLES);
  27. glColor3ub(R, G, B);
  28. glVertex2f(x1, y1);
  29. glColor3ub(G, B, R);
  30. glVertex2f(x2, y2);
  31. glColor3ub(B, R, G);
  32. glVertex2f(x3, y3);
  33. glEnd();
  34. }
  35.  
  36. void drawCircle(int points)
  37. {
  38. glBegin(GL_LINE_LOOP);
  39. //set the color in 3 float number (red, green, blue)
  40. glColor3f(0.0, 1.0, 0.0);
  41.  
  42. float angle, x, y;
  43. for (int i = 0; i < points; i++){
  44. angle = 6.2832 * i / points;
  45. x = 0.5 * cos(angle);
  46. y = 0.5 * sin(angle);
  47. glVertex2f(x, y);
  48. }
  49. glEnd();
  50. }
  51. void square(float R, float G, float B){
  52. glColor3f(R,G,B);
  53. glBegin(GL_POLYGON);
  54. glVertex3f(0, 0, 0.5);
  55. glVertex3f(0.5, 0, 0.5);
  56. glVertex3f(0.5, 0.5, 0.5);
  57. glVertex3f(0, 0.5, 0.5);
  58. glEnd();
  59. }
  60. void cube(float size){
  61. glPushMatrix(); //save a copy of the current matrix
  62. glScalef(size,size,size);
  63. square(1, 0, 0); //red color side
  64. glPushMatrix();
  65. glRotatef(90, 0, 1, 0); //rotate by y
  66. square(0, 1, 0); //green color side
  67. glPopMatrix();
  68. glPushMatrix();
  69. glRotatef(-90, 1, 0, 0);
  70. square(0, 0, 1);
  71. glPopMatrix();
  72. glPushMatrix();
  73. glRotatef(180, 0, 1, 0);
  74. square(0, 1, 1);
  75. glPopMatrix();
  76. glPushMatrix();
  77. glRotatef(-90, 0, 1, 0);
  78. square(1, 0, 1);
  79. glPopMatrix();
  80. glPushMatrix();
  81. glRotatef(90, 1, 0, 0);
  82. square(1, 1, 0);
  83. glPopMatrix();
  84. glPopMatrix(); // restore matrix to its state before cube() was created.
  85. }
  86. void display(){
  87. //clear memory color buffer
  88. glClear(GL_COLOR_BUFFER_BIT);
  89. glLoadIdentity();
  90. //drawTriangle(66, 135, 245, -0.5, -0.5, 0.5, -0.5, 0, 0.5);
  91. //drawCircle(64);
  92. //drawTriangle(0, 0, 255, -1, -1, 0, 1, 1, -1);
  93. //glTranslatef(0.00005, 0.0, 0.0);
  94. static double x = 15;
  95. x = x + 0.15;
  96. glRotatef(x,0,0,1);
  97.  
  98.  
  99. glPushMatrix();
  100. drawTriangle(0, 0, 255, 0, 0, 0.5, 0.5, 0.5, 0);
  101. glPopMatrix();
  102.  
  103. glPushMatrix();
  104. drawTriangle(0, 0, 255, 0, 0, -0.5, -0.5, -0.5, 0);
  105. glPopMatrix();
  106.  
  107. glPushMatrix();
  108. drawTriangle(0, 0, 255, 0, 0, 0, -0.5, 0.5, -0.5);
  109. glPopMatrix();
  110.  
  111. glPushMatrix();
  112. drawTriangle(0, 0, 255, 0, 0, 0, 0.5, -0.5, 0.5);
  113. glPopMatrix();
  114.  
  115.  
  116.  
  117. /*drawTriangle(247, 128, 30,-0.5,0.5,0.5,1,0,0.5);
  118.  
  119. glPushMatrix();
  120. glTranslatef(0.03, 0, 0);
  121. glScalef(0.4,0.5,0.7);
  122. glRotatef(150,0,1,0);
  123. drawTriangle(64, 135, 255, 0.5,0.5,0.5,-0.5,-0.5,-0.5);
  124. glPopMatrix();
  125. */
  126. /*glPushMatrix();
  127. glTranslatef(0.00005, 0.0, 0.0);
  128. drawTriangle(0, 0, 255, -1, -1, 0, 1, 1, -1);
  129. glPopMatrix();*/
  130.  
  131. //switch
  132. //memory buffer
  133. glutSwapBuffers();
  134. }
  135. //reshape windows
  136. void reshape(int WIDTH, int HEIGHT){
  137. //sets viewport size from X to win width and from Y to win height
  138. glViewport(0, 0, WIDTH, HEIGHT);
  139. }
  140.  
  141. void onInit(){
  142. //glClearColor(R,G,B,A) sets windows background RGB color + alpha channel
  143. glClearColor(0.0, 0.0, 0.0, 1.0);
  144. }
  145.  
  146. int main(int argc, char **argv){
  147.  
  148. //glutInit is used to initialize the GLUT library
  149. glutInit(&argc, argv);
  150.  
  151. // glutInitDisplayMode sets the initial display modes
  152. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  153. // glutInitWindowSize sets windows size in pixels (W, H)
  154. glutInitWindowSize(720, 720);
  155. // glutWindowsPosition sets windows position, from left down corner
  156. glutInitWindowPosition(100,100);
  157. // glutCreateWindows create windows and sets windows title
  158. glutCreateWindow ("My first OpenGL Windows");
  159.  
  160. onInit();
  161.  
  162. glutDisplayFunc(display);
  163. glutIdleFunc(display);
  164. glutReshapeFunc(reshape);
  165. glutMainLoop();
  166.  
  167. return 0;
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement