-
Notifications
You must be signed in to change notification settings - Fork 1
/
scene.cc
153 lines (108 loc) · 3.1 KB
/
scene.cc
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "scene.h"
#include <glm/gtc/matrix_transform.hpp>
namespace gl = glue;
namespace iris {
static const char vs_simple[] = R"SHDR(
#version 140
in vec2 pos;
uniform mat4 viewport;
void main()
{
gl_Position = viewport * vec4(pos, 0.0, 1.0);
}
)SHDR";
static const char fs_simple[] = R"SHDR(
#version 140
out vec4 finalColor;
uniform vec4 plot_color;
void main() {
finalColor = plot_color;
}
)SHDR";
void rectangle::draw(glm::mat4 vp) {
glm::mat4 tscale = glm::scale(glm::mat4(1), glm::vec3(size.width, size.height, 0));
glm::mat4 mvp = vp * tscale;
gl::color::rgba gl_color(color);
prg.use();
prg.uniform("plot_color", gl_color);
prg.uniform("viewport", mvp);
va.bind();
glDrawArrays(GL_TRIANGLES, 0, 6);
va.unbind();
prg.unuse();
}
void rectangle::init() {
vs = gl::shader::make(vs_simple, GL_VERTEX_SHADER);
fs = gl::shader::make(fs_simple, GL_FRAGMENT_SHADER);
vs.compile();
fs.compile();
prg = gl::program::make();
prg.attach({vs, fs});
prg.link();
std::vector<float> box = {
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
1.0f, 0.0f};
bb = gl::buffer::make();
va = gl::vertex_array::make();
bb.bind();
va.bind();
bb.data(box);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
bb.unbind();
va.unbind();
}
void rectangle::configure(const iris::rgb &new_color) {
color = new_color;
}
void rectangle::configure(const glue::extent &new_size) {
size = new_size;
}
glue::extent rectangle::bounds() const {
return size;
}
//*****************************************************************************
checkerboard::checkerboard(const iris::dkl &colorspace, double c)
: rd(), gen(rd()), dis(0, 15) {
std::vector<double> circ_phi = iris::linspace(0.0, 2*M_PI, 16);
colors.resize(circ_phi.size());
std::transform(circ_phi.cbegin(), circ_phi.cend(), colors.begin(), [&](const double p){
iris::rgb crgb = colorspace.iso_lum(p, c);
uint8_t creport;
iris::rgb res = crgb.clamp(&creport);
if (creport != 0) {
std::cerr << "[W] color clamped: " << crgb << " → " << res << " @ c: " << c << std::endl;
}
return res;
});
}
void checkerboard::init() {
box.init();
}
void checkerboard::draw(glm::mat4 vp) {
for (float x = 0.f; x < size.width; x += box.bounds().width) {
for(float y = 0.f; y < size.height; y += box.bounds().height) {
glm::mat4 ttrans = glm::translate(glm::mat4(1), glm::vec3(x, y, 0.0f));
box.configure(colors[dis(gen)]);
box.draw(vp * ttrans);
}
}
}
void checkerboard::configure(glue::extent frame, glue::extent box_size) {
size = frame;
box.configure(box_size);
}
void checkerboard::reset_timer() {
t_start = glfwGetTime();
}
double checkerboard::duration() const {
double t_now = glfwGetTime();
return t_now - t_start;
}
}