Advertisement
Guest User

pab

a guest
Jun 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #include <GL/glew.h>
  6. #include <GLFW/glfw3.h>
  7.  
  8. GLFWwindow * window;
  9. const GLuint default_window_width = 800;
  10. const GLuint default_window_height = 600;
  11. const GLchar * window_title = "An OpenGL + GLFW3 Experiment";
  12.  
  13. void open(void);
  14. void update(void);
  15. void render(void);
  16. void close(void);
  17. void keyboard(GLFWwindow* window, int key, int scancode, int action, int mode);
  18.  
  19. GLuint shaderProgram, vertexShaderObject, fragmentShaderObject;
  20.  
  21. GLuint vertexBufferObject, vertexArrayObject, indexBufferObject;
  22.  
  23. int main(void)
  24. {
  25. open();
  26. while (! glfwWindowShouldClose(window))
  27. {
  28. update();
  29. render();
  30. }
  31. close();
  32. exit(EXIT_SUCCESS);
  33. }
  34.  
  35. void open(void)
  36. {
  37. if (! glfwInit())
  38. {
  39. printf("Glfw init failure\n");
  40. exit(EXIT_FAILURE);
  41. }
  42.  
  43. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  44. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  45. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  46. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  47. glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  48. glfwWindowHint(GLFW_DECORATED, GL_TRUE);
  49.  
  50. window = glfwCreateWindow
  51. (
  52. default_window_width,
  53. default_window_height,
  54. "DEFUALT_WINDOW_NAME",
  55. NULL,
  56. NULL
  57. );
  58.  
  59. glfwSetWindowTitle(window, window_title);
  60.  
  61. if (NULL == window)
  62. {
  63. printf("Window creation failure\n");
  64. glfwTerminate();
  65. exit(EXIT_FAILURE);
  66. }
  67.  
  68. glfwMakeContextCurrent(window);
  69.  
  70. glewExperimental = GL_TRUE;
  71.  
  72. if (glewInit() != GLEW_OK)
  73. {
  74. printf("Glew init failure\n");
  75. exit(EXIT_FAILURE);
  76. }
  77.  
  78. glfwSetKeyCallback(window, keyboard);
  79.  
  80. glfwSwapInterval(1); // V-Sync 0=off, 1=60fps, 2=30fps?
  81.  
  82. // Set OpenGL viewport
  83. GLint width, height;
  84. glfwGetFramebufferSize(window, &width, &height);
  85. glViewport(0, 0, width, height);
  86.  
  87. // Vertex shader object
  88. const GLchar * vertexShaderObjectSource = "#version 330 core\n"
  89. "layout (location = 0) in vec3 position;\n"
  90. "void main()\n"
  91. "{\n"
  92. "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
  93. "}\0";
  94.  
  95. vertexShaderObject = glCreateShader(GL_VERTEX_SHADER);
  96. glShaderSource(vertexShaderObject, 1, &vertexShaderObjectSource, NULL);
  97. glCompileShader(vertexShaderObject);
  98.  
  99. // Fragment shader object
  100. const GLchar* fragmentShaderObjectSource = "#version 330 core\n"
  101. "out vec4 color;\n"
  102. "void main()\n"
  103. "{\n"
  104. "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
  105. "}\n\0";
  106.  
  107. fragmentShaderObject = glCreateShader(GL_FRAGMENT_SHADER);
  108. glShaderSource(fragmentShaderObject, 1, &fragmentShaderObjectSource, NULL);
  109. glCompileShader(fragmentShaderObject);
  110.  
  111. // Link the shaders
  112. shaderProgram = glCreateProgram();
  113. glAttachShader(shaderProgram, vertexShaderObject);
  114. glAttachShader(shaderProgram, fragmentShaderObject);
  115. glLinkProgram(shaderProgram);
  116.  
  117. glDeleteShader(vertexShaderObject);
  118. glDeleteShader(fragmentShaderObject);
  119.  
  120. GLfloat vertices[] = {
  121. 0.5f, 0.5f, 0.0f, // Top Right
  122. 0.5f, -0.5f, 0.0f, // Bottom Right
  123. -0.5f, -0.5f, 0.0f, // Bottom Left
  124. -0.5f, 0.5f, 0.0f // Top Left
  125. };
  126.  
  127. GLuint indices[] = {
  128. // Start from 0!
  129. 0, 1, 3, // First triangle
  130. 1, 2, 3 // Second triangle
  131. };
  132.  
  133. // Generate a unique ID for the vertext array object variable "vertexArrayObject"
  134. glGenVertexArrays(1, &vertexArrayObject);
  135.  
  136. glGenBuffers(1, &vertexBufferObject);
  137. glGenBuffers(1, &indexBufferObject);
  138.  
  139. glBindVertexArray(vertexArrayObject);
  140.  
  141. glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
  142. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  143.  
  144. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
  145. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  146.  
  147. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
  148. glEnableVertexAttribArray(0);
  149.  
  150. glBindBuffer(GL_ARRAY_BUFFER, 0);
  151.  
  152. glBindVertexArray(0);
  153. }
  154.  
  155. void update(void)
  156. {
  157. glfwPollEvents();
  158. fflush(stdout);
  159. }
  160.  
  161. void render(void)
  162. {
  163. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  164. glClear(GL_COLOR_BUFFER_BIT);
  165.  
  166. glUseProgram(shaderProgram);
  167. glBindVertexArray(vertexArrayObject);
  168.  
  169. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  170. glBindVertexArray(0);
  171.  
  172. glfwSwapBuffers(window);
  173. }
  174.  
  175. void close(void)
  176. {
  177. glfwTerminate();
  178. }
  179.  
  180. void keyboard(GLFWwindow* window, int key, int scancode, int action, int mode)
  181. {
  182. if (key == GLFW_KEY_UNKNOWN)
  183. {
  184. printf("Key unknown\n");
  185. }
  186. else if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  187. {
  188. glfwSetWindowShouldClose(window, GL_TRUE);
  189. }
  190. else if (key == GLFW_KEY_W || key == GLFW_KEY_UP)
  191. {
  192. if (action == GLFW_PRESS)
  193. printf("↑/W press\n");
  194. else if (action == GLFW_RELEASE)
  195. printf("↑/W release\n");
  196. }
  197. else if (key == GLFW_KEY_A || key == GLFW_KEY_LEFT)
  198. {
  199. if (action == GLFW_PRESS)
  200. printf("←/A press\n");
  201. else if (action == GLFW_RELEASE)
  202. printf("←/A release\n");
  203. }
  204. else if (key == GLFW_KEY_S || key == GLFW_KEY_DOWN)
  205. {
  206. if (action == GLFW_PRESS)
  207. printf("↓/S press\n");
  208. else if (action == GLFW_RELEASE)
  209. printf("↓/S release\n");
  210. }
  211. else if (key == GLFW_KEY_D || key == GLFW_KEY_RIGHT)
  212. {
  213. if (action == GLFW_PRESS)
  214. printf("→/D press\n");
  215. else if (action == GLFW_RELEASE)
  216. printf("→/D release\n");
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement