Advertisement
sak1b

nouka nouka nouka

Dec 11th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 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. #include <stdlib.h>
  16. #include<Windows.h>
  17. #ifdef __APPLE__
  18. #include <GLUT/glut.h>
  19. #else
  20. #include <GL/glut.h>
  21. #endif
  22. #include<math.h>
  23.  
  24. #include<stdio.h>
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/GLAUX.h>
  28.  
  29. double a=0.0,b=-20.0,c=50.0;
  30. GLfloat l_t=0;
  31.  
  32. // Simple QUAD Draw
  33.  
  34. void quad_up()
  35. {
  36. glBegin(GL_QUADS);
  37.  
  38. glColor3f(0.1f,1.0f,0.3f);
  39.  
  40. glVertex3f(1.0f,1.0f,8.0f); // Top front Of The Quad (right)
  41. glVertex3f(1.0f,1.0f,1.0f); // Top back Of The Quad (right)
  42. glVertex3f(1.0f,-1.0f,1.0f); // Bottom back Of The Quad (right)
  43. glVertex3f(1.0f,-1.0f,8.0f); // Bottom front Of The Quad (right)
  44.  
  45. glColor3f(1.0f,1.0f,0.0f);
  46.  
  47. glVertex3f(-1.0f,1.0f,8.0f); // Top front Of The Quad (left)
  48. glVertex3f(-1.0f,1.0f,1.0f); // Top back Of The Quad (left)
  49. glVertex3f(-1.0f,-1.0f,1.0f); // Bottom back Of The Quad (left)
  50. glVertex3f(-1.0f,-1.0f,8.0f); // Bottom front Of The Quad (left)
  51.  
  52.  
  53. glColor3f(1.0f,0.0f,1.0f);
  54.  
  55. glVertex3f(1.0f,1.0f,8.0f); // Right front Of The Quad (top)
  56. glVertex3f(1.0f,1.0f,1.0f); // Right back Of The Quad (top)
  57. glVertex3f(-1.0f,1.0f,1.0f); // Left back Of The Quad (top)
  58. glVertex3f(-1.0f,1.0f,8.0f); // Left front Of The Quad (top)
  59.  
  60.  
  61. glColor3f(0.0f,1.0f,1.0f);
  62. glVertex3f(1.0f,-1.0f,8.0f); // Right front Of The Quad (bo quad();ttom)
  63. glVertex3f(1.0f,-1.0f,1.0f); // Right back Of The Quad (bottom)
  64. glVertex3f(-1.0f,-1.0f,1.0f); // Left back Of The Quad (bottom)
  65. glVertex3f(-1.0f,-1.0f,8.0f); // Left front Of The Quad (bottom)
  66.  
  67.  
  68. glColor3f(0.0f,0.0f,1.0f);
  69. glVertex3f(1.0f,1.0f,1.0f); // Top Right Of The Quad (Back)
  70. glVertex3f(-1.0f,1.0f,1.0f); // Top Left Of The Quad (Back)
  71. glVertex3f(-1.0f,-1.0f,1.0f); // Bottom Left Of The Quad (Back)
  72. glVertex3f(1.0f,-1.0f,1.0f); // Bottom Right Of The Quad (Back)
  73.  
  74.  
  75. glColor3f(1.0f,0.0f,0.0f);
  76. glVertex3f(1.0f,1.0f,8.0f); // Top Right Of The Quad (Front)
  77. glVertex3f(-1.0f,1.0f,8.0f); // Top Left Of The Quad (Front)
  78. glVertex3f(-1.0f,-1.0f,8.0f); // Bottom Left Of The Quad (Front)
  79. glVertex3f(1.0f,-1.0f,8.0f); // Bottom Right Of The Quad (Front)
  80.  
  81. glEnd();
  82. }
  83.  
  84. void quad()
  85. {
  86. // Draw A Quad
  87. glBegin(GL_QUADS);
  88.  
  89. glColor3f(0.2f,1.0f,0.3f);
  90.  
  91. glVertex3f(2.0f,1.0f,8.0f); // Top front Of The Quad (right)
  92. glVertex3f(2.0f,1.0f,1.0f); // Top back Of The Quad (right)
  93. glVertex3f(1.0f,-1.0f,1.0f); // Bottom back Of The Quad (right)
  94. glVertex3f(1.0f,-1.0f,8.0f); // Bottom front Of The Quad (right)
  95.  
  96. glColor3f(1.0f,1.0f,0.0f);
  97.  
  98. glVertex3f(-2.0f,1.0f,8.0f); // Top front Of The Quad (left)
  99. glVertex3f(-2.0f,1.0f,1.0f); // Top back Of The Quad (left)
  100. glVertex3f(-1.0f,-1.0f,1.0f); // Bottom back Of The Quad (left)
  101. glVertex3f(-1.0f,-1.0f,8.0f); // Bottom front Of The Quad (left)
  102.  
  103.  
  104. glColor3f(1.0f,0.0f,1.0f);
  105.  
  106. glVertex3f(2.0f,1.0f,8.0f); // Right front Of The Quad (top)
  107. glVertex3f(2.0f,1.0f,1.0f); // Right back Of The Quad (top)
  108. glVertex3f(-2.0f,1.0f,1.0f); // Left back Of The Quad (top)
  109. glVertex3f(-2.0f,1.0f,8.0f); // Left front Of The Quad (top)
  110.  
  111.  
  112. glColor3f(0.0f,1.0f,1.0f);
  113. glVertex3f(1.0f,-1.0f,8.0f); // Right front Of The Quad (bo quad();ttom)
  114. glVertex3f(1.0f,-1.0f,1.0f); // Right back Of The Quad (bottom)
  115. glVertex3f(-1.0f,-1.0f,1.0f); // Left back Of The Quad (bottom)
  116. glVertex3f(-1.0f,-1.0f,8.0f); // Left front Of The Quad (bottom)
  117.  
  118.  
  119. glColor3f(0.0f,0.0f,1.0f);
  120.  
  121. glVertex3f(2.0f,1.0f,1.0f); // Top Right Of The Quad (Back)
  122. glVertex3f(-2.0f,1.0f,1.0f); // Top Left Of The Quad (Back)
  123. glVertex3f(-1.0f,-1.0f,1.0f); // Bottom Left Of The Quad (Back)
  124. glVertex3f(1.0f,-1.0f,1.0f); // Bottom Right Of The Quad (Back)
  125.  
  126.  
  127. glColor3f(1.0f,0.0f,0.0f);
  128.  
  129. glVertex3f(2.0f,1.0f,8.0f); // Top Right Of The Quad (Front)
  130. glVertex3f(-2.0f,1.0f,8.0f); // Top Left Of The Quad (Front)
  131. glVertex3f(-1.0f,-1.0f,8.0f); // Bottom Left Of The Quad (Front)
  132. glVertex3f(1.0f,-1.0f,8.0f); // Bottom Right Of The Quad (Front)
  133.  
  134. glEnd();
  135. }
  136.  
  137. void init(void)
  138. {
  139.  
  140. glClearColor (0, 0, 0, 0);
  141. }
  142.  
  143. void display(void)
  144. {
  145.  
  146. glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); // GL_COLOR_BUFFER_BIT indicates that the buffer is currently enabled for color writing, GL_DEPTH_BUFFER_BIT indicates that the buffer is currently enabled for depth buffer
  147. glMatrixMode(GL_PROJECTION); // Specify which matrix is the current matrix mode
  148. glLoadIdentity(); // Replace the current matrix with the identity matrix and essentially resets the matrix back to its default state
  149. int w=glutGet(GLUT_WINDOW_WIDTH);
  150. int h=glutGet(GLUT_WINDOW_HEIGHT);
  151. gluPerspective(40,w/h,0.1,100); // fovy specifies the field of view angle in degrees in y direction, aspect specifies aspect ratio that determines the field of view in the x direction, znear and zfar specifies the distance from viewer to the near and far clipping plane respectively (always positive)
  152. glMatrixMode(GL_MODELVIEW); // To allow modeling and viewing transformation on 3D object
  153.  
  154. glLoadIdentity();
  155. gluLookAt( // eyex, eyey, eyez specify the position of the eye point, centerx, centery, centerz specify the position of reference point, upx, upy, upz specify the direction of up vector
  156. a,b,c,
  157. 0,0,0,
  158. 0,1,0
  159. );
  160.  
  161. glPushMatrix();
  162.  
  163. glRotatef(l_t,0.0f,1.0f,0.0f);
  164.  
  165.  
  166.  
  167. quad();
  168.  
  169. glPushMatrix();
  170. glScalef(1,0.5,1);
  171. glTranslatef(0,3,0);
  172. quad_up();
  173. glPopMatrix();
  174.  
  175. glPushMatrix();
  176. glScalef(0.5,0.25,1);
  177. glTranslatef(0,9,0);
  178. quad_up();
  179. glPopMatrix();
  180.  
  181.  
  182.  
  183. glPopMatrix();
  184.  
  185. glutSwapBuffers();
  186.  
  187. }
  188. void my_keyboard(unsigned char key, int p, int q)
  189. {
  190. switch (key) {
  191. case 'f': //front view
  192. a=0.0,b=0.0,c=15.0;
  193. glutPostRedisplay();
  194. break;
  195. case 'b': //back view
  196. a=0.0,b=0.0,c=-15.0;
  197. glutPostRedisplay();
  198. break;
  199. case 'l': //left view
  200. a=-50.0,b=0.0,c=0.0;
  201. glutPostRedisplay();
  202. break;
  203. case 'r': //right view
  204. a=50.0,b=0.0,c=0.0;
  205. glutPostRedisplay();
  206. break;
  207. case 't': //top view
  208. a=0.0,b=50.0,c=0.0001;
  209. glutPostRedisplay();
  210. break;
  211. case 'm': //bottom view
  212. a=0.0,b=-50.0,c=0.0001;
  213. glutPostRedisplay();
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219.  
  220. void spinDisplay_left(void) //spin speed
  221. {
  222.  
  223. l_t =l_t-0.2;
  224. glutPostRedisplay();
  225. }
  226.  
  227. void spinDisplay_right(void) //spin speed
  228. {
  229.  
  230. l_t =l_t+0.2;
  231. glutPostRedisplay();
  232. }
  233.  
  234. void specialKeys(int key,int x,int y)
  235. {
  236. if(key==GLUT_KEY_RIGHT)
  237. a++;
  238. else if(key==GLUT_KEY_LEFT)
  239. a--;
  240. else if(key==GLUT_KEY_UP)
  241. c--;
  242. else if(key==GLUT_KEY_DOWN)
  243. c++;
  244. glutPostRedisplay();
  245. }
  246.  
  247. void mouse(int button, int state, int s, int t)
  248. {
  249.  
  250. switch (button)
  251. {
  252. case GLUT_LEFT_BUTTON:
  253. if (state == GLUT_DOWN)
  254. glutIdleFunc(spinDisplay_left);
  255. break;
  256. case GLUT_RIGHT_BUTTON:
  257. if (state == GLUT_DOWN)
  258. glutIdleFunc(spinDisplay_right);
  259. break;
  260. case GLUT_MIDDLE_BUTTON:
  261. if (state == GLUT_DOWN)
  262. glutIdleFunc(NULL);
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268.  
  269. int main()
  270. {
  271. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  272. glutInitWindowSize (800, 800);
  273. glutInitWindowPosition (0, 0);
  274. glutCreateWindow ("3D View of a Quad");
  275. init();
  276. glEnable(GL_DEPTH_TEST);
  277. glutSpecialFunc(specialKeys);
  278. glutDisplayFunc(display);
  279. glutKeyboardFunc(my_keyboard);
  280. glutMouseFunc(mouse);
  281. glutMainLoop();
  282. return 0;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement