forked from kuravih/gllock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
498 lines (323 loc) · 11.1 KB
/
common.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/glx.h>
#define DEBUG 0
#define TICK 0.02f
float
elapsed_time_s(clock_t start_time)
{
clock_t tick = clock();
return ((float) (tick - start_time)) / CLOCKS_PER_SEC;
}
static GLuint
load_shaders_str(const char* vertex_shader_source, const char* fragment_shader_source)
{
// Create the shaders
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
GLint Result = GL_FALSE;
int InfoLogLength;
#if DEBUG
printf("Compiling vertex shader\n");
printf("------------------------- vertex shader -------------------------\n%s\n-----------------------------------------------------------------\n\n",vertex_shader_source);
#endif
glShaderSource(VertexShaderID, 1, &vertex_shader_source , NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if(InfoLogLength>0)
{
char* VertexShaderErrorMessage;
VertexShaderErrorMessage = (char*) malloc(InfoLogLength+1);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
printf("%s\n", &VertexShaderErrorMessage[0]);
free(VertexShaderErrorMessage);
}
#if DEBUG
printf("Compiling fragment shader\n");
printf("------------------------ fragment shader ------------------------\n%s\n-----------------------------------------------------------------\n\n",fragment_shader_source);
#endif
glShaderSource(FragmentShaderID, 1, &fragment_shader_source , NULL);
glCompileShader(FragmentShaderID);
// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if(InfoLogLength>0)
{
char* FragmentShaderErrorMessage;
FragmentShaderErrorMessage = (char*) malloc(InfoLogLength+1);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
printf("%s\n", &FragmentShaderErrorMessage[0]);
free(FragmentShaderErrorMessage);
}
// Link the program
#if DEBUG
printf("Linking program\n");
#endif
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if(InfoLogLength>0)
{
char* ProgramErrorMessage;
ProgramErrorMessage = (char*) malloc(InfoLogLength+1);
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
printf("%s\n", &ProgramErrorMessage[0]);
free(ProgramErrorMessage);
}
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
return ProgramID;
}
static char*
read_file(const char* filename)
{
char* buffer;
long length = 0;
size_t result = 0;
FILE* fp = fopen(filename, "r");
if(fp)
{
fseek(fp, 0, SEEK_END);
length = ftell(fp);
rewind(fp);
buffer = (char*)malloc(length);
if(buffer)
{
result = fread(buffer, 1, length, fp);
}
fclose(fp);
}
else
{
#if DEBUG
printf("file %s not loading\n",filename);
#endif
exit(0);
}
if (result != length)
{
#if DEBUG
printf("Reading error %s\n",filename);
#endif
exit(3);
}
return buffer;
}
static GLuint
load_shaders_file(const char* vertex_shader_file, const char* fragment_shader_file) {
char *vertex_shader_str, *fragment_shader_str;
GLuint ProgramID;
vertex_shader_str = read_file(vertex_shader_file);
#if DEBUG
printf("------------------------- vertex shader -------------------------\nfile: %s\n-----------------------------------------------------------------\n\n",vertex_shader_file);
#endif
fragment_shader_str = read_file(fragment_shader_file);
#if DEBUG
printf("------------------------ fragment shader ------------------------\nfile: %s\n-----------------------------------------------------------------\n\n",fragment_shader_file);
#endif
ProgramID = load_shaders_str(vertex_shader_str, fragment_shader_str);
free(vertex_shader_str);
free(fragment_shader_str);
return ProgramID;
}
Window
fullscreen_win(Display* xdisplay, Window root) {
GLint attrib[] =
{
GLX_RGBA,
GLX_DEPTH_SIZE, 24,
GLX_DOUBLEBUFFER,
None
};
XVisualInfo* xvisual_info;
xvisual_info = glXChooseVisual(xdisplay, 0, attrib);
if(xvisual_info == NULL)
{
printf("no appropriate visual found\n");
exit(0);
}
XWindowAttributes xwindow_attrib;
XGetWindowAttributes(xdisplay, root, &xwindow_attrib);
XSetWindowAttributes set_xwindow_attrib;
set_xwindow_attrib.colormap = XCreateColormap(xdisplay, root, xvisual_info->visual, AllocNone);
set_xwindow_attrib.event_mask = ExposureMask | KeyPressMask;
set_xwindow_attrib.override_redirect = 1;
return XCreateWindow(xdisplay, root, 0, 0, xwindow_attrib.width, xwindow_attrib.height, 0, xvisual_info->depth, InputOutput, xvisual_info->visual, CWBorderPixel|CWColormap|CWEventMask|CWOverrideRedirect, &set_xwindow_attrib );
}
static XImage*
screen_capture(Display* xdisplay, Window root) {
XWindowAttributes xwindow_attrib;
XGetWindowAttributes(xdisplay, root, &xwindow_attrib);
return XGetImage(xdisplay, root, 0, 0, xwindow_attrib.width, xwindow_attrib.height, AllPlanes, ZPixmap);
}
#define MAJOR_VERSION 3
#define MINOR_VERSION 3
typedef GLXContext (*glXCreateContextAttribsARBProc) (Display*, GLXFBConfig, GLXContext, Bool, const int*);
void
create_gl_context(Display* xdisplay, Window win) {
// Create_the_modern_OpenGL_context
// --------------------------------
static int xvisual_attribs[] =
{
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_DOUBLEBUFFER, 1,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
None
};
int num_fbc;
GLXFBConfig* xfbc;
xfbc = glXChooseFBConfig(xdisplay, DefaultScreen(xdisplay), xvisual_attribs, &num_fbc);
if (!xfbc)
{
printf("glXChooseFBConfig() failed\n");
exit(1);
}
XVisualInfo* xvisual_info;
// Create old OpenGL context to get correct function pointer for glXCreateContextAttribsARB()
xvisual_info = glXGetVisualFromFBConfig(xdisplay, xfbc[0]);
GLXContext xcontext_old;
xcontext_old = glXCreateContext(xdisplay, xvisual_info, 0, GL_TRUE);
glXCreateContextAttribsARBProc glXCreateContextAttribsARB;
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
/* Destroy old context */
glXMakeCurrent(xdisplay, 0, 0);
glXDestroyContext(xdisplay, xcontext_old);
if (!glXCreateContextAttribsARB)
{
printf("glXCreateContextAttribsARB() not found\n");
exit(1);
}
// Set desired minimum OpenGL version
static int context_attribs[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, MAJOR_VERSION,
GLX_CONTEXT_MINOR_VERSION_ARB, MINOR_VERSION,
None
};
// Create modern OpenGL context
GLXContext xcontext;
xcontext = glXCreateContextAttribsARB(xdisplay, xfbc[0], NULL, 1, context_attribs);
if (!xcontext)
{
printf("Failed to create OpenGL context. Exiting.\n");
exit(1);
}
XMapRaised(xdisplay, win);
glXMakeCurrent(xdisplay, win, xcontext);
int major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
printf("OpenGL context created.\nVersion %d.%d\nVendor %s\nRenderer %s\n", major, minor, glGetString(GL_VENDOR), glGetString(GL_RENDERER));
glewExperimental = GL_TRUE; // Needed for core profile
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
exit(0);
}
XWindowAttributes xwindow_attrib;
XGetWindowAttributes(xdisplay, win, &xwindow_attrib);
glViewport(0, 0, xwindow_attrib.width, xwindow_attrib.height);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
GLuint
setup_shaders(Display* xdisplay, Window root, Window win, const char* vertex_shader_file, const char* fragment_shader_file) {
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
GLuint programID;
programID = load_shaders_file(vertex_shader_file, fragment_shader_file); // Create and compile our GLSL program from the shaders
glUseProgram(programID);
GLuint imageDataID = glGetUniformLocation(programID, "imageData");
glUniform1i(imageDataID, 0);
glActiveTexture(GL_TEXTURE0);
GLuint screenSizeID = glGetUniformLocation(programID, "screenSize");
XWindowAttributes xwindow_attrib;
XGetWindowAttributes(xdisplay, win, &xwindow_attrib);
glUniform2f(screenSizeID, xwindow_attrib.width, xwindow_attrib.height);
static const GLfloat g_vertex_buffer_data[] =
{
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f
};
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
XImage* ximage;
ximage = screen_capture(xdisplay, root);
if(ximage == NULL)
{
printf("\n\tximage could not be created.\n\n");
exit(0);
}
GLuint texture_id;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, xwindow_attrib.width, xwindow_attrib.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)(&(ximage->data[0])));
XDestroyImage(ximage);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); // Clear the screen
return programID;
}
void
animate_in(Display* xdisplay, Window win, GLuint timeID) {
animate(xdisplay, win, in, timeID);
}
void
animate_out(Display* xdisplay, Window win, GLuint timeID) {
animate(xdisplay, win, out, timeID);
}
void
animate_cts(Display* xdisplay, Window win, Bool* condition, GLuint timeID) {
float time_s=0;
clock_t start_time;
start_time = clock();
while(*condition)
{
time_s=(elapsed_time_s(start_time)+TICK);
glUniform1f(timeID, time_s);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glXSwapBuffers(xdisplay, win);
}
}
void
animate(Display* xdisplay, Window win, ease_t ease, GLuint timeID) {
float time_s=0;
clock_t start_time;
start_time = clock();
while( (time_s=elapsed_time_s(start_time))<TICK )
{
if (ease == in)
glUniform1f(timeID, time_s);
if (ease == out)
glUniform1f(timeID, TICK-time_s);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glXSwapBuffers(xdisplay, win);
}
}