1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # OpenGL ES1 general support (most of it)
18 module opengles1
is pkgconfig
("glesv1_cm", "x11", "egl")
25 #define GL_GLEXT_PROTOTYPES 1
26 #include <GLES/glext.h>
29 EGLDisplay mnit_display;
30 EGLSurface mnit_surface;
31 EGLContext mnit_context;
32 EGLConfig mnit_config;
37 struct mnit_opengles_Texture {
40 /* offsets on source texture */
41 float src_xo, src_yo, src_xi, src_yi;
43 /* destination width and height */
46 /* may vary depending on scaling */
47 int center_x, center_y;
53 struct mnit_opengles_DrawableTexture {
54 struct mnit_opengles_Texture super;
60 GLenum mnit_opengles_error_code;
62 struct mnit_opengles_Texture *mnit_opengles_load_image( const uint_least32_t *pixels, int width, int height, int has_alpha );
66 #define LOGW(...) ((void)fprintf(stderr, "# warn: %s (%i)\n", __VA_ARGS__))
68 #define LOGI(...) ((void)fprintf(stderr, "# info: %s (%i)\n", __VA_ARGS__))
70 #define LOGI(...) (void)0
73 extern NativeWindowType mnit_window;
74 extern EGLNativeDisplayType mnit_native_display;
76 GLfloat mnit_opengles_vertices[6][3] =
83 GLfloat mnit_opengles_texture[6][2] =
93 struct mnit_opengles_Texture *mnit_opengles_load_image( const uint_least32_t *pixels, int width, int height, int has_alpha )
95 struct mnit_opengles_Texture *image = malloc(sizeof(struct mnit_opengles_Texture));
96 int format = has_alpha? GL_RGBA: GL_RGB;
99 image->height = height;
100 image->center_x = width/2;
101 image->center_y = height/2;
103 image->blended = has_alpha;
110 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
111 LOGW("error loading image after malloc", mnit_opengles_error_code);
114 glGenTextures(1, &image->texture);
116 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
117 LOGW("error loading image after glGenTextures", mnit_opengles_error_code);
120 glBindTexture(GL_TEXTURE_2D, image->texture);
122 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
123 LOGW("error loading image glBindTexture", mnit_opengles_error_code);
126 glTexImage2D( GL_TEXTURE_2D, 0, format, width, height,
127 0, format, GL_UNSIGNED_BYTE, (GLvoid*)pixels);
129 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
130 LOGW("error loading image after glTexImage2D", mnit_opengles_error_code);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
135 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
136 LOGW("error loading image after gtTexParameter", mnit_opengles_error_code);
144 # Uses 3d hardware optimization
145 class Opengles1Display
148 redef type I
: Opengles1Image
151 fun midway_init
( format
: Int ) do end
152 fun extern_init
: Bool is extern import midway_init
`{
153 /* initialize OpenGL ES and EGL */
154 const EGLint attribs[] = {
155 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
161 EGLint w, h, dummy, format;
167 EGLDisplay display = eglGetDisplay(mnit_native_display);
168 if ( display == EGL_NO_DISPLAY) {
169 LOGW("Unable to eglGetDisplay", 0);
173 if ( eglInitialize(display, 0, 0) == EGL_FALSE) {
174 LOGW("Unable to eglInitialize", 0);
178 if ( eglChooseConfig(display, attribs, &config, 1, &numConfigs) == EGL_FALSE) {
179 LOGW("Unable to eglChooseConfig", 0);
183 if ( numConfigs == 0 ) {
184 LOGW("No configs available for egl", 0);
188 if ( eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format) == EGL_FALSE) {
189 LOGW("Unable to eglGetConfigAttrib", 0);
193 /* Used by Android to set buffer geometry */
194 Opengles1Display_midway_init(recv, format);
196 surface = eglCreateWindowSurface(display, config, mnit_window, NULL);
197 context = eglCreateContext(display, config, NULL, NULL);
199 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
200 LOGW("Unable to eglMakeCurrent", 0);
204 eglQuerySurface(display, surface, EGL_WIDTH, &w);
205 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
207 mnit_display = display;
208 mnit_context = context;
209 mnit_surface = surface;
210 mnit_config = config;
215 LOGI("surface", (int)surface);
216 LOGI("display", (int)display);
220 glViewport(0, 0, mnit_width, mnit_height);
221 glMatrixMode(GL_PROJECTION);
223 glOrthof(0.0f, w, h, 0.0f, 0.0f, 1.0f);
224 glMatrixMode(GL_MODELVIEW);
226 glFrontFace( GL_CW );
231 fun close
is extern `{
232 if ( mnit_display != EGL_NO_DISPLAY) {
233 eglMakeCurrent( mnit_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
234 if ( mnit_context != EGL_NO_CONTEXT) {
235 eglDestroyContext( mnit_display, mnit_context );
237 if ( mnit_surface != EGL_NO_SURFACE) {
238 eglDestroySurface( mnit_display, mnit_surface );
240 eglTerminate( mnit_display);
242 mnit_display = EGL_NO_DISPLAY;
243 mnit_context = EGL_NO_CONTEXT;
244 mnit_surface = EGL_NO_SURFACE;
247 redef fun begin
is extern `{
248 glClear(GL_COLOR_BUFFER_BIT);
252 redef fun width
: Int is extern `{
255 redef fun height
: Int is extern `{
259 redef fun finish
is extern `{
260 eglSwapBuffers( mnit_display, mnit_surface );
263 fun set_as_target
is extern `{
264 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
267 redef fun set_viewport
( x
, y
, w
, h
) is extern `{
269 glViewport(0,0, mnit_width, mnit_height );
270 glMatrixMode(GL_PROJECTION);
272 glOrthof(x, x+w, y+h, y, 0.0f, 1.0f);
273 mnit_zoom = ((float)w)/mnit_width;
274 glMatrixMode(GL_MODELVIEW);
275 glFrontFace( GL_CW );
278 redef fun blit
( image
, x
, y
) is extern `{
279 GLfloat texture_coord[4][2] =
281 {image->src_xo, image->src_yi},
282 {image->src_xi, image->src_yi},
283 {image->src_xo, image->src_yo},
284 {image->src_xi, image->src_yo}
289 glBindTexture(GL_TEXTURE_2D, image->texture);
291 glEnableClientState(GL_VERTEX_ARRAY);
292 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
293 glTranslatef( x, y, 0.0f );
294 glScalef( image->width*image->scale, image->height*image->scale, 1.0f );
296 if ( image->blended ) {
298 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
301 glEnable(GL_TEXTURE_2D);
302 glDisable(GL_DEPTH_TEST);
304 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices);
305 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
307 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
309 glDisableClientState(GL_VERTEX_ARRAY);
310 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
311 if ( image->blended ) glDisable(GL_BLEND);
312 glDisable(GL_TEXTURE_2D);
314 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
315 LOGW("error drawing", mnit_opengles_error_code);
319 redef fun blit_centered
( img
, x
, y
)
326 redef fun blit_rotated
( image
, x
, y
, angle
) is extern `{
327 GLfloat texture_coord[4][2] =
329 {image->src_xo, image->src_yi},
330 {image->src_xi, image->src_yi},
331 {image->src_xo, image->src_yo},
332 {image->src_xi, image->src_yo}
337 glBindTexture(GL_TEXTURE_2D, image->texture);
339 glEnableClientState(GL_VERTEX_ARRAY);
340 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
341 glTranslatef( x, y, 0.0f );
342 glRotatef( angle*180.0f/3.14156f, 0, 0, 1.0f );
343 glTranslatef( image->width*image->scale/-2, image->height*image->scale/-2, 0.0f );
344 glScalef( image->width*image->scale, image->height*image->scale, 1.0f );
345 if ( image->blended ) {
347 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
349 glEnable(GL_TEXTURE_2D);
350 glDisable(GL_DEPTH_TEST);
352 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices);
353 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
355 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
357 glDisableClientState(GL_VERTEX_ARRAY);
358 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
359 if ( image->blended ) glDisable(GL_BLEND);
360 glDisable(GL_TEXTURE_2D);
362 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
363 LOGW("error drawing", mnit_opengles_error_code);
367 # a = top left, b = bottom left, c = bottom right, d = top right
368 redef fun blit_stretched
( image
, ax
, ay
, bx
, by
, cx
, cy
, dx
, dy
) is extern `{
369 GLfloat texture_coord[4][2] =
371 {image->src_xo, image->src_yi},
372 {image->src_xi, image->src_yi},
373 {image->src_xo, image->src_yo},
374 {image->src_xi, image->src_yo}
377 GLfloat mnit_opengles_vertices_stretched[6][3] =
387 glBindTexture(GL_TEXTURE_2D, image->texture);
389 glEnableClientState(GL_VERTEX_ARRAY);
390 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
392 if ( image->blended ) {
394 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
397 glEnable(GL_TEXTURE_2D);
398 glDisable(GL_DEPTH_TEST);
400 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices_stretched);
401 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
403 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
405 glDisableClientState(GL_VERTEX_ARRAY);
406 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
407 if ( image->blended ) glDisable(GL_BLEND);
408 glDisable(GL_TEXTURE_2D);
410 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
411 LOGW("error drawing", mnit_opengles_error_code);
415 redef fun clear
( r
, g
, b
: Float ) is extern `{
416 glClearColor( r, g, b, 1.0 );
417 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
420 fun clear_alpha
( r
, g
, b
, a
: Float ) is extern `{
421 glClearColor( r, g, b, a );
422 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
425 # Set the current color applied to all drawing
427 # require: r, g, b, a in [0.0 .. 1.0]
428 fun color
(r
, g
, b
, a
: Float) `{ glColor4f(r, g, b, a); `}
430 # Reset the current color to opaque white
431 fun reset_color `{ glColor4f(1.0f, 1.0f, 1.0f, 1.0f); `}
434 extern class Opengles1Image in "C" `{struct mnit_opengles_Texture *`}
437 redef fun destroy is extern `{ free( recv ); `}
439 redef fun width
: Int is extern `{ return recv->width; `}
440 redef fun height: Int is extern `{ return recv->height; `}
442 fun center_x
: Int `{ return recv->center_x; `}
443 fun center_y: Int `{ return recv->center_y; `}
445 redef fun scale
=( v
: Float ) is extern `{
447 recv->center_x = v*recv->width/2;
448 recv->center_y = v*recv->height/2;
450 redef fun scale
: Float is extern `{ return recv->scale; `}
452 redef fun blended=( v: Bool ) is extern `{ recv->blended = v; `}
453 redef fun blended
: Bool is extern `{ return recv->blended; `}
455 # inherits scale and blend from source
456 redef fun subimage( x, y, w, h: Int ): Image is extern import Opengles1Image.as( Image ) `{
457 struct mnit_opengles_Texture
* image
=
458 malloc
( sizeof
( struct mnit_opengles_Texture
) );
460 image-
>texture
= recv-
>texture
;
463 image-
>center_x
= recv-
>scale
*w
/2;
464 image-
>center_y
= recv-
>scale
*h
/2;
465 image-
>scale
= recv-
>scale
;
466 image-
>blended
= recv-
>blended
;
468 image-
>src_xo
= ((float
)x
)/recv-
>width
;
469 image-
>src_yo
= ((float
)y
)/recv-
>height
;
470 image-
>src_xi
= ((float
)x
+w
)/recv-
>width
;
471 image-
>src_yi
= ((float
)y
+h
)/recv-
>height
;
473 return Opengles1Image_as_Image( image
);
477 # FIXME this class is broken
478 extern class Opengles1DrawableImage in "C" `{struct mnit_opengles_DrawableTexture*`}
480 new ( w
, h
: Int ) is extern `{
481 struct mnit_opengles_DrawableTexture *image =
482 malloc( sizeof(struct mnit_opengles_DrawableTexture) );
485 glGenTextures(1, &image->super.texture);
486 glBindTexture(GL_TEXTURE_2D, image->super.texture);
487 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
488 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
489 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
490 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
491 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
492 /* glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automatic mipmap generation included in OpenGL v1.4c */
493 glBindTexture(GL_TEXTURE_2D, 0);
496 glGenFramebuffersOES( 1, &image->fbo );
497 glBindFramebufferOES( GL_FRAMEBUFFER_OES, image->fbo );
498 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
499 GL_COLOR_ATTACHMENT0_OES,
501 image->super.texture,
505 glGenRenderbuffersOES(1, &image->depth);
506 glBindRenderbufferOES(GL_RENDERBUFFER_OES, image->depth);
507 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES,
509 glBindRenderbufferOES(GL_RENDERBUFFER_OES, 0);
510 glFramebufferRenderbufferOES( GL_FRAMEBUFFER_OES,
511 GL_DEPTH_ATTACHMENT_OES,
515 /* tex framebuffer */
516 glGenRenderbuffersOES(1, &image->color);
517 glBindRenderbufferOES(GL_RENDERBUFFER_OES, image->color);
518 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, w, h);
519 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, image->color );
521 if ( glCheckFramebufferStatusOES( GL_FRAMEBUFFER_OES ) != GL_FRAMEBUFFER_COMPLETE_OES )
523 LOGW("framebuffer not set", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
527 image->super.width = w;
528 image->super.height = h;
529 image->super.center_x = w/2;
530 image->super.center_y = h/2;
531 image->super.scale = 1.0f;
532 image->super.blended = 0;
534 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
535 LOGW("gl error in Opengles1DrawableImage::init", mnit_opengles_error_code);
542 fun set_as_target
is extern `{
543 LOGI( "sat %i", recv->fbo );
544 glBindFramebufferOES(GL_FRAMEBUFFER_OES, recv->fbo);
545 if (glGetError() != GL_NO_ERROR) LOGW("gl error", 0);
546 /*glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES,&recv->fbo);
547 if (glGetError() != GL_NO_ERROR) LOGW( "gl error a");*/
548 glViewport(0, 0, recv->super.width, recv->super.height);
550 glMatrixMode(GL_PROJECTION);
552 glOrthof(0.0f, recv->super.width, recv->super.height, 0.0f, 0.0f, 1.0f);
553 glMatrixMode(GL_MODELVIEW);
554 glFrontFace( GL_CW );
556 glClearColor( 0.0f, 1.0f, 1.0f, 1.0f );
557 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
560 fun unset_as_target
is extern `{
562 /*glBindTexture(GL_TEXTURE_2D, recv->super.texture);
563 glGenerateMipmapOES(GL_TEXTURE_2D);
564 glBindTexture(GL_TEXTURE_2D, 0);*/
565 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
566 if (glGetError() != GL_NO_ERROR) LOGW("gl error", 0);