-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.cpp
512 lines (415 loc) · 17.3 KB
/
Renderer.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include "Renderer.h"
#include "Common.h"
#include "Math.h"
#include <SOIL/SOIL.h>
#include <SDL2/SDL_image.h>
#include <iomanip>
Renderer::Renderer():
modelMatrix(1.0f), projectionMatrix(1.0f), cameraMatrix(1.0f)
{
title = "OpenGL 4.3 Test";
mf_width = 640.0f;
mf_height = 480.0f;
mf_aspectRatio = mf_width / mf_height;
mf_fov = 90.0f;
mf_near = 1.0f;
mf_far = 500.0f;
projMatLocation = -1;
cameraMatLocation = -1;
modelMatLocation = -1;
projectionMatrix = perspectiveProjection(mf_fov, mf_aspectRatio, mf_near, mf_far);
}
Renderer::Renderer(float width, float height):
modelMatrix(1.0f), projectionMatrix(1.0f), cameraMatrix(1.0f)
{
title = "OpenGL 4.3 Test";
mf_width = width;
mf_height = height;
mf_aspectRatio = mf_width / mf_height;
mf_fov = 90.0f;
mf_near = 1.0f;
mf_far = 500.0f;
projMatLocation = -1;
cameraMatLocation = -1;
modelMatLocation = -1;
projectionMatrix = perspectiveProjection(mf_fov, mf_aspectRatio, mf_near, mf_far);
}
Renderer::Renderer(float width, float height, float near, float far, float fov):
modelMatrix(1.0f), projectionMatrix(1.0f), cameraMatrix(1.0f)
{
title = "OpenGL 4.3 Test";
mf_width = width;
mf_height = height;
mf_aspectRatio = mf_width / mf_height;
mf_fov = fov;
mf_near = near;
mf_far = far;
projMatLocation = -1;
cameraMatLocation = -1;
modelMatLocation = -1;
projectionMatrix = perspectiveProjection(mf_fov, mf_aspectRatio, mf_near, mf_far);
}
Mat4<float> Renderer::frustumProjection(float left, float right, float top, float bottom, float near, float far)
{
Mat4<float> m(1.0f);
float A = (right + left) / (right - left);
float B = (top + bottom) / (top - bottom);
float C = -((far + near) / (far - near));
float D = -((2 * far * near) / (far - near));
m[0][0] = (2 * near) / (right - left);
m[1][1] = (2 * near) / (top - bottom);
m[2][0] = A;
m[2][1] = B;
m[2][2] = C;
m[2][3] = -1.0f;
m[3][2] = D;
m[3][3] = 0.0f;
return m;
}
Mat4<float> Renderer::perspectiveProjection(float FOV, float aspectRatio, float near, float far)
{
float right = near * tan(Math::toRadians(FOV / 2));
float left = -right;
float top = right / aspectRatio;
float bottom = left / aspectRatio;
return frustumProjection(left, right, top, bottom, near, far);
}
bool Renderer::initSDL()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mf_width, mf_height, SDL_WINDOW_OPENGL);
if(window != NULL)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
context = SDL_GL_CreateContext(window);
if(context != NULL)
{
int major;
int minor;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
printf("Opengl context version %d.%d\n", major, minor);
}
}
else
{
STUB("Implement proper error handling");
}
}
else
{
STUB("Implement proper error stuff");
}
}
void Renderer::initGL(std::vector<struct ShaderList> list)
{
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
std::cout << "Max texture units:" << maxTextureUnits << std::endl;
glViewport(0, 0, mf_width, mf_height);
glDepthRange(mf_near, mf_far); /*Tell opengl where the near and far planes sit*/
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
//glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LESS);
program = loadProgram(list);
glUseProgram(program);
glUniformMatrix4fv(projMatLocation, 1, GL_FALSE, &projectionMatrix);
glGenBuffers(NUM_INDEXS, ibo);
glGenVertexArrays(NUM_VAOS, vertArrays);
glGenBuffers(NUM_BUFFERS, buffers);
}
void Renderer::cleanup()
{
if(context != NULL)
{
SDL_GL_DeleteContext(context);
}
if(window != NULL)
{
SDL_DestroyWindow(window);
}
SDL_Quit();
}
void Renderer::setWindowTitle(std::string title)
{
std::string newTitle = this->title + " FPS:" + title;
SDL_SetWindowTitle(window, newTitle.c_str());
}
GLint Renderer::loadProgram(std::vector<struct ShaderList> list)
{
GLint program = Shader::LoadShaders(list);
projMatLocation = glGetUniformLocation(program, "vprojectionMat");
if(projMatLocation == -1)
{
std::cout << "Couldn't find vprojectionMat in shader" << std::endl;
}
modelMatLocation = glGetUniformLocation(program, "modelMatrix");
if(modelMatLocation == -1)
{
std::cout << "Couldn't find modelMatrix in shader" << std::endl;
}
cameraMatLocation = glGetUniformLocation(program, "cameraMatrix");
if(cameraMatLocation == -1)
{
std::cout << "Couldn't find cameraMatrix in shader" << std::endl;
}
camPositionLocation = glGetUniformLocation(program, "camPosition");
if(camPositionLocation == -1)
{
std::cout << "Couldn't find camPosition in shader" << std::endl;
}
/*lightsBlockLocation = glGetUniformBlockIndex(program, "lights");
if(lightsBlockLocation == -1)
{
std::cout << "Failed to find lights in shader" << std::endl;
}
enableDiffuseLocation = glGetUniformLocation(program, "enableDiffuse");
if(enableDiffuseLocation== -1)
{
std::cout << "Failed to enableDiffuse in shader" << std::endl;
}
enableSpecularLocation = glGetUniformLocation(program, "enableSpecular");
if(enableSpecularLocation == -1)
{
std::cout << "Failed to find enableSpecular in shader" << std::endl;
}
specularModeLocation = glGetUniformLocation(program, "specularMode");
if(specularModeLocation == -1)
{
std::cout << "Failed to find specularMode in shader" << std::endl;
}*/
textureLocation = glGetUniformLocation(program, "tex");
if(textureLocation == -1)
{
std::cout << "Failed to find tex in shader" << std::endl;
}
normalMapLocation = glGetUniformLocation(program, "normalMap");
if(normalMapLocation == -1)
{
std::cout << "Failed to find normalMap in shader" << std::endl;
}
/*GLint activeUniforms;
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &activeUniforms);
std::cout << "Active uniform count:" << activeUniforms << std::endl;
for(int i = 0; i < activeUniforms; i++)
{
char buffer[100];
GLsizei length;
glGetActiveUniformName(program, i, 100, &length, buffer);
std::cout << std::string(buffer) << std::endl;
}*/
uboIndex = glGetUniformBlockIndex(program, "LightsBlock");
if(uboIndex == GL_INVALID_INDEX)
{
std::cout << "invalid index" << std::endl;
}
glGetActiveUniformBlockiv(program, uboIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uboSize);
uboStride = uboSize / MAX_LIGHTS;
//glGetUniformIndices(program, 2, test, uniformIndices);
glGetUniformIndices(program, numUniforms, shaderUniforms, uniformIndices);
glGetActiveUniformsiv(program, numUniforms, uniformIndices, GL_UNIFORM_OFFSET, uniformOffsets);
glGetActiveUniformsiv(program, numUniforms, uniformIndices, GL_UNIFORM_SIZE, uniformSizes);
glGetActiveUniformsiv(program, numUniforms, uniformIndices, GL_UNIFORM_TYPE, uniformType);
GLint strides[numUniforms];
glGetActiveUniformsiv(program, numUniforms, uniformIndices, GL_UNIFORM_ARRAY_STRIDE, strides);
//memcpy()
glGenBuffers(1, &ubo);
glBindBuffer(GL_UNIFORM_BUFFER, ubo);
glBufferData(GL_UNIFORM_BUFFER, uboSize, NULL, GL_STATIC_DRAW); //We'll init the buffer later?
numEnabledLightsLocation = glGetUniformLocation(program, "numEnabledLights");
if(numEnabledLightsLocation == -1)
{
std::cout << "Failed to find numEnabledLights in shader" << std::endl;
}
return program;
}
void Renderer::useProgram(GLint program)
{
glUseProgram(program);
}
void Renderer::setProjectionMatrix(Mat4<float> projMat)
{
projectionMatrix = projMat;
}
void Renderer::updateCameraMatrix(Mat4<float> camMat)
{
cameraMatrix = camMat;
}
void Renderer::updateModelMatrix(Mat4<float> modMat)
{
modelMatrix = modMat;
}
void Renderer::updateCameraPosition(Vec4<float> camDir)
{
camPosition = camDir;
}
void Renderer::updateLight(Light updatedLight)
{
glBindBuffer(GL_UNIFORM_BUFFER, ubo);
GLintptr offset = updatedLight.getIndex() * uboStride;
GLsizei size = updatedLight.getDataSize();
glBufferSubData(GL_UNIFORM_BUFFER, offset, updatedLight.getDataSize(), updatedLight.getLightData());
glBindBufferBase(GL_UNIFORM_BUFFER, uboIndex, ubo);
}
void Renderer::updateLights(std::vector<Light> lights)
{
glBindBuffer(GL_UNIFORM_BUFFER, ubo);
for( std::vector<Light>::iterator it = lights.begin(); it != lights.end(); ++it)
{
Light light = *it;
GLintptr offset = light.getIndex() * uboStride;
GLsizei size = light.getDataSize();
char *data = light.getLightData();
glBufferSubData(GL_UNIFORM_BUFFER, offset, size, data);// light.getLightData());
glBindBufferBase(GL_UNIFORM_BUFFER, uboIndex, ubo);
}
}
void Renderer::draw()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glUniform1i(textureLocation, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glUniform1i(normalMapLocation, 1);
glUniformMatrix4fv(modelMatLocation, 1, false, &modelMatrix);
glUniformMatrix4fv(cameraMatLocation, 1, false, &cameraMatrix);
glUniform3f(camPositionLocation, camPosition[x], camPosition[y], camPosition[z]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(vertArrays[TRIANGLES]);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo[INDEXS]);
glDrawArrays(GL_TRIANGLES, 0, triangleCount);
SDL_GL_SwapWindow(window);
}
void Renderer::loadPrimitiveData(float *vertices, size_t vsize, unsigned short *indices, size_t icount, float *colours, size_t csize, size_t tsize, float *texCoords, float *normals, size_t nsize)
{
triangleCount = vsize / sizeof(*vertices) / 3;
glBindVertexArray(vertArrays[TRIANGLES]);
size_t bufSize = vsize + csize + tsize + nsize;
if(indices)
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo[INDEXS]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount, indices, GL_STATIC_DRAW);
}
glBindBuffer(GL_ARRAY_BUFFER, buffers[ARRAY_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, bufSize, NULL, GL_STATIC_DRAW);
size_t total = 0.0f;
if(vertices)
{
glBufferSubData(GL_ARRAY_BUFFER, total, vsize, vertices);
vPosition = glGetAttribLocation(program, "vPosition");
if(vPosition == -1)
{
std::cout << "Couldn't find vPosition in shader" << std::endl;
}
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)total);
glEnableVertexAttribArray(vPosition);
total += vsize;
}
if(colours)
{
glBufferSubData(GL_ARRAY_BUFFER, total, csize, colours);
vColour = glGetAttribLocation(program, "vColour");
if(vColour == -1)
{
std::cout << "Couldn't find Colour in shader" << std::endl;
}
glVertexAttribPointer(vColour, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)total);
glEnableVertexAttribArray(vColour);
total += csize;
}
if(tsize)
{
glBufferSubData(GL_ARRAY_BUFFER, total, tsize, texCoords);
GLint texAttrib = glGetAttribLocation(program, "vTexCoord");
if(texAttrib == -1)
{
std::cout << "Couldn't find tCoord in shader" << std::endl;
}
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)total);
glEnableVertexAttribArray(texAttrib);
total += tsize;
}
if(normals)
{
glBufferSubData(GL_ARRAY_BUFFER, total, nsize, normals);
vNormal = glGetAttribLocation(program, "vNormal");
if(vNormal == -1)
{
std::cout << "Failed to find vNormal in shader" << std::endl;
}
glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)(total));
glEnableVertexAttribArray(vNormal);
total += nsize;
}
}
void Renderer::loadTexture(char *name)
{
SDL_Surface *image = IMG_Load(name);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
textures.push_back(texture);
}
void Renderer::loadTest()
{
unsigned short indexs[] = {0, 1, 2, //Front face
2, 3, 0};
float coords[] = {0, 0, 1, 0, 1, 1, 0, 1};
float vertices[] = {-5.0f, -5.0f, 0.0f,
5.0f, -5.0f, 0.0f,
5.0f, 5.0f, 0.0f,
-5.0f, 5.0f, 0.0f};
glBindVertexArray(vertArrays[TRIANGLES]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo[INDEXS]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexs), indexs, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, buffers[ARRAY_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(coords), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(coords), coords);
GLint vPos = glGetAttribLocation(program, "vPosition");
glVertexAttribPointer(vPos, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
glEnableVertexAttribArray(vPos);
GLint tCoord = glGetAttribLocation(program, "tCoord");
glVertexAttribPointer(tCoord, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)sizeof(vertices));
glEnableVertexAttribArray(tCoord);
GLuint texture;
glGenTextures(1, &texture);
SDL_Surface *crate = IMG_Load("basic2.png");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, crate->w, crate->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, crate->pixels);
glUniform1i(glGetUniformLocation(program, "tex"), 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void Renderer::setTriangleCount(float triangles)
{
triangleCount = triangles;
}
void Renderer::setNumEnabledLights(int count)
{
glUniform1i(numEnabledLightsLocation, count);
}
Light Renderer::newLight(int index)
{
int stride = (uboSize / MAX_LIGHTS) * index;
int numElements = numUniforms / MAX_LIGHTS;
int uniformArrayOffset = numElements * index; //This is how much we need to offset into the uniform arrays to get the correct data. i.e offset into uniformIndices Array etc
int offset = uboSize / MAX_LIGHTS;
return Light(index, stride, offset, numElements, shaderUniforms + uniformArrayOffset, uniformIndices + uniformArrayOffset, uniformSizes + uniformArrayOffset, uniformOffsets + uniformArrayOffset, uniformType + uniformArrayOffset);
}
const int Renderer::getMaxLights()
{
return MAX_LIGHTS;
}