-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglRenderer.cpp
333 lines (271 loc) · 8.84 KB
/
glRenderer.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* OpenGL instanced mesh rendering
*
* Copyright (C) 2015 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <array>
#include "glRenderer.h"
using namespace std;
#define deg_to_rad(deg) deg * float(M_PI / 180)
#define INSTANCING_RANGE 3
struct UboInstanceData {
// Model matrix for each instance
glm::mat4 model;
// Color for each instance
// vec4 is used due to memory alignment
// GPU aligns at 16 bytes
glm::vec4 color;
};
struct {
// Global matrices
struct {
glm::mat4 projection;
glm::mat4 view;
} matrices;
// Seperate data for each instance
} uboVS;
std::array<UboInstanceData, (INSTANCING_RANGE * 2 + 1)*(INSTANCING_RANGE * 2 + 1)*(INSTANCING_RANGE * 2 + 1)> uboInstance;
string readFile(const char *fileName)
{
string fileContent;
ifstream fileStream(fileName, ios::in);
if (!fileStream.is_open()) {
printf("File %s not found\n", fileName);
return "";
}
string line = "";
while (!fileStream.eof()) {
getline(fileStream, line);
fileContent.append(line + "\n");
}
fileStream.close();
return fileContent;
}
glRenderer::glRenderer()
{
}
glRenderer::~glRenderer()
{
}
void glRenderer::printProgramLog(GLuint program)
{
GLint result = GL_FALSE;
int logLength;
glGetProgramiv(program, GL_LINK_STATUS, &result);
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar* strInfoLog = new GLchar[logLength + 1];
glGetProgramInfoLog(program, logLength, NULL, strInfoLog);
printf("programlog: %s\n", strInfoLog);
};
}
void glRenderer::printShaderLog(GLuint shader)
{
GLint result = GL_FALSE;
int logLength;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar* strInfoLog = new GLchar[logLength + 1];
glGetShaderInfoLog(shader, logLength, NULL, strInfoLog);
printf("shaderlog: %s\n", strInfoLog);
};
}
GLuint glRenderer::loadShader(const char* vertexShaderFile, const char* fragmentShaderFile)
{
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
// Read shaders
std::string vertShaderStr = readFile(vertexShaderFile);
std::string fragShaderStr = readFile(fragmentShaderFile);
const char *vertShaderSrc = vertShaderStr.c_str();
const char *fragShaderSrc = fragShaderStr.c_str();
GLint result = GL_FALSE;
// Compile vertex shader
std::cout << "Compiling vertex shader." << std::endl;
glShaderSource(vertShader, 1, &vertShaderSrc, NULL);
glCompileShader(vertShader);
printShaderLog(vertShader);
// Compile fragment shader
std::cout << "Compiling fragment shader." << std::endl;
glShaderSource(fragShader, 1, &fragShaderSrc, NULL);
glCompileShader(fragShader);
printShaderLog(fragShader);
std::cout << "Linking program" << std::endl;
GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
// Bind vertex attributes to VBO indices
glBindAttribLocation(program, 0, "inPos");
glBindAttribLocation(program, 1, "inNormal");
glLinkProgram(program);
printProgramLog(program);
glDeleteShader(vertShader);
glDeleteShader(fragShader);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, UBO);
glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOInst);
glUseProgram(program);
return program;
}
void glRenderer::generateShaders()
{
shader = loadShader("../data/shader/mesh.vert", "../data/shader/mesh.frag");
}
void glRenderer::updateUBO()
{
// Update ubo
// Projection
uboVS.matrices.projection = glm::perspective(deg_to_rad(60.0f), (float)1280 / (float)720, 0.001f, 256.0f);
// View
float zoom = -36.0f;
glm::vec3 rotation = glm::vec3(20.0f, -45.0f, 0.0f);
uboVS.matrices.view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
uboVS.matrices.view = glm::rotate(uboVS.matrices.view, deg_to_rad(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
uboVS.matrices.view = glm::rotate(uboVS.matrices.view, deg_to_rad(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
uboVS.matrices.view = glm::rotate(uboVS.matrices.view, deg_to_rad(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glBindBuffer(GL_UNIFORM_BUFFER, UBO);
GLvoid* p = glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY);
memcpy(p, &uboVS, sizeof(uboVS));
glUnmapBuffer(GL_UNIFORM_BUFFER);
// Instanced part
// Colors and model matrices are fixed
float offset = 5.0f;
uint32_t index = 0;
for (int32_t x = -INSTANCING_RANGE; x <= INSTANCING_RANGE; x++)
{
for (int32_t y = -INSTANCING_RANGE; y <= INSTANCING_RANGE; y++)
{
for (int32_t z = -INSTANCING_RANGE; z <= INSTANCING_RANGE; z++)
{
// Instance model matrix
uboInstance[index].model = glm::translate(glm::mat4(), glm::vec3(x * offset, y * offset, z * offset));
uboInstance[index].model = glm::rotate(uboInstance[index].model, deg_to_rad(-45.0f), glm::vec3(0.0f, 1.0f, 0.0f));
// Instance color (randomized)
uboInstance[index].color = glm::vec4(
(float)(rand() % 255) / 255.0f,
(float)(rand() % 255) / 255.0f,
(float)(rand() % 255) / 255.0f,
1.0);
index++;
}
}
}
uint32_t uboSize = uboInstance.size() * sizeof(UboInstanceData);
glBindBuffer(GL_UNIFORM_BUFFER, UBOInst);
p = glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY);
memcpy(p, uboInstance.data(), uboSize);
glUnmapBuffer(GL_UNIFORM_BUFFER);
}
void glRenderer::generateBuffers()
{
// Default VAO needed for OpenGL 3.3+ core profiles
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// Position data
const GLfloat pointPos[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
};
// Color data
const GLfloat pointCol[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
demoMesh = new MeshLoader();
demoMesh->LoadMesh("../data/angryteapot.X");
float scale = 0.05f;
std::vector<glm::vec3> vPos;
std::vector<glm::vec3> vNorm;
for (int m = 0; m < demoMesh->m_Entries.size(); m++)
{
for (int i = 0; i < demoMesh->m_Entries[m].Vertices.size(); i++)
{
vPos.push_back(demoMesh->m_Entries[m].Vertices[i].m_pos * scale);
vNorm.push_back(demoMesh->m_Entries[m].Vertices[i].m_normal);
}
}
uint32_t vBufferSize = vPos.size() * sizeof(glm::vec3);
std::vector<UINT32> indexBuffer;
for (int m = 0; m < demoMesh->m_Entries.size(); m++)
{
int indexBase = indexBuffer.size();
for (int i = 0; i < demoMesh->m_Entries[m].Indices.size(); i++) {
indexBuffer.push_back(demoMesh->m_Entries[m].Indices[i] + indexBase);
}
}
int iBufferSize = indexBuffer.size() * sizeof(UINT32);
indices = indexBuffer.size();
glGenBuffers(2, VBO);
glGenBuffers(1, &IBO);
// Position data
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, vBufferSize, vPos.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
// Normal data
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, vBufferSize, vNorm.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1);
// Indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iBufferSize, indexBuffer.data(), GL_STATIC_DRAW);
// Uniform buffer object
glGenBuffers(1, &UBO);
glBindBuffer(GL_UNIFORM_BUFFER, UBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(uboVS), &uboVS, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
// Instancing ubo
instanceCount = pow((INSTANCING_RANGE * 2) + 1, 3);
glGenBuffers(1, &UBOInst);
glBindBuffer(GL_UNIFORM_BUFFER, UBOInst);
glBufferData(GL_UNIFORM_BUFFER, uboInstance.size() * sizeof(UboInstanceData), uboInstance.data(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
updateUBO();
delete(demoMesh);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_DEPTH_TEST);
}
void glRenderer::renderScene()
{
double frameTimeStart = glfwGetTime();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawElementsInstanced(
GL_TRIANGLES,
indices,
GL_UNSIGNED_INT,
(void*)0,
instanceCount
);
glfwSwapBuffers(window);
}
void glRenderer::keyCallback(int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_W && action == GLFW_PRESS)
wireframe = !wireframe;
if (key == GLFW_KEY_KP_ADD && action == GLFW_PRESS && mods != GLFW_MOD_SHIFT && circleDivisions < 85)
circleDivisions += 1;
if (key == GLFW_KEY_KP_SUBTRACT && action == GLFW_PRESS && mods != GLFW_MOD_SHIFT && circleDivisions > 3)
circleDivisions -= 1;
if (key == GLFW_KEY_KP_ADD && action == GLFW_PRESS && mods == GLFW_MOD_SHIFT)
circleRadius += 0.025f;
if (key == GLFW_KEY_KP_SUBTRACT && action == GLFW_PRESS && mods == GLFW_MOD_SHIFT && circleRadius >= 0.1f)
circleRadius -= 0.025f;
}