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
|
#include "Renderer.hpp"
#include "GameObject.hpp"
#include <RapidJsonHelper.hpp>
#include <rapidjson/document.h>
#include <cassert>
#include <glm/gtc/matrix_transform.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#include <string_view>
using namespace std::literals;
RenderObject::RenderObject()
: mVao{ 0 } {
}
RenderObject::~RenderObject() {
DeleteGLObjects();
}
GLuint RenderObject::GetGLVao() const {
return mVao;
}
void RenderObject::RebuildIfNecessary() {
if (mVao != 0) {
return;
}
assert(mIndexBuf != nullptr);
assert(mVertexFormat != nullptr);
glGenVertexArrays(1, &mVao);
glBindVertexArray(mVao);
auto& vBindings = mVertexBufBinding.bindings;
auto& shaderInfo = mMaterial->GetShader()->GetInfo();
// Setup vertex buffers
for (auto& elm : mVertexFormat->elements) {
assert(elm.bindingIndex < vBindings.size());
auto& buffer = vBindings[elm.bindingIndex];
int index = shaderInfo.FindInputLocation(elm.semantic);
if (index == -1) {
continue;
}
glBindBuffer(GL_ARRAY_BUFFER, buffer->handle);
glEnableVertexAttribArray(index);
glVertexAttribPointer(
index,
Tags::VectorLenOf(elm.type),
Tags::FindGLType(elm.type),
Tags::IsNormalized(elm.type),
mVertexFormat->vertexSize,
(void*)(uintptr_t)elm.offset);
}
// Setup index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuf->handle);
glBindVertexArray(0);
}
void RenderObject::SetMaterial(Material* material) {
mMaterial.Attach(material);
DeleteGLObjects();
}
void RenderObject::UpdateIndexBuffer(GpuIndexBuffer* indexBuffer) {
mIndexBuf.Attach(indexBuffer);
DeleteGLObjects();
}
void RenderObject::UpdateVertexFormat(VertexFormat* vertexFormat) {
mVertexFormat.Attach(vertexFormat);
DeleteGLObjects();
}
void RenderObject::UpdateVertexBufferBindings(BufferBindings** bindingsOut) {
*bindingsOut = &mVertexBufBinding;
DeleteGLObjects();
}
void RenderObject::SetFormat(VertexFormat* vertexFormat, Tags::IndexType indexFormat) {
mIndexBuf.Attach(new GpuIndexBuffer());
mIndexBuf->indexType = indexFormat;
mIndexBuf->count = 0;
mVertexFormat.Attach(vertexFormat);
mVertexBufBinding.Clear();
for (auto& element : vertexFormat->elements) {
if (mVertexBufBinding.GetBinding(element.bindingIndex) == nullptr) {
mVertexBufBinding.SetBinding(element.bindingIndex, new GpuVertexBuffer());
}
}
}
void RenderObject::DeleteGLObjects() {
if (mVao != 0) {
glDeleteVertexArrays(1, &mVao);
mVao = 0;
}
}
Renderer::Renderer()
: binding_WireframeMaterial{ gDefaultMaterial } //
{
mRenderOptions[RO_Shading] = true;
mRenderOptions[RO_Wireframe] = false;
}
void Renderer::LoadBindings(const rapidjson::Value& bindings) {
if (auto rvWireframe = rapidjson::GetProperty(bindings, "WireframeMaterial"sv)) {
Uid uidWireframe;
uidWireframe.Read(*rvWireframe);
// TODO don't assume
binding_WireframeMaterial.Attach(((IresMaterial*)IresManager::instance->FindIres(uidWireframe))->GetInstance());
}
}
void Renderer::SaveBindings(rapidjson::Value& into, rapidjson::Document& root) const {
if (auto ires = binding_WireframeMaterial->GetIres()) {
into.AddMember("WireframeMaterial", ires->GetUid().Write(root), root.GetAllocator());
}
}
void Renderer::BeginFrame(Camera& camera, float currentTime, float deltaTime) {
assert(mInsideFrame == false);
mInsideFrame = true;
mFrame.camera = &camera;
mFrame.matrixView = camera.CalcViewMatrix();
mFrame.matrixProj = camera.CalcProjectionMatrix();
mFrame.time = currentTime;
mFrame.deltaTime = deltaTime;
}
void Renderer::EndFrame() {
assert(mInsideFrame == true);
mInsideFrame = false;
}
void Renderer::Draw(const RenderObject* objects, const GameObject* gameObject, size_t count) {
using namespace Tags;
assert(mInsideFrame);
// Desired order: proj * view * (translate * rotate * scale) * vec
// <----- order of application <----- ^^^ input
glm::mat4 objectMatrix(1.0f);
objectMatrix = glm::translate(objectMatrix, gameObject->GetPos());
objectMatrix *= glm::toMat4(gameObject->GetRotation());
objectMatrix = glm::scale(objectMatrix, gameObject->GetScale());
auto mvpMatrix = mFrame.matrixProj * mFrame.matrixView * objectMatrix;
if (GetRenderOption(RO_Shading)) {
// TODO shader grouping
// TODO material grouping
for (size_t i = 0; i < count; ++i) {
auto& object = objects[i];
auto indexBuffer = object.GetIndexBuffer();
auto mat = object.GetMaterial();
auto shader = mat->GetShader();
glUseProgram(shader->GetProgram());
// Material uniforms
mat->UseUniforms();
// Next available texture unit ID after all material textures
int texIdx = mat->GetTextures().size();
// Autofill uniforms
if (shader->autofill_Transform != kInvalidLocation) {
glUniformMatrix4fv(shader->autofill_Transform, 1, GL_FALSE, &mvpMatrix[0][0]);
}
if (shader->autofill_Time != kInvalidLocation) {
glUniform1f(shader->autofill_Time, mFrame.time);
}
if (shader->autofill_DeltaTime != kInvalidLocation) {
glUniform1f(shader->autofill_DeltaTime, mFrame.deltaTime);
}
if (shader->autofill_TextureAtlas != kInvalidLocation &&
object.autofill_TextureAtlas != nullptr)
{
glActiveTexture(GL_TEXTURE0 + texIdx);
glBindTexture(GL_TEXTURE_2D, object.autofill_TextureAtlas->GetHandle());
glUniform1i(shader->autofill_TextureAtlas, texIdx);
++texIdx;
}
glBindVertexArray(object.GetGLVao());
glDrawElements(GL_TRIANGLES, indexBuffer->count, indexBuffer->GetIndexTypeGL(), 0);
}
}
if (GetRenderOption(RO_Wireframe)) {
auto& mat = *binding_WireframeMaterial;
auto& shader = *mat.GetShader();
auto& shaderInfo = shader.GetInfo();
glUseProgram(shader.GetProgram());
mat.UseUniforms();
// TODO reduce calls with consecutive wireframe setting
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
for (size_t i = 0; i < count; ++i) {
auto& object = objects[i];
auto& vBindings = object.GetVertexBufferBindings().bindings;
auto vf = object.GetVertexFormat();
// Setup vertex buffers
for (auto& elm : vf->elements) {
assert(elm.bindingIndex < vBindings.size());
auto& buffer = vBindings[elm.bindingIndex];
int index = shaderInfo.FindInputLocation(elm.semantic);
if (index == -1) {
continue;
}
glBindBuffer(GL_ARRAY_BUFFER, buffer->handle);
glEnableVertexAttribArray(index);
glVertexAttribPointer(
index,
Tags::VectorLenOf(elm.type),
Tags::FindGLType(elm.type),
Tags::IsNormalized(elm.type),
vf->vertexSize,
(void*)(uintptr_t)elm.offset);
}
// Setup index buffer
auto indexBuffer = object.GetIndexBuffer();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer->handle);
glDrawElements(GL_TRIANGLES, indexBuffer->count, indexBuffer->GetIndexTypeGL(), 0);
}
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
return;
}
}
bool Renderer::GetRenderOption(RenderOption option) const {
return mRenderOptions[option];
}
void Renderer::SetRenderOption(RenderOption option, bool flag) {
mRenderOptions[option] = flag;
}
|