24a904be39b7bba073c65ae8b2cdbdecb65062fb
[nit.git] / lib / mnit / opengles1.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # OpenGL ES1 general support (most of it)
18 module opengles1 is pkgconfig("glesv1_cm", "egl")
19
20 import mnit_display
21
22 in "C header" `{
23 #include <EGL/egl.h>
24 #include <GLES/gl.h>
25
26 EGLDisplay mnit_display;
27 EGLSurface mnit_surface;
28 EGLContext mnit_context;
29 EGLConfig mnit_config;
30 int32_t mnit_width;
31 int32_t mnit_height;
32
33 struct mnit_opengles_Texture {
34 GLuint texture;
35
36 /* offsets on source texture */
37 float src_xo, src_yo, src_xi, src_yi;
38
39 /* destination width and height */
40 int width, height;
41
42 /* may vary depending on scaling */
43 int center_x, center_y;
44
45 float scale;
46 int blended;
47 };
48
49 struct mnit_opengles_DrawableTexture {
50 struct mnit_opengles_Texture super;
51 GLuint fbo;
52 GLuint depth;
53 GLuint color;
54 };
55
56 GLenum mnit_opengles_error_code;
57
58 struct mnit_opengles_Texture *mnit_opengles_load_image(
59 const uint_least32_t *pixels, int width, int height,
60 int width_pow2, int height_pow2, int has_alpha);
61 `}
62
63 in "C" `{
64 extern NativeWindowType mnit_window;
65 extern EGLNativeDisplayType mnit_native_display;
66
67 GLfloat mnit_opengles_vertices[6][3] =
68 {
69 {0.0f, 1.0f, 0.0f},
70 {1.0f, 1.0f, 0.0f},
71 {0.0f, 0.0f, 0.0f},
72 {1.0f, 0.0f, 0.0f},
73 };
74 GLfloat mnit_opengles_texture[6][2] =
75 {
76 {0.0f, 0.0f},
77 {0.0f, 1.0f},
78 {1.0f, 1.0f},
79 {0.0f, 0.0f},
80 {1.0f, 1.0f},
81 {1.0f, 0.0f}
82 };
83
84 struct mnit_opengles_Texture *mnit_opengles_load_image(
85 const uint_least32_t *pixels, int width, int height,
86 int width_pow2, int height_pow2, int has_alpha)
87 {
88 struct mnit_opengles_Texture *image = malloc(sizeof(struct mnit_opengles_Texture));
89 int format = has_alpha? GL_RGBA: GL_RGB;
90
91 image->width = width;
92 image->height = height;
93 image->center_x = width/2;
94 image->center_y = height/2;
95 image->scale = 1.0f;
96 image->blended = has_alpha;
97
98 image->src_xo = 0;
99 image->src_yo = 0;
100 image->src_xi = ((float)width)/width_pow2;
101 image->src_yi = ((float)height)/height_pow2;
102
103 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
104 PRINT_ERROR("error loading image after malloc: %i", mnit_opengles_error_code);
105 }
106
107 glGenTextures(1, &image->texture);
108
109 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
110 PRINT_ERROR("error loading image after glGenTextures: %i", mnit_opengles_error_code);
111 }
112
113 glBindTexture(GL_TEXTURE_2D, image->texture);
114
115 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
116 PRINT_ERROR("error loading image glBindTexture: %i", mnit_opengles_error_code);
117 }
118
119 glTexImage2D( GL_TEXTURE_2D, 0, format, width_pow2, height_pow2,
120 0, format, GL_UNSIGNED_BYTE, (GLvoid*)pixels);
121
122 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
123 PRINT_ERROR("error loading image after glTexImage2D: %i", mnit_opengles_error_code);
124 }
125
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
127
128 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
129 PRINT_ERROR("error loading image after gtTexParameter: %i", mnit_opengles_error_code);
130 }
131
132 return image;
133 }
134 `}
135
136 # OpenGL ES1 display
137 # Uses 3d hardware optimization
138 class Opengles1Display
139 super Display
140
141 redef type I: Opengles1Image
142
143 init do extern_init
144 fun midway_init( format: Int ) do end
145 fun extern_init: Bool is extern import midway_init `{
146 /* initialize OpenGL ES and EGL */
147 const EGLint attribs[] = {
148 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
149 EGL_BLUE_SIZE, 8,
150 EGL_GREEN_SIZE, 8,
151 EGL_RED_SIZE, 8,
152 EGL_NONE
153 };
154 EGLint w, h, dummy, format;
155 EGLint numConfigs;
156 EGLConfig config;
157 EGLSurface surface;
158 EGLContext context;
159
160 EGLDisplay display = eglGetDisplay(mnit_native_display);
161 if ( display == EGL_NO_DISPLAY) {
162 PRINT_ERROR("Unable to eglGetDisplay");
163 return -1;
164 }
165
166 if ( eglInitialize(display, 0, 0) == EGL_FALSE) {
167 PRINT_ERROR("Unable to eglInitialize");
168 return -1;
169 }
170
171 if ( eglChooseConfig(display, attribs, &config, 1, &numConfigs) == EGL_FALSE) {
172 PRINT_ERROR("Unable to eglChooseConfig");
173 return -1;
174 }
175
176 if ( numConfigs == 0 ) {
177 PRINT_ERROR("No configs available for egl");
178 return -1;
179 }
180
181 if ( eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format) == EGL_FALSE) {
182 PRINT_ERROR("Unable to eglGetConfigAttrib");
183 return -1;
184 }
185
186 /* Used by Android to set buffer geometry */
187 Opengles1Display_midway_init(recv, format);
188
189 surface = eglCreateWindowSurface(display, config, mnit_window, NULL);
190 context = eglCreateContext(display, config, NULL, NULL);
191
192 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
193 PRINT_ERROR("Unable to eglMakeCurrent");
194 return -1;
195 }
196
197 eglQuerySurface(display, surface, EGL_WIDTH, &w);
198 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
199
200 mnit_display = display;
201 mnit_context = context;
202 mnit_surface = surface;
203 mnit_config = config;
204 mnit_width = w;
205 mnit_height = h;
206
207 glViewport(0, 0, mnit_width, mnit_height);
208 glMatrixMode(GL_PROJECTION);
209 glLoadIdentity();
210 glOrthof(0.0f, w, h, 0.0f, 0.0f, 1.0f);
211 glMatrixMode(GL_MODELVIEW);
212
213 glFrontFace( GL_CW );
214
215 return 0;
216 `}
217
218 fun close is extern `{
219 if ( mnit_display != EGL_NO_DISPLAY) {
220 eglMakeCurrent( mnit_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
221 if ( mnit_context != EGL_NO_CONTEXT) {
222 eglDestroyContext( mnit_display, mnit_context );
223 }
224 if ( mnit_surface != EGL_NO_SURFACE) {
225 eglDestroySurface( mnit_display, mnit_surface );
226 }
227 eglTerminate( mnit_display);
228 }
229 mnit_display = EGL_NO_DISPLAY;
230 mnit_context = EGL_NO_CONTEXT;
231 mnit_surface = EGL_NO_SURFACE;
232 `}
233
234 redef fun begin is extern `{
235 glClear(GL_COLOR_BUFFER_BIT);
236 glLoadIdentity();
237 `}
238
239 redef fun width: Int is extern `{
240 return mnit_width;
241 `}
242 redef fun height: Int is extern `{
243 return mnit_height;
244 `}
245
246 redef fun finish is extern `{
247 eglSwapBuffers( mnit_display, mnit_surface );
248 `}
249
250 redef fun set_viewport( x, y, w, h ) is extern `{
251 glLoadIdentity();
252 glViewport(0,0, mnit_width, mnit_height );
253 glMatrixMode(GL_PROJECTION);
254 glLoadIdentity();
255 glOrthof(x, x+w, y+h, y, 0.0f, 1.0f);
256 glMatrixMode(GL_MODELVIEW);
257 glFrontFace( GL_CW );
258 `}
259
260 redef fun blit(image, x, y) do native_blit(image, x.to_f, y.to_f)
261
262 private fun native_blit(image: Opengles1Image, x, y: Float) `{
263 GLfloat texture_coord[4][2] =
264 {
265 {image->src_xo, image->src_yi},
266 {image->src_xi, image->src_yi},
267 {image->src_xo, image->src_yo},
268 {image->src_xi, image->src_yo}
269 };
270
271 glLoadIdentity();
272
273 glBindTexture(GL_TEXTURE_2D, image->texture);
274
275 glEnableClientState(GL_VERTEX_ARRAY);
276 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
277 glTranslatef( x, y, 0.0f );
278 glScalef( image->width*image->scale, image->height*image->scale, 1.0f );
279
280 if ( image->blended ) {
281 glEnable(GL_BLEND);
282 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
283 }
284
285 glEnable(GL_TEXTURE_2D);
286 glDisable(GL_DEPTH_TEST);
287
288 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices);
289 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
290
291 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
292
293 glDisableClientState(GL_VERTEX_ARRAY);
294 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
295 if ( image->blended ) glDisable(GL_BLEND);
296 glDisable(GL_TEXTURE_2D);
297
298 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
299 PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
300 }
301 `}
302
303 redef fun blit_centered(img, x, y)
304 do
305 x = x.sub(img.center_x)
306 y = y.sub(img.center_y)
307 blit(img, x, y)
308 end
309
310 redef fun blit_rotated(image, x, y, angle) do native_blit_rotated(image, x.to_f, y.to_f, angle)
311
312 private fun native_blit_rotated(image: Opengles1Image, x, y, angle: Float) `{
313 GLfloat texture_coord[4][2] =
314 {
315 {image->src_xo, image->src_yi},
316 {image->src_xi, image->src_yi},
317 {image->src_xo, image->src_yo},
318 {image->src_xi, image->src_yo}
319 };
320
321 glLoadIdentity();
322
323 glBindTexture(GL_TEXTURE_2D, image->texture);
324
325 glEnableClientState(GL_VERTEX_ARRAY);
326 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
327 glTranslatef( x, y, 0.0f );
328 glRotatef( angle*180.0f/3.14156f, 0, 0, 1.0f );
329 glTranslatef( image->width*image->scale/-2, image->height*image->scale/-2, 0.0f );
330 glScalef( image->width*image->scale, image->height*image->scale, 1.0f );
331 if ( image->blended ) {
332 glEnable(GL_BLEND);
333 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
334 }
335 glEnable(GL_TEXTURE_2D);
336 glDisable(GL_DEPTH_TEST);
337
338 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices);
339 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
340
341 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
342
343 glDisableClientState(GL_VERTEX_ARRAY);
344 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
345 if ( image->blended ) glDisable(GL_BLEND);
346 glDisable(GL_TEXTURE_2D);
347
348 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
349 PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
350 }
351 `}
352
353 # a = top left, b = bottom left, c = bottom right, d = top right
354 redef fun blit_stretched(image, ax, ay, bx, by, cx, cy, dx, dy)
355 do
356 native_blit_stretched(image,
357 ax.to_f, ay.to_f, bx.to_f, by.to_f,
358 cx.to_f, cy.to_f, dx.to_f, dy.to_f)
359 end
360
361 private fun native_blit_stretched(image: I, ax, ay, bx, by, cx, cy, dx, dy: Float) `{
362 GLfloat texture_coord[4][2] =
363 {
364 {image->src_xo, image->src_yi},
365 {image->src_xi, image->src_yi},
366 {image->src_xo, image->src_yo},
367 {image->src_xi, image->src_yo}
368 };
369
370 GLfloat mnit_opengles_vertices_stretched[6][3] =
371 {
372 {bx, by, 0.0f},
373 {cx, cy, 0.0f},
374 {ax, ay, 0.0f},
375 {dx, dy, 0.0f},
376 };
377
378 glLoadIdentity();
379
380 glBindTexture(GL_TEXTURE_2D, image->texture);
381
382 glEnableClientState(GL_VERTEX_ARRAY);
383 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
384
385 if ( image->blended ) {
386 glEnable(GL_BLEND);
387 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
388 }
389
390 glEnable(GL_TEXTURE_2D);
391 glDisable(GL_DEPTH_TEST);
392
393 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices_stretched);
394 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
395
396 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
397
398 glDisableClientState(GL_VERTEX_ARRAY);
399 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
400 if ( image->blended ) glDisable(GL_BLEND);
401 glDisable(GL_TEXTURE_2D);
402
403 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
404 PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
405 }
406 `}
407
408 redef fun clear( r, g, b: Float ) is extern `{
409 glClearColor( r, g, b, 1.0 );
410 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
411 `}
412
413 fun clear_alpha( r, g, b, a: Float ) is extern `{
414 glClearColor( r, g, b, a );
415 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
416 `}
417
418 # Set the current color applied to all drawing
419 #
420 # require: r, g, b, a in [0.0 .. 1.0]
421 fun color(r, g, b, a: Float) `{ glColor4f(r, g, b, a); `}
422
423 # Reset the current color to opaque white
424 fun reset_color `{ glColor4f(1.0f, 1.0f, 1.0f, 1.0f); `}
425 end
426
427 extern class Opengles1Image in "C" `{struct mnit_opengles_Texture *`}
428 super Image
429
430 redef fun destroy is extern `{ free( recv ); `}
431
432 redef fun width: Int is extern `{ return recv->width; `}
433 redef fun height: Int is extern `{ return recv->height; `}
434
435 fun center_x: Int `{ return recv->center_x; `}
436 fun center_y: Int `{ return recv->center_y; `}
437
438 redef fun scale=( v: Float ) is extern `{
439 recv->scale = v;
440 recv->center_x = v*recv->width/2;
441 recv->center_y = v*recv->height/2;
442 `}
443 redef fun scale: Float is extern `{ return recv->scale; `}
444
445 redef fun blended=( v: Bool ) is extern `{ recv->blended = v; `}
446 redef fun blended: Bool is extern `{ return recv->blended; `}
447
448 # inherits scale and blend from source
449 redef fun subimage( x, y, w, h: Int ): Image is extern import Opengles1Image.as( Image ) `{
450 struct mnit_opengles_Texture* image =
451 malloc( sizeof( struct mnit_opengles_Texture ) );
452
453 image->texture = recv->texture;
454 image->width = w;
455 image->height = h;
456 image->center_x = recv->scale*w/2;
457 image->center_y = recv->scale*h/2;
458 image->scale = recv->scale;
459 image->blended = recv->blended;
460
461 float r_dx = recv->src_xi - recv->src_xo;
462 float r_dy = recv->src_yi - recv->src_yo;
463 image->src_xo = recv->src_xo + ((float)x)/recv->width*r_dx;
464 image->src_yo = recv->src_yo + ((float)y)/recv->height*r_dy;
465 image->src_xi = recv->src_xo + ((float)x+w)/recv->width*r_dx;
466 image->src_yi = recv->src_yo + ((float)y+h)/recv->height*r_dy;
467
468 return Opengles1Image_as_Image( image );
469 `}
470 end