-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPitchObject.hpp
109 lines (90 loc) · 2.68 KB
/
PitchObject.hpp
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
#pragma once
#include <array>
#include "Transformation.hpp"
#include "Region.hpp"
#include "Camera.hpp"
#include "ShaderProgram.hpp"
#include "Shader.hpp"
#include "ShaderUniform.hpp"
#include "ShaderAttrib.hpp"
#include "Texture.hpp"
struct PitchObject {
std::string dir;
Transformation transform;
glm::mat4 matrix;
gl::ShaderProgram<
gl::VertexShader,
gl::FragmentShader
> program;
gl::Uniform<gl::UniformType::MAT4> uTransform;
gl::Buffer<GL_ARRAY_BUFFER, gl::BufferElementType::VEC2> buf;
gl::Attrib<decltype(buf)> attrVertex;
gl::VertexArray<decltype(attrVertex)> vao;
gl::Texture grassTx;
using ShaderBuffer = decltype(buf);
using ShaderAttrib = decltype(attrVertex);
using VertexArray = decltype(vao);
using ShaderProgram = decltype(program);
PitchObject(const std::string &dir):
dir(dir),
program({
sys::Path(dir) / sys::Path("shaders"s) / sys::Path("pitch.vert"s),
sys::Path(dir) / sys::Path("shaders"s) / sys::Path("pitch.frag"s)
}),
uTransform("transform"),
attrVertex("vertex", buf),
vao(attrVertex),
grassTx("grass")
{
transform.SetScale(2, 1, 1);
transform.SetPosition(0, 0, 0);
transform.SetRotation(0, 1, 0, 0.f);
}
void init() {
ShaderBuffer::init(buf);
buf.allocate<GL_STREAM_DRAW>(std::vector<float>{
1,1, -1,1, -1,-1,
-1,-1, 1,-1, 1,1,
});
VertexArray::init(vao);
attrVertex.select_buffer(buf);
vao.enable(attrVertex);
vao.set_access(attrVertex, 0, 0);
ShaderProgram::init(program, vao);
grassTx.init(sys::Path(dir) / sys::Path("assets"s) / sys::Path("grass.png"s));
grassTx.uSampler.set_id(program.id());
uTransform.set_id(program.id());
}
Region playable_area() {
return Region(glm::vec2(-1.80, 1.80), glm::vec2(-.95, .95));
}
Region left_half() {
auto playable = playable_area();
return Region(glm::vec2(playable.xs.x, 0), glm::vec2(playable.ys.x, playable.ys.y));
}
Region right_half() {
auto playable = playable_area();
return Region(glm::vec2(0, playable.xs.y), glm::vec2(playable.ys.x, playable.ys.y));
}
void display(Camera &cam) {
ShaderProgram::use(program);
if(transform.has_changed || cam.has_changed) {
matrix = cam.get_matrix() * transform.get_matrix();
uTransform.set_data(matrix);
transform.has_changed = false;
}
gl::Texture::set_active(0);
grassTx.bind();
grassTx.set_data(0);
VertexArray::draw<GL_TRIANGLES>(vao);
gl::Texture::unbind();
ShaderProgram::unuse();
}
void clear() {
grassTx.clear();
ShaderAttrib::clear(attrVertex);
ShaderBuffer::clear(buf);
VertexArray::clear(vao);
ShaderProgram::clear(program);
}
};