2 * Exercise EGL API functions
5 #define EGL_EGLEXT_PROTOTYPES
8 #include <EGL/eglext.h>
18 #define PIXEL_CENTER(x) ((long)(x) + 0.5)
24 #define OPENGL_WIDTH 48
25 #define OPENGL_HEIGHT 13
28 GLenum rgb
, doubleBuffer
, windType
;
33 GLubyte OpenGL_bits
[] = {
34 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
35 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
36 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
37 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
38 0x3e, 0x8f, 0xb7, 0xf9, 0xfc, 0x01,
39 0x63, 0xdb, 0xb0, 0x8d, 0x0d, 0x00,
40 0x63, 0xdb, 0xb7, 0x8d, 0x0d, 0x00,
41 0x63, 0xdb, 0xb6, 0x8d, 0x0d, 0x00,
42 0x63, 0x8f, 0xf3, 0xcc, 0x0d, 0x00,
43 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0a,
44 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0e,
45 0x63, 0x00, 0x00, 0x8c, 0xed, 0x0e,
46 0x3e, 0x00, 0x00, 0xf8, 0x0c, 0x00,
50 static void Init(void)
57 static void Reshape(int width
, int height
)
61 windH
= (GLint
)height
;
65 static void RotateColorMask(void)
67 static GLint rotation
= 0;
69 rotation
= (rotation
+ 1) & 0x3;
72 glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
76 glColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
80 glColorMask(GL_TRUE
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
84 glColorMask(GL_TRUE
, GL_TRUE
, GL_FALSE
, GL_TRUE
);
91 static void Viewport(GLint row
, GLint column
)
95 boxW
= (windW
- (COLS
+ 1) * GAP
) / COLS
;
96 boxH
= (windH
- (ROWS
+ 1) * GAP
) / ROWS
;
98 x
= GAP
+ column
* (boxW
+ GAP
);
99 y
= GAP
+ row
* (boxH
+ GAP
);
101 glViewport(x
, y
, boxW
, boxH
);
103 glMatrixMode(GL_PROJECTION
);
105 glOrtho(-boxW
/2, boxW
/2, -boxH
/2, boxH
/2, 0.0, 1.0);
106 glMatrixMode(GL_MODELVIEW
);
108 glEnable(GL_SCISSOR_TEST
);
109 glScissor(x
, y
, boxW
, boxH
);
123 static float RGBMap
[9][3] = {
135 static void SetColor(int c
)
137 glColor3fv(RGBMap
[c
]);
140 static void Point(void)
145 SetColor(COLOR_WHITE
);
147 for (i
= 1; i
< 8; i
++) {
149 SetColor(COLOR_BLACK
+i
);
162 static void Lines(void)
168 glTranslatef(-12, 0, 0);
169 for (i
= 1; i
< 8; i
++) {
170 SetColor(COLOR_BLACK
+i
);
172 glVertex2i(-boxW
/4, -boxH
/4);
173 glVertex2i(boxW
/4, boxH
/4);
175 glTranslatef(4, 0, 0);
185 static void LineStrip(void)
188 glBegin(GL_LINE_STRIP
);
190 glVertex2f(PIXEL_CENTER(-boxW
/4), PIXEL_CENTER(-boxH
/4));
191 SetColor(COLOR_GREEN
);
192 glVertex2f(PIXEL_CENTER(-boxW
/4), PIXEL_CENTER(boxH
/4));
193 SetColor(COLOR_BLUE
);
194 glVertex2f(PIXEL_CENTER(boxW
/4), PIXEL_CENTER(boxH
/4));
195 SetColor(COLOR_WHITE
);
196 glVertex2f(PIXEL_CENTER(boxW
/4), PIXEL_CENTER(-boxH
/4));
199 glBegin(GL_LINE_STRIP
);
204 static void LineLoop(void)
207 glBegin(GL_LINE_LOOP
);
209 glVertex2f(PIXEL_CENTER(-boxW
/4), PIXEL_CENTER(-boxH
/4));
210 SetColor(COLOR_GREEN
);
211 glVertex2f(PIXEL_CENTER(-boxW
/4), PIXEL_CENTER(boxH
/4));
212 SetColor(COLOR_BLUE
);
213 glVertex2f(PIXEL_CENTER(boxW
/4), PIXEL_CENTER(boxH
/4));
214 SetColor(COLOR_WHITE
);
215 glVertex2f(PIXEL_CENTER(boxW
/4), PIXEL_CENTER(-boxH
/4));
218 glEnable(GL_LOGIC_OP
);
222 glBlendFunc(GL_ONE
, GL_ONE
);
224 SetColor(COLOR_MAGENTA
);
225 glBegin(GL_LINE_LOOP
);
226 glVertex2f(PIXEL_CENTER(-boxW
/8), PIXEL_CENTER(-boxH
/8));
227 glVertex2f(PIXEL_CENTER(-boxW
/8), PIXEL_CENTER(boxH
/8));
229 glBegin(GL_LINE_LOOP
);
230 glVertex2f(PIXEL_CENTER(-boxW
/8), PIXEL_CENTER(boxH
/8+5));
231 glVertex2f(PIXEL_CENTER(boxW
/8), PIXEL_CENTER(boxH
/8+5));
233 glDisable(GL_LOGIC_OP
);
236 SetColor(COLOR_GREEN
);
241 glBegin(GL_LINE_LOOP
);
246 static void Bitmap(void)
250 SetColor(COLOR_GREEN
);
251 glVertex2i(-boxW
/2, 0);
252 glVertex2i(boxW
/2, 0);
253 glVertex2i(0, -boxH
/2);
254 glVertex2i(0, boxH
/2);
257 glVertex2i(0, -3+OPENGL_HEIGHT
);
258 SetColor(COLOR_BLUE
);
260 glVertex2i(OPENGL_WIDTH
, -3);
263 SetColor(COLOR_GREEN
);
265 glPixelStorei(GL_UNPACK_LSB_FIRST
, GL_TRUE
);
266 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
269 glBitmap(OPENGL_WIDTH
, OPENGL_HEIGHT
, 0, 3, 0.0, 0.0, OpenGL_bits
);
272 static void Triangles(void)
275 glBegin(GL_TRIANGLES
);
276 SetColor(COLOR_GREEN
);
277 glVertex2i(-boxW
/4, -boxH
/4);
279 glVertex2i(-boxW
/8, -boxH
/16);
280 SetColor(COLOR_BLUE
);
281 glVertex2i(boxW
/8, -boxH
/16);
283 SetColor(COLOR_GREEN
);
284 glVertex2i(-boxW
/4, boxH
/4);
286 glVertex2i(-boxW
/8, boxH
/16);
287 SetColor(COLOR_BLUE
);
288 glVertex2i(boxW
/8, boxH
/16);
291 glBegin(GL_TRIANGLES
);
293 glVertex2i(-100, 100);
297 static void TriangleStrip(void)
300 glBegin(GL_TRIANGLE_STRIP
);
301 SetColor(COLOR_GREEN
);
302 glVertex2i(-boxW
/4, -boxH
/4);
304 glVertex2i(-boxW
/4, boxH
/4);
305 SetColor(COLOR_BLUE
);
306 glVertex2i(0, -boxH
/4);
307 SetColor(COLOR_WHITE
);
308 glVertex2i(0, boxH
/4);
309 SetColor(COLOR_CYAN
);
310 glVertex2i(boxW
/4, -boxH
/4);
311 SetColor(COLOR_YELLOW
);
312 glVertex2i(boxW
/4, boxH
/4);
315 glBegin(GL_TRIANGLE_STRIP
);
317 glVertex2i(-100, 100);
321 static void TriangleFan(void)
324 GLint x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
;
336 vx
[0][0] = x0
; vx
[0][1] = y1
;
337 vx
[1][0] = x0
; vx
[1][1] = y2
;
338 vx
[2][0] = x1
; vx
[2][1] = y3
;
339 vx
[3][0] = x2
; vx
[3][1] = y3
;
340 vx
[4][0] = x3
; vx
[4][1] = y2
;
341 vx
[5][0] = x3
; vx
[5][1] = y1
;
342 vx
[6][0] = x2
; vx
[6][1] = y0
;
343 vx
[7][0] = x1
; vx
[7][1] = y0
;
345 glBegin(GL_TRIANGLE_FAN
);
346 SetColor(COLOR_WHITE
);
348 for (i
= 0; i
< 8; i
++) {
349 SetColor(COLOR_WHITE
-i
);
354 glBegin(GL_TRIANGLE_FAN
);
356 glVertex2i(-100, 100);
360 static void Rect(void)
363 SetColor(COLOR_GREEN
);
364 glRecti(-boxW
/4, -boxH
/4, boxW
/4, boxH
/4);
367 static void PolygonFunc(void)
370 GLint x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
;
382 vx
[0][0] = x0
; vx
[0][1] = y1
;
383 vx
[1][0] = x0
; vx
[1][1] = y2
;
384 vx
[2][0] = x1
; vx
[2][1] = y3
;
385 vx
[3][0] = x2
; vx
[3][1] = y3
;
386 vx
[4][0] = x3
; vx
[4][1] = y2
;
387 vx
[5][0] = x3
; vx
[5][1] = y1
;
388 vx
[6][0] = x2
; vx
[6][1] = y0
;
389 vx
[7][0] = x1
; vx
[7][1] = y0
;
392 for (i
= 0; i
< 8; i
++) {
393 SetColor(COLOR_WHITE
-i
);
400 glVertex2i(100, 100);
404 static void Quads(void)
408 SetColor(COLOR_GREEN
);
409 glVertex2i(-boxW
/4, -boxH
/4);
411 glVertex2i(-boxW
/8, -boxH
/16);
412 SetColor(COLOR_BLUE
);
413 glVertex2i(boxW
/8, -boxH
/16);
414 SetColor(COLOR_WHITE
);
415 glVertex2i(boxW
/4, -boxH
/4);
417 SetColor(COLOR_GREEN
);
418 glVertex2i(-boxW
/4, boxH
/4);
420 glVertex2i(-boxW
/8, boxH
/16);
421 SetColor(COLOR_BLUE
);
422 glVertex2i(boxW
/8, boxH
/16);
423 SetColor(COLOR_WHITE
);
424 glVertex2i(boxW
/4, boxH
/4);
429 glVertex2i(100, 100);
430 glVertex2i(-100, 100);
434 static void QuadStrip(void)
437 glBegin(GL_QUAD_STRIP
);
438 SetColor(COLOR_GREEN
);
439 glVertex2i(-boxW
/4, -boxH
/4);
441 glVertex2i(-boxW
/4, boxH
/4);
442 SetColor(COLOR_BLUE
);
443 glVertex2i(0, -boxH
/4);
444 SetColor(COLOR_WHITE
);
445 glVertex2i(0, boxH
/4);
446 SetColor(COLOR_CYAN
);
447 glVertex2i(boxW
/4, -boxH
/4);
448 SetColor(COLOR_YELLOW
);
449 glVertex2i(boxW
/4, boxH
/4);
452 glBegin(GL_QUAD_STRIP
);
454 glVertex2i(100, 100);
455 glVertex2i(-100, 100);
459 static void Draw(EGLDisplay dpy
, EGLSurface surf
)
462 glViewport(0, 0, windW
, windH
);
463 glDisable(GL_SCISSOR_TEST
);
465 glPushAttrib(GL_COLOR_BUFFER_BIT
);
467 glColorMask(1, 1, 1, 1);
470 glClearColor(0.0, 0.0, 0.0, 0.0);
471 glClear(GL_COLOR_BUFFER_BIT
);
476 glShadeModel(GL_SMOOTH
);
478 glShadeModel(GL_FLAT
);
482 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
484 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
487 Viewport(0, 0); Point();
488 Viewport(0, 1); Lines();
489 Viewport(0, 2); LineStrip();
490 Viewport(0, 3); LineLoop();
492 Viewport(1, 0); Bitmap();
494 Viewport(1, 1); TriangleFan();
495 Viewport(1, 2); Triangles();
496 Viewport(1, 3); TriangleStrip();
498 Viewport(2, 0); Rect();
499 Viewport(2, 1); PolygonFunc();
500 Viewport(2, 2); Quads();
501 Viewport(2, 3); QuadStrip();
506 eglSwapBuffers(dpy
, surf
);
511 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
)
513 const int binary
= 0;
514 FILE *f
= fopen( filename
, "w" );
517 const GLubyte
*ptr
= buffer
;
520 fprintf(f
,"# ppm-file created by osdemo.c\n");
521 fprintf(f
,"%i %i\n", width
,height
);
524 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
525 for (y
=height
-1; y
>=0; y
--) {
526 for (x
=0; x
<width
; x
++) {
527 i
= (y
*width
+ x
) * 4;
528 fputc(ptr
[i
], f
); /* write red */
529 fputc(ptr
[i
+1], f
); /* write green */
530 fputc(ptr
[i
+2], f
); /* write blue */
538 fprintf(f
,"# ascii ppm file created by osdemo.c\n");
539 fprintf(f
,"%i %i\n", width
, height
);
541 for (y
=height
-1; y
>=0; y
--) {
542 for (x
=0; x
<width
; x
++) {
543 i
= (y
*width
+ x
) * 4;
544 fprintf(f
, " %3d %3d %3d", ptr
[i
], ptr
[i
+1], ptr
[i
+2]);
546 if (counter
% 5 == 0)
556 main(int argc
, char *argv
[])
560 EGLSurface screen_surf
;
561 EGLConfig configs
[10];
562 EGLScreenMESA screen
;
564 EGLint numConfigs
, count
;
566 const GLubyte
*bitmap
;
567 EGLint screenAttribs
[32];
570 EGLDisplay d
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
573 if (!eglInitialize(d
, &maj
, &min
)) {
574 printf("demo: eglInitialize failed\n");
578 printf("EGL version = %d.%d\n", maj
, min
);
579 printf("EGL_VENDOR = %s\n", eglQueryString(d
, EGL_VENDOR
));
580 if (!strstr(eglQueryString(d
, EGL_EXTENSIONS
),
581 "EGL_MESA_screen_surface")) {
582 printf("EGL_MESA_screen_surface is not supported\n");
586 eglGetConfigs(d
, configs
, 10, &numConfigs
);
587 eglGetScreensMESA(d
, &screen
, 1, &count
);
588 eglGetModesMESA(d
, screen
, &mode
, 1, &count
);
590 eglBindAPI(EGL_OPENGL_API
);
591 ctx
= eglCreateContext(d
, configs
[0], EGL_NO_CONTEXT
, NULL
);
592 if (ctx
== EGL_NO_CONTEXT
) {
593 printf("failed to create context\n");
598 screenAttribs
[i
++] = EGL_WIDTH
;
599 eglGetModeAttribMESA(d
, mode
, EGL_WIDTH
, &screenAttribs
[i
++]);
600 screenAttribs
[i
++] = EGL_HEIGHT
;
601 eglGetModeAttribMESA(d
, mode
, EGL_HEIGHT
, &screenAttribs
[i
++]);
602 screenAttribs
[i
] = EGL_NONE
;
604 screen_surf
= eglCreateScreenSurfaceMESA(d
, configs
[0], screenAttribs
);
605 if (screen_surf
== EGL_NO_SURFACE
) {
606 printf("failed to create screen surface\n");
610 eglShowScreenSurfaceMESA(d
, screen
, screen_surf
, mode
);
612 b
= eglMakeCurrent(d
, screen_surf
, screen_surf
, ctx
);
614 printf("make current failed\n");
617 glViewport(0, 0, 1024, 768);
623 /* some drivers crash when rendering to front buffer */
625 glDrawBuffer( GL_FRONT
);
626 glClearColor( 0, 1.0, 0, 1);
628 glClear( GL_COLOR_BUFFER_BIT
);
632 glDrawBuffer( GL_BACK
);
634 Draw(d
, screen_surf
);
637 /* TODO EGL_KHR_lock_surface */
640 write_ppm("dump.ppm", bitmap
, 1024, 768);
642 eglDestroySurface(d
, screen_surf
);
643 eglDestroyContext(d
, ctx
);