2693c372054e48c08c6ce21a2b53b18fc8c54a52
2 * Exercise EGL API functions
13 #define PIXEL_CENTER(x) ((long)(x) + 0.5)
19 #define OPENGL_WIDTH 48
20 #define OPENGL_HEIGHT 13
23 GLenum rgb
, doubleBuffer
, windType
;
28 GLubyte OpenGL_bits
[] = {
29 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
30 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
31 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
32 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
33 0x3e, 0x8f, 0xb7, 0xf9, 0xfc, 0x01,
34 0x63, 0xdb, 0xb0, 0x8d, 0x0d, 0x00,
35 0x63, 0xdb, 0xb7, 0x8d, 0x0d, 0x00,
36 0x63, 0xdb, 0xb6, 0x8d, 0x0d, 0x00,
37 0x63, 0x8f, 0xf3, 0xcc, 0x0d, 0x00,
38 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0a,
39 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0e,
40 0x63, 0x00, 0x00, 0x8c, 0xed, 0x0e,
41 0x3e, 0x00, 0x00, 0xf8, 0x0c, 0x00,
45 static void Init(void)
52 static void Reshape(int width
, int height
)
56 windH
= (GLint
)height
;
60 static void RotateColorMask(void)
62 static GLint rotation
= 0;
64 rotation
= (rotation
+ 1) & 0x3;
67 glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
71 glColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
75 glColorMask(GL_TRUE
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
79 glColorMask(GL_TRUE
, GL_TRUE
, GL_FALSE
, GL_TRUE
);
86 static void Viewport(GLint row
, GLint column
)
90 boxW
= (windW
- (COLS
+ 1) * GAP
) / COLS
;
91 boxH
= (windH
- (ROWS
+ 1) * GAP
) / ROWS
;
93 x
= GAP
+ column
* (boxW
+ GAP
);
94 y
= GAP
+ row
* (boxH
+ GAP
);
96 glViewport(x
, y
, boxW
, boxH
);
98 glMatrixMode(GL_PROJECTION
);
100 glOrtho(-boxW
/2, boxW
/2, -boxH
/2, boxH
/2, 0.0, 1.0);
101 glMatrixMode(GL_MODELVIEW
);
103 glEnable(GL_SCISSOR_TEST
);
104 glScissor(x
, y
, boxW
, boxH
);
118 static float RGBMap
[9][3] = {
130 static void SetColor(int c
)
132 glColor3fv(RGBMap
[c
]);
135 static void Point(void)
140 SetColor(COLOR_WHITE
);
142 for (i
= 1; i
< 8; i
++) {
144 SetColor(COLOR_BLACK
+i
);
157 static void Lines(void)
163 glTranslatef(-12, 0, 0);
164 for (i
= 1; i
< 8; i
++) {
165 SetColor(COLOR_BLACK
+i
);
167 glVertex2i(-boxW
/4, -boxH
/4);
168 glVertex2i(boxW
/4, boxH
/4);
170 glTranslatef(4, 0, 0);
180 static void LineStrip(void)
183 glBegin(GL_LINE_STRIP
);
185 glVertex2f(PIXEL_CENTER(-boxW
/4), PIXEL_CENTER(-boxH
/4));
186 SetColor(COLOR_GREEN
);
187 glVertex2f(PIXEL_CENTER(-boxW
/4), PIXEL_CENTER(boxH
/4));
188 SetColor(COLOR_BLUE
);
189 glVertex2f(PIXEL_CENTER(boxW
/4), PIXEL_CENTER(boxH
/4));
190 SetColor(COLOR_WHITE
);
191 glVertex2f(PIXEL_CENTER(boxW
/4), PIXEL_CENTER(-boxH
/4));
194 glBegin(GL_LINE_STRIP
);
199 static void LineLoop(void)
202 glBegin(GL_LINE_LOOP
);
204 glVertex2f(PIXEL_CENTER(-boxW
/4), PIXEL_CENTER(-boxH
/4));
205 SetColor(COLOR_GREEN
);
206 glVertex2f(PIXEL_CENTER(-boxW
/4), PIXEL_CENTER(boxH
/4));
207 SetColor(COLOR_BLUE
);
208 glVertex2f(PIXEL_CENTER(boxW
/4), PIXEL_CENTER(boxH
/4));
209 SetColor(COLOR_WHITE
);
210 glVertex2f(PIXEL_CENTER(boxW
/4), PIXEL_CENTER(-boxH
/4));
213 glEnable(GL_LOGIC_OP
);
217 glBlendFunc(GL_ONE
, GL_ONE
);
219 SetColor(COLOR_MAGENTA
);
220 glBegin(GL_LINE_LOOP
);
221 glVertex2f(PIXEL_CENTER(-boxW
/8), PIXEL_CENTER(-boxH
/8));
222 glVertex2f(PIXEL_CENTER(-boxW
/8), PIXEL_CENTER(boxH
/8));
224 glBegin(GL_LINE_LOOP
);
225 glVertex2f(PIXEL_CENTER(-boxW
/8), PIXEL_CENTER(boxH
/8+5));
226 glVertex2f(PIXEL_CENTER(boxW
/8), PIXEL_CENTER(boxH
/8+5));
228 glDisable(GL_LOGIC_OP
);
231 SetColor(COLOR_GREEN
);
236 glBegin(GL_LINE_LOOP
);
241 static void Bitmap(void)
245 SetColor(COLOR_GREEN
);
246 glVertex2i(-boxW
/2, 0);
247 glVertex2i(boxW
/2, 0);
248 glVertex2i(0, -boxH
/2);
249 glVertex2i(0, boxH
/2);
252 glVertex2i(0, -3+OPENGL_HEIGHT
);
253 SetColor(COLOR_BLUE
);
255 glVertex2i(OPENGL_WIDTH
, -3);
258 SetColor(COLOR_GREEN
);
260 glPixelStorei(GL_UNPACK_LSB_FIRST
, GL_TRUE
);
261 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
264 glBitmap(OPENGL_WIDTH
, OPENGL_HEIGHT
, 0, 3, 0.0, 0.0, OpenGL_bits
);
267 static void Triangles(void)
270 glBegin(GL_TRIANGLES
);
271 SetColor(COLOR_GREEN
);
272 glVertex2i(-boxW
/4, -boxH
/4);
274 glVertex2i(-boxW
/8, -boxH
/16);
275 SetColor(COLOR_BLUE
);
276 glVertex2i(boxW
/8, -boxH
/16);
278 SetColor(COLOR_GREEN
);
279 glVertex2i(-boxW
/4, boxH
/4);
281 glVertex2i(-boxW
/8, boxH
/16);
282 SetColor(COLOR_BLUE
);
283 glVertex2i(boxW
/8, boxH
/16);
286 glBegin(GL_TRIANGLES
);
288 glVertex2i(-100, 100);
292 static void TriangleStrip(void)
295 glBegin(GL_TRIANGLE_STRIP
);
296 SetColor(COLOR_GREEN
);
297 glVertex2i(-boxW
/4, -boxH
/4);
299 glVertex2i(-boxW
/4, boxH
/4);
300 SetColor(COLOR_BLUE
);
301 glVertex2i(0, -boxH
/4);
302 SetColor(COLOR_WHITE
);
303 glVertex2i(0, boxH
/4);
304 SetColor(COLOR_CYAN
);
305 glVertex2i(boxW
/4, -boxH
/4);
306 SetColor(COLOR_YELLOW
);
307 glVertex2i(boxW
/4, boxH
/4);
310 glBegin(GL_TRIANGLE_STRIP
);
312 glVertex2i(-100, 100);
316 static void TriangleFan(void)
319 GLint x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
;
331 vx
[0][0] = x0
; vx
[0][1] = y1
;
332 vx
[1][0] = x0
; vx
[1][1] = y2
;
333 vx
[2][0] = x1
; vx
[2][1] = y3
;
334 vx
[3][0] = x2
; vx
[3][1] = y3
;
335 vx
[4][0] = x3
; vx
[4][1] = y2
;
336 vx
[5][0] = x3
; vx
[5][1] = y1
;
337 vx
[6][0] = x2
; vx
[6][1] = y0
;
338 vx
[7][0] = x1
; vx
[7][1] = y0
;
340 glBegin(GL_TRIANGLE_FAN
);
341 SetColor(COLOR_WHITE
);
343 for (i
= 0; i
< 8; i
++) {
344 SetColor(COLOR_WHITE
-i
);
349 glBegin(GL_TRIANGLE_FAN
);
351 glVertex2i(-100, 100);
355 static void Rect(void)
358 SetColor(COLOR_GREEN
);
359 glRecti(-boxW
/4, -boxH
/4, boxW
/4, boxH
/4);
362 static void PolygonFunc(void)
365 GLint x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
;
377 vx
[0][0] = x0
; vx
[0][1] = y1
;
378 vx
[1][0] = x0
; vx
[1][1] = y2
;
379 vx
[2][0] = x1
; vx
[2][1] = y3
;
380 vx
[3][0] = x2
; vx
[3][1] = y3
;
381 vx
[4][0] = x3
; vx
[4][1] = y2
;
382 vx
[5][0] = x3
; vx
[5][1] = y1
;
383 vx
[6][0] = x2
; vx
[6][1] = y0
;
384 vx
[7][0] = x1
; vx
[7][1] = y0
;
387 for (i
= 0; i
< 8; i
++) {
388 SetColor(COLOR_WHITE
-i
);
395 glVertex2i(100, 100);
399 static void Quads(void)
403 SetColor(COLOR_GREEN
);
404 glVertex2i(-boxW
/4, -boxH
/4);
406 glVertex2i(-boxW
/8, -boxH
/16);
407 SetColor(COLOR_BLUE
);
408 glVertex2i(boxW
/8, -boxH
/16);
409 SetColor(COLOR_WHITE
);
410 glVertex2i(boxW
/4, -boxH
/4);
412 SetColor(COLOR_GREEN
);
413 glVertex2i(-boxW
/4, boxH
/4);
415 glVertex2i(-boxW
/8, boxH
/16);
416 SetColor(COLOR_BLUE
);
417 glVertex2i(boxW
/8, boxH
/16);
418 SetColor(COLOR_WHITE
);
419 glVertex2i(boxW
/4, boxH
/4);
424 glVertex2i(100, 100);
425 glVertex2i(-100, 100);
429 static void QuadStrip(void)
432 glBegin(GL_QUAD_STRIP
);
433 SetColor(COLOR_GREEN
);
434 glVertex2i(-boxW
/4, -boxH
/4);
436 glVertex2i(-boxW
/4, boxH
/4);
437 SetColor(COLOR_BLUE
);
438 glVertex2i(0, -boxH
/4);
439 SetColor(COLOR_WHITE
);
440 glVertex2i(0, boxH
/4);
441 SetColor(COLOR_CYAN
);
442 glVertex2i(boxW
/4, -boxH
/4);
443 SetColor(COLOR_YELLOW
);
444 glVertex2i(boxW
/4, boxH
/4);
447 glBegin(GL_QUAD_STRIP
);
449 glVertex2i(100, 100);
450 glVertex2i(-100, 100);
454 static void Draw(EGLDisplay dpy
, EGLSurface surf
)
457 glViewport(0, 0, windW
, windH
);
458 glDisable(GL_SCISSOR_TEST
);
460 glPushAttrib(GL_COLOR_BUFFER_BIT
);
462 glColorMask(1, 1, 1, 1);
465 glClearColor(0.0, 0.0, 0.0, 0.0);
466 glClear(GL_COLOR_BUFFER_BIT
);
471 glShadeModel(GL_SMOOTH
);
473 glShadeModel(GL_FLAT
);
477 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
479 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
482 Viewport(0, 0); Point();
483 Viewport(0, 1); Lines();
484 Viewport(0, 2); LineStrip();
485 Viewport(0, 3); LineLoop();
487 Viewport(1, 0); Bitmap();
489 Viewport(1, 1); TriangleFan();
490 Viewport(1, 2); Triangles();
491 Viewport(1, 3); TriangleStrip();
493 Viewport(2, 0); Rect();
494 Viewport(2, 1); PolygonFunc();
495 Viewport(2, 2); Quads();
496 Viewport(2, 3); QuadStrip();
501 eglSwapBuffers(dpy
, surf
);
506 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
)
508 const int binary
= 0;
509 FILE *f
= fopen( filename
, "w" );
512 const GLubyte
*ptr
= buffer
;
515 fprintf(f
,"# ppm-file created by osdemo.c\n");
516 fprintf(f
,"%i %i\n", width
,height
);
519 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
520 for (y
=height
-1; y
>=0; y
--) {
521 for (x
=0; x
<width
; x
++) {
522 i
= (y
*width
+ x
) * 4;
523 fputc(ptr
[i
], f
); /* write red */
524 fputc(ptr
[i
+1], f
); /* write green */
525 fputc(ptr
[i
+2], f
); /* write blue */
533 fprintf(f
,"# ascii ppm file created by osdemo.c\n");
534 fprintf(f
,"%i %i\n", width
, height
);
536 for (y
=height
-1; y
>=0; y
--) {
537 for (x
=0; x
<width
; x
++) {
538 i
= (y
*width
+ x
) * 4;
539 fprintf(f
, " %3d %3d %3d", ptr
[i
], ptr
[i
+1], ptr
[i
+2]);
541 if (counter
% 5 == 0)
550 #include "../src/egl/main/egldisplay.h"
552 typedef struct fb_display
554 _EGLDisplay Base
; /* base class/object */
560 main(int argc
, char *argv
[])
564 EGLSurface screen_surf
;
565 EGLConfig configs
[10];
566 EGLScreenMESA screen
;
568 EGLint numConfigs
, count
;
570 const EGLint screenAttribs
[] = {
577 EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
579 EGLDisplay d
= eglGetDisplay(":0");
582 if (!eglInitialize(d
, &maj
, &min
)) {
583 printf("demo: eglInitialize failed\n");
587 printf("EGL version = %d.%d\n", maj
, min
);
588 printf("EGL_VENDOR = %s\n", eglQueryString(d
, EGL_VENDOR
));
590 eglGetConfigs(d
, configs
, 10, &numConfigs
);
591 eglGetScreensMESA(d
, &screen
, 1, &count
);
592 eglGetModesMESA(d
, screen
, &mode
, 1, &count
);
594 ctx
= eglCreateContext(d
, configs
[0], EGL_NO_CONTEXT
, NULL
);
595 if (ctx
== EGL_NO_CONTEXT
) {
596 printf("failed to create context\n");
600 screen_surf
= eglCreateScreenSurfaceMESA(d
, configs
[0], screenAttribs
);
601 if (screen_surf
== EGL_NO_SURFACE
) {
602 printf("failed to create screen surface\n");
606 eglShowSurfaceMESA(d
, screen
, screen_surf
, mode
);
608 b
= eglMakeCurrent(d
, screen_surf
, screen_surf
, ctx
);
610 printf("make current failed\n");
613 glViewport(0, 0, 1024, 768);
619 glDrawBuffer( GL_FRONT
);
620 glClearColor( 0, 1.0, 0, 1);
622 glClear( GL_COLOR_BUFFER_BIT
);
625 glDrawBuffer( GL_BACK
);
627 Draw(d
, screen_surf
);
629 write_ppm("dump.ppm", ((struct fb_display
*)_eglLookupDisplay(d
))->pFB
, 1024, 768);
631 eglDestroySurface(d
, screen_surf
);
632 eglDestroyContext(d
, ctx
);