2
0

msaa.c 6.0 KB

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