-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegloapp.cpp
240 lines (194 loc) · 6.96 KB
/
egloapp.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* eglo -- Minimalistic X11/EGL abstraction for Pocket C.H.I.P
* Copyright (c) 2016, 2017, 2018 Thomas Perl <[email protected]>.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
**/
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <vector>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <X11/keysym.h>
#include "eglo.h"
#define FAIL(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
namespace {
const char *vertex_src =
"attribute vec4 position;"
"attribute vec4 color;"
"varying mediump vec4 col;"
"void main() {"
" gl_Position = position;"
" col = color;"
"}";
const char *fragment_src =
"varying mediump vec4 col;"
"void main() {"
" gl_FragColor = col;"
"}";
GLuint
load_shader(const char *shader_source, GLenum type)
{
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &shader_source, nullptr);
glCompileShader(shader);
GLint length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
if (length > 0) {
std::vector<char> buffer(length);
glGetShaderInfoLog(shader, length, nullptr, buffer.data());
printf("shader info: %s\n", buffer.data());
}
GLint success = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success != GL_TRUE) {
FAIL("Could not compile shader");
}
return shader;
}
typedef float f32;
struct v3 {
v3(f32 x=0.f, f32 y=0.f, f32 z=0.f) : x(x), y(y), z(z) {}
v3 operator*(float f) const { return v3{x*f, y*f, z*f}; }
v3 operator*(const v3 &o) const { return v3{x*o.x, y*o.y, z*o.z}; }
v3 operator+(const v3 &o) const { return v3{x+o.x, y+o.y, z+o.z}; }
v3 &operator+=(const v3 &o) { x+=o.x; y+=o.y; z+=o.z; return *this; }
f32 x; f32 y; f32 z;
};
struct v4 {
v4(f32 r=0.f, f32 g=0.f, f32 b=0.f, f32 a=0.f) : r(r), g(g), b(b), a(a) {}
v4 operator*(float f) const { return v4{r*f, g*f, b*f, a*f}; }
v4 operator*(const v4 &o) const { return v4{r*o.r, g*o.g, b*o.b, a*o.a}; }
v4 operator+(const v4 &o) const { return v4{r+o.r, g+o.g, b+o.b, a+o.a}; }
v4 &operator*=(float f) { r*=f; g*=f; b*=f; a*=f; return *this; }
v4 &operator+=(const v4 &o) { r+=o.r; g+=o.g; b+=o.b; a+=o.a; return *this; }
float intensity() { return r + g + b; }
f32 r; f32 g; f32 b; f32 a;
};
struct vtx {
vtx(const v3 &pos=v3(), const v4 &col=v4()) : position(pos), color(col) {}
v3 position; v4 color;
};
GLint position_loc, color_loc;
std::vector<vtx> vertices;
std::vector<vtx> vertices_tmp;
int vertices_offset = 0;
f32 rnd() { return f32(random() % 1000) / 1000.f; }
v4 target {rnd(), rnd(), rnd(), rnd()};
v4 base{ 0.6f, 0.f, 0.f, 0.f };
void egloapp_init(int width, int height)
{
const char *egl_extensions = eglQueryString(eglGetDisplay(EGL_DEFAULT_DISPLAY), EGL_EXTENSIONS);
const GLubyte *gl_extensions = glGetString(GL_EXTENSIONS);
printf("= EGL extensions==\n%s\n\n= GL extensions =\n%s\n\n",
egl_extensions, gl_extensions);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, load_shader(vertex_src, GL_VERTEX_SHADER));
glAttachShader(shaderProgram, load_shader(fragment_src, GL_FRAGMENT_SHADER));
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
position_loc = glGetAttribLocation(shaderProgram, "position");
color_loc = glGetAttribLocation(shaderProgram, "color");
if (position_loc < 0 || color_loc < 0) {
FAIL("Unable to get uniform location");
}
glEnableVertexAttribArray(position_loc);
glEnableVertexAttribArray(color_loc);
glViewport(0, 0, width, height);
glClearColor(0.f, 0.f, 0.f, 0.f);
}
void egloapp_render()
{
glClear(GL_COLOR_BUFFER_BIT);
int off = vertices_offset;
int max = 5000;
if (vertices.size() > max && off < vertices.size() - max) {
off = vertices.size() - max;
}
char *ptr = (char *)(vertices.data() + off);
glVertexAttribPointer(position_loc, 3, GL_FLOAT, false, sizeof(vtx), ptr);
glVertexAttribPointer(color_loc, 4, GL_FLOAT, false, sizeof(vtx), ptr + sizeof(vtx::position));
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size() - off);
}
void egloapp_update()
{
static int i = 0;
if (i++ % 100 == 0) {
target = { rnd(), rnd(), rnd(), rnd() };
}
for (auto it = vertices.begin() + vertices_offset; it != vertices.end(); ++it) {
it->color *= 0.99f;
it->position += v3{0.5f-rnd(), 0.5f-rnd(), 0.5f-rnd()} * 0.05f;
}
auto it = vertices.begin() + vertices_offset;
while (it != vertices.end() && it->color.intensity() < 0.03f) {
vertices_offset++;
++it;
}
if (vertices_offset > 1000) {
vertices_tmp.clear();
vertices_tmp.reserve(vertices.size() - vertices_offset);
vertices_tmp.insert(vertices_tmp.begin(), vertices.begin() + vertices_offset, vertices.end());
vertices.swap(vertices_tmp);
vertices_offset = 0;
}
float alpha = 0.9f;
base *= alpha;
base += target * (1.f - alpha);
}
};
int main(int argc, char *argv[])
{
int width = 0, height = 0;
if (eglo_init(&width, &height, 2) != 0) {
FAIL("Could not create window");
}
egloapp_init(width, height);
bool quit = false;
while (!quit) {
EgloEvent e;
while (eglo_next_event(&e)) {
switch (e.type) {
case EGLO_QUIT:
quit = true;
break;
case EGLO_KEY_DOWN:
if (e.key.key == XK_Escape) {
quit = true;
}
break;
case EGLO_KEY_UP:
break;
case EGLO_MOUSE_DOWN:
break;
case EGLO_MOUSE_MOTION:
for (int x=0; x<40; x++) {
vertices.emplace_back(v3{
0.05f * (rnd() - 0.5f) + 2.f * (float(e.mouse.x) / float(width)) - 1.f,
0.05f * (rnd() - 0.5f) + 1.f - 2.f * float(e.mouse.y) / float(height),
0.f,
}, base + v4{rnd(), rnd(), rnd(), 1.f} * 0.2f);
}
break;
case EGLO_MOUSE_UP:
break;
}
}
egloapp_render();
egloapp_update();
eglo_swap_buffers();
}
eglo_quit();
return 0;
}