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", "egl")
26 EGLDisplay mnit_display;
27 EGLSurface mnit_surface;
28 EGLContext mnit_context;
29 EGLConfig mnit_config;
33 struct mnit_opengles_Texture {
36 /* offsets on source texture */
37 float src_xo, src_yo, src_xi, src_yi;
39 /* destination width and height */
42 /* may vary depending on scaling */
43 int center_x, center_y;
49 struct mnit_opengles_DrawableTexture {
50 struct mnit_opengles_Texture super;
56 GLenum mnit_opengles_error_code;
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);
64 extern NativeWindowType mnit_window;
65 extern EGLNativeDisplayType mnit_native_display;
67 GLfloat mnit_opengles_vertices[6][3] =
74 GLfloat mnit_opengles_texture[6][2] =
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)
88 struct mnit_opengles_Texture *image = malloc(sizeof(struct mnit_opengles_Texture));
89 int format = has_alpha? GL_RGBA: GL_RGB;
92 image->height = height;
93 image->center_x = width/2;
94 image->center_y = height/2;
96 image->blended = has_alpha;
100 image->src_xi = ((float)width)/width_pow2;
101 image->src_yi = ((float)height)/height_pow2;
103 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
104 PRINT_ERROR("error loading image after malloc: %i", mnit_opengles_error_code);
107 glGenTextures(1, &image->texture);
109 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
110 PRINT_ERROR("error loading image after glGenTextures: %i", mnit_opengles_error_code);
113 glBindTexture(GL_TEXTURE_2D, image->texture);
115 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
116 PRINT_ERROR("error loading image glBindTexture: %i", mnit_opengles_error_code);
119 glTexImage2D( GL_TEXTURE_2D, 0, format, width_pow2, height_pow2,
120 0, format, GL_UNSIGNED_BYTE, (GLvoid*)pixels);
122 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
123 PRINT_ERROR("error loading image after glTexImage2D: %i", mnit_opengles_error_code);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
129 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
130 PRINT_ERROR("error loading image after gtTexParameter: %i", mnit_opengles_error_code);
138 # Uses 3d hardware optimization
139 class Opengles1Display
142 redef type I
: Opengles1Image
145 fun midway_init
( format
: Int ) do end
146 fun extern_init
: Bool is extern import midway_init
`{
147 /* initialize OpenGL ES and EGL */
148 const EGLint attribs[] = {
149 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
155 EGLint w, h, dummy, format;
161 EGLDisplay display = eglGetDisplay(mnit_native_display);
162 if ( display == EGL_NO_DISPLAY) {
163 PRINT_ERROR("Unable to eglGetDisplay");
167 if ( eglInitialize(display, 0, 0) == EGL_FALSE) {
168 PRINT_ERROR("Unable to eglInitialize");
172 if ( eglChooseConfig(display, attribs, &config, 1, &numConfigs) == EGL_FALSE) {
173 PRINT_ERROR("Unable to eglChooseConfig");
177 if ( numConfigs == 0 ) {
178 PRINT_ERROR("No configs available for egl");
182 if ( eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format) == EGL_FALSE) {
183 PRINT_ERROR("Unable to eglGetConfigAttrib");
187 /* Used by Android to set buffer geometry */
188 Opengles1Display_midway_init(recv, format);
190 surface = eglCreateWindowSurface(display, config, mnit_window, NULL);
191 context = eglCreateContext(display, config, NULL, NULL);
193 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
194 PRINT_ERROR("Unable to eglMakeCurrent");
198 eglQuerySurface(display, surface, EGL_WIDTH, &w);
199 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
201 mnit_display = display;
202 mnit_context = context;
203 mnit_surface = surface;
204 mnit_config = config;
208 glViewport(0, 0, mnit_width, mnit_height);
209 glMatrixMode(GL_PROJECTION);
211 glOrthof(0.0f, w, h, 0.0f, 0.0f, 1.0f);
212 glMatrixMode(GL_MODELVIEW);
214 glFrontFace( GL_CW );
219 fun close
is extern `{
220 if ( mnit_display != EGL_NO_DISPLAY) {
221 eglMakeCurrent( mnit_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
222 if ( mnit_context != EGL_NO_CONTEXT) {
223 eglDestroyContext( mnit_display, mnit_context );
225 if ( mnit_surface != EGL_NO_SURFACE) {
226 eglDestroySurface( mnit_display, mnit_surface );
228 eglTerminate( mnit_display);
230 mnit_display = EGL_NO_DISPLAY;
231 mnit_context = EGL_NO_CONTEXT;
232 mnit_surface = EGL_NO_SURFACE;
235 redef fun begin
is extern `{
236 glClear(GL_COLOR_BUFFER_BIT);
240 redef fun width
: Int is extern `{
243 redef fun height
: Int is extern `{
247 redef fun finish
is extern `{
248 eglSwapBuffers( mnit_display, mnit_surface );
251 redef fun set_viewport
( x
, y
, w
, h
) is extern `{
253 glViewport(0,0, mnit_width, mnit_height );
254 glMatrixMode(GL_PROJECTION);
256 glOrthof(x, x+w, y+h, y, 0.0f, 1.0f);
257 glMatrixMode(GL_MODELVIEW);
258 glFrontFace( GL_CW );
261 redef fun blit
(image
, x
, y
) do native_blit
(image
, x
.to_f
, y
.to_f
)
263 private fun native_blit
(image
: Opengles1Image, x
, y
: Float) `{
264 GLfloat texture_coord[4][2] =
266 {image->src_xo, image->src_yi},
267 {image->src_xi, image->src_yi},
268 {image->src_xo, image->src_yo},
269 {image->src_xi, image->src_yo}
274 glBindTexture(GL_TEXTURE_2D, image->texture);
276 glEnableClientState(GL_VERTEX_ARRAY);
277 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
278 glTranslatef( x, y, 0.0f );
279 glScalef( image->width*image->scale, image->height*image->scale, 1.0f );
281 if ( image->blended ) {
283 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
286 glEnable(GL_TEXTURE_2D);
287 glDisable(GL_DEPTH_TEST);
289 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices);
290 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
292 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
294 glDisableClientState(GL_VERTEX_ARRAY);
295 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
296 if ( image->blended ) glDisable(GL_BLEND);
297 glDisable(GL_TEXTURE_2D);
299 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
300 PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
304 redef fun blit_centered
(img
, x
, y
)
306 x
= x
.sub
(img
.center_x
)
307 y
= y
.sub
(img
.center_y
)
311 redef fun blit_rotated
(image
, x
, y
, angle
) do native_blit_rotated
(image
, x
.to_f
, y
.to_f
, angle
)
313 private fun native_blit_rotated
(image
: Opengles1Image, x
, y
, angle
: Float) `{
314 GLfloat texture_coord[4][2] =
316 {image->src_xo, image->src_yi},
317 {image->src_xi, image->src_yi},
318 {image->src_xo, image->src_yo},
319 {image->src_xi, image->src_yo}
324 glBindTexture(GL_TEXTURE_2D, image->texture);
326 glEnableClientState(GL_VERTEX_ARRAY);
327 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
328 glTranslatef( x, y, 0.0f );
329 glRotatef( angle*180.0f/3.14156f, 0, 0, 1.0f );
330 glTranslatef( image->width*image->scale/-2, image->height*image->scale/-2, 0.0f );
331 glScalef( image->width*image->scale, image->height*image->scale, 1.0f );
332 if ( image->blended ) {
334 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
336 glEnable(GL_TEXTURE_2D);
337 glDisable(GL_DEPTH_TEST);
339 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices);
340 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
342 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
344 glDisableClientState(GL_VERTEX_ARRAY);
345 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
346 if ( image->blended ) glDisable(GL_BLEND);
347 glDisable(GL_TEXTURE_2D);
349 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
350 PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
354 # a = top left, b = bottom left, c = bottom right, d = top right
355 redef fun blit_stretched
(image
, ax
, ay
, bx
, by
, cx
, cy
, dx
, dy
)
357 native_blit_stretched
(image
,
358 ax
.to_f
, ay
.to_f
, bx
.to_f
, by
.to_f
,
359 cx
.to_f
, cy
.to_f
, dx
.to_f
, dy
.to_f
)
362 private fun native_blit_stretched
(image
: I
, ax
, ay
, bx
, by
, cx
, cy
, dx
, dy
: Float) `{
363 GLfloat texture_coord[4][2] =
365 {image->src_xo, image->src_yi},
366 {image->src_xi, image->src_yi},
367 {image->src_xo, image->src_yo},
368 {image->src_xi, image->src_yo}
371 GLfloat mnit_opengles_vertices_stretched[6][3] =
381 glBindTexture(GL_TEXTURE_2D, image->texture);
383 glEnableClientState(GL_VERTEX_ARRAY);
384 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
386 if ( image->blended ) {
388 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
391 glEnable(GL_TEXTURE_2D);
392 glDisable(GL_DEPTH_TEST);
394 glVertexPointer(3, GL_FLOAT, 0, mnit_opengles_vertices_stretched);
395 glTexCoordPointer(2, GL_FLOAT, 0, texture_coord );
397 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
399 glDisableClientState(GL_VERTEX_ARRAY);
400 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
401 if ( image->blended ) glDisable(GL_BLEND);
402 glDisable(GL_TEXTURE_2D);
404 if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
405 PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
409 redef fun clear
( r
, g
, b
: Float ) is extern `{
410 glClearColor( r, g, b, 1.0 );
411 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
414 fun clear_alpha
( r
, g
, b
, a
: Float ) is extern `{
415 glClearColor( r, g, b, a );
416 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
419 # Set the current color applied to all drawing
421 # require: r, g, b, a in [0.0 .. 1.0]
422 fun color
(r
, g
, b
, a
: Float) `{ glColor4f(r, g, b, a); `}
424 # Reset the current color to opaque white
425 fun reset_color `{ glColor4f(1.0f, 1.0f, 1.0f, 1.0f); `}
428 extern class Opengles1Image in "C" `{struct mnit_opengles_Texture *`}
431 redef fun destroy is extern `{ free( recv ); `}
433 redef fun width
: Int is extern `{ return recv->width; `}
434 redef fun height: Int is extern `{ return recv->height; `}
436 fun center_x
: Int `{ return recv->center_x; `}
437 fun center_y: Int `{ return recv->center_y; `}
439 redef fun scale
=( v
: Float ) is extern `{
441 recv->center_x = v*recv->width/2;
442 recv->center_y = v*recv->height/2;
444 redef fun scale
: Float is extern `{ return recv->scale; `}
446 redef fun blended=( v: Bool ) is extern `{ recv->blended = v; `}
447 redef fun blended
: Bool is extern `{ return recv->blended; `}
449 # inherits scale and blend from source
450 redef fun subimage( x, y, w, h: Int ): Image is extern import Opengles1Image.as( Image ) `{
451 struct mnit_opengles_Texture
* image
=
452 malloc
( sizeof
( struct mnit_opengles_Texture
) );
454 image-
>texture
= recv-
>texture
;
457 image-
>center_x
= recv-
>scale
*w
/2;
458 image-
>center_y
= recv-
>scale
*h
/2;
459 image-
>scale
= recv-
>scale
;
460 image-
>blended
= recv-
>blended
;
462 float r_dx
= recv-
>src_xi
- recv-
>src_xo
;
463 float r_dy
= recv-
>src_yi
- recv-
>src_yo
;
464 image-
>src_xo
= recv-
>src_xo
+ ((float
)x
)/recv-
>width
*r_dx
;
465 image-
>src_yo
= recv-
>src_yo
+ ((float
)y
)/recv-
>height
*r_dy
;
466 image-
>src_xi
= recv-
>src_xo
+ ((float
)x
+w
)/recv-
>width
*r_dx
;
467 image-
>src_yi
= recv-
>src_yo
+ ((float
)y
+h
)/recv-
>height
*r_dy
;
469 return Opengles1Image_as_Image( image
);