labyrinth
view src/gles/sanegl.c @ 5:c8826e5ebec1
- changed every data loading function to return dummy objects instead of failing
- fixed mistake in AndroidManifest.xml
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 01 May 2015 05:58:41 +0300 |
parents | |
children |
line source
1 /*
2 SaneGL - a small library to bring back sanity to OpenGL ES 2.x
3 Copyright (C) 2011-2013 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23 #include <assert.h>
24 #include "sanegl.h"
26 #define MAX_VERTS 512
28 static void gl_draw_immediate(void);
30 typedef struct { float x, y; } vec2_t;
31 typedef struct { float x, y, z; } vec3_t;
32 typedef struct { float x, y, z, w; } vec4_t;
34 static int prim = -1;
36 static vec3_t cur_normal;
37 static vec4_t cur_color;
38 static vec2_t cur_texcoord;
40 static vec4_t *vert_arr, *col_arr;
41 static vec3_t *norm_arr;
42 static vec2_t *texc_arr;
44 static int num_verts, vert_calls;
46 /* immediate mode rendering */
47 void gl_begin(int p)
48 {
49 if(!vert_arr) {
50 vert_arr = malloc(MAX_VERTS * sizeof *vert_arr);
51 norm_arr = malloc(MAX_VERTS * sizeof *norm_arr);
52 texc_arr = malloc(MAX_VERTS * sizeof *texc_arr);
53 col_arr = malloc(MAX_VERTS * sizeof *col_arr);
54 assert(vert_arr && norm_arr && texc_arr && col_arr);
55 }
57 prim = p;
58 num_verts = vert_calls = 0;
59 }
61 void gl_end(void)
62 {
63 if(num_verts > 0) {
64 gl_draw_immediate();
65 }
66 }
68 static void gl_draw_immediate(void)
69 {
70 int glprim;
72 glprim = prim == GL_QUADS ? GL_TRIANGLES : prim;
74 glEnableClientState(GL_VERTEX_ARRAY);
75 glEnableClientState(GL_NORMAL_ARRAY);
76 glEnableClientState(GL_COLOR_ARRAY);
77 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
79 glVertexPointer(4, GL_FLOAT, 0, vert_arr);
80 glNormalPointer(GL_FLOAT, 0, norm_arr);
81 glColorPointer(4, GL_FLOAT, 0, col_arr);
82 glTexCoordPointer(2, GL_FLOAT, 0, texc_arr);
84 glDrawArrays(glprim, 0, num_verts);
86 glDisableClientState(GL_VERTEX_ARRAY);
87 glDisableClientState(GL_NORMAL_ARRAY);
88 glDisableClientState(GL_COLOR_ARRAY);
89 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
90 }
92 void gl_vertex2f(float x, float y)
93 {
94 gl_vertex4f(x, y, 0.0f, 1.0f);
95 }
97 void gl_vertex3f(float x, float y, float z)
98 {
99 gl_vertex4f(x, y, z, 1.0f);
100 }
102 void gl_vertex4f(float x, float y, float z, float w)
103 {
104 int i, buffer_full;
106 if(prim == GL_QUADS && vert_calls % 4 == 3) {
107 for(i=0; i<2; i++) {
108 col_arr[num_verts] = col_arr[num_verts - 3 + i];
109 texc_arr[num_verts] = texc_arr[num_verts - 3 + i];
110 norm_arr[num_verts] = norm_arr[num_verts - 3 + i];
111 vert_arr[num_verts] = vert_arr[num_verts - 3 + i];
112 num_verts++;
113 }
114 }
116 vert_arr[num_verts].x = x;
117 vert_arr[num_verts].y = y;
118 vert_arr[num_verts].z = z;
119 vert_arr[num_verts].w = w;
121 col_arr[num_verts] = cur_color;
122 norm_arr[num_verts] = cur_normal;
123 texc_arr[num_verts] = cur_texcoord;
125 vert_calls++;
126 num_verts++;
128 if(prim == GL_QUADS) {
129 /* leave space for 6 more worst-case and don't allow flushes mid-quad */
130 buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0;
131 } else {
132 buffer_full = num_verts >= MAX_VERTS - prim;
133 }
135 if(buffer_full) {
136 gl_draw_immediate();
137 gl_begin(prim); /* reset everything */
138 }
139 }
142 void gl_normal3f(float x, float y, float z)
143 {
144 cur_normal.x = x;
145 cur_normal.y = y;
146 cur_normal.z = z;
147 }
150 void gl_color3f(float r, float g, float b)
151 {
152 cur_color.x = r;
153 cur_color.y = g;
154 cur_color.z = b;
155 cur_color.w = 1.0f;
156 }
158 void gl_color4f(float r, float g, float b, float a)
159 {
160 cur_color.x = r;
161 cur_color.y = g;
162 cur_color.z = b;
163 cur_color.w = a;
164 }
167 void gl_texcoord1f(float s)
168 {
169 cur_texcoord.x = s;
170 cur_texcoord.y = 0.0f;
171 }
173 void gl_texcoord2f(float s, float t)
174 {
175 cur_texcoord.x = s;
176 cur_texcoord.y = t;
177 }
179 void glu_perspective(float fov, float aspect, float nearz, float farz)
180 {
181 float fovrad = M_PI * fov / 180.0;
182 float halfsz = tan(fovrad) * nearz;
184 glFrustumf(-halfsz * aspect, halfsz * aspect, -halfsz, halfsz, nearz, farz);
185 }