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