aboutsummaryrefslogtreecommitdiff
path: root/source/30-game/Renderer.cpp
blob: 3497449bed23315f4781c8440875962c69215459 (plain)
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
#include "Renderer.hpp"

#include "GameObject.hpp"

#include <cassert>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>

RenderObject::RenderObject()
	: mVao{ GL_NONE } {
}

RenderObject::~RenderObject() {
	DeleteGLObjects();
}

GLuint RenderObject::GetGLVao() const {
	return mVao;
}

void RenderObject::RebuildIfNecessary() {
	if (mVao != GL_NONE) {
		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(GL_NONE);
}

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 != GL_NONE) {
		glDeleteVertexArrays(1, &mVao);
		mVao = GL_NONE;
	}
}

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);

	auto vpMatrix = mFrame.matrixProj * mFrame.matrixView;

	// 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) {
			glm::mat4 objectMatrix(1.0f);
			objectMatrix = glm::translate(objectMatrix, gameObject->GetPos());
			objectMatrix *= glm::toMat4(gameObject->GetRotation());
			objectMatrix = glm::scale(objectMatrix, gameObject->GetScale());
			glm::mat4 transform = vpMatrix * objectMatrix;

			glUniformMatrix4fv(shader->autofill_Transform, 1, GL_FALSE, &transform[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);
	}
}