Platformer in OpenGL
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

163 rader
4.7 KiB

5 år sedan
  1. //========================================================================
  2. // Simple GLFW example
  3. // Copyright (c) Camilla Berglund <elmindreda@glfw.org>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. //! [code]
  26. #include <glad/glad.h>
  27. #include <GLFW/glfw3.h>
  28. #include "linmath.h"
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. static const struct
  32. {
  33. float x, y;
  34. float r, g, b;
  35. } vertices[3] =
  36. {
  37. { -0.6f, -0.4f, 1.f, 0.f, 0.f },
  38. { 0.6f, -0.4f, 0.f, 1.f, 0.f },
  39. { 0.f, 0.6f, 0.f, 0.f, 1.f }
  40. };
  41. static const char* vertex_shader_text =
  42. "uniform mat4 MVP;\n"
  43. "attribute vec3 vCol;\n"
  44. "attribute vec2 vPos;\n"
  45. "varying vec3 color;\n"
  46. "void main()\n"
  47. "{\n"
  48. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  49. " color = vCol;\n"
  50. "}\n";
  51. static const char* fragment_shader_text =
  52. "varying vec3 color;\n"
  53. "void main()\n"
  54. "{\n"
  55. " gl_FragColor = vec4(color, 1.0);\n"
  56. "}\n";
  57. static void error_callback(int error, const char* description)
  58. {
  59. fprintf(stderr, "Error: %s\n", description);
  60. }
  61. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  62. {
  63. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  64. glfwSetWindowShouldClose(window, GLFW_TRUE);
  65. }
  66. int main(void)
  67. {
  68. GLFWwindow* window;
  69. GLuint vertex_buffer, vertex_shader, fragment_shader, program;
  70. GLint mvp_location, vpos_location, vcol_location;
  71. glfwSetErrorCallback(error_callback);
  72. if (!glfwInit())
  73. exit(EXIT_FAILURE);
  74. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  75. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  76. window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  77. if (!window)
  78. {
  79. glfwTerminate();
  80. exit(EXIT_FAILURE);
  81. }
  82. glfwSetKeyCallback(window, key_callback);
  83. glfwMakeContextCurrent(window);
  84. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  85. glfwSwapInterval(1);
  86. // NOTE: OpenGL error checks have been omitted for brevity
  87. glGenBuffers(1, &vertex_buffer);
  88. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  89. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  90. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  91. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  92. glCompileShader(vertex_shader);
  93. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  94. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  95. glCompileShader(fragment_shader);
  96. program = glCreateProgram();
  97. glAttachShader(program, vertex_shader);
  98. glAttachShader(program, fragment_shader);
  99. glLinkProgram(program);
  100. mvp_location = glGetUniformLocation(program, "MVP");
  101. vpos_location = glGetAttribLocation(program, "vPos");
  102. vcol_location = glGetAttribLocation(program, "vCol");
  103. glEnableVertexAttribArray(vpos_location);
  104. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  105. sizeof(float) * 5, (void*) 0);
  106. glEnableVertexAttribArray(vcol_location);
  107. glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
  108. sizeof(float) * 5, (void*) (sizeof(float) * 2));
  109. while (!glfwWindowShouldClose(window))
  110. {
  111. float ratio;
  112. int width, height;
  113. mat4x4 m, p, mvp;
  114. glfwGetFramebufferSize(window, &width, &height);
  115. ratio = width / (float) height;
  116. glViewport(0, 0, width, height);
  117. glClear(GL_COLOR_BUFFER_BIT);
  118. mat4x4_identity(m);
  119. mat4x4_rotate_Z(m, m, (float) glfwGetTime());
  120. mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  121. mat4x4_mul(mvp, p, m);
  122. glUseProgram(program);
  123. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  124. glDrawArrays(GL_TRIANGLES, 0, 3);
  125. glfwSwapBuffers(window);
  126. glfwPollEvents();
  127. }
  128. glfwDestroyWindow(window);
  129. glfwTerminate();
  130. exit(EXIT_SUCCESS);
  131. }
  132. //! [code]