1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-23 10:48:51 -05:00
glfw/tests/msaa.c

221 lines
6.0 KiB
C
Raw Normal View History

2010-09-07 11:34:51 -04:00
//========================================================================
2015-05-19 09:16:42 -04:00
// Multisample anti-aliasing test
2016-11-21 10:23:59 -05:00
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
2010-09-07 11:34:51 -04:00
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
//
// This test renders two high contrast, slowly rotating quads, one aliased
// and one (hopefully) anti-aliased, thus allowing for visual verification
2015-05-19 09:16:42 -04:00
// of whether MSAA is indeed enabled
2010-09-07 11:34:51 -04:00
//
//========================================================================
#include <glad/gl.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
2010-09-07 11:34:51 -04:00
2016-08-21 17:37:49 -04:00
#if defined(_MSC_VER)
// Make MS math.h define M_PI
#define _USE_MATH_DEFINES
#endif
#include "linmath.h"
2010-09-07 11:34:51 -04:00
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
2016-09-21 09:40:27 -04:00
static const vec2 vertices[4] =
2016-08-21 17:37:49 -04:00
{
{ -0.6f, -0.6f },
{ 0.6f, -0.6f },
{ 0.6f, 0.6f },
{ -0.6f, 0.6f }
};
static const char* vertex_shader_text =
"#version 110\n"
2016-08-21 17:37:49 -04:00
"uniform mat4 MVP;\n"
"attribute vec2 vPos;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
"}\n";
static const char* fragment_shader_text =
"#version 110\n"
2016-08-21 17:37:49 -04:00
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0);\n"
"}\n";
2016-08-21 17:37:49 -04:00
static void error_callback(int error, const char* description)
2010-09-07 11:34:51 -04:00
{
2016-08-21 17:37:49 -04:00
fprintf(stderr, "Error: %s\n", description);
2010-09-07 11:34:51 -04:00
}
2013-05-30 11:19:12 -04:00
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
2011-03-05 07:17:39 -05:00
{
if (action != GLFW_PRESS)
return;
2011-03-05 07:17:39 -05:00
switch (key)
{
case GLFW_KEY_SPACE:
glfwSetTime(0.0);
break;
2016-08-21 18:05:44 -04:00
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GLFW_TRUE);
break;
}
2011-03-05 07:17:39 -05:00
}
static void usage(void)
2010-09-07 11:34:51 -04:00
{
2015-05-19 09:16:42 -04:00
printf("Usage: msaa [-h] [-s SAMPLES]\n");
}
int main(int argc, char** argv)
{
int ch, samples = 4;
GLFWwindow* window;
2016-08-21 17:37:49 -04:00
GLuint vertex_buffer, vertex_shader, fragment_shader, program;
2016-09-20 09:01:15 -04:00
GLint mvp_location, vpos_location;
while ((ch = getopt(argc, argv, "hs:")) != -1)
{
switch (ch)
{
case 'h':
usage();
exit(EXIT_SUCCESS);
case 's':
samples = atoi(optarg);
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
glfwSetErrorCallback(error_callback);
2010-09-07 11:34:51 -04:00
if (!glfwInit())
exit(EXIT_FAILURE);
if (samples)
2015-05-19 09:16:42 -04:00
printf("Requesting MSAA with %i samples\n", samples);
else
2015-05-19 09:16:42 -04:00
printf("Requesting that MSAA not be available\n");
glfwWindowHint(GLFW_SAMPLES, samples);
2016-08-21 17:37:49 -04:00
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
2010-09-07 11:34:51 -04:00
2012-09-27 15:37:36 -04:00
window = glfwCreateWindow(800, 400, "Aliasing Detector", NULL, NULL);
if (!window)
2010-09-07 11:34:51 -04:00
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(1);
2015-10-13 21:11:20 -04:00
glGetIntegerv(GL_SAMPLES, &samples);
2010-09-07 11:34:51 -04:00
if (samples)
2015-05-19 09:16:42 -04:00
printf("Context reports MSAA is available with %i samples\n", samples);
2010-09-07 11:34:51 -04:00
else
2015-05-19 09:16:42 -04:00
printf("Context reports MSAA is unavailable\n");
2010-09-07 11:34:51 -04:00
2016-08-21 17:37:49 -04:00
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
mvp_location = glGetUniformLocation(program, "MVP");
vpos_location = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*) 0);
2010-09-07 11:34:51 -04:00
while (!glfwWindowShouldClose(window))
2010-09-07 11:34:51 -04:00
{
2016-08-21 17:37:49 -04:00
float ratio;
int width, height;
mat4x4 m, p, mvp;
const double angle = glfwGetTime() * M_PI / 180.0;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
2010-09-07 11:34:51 -04:00
2016-08-21 17:37:49 -04:00
glViewport(0, 0, width, height);
2010-09-07 11:34:51 -04:00
glClear(GL_COLOR_BUFFER_BIT);
2016-08-21 17:37:49 -04:00
glUseProgram(program);
2010-09-07 11:34:51 -04:00
2016-08-21 17:37:49 -04:00
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f);
2010-09-07 11:34:51 -04:00
2016-08-21 17:37:49 -04:00
mat4x4_translate(m, -1.f, 0.f, 0.f);
mat4x4_rotate_Z(m, m, (float) angle);
mat4x4_mul(mvp, p, m);
2010-09-07 11:34:51 -04:00
2016-08-21 17:37:49 -04:00
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
glDisable(GL_MULTISAMPLE);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
mat4x4_translate(m, 1.f, 0.f, 0.f);
mat4x4_rotate_Z(m, m, (float) angle);
mat4x4_mul(mvp, p, m);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
glEnable(GL_MULTISAMPLE);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2010-09-07 11:34:51 -04:00
glfwSwapBuffers(window);
glfwPollEvents();
2010-09-07 11:34:51 -04:00
}
2016-08-21 17:37:49 -04:00
glfwDestroyWindow(window);
2010-09-07 11:34:51 -04:00
glfwTerminate();
exit(EXIT_SUCCESS);
}