Platformer in OpenGL
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.3 KiB

5 년 전
  1. //========================================================================
  2. // Multisample anti-aliasing test
  3. // Copyright (c) Camilla Berglund <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/glad.h>
  32. #include <GLFW/glfw3.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "getopt.h"
  36. static void error_callback(int error, const char* description)
  37. {
  38. fprintf(stderr, "Error: %s\n", description);
  39. }
  40. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  41. {
  42. glViewport(0, 0, width, height);
  43. }
  44. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  45. {
  46. if (action != GLFW_PRESS)
  47. return;
  48. switch (key)
  49. {
  50. case GLFW_KEY_SPACE:
  51. glfwSetTime(0.0);
  52. break;
  53. }
  54. }
  55. static void usage(void)
  56. {
  57. printf("Usage: msaa [-h] [-s SAMPLES]\n");
  58. }
  59. int main(int argc, char** argv)
  60. {
  61. int ch, samples = 4;
  62. GLFWwindow* window;
  63. while ((ch = getopt(argc, argv, "hs:")) != -1)
  64. {
  65. switch (ch)
  66. {
  67. case 'h':
  68. usage();
  69. exit(EXIT_SUCCESS);
  70. case 's':
  71. samples = atoi(optarg);
  72. break;
  73. default:
  74. usage();
  75. exit(EXIT_FAILURE);
  76. }
  77. }
  78. glfwSetErrorCallback(error_callback);
  79. if (!glfwInit())
  80. exit(EXIT_FAILURE);
  81. if (samples)
  82. printf("Requesting MSAA with %i samples\n", samples);
  83. else
  84. printf("Requesting that MSAA not be available\n");
  85. glfwWindowHint(GLFW_SAMPLES, samples);
  86. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  87. window = glfwCreateWindow(800, 400, "Aliasing Detector", NULL, NULL);
  88. if (!window)
  89. {
  90. glfwTerminate();
  91. exit(EXIT_FAILURE);
  92. }
  93. glfwSetKeyCallback(window, key_callback);
  94. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  95. glfwMakeContextCurrent(window);
  96. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  97. glfwSwapInterval(1);
  98. if (!GLAD_GL_ARB_multisample && !GLAD_GL_VERSION_1_3)
  99. {
  100. printf("Multisampling is not supported\n");
  101. glfwTerminate();
  102. exit(EXIT_FAILURE);
  103. }
  104. glfwShowWindow(window);
  105. glGetIntegerv(GL_SAMPLES, &samples);
  106. if (samples)
  107. printf("Context reports MSAA is available with %i samples\n", samples);
  108. else
  109. printf("Context reports MSAA is unavailable\n");
  110. glMatrixMode(GL_PROJECTION);
  111. glOrtho(0.f, 1.f, 0.f, 0.5f, 0.f, 1.f);
  112. glMatrixMode(GL_MODELVIEW);
  113. while (!glfwWindowShouldClose(window))
  114. {
  115. GLfloat time = (GLfloat) glfwGetTime();
  116. glClear(GL_COLOR_BUFFER_BIT);
  117. glLoadIdentity();
  118. glTranslatef(0.25f, 0.25f, 0.f);
  119. glRotatef(time, 0.f, 0.f, 1.f);
  120. glDisable(GL_MULTISAMPLE_ARB);
  121. glRectf(-0.15f, -0.15f, 0.15f, 0.15f);
  122. glLoadIdentity();
  123. glTranslatef(0.75f, 0.25f, 0.f);
  124. glRotatef(time, 0.f, 0.f, 1.f);
  125. glEnable(GL_MULTISAMPLE_ARB);
  126. glRectf(-0.15f, -0.15f, 0.15f, 0.15f);
  127. glfwSwapBuffers(window);
  128. glfwPollEvents();
  129. }
  130. glfwTerminate();
  131. exit(EXIT_SUCCESS);
  132. }