msaa.c
Go to the documentation of this file.
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 
32 #include <glad/glad.h>
33 #include <GLFW/glfw3.h>
34 
35 #if defined(_MSC_VER)
36  // Make MS math.h define M_PI
37  #define _USE_MATH_DEFINES
38 #endif
39 
40 #include "linmath.h"
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 
45 #include "getopt.h"
46 
47 static const vec2 vertices[4] =
48 {
49  { -0.6f, -0.6f },
50  { 0.6f, -0.6f },
51  { 0.6f, 0.6f },
52  { -0.6f, 0.6f }
53 };
54 
55 static const char* vertex_shader_text =
56 "#version 110\n"
57 "uniform mat4 MVP;\n"
58 "attribute vec2 vPos;\n"
59 "void main()\n"
60 "{\n"
61 " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
62 "}\n";
63 
64 static const char* fragment_shader_text =
65 "#version 110\n"
66 "void main()\n"
67 "{\n"
68 " gl_FragColor = vec4(1.0);\n"
69 "}\n";
70 
71 static void error_callback(int error, const char* description)
72 {
73  fprintf(stderr, "Error: %s\n", description);
74 }
75 
76 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
77 {
78  if (action != GLFW_PRESS)
79  return;
80 
81  switch (key)
82  {
83  case GLFW_KEY_SPACE:
84  glfwSetTime(0.0);
85  break;
86  case GLFW_KEY_ESCAPE:
88  break;
89  }
90 }
91 
92 static void usage(void)
93 {
94  printf("Usage: msaa [-h] [-s SAMPLES]\n");
95 }
96 
97 int main(int argc, char** argv)
98 {
99  int ch, samples = 4;
101  GLuint vertex_buffer, vertex_shader, fragment_shader, program;
102  GLint mvp_location, vpos_location;
103 
104  while ((ch = getopt(argc, argv, "hs:")) != -1)
105  {
106  switch (ch)
107  {
108  case 'h':
109  usage();
110  exit(EXIT_SUCCESS);
111  case 's':
112  samples = atoi(optarg);
113  break;
114  default:
115  usage();
116  exit(EXIT_FAILURE);
117  }
118  }
119 
121 
122  if (!glfwInit())
123  exit(EXIT_FAILURE);
124 
125  if (samples)
126  printf("Requesting MSAA with %i samples\n", samples);
127  else
128  printf("Requesting that MSAA not be available\n");
129 
130  glfwWindowHint(GLFW_SAMPLES, samples);
133 
134  window = glfwCreateWindow(800, 400, "Aliasing Detector", NULL, NULL);
135  if (!window)
136  {
137  glfwTerminate();
138  exit(EXIT_FAILURE);
139  }
140 
142 
143  glfwMakeContextCurrent(window);
145  glfwSwapInterval(1);
146 
147  glGetIntegerv(GL_SAMPLES, &samples);
148  if (samples)
149  printf("Context reports MSAA is available with %i samples\n", samples);
150  else
151  printf("Context reports MSAA is unavailable\n");
152 
153  glGenBuffers(1, &vertex_buffer);
154  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
156 
157  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
158  glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
159  glCompileShader(vertex_shader);
160 
161  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
162  glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
163  glCompileShader(fragment_shader);
164 
165  program = glCreateProgram();
166  glAttachShader(program, vertex_shader);
167  glAttachShader(program, fragment_shader);
168  glLinkProgram(program);
169 
170  mvp_location = glGetUniformLocation(program, "MVP");
171  vpos_location = glGetAttribLocation(program, "vPos");
172 
173  glEnableVertexAttribArray(vpos_location);
174  glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
175  sizeof(vertices[0]), (void*) 0);
176 
177  while (!glfwWindowShouldClose(window))
178  {
179  float ratio;
180  int width, height;
181  mat4x4 m, p, mvp;
182  const double angle = glfwGetTime() * M_PI / 180.0;
183 
184  glfwGetFramebufferSize(window, &width, &height);
185  ratio = width / (float) height;
186 
187  glViewport(0, 0, width, height);
189 
190  glUseProgram(program);
191 
192  mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f);
193 
194  mat4x4_translate(m, -1.f, 0.f, 0.f);
195  mat4x4_rotate_Z(m, m, (float) angle);
196  mat4x4_mul(mvp, p, m);
197 
198  glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
201 
202  mat4x4_translate(m, 1.f, 0.f, 0.f);
203  mat4x4_rotate_Z(m, m, (float) angle);
204  mat4x4_mul(mvp, p, m);
205 
206  glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
209 
210  glfwSwapBuffers(window);
211  glfwPollEvents();
212  }
213 
214  glfwDestroyWindow(window);
215 
216  glfwTerminate();
217  exit(EXIT_SUCCESS);
218 }
219 
#define glBufferData
#define glCreateProgram
#define GL_VERTEX_SHADER
vec4 mat4x4[4]
Definition: linmath.h:83
#define GL_STATIC_DRAW
khronos_float_t GLfloat
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
static void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
Definition: linmath.h:341
int main(int argc, char **argv)
Definition: msaa.c:97
#define glGetAttribLocation
static const vec2 vertices[4]
Definition: msaa.c:47
GLfloat GLfloat p
Definition: glext.h:12687
const GLfloat * m
Definition: glext.h:6814
void *(* GLADloadproc)(const char *name)
static GLFWwindow * window
Definition: joysticks.c:55
#define glGetIntegerv
static void error_callback(int error, const char *description)
Definition: msaa.c:71
#define GLFW_CONTEXT_VERSION_MAJOR
Context client API major version hint and attribute.
Definition: glfw3.h:921
#define GLFW_KEY_SPACE
Definition: glfw3.h:360
#define glDrawArrays
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
GLfloat angle
Definition: glext.h:6819
#define glUniformMatrix4fv
static void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle)
Definition: linmath.h:247
#define GL_MULTISAMPLE
int getopt(int argc, char *const argv[], const char *optstring)
Definition: getopt.c:52
#define glUseProgram
#define glEnable
#define glCreateShader
GLuint64 key
Definition: glext.h:8966
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
#define glVertexAttribPointer
static void mat4x4_translate(mat4x4 T, float x, float y, float z)
Definition: linmath.h:165
#define GL_FLOAT
#define glEnableVertexAttribArray
#define GL_COLOR_BUFFER_BIT
GLbitfield GLuint program
Definition: glext.h:1889
int GLint
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
#define glBindBuffer
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
#define glGetUniformLocation
#define GL_FRAGMENT_SHADER
#define glShaderSource
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: msaa.c:76
action
Definition: enums.py:62
#define glViewport
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
#define glAttachShader
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
#define glCompileShader
GLFWAPI void glfwSetTime(double time)
Sets the GLFW timer.
Definition: input.c:1282
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
#define GLFW_TRUE
One.
Definition: glfw3.h:279
#define GL_FALSE
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
unsigned int GLuint
#define glGenBuffers
GLsizei samples
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
#define glLinkProgram
#define GL_TRIANGLE_FAN
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
#define GL_SAMPLES
#define GLFW_SAMPLES
Framebuffer MSAA samples hint.
Definition: glfw3.h:893
static void usage(void)
Definition: msaa.c:92
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:1072
#define NULL
Definition: tinycthread.c:47
char * optarg
Definition: getopt.c:36
static void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b)
Definition: linmath.h:145
#define GLFW_CONTEXT_VERSION_MINOR
Context client API minor version hint and attribute.
Definition: glfw3.h:927
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
static const char * fragment_shader_text
Definition: msaa.c:64
#define GL_ARRAY_BUFFER
static const char * vertex_shader_text
Definition: msaa.c:55
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
#define glDisable
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:22