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

#include "RcPtr.hpp"
#include "Shader.hpp"
#include "Texture.hpp"

#include <glad/glad.h>
#include <cstddef>
#include <cstdint>
#include <glm/glm.hpp>
#include <memory>
#include <span>
#include <string_view>
#include <vector>

// TODO support multiple sizes of vectors and matrices
class Material : public RefCounted {
public:
	struct ScalarUniform {
		union {
			float floatValue;
			int32_t intValue;
			uint32_t uintValue;
		};
		GLenum actualType;
		GLint location;
	};

	struct VectorUniform {
		float value[4];
		int actualLength;
		GLint location;
	};

	struct MatrixUniform {
		float value[16];
		int actualWidth;
		int actualHeight;
		GLint location;
	};

	struct TextureUniform {
		RcPtr<Texture> value;
		GLint location;
	};

	RcPtr<Shader> mShader;
	std::vector<ScalarUniform> mBoundScalars;
	std::vector<VectorUniform> mBoundVectors;
	std::vector<MatrixUniform> mBoundMatrices;
	std::vector<TextureUniform> mBoundTextures;

public:
	Material(Shader* shader);

	void SetFloat(const char* name, float value);
	void SetInt(const char* name, int32_t value);
	void SetUInt(const char* name, uint32_t value);

	/// Instanciated for length == 1, 2, 3, 4
	template <int length>
	void SetVector(const char* name, const glm::vec<length, float>& vec);

	/// Instanciated for sizes (2,2) (3,3) (4,4) (2,3) (3,2) (2,4) (4,2) (3,4) (4,3)
	template <int width, int height>
	void SetMatrix(const char* name, const glm::mat<width, height, float>& mat);

	void SetTexture(const char* name, Texture* texture);

	std::span<const VectorUniform> GetVectors() const;
	std::span<const MatrixUniform> GetMatrices() const;
	std::span<const TextureUniform> GetTextures() const;
	const Shader& GetShader() const;

	void UseUniforms() const;
};