3 This file is part of gldbg
4 Copyright © 2009 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)),
78 decoder->glColor3ub = wrap_normalized<GLubyte, glColor3f>;
79 decoder->glColor3ubv = wrap_array<GLubyte, wrap_normalized<GLubyte, glColor3f> >;
80 decoder->glColor4ub = wrap_normalized<GLubyte, glColor4f>;
81 decoder->glColor4ubv = wrap_array<GLubyte, wrap_normalized<GLubyte, glColor4f> >;
82 decoder->glColor3f = glColor3f;
83 decoder->glColor3fv = wrap_array<float, glColor3f>;
84 decoder->glColor4f = glColor4f;
85 decoder->glColor4fv = wrap_array<float, glColor4f>;
86 decoder->glTexCoord1f = glTexCoord1f;
87 decoder->glTexCoord1fv = wrap_array<float, glTexCoord1f>;
88 decoder->glTexCoord2f = glTexCoord2f;
89 decoder->glTexCoord2fv = wrap_array<float, glTexCoord2f>;
90 decoder->glTexCoord3f = glTexCoord3f;
91 decoder->glTexCoord3fv = wrap_array<float, glTexCoord3f>;
92 decoder->glTexCoord4f = glTexCoord4f;
93 decoder->glTexCoord4fv = wrap_array<float, glTexCoord4f>;
94 decoder->glNormal3f = glNormal3f;
95 decoder->glNormal3fv = wrap_array<float, glNormal3f>;
96 decoder->glActiveTexture = glActiveTexture;
97 decoder->glActiveTextureARB = glActiveTexture;
98 decoder->glBindTexture = glBindTexture;
99 decoder->glTexImage2D = glTexImage2D;
100 decoder->glTexParameteri = glTexParameteri;
101 decoder->glTexParameteriv = glTexParameteriv;
102 decoder->glDeleteTextures = glDeleteTextures;
103 decoder->glBindBuffer = glBindBuffer;
104 decoder->glBindBufferARB = glBindBuffer;
105 decoder->glBufferData = glBufferData;
106 decoder->glBufferDataARB = glBufferData;
107 decoder->glBufferSubData = glBufferSubData;
108 decoder->glBufferSubDataARB = glBufferSubData;
113 gldecoder_delete(decoder);
116 int GlState::decode(const char *data, unsigned len)
118 return gldecoder_decode(decoder, data, len);
121 const TextureState &GlState::get_texture(unsigned id) const
123 TextureMap::const_iterator i = textures.find(id);
124 if(i==textures.end())
125 throw KeyError("Unknown texture");
129 const BufferState &GlState::get_buffer(unsigned id) const
131 BufferMap::const_iterator i = buffers.find(id);
133 throw KeyError("Unknown buffer");
137 const BufferState *GlState::get_current_buffer(GLenum target) const
139 return const_cast<GlState *>(this)->get_current_buffer(target);
142 TextureState *GlState::get_current_texture(GLenum target)
144 return texunits[active_tex].get_current_texture(target);
147 BufferState *GlState::get_current_buffer(GLenum target)
149 if(target==GL_ARRAY_BUFFER)
151 else if(target==GL_ELEMENT_ARRAY_BUFFER)
152 return element_buffer;
156 void GlState::set_current_texture(GLenum target, unsigned id)
158 TexUnitState &unit = texunits[active_tex];
159 TextureState *tex = 0;
166 tex->target = target;
168 else if(tex->target!=target)
171 unit.set_current_texture(target, tex);
174 void GlState::set_current_buffer(GLenum target, unsigned id)
176 BufferState *buf = 0;
184 if(target==GL_ARRAY_BUFFER)
186 else if(target==GL_ELEMENT_ARRAY_BUFFER)
187 element_buffer = buf;
192 void GlState::glColor3f(void *user_data, float r, float g, float b)
193 { glColor4f(user_data, r, g, b, 1.0f); }
195 void GlState::glColor4f(void *user_data, float r, float g, float b, float a)
197 Vector4 &color = reinterpret_cast<GlState *>(user_data)->color;
204 void GlState::glNormal3f(void *user_data, float x, float y, float z)
206 Vector3 &normal = reinterpret_cast<GlState *>(user_data)->normal;
212 void GlState::glTexCoord1f(void *user_data, float s)
213 { glTexCoord4f(user_data, s, 0.0f, 0.0f, 1.0f); }
215 void GlState::glTexCoord2f(void *user_data, float s, float t)
216 { glTexCoord4f(user_data, s, t, 0.0f, 1.0f); }
218 void GlState::glTexCoord3f(void *user_data, float s, float t, float p)
219 { glTexCoord4f(user_data, s, t, p, 1.0f); }
221 void GlState::glTexCoord4f(void *user_data, float s, float t, float p, float q)
223 unsigned index = reinterpret_cast<GlState *>(user_data)->active_tex;
224 glMultiTexCoord4f(user_data, index, s, t, p, q);
227 void GlState::glMultiTexCoord4f(void *user_data, unsigned index, float s, float t, float p, float q)
229 Vector4 &texcoord = reinterpret_cast<GlState *>(user_data)->texcoord[index];
238 void GlState::glActiveTexture(void *user_data, unsigned index)
240 reinterpret_cast<GlState *>(user_data)->active_tex = index-GL_TEXTURE0;
243 void GlState::glBindTexture(void *user_data, GLenum target, unsigned id)
244 { reinterpret_cast<GlState *>(user_data)->set_current_texture(target, id); }
246 void GlState::glTexImage2D(void *user_data, GLenum target, int level, int ifmt, int width, int height, int, GLenum, GLenum, const void *)
248 if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
249 tex->set_image_2d(level, ifmt, width, height);
252 void GlState::glTexParameteri(void *user_data, GLenum target, GLenum param, int value)
253 { glTexParameteriv(user_data, target, param, &value); }
255 void GlState::glTexParameteriv(void *user_data, GLenum target, GLenum param, const int *values)
257 if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
258 tex->set_parameter(param, values);
261 void GlState::glDeleteTextures(void *user_data, int count, const unsigned *ids)
263 for(int i=0; i<count; ++i)
264 reinterpret_cast<GlState *>(user_data)->textures.erase(ids[i]);
269 void GlState::glBindBuffer(void *user_data, GLenum target, unsigned id)
270 { reinterpret_cast<GlState *>(user_data)->set_current_buffer(target, id); }
272 void GlState::glBufferData(void *user_data, GLenum target, int size, const void *data, GLenum usage)
274 if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
275 buf->set_data(size, data, usage);
278 void GlState::glBufferSubData(void *user_data, GLenum target, int offset, int size, const void *data)
280 if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
281 buf->set_sub_data(offset, size, data);