msaa.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //========================================================================
  2. // Multisample anti-aliasing test
  3. // Copyright (c) Camilla Löwy <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. //
  26. // This test renders two high contrast, slowly rotating quads, one aliased
  27. // and one (hopefully) anti-aliased, thus allowing for visual verification
  28. // of whether MSAA is indeed enabled
  29. //
  30. //========================================================================
  31. #include <glad/gl.h>
  32. #define GLFW_INCLUDE_NONE
  33. #include <GLFW/glfw3.h>
  34. #if defined(_MSC_VER)
  35. // Make MS math.h define M_PI
  36. #define _USE_MATH_DEFINES
  37. #endif
  38. #include "linmath.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include "getopt.h"
  42. static const vec2 vertices[4] =
  43. {
  44. { -0.6f, -0.6f },
  45. { 0.6f, -0.6f },
  46. { 0.6f, 0.6f },
  47. { -0.6f, 0.6f }
  48. };
  49. static const char* vertex_shader_text =
  50. "#version 110\n"
  51. "uniform mat4 MVP;\n"
  52. "attribute vec2 vPos;\n"
  53. "void main()\n"
  54. "{\n"
  55. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  56. "}\n";
  57. static const char* fragment_shader_text =
  58. "#version 110\n"
  59. "void main()\n"
  60. "{\n"
  61. " gl_FragColor = vec4(1.0);\n"
  62. "}\n";
  63. static void error_callback(int error, const char* description)
  64. {
  65. fprintf(stderr, "Error: %s\n", description);
  66. }
  67. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  68. {
  69. if (action != GLFW_PRESS)
  70. return;
  71. switch (key)
  72. {
  73. case GLFW_KEY_SPACE:
  74. glfwSetTime(0.0);
  75. break;
  76. case GLFW_KEY_ESCAPE:
  77. glfwSetWindowShouldClose(window, GLFW_TRUE);
  78. break;
  79. }
  80. }
  81. static void usage(void)
  82. {
  83. printf("Usage: msaa [-h] [-s SAMPLES]\n");
  84. }
  85. int main(int argc, char** argv)
  86. {
  87. int ch, samples = 4;
  88. GLFWwindow* window;
  89. GLuint vertex_buffer, vertex_shader, fragment_shader, program;
  90. GLint mvp_location, vpos_location;
  91. while ((ch = getopt(argc, argv, "hs:")) != -1)
  92. {
  93. switch (ch)
  94. {
  95. case 'h':
  96. usage();
  97. exit(EXIT_SUCCESS);
  98. case 's':
  99. samples = atoi(optarg);
  100. break;
  101. default:
  102. usage();
  103. exit(EXIT_FAILURE);
  104. }
  105. }
  106. glfwSetErrorCallback(error_callback);
  107. if (!glfwInit())
  108. exit(EXIT_FAILURE);
  109. if (samples)
  110. printf("Requesting MSAA with %i samples\n", samples);
  111. else
  112. printf("Requesting that MSAA not be available\n");
  113. glfwWindowHint(GLFW_SAMPLES, samples);
  114. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  115. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  116. window = glfwCreateWindow(800, 400, "Aliasing Detector", NULL, NULL);
  117. if (!window)
  118. {
  119. glfwTerminate();
  120. exit(EXIT_FAILURE);
  121. }
  122. glfwSetKeyCallback(window, key_callback);
  123. glfwMakeContextCurrent(window);
  124. gladLoadGL(glfwGetProcAddress);
  125. glfwSwapInterval(1);
  126. glGetIntegerv(GL_SAMPLES, &samples);
  127. if (samples)
  128. printf("Context reports MSAA is available with %i samples\n", samples);
  129. else
  130. printf("Context reports MSAA is unavailable\n");
  131. glGenBuffers(1, &vertex_buffer);
  132. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  133. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  134. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  135. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  136. glCompileShader(vertex_shader);
  137. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  138. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  139. glCompileShader(fragment_shader);
  140. program = glCreateProgram();
  141. glAttachShader(program, vertex_shader);
  142. glAttachShader(program, fragment_shader);
  143. glLinkProgram(program);
  144. mvp_location = glGetUniformLocation(program, "MVP");
  145. vpos_location = glGetAttribLocation(program, "vPos");
  146. glEnableVertexAttribArray(vpos_location);
  147. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  148. sizeof(vertices[0]), (void*) 0);
  149. while (!glfwWindowShouldClose(window))
  150. {
  151. float ratio;
  152. int width, height;
  153. mat4x4 m, p, mvp;
  154. const double angle = glfwGetTime() * M_PI / 180.0;
  155. glfwGetFramebufferSize(window, &width, &height);
  156. ratio = width / (float) height;
  157. glViewport(0, 0, width, height);
  158. glClear(GL_COLOR_BUFFER_BIT);
  159. glUseProgram(program);
  160. mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f);
  161. mat4x4_translate(m, -1.f, 0.f, 0.f);
  162. mat4x4_rotate_Z(m, m, (float) angle);
  163. mat4x4_mul(mvp, p, m);
  164. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  165. glDisable(GL_MULTISAMPLE);
  166. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  167. mat4x4_translate(m, 1.f, 0.f, 0.f);
  168. mat4x4_rotate_Z(m, m, (float) angle);
  169. mat4x4_mul(mvp, p, m);
  170. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  171. glEnable(GL_MULTISAMPLE);
  172. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  173. glfwSwapBuffers(window);
  174. glfwPollEvents();
  175. }
  176. glfwDestroyWindow(window);
  177. glfwTerminate();
  178. exit(EXIT_SUCCESS);
  179. }