]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/glstate.cpp
f910b49cc041bd87ef2072f61837be3e41b2cfc6
[gldbg.git] / flavors / gl / source / glstate.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <stdexcept>
9 #include "glstate.h"
10
11 using namespace std;
12
13 namespace {
14
15 template<typename T>
16 float normalize(T v);
17
18 template<>
19 float normalize<GLubyte>(GLubyte v)
20 { return v/255.0f; }
21
22 template<>
23 float normalize<GLbyte>(GLbyte v)
24 { return (2*v+1)/255.0f; }
25
26 template<typename T, void (*func)(void *, float)>
27 void wrap_normalized(void *user_data, T v0)
28 { func(user_data, v0); }
29
30 template<typename T, void (*func)(void *, float, float)>
31 void wrap_normalized(void *user_data, T v0, T v1)
32 { func(user_data, v0, v1); }
33
34 template<typename T, void (*func)(void *, float, float, float)>
35 void wrap_normalized(void *user_data, T v0, T v1, T v2)
36 { func(user_data, v0, v1, v2); }
37
38 template<typename T, void (*func)(void *, float, float, float, float)>
39 void wrap_normalized(void *user_data, T v0, T v1, T v2, T v3)
40 { func(user_data, v0, v1, v2, v3); }
41
42 template<typename T, void (*func)(void *, T)>
43 void wrap_array(void *user_data, const T *v)
44 { func(user_data, v[0]); }
45
46 template<typename T, void (*func)(void *, T, T)>
47 void wrap_array(void *user_data, const T *v)
48 { func(user_data, v[0], v[1]); }
49
50 template<typename T, void (*func)(void *, T, T, T)>
51 void wrap_array(void *user_data, const T *v)
52 { func(user_data, v[0], v[1], v[2]); }
53
54 template<typename T, void (*func)(void *, T, T, T, T)>
55 void wrap_array(void *user_data, const T *v)
56 { func(user_data, v[0], v[1], v[2], v[3]); }
57
58 }
59
60
61 Vector3::Vector3():
62         x(0), y(0), z(0)
63 { }
64
65
66 Vector4::Vector4():
67         x(0), y(0), z(0), w(1)
68 { }
69
70
71 GlState::GlState():
72         decoder(gldecoder_new(this, NULL)),
73         active_tex(0),
74         client_active_tex(0),
75         array_buffer(0),
76         element_buffer(0)
77 {
78         vertex_array.kind = GL_VERTEX_ARRAY;
79         normal_array.kind = GL_NORMAL_ARRAY;
80         color_array.kind = GL_COLOR_ARRAY;
81         for(unsigned i=0; i<8; ++i)
82         {
83                 texcoord_arrays[i].kind = GL_TEXTURE_COORD_ARRAY;
84                 texcoord_arrays[i].index = i;
85         }
86
87         decoder->glColor3ub = wrap_normalized<GLubyte, glColor3f>;
88         decoder->glColor3ubv = wrap_array<GLubyte, wrap_normalized<GLubyte, glColor3f> >;
89         decoder->glColor4ub = wrap_normalized<GLubyte, glColor4f>;
90         decoder->glColor4ubv = wrap_array<GLubyte, wrap_normalized<GLubyte, glColor4f> >;
91         decoder->glColor3f = glColor3f;
92         decoder->glColor3fv = wrap_array<float, glColor3f>;
93         decoder->glColor4f = glColor4f;
94         decoder->glColor4fv = wrap_array<float, glColor4f>;
95         decoder->glTexCoord1f = glTexCoord1f;
96         decoder->glTexCoord1fv = wrap_array<float, glTexCoord1f>;
97         decoder->glTexCoord2f = glTexCoord2f;
98         decoder->glTexCoord2fv = wrap_array<float, glTexCoord2f>;
99         decoder->glTexCoord3f = glTexCoord3f;
100         decoder->glTexCoord3fv = wrap_array<float, glTexCoord3f>;
101         decoder->glTexCoord4f = glTexCoord4f;
102         decoder->glTexCoord4fv = wrap_array<float, glTexCoord4f>;
103         decoder->glNormal3f = glNormal3f;
104         decoder->glNormal3fv = wrap_array<float, glNormal3f>;
105
106         decoder->glActiveTexture = glActiveTexture;
107         decoder->glActiveTextureARB = glActiveTexture;
108         decoder->glBindTexture = glBindTexture;
109         decoder->glTexImage2D = glTexImage2D;
110         decoder->glTexParameteri = glTexParameteri;
111         decoder->glTexParameteriv = glTexParameteriv;
112         decoder->glDeleteTextures = glDeleteTextures;
113
114         decoder->glVertexPointer = glVertexPointer;
115         decoder->glNormalPointer = glNormalPointer;
116         decoder->glColorPointer = glColorPointer;
117         decoder->glClientActiveTexture = glClientActiveTexture;
118         decoder->glClientActiveTextureARB = glClientActiveTexture;
119         decoder->glTexCoordPointer = glTexCoordPointer;
120
121         decoder->glBindBuffer = glBindBuffer;
122         decoder->glBindBufferARB = glBindBuffer;
123         decoder->glBufferData = glBufferData;
124         decoder->glBufferDataARB = glBufferData;
125         decoder->glBufferSubData = glBufferSubData;
126         decoder->glBufferSubDataARB = glBufferSubData;
127         decoder->glDeleteBuffers = glDeleteBuffers;
128         decoder->glDeleteBuffersARB = glDeleteBuffers;
129
130         decoder->glDrawElements = glDrawElements;
131         decoder->glDrawRangeElements = glDrawRangeElements;
132         decoder->glDrawRangeElementsEXT = glDrawRangeElements;
133 }
134
135 GlState::~GlState()
136 {
137         gldecoder_delete(decoder);
138 }
139
140 int GlState::decode(const char *data, unsigned len)
141 {
142         return gldecoder_decode(decoder, data, len);
143 }
144
145 const TextureState &GlState::get_texture(unsigned id) const
146 {
147         TextureMap::const_iterator i = textures.find(id);
148         if(i==textures.end())
149                 throw runtime_error("Unknown texture");
150         return i->second;
151 }
152
153 const BufferState &GlState::get_buffer(unsigned id) const
154 {
155         BufferMap::const_iterator i = buffers.find(id);
156         if(i==buffers.end())
157                 throw runtime_error("Unknown buffer");
158         return i->second;
159 }
160
161 const BufferState *GlState::get_current_buffer(GLenum target) const
162 {
163         return const_cast<GlState *>(this)->get_current_buffer(target);
164 }
165
166 bool &GlState::get_boolean_state(GLenum state)
167 {
168         if(state==GL_VERTEX_ARRAY)
169                 return vertex_array.enabled;
170         else if(state==GL_NORMAL_ARRAY)
171                 return normal_array.enabled;
172         else if(state==GL_COLOR_ARRAY)
173                 return color_array.enabled;
174         else if(state==GL_TEXTURE_COORD_ARRAY)
175                 return texcoord_arrays[client_active_tex].enabled;
176
177         static bool dummy;
178         return dummy;
179 }
180
181 TextureState *GlState::get_current_texture(GLenum target)
182 {
183         return texunits[active_tex].get_current_texture(target);
184 }
185
186 BufferState *GlState::get_current_buffer(GLenum target)
187 {
188         if(target==GL_ARRAY_BUFFER)
189                 return array_buffer;
190         else if(target==GL_ELEMENT_ARRAY_BUFFER)
191                 return element_buffer;
192         return 0;
193 }
194
195 const ArrayState &GlState::get_array(GLenum array) const
196 {
197         if(array==GL_VERTEX_ARRAY)
198                 return vertex_array;
199         else if(array==GL_NORMAL_ARRAY)
200                 return normal_array;
201         else if(array==GL_COLOR_ARRAY)
202                 return color_array;
203         else if(array==GL_TEXTURE_COORD_ARRAY)
204                 return texcoord_arrays[client_active_tex];
205         else
206                 throw logic_error("Invalid array");
207 }
208
209 const ArrayState &GlState::get_texture_coord_array(unsigned index) const
210 {
211         return texcoord_arrays[index];
212 }
213
214 const ArrayState &GlState::get_attrib_array(unsigned index) const
215 {
216         map<unsigned, ArrayState>::const_iterator i = attrib_arrays.find(index);
217         if(i!=attrib_arrays.end())
218                 return i->second;
219
220         // XXX Return a dummy?
221         throw runtime_error("Unknown attribute array");
222 }
223
224 void GlState::set_current_texture(GLenum target, unsigned id)
225 {
226         TexUnitState &unit = texunits[active_tex];
227         TextureState *tex = 0;
228         if(id)
229         {
230                 tex = &textures[id];
231                 if(!tex->id)
232                 {
233                         tex->id = id;
234                         tex->target = target;
235                 }
236                 else if(tex->target!=target)
237                         return;
238         }
239         unit.set_current_texture(target, tex);
240 }
241
242 void GlState::set_current_buffer(GLenum target, unsigned id)
243 {
244         BufferState *buf = 0;
245         if(id)
246         {
247                 buf = &buffers[id];
248                 if(!buf->id)
249                         buf->id = id;
250         }
251
252         if(target==GL_ARRAY_BUFFER)
253                 array_buffer = buf;
254         else if(target==GL_ELEMENT_ARRAY_BUFFER)
255                 element_buffer = buf;
256 }
257
258 // Boolean state
259
260 void GlState::glEnableClientState(void *user_data, GLenum state)
261 {
262         reinterpret_cast<GlState *>(user_data)->get_boolean_state(state) = true;
263 }
264
265 void GlState::glDisableClientState(void *user_data, GLenum state)
266 {
267         reinterpret_cast<GlState *>(user_data)->get_boolean_state(state) = false;
268 }
269
270 void GlState::glEnableVertexAttribArray(void *user_data, unsigned index)
271 {
272         reinterpret_cast<GlState *>(user_data)->attrib_arrays[index].enabled = true;
273 }
274
275 void GlState::glDisableVertexAttribArray(void *user_data, unsigned index)
276 {
277         reinterpret_cast<GlState *>(user_data)->attrib_arrays[index].enabled = false;
278 }
279
280 // Vertex attributes
281
282 void GlState::glColor3f(void *user_data, float r, float g, float b)
283 { glColor4f(user_data, r, g, b, 1.0f); }
284
285 void GlState::glColor4f(void *user_data, float r, float g, float b, float a)
286 {
287         Vector4 &color = reinterpret_cast<GlState *>(user_data)->color;
288         color.r = r;
289         color.g = g;
290         color.b = b;
291         color.a = a;
292 }
293
294 void GlState::glNormal3f(void *user_data, float x, float y, float z)
295 {
296         Vector3 &normal = reinterpret_cast<GlState *>(user_data)->normal;
297         normal.x = x;
298         normal.y = y;
299         normal.z = z;
300 }
301
302 void GlState::glTexCoord1f(void *user_data, float s)
303 { glTexCoord4f(user_data, s, 0.0f, 0.0f, 1.0f); }
304
305 void GlState::glTexCoord2f(void *user_data, float s, float t)
306 { glTexCoord4f(user_data, s, t, 0.0f, 1.0f); }
307
308 void GlState::glTexCoord3f(void *user_data, float s, float t, float p)
309 { glTexCoord4f(user_data, s, t, p, 1.0f); }
310
311 void GlState::glTexCoord4f(void *user_data, float s, float t, float p, float q)
312 {
313         unsigned index = reinterpret_cast<GlState *>(user_data)->active_tex;
314         glMultiTexCoord4f(user_data, index, s, t, p, q);
315 }
316
317 void GlState::glMultiTexCoord4f(void *user_data, unsigned index, float s, float t, float p, float q)
318 {
319         Vector4 &texcoord = reinterpret_cast<GlState *>(user_data)->texcoord[index];
320         texcoord.s = s;
321         texcoord.t = t;
322         texcoord.p = p;
323         texcoord.q = q;
324 }
325
326 // Textures
327
328 void GlState::glActiveTexture(void *user_data, unsigned index)
329 {
330         reinterpret_cast<GlState *>(user_data)->active_tex = index-GL_TEXTURE0;
331 }
332
333 void GlState::glBindTexture(void *user_data, GLenum target, unsigned id)
334 {
335         reinterpret_cast<GlState *>(user_data)->set_current_texture(target, id);
336 }
337
338 void GlState::glTexImage2D(void *user_data, GLenum target, int level, int ifmt, int width, int height, int, GLenum, GLenum, const void *)
339 {
340         if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
341                 tex->set_image_2d(level, ifmt, width, height);
342 }
343
344 void GlState::glTexParameteri(void *user_data, GLenum target, GLenum param, int value)
345 { glTexParameteriv(user_data, target, param, &value); }
346
347 void GlState::glTexParameteriv(void *user_data, GLenum target, GLenum param, const int *values)
348 {
349         if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
350                 tex->set_parameter(param, values);
351 }
352
353 void GlState::glDeleteTextures(void *user_data, int count, const unsigned *ids)
354 {
355         for(int i=0; i<count; ++i)
356                 reinterpret_cast<GlState *>(user_data)->textures.erase(ids[i]);
357 }
358
359 // Vertex arrays
360
361 void GlState::glVertexPointer(void *user_data, int size, GLenum type, int stride, const void *data)
362 {
363         GlState *self = reinterpret_cast<GlState *>(user_data);
364         self->vertex_array.set(size, type, false, stride, self->array_buffer, reinterpret_cast<long>(data));
365 }
366
367 void GlState::glNormalPointer(void *user_data, GLenum type, int stride, const void *data)
368 {
369         GlState *self = reinterpret_cast<GlState *>(user_data);
370         self->normal_array.set(3, type, true, stride, self->array_buffer, reinterpret_cast<long>(data));
371 }
372
373 void GlState::glColorPointer(void *user_data, int size, GLenum type, int stride, const void *data)
374 {
375         GlState *self = reinterpret_cast<GlState *>(user_data);
376         self->color_array.set(size, type, true, stride, self->array_buffer, reinterpret_cast<long>(data));
377 }
378
379 void GlState::glClientActiveTexture(void *user_data, unsigned index)
380 {
381         reinterpret_cast<GlState *>(user_data)->client_active_tex = index-GL_TEXTURE0;
382 }
383
384 void GlState::glTexCoordPointer(void *user_data, int size, GLenum type, int stride, const void *data)
385 {
386         GlState *self = reinterpret_cast<GlState *>(user_data);
387         self->texcoord_arrays[self->client_active_tex].set(size, type, false, stride, self->array_buffer, reinterpret_cast<long>(data));
388 }
389
390 void GlState::glVertexAttribPointer(void *user_data, unsigned index, int size, GLenum type, int norm, int stride, const void *data)
391 {
392         GlState *self = reinterpret_cast<GlState *>(user_data);
393         self->attrib_arrays[index].set(size, type, norm, stride, self->array_buffer, reinterpret_cast<long>(data));
394 }
395
396 // Buffer objects
397
398 void GlState::glBindBuffer(void *user_data, GLenum target, unsigned id)
399 { reinterpret_cast<GlState *>(user_data)->set_current_buffer(target, id); }
400
401 void GlState::glBufferData(void *user_data, GLenum target, int size, const void *data, GLenum usage)
402 {
403         if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
404                 buf->set_data(size, data, usage);
405 }
406
407 void GlState::glBufferSubData(void *user_data, GLenum target, int offset, int size, const void *data)
408 {
409         if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
410                 buf->set_sub_data(offset, size, data);
411 }
412
413 void GlState::glDeleteBuffers(void *user_data, int count, const unsigned *ids)
414 {
415         for(int i=0; i<count; ++i)
416                 reinterpret_cast<GlState *>(user_data)->buffers.erase(ids[i]);
417 }
418
419 void GlState::glDrawElements(void *user_data, GLenum, int, GLenum type, const void *)
420 {
421         if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->element_buffer)
422                 buf->content.update_elements(type);
423 }
424
425 void GlState::glDrawRangeElements(void *user_data, GLenum, unsigned, unsigned, int, GLenum type, const void *)
426 {
427         if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->element_buffer)
428                 buf->content.update_elements(type);
429 }