triangle-opengl.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //========================================================================
  2. // OpenGL triangle example
  3. // Copyright (c) Camilla Löwy <[email protected]>
  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. #define GLAD_GL_IMPLEMENTATION
  27. #include <glad/gl.h>
  28. #define GLFW_INCLUDE_NONE
  29. #include <GLFW/glfw3.h>
  30. #include "linmath.h"
  31. #include <stdlib.h>
  32. #include <stddef.h>
  33. #include <stdio.h>
  34. typedef struct Vertex
  35. {
  36. vec2 pos;
  37. vec3 col;
  38. } Vertex;
  39. static const Vertex vertices[3] =
  40. {
  41. { { -0.6f, -0.4f }, { 1.f, 0.f, 0.f } },
  42. { { 0.6f, -0.4f }, { 0.f, 1.f, 0.f } },
  43. { { 0.f, 0.6f }, { 0.f, 0.f, 1.f } }
  44. };
  45. static const char* vertex_shader_text =
  46. "#version 330\n"
  47. "uniform mat4 MVP;\n"
  48. "in vec3 vCol;\n"
  49. "in vec2 vPos;\n"
  50. "out vec3 color;\n"
  51. "void main()\n"
  52. "{\n"
  53. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  54. " color = vCol;\n"
  55. "}\n";
  56. static const char* fragment_shader_text =
  57. "#version 330\n"
  58. "in vec3 color;\n"
  59. "out vec4 fragment;\n"
  60. "void main()\n"
  61. "{\n"
  62. " fragment = vec4(color, 1.0);\n"
  63. "}\n";
  64. static void error_callback(int error, const char* description)
  65. {
  66. fprintf(stderr, "Error: %s\n", description);
  67. }
  68. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  69. {
  70. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  71. glfwSetWindowShouldClose(window, GLFW_TRUE);
  72. }
  73. int main(void)
  74. {
  75. glfwSetErrorCallback(error_callback);
  76. if (!glfwInit())
  77. exit(EXIT_FAILURE);
  78. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  79. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  80. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  81. GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Triangle", NULL, NULL);
  82. if (!window)
  83. {
  84. glfwTerminate();
  85. exit(EXIT_FAILURE);
  86. }
  87. glfwSetKeyCallback(window, key_callback);
  88. glfwMakeContextCurrent(window);
  89. gladLoadGL(glfwGetProcAddress);
  90. glfwSwapInterval(1);
  91. // NOTE: OpenGL error checks have been omitted for brevity
  92. GLuint vertex_buffer;
  93. glGenBuffers(1, &vertex_buffer);
  94. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  95. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  96. const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  97. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  98. glCompileShader(vertex_shader);
  99. const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  100. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  101. glCompileShader(fragment_shader);
  102. const GLuint program = glCreateProgram();
  103. glAttachShader(program, vertex_shader);
  104. glAttachShader(program, fragment_shader);
  105. glLinkProgram(program);
  106. const GLint mvp_location = glGetUniformLocation(program, "MVP");
  107. const GLint vpos_location = glGetAttribLocation(program, "vPos");
  108. const GLint vcol_location = glGetAttribLocation(program, "vCol");
  109. GLuint vertex_array;
  110. glGenVertexArrays(1, &vertex_array);
  111. glBindVertexArray(vertex_array);
  112. glEnableVertexAttribArray(vpos_location);
  113. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  114. sizeof(Vertex), (void*) offsetof(Vertex, pos));
  115. glEnableVertexAttribArray(vcol_location);
  116. glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
  117. sizeof(Vertex), (void*) offsetof(Vertex, col));
  118. while (!glfwWindowShouldClose(window))
  119. {
  120. int width, height;
  121. glfwGetFramebufferSize(window, &width, &height);
  122. const float ratio = width / (float) height;
  123. glViewport(0, 0, width, height);
  124. glClear(GL_COLOR_BUFFER_BIT);
  125. mat4x4 m, p, mvp;
  126. mat4x4_identity(m);
  127. mat4x4_rotate_Z(m, m, (float) glfwGetTime());
  128. mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  129. mat4x4_mul(mvp, p, m);
  130. glUseProgram(program);
  131. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) &mvp);
  132. glBindVertexArray(vertex_array);
  133. glDrawArrays(GL_TRIANGLES, 0, 3);
  134. glfwSwapBuffers(window);
  135. glfwPollEvents();
  136. }
  137. glfwDestroyWindow(window);
  138. glfwTerminate();
  139. exit(EXIT_SUCCESS);
  140. }
  141. //! [code]