-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpbr.cpp
More file actions
110 lines (94 loc) · 4.51 KB
/
pbr.cpp
File metadata and controls
110 lines (94 loc) · 4.51 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
#include "pbr.h"
std::list<ReflectionProbeComponent3*> Internal::Storage::PBR::activeProbes;
PBRMaterial3::PBRMaterial3(const Texture2D& albedo, const Texture2D& normals, const Texture2D& materialData, bool pooledInstanced) :
Material3("BasePBR"),
albedo(albedo),
normals(normals),
materialData(materialData),
pooledInstanced(pooledInstanced)
{ }
ReflectionProbeComponent3::ReflectionProbeComponent3() :
environmentMap(Cubemap::Descriptor(Internal::Storage::Rendering::ENVIRONMENT_MAP_RESOLUTION, Internal::Storage::Rendering::ENVIRONMENT_MAP_RESOLUTION, GL_RGB16F, true, false)),
irradianceMap(Cubemap::Descriptor(Internal::Storage::Rendering::IRRADIANCE_MAP_RESOLUTION, Internal::Storage::Rendering::IRRADIANCE_MAP_RESOLUTION, GL_RGB16F, true, false)),
prefilteredEnvironmentMap(Cubemap::Descriptor(Internal::Storage::Rendering::PREFILTERED_ENVIRONMENT_MAP_RESOLUTION, Internal::Storage::Rendering::PREFILTERED_ENVIRONMENT_MAP_RESOLUTION, GL_RGB16F, true, true))
{ }
ReflectionProbeComponent3::ReflectionProbeComponent3(const ReflectionProbeComponent3& other) :
environmentMap(other.environmentMap),
irradianceMap(other.irradianceMap)
{ }
void ReflectionProbeComponent3::OnEnable()
{
Internal::Storage::PBR::activeProbes.push_back(this);
}
void ReflectionProbeComponent3::OnDisable() {
Internal::Storage::PBR::activeProbes.remove(this);
}
void ReflectionProbeComponent3::Refresh() {
Scene::ConductProbe(gameObject->transform, environmentMap);
Scene::ComputeIrradiance(environmentMap, irradianceMap);
Scene::ComputePrefilteredEnvironmentMap(environmentMap, prefilteredEnvironmentMap);
}
PBROpaqueProcess3::PBROpaqueProcess3() :
shader("Internal/Shaders/pbr")
{ }
void PBROpaqueProcess3::Run(const RMatrix4x4& projectionView, const Camera3* camera) {
shader.Bind();
if (Internal::Storage::RenderingObj::directionalLight != nullptr) {
Shader::LoadVector3(0, Internal::Storage::RenderingObj::directionalLight->gameObject->transform.GetWorldForward());
Shader::LoadVector3(1, &Internal::Storage::RenderingObj::directionalLight->color.r);
} else {
Shader::LoadVector3(0, FVector3(0.0F, -1.0F, 0.0F));
Shader::LoadVector3(1, FVector3(0.0F, 0.0F, 0.0F));
}
Internal::Storage::Rendering::brdfLUT.Bind(0);
if (!Internal::Storage::PBR::activeProbes.empty()) {
Internal::Storage::PBR::activeProbes.front()->prefilteredEnvironmentMap.Bind(1);
Internal::Storage::PBR::activeProbes.front()->irradianceMap.Bind(2);
} else {
Internal::Storage::Rendering::defaultCubemap.Bind(1);
Internal::Storage::Rendering::defaultCubemap.Bind(2);
}
struct AttributeData {
alignas(16) FMatrix4x4 p_v_t;
alignas(16) FVector3 rotation0;
alignas(16) FVector3 rotation1;
alignas(16) FVector3 rotation2;
alignas(16) FVector3 relative_camera_pos;
};
static std::vector<AttributeData> data;
RVector3 camPos = camera->gameObject->transform.GetWorldPosition();
for (auto& hashPair : objects) {
hashPair.first.first.Bind();
hashPair.first.second->LoadMaterial();
data.resize(hashPair.second.objects.size());
for (int i = 0; i < hashPair.second.objects.size(); ++i) {
data[i].p_v_t = static_cast<FMatrix4x4>(projectionView * hashPair.second.objects[i]->GetMatrix());
FMatrix3x3 rotation = hashPair.second.objects[i]->GetWorldRotationMatrix3x3();
data[i].rotation0 = rotation[0];
data[i].rotation1 = rotation[1];
data[i].rotation2 = rotation[2];
data[i].relative_camera_pos = static_cast<FVector3>(camPos - hashPair.second.objects[i]->GetWorldPosition());
}
hashPair.second.RefreshSSBO(&data[0], hashPair.second.objects.size() * sizeof(AttributeData));
hashPair.second.Bind();
glDrawElementsInstanced(GL_TRIANGLES, hashPair.first.first.GetDrawCount(), GL_UNSIGNED_SHORT, 0, hashPair.second.objects.size());
}
}
void PBROpaqueProcess3::AddObject(const RenderComponent3& object) {
objects[std::pair<Mesh, const PBRMaterial3*>(object.GetMesh(), reinterpret_cast<const PBRMaterial3*>(object.GetMaterial()))].objects.push_back(&object.gameObject->transform);
}
void PBROpaqueProcess3::RemoveObject(const RenderComponent3& object) {
auto hashPair = objects.find(std::pair<Mesh, const PBRMaterial3*>(object.GetMesh(), reinterpret_cast<const PBRMaterial3*>(object.GetMaterial())));
RemoveVectorComponent(hashPair->second.objects, &object.gameObject->transform);
if (hashPair->second.objects.empty())
objects.erase(hashPair);
}
namespace Internal {
namespace PBR {
void Init() {
std::vector<ObjectRenderingProcess3*> basePBRProcess;
basePBRProcess.push_back(new PBROpaqueProcess3());
Rendering::RegisterProcess("BasePBR", basePBRProcess);
}
}
}