From a280c97321dfb76604f4f432176c7daf147facb6 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Wed, 21 Sep 2016 02:44:27 +0200 Subject: [PATCH] Convert reopen test to GL2 --- tests/reopen.c | 141 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 48 deletions(-) diff --git a/tests/reopen.c b/tests/reopen.c index caaf3626..4ea0c1e2 100644 --- a/tests/reopen.c +++ b/tests/reopen.c @@ -40,16 +40,35 @@ #include #include +#include "linmath.h" + +static const char* vertex_shader_text = +"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 = +"void main()\n" +"{\n" +" gl_FragColor = vec4(1.0);\n" +"}\n"; + +static const vec2 vertices[4] = +{ + { -0.5f, -0.5f }, + { 0.5f, -0.5f }, + { 0.5f, 0.5f }, + { -0.5f, 0.5f } +}; + static void error_callback(int error, const char* description) { fprintf(stderr, "Error: %s\n", description); } -static void framebuffer_size_callback(GLFWwindow* window, int width, int height) -{ - glViewport(0, 0, width, height); -} - static void window_close_callback(GLFWwindow* window) { printf("Close callback triggered\n"); @@ -69,40 +88,6 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, } } -static GLFWwindow* open_window(int width, int height, GLFWmonitor* monitor) -{ - double base; - GLFWwindow* window; - - base = glfwGetTime(); - - window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL); - if (!window) - return NULL; - - glfwMakeContextCurrent(window); - gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); - glfwSwapInterval(1); - - glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - glfwSetWindowCloseCallback(window, window_close_callback); - glfwSetKeyCallback(window, key_callback); - - if (monitor) - { - printf("Opening full screen window on monitor %s took %0.3f seconds\n", - glfwGetMonitorName(monitor), - glfwGetTime() - base); - } - else - { - printf("Opening regular window took %0.3f seconds\n", - glfwGetTime() - base); - } - - return window; -} - static void close_window(GLFWwindow* window) { double base = glfwGetTime(); @@ -113,6 +98,7 @@ static void close_window(GLFWwindow* window) int main(int argc, char** argv) { int count = 0; + double base; GLFWwindow* window; srand((unsigned int) time(NULL)); @@ -122,12 +108,17 @@ int main(int argc, char** argv) if (!glfwInit()) exit(EXIT_FAILURE); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + for (;;) { int width, height; GLFWmonitor* monitor = NULL; + GLuint vertex_shader, fragment_shader, program, vertex_buffer; + GLint mvp_location, vpos_location; - if (count % 2 == 0) + if (count & 1) { int monitorCount; GLFWmonitor** monitors = glfwGetMonitors(&monitorCount); @@ -146,27 +137,81 @@ int main(int argc, char** argv) height = 480; } - window = open_window(width, height, monitor); + base = glfwGetTime(); + + window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } - glMatrixMode(GL_PROJECTION); - glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f); - glMatrixMode(GL_MODELVIEW); + if (monitor) + { + printf("Opening full screen window on monitor %s took %0.3f seconds\n", + glfwGetMonitorName(monitor), + glfwGetTime() - base); + } + else + { + printf("Opening regular window took %0.3f seconds\n", + glfwGetTime() - base); + } + + glfwSetWindowCloseCallback(window, window_close_callback); + glfwSetKeyCallback(window, key_callback); + + glfwMakeContextCurrent(window); + gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); + glfwSwapInterval(1); + + 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"); + + glGenBuffers(1, &vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + glEnableVertexAttribArray(vpos_location); + glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, + sizeof(vertices[0]), (void*) 0); glfwSetTime(0.0); while (glfwGetTime() < 5.0) { + float ratio; + int width, height; + mat4x4 m, p, mvp; + + glfwGetFramebufferSize(window, &width, &height); + ratio = width / (float) height; + + glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); - glPushMatrix(); - glRotatef((GLfloat) glfwGetTime() * 100.f, 0.f, 0.f, 1.f); - glRectf(-0.5f, -0.5f, 1.f, 1.f); - glPopMatrix(); + mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f); + + mat4x4_identity(m); + mat4x4_rotate_Z(m, m, (float) glfwGetTime()); + mat4x4_mul(mvp, p, m); + + glUseProgram(program); + glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glfwSwapBuffers(window); glfwPollEvents();