Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
9.6 KiB

5 years ago
  1. /*
  2. * 3-D gear wheels. This program is in the public domain.
  3. *
  4. * Command line options:
  5. * -info print GL implementation information
  6. * -exit automatically exit after 30 seconds
  7. *
  8. *
  9. * Brian Paul
  10. *
  11. *
  12. * Marcus Geelnard:
  13. * - Conversion to GLFW
  14. * - Time based rendering (frame rate independent)
  15. * - Slightly modified camera that should work better for stereo viewing
  16. *
  17. *
  18. * Camilla Berglund:
  19. * - Removed FPS counter (this is not a benchmark)
  20. * - Added a few comments
  21. * - Enabled vsync
  22. */
  23. #if defined(_MSC_VER)
  24. // Make MS math.h define M_PI
  25. #define _USE_MATH_DEFINES
  26. #endif
  27. #include <math.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <glad/glad.h>
  32. #include <GLFW/glfw3.h>
  33. /**
  34. Draw a gear wheel. You'll probably want to call this function when
  35. building a display list since we do a lot of trig here.
  36. Input: inner_radius - radius of hole at center
  37. outer_radius - radius at center of teeth
  38. width - width of gear teeth - number of teeth
  39. tooth_depth - depth of tooth
  40. **/
  41. static void
  42. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  43. GLint teeth, GLfloat tooth_depth)
  44. {
  45. GLint i;
  46. GLfloat r0, r1, r2;
  47. GLfloat angle, da;
  48. GLfloat u, v, len;
  49. r0 = inner_radius;
  50. r1 = outer_radius - tooth_depth / 2.f;
  51. r2 = outer_radius + tooth_depth / 2.f;
  52. da = 2.f * (float) M_PI / teeth / 4.f;
  53. glShadeModel(GL_FLAT);
  54. glNormal3f(0.f, 0.f, 1.f);
  55. /* draw front face */
  56. glBegin(GL_QUAD_STRIP);
  57. for (i = 0; i <= teeth; i++) {
  58. angle = i * 2.f * (float) M_PI / teeth;
  59. glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), width * 0.5f);
  60. glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), width * 0.5f);
  61. if (i < teeth) {
  62. glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), width * 0.5f);
  63. glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), width * 0.5f);
  64. }
  65. }
  66. glEnd();
  67. /* draw front sides of teeth */
  68. glBegin(GL_QUADS);
  69. da = 2.f * (float) M_PI / teeth / 4.f;
  70. for (i = 0; i < teeth; i++) {
  71. angle = i * 2.f * (float) M_PI / teeth;
  72. glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), width * 0.5f);
  73. glVertex3f(r2 * (float) cos(angle + da), r2 * (float) sin(angle + da), width * 0.5f);
  74. glVertex3f(r2 * (float) cos(angle + 2 * da), r2 * (float) sin(angle + 2 * da), width * 0.5f);
  75. glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), width * 0.5f);
  76. }
  77. glEnd();
  78. glNormal3f(0.0, 0.0, -1.0);
  79. /* draw back face */
  80. glBegin(GL_QUAD_STRIP);
  81. for (i = 0; i <= teeth; i++) {
  82. angle = i * 2.f * (float) M_PI / teeth;
  83. glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), -width * 0.5f);
  84. glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), -width * 0.5f);
  85. if (i < teeth) {
  86. glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), -width * 0.5f);
  87. glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), -width * 0.5f);
  88. }
  89. }
  90. glEnd();
  91. /* draw back sides of teeth */
  92. glBegin(GL_QUADS);
  93. da = 2.f * (float) M_PI / teeth / 4.f;
  94. for (i = 0; i < teeth; i++) {
  95. angle = i * 2.f * (float) M_PI / teeth;
  96. glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), -width * 0.5f);
  97. glVertex3f(r2 * (float) cos(angle + 2 * da), r2 * (float) sin(angle + 2 * da), -width * 0.5f);
  98. glVertex3f(r2 * (float) cos(angle + da), r2 * (float) sin(angle + da), -width * 0.5f);
  99. glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), -width * 0.5f);
  100. }
  101. glEnd();
  102. /* draw outward faces of teeth */
  103. glBegin(GL_QUAD_STRIP);
  104. for (i = 0; i < teeth; i++) {
  105. angle = i * 2.f * (float) M_PI / teeth;
  106. glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), width * 0.5f);
  107. glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), -width * 0.5f);
  108. u = r2 * (float) cos(angle + da) - r1 * (float) cos(angle);
  109. v = r2 * (float) sin(angle + da) - r1 * (float) sin(angle);
  110. len = (float) sqrt(u * u + v * v);
  111. u /= len;
  112. v /= len;
  113. glNormal3f(v, -u, 0.0);
  114. glVertex3f(r2 * (float) cos(angle + da), r2 * (float) sin(angle + da), width * 0.5f);
  115. glVertex3f(r2 * (float) cos(angle + da), r2 * (float) sin(angle + da), -width * 0.5f);
  116. glNormal3f((float) cos(angle), (float) sin(angle), 0.f);
  117. glVertex3f(r2 * (float) cos(angle + 2 * da), r2 * (float) sin(angle + 2 * da), width * 0.5f);
  118. glVertex3f(r2 * (float) cos(angle + 2 * da), r2 * (float) sin(angle + 2 * da), -width * 0.5f);
  119. u = r1 * (float) cos(angle + 3 * da) - r2 * (float) cos(angle + 2 * da);
  120. v = r1 * (float) sin(angle + 3 * da) - r2 * (float) sin(angle + 2 * da);
  121. glNormal3f(v, -u, 0.f);
  122. glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), width * 0.5f);
  123. glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), -width * 0.5f);
  124. glNormal3f((float) cos(angle), (float) sin(angle), 0.f);
  125. }
  126. glVertex3f(r1 * (float) cos(0), r1 * (float) sin(0), width * 0.5f);
  127. glVertex3f(r1 * (float) cos(0), r1 * (float) sin(0), -width * 0.5f);
  128. glEnd();
  129. glShadeModel(GL_SMOOTH);
  130. /* draw inside radius cylinder */
  131. glBegin(GL_QUAD_STRIP);
  132. for (i = 0; i <= teeth; i++) {
  133. angle = i * 2.f * (float) M_PI / teeth;
  134. glNormal3f(-(float) cos(angle), -(float) sin(angle), 0.f);
  135. glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), -width * 0.5f);
  136. glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), width * 0.5f);
  137. }
  138. glEnd();
  139. }
  140. static GLfloat view_rotx = 20.f, view_roty = 30.f, view_rotz = 0.f;
  141. static GLint gear1, gear2, gear3;
  142. static GLfloat angle = 0.f;
  143. /* OpenGL draw function & timing */
  144. static void draw(void)
  145. {
  146. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  147. glPushMatrix();
  148. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  149. glRotatef(view_roty, 0.0, 1.0, 0.0);
  150. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  151. glPushMatrix();
  152. glTranslatef(-3.0, -2.0, 0.0);
  153. glRotatef(angle, 0.0, 0.0, 1.0);
  154. glCallList(gear1);
  155. glPopMatrix();
  156. glPushMatrix();
  157. glTranslatef(3.1f, -2.f, 0.f);
  158. glRotatef(-2.f * angle - 9.f, 0.f, 0.f, 1.f);
  159. glCallList(gear2);
  160. glPopMatrix();
  161. glPushMatrix();
  162. glTranslatef(-3.1f, 4.2f, 0.f);
  163. glRotatef(-2.f * angle - 25.f, 0.f, 0.f, 1.f);
  164. glCallList(gear3);
  165. glPopMatrix();
  166. glPopMatrix();
  167. }
  168. /* update animation parameters */
  169. static void animate(void)
  170. {
  171. angle = 100.f * (float) glfwGetTime();
  172. }
  173. /* change view angle, exit upon ESC */
  174. void key( GLFWwindow* window, int k, int s, int action, int mods )
  175. {
  176. if( action != GLFW_PRESS ) return;
  177. switch (k) {
  178. case GLFW_KEY_Z:
  179. if( mods & GLFW_MOD_SHIFT )
  180. view_rotz -= 5.0;
  181. else
  182. view_rotz += 5.0;
  183. break;
  184. case GLFW_KEY_ESCAPE:
  185. glfwSetWindowShouldClose(window, GLFW_TRUE);
  186. break;
  187. case GLFW_KEY_UP:
  188. view_rotx += 5.0;
  189. break;
  190. case GLFW_KEY_DOWN:
  191. view_rotx -= 5.0;
  192. break;
  193. case GLFW_KEY_LEFT:
  194. view_roty += 5.0;
  195. break;
  196. case GLFW_KEY_RIGHT:
  197. view_roty -= 5.0;
  198. break;
  199. default:
  200. return;
  201. }
  202. }
  203. /* new window size */
  204. void reshape( GLFWwindow* window, int width, int height )
  205. {
  206. GLfloat h = (GLfloat) height / (GLfloat) width;
  207. GLfloat xmax, znear, zfar;
  208. znear = 5.0f;
  209. zfar = 30.0f;
  210. xmax = znear * 0.5f;
  211. glViewport( 0, 0, (GLint) width, (GLint) height );
  212. glMatrixMode( GL_PROJECTION );
  213. glLoadIdentity();
  214. glFrustum( -xmax, xmax, -xmax*h, xmax*h, znear, zfar );
  215. glMatrixMode( GL_MODELVIEW );
  216. glLoadIdentity();
  217. glTranslatef( 0.0, 0.0, -20.0 );
  218. }
  219. /* program & OpenGL initialization */
  220. static void init(void)
  221. {
  222. static GLfloat pos[4] = {5.f, 5.f, 10.f, 0.f};
  223. static GLfloat red[4] = {0.8f, 0.1f, 0.f, 1.f};
  224. static GLfloat green[4] = {0.f, 0.8f, 0.2f, 1.f};
  225. static GLfloat blue[4] = {0.2f, 0.2f, 1.f, 1.f};
  226. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  227. glEnable(GL_CULL_FACE);
  228. glEnable(GL_LIGHTING);
  229. glEnable(GL_LIGHT0);
  230. glEnable(GL_DEPTH_TEST);
  231. /* make the gears */
  232. gear1 = glGenLists(1);
  233. glNewList(gear1, GL_COMPILE);
  234. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  235. gear(1.f, 4.f, 1.f, 20, 0.7f);
  236. glEndList();
  237. gear2 = glGenLists(1);
  238. glNewList(gear2, GL_COMPILE);
  239. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  240. gear(0.5f, 2.f, 2.f, 10, 0.7f);
  241. glEndList();
  242. gear3 = glGenLists(1);
  243. glNewList(gear3, GL_COMPILE);
  244. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  245. gear(1.3f, 2.f, 0.5f, 10, 0.7f);
  246. glEndList();
  247. glEnable(GL_NORMALIZE);
  248. }
  249. /* program entry */
  250. int main(int argc, char *argv[])
  251. {
  252. GLFWwindow* window;
  253. int width, height;
  254. if( !glfwInit() )
  255. {
  256. fprintf( stderr, "Failed to initialize GLFW\n" );
  257. exit( EXIT_FAILURE );
  258. }
  259. glfwWindowHint(GLFW_DEPTH_BITS, 16);
  260. window = glfwCreateWindow( 300, 300, "Gears", NULL, NULL );
  261. if (!window)
  262. {
  263. fprintf( stderr, "Failed to open GLFW window\n" );
  264. glfwTerminate();
  265. exit( EXIT_FAILURE );
  266. }
  267. // Set callback functions
  268. glfwSetFramebufferSizeCallback(window, reshape);
  269. glfwSetKeyCallback(window, key);
  270. glfwMakeContextCurrent(window);
  271. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  272. glfwSwapInterval( 1 );
  273. glfwGetFramebufferSize(window, &width, &height);
  274. reshape(window, width, height);
  275. // Parse command-line options
  276. init();
  277. // Main loop
  278. while( !glfwWindowShouldClose(window) )
  279. {
  280. // Draw gears
  281. draw();
  282. // Update animation
  283. animate();
  284. // Swap buffers
  285. glfwSwapBuffers(window);
  286. glfwPollEvents();
  287. }
  288. // Terminate GLFW
  289. glfwTerminate();
  290. // Exit program
  291. exit( EXIT_SUCCESS );
  292. }