-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.cpp
More file actions
112 lines (92 loc) · 4.11 KB
/
batch.cpp
File metadata and controls
112 lines (92 loc) · 4.11 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2017 Pierre Fourgeaud
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "graphics/batch.h"
#include <logger.h>
#include "graphics/camera.h"
#include "graphics/material.h"
#include "graphics/mesh.h"
#include "graphics/rendersystem.h"
#include "graphics/shaderprogram.h"
#include "graphics/technique.h"
namespace CodeHero {
void Batch::Draw(RenderSystem& iRS, const std::shared_ptr<Camera>& iCamera) {
if (m_pMaterial->HasTechnique()) {
auto technique = m_pMaterial->GetTechnique();
iRS.SetBlendMode(technique->IsBlendEnabled(), technique->GetSrcBlendMode(),
technique->GetDstBlendMode());
auto shaderProgram = technique->GetCachedShaderProgram();
if (!shaderProgram) {
auto vtxShader = technique->GetShader(Shader::T_Vertex)
->GetInstance(technique->GetShaderDefines(Shader::T_Vertex));
auto fragShader = technique->GetShader(Shader::T_Fragment)
->GetInstance(technique->GetShaderDefines(Shader::T_Fragment));
// TODO(pierre) Could be also cached in the RS to avoid recompilation ?
shaderProgram = iRS.CreateShaderProgram();
shaderProgram->Attach(vtxShader).Attach(fragShader).Link();
technique->SetCachedShaderProgram(shaderProgram);
}
shaderProgram->Use();
if (m_pMaterial->HasTexture(TU_Diffuse)) {
m_pMaterial->GetTexture(TU_Diffuse)->Bind(0);
iRS.SetShaderParameter("material.diffuse", 0);
}
if (m_pMaterial->HasTexture(TU_Specular)) {
m_pMaterial->GetTexture(TU_Specular)->Bind(1);
iRS.SetShaderParameter("material.specular", 1);
}
if (m_pMaterial->HasTexture(TU_Opacity)) {
m_pMaterial->GetTexture(TU_Opacity)->Bind(2);
iRS.SetShaderParameter("material.opacity", 2);
}
// TODO(pierre) Shininess should be a variable parameter, not a constant.
iRS.SetShaderParameter("material.shininess", 32.0f);
iRS.SetShaderParameter("material.textureCoordsOffset",
m_pMaterial->GetTextureCoordsOffset());
iRS.SetShaderParameter("material.diffuseColor", m_pMaterial->GetDiffuseColor());
} else {
// TODO(pierre) Provide default technique if none was set (or set a default one
// automatically)
LOGW << "[Batch::Draw]: Your material does not have a technique, it is very "
<< "unlikely that the object will be rendered." << std::endl;
}
// View
iRS.SetShaderParameter("view", iCamera->GetView());
iRS.SetShaderParameter("projection", iCamera->GetProjection());
// Lights
if (m_pVertexDirLights && !m_pVertexDirLights->empty()) {
iRS.SetShaderParameter("dirLights", m_pVertexDirLights->data(), m_pVertexDirLights->size());
}
if (m_pVertexPointLights && !m_pVertexPointLights->empty()) {
iRS.SetShaderParameter("pointLights", m_pVertexPointLights->data(),
m_pVertexPointLights->size());
}
// Model
iRS.SetShaderParameter("model", m_WorldTransform);
// Params
iRS.SetCullMode(m_pMaterial->GetCullEnabled());
// TODO(pierre) For now both are enabled at the same time,
// in the future we might want to differentiate them.
iRS.SetDepthTest(m_pMaterial->GetDepthTest());
iRS.SetDepthMask(m_pMaterial->GetDepthTest());
m_pMesh->Draw(iRS);
}
void Batch::SetMaterial(const std::shared_ptr<Material>& iMaterial) {
m_pMaterial = iMaterial;
}
void Batch::SetMesh(const std::shared_ptr<Mesh>& iMesh) {
m_pMesh = iMesh;
}
void Batch::SetWorldTransform(const Matrix4& iWorldTransform) {
m_WorldTransform = iWorldTransform;
}
void Batch::SetDistanceFromCamera(const float& iDistanceFromCamera) {
m_DistanceFromCamera = iDistanceFromCamera;
}
void Batch::SetVertexDirLights(std::vector<float>* iVertex) {
m_pVertexDirLights = iVertex;
}
void Batch::SetVertexPointLights(std::vector<float>* iVertex) {
m_pVertexPointLights = iVertex;
}
} // namespace CodeHero