msaa.c 6.0 KB

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