Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include "GL\glut.h"
  2. #include <math.h>
  3.  
  4. #define GL_PI 3.1415f
  5.  
  6. static GLfloat xRot = 0.0f;
  7. static GLfloat yRot = 0.0f;
  8.  
  9. void RenderScene()
  10. {
  11. GLfloat x, y, z, angle;
  12.  
  13. glClear(GL_COLOR_BUFFER_BIT);
  14. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  15. glColor3f(0.0f, 1.0f, 0.0f);
  16.  
  17. //しばらく後に解説
  18. glPushMatrix();
  19. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  20. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  21.  
  22. glBegin(GL_LINES);
  23. z = 0.0f;
  24. for (angle = 0.0f; angle <= GL_PI; angle += (GL_PI / 20.0f))
  25. {
  26. x = 50.0f * cos(angle);
  27. y = 50.0f * sin(angle);
  28. glVertex3f(x, y, z);
  29.  
  30. //180度移動して点を取得
  31. x = 50.0f * cos(angle+GL_PI);
  32. y = 50.0f * sin(angle+GL_PI);
  33. glVertex3f(x, y, z);
  34. }
  35. glEnd();
  36. glPopMatrix();
  37. glutSwapBuffers();
  38. }
  39. void controlKeys(int key, int x, int y)
  40. {
  41. if (key == GLUT_KEY_UP)
  42. xRot -= 5.0f;
  43.  
  44. if (key == GLUT_KEY_DOWN)
  45. xRot += 5.0f;
  46.  
  47. if (key == GLUT_KEY_LEFT)
  48. yRot -= 5.0f;
  49.  
  50. if (key == GLUT_KEY_RIGHT)
  51. yRot += 5.0f;
  52.  
  53. if (key > 356.0f)
  54. xRot = 0.0f;
  55.  
  56. if (key < -1.0f)
  57. xRot = 355.0f;
  58.  
  59. if (key > 356.0f)
  60. yRot = 0.0f;
  61.  
  62. if (key < -1.0f)
  63. yRot = 355.0f;
  64.  
  65. glutPostRedisplay();
  66. }
  67. void ChangeSize(int w, int h)
  68. {
  69. GLfloat nRange = 100.0f;
  70.  
  71. if (h == 0) h = 1; //0除算を防ぐためのチェッカー
  72.  
  73. glViewport(0, 0, w, h);
  74.  
  75. glMatrixMode(GL_PROJECTION);
  76. glLoadIdentity();
  77.  
  78. if (w <= h) {
  79. GLfloat aspect = h / w;
  80. glOrtho(-nRange, nRange, -nRange * aspect, nRange*aspect, -nRange, nRange);
  81. }
  82. else
  83. {
  84. GLfloat aspect = w / h;
  85. glOrtho(-nRange * aspect, nRange * aspect, -nRange, nRange, -nRange, nRange);
  86. }
  87. glMatrixMode(GL_MODELVIEW);
  88. glLoadIdentity();
  89. }
  90.  
  91. int main(int argc, char* argv[])
  92. {
  93. glutInit(&argc, argv);
  94. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  95. glutInitWindowSize(800, 600);
  96. glutCreateWindow("Complex Line");
  97. glutReshapeFunc(ChangeSize);
  98. glutSpecialFunc(controlKeys);
  99. glutDisplayFunc(RenderScene);
  100.  
  101. glutMainLoop();
  102.  
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement