-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cc
executable file
·354 lines (259 loc) · 8.5 KB
/
demo.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Important: include GLEW before other graphic libraries
#define GLEW_STATIC
#include <GL/glew.h>
//#include <GLFW/glfw3.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <EGL/egl.h>
#include <EGL/eglext.h>
//#include <GL/osmesa.h>
#include <GL/gl.h>
//#include <GL/glext.h>
#include "shader.h"
#include "third_party/stb_image/stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "demo.h"
#include <wayland-client.h>
static
void key_callback(GLFWwindow*, int, int, int, int);
static
void cursor_pos_callback(GLFWwindow*, double, double);
void error_callback(int, const char*);
static
int error(const char*);
static float mouseX = 0.5;
static float mouseY = 0.5;
void GLAPIENTRY
MessageCallback(
GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam)
{
fprintf(
stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
type, severity, message
);
}
void assert_egl_error(const std::string& msg) {
EGLint error = eglGetError();
if (error == EGL_SUCCESS)
return;
std::cout << "EGL error 0x" << std::hex << error << " at " << msg;
exit(-1);
}
namespace sandbox {
GLfloat quadVertices[] = {
// Position // UV
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
unsigned Demo :: loadImage(const char* path) {
stbi_set_flip_vertically_on_load(true);
int width, height, channels;
unsigned char *image = stbi_load(
path,
&width,
&height,
&channels,
STBI_rgb_alpha
);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(image);
return texture;
}
unsigned Demo :: createTexture(int width, int height) {
unsigned int tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
return tex;
}
void Demo :: recreateSquareFramebuffer(unsigned &framebuffer, unsigned &texture, int width, int height) {
int nwidth = width, nheight = height;
if(width > height)
nheight *= width / height;
else
nwidth *= height / width;
recreateFramebuffer(framebuffer, texture, nwidth, nheight);
}
void Demo :: recreateFramebuffer(unsigned &framebuffer, unsigned &texture, int width, int height) {
if(framebuffer != 0)
glDeleteFramebuffers(1, &framebuffer);
glGenFramebuffers(1, &framebuffer);
if(texture != 0)
glDeleteTextures(1, &texture);
texture = createTexture(width, height);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Demo :: Demo(int argc, char **argv):
m_argc(), m_argv() {
}
Demo :: ~Demo() {
}
int Demo :: run() {
// Init Wayland
wDisplay = wl_display_create();
if(!wDisplay) {
fprintf(stderr, "Unable to create W1ayland display.\n");
return 1;
}
const char *wSocket = wl_display_add_socket_auto(wDisplay);
if(!wSocket) {
fprintf(stderr, "Unable to add socket to Wayland display.\n");
return 1;
}
fprintf(stderr, "Running Wayland display on socket %s\n", wSocket);
wl_display_run(wDisplay);
// Init EGL
EGLDisplay display;
EGLConfig config;
EGLContext context;
EGLSurface surface;
EGLint num_config;
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
assert_egl_error("eglGetDisplay");
eglInitialize(display, 0, 0);
assert_egl_error("eglInitialize");
eglChooseConfig(display, nullptr, &config, 1, &num_config);
assert_egl_error("eglChooseConfig");
eglBindAPI(EGL_OPENGL_API);
assert_egl_error("eglBindAPI");
context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
assert_egl_error("eglCreateContext");
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, context);
assert_egl_error("eglMakeCurrent");
// Init GLEW
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
return error("Failed to initialize GLEW");
// Enable debug
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, 0);
// Load shaders
Shader shader(
"./shaders/sdf.vert",
"./shaders/sdf.frag"
);
// Screen array and buffer
unsigned int quadVAO, quadVBO;
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
// Load textures
GLuint texture0 = loadImage("../shader/resources/background.png");
GLuint texture1 = loadImage("../shader/resources/photo1.png");
// Some framebuffers
GLuint fbMain = 0, texMain = 0;
// Main cycle
int rwidth = 0, rheight = 0;
do {
int width, height;
//glfwGetFramebufferSize(m_window, &width, &height);
width = this->initialWidth; height = this->initialHeight;
// Resize textures
if(height != rheight || width != rwidth) {
rheight = height;
rwidth = width;
this->recreateFramebuffer(fbMain, texMain, width, height);
}
glViewport(0, 0, width, height);
GLfloat time = 0;//glfwGetTime();
glBindFramebuffer(GL_FRAMEBUFFER, fbMain);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
shader.use();
glUniform1f(glGetUniformLocation(shader.program, "uTime"), time);
glUniform2f(glGetUniformLocation(shader.program, "uSize"),
width, height
);
glUniform2f(glGetUniformLocation(shader.program,
"uMouse"), mouseX, mouseY
);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture0);
glUniform1i(glGetUniformLocation(shader.program, "uTexture0"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(shader.program, "uTexture1"), 1);
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
/*
glfwSwapBuffers(m_window);
glfwPollEvents();
*/
}
while (true);//(!glfwWindowShouldClose(m_window));
// Ternimate
glDeleteVertexArrays(1, &quadVAO);
glDeleteBuffers(1, &quadVBO);
/*
glfwDestroyWindow(m_window);
glfwTerminate();
*/
// Terminate EGL
eglDestroyContext(display, context);
assert_egl_error("eglDestroyContext");
eglTerminate(display);
assert_egl_error("eglTerminate");
wl_display_destroy(wDisplay);
return 0;
}
} // end namespace sandbox
/*
void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
static void cursor_pos_callback(GLFWwindow* w,
double x, double y) {
int width, height;
glfwGetFramebufferSize(w, &width, &height);
int wx, wy;
glfwGetWindowPos(w, &wx, &wy);
const GLFWvidmode *vidmode = glfwGetVideoMode(
glfwGetPrimaryMonitor()
);
mouseX = (x - wx) / width;
mouseY = (y - wy) / height;
}
*/
static int error(const char *s) {
printf("%s\n", s);
return -1;
}