1
1
#include < stdio.h>
2
2
#include < stdlib.h>
3
3
4
+ #include < math.h>
5
+
4
6
#include < EGL/egl.h>
5
7
#include < GLES/gl.h>
8
+
6
9
#include " gl_platform.h"
7
10
#include " gl.h"
8
11
12
+ #include " shader_stuff.h"
13
+
14
+
9
15
static EGLDisplay edpy;
10
16
static EGLSurface esfc;
11
17
static EGLContext ectxt;
@@ -14,6 +20,29 @@ static EGLContext ectxt;
14
20
void *gl_es_display;
15
21
void *gl_es_surface;
16
22
23
+
24
+ static float vertex_coords[] = {
25
+ -1 .0f , 1 .0f , 0 .0f , // 0 0 1
26
+ 1 .0f , 1 .0f , 0 .0f , // 1 ^
27
+ -1 .0f , -1 .0f , 0 .0f , // 2 | 2 3
28
+ 1 .0f , -1 .0f , 0 .0f , // 3 +-->
29
+ };
30
+
31
+ static float orig_texture_coords[] = {
32
+ -0 .5f , -0 .5f ,
33
+ 0 .5f , -0 .5f ,
34
+ -0 .5f , 0 .5f ,
35
+ 0 .5f , 0 .5f ,
36
+ };
37
+
38
+ static float texture_coords[] = {
39
+ 0 .0f , 0 .0f , // we flip this:
40
+ 1 .0f , 0 .0f , // v^
41
+ 0 .0f , 1 .0f , // | u
42
+ 1 .0f , 1 .0f , // +-->
43
+ };
44
+
45
+
17
46
static int gl_have_error (const char *name)
18
47
{
19
48
GLenum e = glGetError ();
@@ -42,11 +71,25 @@ int gl_init(void *display, void *window, int *quirks)
42
71
EGLint num_config;
43
72
int retval = -1 ;
44
73
int ret;
45
- EGLint attr[] =
46
- {
47
- EGL_NONE
48
- };
49
-
74
+
75
+ static const EGLint config_attributes[] =
76
+ {
77
+ EGL_RED_SIZE, 8 ,
78
+ EGL_GREEN_SIZE, 8 ,
79
+ EGL_BLUE_SIZE, 8 ,
80
+ EGL_ALPHA_SIZE, 8 ,
81
+ EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
82
+ EGL_NONE
83
+ };
84
+
85
+ static const EGLint context_attributes[] =
86
+ {
87
+ EGL_CONTEXT_CLIENT_VERSION, 2 ,
88
+ EGL_NONE
89
+ };
90
+
91
+
92
+ // gl_platform_init() does Raspi-specific stuff like bcm_host_init()
50
93
ret = gl_platform_init (&display, &window, quirks);
51
94
if (ret != 0 ) {
52
95
printf (" gl_platform_init failed with %d\n " , ret);
@@ -70,7 +113,7 @@ int gl_init(void *display, void *window, int *quirks)
70
113
goto out;
71
114
}
72
115
73
- if (!eglChooseConfig (edpy, attr , &ecfg, 1 , &num_config)) {
116
+ if (!eglChooseConfig (edpy, config_attributes , &ecfg, 1 , &num_config)) {
74
117
printf (" Failed to choose config (%x)\n " , eglGetError ());
75
118
goto out;
76
119
}
@@ -88,7 +131,8 @@ int gl_init(void *display, void *window, int *quirks)
88
131
goto out;
89
132
}
90
133
91
- ectxt = eglCreateContext (edpy, ecfg, EGL_NO_CONTEXT, NULL );
134
+ // ectxt = eglCreateContext(edpy, ecfg, EGL_NO_CONTEXT, NULL);
135
+ ectxt = eglCreateContext (edpy, ecfg, EGL_NO_CONTEXT, context_attributes);
92
136
if (ectxt == EGL_NO_CONTEXT) {
93
137
printf (" Unable to create EGL context (%x)\n " ,
94
138
eglGetError ());
@@ -97,16 +141,20 @@ int gl_init(void *display, void *window, int *quirks)
97
141
98
142
eglMakeCurrent (edpy, esfc, esfc, ectxt);
99
143
100
- glEnable (GL_TEXTURE_2D);
144
+ // glEnable(GL_TEXTURE_2D); // for old fixed-function pipeline
145
+ // if (gl_have_error("glEnable(GL_TEXTURE_2D)")) goto out;
101
146
102
147
glGenTextures (1 , &texture_name);
148
+ if (gl_have_error (" glGenTextures" )) goto out;
149
+
150
+
103
151
104
152
glBindTexture (GL_TEXTURE_2D, texture_name);
153
+ if (gl_have_error (" glBindTexture" )) goto out;
105
154
106
155
glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGB, 1024 , 512 , 0 , GL_RGB,
107
156
GL_UNSIGNED_SHORT_5_6_5, tmp_texture_mem);
108
- if (gl_have_error (" glTexImage2D" ))
109
- goto out;
157
+ if (gl_have_error (" glTexImage2D" )) goto out;
110
158
111
159
// no mipmaps
112
160
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -126,57 +174,93 @@ int gl_init(void *display, void *window, int *quirks)
126
174
gl_es_display = (void *)edpy;
127
175
gl_es_surface = (void *)esfc;
128
176
retval = 0 ;
177
+
178
+ int shader_stuff_result;
179
+ shader_stuff_result = shader_stuff_init ();
180
+ shader_stuff_result = shader_stuff_reload_shaders ();
181
+ shader_stuff_result = shader_stuff_set_data (vertex_coords, texture_coords, texture_name);
182
+
129
183
out:
130
184
free (tmp_texture_mem);
131
185
return retval;
132
186
}
133
187
134
- static float vertices[] = {
135
- -1 .0f , 1 .0f , 0 .0f , // 0 0 1
136
- 1 .0f , 1 .0f , 0 .0f , // 1 ^
137
- -1 .0f , -1 .0f , 0 .0f , // 2 | 2 3
138
- 1 .0f , -1 .0f , 0 .0f , // 3 +-->
139
- };
140
-
141
- static float texture[] = {
142
- 0 .0f , 0 .0f , // we flip this:
143
- 1 .0f , 0 .0f , // v^
144
- 0 .0f , 1 .0f , // | u
145
- 1 .0f , 1 .0f , // +-->
146
- };
188
+ static int framecount = 0 ;
147
189
148
190
int gl_flip (const void *fb, int w, int h)
149
191
{
150
192
static int old_w, old_h;
151
193
194
+ if (framecount % 60 == 0 )
195
+ {
196
+ // printf("gl_flip() w: %d, h: %d\n", w, h);
197
+ }
198
+
199
+ if (framecount % 30 == 0 )
200
+ {
201
+ if (shader_stuff_shader_needs_reload ()) {
202
+ shader_stuff_reload_shaders ();
203
+ // shader_stuff_set_data(vertex_coords, texture_coords, texture_name);
204
+
205
+ }
206
+ }
207
+
208
+ framecount++;
209
+ float floattime = (framecount * 0 .04f );
210
+
152
211
if (fb != NULL ) {
153
212
if (w != old_w || h != old_h) {
154
213
float f_w = (float )w / 1024 .0f ;
155
214
float f_h = (float )h / 512 .0f ;
156
- texture [1 *2 + 0 ] = f_w;
157
- texture [2 *2 + 1 ] = f_h;
158
- texture [3 *2 + 0 ] = f_w;
159
- texture [3 *2 + 1 ] = f_h;
215
+ texture_coords [1 *2 + 0 ] = f_w;
216
+ texture_coords [2 *2 + 1 ] = f_h;
217
+ texture_coords [3 *2 + 0 ] = f_w;
218
+ texture_coords [3 *2 + 1 ] = f_h;
160
219
old_w = w;
161
220
old_h = h;
221
+ }
222
+ /*
223
+ float rotmat[4]; // 2d rotation matrix
224
+ rotmat[0] = cos(floattime);
225
+ rotmat[1] = sin(floattime);
226
+ rotmat[2] = -sin(floattime);
227
+ rotmat[3] = cos(floattime);
228
+
229
+ for (int i=0; i<4; i++) {
230
+ float f_w = (float)w / 1024.0f;
231
+ float f_h = (float)h / 512.0f;
232
+ float x = orig_texture_coords[i*2 + 0] * f_w;
233
+ float y = orig_texture_coords[i*2 + 1] * f_h;
234
+ texture_coords[i*2 + 0] =
235
+ f_w * 0.5f + (x * rotmat[0] + y * rotmat[1]);
236
+ texture_coords[i*2 + 1] =
237
+ f_h * 0.5f + (x * rotmat[2] + y * rotmat[3]);
238
+
162
239
}
240
+ */
163
241
164
242
glTexSubImage2D (GL_TEXTURE_2D, 0 , 0 , 0 , w, h,
165
243
GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
166
244
if (gl_have_error (" glTexSubImage2D" ))
167
245
return -1 ;
168
- }
246
+ } // if (fb != NULL)
247
+
248
+ // glVertexPointer(3, GL_FLOAT, 0, vertex_coords);
249
+ // if (gl_have_error("glVertexPointer")) return -1;
250
+
251
+ // glTexCoordPointer(2, GL_FLOAT, 0, texture_coords);
252
+ // if (gl_have_error("glTexCoordPointer")) return -1;
253
+
254
+ shader_stuff_frame (framecount, w, h, 800 , 480 ); // TODO! hard-coded output size
255
+ if (gl_have_error (" use program" )) return -1 ;
169
256
170
- glVertexPointer (3 , GL_FLOAT, 0 , vertices);
171
- glTexCoordPointer (2 , GL_FLOAT, 0 , texture);
172
257
glDrawArrays (GL_TRIANGLE_STRIP, 0 , 4 );
258
+ // glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
173
259
174
- if (gl_have_error (" glDrawArrays" ))
175
- return -1 ;
260
+ if (gl_have_error (" glDrawArrays" )) return -1 ;
176
261
177
262
eglSwapBuffers (edpy, esfc);
178
- if (gles_have_error (" eglSwapBuffers" ))
179
- return -1 ;
263
+ if (gles_have_error (" eglSwapBuffers" )) return -1 ;
180
264
181
265
return 0 ;
182
266
}
0 commit comments