-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityRenderer.cpp
209 lines (142 loc) · 6.78 KB
/
EntityRenderer.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
#include "math.hpp"
#include "staticShader.h"
#include "EntityRenderer.h"
#include <iostream>
#include "window.h"
#include <math.h>
// #include "model.hpp"
EntityRenderer::EntityRenderer() {
}
EntityRenderer::EntityRenderer(Window window, StaticShader staticShader, glm::mat4 projectionMatrix){
this->staticShader = staticShader;
this->staticShader.start();
staticShader.setMat4("projectionMatrix", projectionMatrix); // Load Projection Matrix on uniform in Vertex Shader
this->staticShader.stop();
// this->staticShader = staticShader;
//
//
//// // Render Optimization
//// //---------------------------------------------------------
//// glEnable(GL_CULL_FACE); // Ne renderuje deo objekast koji nije okrenut Kameri
//// glCullFace(GL_BACK);
//// //---------------------------------------------------------
//
// // Create Projection Matrix only once in our Application
// //glm::mat4 projectionMatrix = createProjectionMatrix(window);
//
// staticShader.start();
// staticShader.setMat4("projectionMatrix", projectionMatrix); // Load Projection Matrix on uniform in Vertex Shader
// staticShader.stop();
}
//void EntityRenderer::prepare() {
//
// glEnable(GL_DEPTH_TEST); // Enable Depth Testing
// glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//}
void EntityRenderer::render(std::vector<Entity> entities) {
// // Retrive All Keys from HashMap - entities
// std::vector<TexturedModel> keys; // All Keys
// for(std::map<TexturedModel, std::vector<Entity>>::iterator it = entities.begin(); it != entities.end(); ++it) {
// keys.push_back(it->first);
// }
// //-------------------------------------------------------------------
// Loop All TexturedModels - Keys
int i;
for(i = 0; i < entities.size(); i++) {
TexturedModel texturedModel2 = entities.at(i).texturedModel;
Entity entity = entities.at(i);
EntityRenderer::prepareTexturedModel(texturedModel2);
//--------------------------------------------------------------
// Get All Entities that use that TexturedModel
// std::vector<Entity> batch; // Store All Entities for TexturedModel
// for(auto itr = entities.find(texturedModel2); itr != entities.end(); itr++) { // Search Values for Given Key - Key: TexturedModel, Value: vector<Entitiy> entities
//
// batch.assign(itr->second.begin(), itr->second.end()); // Add All Entities From TexturedModel to Batch
//
// }
//-------------------------------------------------------------
int j;
// for(j = 0; j < batch.size(); j++) {
EntityRenderer::prepareInstance(entity);
glDrawArrays(GL_TRIANGLES, 0, entity.texturedModel.rawModel.vertexCount);
//}
EntityRenderer::unbindTexturedModel();
}
}
// Enable OpenGL Texture Setting for TexturedModel
void EntityRenderer::prepareTexturedModel(TexturedModel texturedModel) {
RawModel rawModel = texturedModel.rawModel;
glBindVertexArray(rawModel.vaoID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// Fake Lighting
ModelTexture texture = texturedModel.texture;
if(texture.hasTransparency == true) {
glDisable(GL_CULL_FACE);
}
this->staticShader.loadFakeLightingVariable(texture.useFakeLighting);
//---------------------------------------------------------------------------------
// Specular Light Setting
this->staticShader.loadShineVariables(texture.shineDamper, texture.reflectivity);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texturedModel.texture.textureID);
//---------------------------------------------------------------------------------
}
// Settup Tranformation Matrix
void EntityRenderer::prepareInstance(Entity entity) {
Math math;
glm::mat4 transformationMatrix = math.createTransformationMatrix(entity.position, entity.rotX, entity.rotY, entity.rotZ, entity.scale); // Create TransformationMatrix - Model Matrix
//staticShader.loadTransformationMatrix(transformationMatrix); // Load Model Matrix to its Uniform on Vertex Shader
staticShader.setMat4("transformationMatrix", transformationMatrix); // Load Model Matrix to its Uniform on Vertex Shader
}
// Unbind OpenGL Setting for Current Active Texture
void EntityRenderer::unbindTexturedModel() {
glEnable(GL_CULL_FACE); // Ne renderuje deo objekast koji nije okrenut Kameri
glCullFace(GL_BACK);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);
}
void EntityRenderer::render(Window window, Entity entity, StaticShader staticShader) {
TexturedModel model = entity.texturedModel;
// Specular Light
ModelTexture texture = entity.texturedModel.texture;
staticShader.loadShineVariables(texture.shineDamper, texture.reflectivity); // Load Specular Data of Object to Fragment Shade
//---------------------------------------------------------------------
glBindVertexArray(model.rawModel.vaoID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// Load Tranfromations to Vertex Shader
Math math;
glm::mat4 transformationMatrix = math.createTransformationMatrix(entity.position, entity.rotX, entity.rotY, entity.rotZ, entity.scale); // Create TransformationMatrix - Model Matrix
//staticShader.loadTransformationMatrix(transformationMatrix); // Load Model Matrix to its Uniform on Vertex Shader
staticShader.setMat4("transformationMatrix", transformationMatrix); // Load Model Matrix to its Uniform on Vertex Shader
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model.texture.textureID);
// std::cout << entity.texturedModel.rawModel.vertexCount << std::endl;
glDrawElements(GL_TRIANGLES, entity.texturedModel.rawModel.vertexCount, GL_UNSIGNED_INT, 0);
// glDrawArrays(GL_TRIANGLES, 0, entity.texturedModel.rawModel.vertexCount);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);
}
//glm::mat4 EntityRenderer::createProjectionMatrix(Window window) {
//
//
// glm::mat4 projectionMatrix;
//
// float aspectRatio = (float) window.WIDTH / (float) window.HEIGHT;
// float scaleY = (1.0f / std::tan(glm::radians(FOV / 2.0f))) * aspectRatio;
// float scaleX = scaleY / aspectRatio;
// float frustumLength = this->FAR_PLANE - this->NEAR_PLANE;
//
// projectionMatrix = glm::perspective(glm::radians(FOV), aspectRatio, this->NEAR_PLANE, this->FAR_PLANE);
//
// return projectionMatrix;
//
//}