summaryrefslogtreecommitdiff
path: root/themes/src/summer/SummerTheme.cpp
blob: 1f76b56d9c0b71312225db0b6e631f3c85d17c6d (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
#include "SummerTheme.h"
#include "../Renderer2d.h"
#include "../list.h"
#include "../mathlib.h"
#include "../shaders/sun_frag.h"
#include "../shaders/sun_vert.h"
#include <vector>

SummerTheme::SummerTheme(WebglContext* context)
{
    renderer.load(context);
    load(context);
}

SummerTheme::~SummerTheme()
{
    unload();
}

void SummerTheme::load(WebglContext* context) {
    renderer.load(context, shader_sun_vert, shader_sun_frag);
	renderer.clearColor = Vector4(0, 181, 286, 255.f).toNormalizedColor();
	sun.sectors = 180;
	sun.radius = renderer.context->width / 4.f;
	sun.load(&renderer);
}

void SummerTheme::update(f32 dtSeconds) {
	sun.update(dtSeconds);
}

void SummerTheme::render() {
    renderer.render();
	sun.render(&renderer);
}

void SummerTheme::unload() {
	sun.unload();
}

void Sun::load(Renderer2d* renderer) {
	matte::List<Vertex2D> vertices;
	matte::List<u32> indices;
	Vector4 sunColor = Vector4(249, 215, 28, 255).toNormalizedColor();
	vertices.add({ Vector2(0, 0), sunColor, Mat4x4() });

    f32 radiansPerSector = (2.f * PI) / sectors;
	for (i32 i = 0; i <= sectors; i++) {
		f32 radians = radiansPerSector * i;
		f32 cosAngle = cosf(radians);
		f32 sinAngle = sinf(radians);
		Vector2 vertex = Vector2(radius * cosAngle, radius * sinAngle);
		vertices.add({ vertex, sunColor, Mat4x4() });

		u32 first = i;
		u32 second = 0;
		u32 third = i + 1;
		indices.add(first);
		indices.add(second);
		indices.add(third);
	}
	
	mesh.load(&vertices.data[0], vertices.numElements, &indices.data[0], indices.numElements, renderer);
	mesh.model = Mat4x4().translateByVec2(Vector2(renderer->context->width / 2.f, renderer->context->height / 2.f));
	vertices.deallocate();
	indices.deallocate();
}

void Sun::update(f32 dtSeconds) {

}

void Sun::render(Renderer2d* renderer) {
	setShaderMat4(renderer->uniforms.model, mesh.model);
	glBindVertexArray(mesh.vao);
    glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

void Sun::unload() {
	mesh.unload();
}