-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
291 lines (224 loc) · 6.21 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// OpenGL headers
#include <GL/glew.h> // this must be included before gl/GL.h
#include <GL/gl.h>
#include "include/vmath.h"
using namespace vmath;
#include "include/window_manager.h"
#include "include/LoadShaders.h"
// #include "include/printGLinfo.h"
GLuint shaderProgramObject = 0;
enum
{
AMC_ATTRIBUTE_POSITION,
AMC_ATTRIBUTE_COLOR
};
GLuint vao_cube = 0;
GLuint vbo_position_cube = 0;
GLuint vbo_color_cube = 0;
GLuint mvpMatrixUniform = 0;
mat4 perspectiveProjectionMatrix;
GLfloat angle_cube = 0.0f;
void init(void);
void uninit();
void keyboard(unsigned int);
void draw(void);
void update(void);
void resize(int, int);
// entry point function
int main(int argc, char **argv)
{
acewmInitializeCallback(init);
acewmKeyboardCallback(keyboard);
acewmReshapeCallback(resize);
acewmDisplayCallback(draw);
acewmUpdateCallback(update);
acewmCreateWindow("ACE engine demo!!!", 100, 100, 800, 600);
acewmEventLoop();
acewmUninitiiizeCallback(uninit);
return 0;
}
void keyboard(unsigned int)
{
}
void init(void)
{
// function declarations
// printGLInfo(gpFile);
// initialize GLEW
if (glewInit() != GLEW_OK)
{
exit(1);
}
// shader program
ShaderInfo shaders[] =
{
{ GL_VERTEX_SHADER, "./shaders/shader.vs" },
{ GL_FRAGMENT_SHADER, "./shaders/shader.fs" },
{ GL_NONE, NULL }
};
shaderProgramObject = LoadShaders( shaders );
glUseProgram( shaderProgramObject );
// get shader uniform location
mvpMatrixUniform = glGetUniformLocation(shaderProgramObject, "uMVPMatrix");
// interleaved
const GLfloat cubeVertices[] = {
// top
1.0f, 1.0f, -1.0f,
0.0f, 1.0f, 0.0f,
-1.0f, 1.0f, -1.0f,
0.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f,
// bottom
1.0f, -1.0f, -1.0f,
1.0f, 0.5f, 0.0f,
-1.0f, -1.0f, -1.0f,
1.0f, 0.5f, 0.0f,
-1.0f, -1.0f, 1.0f,
1.0f, 0.5f, 0.0f,
1.0f, -1.0f, 1.0f,
1.0f, 0.5f, 0.0f,
// front
1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
// back
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 0.0f,
// right
1.0f, 1.0f, -1.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f,
0.0f, 0.0f, 1.0f,
1.0f, -1.0f, -1.0f,
0.0f, 0.0f, 1.0f,
// left
-1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, 0.0f, 1.0f
};
// Vertex Array Object for cube
glGenVertexArrays(1, &vao_cube);
glBindVertexArray(vao_cube);
// VBO for position
glGenBuffers(1, &vbo_position_cube);
glBindBuffer(GL_ARRAY_BUFFER, vbo_position_cube);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glVertexAttribPointer(AMC_ATTRIBUTE_POSITION, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(AMC_ATTRIBUTE_POSITION);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// VBO for color
glGenBuffers(1, &vbo_color_cube);
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_cube);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glVertexAttribPointer(AMC_ATTRIBUTE_COLOR, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(AMC_ATTRIBUTE_COLOR);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// enabling depth
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
// set the clearColor() of window to blue
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// initialize perspective projection matrix
perspectiveProjectionMatrix = vmath::mat4::identity();
// return(0);
}
void resize(int width, int height)
{
// code
if (height <= 0)
height = 1;
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
// set perspective projection matrix
perspectiveProjectionMatrix = vmath::perspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
}
void draw(void)
{
// code
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgramObject);
// transformations
// pyramid
mat4 modelViewMatrix = vmath::mat4::identity();
mat4 modelViewProjectionMatrix = vmath::mat4::identity();
mat4 translationMatrix = vmath::mat4::identity();
mat4 rotationMatrix = vmath::mat4::identity();
// cube
translationMatrix = vmath::translate(0.0f, 0.0f, -6.0f);
mat4 scaleMatrix = vmath::mat4::identity();
scaleMatrix = vmath::scale(0.75f, 0.75f, 0.75f);
mat4 rotationMatrix1 = vmath::mat4::identity();
rotationMatrix1 = vmath::rotate(angle_cube, 1.0f, 0.0f, 0.0f);
mat4 rotationMatrix2 = vmath::mat4::identity();
rotationMatrix2 = vmath::rotate(angle_cube, 0.0f, 1.0f, 0.0f);
mat4 rotationMatrix3 = vmath::mat4::identity();
rotationMatrix3 = vmath::rotate(angle_cube, 0.0f, 0.0f, 1.0f);
rotationMatrix = rotationMatrix1 * rotationMatrix2 * rotationMatrix3;
modelViewMatrix = translationMatrix * scaleMatrix * rotationMatrix;
modelViewProjectionMatrix = perspectiveProjectionMatrix * modelViewMatrix; // order of multiplication is important
// push above mvp vertex shader's mvp uniform
glUniformMatrix4fv(mvpMatrixUniform, 1, GL_FALSE, modelViewProjectionMatrix);
glBindVertexArray(vao_cube);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDrawArrays(GL_TRIANGLE_FAN, 4, 4);
glDrawArrays(GL_TRIANGLE_FAN, 8, 4);
glDrawArrays(GL_TRIANGLE_FAN, 12, 4);
glDrawArrays(GL_TRIANGLE_FAN, 16, 4);
glDrawArrays(GL_TRIANGLE_FAN, 20, 4);
glBindVertexArray(0);
glUseProgram(0);
acewmSwapBuffers();
}
void update(void)
{
// code
angle_cube = angle_cube - 1.0f;
if (angle_cube <= 0.0f)
{
angle_cube = angle_cube + 360.0f;
}
}
void uninit(void)
{
// code
DeleteShaderProg(shaderProgramObject);
// cube
if (vbo_color_cube)
{
glDeleteBuffers(1, &vbo_color_cube);
vbo_color_cube = 0;
}
if (vbo_position_cube)
{
glDeleteBuffers(1, &vbo_position_cube);
vbo_position_cube = 0;
}
// delete VAO
if (vao_cube)
{
glDeleteVertexArrays(1, &vao_cube);
vao_cube = 0;
}
}