simple.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //========================================================================
  2. // Simple GLFW 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. #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. "#version 110\n"
  43. "uniform mat4 MVP;\n"
  44. "attribute vec3 vCol;\n"
  45. "attribute vec2 vPos;\n"
  46. "varying vec3 color;\n"
  47. "void main()\n"
  48. "{\n"
  49. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  50. " color = vCol;\n"
  51. "}\n";
  52. static const char* fragment_shader_text =
  53. "#version 110\n"
  54. "varying vec3 color;\n"
  55. "void main()\n"
  56. "{\n"
  57. " gl_FragColor = vec4(color, 1.0);\n"
  58. "}\n";
  59. static void error_callback(int error, const char* description)
  60. {
  61. fprintf(stderr, "Error: %s\n", description);
  62. }
  63. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  64. {
  65. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  66. glfwSetWindowShouldClose(window, GLFW_TRUE);
  67. }
  68. int main(void)
  69. {
  70. GLFWwindow* window;
  71. GLuint vertex_buffer, vertex_shader, fragment_shader, program;
  72. GLint mvp_location, vpos_location, vcol_location;
  73. glfwSetErrorCallback(error_callback);
  74. if (!glfwInit())
  75. exit(EXIT_FAILURE);
  76. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  77. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  78. window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  79. if (!window)
  80. {
  81. glfwTerminate();
  82. exit(EXIT_FAILURE);
  83. }
  84. glfwSetKeyCallback(window, key_callback);
  85. glfwMakeContextCurrent(window);
  86. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  87. glfwSwapInterval(1);
  88. // NOTE: OpenGL error checks have been omitted for brevity
  89. glGenBuffers(1, &vertex_buffer);
  90. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  91. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  92. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  93. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  94. glCompileShader(vertex_shader);
  95. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  96. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  97. glCompileShader(fragment_shader);
  98. program = glCreateProgram();
  99. glAttachShader(program, vertex_shader);
  100. glAttachShader(program, fragment_shader);
  101. glLinkProgram(program);
  102. mvp_location = glGetUniformLocation(program, "MVP");
  103. vpos_location = glGetAttribLocation(program, "vPos");
  104. vcol_location = glGetAttribLocation(program, "vCol");
  105. glEnableVertexAttribArray(vpos_location);
  106. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  107. sizeof(vertices[0]), (void*) 0);
  108. glEnableVertexAttribArray(vcol_location);
  109. glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
  110. sizeof(vertices[0]), (void*) (sizeof(float) * 2));
  111. while (!glfwWindowShouldClose(window))
  112. {
  113. float ratio;
  114. int width, height;
  115. mat4x4 m, p, mvp;
  116. glfwGetFramebufferSize(window, &width, &height);
  117. ratio = width / (float) height;
  118. glViewport(0, 0, width, height);
  119. glClear(GL_COLOR_BUFFER_BIT);
  120. mat4x4_identity(m);
  121. mat4x4_rotate_Z(m, m, (float) glfwGetTime());
  122. mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  123. mat4x4_mul(mvp, p, m);
  124. glUseProgram(program);
  125. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  126. glDrawArrays(GL_TRIANGLES, 0, 3);
  127. glfwSwapBuffers(window);
  128. glfwPollEvents();
  129. }
  130. glfwDestroyWindow(window);
  131. glfwTerminate();
  132. exit(EXIT_SUCCESS);
  133. }
  134. //! [code]