Merge remote branch 'origin/7.8'
[mesa.git] / progs / egl / opengles1 / tri.c
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:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
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.
20 */
21
22 /*
23 * Draw a triangle with X/EGL and OpenGL ES 1.x
24 * Brian Paul
25 * 5 June 2008
26 */
27
28 #define USE_FIXED_POINT 0
29
30
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>
37
38 #include "eglut.h"
39
40
41 #define FLOAT_TO_FIXED(X) ((X) * 65535.0)
42
43
44
45 static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
46
47
48 static void
49 draw(void)
50 {
51 #if USE_FIXED_POINT
52 static const GLfixed verts[3][2] = {
53 { -65536, -65536 },
54 { 65536, -65536 },
55 { 0, 65536 }
56 };
57 static const GLfixed colors[3][4] = {
58 { 65536, 0, 0, 65536 },
59 { 0, 65536, 0 , 65536},
60 { 0, 0, 65536 , 65536}
61 };
62 #else
63 static const GLfloat verts[3][2] = {
64 { -1, -1 },
65 { 1, -1 },
66 { 0, 1 }
67 };
68 static const GLfloat colors[3][4] = {
69 { 1, 0, 0, 1 },
70 { 0, 1, 0, 1 },
71 { 0, 0, 1, 1 }
72 };
73 #endif
74
75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
76
77 glPushMatrix();
78 glRotatef(view_rotx, 1, 0, 0);
79 glRotatef(view_roty, 0, 1, 0);
80 glRotatef(view_rotz, 0, 0, 1);
81
82 {
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);
92
93 /* draw triangle */
94 glDrawArrays(GL_TRIANGLES, 0, 3);
95
96 /* draw some points */
97 glPointSizex(FLOAT_TO_FIXED(15.5));
98 glDrawArrays(GL_POINTS, 0, 3);
99
100 glDisableClientState(GL_VERTEX_ARRAY);
101 glDisableClientState(GL_COLOR_ARRAY);
102 }
103
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);
109 }
110
111 glPopMatrix();
112 }
113
114
115 /* new window size or exposure */
116 static void
117 reshape(int width, int height)
118 {
119 GLfloat ar = (GLfloat) width / (GLfloat) height;
120
121 glViewport(0, 0, (GLint) width, (GLint) height);
122
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
130
131 glMatrixMode(GL_MODELVIEW);
132 glLoadIdentity();
133 glTranslatef(0.0, 0.0, -10.0);
134 }
135
136
137 static void
138 test_query_matrix(void)
139 {
140 PFNGLQUERYMATRIXXOESPROC procQueryMatrixx;
141 typedef void (*voidproc)();
142 GLfixed mantissa[16];
143 GLint exponent[16];
144 GLbitfield rv;
145 int i;
146
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);
154 }
155 else {
156 printf("matrix[%d] = %f * 2^(%d)\n", i, mantissa[i]/65536.0, exponent[i]);
157 }
158 }
159 assert(!eglGetProcAddress("glFoo"));
160 }
161
162
163 static void
164 init(void)
165 {
166 glClearColor(0.4, 0.4, 0.4, 0.0);
167
168 if (0)
169 test_query_matrix();
170 }
171
172 static void
173 special_key(int special)
174 {
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;
190 }
191 }
192
193 int
194 main(int argc, char *argv[])
195 {
196 eglutInitWindowSize(300, 300);
197 eglutInitAPIMask(EGLUT_OPENGL_ES1_BIT);
198 eglutInit(argc, argv);
199
200 eglutCreateWindow("tri");
201
202 eglutReshapeFunc(reshape);
203 eglutDisplayFunc(draw);
204 eglutSpecialFunc(special_key);
205
206 init();
207
208 eglutMainLoop();
209
210 return 0;
211 }