aboutsummaryrefslogtreecommitdiff
path: root/source/Renderer.hpp
blob: 7d96ce2276fee70d3db00fc13f9f9cbb15cb79a0 (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
#pragma once

#include "Camera.hpp"
#include "Material.hpp"
#include "RcPtr.hpp"
#include "VertexIndex.hpp"

#include <glad/glad.h>
#include <rapidjson/fwd.h>
#include <cstddef>
#include <glm/glm.hpp>

// TODO add optional support for OpenGL separate attrib binding & only depend on vertex format

class GameObject;

class RenderObject {
public:
	RcPtr<Texture> autofill_TextureAtlas;

private:
	RcPtr<Material> mMaterial;
	RcPtr<GpuIndexBuffer> mIndexBuf;
	RcPtr<VertexFormat> mVertexFormat;
	BufferBindings mVertexBufBinding;
	GLuint mVao;

public:
	RenderObject();
	~RenderObject();

	GLuint GetGLVao() const;
	void RebuildIfNecessary();

	Material* GetMaterial() const { return mMaterial.Get(); }
	void SetMaterial(Material* material);

	GpuIndexBuffer* GetIndexBuffer() const { return mIndexBuf.Get(); }
	const VertexFormat* GetVertexFormat() const { return mVertexFormat.Get(); }
	const BufferBindings& GetVertexBufferBindings() const { return mVertexBufBinding; }
	void UpdateIndexBuffer(GpuIndexBuffer* indexBuffer);
	void UpdateVertexFormat(VertexFormat* vertexFormat);
	// Assumes the fetched BufferBinding object is modified
	void UpdateVertexBufferBindings(BufferBindings** bindingsOut);
	void SetFormat(VertexFormat* vertexFormat, Tags::IndexType indexFormat);

private:
	void DeleteGLObjects();
};

struct RendererFrameInfo {
	Camera* camera;
	glm::mat4 matrixView;
	glm::mat4 matrixProj;
	float time;
	float deltaTime;
};

class Renderer {
public:
	// NOTE: see Renderer constructor for default values
	enum RenderOption {
		/// Render everything directly using objects' provided material and vertex/index data.
		RO_Shading,
		/// Render everything as wireframes using provided position data.
		RO_Wireframe,
		RO_COUNT,
	};

public:
	RcPtr<Material> binding_WireframeMaterial;

private:
	RendererFrameInfo mFrame;
	bool mInsideFrame = false;
	bool mRenderOptions[RO_COUNT] = {};

public:
	Renderer();

	void LoadBindings(const rapidjson::Value& bindings);
	void SaveBindings(rapidjson::Value& into, rapidjson::Document& root)const;

	void BeginFrame(Camera& camera, float currentTime, float deltaTime);
	const RendererFrameInfo& GetLastFrameInfo() const { return mFrame; }
	void Draw(const RenderObject* objects, const GameObject* gameObject, size_t count);
	void EndFrame();

	bool GetRenderOption(RenderOption option) const;
	void SetRenderOption(RenderOption option, bool flag);
};