WIP - port to Mali EGL
[mesa-demos/mali.git] / src / egl / opengles1 / tri.c
blobd80c9060826aa3492da35ce1b66463605913025b
1 /*
2 * Copyright (C) 2008 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Draw a triangle with X/EGL and OpenGL ES 1.x
24 * Brian Paul
25 * 5 June 2008
28 #define USE_FIXED_POINT 1
31 #include <assert.h>
32 #include <math.h>
33 #include <stdio.h>
34 #include <GLES/gl.h> /* use OpenGL ES 1.x */
35 #include <GLES/glext.h>
36 #include <EGL/egl.h>
38 #include "eglut.h"
41 #define FLOAT_TO_FIXED(X) ((X) * 65535.0)
45 static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
48 static void
49 draw(void)
51 #if USE_FIXED_POINT
52 static const GLfixed verts[3][2] = {
53 { -65536, -65536 },
54 { 65536, -65536 },
55 { 0, 65536 }
57 static const GLfixed colors[3][4] = {
58 { 65536, 0, 0, 65536 },
59 { 0, 65536, 0 , 65536},
60 { 0, 0, 65536 , 65536}
62 #else
63 static const GLfloat verts[3][2] = {
64 { -1, -1 },
65 { 1, -1 },
66 { 0, 1 }
68 static const GLfloat colors[3][4] = {
69 { 1, 0, 0, 1 },
70 { 0, 1, 0, 1 },
71 { 0, 0, 1, 1 }
73 #endif
75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77 glPushMatrix();
78 glRotatef(view_rotx, 1, 0, 0);
79 glRotatef(view_roty, 0, 1, 0);
80 glRotatef(view_rotz, 0, 0, 1);
83 #if USE_FIXED_POINT
84 glVertexPointer(2, GL_FIXED, 0, verts);
85 glColorPointer(4, GL_FIXED, 0, colors);
86 #else
87 glVertexPointer(2, GL_FLOAT, 0, verts);
88 glColorPointer(4, GL_FLOAT, 0, colors);
89 #endif
90 glEnableClientState(GL_VERTEX_ARRAY);
91 glEnableClientState(GL_COLOR_ARRAY);
93 /* draw triangle */
94 glDrawArrays(GL_TRIANGLES, 0, 3);
96 /* draw some points */
97 glPointSizex(FLOAT_TO_FIXED(15.5));
98 glDrawArrays(GL_POINTS, 0, 3);
100 glDisableClientState(GL_VERTEX_ARRAY);
101 glDisableClientState(GL_COLOR_ARRAY);
104 if (0) {
105 /* test code */
106 GLfixed size;
107 glGetFixedv(GL_POINT_SIZE, &size);
108 printf("GL_POINT_SIZE = 0x%x %f\n", size, size / 65536.0);
111 glPopMatrix();
115 /* new window size or exposure */
116 static void
117 reshape(int width, int height)
119 GLfloat ar = (GLfloat) width / (GLfloat) height;
121 glViewport(0, 0, (GLint) width, (GLint) height);
123 glMatrixMode(GL_PROJECTION);
124 glLoadIdentity();
125 #ifdef GL_VERSION_ES_CM_1_0
126 glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
127 #else
128 glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
129 #endif
131 glMatrixMode(GL_MODELVIEW);
132 glLoadIdentity();
133 glTranslatef(0.0, 0.0, -10.0);
137 static void
138 test_query_matrix(void)
140 PFNGLQUERYMATRIXXOESPROC procQueryMatrixx;
141 typedef void (*voidproc)();
142 GLfixed mantissa[16];
143 GLint exponent[16];
144 GLbitfield rv;
145 int i;
147 procQueryMatrixx = (PFNGLQUERYMATRIXXOESPROC) eglGetProcAddress("glQueryMatrixxOES");
148 assert(procQueryMatrixx);
149 /* Actually try out this one */
150 rv = (*procQueryMatrixx)(mantissa, exponent);
151 for (i = 0; i < 16; i++) {
152 if (rv & (1<<i)) {
153 printf("matrix[%d] invalid\n", i);
155 else {
156 printf("matrix[%d] = %f * 2^(%d)\n", i, mantissa[i]/65536.0, exponent[i]);
159 assert(!eglGetProcAddress("glFoo"));
163 static void
164 init(void)
166 glClearColor(0.4, 0.4, 0.4, 0.0);
168 if (0)
169 test_query_matrix();
172 static void
173 special_key(int special)
175 switch (special) {
176 case EGLUT_KEY_LEFT:
177 view_roty += 5.0;
178 break;
179 case EGLUT_KEY_RIGHT:
180 view_roty -= 5.0;
181 break;
182 case EGLUT_KEY_UP:
183 view_rotx += 5.0;
184 break;
185 case EGLUT_KEY_DOWN:
186 view_rotx -= 5.0;
187 break;
188 default:
189 break;
194 main(int argc, char *argv[])
196 eglutInitWindowSize(300, 300);
197 eglutInitAPIMask(EGLUT_OPENGL_ES1_BIT);
198 eglutInit(argc, argv);
200 eglutCreateWindow("tri");
202 eglutReshapeFunc(reshape);
203 eglutDisplayFunc(draw);
204 eglutSpecialFunc(special_key);
206 init();
208 eglutMainLoop();
210 return 0;