3 This file is part of gldbg
4 Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
8 #include <msp/core/except.h>
20 float normalize<GLubyte>(GLubyte v)
24 float normalize<GLbyte>(GLbyte v)
25 { return (2*v+1)/255.0f; }
27 template<typename T, void (*func)(void *, float)>
28 void wrap_normalized(void *user_data, T v0)
29 { func(user_data, v0); }
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); }
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); }
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); }
43 template<typename T, void (*func)(void *, T)>
44 void wrap_array(void *user_data, const T *v)
45 { func(user_data, v[0]); }
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]); }
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]); }
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]); }
68 x(0), y(0), z(0), w(1)
73 decoder(gldecoder_new(this, NULL)),
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)
84 texcoord_arrays[i].kind = GL_TEXTURE_COORD_ARRAY;
85 texcoord_arrays[i].index = i;
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>;
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;
115 decoder->glVertexPointer = glVertexPointer;
116 decoder->glNormalPointer = glNormalPointer;
117 decoder->glColorPointer = glColorPointer;
118 decoder->glClientActiveTexture = glClientActiveTexture;
119 decoder->glClientActiveTextureARB = glClientActiveTexture;
120 decoder->glTexCoordPointer = glTexCoordPointer;
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;
131 decoder->glDrawElements = glDrawElements;
132 decoder->glDrawRangeElements = glDrawRangeElements;
133 decoder->glDrawRangeElementsEXT = glDrawRangeElements;
138 gldecoder_delete(decoder);
141 int GlState::decode(const char *data, unsigned len)
143 return gldecoder_decode(decoder, data, len);
146 const TextureState &GlState::get_texture(unsigned id) const
148 TextureMap::const_iterator i = textures.find(id);
149 if(i==textures.end())
150 throw KeyError("Unknown texture");
154 const BufferState &GlState::get_buffer(unsigned id) const
156 BufferMap::const_iterator i = buffers.find(id);
158 throw KeyError("Unknown buffer");
162 const BufferState *GlState::get_current_buffer(GLenum target) const
164 return const_cast<GlState *>(this)->get_current_buffer(target);
167 bool &GlState::get_boolean_state(GLenum state)
169 if(state==GL_VERTEX_ARRAY)
170 return vertex_array.enabled;
171 else if(state==GL_NORMAL_ARRAY)
172 return normal_array.enabled;
173 else if(state==GL_COLOR_ARRAY)
174 return color_array.enabled;
175 else if(state==GL_TEXTURE_COORD_ARRAY)
176 return texcoord_arrays[client_active_tex].enabled;
182 TextureState *GlState::get_current_texture(GLenum target)
184 return texunits[active_tex].get_current_texture(target);
187 BufferState *GlState::get_current_buffer(GLenum target)
189 if(target==GL_ARRAY_BUFFER)
191 else if(target==GL_ELEMENT_ARRAY_BUFFER)
192 return element_buffer;
196 const ArrayState &GlState::get_array(GLenum array) const
198 if(array==GL_VERTEX_ARRAY)
200 else if(array==GL_NORMAL_ARRAY)
202 else if(array==GL_COLOR_ARRAY)
204 else if(array==GL_TEXTURE_COORD_ARRAY)
205 return texcoord_arrays[client_active_tex];
207 throw InvalidParameterValue("Invalid array");
210 const ArrayState &GlState::get_texture_coord_array(unsigned index) const
212 return texcoord_arrays[index];
215 const ArrayState &GlState::get_attrib_array(unsigned index) const
217 map<unsigned, ArrayState>::const_iterator i = attrib_arrays.find(index);
218 if(i!=attrib_arrays.end())
221 // XXX Return a dummy?
222 throw KeyError("Unknown attribute array");
225 void GlState::set_current_texture(GLenum target, unsigned id)
227 TexUnitState &unit = texunits[active_tex];
228 TextureState *tex = 0;
235 tex->target = target;
237 else if(tex->target!=target)
240 unit.set_current_texture(target, tex);
243 void GlState::set_current_buffer(GLenum target, unsigned id)
245 BufferState *buf = 0;
253 if(target==GL_ARRAY_BUFFER)
255 else if(target==GL_ELEMENT_ARRAY_BUFFER)
256 element_buffer = buf;
261 void GlState::glEnableClientState(void *user_data, GLenum state)
263 reinterpret_cast<GlState *>(user_data)->get_boolean_state(state) = true;
266 void GlState::glDisableClientState(void *user_data, GLenum state)
268 reinterpret_cast<GlState *>(user_data)->get_boolean_state(state) = false;
271 void GlState::glEnableVertexAttribArray(void *user_data, unsigned index)
273 reinterpret_cast<GlState *>(user_data)->attrib_arrays[index].enabled = true;
276 void GlState::glDisableVertexAttribArray(void *user_data, unsigned index)
278 reinterpret_cast<GlState *>(user_data)->attrib_arrays[index].enabled = false;
283 void GlState::glColor3f(void *user_data, float r, float g, float b)
284 { glColor4f(user_data, r, g, b, 1.0f); }
286 void GlState::glColor4f(void *user_data, float r, float g, float b, float a)
288 Vector4 &color = reinterpret_cast<GlState *>(user_data)->color;
295 void GlState::glNormal3f(void *user_data, float x, float y, float z)
297 Vector3 &normal = reinterpret_cast<GlState *>(user_data)->normal;
303 void GlState::glTexCoord1f(void *user_data, float s)
304 { glTexCoord4f(user_data, s, 0.0f, 0.0f, 1.0f); }
306 void GlState::glTexCoord2f(void *user_data, float s, float t)
307 { glTexCoord4f(user_data, s, t, 0.0f, 1.0f); }
309 void GlState::glTexCoord3f(void *user_data, float s, float t, float p)
310 { glTexCoord4f(user_data, s, t, p, 1.0f); }
312 void GlState::glTexCoord4f(void *user_data, float s, float t, float p, float q)
314 unsigned index = reinterpret_cast<GlState *>(user_data)->active_tex;
315 glMultiTexCoord4f(user_data, index, s, t, p, q);
318 void GlState::glMultiTexCoord4f(void *user_data, unsigned index, float s, float t, float p, float q)
320 Vector4 &texcoord = reinterpret_cast<GlState *>(user_data)->texcoord[index];
329 void GlState::glActiveTexture(void *user_data, unsigned index)
331 reinterpret_cast<GlState *>(user_data)->active_tex = index-GL_TEXTURE0;
334 void GlState::glBindTexture(void *user_data, GLenum target, unsigned id)
336 reinterpret_cast<GlState *>(user_data)->set_current_texture(target, id);
339 void GlState::glTexImage2D(void *user_data, GLenum target, int level, int ifmt, int width, int height, int, GLenum, GLenum, const void *)
341 if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
342 tex->set_image_2d(level, ifmt, width, height);
345 void GlState::glTexParameteri(void *user_data, GLenum target, GLenum param, int value)
346 { glTexParameteriv(user_data, target, param, &value); }
348 void GlState::glTexParameteriv(void *user_data, GLenum target, GLenum param, const int *values)
350 if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
351 tex->set_parameter(param, values);
354 void GlState::glDeleteTextures(void *user_data, int count, const unsigned *ids)
356 for(int i=0; i<count; ++i)
357 reinterpret_cast<GlState *>(user_data)->textures.erase(ids[i]);
362 void GlState::glVertexPointer(void *user_data, int size, GLenum type, int stride, const void *data)
364 GlState *self = reinterpret_cast<GlState *>(user_data);
365 self->vertex_array.set(size, type, false, stride, self->array_buffer, reinterpret_cast<long>(data));
368 void GlState::glNormalPointer(void *user_data, GLenum type, int stride, const void *data)
370 GlState *self = reinterpret_cast<GlState *>(user_data);
371 self->normal_array.set(3, type, true, stride, self->array_buffer, reinterpret_cast<long>(data));
374 void GlState::glColorPointer(void *user_data, int size, GLenum type, int stride, const void *data)
376 GlState *self = reinterpret_cast<GlState *>(user_data);
377 self->color_array.set(size, type, true, stride, self->array_buffer, reinterpret_cast<long>(data));
380 void GlState::glClientActiveTexture(void *user_data, unsigned index)
382 reinterpret_cast<GlState *>(user_data)->client_active_tex = index-GL_TEXTURE0;
385 void GlState::glTexCoordPointer(void *user_data, int size, GLenum type, int stride, const void *data)
387 GlState *self = reinterpret_cast<GlState *>(user_data);
388 self->texcoord_arrays[self->client_active_tex].set(size, type, false, stride, self->array_buffer, reinterpret_cast<long>(data));
391 void GlState::glVertexAttribPointer(void *user_data, unsigned index, int size, GLenum type, int norm, int stride, const void *data)
393 GlState *self = reinterpret_cast<GlState *>(user_data);
394 self->attrib_arrays[index].set(size, type, norm, stride, self->array_buffer, reinterpret_cast<long>(data));
399 void GlState::glBindBuffer(void *user_data, GLenum target, unsigned id)
400 { reinterpret_cast<GlState *>(user_data)->set_current_buffer(target, id); }
402 void GlState::glBufferData(void *user_data, GLenum target, int size, const void *data, GLenum usage)
404 if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
405 buf->set_data(size, data, usage);
408 void GlState::glBufferSubData(void *user_data, GLenum target, int offset, int size, const void *data)
410 if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
411 buf->set_sub_data(offset, size, data);
414 void GlState::glDeleteBuffers(void *user_data, int count, const unsigned *ids)
416 for(int i=0; i<count; ++i)
417 reinterpret_cast<GlState *>(user_data)->buffers.erase(ids[i]);
420 void GlState::glDrawElements(void *user_data, GLenum, int, GLenum type, const void *)
422 if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->element_buffer)
423 buf->content.update_elements(type);
426 void GlState::glDrawRangeElements(void *user_data, GLenum, unsigned, unsigned, int, GLenum type, const void *)
428 if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->element_buffer)
429 buf->content.update_elements(type);