-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
387 lines (302 loc) · 11.7 KB
/
main.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
/* Created by exoticorn ( http://talk.maemo.org/showthread.php?t=37356 )
* edited and commented by André Bergner [endboss]
*
* libraries needed: libx11-dev, libgles2-dev
*
* compile with: gcc -lX11 -lEGL -lGLESv2 main.cpp
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
const char vertex_src [] =
" \
attribute vec4 position; \
varying mediump vec2 pos; \
uniform vec4 offset; \
\
void main() \
{ \
gl_Position = position + offset; \
pos = position.xy; \
} \
";
const char fragment_src [] =
" \
varying mediump vec2 pos; \
uniform mediump float phase; \
\
void main() \
{ \
gl_FragColor = vec4( 1., 0.9, 0.7, 1.0 ) * \
cos( 30.*sqrt(pos.x*pos.x + 1.5*pos.y*pos.y) \
+ atan(pos.y,pos.x) - phase ); \
} \
";
// some more formulas to play with...
// cos( 20.*(pos.x*pos.x + pos.y*pos.y) - phase );
// cos( 20.*sqrt(pos.x*pos.x + pos.y*pos.y) + atan(pos.y,pos.x) - phase );
// cos( 30.*sqrt(pos.x*pos.x + 1.5*pos.y*pos.y - 1.8*pos.x*pos.y*pos.y)
// + atan(pos.y,pos.x) - phase );
void
print_shader_info_log (
GLuint shader // handle to the shader
)
{
GLint length;
glGetShaderiv ( shader , GL_INFO_LOG_LENGTH , &length );
if ( length ) {
char* buffer = malloc(sizeof (char) * length);
glGetShaderInfoLog ( shader , length , NULL , buffer );
printf ("shader info: %s", buffer);
free (buffer);
GLint success;
glGetShaderiv( shader, GL_COMPILE_STATUS, &success );
if ( success != GL_TRUE ) exit ( 1 );
}
}
GLuint
load_shader (
const char *shader_source,
GLenum type
)
{
GLuint shader = glCreateShader( type );
glShaderSource ( shader , 1 , &shader_source , NULL );
glCompileShader ( shader );
print_shader_info_log ( shader );
return shader;
}
Display *x_display;
Window win;
EGLDisplay egl_display;
EGLContext egl_context;
GLfloat
norm_x = 0.0,
norm_y = 0.0,
offset_x = 0.0,
offset_y = 0.0,
p1_pos_x = 0.0,
p1_pos_y = 0.0;
GLint
phase_loc,
offset_loc,
position_loc;
EGLSurface egl_surface;
EGLBoolean update_pos = EGL_FALSE;
const float vertexArray[] = {
0.0, 0.5, 0.0,
-0.5, 0.0, 0.0,
0.0, -0.5, 0.0,
0.5, 0.0, 0.0,
0.0, 0.5, 0.0
};
void render()
{
static float phase = 0;
static int donesetup = 0;
static XWindowAttributes gwa;
//// draw
if ( !donesetup ) {
XWindowAttributes gwa;
XGetWindowAttributes ( x_display , win , &gwa );
glViewport ( 0 , 0 , gwa.width , gwa.height );
glClearColor ( 0.08 , 0.06 , 0.07 , 1.); // background color
donesetup = 1;
}
glClear ( GL_COLOR_BUFFER_BIT );
glUniform1f ( phase_loc , phase ); // write the value of phase to the shaders phase
phase = fmodf ( phase + 0.5f , 2.f * 3.141f ); // and update the local variable
if ( update_pos ) { // if the position of the texture has changed due to user action
GLfloat old_offset_x = offset_x;
GLfloat old_offset_y = offset_y;
offset_x = norm_x - p1_pos_x;
offset_y = norm_y - p1_pos_y;
p1_pos_x = norm_x;
p1_pos_y = norm_y;
offset_x += old_offset_x;
offset_y += old_offset_y;
update_pos = EGL_FALSE;
}
glUniform4f ( offset_loc , offset_x , offset_y , 0.0 , 0.0 );
glVertexAttribPointer ( position_loc, 3, GL_FLOAT, GL_FALSE, 0, vertexArray );
glEnableVertexAttribArray ( position_loc );
glDrawArrays ( GL_TRIANGLE_STRIP, 0, 5 );
eglSwapBuffers ( egl_display, egl_surface ); // get the rendered buffer to the screen
}
////////////////////////////////////////////////////////////////////////////////////////////
static int
our_error_handler (
Display * x_display,
XErrorEvent* event
)
{
printf ("cought X11 error %d (looks like this is not Maemo)\n", event->error_code);
}
int main()
{
/////// the X11 part //////////////////////////////////////////////////////////////////
// in the first part the program opens a connection to the X11 window manager
//
int (*old_handler) (Display *, XErrorEvent *);
x_display = XOpenDisplay ( NULL ); // open the standard display (the primary screen)
if ( x_display == NULL ) {
fprintf (stderr, "cannot connect to X server\n");
return 1;
}
Window root = DefaultRootWindow( x_display ); // get the root window (usually the whole screen)
XSetWindowAttributes swa;
swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask;
win = XCreateWindow ( // create a window with the provided parameters
x_display, root,
0, 0, 800, 480, 0,
CopyFromParent, InputOutput,
CopyFromParent, CWEventMask,
&swa );
XSetWindowAttributes xattr;
Atom atom;
int one = 1;
xattr.override_redirect = False;
XChangeWindowAttributes ( x_display, win, CWOverrideRedirect, &xattr );
atom = XInternAtom ( x_display, "_NET_WM_STATE_FULLSCREEN", True );
XChangeProperty (
x_display, win,
XInternAtom ( x_display, "_NET_WM_STATE", True ),
XA_ATOM, 32, PropModeReplace,
(unsigned char*) &atom, 1 );
/* this makes the code crash on non-Maemo platforms - protect it with an error handler */
old_handler = XSetErrorHandler (our_error_handler);
XChangeProperty (
x_display, win,
XInternAtom ( x_display, "_HILDON_NON_COMPOSITED_WINDOW", True ),
XA_INTEGER, 32, PropModeReplace,
(unsigned char*) &one, 1);
XFlush (x_display);
XSetErrorHandler (old_handler);
XMapWindow ( x_display , win ); // make the window visible on the screen
XStoreName ( x_display , win , "GL test" ); // give the window a name
//// get identifiers for the provided atom name strings
Atom wm_state = XInternAtom ( x_display, "_NET_WM_STATE", False );
Atom fullscreen = XInternAtom ( x_display, "_NET_WM_STATE_FULLSCREEN", False );
XEvent xev;
memset ( &xev, 0, sizeof(xev) );
xev.type = ClientMessage;
xev.xclient.window = win;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 1;
xev.xclient.data.l[1] = fullscreen;
XSendEvent ( // send an event mask to the X-server
x_display,
DefaultRootWindow ( x_display ),
False,
SubstructureNotifyMask,
&xev );
/////// the egl part //////////////////////////////////////////////////////////////////
// egl provides an interface to connect the graphics related functionality of openGL ES
// with the windowing interface and functionality of the native operation system (X11
// in our case.
egl_display = eglGetDisplay( (EGLNativeDisplayType) x_display );
if ( egl_display == EGL_NO_DISPLAY ) {
fprintf (stderr, "Got no EGL display.\n");
return 1;
}
if ( !eglInitialize( egl_display, NULL, NULL ) ) {
fprintf (stderr, "Unable to initialize EGL\n");
return 1;
}
EGLint attr[] = { // some attributes to set up our egl-interface
EGL_BUFFER_SIZE, 16,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLConfig ecfg;
EGLint num_config;
if ( !eglChooseConfig( egl_display, attr, &ecfg, 1, &num_config ) ) {
fprintf (stderr, "Failed to choose config (eglError: %d)\n", eglGetError());
return 1;
}
if ( num_config != 1 ) {
fprintf (stderr, "Didn't get exactly one config, but %d\n", num_config);
return 1;
}
egl_surface = eglCreateWindowSurface ( egl_display, ecfg, (void*)win, NULL );
if ( egl_surface == EGL_NO_SURFACE ) {
fprintf (stderr, "Unable to create EGL surface (eglError: %d\n", eglGetError());
return 1;
}
//// egl-contexts collect all state descriptions needed required for operation
EGLint ctxattr[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
egl_context = eglCreateContext ( egl_display, ecfg, EGL_NO_CONTEXT, ctxattr );
if ( egl_context == EGL_NO_CONTEXT ) {
fprintf (stderr, "Unable to create EGL context (eglError: %d\n", eglGetError());
return 1;
}
//// associate the egl-context with the egl-surface
eglMakeCurrent( egl_display, egl_surface, egl_surface, egl_context );
/////// the openGL part ///////////////////////////////////////////////////////////////
GLuint vertexShader = load_shader ( vertex_src , GL_VERTEX_SHADER ); // load vertex shader
GLuint fragmentShader = load_shader ( fragment_src , GL_FRAGMENT_SHADER ); // load fragment shader
GLuint shaderProgram = glCreateProgram (); // create program object
glAttachShader ( shaderProgram, vertexShader ); // and attach both...
glAttachShader ( shaderProgram, fragmentShader ); // ... shaders to it
glLinkProgram ( shaderProgram ); // link the program
glUseProgram ( shaderProgram ); // and select it for usage
//// now get the locations (kind of handle) of the shaders variables
position_loc = glGetAttribLocation ( shaderProgram , "position" );
phase_loc = glGetUniformLocation ( shaderProgram , "phase" );
offset_loc = glGetUniformLocation ( shaderProgram , "offset" );
if ( position_loc < 0 || phase_loc < 0 || offset_loc < 0 ) {
fprintf (stderr, "Unable to get uniform location\n");
return 1;
}
const float
window_width = 800.0,
window_height = 480.0;
//// this is needed for time measuring --> frames per second
struct timezone tz;
struct timeval t1, t2;
gettimeofday ( &t1 , &tz );
int num_frames = 0;
EGLBoolean quit = EGL_FALSE;
while ( !quit ) { // the main loop
while ( XPending ( x_display ) ) { // check for events from the x-server
XEvent xev;
XNextEvent( x_display, &xev );
if ( xev.type == MotionNotify ) { // if mouse has moved
// cout << "move to: << xev.xmotion.x << "," << xev.xmotion.y << endl;
GLfloat window_y = (window_height - xev.xmotion.y) - window_height / 2.0;
norm_y = window_y / (window_height / 2.0);
GLfloat window_x = xev.xmotion.x - window_width / 2.0;
norm_x = window_x / (window_width / 2.0);
update_pos = EGL_TRUE;
}
if ( xev.type == KeyPress ) quit = EGL_TRUE;
}
render(); // now we finally put something on the screen
if ( ++num_frames % 100 == 0 ) {
gettimeofday( &t2, &tz );
float dt = t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec) * 1e-6;
printf ("fps: %f\n", num_frames / dt);
num_frames = 0;
t1 = t2;
}
// usleep( 1000*10 );
}
//// cleaning up...
eglDestroyContext ( egl_display, egl_context );
eglDestroySurface ( egl_display, egl_surface );
eglTerminate ( egl_display );
XDestroyWindow ( x_display, win );
XCloseDisplay ( x_display );
return 0;
}