-
Notifications
You must be signed in to change notification settings - Fork 3
/
screen_quad.cpp
145 lines (109 loc) · 3.7 KB
/
screen_quad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdlib.h>
#include <stdio.h>
#include "gl.h"
#include "shader.h"
#include "glfw_utilities.h"
int main(int argc, char *argv[])
{
if (!glfwInit())
{
fprintf(stderr, "Failed initializing GLFW\n");
return EXIT_FAILURE;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow *window = glfwCreateWindow(640, 480, "Screen Quad - GLCollection", NULL, NULL);
if (!window)
{
fprintf(stderr, "Failed creating window\n");
glfwTerminate();
return EXIT_FAILURE;
}
glcCenterWindow(window, glcGetBestMonitor(window));
glfwShowWindow(window);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
fprintf(stderr, "Failed loading OpenGL functions and extensions\n");
glfwTerminate();
return EXIT_FAILURE;
}
static const GLchar *vertexShaderSource =
"#version 330 core\n"
"\n"
"in vec2 position;\n"
"in vec2 texCoord;\n"
"\n"
"out vec2 vTexCoord;\n"
"\n"
"void main()\n"
"{\n"
" vTexCoord = texCoord;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
static const GLchar *fragmentShaderSource =
"#version 330 core\n"
"\n"
"out vec4 fragColor;\n"
"\n"
"in vec2 vTexCoord;\n"
"\n"
"uniform float time;\n"
"\n"
"void main()\n"
"{\n"
// " fragColor = vec4(vTexCoord, 0.0, 1.0);\n"
" fragColor = vec4(sin(time + vTexCoord.xyx + vec3(0.0, 2.0, 4.0)) * 0.5 + 0.5, 1.0);\n"
"}\n";
const GLuint vertexShader = glcCreateShader(GL_VERTEX_SHADER, vertexShaderSource);
const GLuint fragmentShader = glcCreateShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
if ((vertexShader == GLC_NULL_HANDLE) || (fragmentShader == GLC_NULL_HANDLE))
return EXIT_FAILURE;
const GLuint program = glcCreateProgram(vertexShader, fragmentShader);
if (program == GLC_NULL_HANDLE)
return EXIT_FAILURE;
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
glUseProgram(program);
const GLint positionLocation = glGetAttribLocation(program, "position");
const GLint texCoordLocation = glGetAttribLocation(program, "texCoord");
const GLint timeLocation = glGetUniformLocation(program, "time");
if ((positionLocation == -1) || (texCoordLocation == -1))
return EXIT_FAILURE;
static const GLfloat vertices[] = {
// X, Y, U, V
-1.0f, 1.0f, 0.0f, 1.0f, // Top Left
-1.0f, -1.0f, 0.0f, 0.0f, // Bottom Left
1.0f, -1.0f, 1.0f, 0.0f, // Bottom Right
1.0f, 1.0f, 1.0f, 1.0f, // Top Right
};
static const GLsizei vertexSize = 4;
static const GLsizei vertexCount = sizeof(vertices) / sizeof(*vertices) / vertexSize;
GLuint vao, vbo;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glCreateBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray((GLuint)positionLocation);
glVertexAttribPointer((GLuint)positionLocation, 2, GL_FLOAT, GL_FALSE, vertexSize * sizeof(GLfloat), 0);
glEnableVertexAttribArray((GLuint)texCoordLocation);
glVertexAttribPointer((GLuint)texCoordLocation, 2, GL_FLOAT, GL_FALSE, vertexSize * sizeof(GLfloat), reinterpret_cast<const GLvoid*>(2 * sizeof(GLfloat)));
while (!glfwWindowShouldClose(window))
{
int viewportWidth, viewportHeight;
glfwGetFramebufferSize(window, &viewportWidth, &viewportHeight);
glViewport(0, 0, viewportWidth, viewportHeight);
if (timeLocation != -1)
glUniform1f(timeLocation, static_cast<GLfloat>(glfwGetTime()));
glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteProgram(program);
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS;
}