-
Notifications
You must be signed in to change notification settings - Fork 1
/
CursorObject.hpp
124 lines (102 loc) · 2.98 KB
/
CursorObject.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once
#include <vector>
#include "Transformation.hpp"
#include "ShaderProgram.hpp"
#include "Texture.hpp"
#include "Timer.hpp"
#include "Region.hpp"
namespace ui {
struct CursorObject {
std::string dir;
Transformation transform;
enum class State {
POINTER,
SELECTOR
};
gl::Uniform<gl::UniformType::MAT4> uTransform;
gl::Texture pointerTx;
gl::Texture selectorTx;
gl::ShaderProgram<
gl::VertexShader,
gl::FragmentShader
> program;
gl::Buffer<GL_ARRAY_BUFFER, gl::BufferElementType::VEC2> buf;
gl::Attrib<decltype(buf)> attrVertex;
gl::VertexArray<decltype(attrVertex)> vao;
using ShaderBuffer = decltype(buf);
using ShaderAttrib = decltype(attrVertex);
using ShaderProgram = decltype(program);
using VertexArray = decltype(vao);
State state = State::POINTER;
CursorObject(const std::string &dir):
dir(dir),
uTransform("transform"),
pointerTx("cursorTx"),
selectorTx("cursorTx"),
program({
sys::Path(dir) / sys::Path("shaders"s) / sys::Path("cursor.vert"s),
sys::Path(dir) / sys::Path("shaders"s) / sys::Path("cursor.frag"s)
}),
attrVertex("vertex", buf),
vao(attrVertex)
{
transform.SetScale(.02f);
transform.SetPosition(0, 0, 0);
transform.SetRotation(0, 0, 1, 270.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);
vao.enable(attrVertex);
vao.set_access(attrVertex, 0, 0);
ShaderProgram::init(program, vao);
pointerTx.init(sys::Path(dir) / sys::Path("assets"s) / sys::Path("pointer.png"s));
selectorTx.init(sys::Path(dir) / sys::Path("assets"s) / sys::Path("selector.png"s));
pointerTx.uSampler.set_id(program.id());
selectorTx.uSampler.set_id(program.id());
uTransform.set_id(program.id());
}
void mouse(float m_x, float m_y) {
transform.SetPosition(m_x*2-1 + .02, -(m_y*2-1 + .02), .5);
}
glm::vec2 get_position() const {
glm::vec4 pos = transform.GetPosition();
return {pos.x, pos.y};
}
void display() {
ShaderProgram::use(program);
glEnable(GL_BLEND); GLERROR
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GLERROR
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO); GLERROR
if(transform.has_changed) {
glm::mat4 matrix = transform.get_matrix();
uTransform.set_data(matrix);
/* transform.has_changed = false; */
}
gl::Texture::set_active(0);
if(state == State::POINTER) {
pointerTx.bind();
pointerTx.set_data(0);
} else {
selectorTx.bind();
selectorTx.set_data(0);
}
VertexArray::draw<GL_TRIANGLES>(vao);
gl::Texture::unbind();
glDisable(GL_BLEND); GLERROR
ShaderProgram::unuse();
}
void clear() {
pointerTx.clear();
selectorTx.clear();
ShaderAttrib::clear(attrVertex);
ShaderBuffer::clear(buf);
VertexArray::clear(vao);
ShaderProgram::clear(program);
}
};
}