summaryrefslogtreecommitdiff
path: root/frontend/shared_cpp/OrthographicRenderer.h
blob: c72639e4d98cece5fb6669eae92748a6106d7f30 (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
#pragma once

#include "types.h"
#include "Shader.h"
#include "mathlib.h"

struct OrthographicRenderer {
	Mat4x4 projection;
	uint32 shader;

	struct {
		int32 position;
		int32 color;
	} attributes;

	struct {
		int32 projection;
		int32 model;
	} uniforms;

	void load();
	void render();
	void unload();
};

struct OrthographicVertex {
	Vector2 position;
	Vector4 color;
};

template <uint32 N>
struct OrthographicShape {
	uint32 vao;
	uint32 vbo;
	OrthographicVertex vertices[N];
	Mat4x4 model;

	void load(OrthographicRenderer* renderer);
	void render(OrthographicRenderer* renderer);
	void unload();
};

template <uint32 N>
void OrthographicShape<N>::load(OrthographicRenderer* renderer) {
	useShader(renderer->shader);

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, N * sizeof(OrthographicVertex), &vertices[0], GL_STATIC_DRAW);

	glEnableVertexAttribArray(renderer->attributes.position);
	glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)0);

	glEnableVertexAttribArray(renderer->attributes.color);
	glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)offsetof(OrthographicVertex, color));

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);
}

template <uint32 N>
void OrthographicShape<N>::render(OrthographicRenderer* renderer) {
	setShaderMat4(renderer->uniforms.model, model);

	glBindVertexArray(vao);
	glDrawArrays(GL_TRIANGLES, 0, 3);
	glBindVertexArray(0);
}

template <uint32 N>
void OrthographicShape<N>::unload() {
	glDeleteVertexArrays(1, &vao);
	glDeleteBuffers(1, &vbo);
}