Guest User

Untitled

a guest
Apr 23rd, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <glut.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <math.h>
  7.  
  8. #define M_PI 3.14159265358979323846
  9. #define deg2rad(a) (a)* M_PI / 180
  10.  
  11. GLfloat aspect = 1.0;
  12. GLuint width = 1, height = 1;
  13.  
  14. GLuint delay = 25; // delay 25 msec = 40 fps (max)
  15.  
  16. enum Planets { Mercury = 0, Venus, Terra, Mars };
  17. enum Moons { Moon = 4, Phobos, Deimos };
  18.  
  19. GLfloat radius [] = { 5, 12, 13, 7, 4, 2, 2 };
  20.  
  21. GLfloat dist [] = { 58, 108, 150, 207, 20, 15, 10 };
  22.  
  23. GLfloat period [] = { 0.3, 0.6, 1, 2, 0.3, 0.35, 0.25 };
  24.  
  25. GLfloat spin [] = {0, 0, 0, 0, 0, 0, 0, 0};
  26.  
  27. GLfloat fdist = 1;
  28. GLfloat fperiod = 1;
  29. GLfloat fradius = 1;
  30.  
  31. void normalize( GLfloat *xx, GLfloat *yy, GLfloat *zz,
  32. GLfloat x, GLfloat y, GLfloat z )
  33. {
  34. GLfloat mag = sqrt(x*x + y*y + z*z);
  35. *xx = x/mag; *yy = y/mag; *zz = z/mag;
  36. }
  37.  
  38. void cross( GLfloat *rx, GLfloat *ry, GLfloat *rz,
  39. GLfloat ax, GLfloat ay, GLfloat az,
  40. GLfloat bx, GLfloat by, GLfloat bz)
  41. {
  42. *rx = ay*bz - az*by;
  43. *ry = az*bx - ax*bz;
  44. *rz = ax*by - ay*bx;
  45. }
  46.  
  47. void myPerspective(GLfloat fov, GLfloat asp, GLfloat zNear, GLfloat zFar)
  48. {
  49. //gluPerspective(fov, asp, zNear, zFar); // todo:
  50. GLfloat f = 2/tan(deg2rad(fov));
  51. GLfloat m[16] = {f/asp, 0, 0, 0,
  52. 0, f, 0, 0,
  53. 0, 0, (zNear+zFar)/(zNear-zFar), -1,
  54. 0, 0, (2*zNear*zFar)/(zNear-zFar), 0};
  55. glMultMatrixf(m);
  56. }
  57.  
  58. void myTranslate(GLfloat x, GLfloat y, GLfloat z)
  59. {
  60. //glTranslatef(x, y, z); // todo:
  61. GLfloat m[16] = {1, 0, 0, 0,
  62. 0, 1, 0, 0,
  63. 0, 0, 1, 0,
  64. x, y, z, 1};
  65. glMultMatrixf(m);
  66. }
  67.  
  68. void myLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,
  69. GLdouble centerx, GLdouble centery, GLdouble centerz,
  70. GLdouble upx, GLdouble upy, GLdouble upz)
  71. {
  72. /*gluLookAt(eyex, eyey, eyez,
  73. centerx, centery, centerz,
  74. upx, upy, upz); */// todo:
  75. GLfloat Fx = centerx-eyex;
  76. GLfloat Fy = centery-eyey;
  77. GLfloat Fz = centerz-eyez;
  78. GLfloat fxx, fyy, fzz;
  79. normalize (&fxx, &fyy, &fzz, Fx, Fy, Fz);
  80.  
  81. GLfloat sx, sy, sz;
  82. cross (&sx, &sy, &sz, fxx, fyy, fzz, upx, upy, upz);
  83.  
  84. GLfloat sxx, syy, szz;
  85. normalize (&sxx, &syy, &szz, sx, sy, sz);
  86.  
  87. GLfloat ux, uy, uz;
  88. cross (&ux, &uy, &uz, sxx, syy, szz, fxx, fyy, fzz);
  89.  
  90. GLfloat uxx, uyy, uzz;
  91. normalize (&uxx, &uyy, &uzz, ux, uy, uz);
  92.  
  93. const GLfloat m[16] = {sxx, uxx, -fxx, 0,
  94. syy, uyy, -fyy, 0,
  95. szz, uzz, -fzz, 0,
  96. 0, 0, 0, 1};
  97.  
  98. glMultMatrixf (m);
  99. myTranslate(-eyex, -eyey, -eyez);
  100. }
  101.  
  102. void myRotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
  103. {
  104. //glRotatef(angle, x, y, z); // todo:
  105. GLfloat s = sin(deg2rad(angle));
  106. GLfloat c = cos(deg2rad(angle));
  107. GLfloat m[16] = {x*x*(1-c)+c, y*x*(1-c)+z*s, z*x*(1-c)-y*s, 0,
  108. x*y*(1-c)-z*s, y*y*(1-c)+c, z*y*(1-c)+x*s, 0,
  109. x*z*(1-c)+y*s, y*z*(1-c)+x*s, z*z*(1-c)+c, 0,
  110. 0, 0, 0, 1};
  111. glMultMatrixf(m);
  112. }
  113.  
  114. void myScale(GLfloat x, GLfloat y, GLfloat z)
  115. {
  116. //glScalef(x, y, z); // todo:
  117. GLfloat m[16] = {x, 0, 0, 0,
  118. 0, y, 0, 0,
  119. 0, 0, z, 0,
  120. 0, 0, 0, 1};
  121. glMultMatrixf(m);
  122. }
  123.  
  124. void init(void)
  125. {
  126. glClearColor(0.0, 0.0, 0.0, 0.0);
  127. glEnable(GL_DEPTH_TEST);
  128. glEnable(GL_NORMALIZE);
  129. glEnable(GL_LIGHTING);
  130. glEnable(GL_LIGHT0);
  131. }
  132.  
  133. void display(void)
  134. {
  135. //Render objects
  136. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  137.  
  138. glPushMatrix();
  139.  
  140. glPushMatrix();
  141. myScale(5, 5, 5);
  142. glutSolidSphere(10, 20, 20); // the Sun
  143. glPopMatrix();
  144.  
  145. myRotate(spin[Terra], 0, 1, 0);
  146. myTranslate(dist[Terra] * fdist, 0, 0);
  147. glutSolidSphere(radius[Terra] * fradius, 20, 20); // the Earth
  148.  
  149. myRotate(spin[Moon], 0, 1, 0);
  150. myTranslate(dist[Moon] * fdist, 0, 0);
  151. glutSolidSphere(radius[Moon] * fradius, 20, 20); // the Moon
  152.  
  153. glPopMatrix();
  154. glPushMatrix();
  155. myRotate(spin[Mercury], 0, 1, 0);
  156. myTranslate(dist[Mercury] * fdist, 0, 0);
  157. glutSolidSphere(radius[Mercury] * fradius, 20, 20); // the Mercury
  158. glPopMatrix();
  159. glPushMatrix();
  160. myRotate(spin[Venus], 0, 1, 0);
  161. myTranslate(dist[Venus] * fdist, 0, 0);
  162. glutSolidSphere(radius[Venus] * fradius, 20, 20); // the Venus
  163. glPopMatrix();
  164. glPushMatrix();
  165. myRotate(spin[Mars], 0, 1, 0);
  166. myTranslate(dist[Mars] * fdist, 0, 0);
  167. glutSolidSphere(radius[Mars] * fradius, 20, 20); // the Mars
  168.  
  169. glPushMatrix();
  170. myRotate(spin[Phobos], 0, 1, 0);
  171. myTranslate(dist[Phobos] * fdist, 0, 0);
  172. glutSolidSphere(radius[Phobos] * fradius, 20, 20); // the Phobos
  173. glPopMatrix();
  174. glPushMatrix();
  175. myRotate(spin[Deimos], 0, 1, 0);
  176. myTranslate(dist[Deimos] * fdist, 0, 0);
  177. glutSolidSphere(radius[Deimos] * fradius, 20, 20); // the Deimos
  178. glPopMatrix();
  179. glPopMatrix();
  180.  
  181. glutSwapBuffers();
  182. }
  183.  
  184. void timer(int value)
  185. {
  186. glutTimerFunc(value, timer,value);
  187. for (int i=0; i<7; i++)
  188. {
  189. spin[i] += 1/period[i] * fperiod;
  190. if (spin[i] >= 360) spin[i] -= 360;
  191. }
  192. glutPostRedisplay();
  193. }
  194.  
  195. GLfloat xx=300,yy=300,zz=300;
  196.  
  197. void reshape(int w, int h)
  198. {
  199. glViewport(0,0, (GLsizei) w, (GLsizei) h);
  200.  
  201. aspect = (GLfloat) w / (GLfloat) h;
  202.  
  203. glMatrixMode(GL_PROJECTION);
  204. glLoadIdentity();
  205. myPerspective(45.0f, aspect, 0.1f, 1000.0f);
  206. glMatrixMode(GL_MODELVIEW);
  207. glLoadIdentity();
  208.  
  209. myLookAt(xx,yy,zz, 0,0,0, 0,1,0);
  210.  
  211. width = w;
  212. height = h;
  213. }
  214.  
  215.  
  216. void keyboard(unsigned char key, int x, int y)
  217. {
  218. switch (key)
  219. {
  220. case 27: exit(0);
  221. case 'w': {if(yy>0) yy=-300; reshape (width,height);break;}
  222. case 's': {if(yy<0) yy=300; reshape (width,height);break;}
  223. case 'a': {if(xx>0) xx=-300; reshape (width,height);break;}
  224. case 'd': {if(xx<0) xx=300; reshape (width,height);break;}
  225.  
  226. }
  227. }
  228.  
  229.  
  230. int main(int argc, char **argv)
  231. {
  232. glutInit(&argc, argv);
  233. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  234. glutInitWindowSize(800, 600);
  235. glutInitWindowPosition(100,100);
  236. glutCreateWindow("Transforms");
  237. init();
  238. glutDisplayFunc(display);
  239. glutReshapeFunc(reshape);
  240. glutKeyboardFunc(keyboard);
  241. glutTimerFunc(delay, timer, delay);
  242. glutMainLoop();
  243. return 0;
  244. }
Add Comment
Please, Sign In to add comment