rev |
line source |
nuclear@0
|
1 #include <map>
|
nuclear@0
|
2 #include <vector>
|
nuclear@0
|
3 #include "unistate.h"
|
nuclear@0
|
4 #include "shader.h"
|
nuclear@0
|
5 #include "logger.h"
|
nuclear@0
|
6
|
nuclear@0
|
7 struct StateItem {
|
nuclear@0
|
8 StType type;
|
nuclear@0
|
9
|
nuclear@0
|
10 union {
|
nuclear@0
|
11 int ival[4];
|
nuclear@0
|
12 float fval[16];
|
nuclear@0
|
13 };
|
nuclear@0
|
14 int transpose; // for matrices
|
nuclear@0
|
15 };
|
nuclear@0
|
16
|
nuclear@0
|
17 static const char *typestr(StType type);
|
nuclear@0
|
18 static int type_nelem(StType type);
|
nuclear@0
|
19 static StType float_type(int elem);
|
nuclear@0
|
20 static StType int_type(int elem);
|
nuclear@0
|
21
|
nuclear@0
|
22 std::vector<StateItem> state;
|
nuclear@0
|
23 std::map<std::string, int> stateidx;
|
nuclear@0
|
24
|
nuclear@0
|
25
|
nuclear@0
|
26 int add_unistate(const char *name, StType type)
|
nuclear@0
|
27 {
|
nuclear@0
|
28 static const float ident3[] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
|
nuclear@0
|
29 static const float ident4[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
30
|
nuclear@0
|
31 if(stateidx.find(name) != stateidx.end()) {
|
nuclear@0
|
32 return stateidx[name];
|
nuclear@0
|
33 }
|
nuclear@0
|
34
|
nuclear@0
|
35 StateItem sitem;
|
nuclear@0
|
36 memset(&sitem, 0, sizeof sitem);
|
nuclear@0
|
37 sitem.type = type;
|
nuclear@0
|
38
|
nuclear@0
|
39 // initialize to a reasonable default value
|
nuclear@0
|
40 switch(type) {
|
nuclear@0
|
41 case ST_MATRIX3:
|
nuclear@0
|
42 memcpy(sitem.fval, ident3, sizeof ident3);
|
nuclear@0
|
43 break;
|
nuclear@0
|
44
|
nuclear@0
|
45 case ST_MATRIX4:
|
nuclear@0
|
46 memcpy(sitem.fval, ident4, sizeof ident4);
|
nuclear@0
|
47 break;
|
nuclear@0
|
48
|
nuclear@0
|
49 default:
|
nuclear@0
|
50 break; // in all other cases leave it zero (see memset above)
|
nuclear@0
|
51 }
|
nuclear@0
|
52
|
nuclear@0
|
53 int sidx = state.size();
|
nuclear@0
|
54 state.push_back(sitem);
|
nuclear@0
|
55 stateidx[name] = sidx;
|
nuclear@0
|
56
|
nuclear@0
|
57 debug_log("adding uniform state [%d]: %s %s\n", sidx, typestr(sitem.type), name);
|
nuclear@0
|
58
|
nuclear@0
|
59 return sidx;
|
nuclear@0
|
60 }
|
nuclear@0
|
61
|
nuclear@0
|
62 int get_unistate_index(const char *name)
|
nuclear@0
|
63 {
|
nuclear@0
|
64 std::map<std::string, int>::const_iterator it = stateidx.find(name);
|
nuclear@0
|
65 if(it != stateidx.end()) {
|
nuclear@0
|
66 return it->second;
|
nuclear@0
|
67 }
|
nuclear@0
|
68 return -1;
|
nuclear@0
|
69 }
|
nuclear@0
|
70
|
nuclear@0
|
71 #define CHECK_INDEX(i) \
|
nuclear@0
|
72 if(i < 0 || i >= (int)state.size()) return
|
nuclear@0
|
73
|
nuclear@0
|
74 #define CHECK_COUNT(count, type) \
|
nuclear@0
|
75 do { \
|
nuclear@0
|
76 int max_elem = type_nelem(type); \
|
nuclear@0
|
77 if(!(count) || (count) > max_elem) { \
|
nuclear@0
|
78 count = max_elem; \
|
nuclear@0
|
79 } \
|
nuclear@0
|
80 } while(0)
|
nuclear@0
|
81
|
nuclear@0
|
82 void set_unistate(int sidx, const int *val, int count)
|
nuclear@0
|
83 {
|
nuclear@0
|
84 CHECK_INDEX(sidx);
|
nuclear@0
|
85 CHECK_COUNT(count, state[sidx].type);
|
nuclear@0
|
86
|
nuclear@0
|
87 memcpy(state[sidx].ival, val, count * sizeof *state[sidx].ival);
|
nuclear@0
|
88 }
|
nuclear@0
|
89
|
nuclear@0
|
90 void set_unistate(int sidx, const float *val, int count)
|
nuclear@0
|
91 {
|
nuclear@0
|
92 CHECK_INDEX(sidx);
|
nuclear@0
|
93 CHECK_COUNT(count, state[sidx].type);
|
nuclear@0
|
94
|
nuclear@0
|
95 memcpy(state[sidx].fval, val, count * sizeof *state[sidx].fval);
|
nuclear@0
|
96 state[sidx].transpose = 0;
|
nuclear@0
|
97 }
|
nuclear@0
|
98
|
nuclear@0
|
99 void get_unistate(int sidx, int *val, int count)
|
nuclear@0
|
100 {
|
nuclear@0
|
101 CHECK_INDEX(sidx);
|
nuclear@0
|
102 CHECK_COUNT(count, state[sidx].type);
|
nuclear@0
|
103
|
nuclear@0
|
104 memcpy(val, state[sidx].ival, count * sizeof *val);
|
nuclear@0
|
105 }
|
nuclear@0
|
106
|
nuclear@0
|
107 void get_unistate(int sidx, float *val, int count)
|
nuclear@0
|
108 {
|
nuclear@0
|
109 CHECK_INDEX(sidx);
|
nuclear@0
|
110 CHECK_COUNT(count, state[sidx].type);
|
nuclear@0
|
111
|
nuclear@0
|
112 memcpy(val, state[sidx].fval, count * sizeof *val);
|
nuclear@0
|
113 }
|
nuclear@0
|
114
|
nuclear@0
|
115 void set_unistate(int sidx, int val)
|
nuclear@0
|
116 {
|
nuclear@0
|
117 set_unistate(sidx, &val, 1);
|
nuclear@0
|
118 }
|
nuclear@0
|
119
|
nuclear@0
|
120 void set_unistate(int sidx, float val)
|
nuclear@0
|
121 {
|
nuclear@0
|
122 set_unistate(sidx, &val, 1);
|
nuclear@0
|
123 }
|
nuclear@0
|
124
|
nuclear@0
|
125 void set_unistate(int sidx, const Vector2 &vec)
|
nuclear@0
|
126 {
|
nuclear@0
|
127 set_unistate(sidx, &vec.x, 2);
|
nuclear@0
|
128 }
|
nuclear@0
|
129
|
nuclear@0
|
130 void set_unistate(int sidx, const Vector3 &vec)
|
nuclear@0
|
131 {
|
nuclear@0
|
132 set_unistate(sidx, &vec.x, 3);
|
nuclear@0
|
133 }
|
nuclear@0
|
134
|
nuclear@0
|
135 void set_unistate(int sidx, const Vector4 &vec)
|
nuclear@0
|
136 {
|
nuclear@0
|
137 set_unistate(sidx, &vec.x, 4);
|
nuclear@0
|
138 }
|
nuclear@0
|
139
|
nuclear@0
|
140 void set_unistate(int sidx, const Matrix3x3 &mat)
|
nuclear@0
|
141 {
|
nuclear@0
|
142 set_unistate(sidx, mat[0], 9);
|
nuclear@0
|
143 state[sidx].transpose = 1;
|
nuclear@0
|
144 }
|
nuclear@0
|
145
|
nuclear@0
|
146 void set_unistate(int sidx, const Matrix4x4 &mat)
|
nuclear@0
|
147 {
|
nuclear@0
|
148 set_unistate(sidx, mat[0], 16);
|
nuclear@0
|
149 state[sidx].transpose = 1;
|
nuclear@0
|
150 }
|
nuclear@0
|
151
|
nuclear@0
|
152
|
nuclear@0
|
153 int set_unistate(const char *name, int *val, int count)
|
nuclear@0
|
154 {
|
nuclear@0
|
155 int sidx = get_unistate_index(name);
|
nuclear@0
|
156 if(sidx < 0) {
|
nuclear@0
|
157 StType type = int_type(count);
|
nuclear@0
|
158 if(type == ST_UNKNOWN) {
|
nuclear@0
|
159 error_log("invalid element count (%d) while setting previously unknown unistate item \"%s\"\n",
|
nuclear@0
|
160 count, name);
|
nuclear@0
|
161 return -1;
|
nuclear@0
|
162 }
|
nuclear@0
|
163
|
nuclear@0
|
164 sidx = add_unistate(name, type);
|
nuclear@0
|
165 }
|
nuclear@0
|
166 set_unistate(sidx, val);
|
nuclear@0
|
167 return sidx;
|
nuclear@0
|
168 }
|
nuclear@0
|
169
|
nuclear@0
|
170 int set_unistate(const char *name, float *val, int count)
|
nuclear@0
|
171 {
|
nuclear@0
|
172 int sidx = get_unistate_index(name);
|
nuclear@0
|
173 if(sidx < 0) {
|
nuclear@0
|
174 StType type = float_type(count);
|
nuclear@0
|
175 if(type == ST_UNKNOWN) {
|
nuclear@0
|
176 error_log("invalid element count (%d) while setting previously unknown unistate item \"%s\"\n",
|
nuclear@0
|
177 count, name);
|
nuclear@0
|
178 return -1;
|
nuclear@0
|
179 }
|
nuclear@0
|
180
|
nuclear@0
|
181 sidx = add_unistate(name, type);
|
nuclear@0
|
182 }
|
nuclear@0
|
183 set_unistate(sidx, val);
|
nuclear@0
|
184 return sidx;
|
nuclear@0
|
185 }
|
nuclear@0
|
186
|
nuclear@0
|
187 int set_unistate(const char *name, int val)
|
nuclear@0
|
188 {
|
nuclear@0
|
189 int sidx = get_unistate_index(name);
|
nuclear@0
|
190 if(sidx < 0) {
|
nuclear@0
|
191 sidx = add_unistate(name, ST_INT);
|
nuclear@0
|
192 }
|
nuclear@0
|
193 set_unistate(sidx, val);
|
nuclear@0
|
194 return sidx;
|
nuclear@0
|
195 }
|
nuclear@0
|
196
|
nuclear@0
|
197 int set_unistate(const char *name, float val)
|
nuclear@0
|
198 {
|
nuclear@0
|
199 int sidx = get_unistate_index(name);
|
nuclear@0
|
200 if(sidx < 0) {
|
nuclear@0
|
201 sidx = add_unistate(name, ST_FLOAT);
|
nuclear@0
|
202 }
|
nuclear@0
|
203 set_unistate(sidx, val);
|
nuclear@0
|
204 return sidx;
|
nuclear@0
|
205 }
|
nuclear@0
|
206
|
nuclear@0
|
207 int set_unistate(const char *name, const Vector2 &vec)
|
nuclear@0
|
208 {
|
nuclear@0
|
209 int sidx = get_unistate_index(name);
|
nuclear@0
|
210 if(sidx < 0) {
|
nuclear@0
|
211 sidx = add_unistate(name, ST_FLOAT2);
|
nuclear@0
|
212 }
|
nuclear@0
|
213 set_unistate(sidx, vec);
|
nuclear@0
|
214 return sidx;
|
nuclear@0
|
215 }
|
nuclear@0
|
216
|
nuclear@0
|
217 int set_unistate(const char *name, const Vector3 &vec)
|
nuclear@0
|
218 {
|
nuclear@0
|
219 int sidx = get_unistate_index(name);
|
nuclear@0
|
220 if(sidx < 0) {
|
nuclear@0
|
221 sidx = add_unistate(name, ST_FLOAT3);
|
nuclear@0
|
222 }
|
nuclear@0
|
223 set_unistate(sidx, vec);
|
nuclear@0
|
224 return sidx;
|
nuclear@0
|
225 }
|
nuclear@0
|
226
|
nuclear@0
|
227 int set_unistate(const char *name, const Vector4 &vec)
|
nuclear@0
|
228 {
|
nuclear@0
|
229 int sidx = get_unistate_index(name);
|
nuclear@0
|
230 if(sidx < 0) {
|
nuclear@0
|
231 sidx = add_unistate(name, ST_FLOAT4);
|
nuclear@0
|
232 }
|
nuclear@0
|
233 set_unistate(sidx, vec);
|
nuclear@0
|
234 return sidx;
|
nuclear@0
|
235 }
|
nuclear@0
|
236
|
nuclear@0
|
237 int set_unistate(const char *name, const Matrix3x3 &mat)
|
nuclear@0
|
238 {
|
nuclear@0
|
239 int sidx = get_unistate_index(name);
|
nuclear@0
|
240 if(sidx < 0) {
|
nuclear@0
|
241 sidx = add_unistate(name, ST_MATRIX3);
|
nuclear@0
|
242 }
|
nuclear@0
|
243 set_unistate(sidx, mat);
|
nuclear@0
|
244 return sidx;
|
nuclear@0
|
245 }
|
nuclear@0
|
246
|
nuclear@0
|
247 int set_unistate(const char *name, const Matrix4x4 &mat)
|
nuclear@0
|
248 {
|
nuclear@0
|
249 int sidx = get_unistate_index(name);
|
nuclear@0
|
250 if(sidx < 0) {
|
nuclear@0
|
251 sidx = add_unistate(name, ST_MATRIX4);
|
nuclear@0
|
252 }
|
nuclear@0
|
253 set_unistate(sidx, mat);
|
nuclear@0
|
254 return sidx;
|
nuclear@0
|
255 }
|
nuclear@0
|
256
|
nuclear@0
|
257
|
nuclear@0
|
258 int get_unistate_int(int sidx)
|
nuclear@0
|
259 {
|
nuclear@0
|
260 int val = 0;
|
nuclear@0
|
261 get_unistate(sidx, &val, 1);
|
nuclear@0
|
262 return val;
|
nuclear@0
|
263 }
|
nuclear@0
|
264
|
nuclear@0
|
265 float get_unistate_float(int sidx)
|
nuclear@0
|
266 {
|
nuclear@0
|
267 float val = 0.0f;
|
nuclear@0
|
268 get_unistate(sidx, &val, 1);
|
nuclear@0
|
269 return val;
|
nuclear@0
|
270 }
|
nuclear@0
|
271
|
nuclear@0
|
272 Vector2 get_unistate_vec2(int sidx)
|
nuclear@0
|
273 {
|
nuclear@0
|
274 float val[2] = {0.0f, 0.0f};
|
nuclear@0
|
275 get_unistate(sidx, val, 2);
|
nuclear@0
|
276 return Vector2(val[0], val[1]);
|
nuclear@0
|
277 }
|
nuclear@0
|
278
|
nuclear@0
|
279 Vector3 get_unistate_vec3(int sidx)
|
nuclear@0
|
280 {
|
nuclear@0
|
281 float val[3] = {0.0f, 0.0f, 0.0f};
|
nuclear@0
|
282 get_unistate(sidx, val, 3);
|
nuclear@0
|
283 return Vector3(val[0], val[1], val[2]);
|
nuclear@0
|
284 }
|
nuclear@0
|
285
|
nuclear@0
|
286 Vector4 get_unistate_vec4(int sidx)
|
nuclear@0
|
287 {
|
nuclear@0
|
288 float val[4] = {0.0f, 0.0f, 0.0f};
|
nuclear@0
|
289 get_unistate(sidx, val, 4);
|
nuclear@0
|
290 return Vector4(val[0], val[1], val[2], val[3]);
|
nuclear@0
|
291 }
|
nuclear@0
|
292
|
nuclear@0
|
293 Matrix3x3 get_unistate_mat3(int sidx)
|
nuclear@0
|
294 {
|
nuclear@0
|
295 Matrix3x3 res;
|
nuclear@0
|
296 get_unistate(sidx, res.m[0], 9);
|
nuclear@0
|
297 return res;
|
nuclear@0
|
298 }
|
nuclear@0
|
299
|
nuclear@0
|
300 Matrix4x4 get_unistate_mat4(int sidx)
|
nuclear@0
|
301 {
|
nuclear@0
|
302 Matrix4x4 res;
|
nuclear@0
|
303 get_unistate(sidx, res.m[0], 16);
|
nuclear@0
|
304 return res;
|
nuclear@0
|
305 }
|
nuclear@0
|
306
|
nuclear@0
|
307
|
nuclear@0
|
308 int get_unistate_int(const char *name)
|
nuclear@0
|
309 {
|
nuclear@0
|
310 int sidx = get_unistate_index(name);
|
nuclear@0
|
311 if(sidx == -1) {
|
nuclear@0
|
312 return 0;
|
nuclear@0
|
313 }
|
nuclear@0
|
314 return get_unistate_int(sidx);
|
nuclear@0
|
315 }
|
nuclear@0
|
316
|
nuclear@0
|
317 float get_unistate_float(const char *name)
|
nuclear@0
|
318 {
|
nuclear@0
|
319 int sidx = get_unistate_index(name);
|
nuclear@0
|
320 if(sidx == -1) {
|
nuclear@0
|
321 return 0.0f;
|
nuclear@0
|
322 }
|
nuclear@0
|
323 return get_unistate_float(sidx);
|
nuclear@0
|
324 }
|
nuclear@0
|
325
|
nuclear@0
|
326 Vector2 get_unistate_vec2(const char *name)
|
nuclear@0
|
327 {
|
nuclear@0
|
328 int sidx = get_unistate_index(name);
|
nuclear@0
|
329 if(sidx == -1) {
|
nuclear@0
|
330 return Vector2();
|
nuclear@0
|
331 }
|
nuclear@0
|
332 return get_unistate_vec2(sidx);
|
nuclear@0
|
333 }
|
nuclear@0
|
334
|
nuclear@0
|
335 Vector3 get_unistate_vec3(const char *name)
|
nuclear@0
|
336 {
|
nuclear@0
|
337 int sidx = get_unistate_index(name);
|
nuclear@0
|
338 if(sidx == -1) {
|
nuclear@0
|
339 return Vector3();
|
nuclear@0
|
340 }
|
nuclear@0
|
341 return get_unistate_vec3(sidx);
|
nuclear@0
|
342 }
|
nuclear@0
|
343
|
nuclear@0
|
344 Vector4 get_unistate_vec4(const char *name)
|
nuclear@0
|
345 {
|
nuclear@0
|
346 int sidx = get_unistate_index(name);
|
nuclear@0
|
347 if(sidx == -1) {
|
nuclear@0
|
348 return Vector4();
|
nuclear@0
|
349 }
|
nuclear@0
|
350 return get_unistate_vec4(sidx);
|
nuclear@0
|
351 }
|
nuclear@0
|
352
|
nuclear@0
|
353 Matrix3x3 get_unistate_mat3(const char *name)
|
nuclear@0
|
354 {
|
nuclear@0
|
355 int sidx = get_unistate_index(name);
|
nuclear@0
|
356 if(sidx == -1) {
|
nuclear@0
|
357 return Matrix3x3();
|
nuclear@0
|
358 }
|
nuclear@0
|
359 return get_unistate_mat3(sidx);
|
nuclear@0
|
360 }
|
nuclear@0
|
361
|
nuclear@0
|
362 Matrix4x4 get_unistate_mat4(const char *name)
|
nuclear@0
|
363 {
|
nuclear@0
|
364 int sidx = get_unistate_index(name);
|
nuclear@0
|
365 if(sidx == -1) {
|
nuclear@0
|
366 return Matrix4x4();
|
nuclear@0
|
367 }
|
nuclear@0
|
368 return get_unistate_mat4(sidx);
|
nuclear@0
|
369 }
|
nuclear@0
|
370
|
nuclear@0
|
371
|
nuclear@0
|
372 void setup_unistate(const ShaderProg *sdr)
|
nuclear@0
|
373 {
|
nuclear@0
|
374 if(!sdr) {
|
nuclear@0
|
375 if(!(sdr = ShaderProg::current)) {
|
nuclear@0
|
376 return;
|
nuclear@0
|
377 }
|
nuclear@0
|
378 }
|
nuclear@0
|
379
|
nuclear@0
|
380 sdr->setup_state_uniforms();
|
nuclear@0
|
381 }
|
nuclear@0
|
382
|
nuclear@0
|
383 bool setup_unistate(int sidx, const ShaderProg *sdr, int loc)
|
nuclear@0
|
384 {
|
nuclear@0
|
385 if(loc < 0 || sidx < 0 || sidx >= (int)state.size()) {
|
nuclear@0
|
386 return false;
|
nuclear@0
|
387 }
|
nuclear@0
|
388
|
nuclear@0
|
389 CHECKGLERR;
|
nuclear@0
|
390 glUseProgram(sdr->get_id());
|
nuclear@0
|
391 CHECKGLERR;
|
nuclear@0
|
392
|
nuclear@0
|
393 switch(state[sidx].type) {
|
nuclear@0
|
394 case ST_INT:
|
nuclear@0
|
395 glUniform1iv(loc, 1, state[sidx].ival);
|
nuclear@0
|
396 break;
|
nuclear@0
|
397 case ST_INT2:
|
nuclear@0
|
398 glUniform2iv(loc, 1, state[sidx].ival);
|
nuclear@0
|
399 break;
|
nuclear@0
|
400 case ST_INT3:
|
nuclear@0
|
401 glUniform3iv(loc, 1, state[sidx].ival);
|
nuclear@0
|
402 break;
|
nuclear@0
|
403 case ST_INT4:
|
nuclear@0
|
404 glUniform4iv(loc, 1, state[sidx].ival);
|
nuclear@0
|
405 break;
|
nuclear@0
|
406
|
nuclear@0
|
407 case ST_FLOAT:
|
nuclear@0
|
408 glUniform1fv(loc, 1, state[sidx].fval);
|
nuclear@0
|
409 break;
|
nuclear@0
|
410 case ST_FLOAT2:
|
nuclear@0
|
411 glUniform2fv(loc, 1, state[sidx].fval);
|
nuclear@0
|
412 break;
|
nuclear@0
|
413 case ST_FLOAT3:
|
nuclear@0
|
414 glUniform3fv(loc, 1, state[sidx].fval);
|
nuclear@0
|
415 break;
|
nuclear@0
|
416 case ST_FLOAT4:
|
nuclear@0
|
417 glUniform4fv(loc, 1, state[sidx].fval);
|
nuclear@0
|
418 break;
|
nuclear@0
|
419
|
nuclear@0
|
420 case ST_MATRIX3:
|
nuclear@0
|
421 #ifdef GL_ES_VERSION_2_0
|
nuclear@0
|
422 {
|
nuclear@0
|
423 float tmat[9], *ptr = tmat;
|
nuclear@0
|
424 for(int i=0; i<3; i++) {
|
nuclear@0
|
425 for(int j=0; j<3; j++) {
|
nuclear@0
|
426 *ptr++ = state[sidx].fval[j * 3 + i];
|
nuclear@0
|
427 }
|
nuclear@0
|
428 }
|
nuclear@0
|
429 glUniformMatrix3fv(loc, 1, GL_FALSE, tmat);
|
nuclear@0
|
430 }
|
nuclear@0
|
431 #else
|
nuclear@0
|
432 glUniformMatrix3fv(loc, 1, state[sidx].transpose, state[sidx].fval);
|
nuclear@0
|
433 #endif
|
nuclear@0
|
434 break;
|
nuclear@0
|
435
|
nuclear@0
|
436 case ST_MATRIX4:
|
nuclear@0
|
437 #ifdef GL_ES_VERSION_2_0
|
nuclear@0
|
438 {
|
nuclear@0
|
439 float tmat[16], *ptr = tmat;
|
nuclear@0
|
440 for(int i=0; i<4; i++) {
|
nuclear@0
|
441 for(int j=0; j<4; j++) {
|
nuclear@0
|
442 *ptr++ = state[sidx].fval[j * 4 + i];
|
nuclear@0
|
443 }
|
nuclear@0
|
444 }
|
nuclear@0
|
445 glUniformMatrix4fv(loc, 1, GL_FALSE, tmat);
|
nuclear@0
|
446 }
|
nuclear@0
|
447 #else
|
nuclear@0
|
448 glUniformMatrix4fv(loc, 1, state[sidx].transpose, state[sidx].fval);
|
nuclear@0
|
449 #endif
|
nuclear@0
|
450 break;
|
nuclear@0
|
451
|
nuclear@0
|
452 default:
|
nuclear@0
|
453 return false;
|
nuclear@0
|
454 }
|
nuclear@0
|
455
|
nuclear@0
|
456 CHECKGLERR;
|
nuclear@0
|
457 return true;
|
nuclear@0
|
458 }
|
nuclear@0
|
459
|
nuclear@0
|
460 bool setup_unistate(const char *name, const ShaderProg *sdr)
|
nuclear@0
|
461 {
|
nuclear@0
|
462 int loc = sdr->get_uniform_location(name);
|
nuclear@0
|
463 if(loc == -1) {
|
nuclear@0
|
464 return false;
|
nuclear@0
|
465 }
|
nuclear@0
|
466 return setup_unistate(get_unistate_index(name), sdr, loc);
|
nuclear@0
|
467 }
|
nuclear@0
|
468
|
nuclear@0
|
469 void set_world_matrix(const Matrix4x4 &mat)
|
nuclear@0
|
470 {
|
nuclear@0
|
471 static int sidx = -1, sidx_transp, sidx_mat3;
|
nuclear@0
|
472
|
nuclear@0
|
473 if(sidx == -1) {
|
nuclear@0
|
474 sidx = add_unistate("st_world_matrix", ST_MATRIX4);
|
nuclear@0
|
475 sidx_mat3 = add_unistate("st_world_matrix3", ST_MATRIX3);
|
nuclear@0
|
476 sidx_transp = add_unistate("st_world_matrix_transpose", ST_MATRIX4);
|
nuclear@0
|
477 }
|
nuclear@0
|
478
|
nuclear@0
|
479 set_unistate(sidx, mat);
|
nuclear@0
|
480 set_unistate(sidx_mat3, Matrix3x3(mat));
|
nuclear@0
|
481 set_unistate(sidx_transp, mat[0]); // by using the float* variant, we unset the transpose flag
|
nuclear@0
|
482 }
|
nuclear@0
|
483
|
nuclear@0
|
484 void set_view_matrix(const Matrix4x4 &mat)
|
nuclear@0
|
485 {
|
nuclear@0
|
486 static int sidx = -1, sidx_transp, sidx_mat3;
|
nuclear@0
|
487
|
nuclear@0
|
488 if(sidx == -1) {
|
nuclear@0
|
489 sidx = add_unistate("st_view_matrix", ST_MATRIX4);
|
nuclear@0
|
490 sidx_mat3 = add_unistate("st_view_matrix3", ST_MATRIX3);
|
nuclear@0
|
491 sidx_transp = add_unistate("st_view_matrix_transpose", ST_MATRIX4);
|
nuclear@0
|
492 }
|
nuclear@0
|
493
|
nuclear@0
|
494 set_unistate(sidx, mat);
|
nuclear@0
|
495 set_unistate(sidx_mat3, Matrix3x3(mat));
|
nuclear@0
|
496 set_unistate(sidx_transp, mat[0]); // by using the float* variant, we unset the transpose flag
|
nuclear@0
|
497 }
|
nuclear@0
|
498
|
nuclear@0
|
499 void set_projection_matrix(const Matrix4x4 &mat)
|
nuclear@0
|
500 {
|
nuclear@0
|
501 static int sidx = -1;
|
nuclear@0
|
502
|
nuclear@0
|
503 if(sidx == -1) {
|
nuclear@0
|
504 sidx = add_unistate("st_proj_matrix", ST_MATRIX4);
|
nuclear@0
|
505 }
|
nuclear@0
|
506
|
nuclear@0
|
507 set_unistate(sidx, mat);
|
nuclear@0
|
508 }
|
nuclear@0
|
509
|
nuclear@0
|
510 void set_texture_matrix(const Matrix4x4 &mat)
|
nuclear@0
|
511 {
|
nuclear@0
|
512 static int sidx = -1;
|
nuclear@0
|
513
|
nuclear@0
|
514 if(sidx == -1) {
|
nuclear@0
|
515 sidx = add_unistate("st_tex_matrix", ST_MATRIX4);
|
nuclear@0
|
516 }
|
nuclear@0
|
517
|
nuclear@0
|
518 set_unistate(sidx, mat);
|
nuclear@0
|
519 }
|
nuclear@0
|
520
|
nuclear@0
|
521 Matrix4x4 get_world_matrix()
|
nuclear@0
|
522 {
|
nuclear@0
|
523 static int sidx = -1;
|
nuclear@0
|
524
|
nuclear@0
|
525 if(sidx == -1) {
|
nuclear@0
|
526 if((sidx = get_unistate_index("st_world_matrix")) == -1) {
|
nuclear@0
|
527 return Matrix4x4();
|
nuclear@0
|
528 }
|
nuclear@0
|
529 }
|
nuclear@0
|
530 return get_unistate_mat4(sidx);
|
nuclear@0
|
531 }
|
nuclear@0
|
532
|
nuclear@0
|
533 Matrix4x4 get_view_matrix()
|
nuclear@0
|
534 {
|
nuclear@0
|
535 static int sidx = -1;
|
nuclear@0
|
536
|
nuclear@0
|
537 if(sidx == -1) {
|
nuclear@0
|
538 if((sidx = get_unistate_index("st_view_matrix")) == -1) {
|
nuclear@0
|
539 return Matrix4x4();
|
nuclear@0
|
540 }
|
nuclear@0
|
541 }
|
nuclear@0
|
542 return get_unistate_mat4(sidx);
|
nuclear@0
|
543 }
|
nuclear@0
|
544
|
nuclear@0
|
545 Matrix4x4 get_projection_matrix()
|
nuclear@0
|
546 {
|
nuclear@0
|
547 static int sidx = -1;
|
nuclear@0
|
548
|
nuclear@0
|
549 if(sidx == -1) {
|
nuclear@0
|
550 if((sidx = get_unistate_index("st_proj_matrix")) == -1) {
|
nuclear@0
|
551 return Matrix4x4();
|
nuclear@0
|
552 }
|
nuclear@0
|
553 }
|
nuclear@0
|
554 return get_unistate_mat4(sidx);
|
nuclear@0
|
555 }
|
nuclear@0
|
556
|
nuclear@0
|
557 Matrix4x4 get_texture_matrix()
|
nuclear@0
|
558 {
|
nuclear@0
|
559 static int sidx = -1;
|
nuclear@0
|
560
|
nuclear@0
|
561 if(sidx == -1) {
|
nuclear@0
|
562 if((sidx = get_unistate_index("st_tex_matrix")) == -1) {
|
nuclear@0
|
563 return Matrix4x4();
|
nuclear@0
|
564 }
|
nuclear@0
|
565 }
|
nuclear@0
|
566 return get_unistate_mat4(sidx);
|
nuclear@0
|
567 }
|
nuclear@0
|
568
|
nuclear@0
|
569 void setup_gl_matrices()
|
nuclear@0
|
570 {
|
nuclear@0
|
571 #ifdef USE_OLDGL
|
nuclear@0
|
572 Matrix4x4 modelview = get_world_matrix() * get_view_matrix();
|
nuclear@0
|
573 Matrix4x4 proj = get_projection_matrix();
|
nuclear@0
|
574 Matrix4x4 tex = get_texture_matrix();
|
nuclear@0
|
575
|
nuclear@0
|
576 glMatrixMode(GL_TEXTURE);
|
nuclear@0
|
577 glLoadTransposeMatrixf(tex[0]);
|
nuclear@0
|
578 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
579 glLoadTransposeMatrixf(proj[0]);
|
nuclear@0
|
580 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
581 glLoadTransposeMatrixf(modelview[0]);
|
nuclear@0
|
582 #endif
|
nuclear@0
|
583 }
|
nuclear@0
|
584
|
nuclear@0
|
585 static const char *typestr(StType type)
|
nuclear@0
|
586 {
|
nuclear@0
|
587 switch(type) {
|
nuclear@0
|
588 case ST_INT:
|
nuclear@0
|
589 return "int";
|
nuclear@0
|
590 case ST_INT2:
|
nuclear@0
|
591 return "ivec2";
|
nuclear@0
|
592 case ST_INT3:
|
nuclear@0
|
593 return "ivec3";
|
nuclear@0
|
594 case ST_INT4:
|
nuclear@0
|
595 return "ivec4";
|
nuclear@0
|
596 case ST_FLOAT:
|
nuclear@0
|
597 return "float";
|
nuclear@0
|
598 case ST_FLOAT2:
|
nuclear@0
|
599 return "vec2";
|
nuclear@0
|
600 case ST_FLOAT3:
|
nuclear@0
|
601 return "vec3";
|
nuclear@0
|
602 case ST_FLOAT4:
|
nuclear@0
|
603 return "vec4";
|
nuclear@0
|
604 case ST_MATRIX3:
|
nuclear@0
|
605 return "mat3";
|
nuclear@0
|
606 case ST_MATRIX4:
|
nuclear@0
|
607 return "mat4";
|
nuclear@0
|
608
|
nuclear@0
|
609 default:
|
nuclear@0
|
610 break;
|
nuclear@0
|
611 }
|
nuclear@0
|
612 return "<unknown>";
|
nuclear@0
|
613 }
|
nuclear@0
|
614
|
nuclear@0
|
615 static int type_nelem(StType type)
|
nuclear@0
|
616 {
|
nuclear@0
|
617 switch(type) {
|
nuclear@0
|
618 case ST_INT:
|
nuclear@0
|
619 case ST_FLOAT:
|
nuclear@0
|
620 return 1;
|
nuclear@0
|
621 case ST_INT2:
|
nuclear@0
|
622 case ST_FLOAT2:
|
nuclear@0
|
623 return 2;
|
nuclear@0
|
624 case ST_INT3:
|
nuclear@0
|
625 case ST_FLOAT3:
|
nuclear@0
|
626 return 3;
|
nuclear@0
|
627 case ST_INT4:
|
nuclear@0
|
628 case ST_FLOAT4:
|
nuclear@0
|
629 return 4;
|
nuclear@0
|
630 case ST_MATRIX3:
|
nuclear@0
|
631 return 9;
|
nuclear@0
|
632 case ST_MATRIX4:
|
nuclear@0
|
633 return 16;
|
nuclear@0
|
634
|
nuclear@0
|
635 default:
|
nuclear@0
|
636 break;
|
nuclear@0
|
637 }
|
nuclear@0
|
638
|
nuclear@0
|
639 return 0;
|
nuclear@0
|
640 }
|
nuclear@0
|
641
|
nuclear@0
|
642 static StType float_type(int elem)
|
nuclear@0
|
643 {
|
nuclear@0
|
644 switch(elem) {
|
nuclear@0
|
645 case 1:
|
nuclear@0
|
646 return ST_FLOAT;
|
nuclear@0
|
647 case 2:
|
nuclear@0
|
648 return ST_FLOAT2;
|
nuclear@0
|
649 case 3:
|
nuclear@0
|
650 return ST_FLOAT3;
|
nuclear@0
|
651 case 4:
|
nuclear@0
|
652 return ST_FLOAT4;
|
nuclear@0
|
653 case 9:
|
nuclear@0
|
654 return ST_MATRIX3;
|
nuclear@0
|
655 case 16:
|
nuclear@0
|
656 return ST_MATRIX4;
|
nuclear@0
|
657 default:
|
nuclear@0
|
658 break;
|
nuclear@0
|
659 }
|
nuclear@0
|
660 return ST_UNKNOWN;
|
nuclear@0
|
661 }
|
nuclear@0
|
662
|
nuclear@0
|
663 static StType int_type(int elem)
|
nuclear@0
|
664 {
|
nuclear@0
|
665 switch(elem) {
|
nuclear@0
|
666 case 1:
|
nuclear@0
|
667 return ST_INT;
|
nuclear@0
|
668 case 2:
|
nuclear@0
|
669 return ST_INT2;
|
nuclear@0
|
670 case 3:
|
nuclear@0
|
671 return ST_INT3;
|
nuclear@0
|
672 case 4:
|
nuclear@0
|
673 return ST_INT4;
|
nuclear@0
|
674 default:
|
nuclear@0
|
675 break;
|
nuclear@0
|
676 }
|
nuclear@0
|
677 return ST_UNKNOWN;
|
nuclear@0
|
678 }
|