]> git.cworth.org Git - apitrace-tests/blob - apps/egl/gles1/tri.c
Automate gles tests.
[apitrace-tests] / apps / egl / gles1 / 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 <stdlib.h>
35 #include <GLES/gl.h>  /* use OpenGL ES 1.x */
36 #include <GLES/glext.h>
37 #include <EGL/egl.h>
38
39 #include "eglut.h"
40
41
42 #define FLOAT_TO_FIXED(X)   ((X) * 65535.0)
43
44
45
46 static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
47
48
49 static void
50 idle(void)
51 {
52    exit(0);
53 }
54
55
56 static void
57 draw(void)
58 {
59 #if USE_FIXED_POINT
60    static const GLfixed verts[3][2] = {
61       { -65536, -65536 },
62       {  65536, -65536 },
63       {      0,  65536 }
64    };
65    static const GLfixed colors[3][4] = {
66       { 65536,     0,     0,    65536 },
67       {     0, 65536,     0 ,   65536},
68       {     0,     0, 65536 ,   65536}
69    };
70 #else
71    static const GLfloat verts[3][2] = {
72       { -1, -1 },
73       {  1, -1 },
74       {  0,  1 }
75    };
76    static const GLfloat colors[3][4] = {
77       { 1, 0, 0, 1 },
78       { 0, 1, 0, 1 },
79       { 0, 0, 1, 1 }
80    };
81 #endif
82
83    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84
85    glPushMatrix();
86    glRotatef(view_rotx, 1, 0, 0);
87    glRotatef(view_roty, 0, 1, 0);
88    glRotatef(view_rotz, 0, 0, 1);
89
90    {
91 #if USE_FIXED_POINT
92       glVertexPointer(2, GL_FIXED, 0, verts);
93       glColorPointer(4, GL_FIXED, 0, colors);
94 #else
95       glVertexPointer(2, GL_FLOAT, 0, verts);
96       glColorPointer(4, GL_FLOAT, 0, colors);
97 #endif
98       glEnableClientState(GL_VERTEX_ARRAY);
99       glEnableClientState(GL_COLOR_ARRAY);
100
101       /* draw triangle */
102       glDrawArrays(GL_TRIANGLES, 0, 3);
103
104       /* draw some points */
105       glPointSizex(FLOAT_TO_FIXED(15.5));
106       glDrawArrays(GL_POINTS, 0, 3);
107
108       glDisableClientState(GL_VERTEX_ARRAY);
109       glDisableClientState(GL_COLOR_ARRAY);
110    }
111
112    if (0) {
113       /* test code */
114       GLfixed size;
115       glGetFixedv(GL_POINT_SIZE, &size);
116       printf("GL_POINT_SIZE = 0x%x %f\n", size, size / 65536.0);
117    }
118
119    glPopMatrix();
120
121    eglutIdleFunc(idle);
122 }
123
124
125 /* new window size or exposure */
126 static void
127 reshape(int width, int height)
128 {
129    GLfloat ar = (GLfloat) width / (GLfloat) height;
130
131    glViewport(0, 0, (GLint) width, (GLint) height);
132
133    glMatrixMode(GL_PROJECTION);
134    glLoadIdentity();
135 #ifdef GL_VERSION_ES_CM_1_0
136    glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
137 #else
138    glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
139 #endif
140    
141    glMatrixMode(GL_MODELVIEW);
142    glLoadIdentity();
143    glTranslatef(0.0, 0.0, -10.0);
144 }
145
146
147 static void
148 test_query_matrix(void)
149 {
150    PFNGLQUERYMATRIXXOESPROC procQueryMatrixx;
151    typedef void (*voidproc)();
152    GLfixed mantissa[16];
153    GLint exponent[16];
154    GLbitfield rv;
155    int i;
156
157    procQueryMatrixx = (PFNGLQUERYMATRIXXOESPROC) eglGetProcAddress("glQueryMatrixxOES");
158    assert(procQueryMatrixx);
159    /* Actually try out this one */
160    rv = (*procQueryMatrixx)(mantissa, exponent);
161    for (i = 0; i < 16; i++) {
162       if (rv & (1<<i)) {
163         printf("matrix[%d] invalid\n", i);
164       }
165       else {
166          printf("matrix[%d] = %f * 2^(%d)\n", i, mantissa[i]/65536.0, exponent[i]);
167       }
168    }
169    assert(!eglGetProcAddress("glFoo"));
170 }
171
172
173 static void
174 init(void)
175 {
176    glClearColor(0.4, 0.4, 0.4, 0.0);
177
178    if (0)
179       test_query_matrix();
180 }
181
182 static void
183 special_key(int special)
184 {
185    switch (special) {
186    case EGLUT_KEY_LEFT:
187       view_roty += 5.0;
188       break;
189    case EGLUT_KEY_RIGHT:
190       view_roty -= 5.0;
191       break;
192    case EGLUT_KEY_UP:
193       view_rotx += 5.0;
194       break;
195    case EGLUT_KEY_DOWN:
196       view_rotx -= 5.0;
197       break;
198    default:
199       break;
200    }
201 }
202
203 int
204 main(int argc, char *argv[])
205 {
206    eglutInitWindowSize(300, 300);
207    eglutInitAPIMask(EGLUT_OPENGL_ES1_BIT);
208    eglutInit(argc, argv);
209
210    eglutCreateWindow("tri");
211
212    eglutReshapeFunc(reshape);
213    eglutDisplayFunc(draw);
214    eglutSpecialFunc(special_key);
215
216    init();
217
218    eglutMainLoop();
219
220    return 0;
221 }