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