Advertisement
Guest User

Untitled

a guest
Apr 13th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <math.h>
  5. // Lighting values
  6. GLfloat  whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
  7. GLfloat  sourceLight[] = { 1.8f, 0.8f, 0.8f, 1.0f };
  8. GLfloat  lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  9. // Called to draw scene
  10. void RenderScene(void)
  11. {
  12. // Earth and Moon angle of revolution********** start off by adding the two variables to keep track of the rotation for each object.
  13. static float fMoonRot =0.0f; // Angle For The
  14. static float fEarthRot = 0.0f;// Angle For The
  15. // Clear the window with current clearing color
  16. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  17. // Save the matrix state and do the rotations****
  18. glMatrixMode(GL_MODELVIEW);
  19. glPushMatrix();
  20. // Set light position before viewing transformation******
  21. glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  22. // Translate the whole scene out and into view  *** angle viewr for me
  23. glTranslatef(0.0f, 0.0f, -300.0f); 
  24. // Set material color, Red
  25. // Sun
  26. glColor3ub(255, 255, 0);
  27. glutSolidSphere(55.0f, 190, 190);//(size,redioes,reaioes)
  28. glPointSize(1);
  29. glBegin(GL_POINTS);
  30. glColor3f(1,1,1);
  31. glVertex3f(50,40,10);
  32. glVertex3f(67,80,8);
  33. glVertex3f(60,60,170);
  34. glVertex3f(80,40,70);
  35. glVertex3f(99,80,8);
  36. glVertex3f(77,60,154);
  37. glVertex3f(88,90,10);
  38. glVertex3f(100,70,16);
  39. glVertex3f(80,60,70);
  40. glVertex3f(123,50,128);
  41. glVertex3f(77,60,114);
  42. glVertex3f(100,40,10);
  43. glVertex3f(120,80,8);
  44. glVertex3f(119,90,10);
  45. glVertex3f(113,80,8);
  46. glVertex3f(87,60,170);
  47. glVertex3f(100,40,70);
  48. glVertex3f(129,80,8);
  49. glVertex3f(77,60,124);
  50. glVertex3f(100,90,90);
  51. glVertex3f(120,90,100);
  52. glVertex3f(130,70,90);
  53. glVertex3f(111,66,110);
  54. glVertex3f(141,55,84);
  55. glVertex3f(120,90,120);
  56. glVertex3f(133,124,55);
  57. glVertex3f(130,49,136);
  58. glVertex3f(133,96,82);
  59. glVertex3f(140,40,90);
  60. glVertex3f(150,50,88);
  61. glVertex3f(211,67,84);
  62. glVertex3f(200,80,8);
  63. glVertex3f(100,60,170);
  64. glVertex3f(60,50,130);
  65. glVertex3f(150,90,128);
  66. glVertex3f(177,160,154);
  67. glVertex3f(-50,-40,10);
  68. glVertex3f(-67,-80,8);
  69. glVertex3f(-60,-60,170);
  70. glVertex3f(-80,-40,70);
  71. glVertex3f(-99,-80,8);
  72. glVertex3f(-77,-60,154);
  73. glVertex3f(-88,-90,10);
  74. glVertex3f(-100,-99,3);
  75. glVertex3f(-100,-70,16);
  76. glVertex3f(-80,-60,70);
  77. glVertex3f(-123,-50,128);
  78. glVertex3f(-77,-60,114);
  79. glVertex3f(-100,-40,10);
  80. glVertex3f(-50,-90,10);
  81. glVertex3f(-119,-90,10);
  82. glVertex3f(-113,-80,8);
  83. glVertex3f(-87,-60,170);
  84. glVertex3f(-100,-40,70);
  85. glVertex3f(-129,-80,8);
  86. glVertex3f(-77,-60,124);
  87. glVertex3f(-100,-90,90);
  88. glVertex3f(-120,-90,100);
  89. glVertex3f(-130,-70,90);
  90. glVertex3f(-111,-66,110);
  91. glVertex3f(-141,-55,84);
  92. glVertex3f(-120,-90,120);
  93. glVertex3f(-130,-49,136);
  94. glVertex3f(-133,-96,82);
  95. glVertex3f(-140,-40,90);
  96. glVertex3f(-150,-50,88);
  97. glVertex3f(-211,-67,84);
  98. glVertex3f(-200,-80,8);
  99. glVertex3f(-100,-60,110);
  100. glVertex3f(-60,-50,130);
  101. glVertex3f(-150,-90,128);
  102. glVertex3f(-127,-100,114);
  103. glVertex3f(-150,140,-50);
  104. glVertex3f(-67,180,-80);
  105. glVertex3f(-60,160,-170);
  106. glVertex3f(-180,140,-70);
  107. glVertex3f(-130,188,-70);
  108. glVertex3f(-99,180,-60);
  109. glVertex3f(-77,60,-154);
  110. glVertex3f(-288,90,-66);
  111. glVertex3f(-100,99,-53);
  112. glVertex3f(-100,199,-90);
  113. glVertex3f(-123,155,-128);
  114. glVertex3f(-77,60,-114);
  115. glVertex3f(-109,174,-100);
  116. glVertex3f(-50,90,-63);
  117. glVertex3f(-120,123,-87);
  118. glVertex3f(-119,90,-10);
  119. glVertex3f(-87,210,-170);
  120. glVertex3f(-100,40,-70);
  121. glVertex3f(-129,180,-40);
  122. glVertex3f(-77,60,-124);
  123. glVertex3f(-215,190,-90);
  124. glVertex3f(-120,90,-100);
  125. glVertex3f(-130,70,-90);
  126. glVertex3f(-111,166,-110);
  127. glVertex3f(-141,55,-84);
  128. glVertex3f(-120,90,-120);
  129. glVertex3f(-133,124,-45);
  130. glVertex3f(-130,49,-136);
  131. glVertex3f(-133,96,-82);
  132. glVertex3f(-140,184,-90);
  133. glVertex3f(-150,50,-88);
  134. glVertex3f(-211,152,-84);
  135. glVertex3f(-200,142,-55);
  136. glVertex3f(-100,143,-110);
  137. glVertex3f(-60,165,-130);
  138. glVertex3f(-150,90,-128);
  139. glVertex3f(-127,100,-114);
  140. glVertex3f(-250,50,-158);
  141. glVertex3f(-217,196,-84);
  142. glVertex3f(-200,142,-55);
  143. glVertex3f(-188,60,-169);
  144. glVertex3f(-146,165,-130);
  145. glVertex3f(-124,90,-128);
  146. glVertex3f(-156,100,-178);
  147. glVertex3f(-150,50,-88);
  148. glVertex3f(-211,67,-84);
  149. glVertex3f(-200,142,-0);
  150. glVertex3f(-100,60,-0);
  151. glVertex3f(-60,165,-130);
  152. glVertex3f(-150,90,-128);
  153. glVertex3f(-127,100,-114);
  154. glVertex3f(150,-40,-50);
  155. glVertex3f(67,-80,-80);
  156. glVertex3f(60,-60,-170);
  157. glVertex3f(99,-80,-60);
  158. glVertex3f(77,-160,-154);
  159. glVertex3f(288,-90,-66);
  160. glVertex3f(100,-99,-3);
  161. glVertex3f(100,-170,-16);
  162. glVertex3f(130,-60,-70);
  163. glVertex3f(123,50,-128);
  164. glVertex3f(77,-60,-114);
  165. glVertex3f(109,-40,-100);
  166. glVertex3f(150,-90,-63);
  167. glVertex3f(120,-80,-87);
  168. glVertex3f(119,-90,-10);
  169. glVertex3f(113,-80,-99);
  170. glVertex3f(87,-60,-170);
  171. glVertex3f(100,-40,-70);
  172. glVertex3f(129,-80,-40);
  173. glVertex3f(77,-60,-124);
  174. glVertex3f(215,-90,-90);
  175. glVertex3f(120,-90,-100);
  176. glVertex3f(130,-70,-90);
  177. glVertex3f(141,-55,-84);
  178. glVertex3f(120,-90,-120);
  179. glVertex3f(208,-124,-45);
  180. glVertex3f(130,-149,-136);
  181. glVertex3f(140,-140,-90);
  182. glVertex3f(150,-99,-88);
  183. glVertex3f(211,-67,-84);
  184. glVertex3f(250,-121,-110);
  185. glVertex3f(222,-50,-130);
  186. glVertex3f(150,-90,-128);
  187. glVertex3f(244,-300,-114);
  188. glVertex3f(0,-90,-128);
  189. glVertex3f(0,66,114);
  190. glVertex3f(0,88,132);
  191. glVertex3f(10,80,114);
  192. glVertex3f(20,66,20);
  193. glVertex3f(50,90,114);
  194. glVertex3f(0,-90,114);
  195. glVertex3f(10,-90,123);
  196. glVertex3f(19,-90,12);
  197. glVertex3f(5,-88,115);
  198. glVertex3f(0,-70,131);
  199. glVertex3f(40,77,120);
  200. glVertex3f(61,66,55);
  201. glVertex3f(40,90,114);
  202. glVertex3f(0,-90,122);
  203. glVertex3f(44,-90,111);
  204. glVertex3f(-19,-90,12);
  205. glVertex3f(15,-88,115);
  206. glVertex3f(0,70,122);
  207. glVertex3f(-40,-77,-120);
  208. glVertex3f(-61,-66,-55);
  209. glVertex3f(-40,-90,-114);
  210. glVertex3f(0,-99,-122);
  211. glVertex3f(-55,-90,-111);
  212. glVertex3f(-80,-100,-12);
  213. glVertex3f(230,0,15);
  214. glVertex3f(250,10,20);
  215. glVertex3f(270,10,20);
  216. glVertex3f(-230,0,-15);
  217. glVertex3f(-250,10,-20);
  218. glVertex3f(230,0,-15);
  219. glVertex3f(-250,10,20);
  220. glColor3f(1,0,0);
  221. glVertex3f(100,99,3);
  222. glVertex3f(50,90,10);
  223. glVertex3f(260,14,25);
  224. glVertex3f(200,-80,-122);
  225. glVertex3f(180,-40,-70);
  226. glVertex3f(-113,280,-99);
  227. glVertex3f(-120,-80,8);
  228. glVertex3f(-133,-124,55);
  229. glVertex3f(111,-122,-110);
  230. glVertex3f(222,-96,-99);
  231. glEnd();
  232. glFlush();
  233. //(eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX,upY,upZ)
  234. gluLookAt(1, 0.25,0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  235. // Move the light after we draw the sun!
  236. glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  237. //new
  238. //  glRotatef(fEarthRot, 0.0f, 3.0f, 0.0f);
  239. // Rotate coordinate system*****(x,y,z)
  240. // Draw the Mercury
  241. glRotatef(fEarthRot, 0.0f, 3.0f, 0.0f);
  242. glColor3ub(114, 155, 145);
  243. glTranslatef(70.0f,0.0f,0.0f);//****position
  244. glutSolidSphere(10.0f, 190, 190);//***redios
  245. glColor3ub(114,0,0);
  246. glTranslatef(60.0f,0.0f,0.0f);//****position
  247. glutSolidSphere(20.0f, 190, 190);
  248. // Draw the Earth
  249. glColor3ub(0, 0, 145);
  250. glTranslatef(77.0f,0.0f,0.0f);//****position
  251. glutSolidSphere(25.0f, 190, 190);//***redios
  252. // Rotate from Earth based coordinates and draw Moon
  253. glColor3ub(255,255,255);
  254. glRotatef(fMoonRot,0.0f, 1.0f, 0.0f);
  255. glTranslatef(30.0f, 0.0f, 0.0f);
  256. /*glTranslatef(x, y, z) moves along the X, Y and Z axis, in that order.
  257. The line of code below moves left on the X axis 1.5 units.
  258. It does not move on the Y axis at all (0.0),
  259. and it moves into the screen 6.0 units.
  260. When you translate, you are not moving a set amount from the center of the screen,
  261. you are moving a set amount from wherever you currently were on the screen*/
  262. fMoonRot+= 8.0f;//rotate the moon
  263. if(fMoonRot > 360.0f)
  264. fMoonRot = 0.0f;
  265. glutSolidSphere(5.0f, 180, 180);
  266. // Restore the matrix state
  267. glPopMatrix();  // Modelview matrix
  268. // Step earth orbit 5 degrees
  269. fEarthRot += 2.0f;
  270. if(fEarthRot > 360.0f)
  271. fEarthRot = 0.0f;
  272. // Show the image**** it swaps the front buffer and back buffer
  273. glutSwapBuffers();
  274. }
  275. // This function does any needed initialization on the rendering
  276. // context.
  277. void SetupRC()
  278. {
  279. // Light values and coordinates
  280. glEnable(GL_DEPTH_TEST);
  281. // Hidden surface removal**/* */
  282. //  glFrontFace(GL_CCW); // Counter clock-wise polygons face out******* no need
  283. // glEnable(GL_CULL_FACE);
  284. // Do not calculate inside of jet ******* no need
  285. // Enable lighting**
  286. glEnable(GL_LIGHTING);
  287. // Setup and enable light 0
  288. //glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
  289. glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
  290. //glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  291. glEnable(GL_LIGHT0);//transparent--When it is enabled, light sources that are enabled contribute to the lighting calculation.-
  292. // Enable color tracking***************************************
  293. glEnable(GL_COLOR_MATERIAL);// specifies which material parameters track the current color All the time
  294. // Set Material properties to follow glColor values
  295. //   glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  296. // Black blue background
  297. // glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
  298. }
  299. //motion*********************************
  300. void TimerFunc(int value)
  301. {
  302. glutPostRedisplay();// Post repaint request to activate display
  303. glutTimerFunc(100, TimerFunc, 1);// // where millis is the delay in milliseconds, value will be passed to the timer function.
  304. }
  305. void ChangeSize(int w, int h)
  306. {
  307. GLfloat fAspect;
  308. // Prevent a divide by zero
  309. if(h == 0)
  310. h = 1;
  311. glViewport(0, 0, w, h); // Set Viewport to window dimensions
  312. /*Viewport: Viewport refers to the display area on the window (screen),
  313. which is measured in pixels in screen coordinates
  314. (excluding the title bar).
  315. */
  316. // Calculate aspect ratio of the window
  317. fAspect = (GLfloat)w/(GLfloat)h;
  318. // Set the perspective coordinate system
  319. glMatrixMode(GL_PROJECTION);
  320. glLoadIdentity();/*When you do a glLoadIdentity() what you are doing is moving back to the
  321. center of the screen with the X axis running left to right,
  322. the Y axis moving up and down, and the Z axis moving into,
  323. and out of the screen.
  324. The center of an OpenGL screen is 0.0f on the X and Y axis. To the left of center would be a negative number.
  325. To tand 425he right would be a positive number.
  326. Moving towards the top of the screen would be a positive number, moving to the bottom of the screen would be a negative number.
  327. Moving deeper into the screen is a negative number, moving towards the viewer would be a positive number.*/
  328. // field of view of 45 degrees, near and far planes 1.0
  329. gluPerspective(50.0f, fAspect, 1.0, 700.0);//determines the global coordinate system display.
  330. // Modelview matrix reset
  331. glMatrixMode(GL_MODELVIEW);
  332. glLoadIdentity();
  333. }
  334. int main(int argc, char* argv[])
  335. {
  336. glutInit(&argc, argv);
  337. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  338. glutCreateWindow("Solar System");
  339. glutInitWindowSize(500, 500);
  340. glutReshapeFunc(ChangeSize);
  341. glutDisplayFunc(RenderScene);
  342. glutTimerFunc(500, TimerFunc, 1);
  343. SetupRC();
  344. glutMainLoop();
  345. return 0;
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement