Advertisement
qberik

Untitled

Mar 31st, 2023
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <GL/gl.h>
  2. #include <GL/glut.h>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. bool timeChange = true;
  9. bool showKeyCode = false;
  10. bool keypressed[10] = {};
  11.  
  12. GLfloat PI2 = 6.283185307179586;
  13.  
  14. GLfloat param = 0;
  15.  
  16. GLfloat ToroidR = 30;
  17. GLfloat Toroidr = 10;
  18.  
  19. GLfloat Xrotation = 0.0;
  20. GLfloat Yrotation = 0.0;
  21. GLfloat Zrotation = 0.0;
  22. GLfloat RotationSpeed = 2.5;
  23.  
  24. GLint CoordinatResolutionX = 100;
  25. GLint CoordinatResolutionY = 100;
  26. GLint CoordinatResolutionZ = 100;
  27.  
  28.  
  29. void display() {
  30.  
  31. glClearColor(0.1, 0.1, 0.1, 1);
  32.  
  33. glClear(GL_COLOR_BUFFER_BIT);
  34.  
  35. glLoadIdentity();
  36.  
  37. glPointSize( 5 );
  38.  
  39.  
  40.  
  41. glRotatef(Xrotation, 1.0f, 0.0f, 0.0f);
  42. glRotatef(Yrotation, 0.0f, 1.0f, 0.0f);
  43. glRotatef(Zrotation, 0.0f, 0.0f, 1.0f);
  44.  
  45.  
  46.  
  47.  
  48. for( int x = -50; x < 100; x+=1 )
  49. for( int y = -50; y < 100; y+=1 )
  50. for( int z = -50; z < 100; z+=1 ){
  51. float tor = ( x * x + y * y + z * z + ToroidR * ToroidR - Toroidr * Toroidr) * ( x * x + y * y + z * z + ToroidR * ToroidR - Toroidr * Toroidr) - 4 * ToroidR * ToroidR * ( x * x + y * y );
  52.  
  53. if ( tor >= -20000 && tor <= 20000 ){
  54. GLfloat r = ( x + 50 ) / 100.0;
  55. GLfloat g = ( y + 50 ) / 100.0;
  56. GLfloat b = ( z + 50 ) / 100.0;
  57. //GLfloat size = ( ( ( x + y + 100 ) / 200.0 ) ) * 1 * 10;
  58. GLfloat size = ( sin( ( x + y + z ) / 100.0 * 6 + param ) / 3.0 + 0.5 ) * 10 ;
  59.  
  60.  
  61. glColor3f( r, g, b );
  62.  
  63. glPointSize(size);
  64.  
  65. glBegin(GL_POINTS);
  66. glVertex3f(x,y,z);
  67. glEnd();
  68. }
  69. }
  70.  
  71.  
  72.  
  73. /*
  74. for( int i = 0; i < point_count; i++ ){
  75. GLfloat r = i / (float)(point_count);
  76. GLfloat g = cos( i / (float)(point_count) * PI2 ) ;
  77. GLfloat b = 0.3 + ( ( sin( param ) + 1 ) / 5.0 );
  78.  
  79. glColor3f( r, g, b );
  80.  
  81. GLfloat x = ( ( i / (float)(point_count) * CoordinatResolutionX ) ) - ( CoordinatResolutionX / 2.0 );
  82. GLfloat y = sin( param + i * 0.3 ) * 10;
  83. glVertex3f( x, y, 0 );
  84.  
  85. }
  86. */
  87.  
  88.  
  89.  
  90. glColor3f( 1,1,1 );
  91. glBegin(GL_LINES);
  92. glVertex3f( -50, 0, 0 );
  93. glVertex3f( 50, 0, 0 );
  94.  
  95. glVertex3f( 45, -5, 0 );
  96. glVertex3f( 50, 0, 0 );
  97. glVertex3f( 45, 5, 0 );
  98. glVertex3f( 50, 0, 0 );
  99.  
  100.  
  101. glVertex3f( 0, -50, 0 );
  102. glVertex3f( 0, 50, 0 );
  103.  
  104. glVertex3f( -5, 45, 0 );
  105. glVertex3f( 0, 50, 0 );
  106. glVertex3f( 5, 45, 0 );
  107. glVertex3f( 0, 50, 0 );
  108.  
  109. glEnd();
  110.  
  111.  
  112. glutSwapBuffers();
  113. }
  114.  
  115. void ChangeSize(GLsizei w, GLsizei h){
  116.  
  117. GLfloat aspectRatio;
  118. if (h == 0)
  119. h = 1;
  120. glViewport(0, 0, w, h);
  121. glMatrixMode(GL_PROJECTION);
  122. glLoadIdentity();
  123. aspectRatio = (GLfloat)w / (GLfloat)h;
  124. if (w <= h) {
  125. GLfloat windowWidth = (CoordinatResolutionX / 2.0);
  126. GLfloat windowHeight = ( CoordinatResolutionY / 2.0 ) / aspectRatio;
  127. glOrtho(-CoordinatResolutionX/2.0, CoordinatResolutionX/2.0, -windowHeight, windowHeight, -CoordinatResolutionZ/2.0, CoordinatResolutionZ/2.0);
  128. } else {
  129. GLfloat windowWidth = ( CoordinatResolutionX / 2.0 ) * aspectRatio;
  130. GLfloat windowHeight =( CoordinatResolutionY / 2.0 );
  131. glOrtho(-windowWidth, windowWidth, -CoordinatResolutionY/2.0, -CoordinatResolutionY/2.0, -CoordinatResolutionZ/2.0, CoordinatResolutionZ/2.0);
  132. }
  133. glMatrixMode(GL_MODELVIEW);
  134. glLoadIdentity();
  135. }
  136.  
  137.  
  138.  
  139. void inc( int x ){
  140. if ( timeChange )
  141. param += 0.02 * x;
  142.  
  143. if ( keypressed[1] )
  144. Xrotation += RotationSpeed;
  145. if ( keypressed[2] )
  146. Xrotation -= RotationSpeed;
  147. if ( keypressed[3] )
  148. Yrotation += RotationSpeed;
  149. if ( keypressed[4] )
  150. Yrotation -= RotationSpeed;
  151. if ( keypressed[5] )
  152. Zrotation += RotationSpeed;
  153. if ( keypressed[6] )
  154. Zrotation -= RotationSpeed;
  155.  
  156.  
  157. glutPostRedisplay();
  158. glutTimerFunc(16, inc , 3);
  159. }
  160.  
  161.  
  162.  
  163. void keyDown ( int key, int x, int y)
  164. {
  165. if( showKeyCode )
  166. std::cout << "keydown " << key << "\n";
  167.  
  168. if( key == GLUT_KEY_END && !keypressed[0] ){
  169. timeChange = !timeChange;
  170. keypressed[0] = true;
  171. }
  172.  
  173. if( key == GLUT_KEY_UP && !keypressed[1] ){
  174. keypressed[1] = true;
  175. }
  176. if( key == GLUT_KEY_DOWN && !keypressed[2] ){
  177. keypressed[2] = true;
  178. }
  179. if( key == GLUT_KEY_LEFT && !keypressed[3] ){
  180. keypressed[3] = true;
  181. }
  182. if( key == GLUT_KEY_RIGHT && !keypressed[4] ){
  183. keypressed[4] = true;
  184. }
  185. if( key == GLUT_KEY_PAGE_UP && !keypressed[5] ){
  186. keypressed[5] = true;
  187. }
  188. if( key == GLUT_KEY_PAGE_DOWN && !keypressed[6] ){
  189. keypressed[6] = true;
  190. }
  191. }
  192.  
  193. void keyUp ( int key, int x, int y)
  194. {
  195. if( showKeyCode )
  196. std::cout << "keyup " << key << "\n";
  197.  
  198. if( key == GLUT_KEY_END ){
  199. keypressed[0] = false;
  200. }
  201.  
  202. if( key == GLUT_KEY_UP ){
  203. keypressed[1] = false;
  204. }
  205. if( key == GLUT_KEY_DOWN ){
  206. keypressed[2] = false;
  207. }
  208. if( key == GLUT_KEY_LEFT ){
  209. keypressed[3] = false;
  210. }
  211. if( key == GLUT_KEY_RIGHT ){
  212. keypressed[4] = false;
  213. }
  214. if( key == GLUT_KEY_PAGE_UP ){
  215. keypressed[5] = false;
  216. }
  217. if( key == GLUT_KEY_PAGE_DOWN ){
  218. keypressed[6] = false;
  219. }
  220. }
  221.  
  222. int main(int argc, char** argv) {
  223.  
  224. glutInit(&argc, argv);
  225. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  226.  
  227. glutInitWindowPosition(1228,500);
  228.  
  229. glutInitWindowSize(1000, 1000);
  230. glutCreateWindow("float");
  231.  
  232. glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
  233. glutSpecialFunc(keyDown);
  234. glutSpecialUpFunc(keyUp);
  235.  
  236.  
  237. glutDisplayFunc(display);
  238. glutReshapeFunc(ChangeSize);
  239. glutTimerFunc(16, inc , 3);
  240.  
  241. glutMainLoop();
  242.  
  243. return 0;
  244. }
  245.  
  246.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement