-
Notifications
You must be signed in to change notification settings - Fork 3
/
cube.cpp
219 lines (165 loc) · 6.14 KB
/
cube.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdlib.h>
#include <stdio.h>
#include "gl.h"
#include "shader.h"
#include "linmath.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, "Cube - 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"
"out vec3 vNormal;\n"
"\n"
"in vec3 position;\n"
"in vec3 normal;\n"
"\n"
"uniform mat4 mvp;\n"
"\n"
"void main()\n"
"{\n"
" vNormal = normal;\n"
" gl_Position = mvp * vec4(position, 1.0);\n"
"}\n";
static const GLchar *fragmentShaderSource =
"#version 330 core\n"
"\n"
"out vec4 fragColor;\n"
"\n"
"in vec3 vNormal;\n"
"\n"
"void main()\n"
"{\n"
" fragColor = vec4(abs(vNormal), 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 normalLocation = glGetAttribLocation(program, "normal");
const GLint mvpLocation = glGetUniformLocation(program, "mvp");
if ((positionLocation == -1) || (normalLocation == -1))
return EXIT_FAILURE;
static const GLfloat vertices[] = {
// X, Y, Z, NX, NY, NZ
// Front
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // Top Left
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // Bottom Left
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // Top Right
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // Top Right
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // Bottom Left
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // Bottom Right
// Back
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // Top Left
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // Bottom Left
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // Top Right
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // Top Right
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // Bottom Left
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // Bottom Right
// Top
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // Top Left
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Bottom Left
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // Top Right
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // Top Right
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Bottom Left
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Bottom Right
// Bottom
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, // Top Left
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, // Bottom Left
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, // Top Right
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, // Top Right
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, // Bottom Left
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, // Bottom Right
// Right
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Top Left
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Bottom Left
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // Top Right
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // Top Right
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Bottom Left
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // Bottom Right
// Left
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // Top Left
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // Bottom Left
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // Top Right
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // Top Right
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // Bottom Left
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // Bottom Right
};
static const GLsizei vertexSize = 6;
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, 3, GL_FLOAT, GL_FALSE, vertexSize * sizeof(GLfloat), 0);
glEnableVertexAttribArray((GLuint)normalLocation);
glVertexAttribPointer((GLuint)normalLocation, 3, GL_FLOAT, GL_FALSE, vertexSize * sizeof(GLfloat), reinterpret_cast<const GLvoid*>(3 * sizeof(GLfloat)));
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
float model[16], view[16], projection[16];
float mvp[16];
float temp[16];
static const float fov = 70.0f;
static const float zNear = 0.01f;
static const float zFar = 10.0f;
while (!glfwWindowShouldClose(window))
{
int viewportWidth, viewportHeight;
glfwGetFramebufferSize(window, &viewportWidth, &viewportHeight);
if (mvpLocation != -1)
{
const float aspect = static_cast<float>(viewportWidth) / static_cast<float>(viewportHeight);
const float time = static_cast<GLfloat>(glfwGetTime());
mat4Perspective(projection, fov, aspect, zNear, zFar);
mat4Translation(view, 0.0f, 0.0f, -2.0f);
mat4Rotation(model, time, 0.0f, 1.0f, 0.0f);
mat4Multiply(temp, projection, view);
mat4Multiply(mvp, temp, model);
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, mvp);
}
glViewport(0, 0, viewportWidth, viewportHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteProgram(program);
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS;
}