Advertisement
Guest User

solar system

a guest
Oct 31st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. // OpenGLApplication.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "glut.h"
  6. #include <gl/gl.h>
  7.  
  8. int screen_width=640;
  9. int screen_height=480;
  10. float z = 280.0, d = 10.0;
  11. int r = 1;
  12. int count = 0;
  13. void initOpenGL()
  14. {
  15. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  16. glShadeModel(GL_SMOOTH);
  17. glViewport(0, 0, screen_width, screen_height);
  18. glMatrixMode(GL_PROJECTION);
  19. glLoadIdentity();
  20. gluPerspective(45.0f, (GLfloat)screen_width/(GLfloat)screen_height, 1.0f, 1000.0f);
  21. glEnable(GL_DEPTH_TEST);
  22. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  23. glMatrixMode(GL_MODELVIEW);
  24. }
  25.  
  26. void renderScene(void)
  27. {
  28. count++;
  29. if(count % 30 == 0)
  30. r++;
  31.  
  32. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  33. glLoadIdentity();
  34. gluLookAt(0.0, 0.0, z, 0.0, 0.0, -10.0, 0.0, 1.0, 0.0);
  35. glColor4f(1,0.8,0.2,0.9);
  36.  
  37. glPushMatrix();
  38. glRotatef(r/5,0,1,0);
  39. glutSolidSphere(30,16,16);
  40. glPopMatrix();
  41.  
  42.  
  43. glRotatef(r,0,1,0);
  44.  
  45.  
  46. glTranslatef(70,0,0);
  47. glColor4f(0,0.3,1,0.9);
  48. glPushMatrix();
  49. glRotatef(r,0,1,0);
  50. glutSolidSphere(15,16,16);
  51. glPopMatrix();
  52.  
  53. glPushMatrix();
  54.  
  55. glRotatef(r,0,1,0);
  56. glRotatef(r*5,0,1,0);
  57. glTranslatef(20,0,0);
  58. glColor4f(0.3,0,0.3,0.9);
  59. glutSolidSphere(8,16,16);
  60.  
  61. glPopMatrix();
  62.  
  63. glutSwapBuffers();
  64. }
  65.  
  66. void changeSize(int w, int h)
  67. {
  68. screen_width=w;
  69. screen_height=h;
  70.  
  71. if(h == 0)
  72. h = 1;
  73.  
  74. float ratio = 1.0*w/h;
  75.  
  76. glMatrixMode(GL_PROJECTION);
  77. glLoadIdentity();
  78. glViewport(0, 0, w, h);
  79. gluPerspective(45.0f, ratio, 1.0f, 1000.0f);
  80. glMatrixMode(GL_MODELVIEW);
  81. glLoadIdentity();
  82. gluLookAt(0.0f, 0.0f, 50.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
  83. }
  84.  
  85. void processNormalKeys(unsigned char key, int x, int y)
  86. {
  87.  
  88. switch(key)
  89. {
  90. case 't':
  91. //process
  92. glutPostRedisplay();
  93. break;
  94. case 's':
  95. z=z*2;
  96. break;
  97. case 'w':
  98. z = (int)z/2;
  99. break;
  100. case 'r':
  101. r++;
  102. break;
  103. case 'y':
  104. r--;
  105. break;
  106. case 'd':
  107. d=d++;
  108. break;
  109. case 'a':
  110. d=(int)d--;
  111. break;
  112.  
  113. }
  114.  
  115.  
  116. }
  117.  
  118. int main(int argc, char* argv[])
  119. {
  120. //Initialize the GLUT library
  121. glutInit(&argc, argv);
  122. //Set the display mode
  123. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  124. //Set the initial position and dimensions of the window
  125. glutInitWindowPosition(100, 100);
  126. glutInitWindowSize(screen_width, screen_height);
  127. //creates the window
  128. glutCreateWindow("First OpenGL Application");
  129. //Specifies the function to call when the window needs to be redisplayed
  130. glutDisplayFunc(renderScene);
  131. //Sets the idle callback function
  132. glutIdleFunc(renderScene);
  133. //Sets the reshape callback function
  134. glutReshapeFunc(changeSize);
  135. //Keyboard callback function
  136. glutKeyboardFunc(processNormalKeys);
  137. //Initialize some OpenGL parameters
  138. initOpenGL();
  139. //Starts the GLUT infinite loop
  140. glutMainLoop();
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement