-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelRenderComponent.cpp
More file actions
97 lines (73 loc) · 3.62 KB
/
Copy pathModelRenderComponent.cpp
File metadata and controls
97 lines (73 loc) · 3.62 KB
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
#include "ModelRenderComponent.h"
#include "ModelManager.h"
#include "Game.h"
#include "ShaderManager.h"
#include "Visitor.h"
void ModelRenderComponent::init(const char* filepath, ShaderProgram* defaultProgram) {
init(ModelManager::instance()->getModel(filepath));
if(defaultProgram != nullptr && getModel()->getMeshes() != nullptr) {
getModel()->getMeshes()[0].getMaterial()->setShaderProgram(defaultProgram);
}
}
void ModelRenderComponent::init(Model* model) {
m_xModel = model;
}
void ModelRenderComponent::accept(Visitor* visitor) {
visitor->visitModelRenderComponent(this);
}
void ModelRenderComponent::render() {
Mesh* meshes;
ShaderProgram* shader = getModel()->getMeshes()[0].getMaterial()->getShaderProgram();
glUseProgram(shader->glID());
if (m_xModel == nullptr || (meshes = m_xModel->getMeshes()) == nullptr)
return;
/* Setting of the uniforms is highly un-optimized.
Renderings should be sorted per program and use on glUseProgram per program.
Operations marked with 'c' are constant and should only be set once per glUseProgram.
Edit: changing rendering order to sort by shader would mess with drawing transparent materials.
*/
/* c */shader->uniformMatrix4fv("projection", 1, GL_FALSE, (GLfloat*) &getGameObject()->getScene()->getCamera()->getProjectionMatrix() );
/* c */shader->uniformMatrix4fv("view", 1, GL_FALSE, (GLfloat*) &getGameObject()->getScene()->getCamera()->getViewMatrix() );
/* c */shader->uniform3fv("ambient_light", 1, (GLfloat*) &getGameObject()->getScene()->getAmbientLight() );
/* c */Game::instance()->getRenderer()->updateLights(LT_POINT, getGameObject()->getScene());
/* c */Game::instance()->getRenderer()->updateLights(LT_DIRECTIONAL, getGameObject()->getScene());
/* c */shader->uniform1ui("point_light_count", Game::instance()->getRenderer()->getLightCount(LT_POINT));
/* c */shader->uniform1ui("dir_light_count", Game::instance()->getRenderer()->getLightCount(LT_DIRECTIONAL));
shader->uniformMatrix4fv("model", 1, GL_FALSE, (GLfloat*) &getGameObject()->getMatrix());
if((shader->shaderFlags() & SHADER_ALPHA_BLENDING_ADDITIVE)) {
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glBlendEquation(GL_MAX);
} else {
glDisable(GL_BLEND);
}
for (unsigned int i = 0; i < m_xModel->numMeshes(); ++i) {
glBindBuffer(GL_ARRAY_BUFFER, meshes[i].getVertexBufferID());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshes[i].getIndexBufferID());
Material* material = meshes[i].getMaterial();
if (material->numTextures(MATERIAL_DIFFUSE) > 0) {
glActiveTexture(TEXSLOT_DIFFUSE);
glBindTexture(GL_TEXTURE_2D, material->getTexture(material->getIdsOfType(MATERIAL_DIFFUSE)[0])->getTexID());
}
if (material->numTextures(MATERIAL_NORMALMAP) > 0) {
glActiveTexture(TEXSLOT_NORMAL);
glBindTexture(GL_TEXTURE_2D, material->getTexture(material->getIdsOfType(MATERIAL_NORMALMAP)[0])->getTexID());
}
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
//Vertex positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), 0);
//Vertex normals
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (const GLvoid*)sizeof(glm::vec3));
//Vertex texture coordinates
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (const GLvoid*)(sizeof(glm::vec3) * 2));
//Vertex tangents
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (const GLvoid*)(sizeof(glm::vec3) * 2 + sizeof(glm::vec2)));
glDrawElements(GL_TRIANGLES, meshes[i].getIndexCount(), GL_UNSIGNED_INT, 0);
}
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
}