Unofficial OpenGL Software Development Kit: C:/Projects/distro/glsdk_0_5_1/glmesh/include/glmesh/VertexWriter.h Source File

Unofficial OpenGL Software Development Kit 0.5.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VertexWriter.h
Go to the documentation of this file.
1
2 #ifndef GLSDK_MESH_VERTEX_WRITER_H
3 #define GLSDK_MESH_VERTEX_WRITER_H
4
11 #include <vector>
12 #include <string>
13 #include <exception>
14 #include <glm/glm.hpp>
15 #include <glm/gtc/half_float.hpp>
16 #include "VertexFormat.h"
17
18 namespace glmesh
19 {
22
24 class VertexWriterException : public std::exception
25 {
26 public:
27 virtual ~VertexWriterException() throw() {}
28
29 virtual const char *what() const throw() {return message.c_str();}
30
31 protected:
32 std::string message;
33 };
34
36 class MismatchWriterTypeException : public VertexWriterException
37 {
38 public:
39 MismatchWriterTypeException(int eRequiredType, const std::string &realType);
40 };
41
43
44 namespace _detail
45 {
46 template<typename BaseType>
47 void VerifyType(const VertexFormat &fmt, size_t currAttrib);
48
49 template<>
50 inline void VerifyType<glm::half>(const VertexFormat &fmt, size_t currAttrib)
51 {
52 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
53 if(eType != VDT_HALF_FLOAT)
54 throw MismatchWriterTypeException(eType, "half");
55 }
56
57 template<>
58 inline void VerifyType<GLfloat>(const VertexFormat &fmt, size_t currAttrib)
59 {
60 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
61 if(eType != VDT_SINGLE_FLOAT)
62 throw MismatchWriterTypeException(eType, "float");
63 }
64
65 template<>
66 inline void VerifyType<GLdouble>(const VertexFormat &fmt, size_t currAttrib)
67 {
68 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
69 if(eType != VDT_DOUBLE_FLOAT)
70 throw MismatchWriterTypeException(eType, "double");
71 }
72
73 template<>
74 inline void VerifyType<GLbyte>(const VertexFormat &fmt, size_t currAttrib)
75 {
76 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
77 if(eType != VDT_SIGN_BYTE)
78 throw MismatchWriterTypeException(eType, "signed byte");
79 }
80
81 template<>
82 inline void VerifyType<GLubyte>(const VertexFormat &fmt, size_t currAttrib)
83 {
84 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
85 if(eType != VDT_UNSIGN_BYTE)
86 throw MismatchWriterTypeException(eType, "unsigned byte");
87 }
88
89 template<>
90 inline void VerifyType<GLshort>(const VertexFormat &fmt, size_t currAttrib)
91 {
92 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
93 if(eType != VDT_SIGN_SHORT)
94 throw MismatchWriterTypeException(eType, "signed short");
95 }
96
97 template<>
98 inline void VerifyType<GLushort>(const VertexFormat &fmt, size_t currAttrib)
99 {
100 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
101 if(eType != VDT_UNSIGN_SHORT)
102 throw MismatchWriterTypeException(eType, "unsigned short");
103 }
104
105 template<>
106 inline void VerifyType<GLint>(const VertexFormat &fmt, size_t currAttrib)
107 {
108 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
109 if(eType != VDT_SIGN_INT)
110 throw MismatchWriterTypeException(eType, "signed int");
111 }
112
113 template<>
114 inline void VerifyType<GLuint>(const VertexFormat &fmt, size_t currAttrib)
115 {
116 VertexDataType eType = fmt.GetAttribDesc(currAttrib).GetVertexDataType();
117 if(eType != VDT_UNSIGN_INT)
118 throw MismatchWriterTypeException(eType, "unsigned int");
119 }
120
121 template<typename BaseType>
122 inline void ExtractData(void *pData, const glm::detail::tvec4<BaseType> &val);
123
124 template<>
125 inline void ExtractData<glm::half>(void *pData, const glm::detail::tvec4<glm::half> &val)
126 {
127 glm::half *theData = (glm::half*)pData;
128 theData[0] = val[0];
129 theData[1] = val[1];
130 theData[2] = val[2];
131 theData[3] = val[3];
132 }
133
134 template<>
135 inline void ExtractData<GLfloat>(void *pData, const glm::detail::tvec4<GLfloat> &val)
136 {
137 GLfloat *theData = (GLfloat*)pData;
138 theData[0] = val[0];
139 theData[1] = val[1];
140 theData[2] = val[2];
141 theData[3] = val[3];
142 }
143
144 template<>
145 inline void ExtractData<GLdouble>(void *pData, const glm::detail::tvec4<GLdouble> &val)
146 {
147 GLdouble *theData = (GLdouble*)pData;
148 theData[0] = val[0];
149 theData[1] = val[1];
150 theData[2] = val[2];
151 theData[3] = val[3];
152 }
153
154 template<>
155 inline void ExtractData<GLbyte>(void *pData, const glm::detail::tvec4<GLbyte> &val)
156 {
157 GLbyte *theData = (GLbyte*)pData;
158 theData[0] = val[0];
159 theData[1] = val[1];
160 theData[2] = val[2];
161 theData[3] = val[3];
162 }
163
164 template<>
165 inline void ExtractData<GLubyte>(void *pData, const glm::detail::tvec4<GLubyte> &val)
166 {
167 GLubyte *theData = (GLubyte*)pData;
168 theData[0] = val[0];
169 theData[1] = val[1];
170 theData[2] = val[2];
171 theData[3] = val[3];
172 }
173
174 template<>
175 inline void ExtractData<GLshort>(void *pData, const glm::detail::tvec4<GLshort> &val)
176 {
177 GLshort *theData = (GLshort*)pData;
178 theData[0] = val[0];
179 theData[1] = val[1];
180 theData[2] = val[2];
181 theData[3] = val[3];
182 }
183
184 template<>
185 inline void ExtractData<GLushort>(void *pData, const glm::detail::tvec4<GLushort> &val)
186 {
187 GLushort *theData = (GLushort*)pData;
188 theData[0] = val[0];
189 theData[1] = val[1];
190 theData[2] = val[2];
191 theData[3] = val[3];
192 }
193
194 template<>
195 inline void ExtractData<GLint>(void *pData, const glm::detail::tvec4<GLint> &val)
196 {
197 GLint *theData = (GLint*)pData;
198 theData[0] = val[0];
199 theData[1] = val[1];
200 theData[2] = val[2];
201 theData[3] = val[3];
202 }
203
204 template<>
205 inline void ExtractData<GLuint>(void *pData, const glm::detail::tvec4<GLuint> &val)
206 {
207 GLuint *theData = (GLuint*)pData;
208 theData[0] = val[0];
209 theData[1] = val[1];
210 theData[2] = val[2];
211 theData[3] = val[3];
212 }
213
214 template<typename BaseType> struct InfoTraits;
215
216 template<>
217 struct InfoTraits<GLubyte>
218 {
219 static const GLubyte zero;
220 static const GLubyte one;
221 };
222
223 template<>
224 struct InfoTraits<GLbyte>
225 {
226 static const GLbyte zero;
227 static const GLbyte one;
228 };
229
230 template<>
231 struct InfoTraits<GLushort>
232 {
233 static const GLushort zero;
234 static const GLushort one;
235 };
236
237 template<>
238 struct InfoTraits<GLshort>
239 {
240 static const GLshort zero;
241 static const GLshort one;
242 };
243
244 template<>
245 struct InfoTraits<GLuint>
246 {
247 static const GLuint zero;
248 static const GLuint one;
249 };
250
251 template<>
252 struct InfoTraits<GLint>
253 {
254 static const GLint zero;
255 static const GLint one;
256 };
257
258 template<>
259 struct InfoTraits<glm::half>
260 {
261 static const glm::half zero;
262 static const glm::half one;
263 };
264
265 template<>
266 struct InfoTraits<GLfloat>
267 {
268 static const GLfloat zero;
269 static const GLfloat one;
270 };
271
272 template<>
273 struct InfoTraits<GLdouble>
274 {
275 static const GLdouble zero;
276 static const GLdouble one;
277 };
278
279 }
280
283
318 template<typename Sink>
319 class VertexWriter
320 {
321 public:
323 VertexWriter() : m_currAttrib(0) {}
324
364
365
366 template<typename BaseType>
367 void Attrib(BaseType x)
368 {
369 Attrib<BaseType>(glm::detail::tvec4<BaseType>(x,
370 _detail::InfoTraits<BaseType>::zero,
371 _detail::InfoTraits<BaseType>::zero,
372 _detail::InfoTraits<BaseType>::one));
373 }
374
375 template<typename BaseType>
376 void Attrib(BaseType x, BaseType y)
377 {
378 Attrib<BaseType>(glm::detail::tvec4<BaseType>(x, y,
379 _detail::InfoTraits<BaseType>::zero,
380 _detail::InfoTraits<BaseType>::one));
381 }
382
383 template<typename BaseType>
384 void Attrib(const glm::detail::tvec2<BaseType> &val)
385 {
386 Attrib<BaseType>(glm::detail::tvec4<BaseType>(val,
387 _detail::InfoTraits<BaseType>::zero,
388 _detail::InfoTraits<BaseType>::one));
389 }
390
391 template<typename BaseType>
392 void Attrib(BaseType x, BaseType y, BaseType z)
393 {
394 Attrib<BaseType>(glm::detail::tvec4<BaseType>(x, y, z, _detail::InfoTraits<BaseType>::one));
395 }
396
397 template<typename BaseType>
398 void Attrib(const glm::detail::tvec3<BaseType> &val)
399 {
400 Attrib<BaseType>(glm::detail::tvec4<BaseType>(val, _detail::InfoTraits<BaseType>::one));
401 }
402
403 template<typename BaseType>
404 void Attrib(BaseType x, BaseType y, BaseType z, BaseType w)
405 {
406 Attrib<BaseType>(glm::detail::tvec4<BaseType>(x, y, z, w));
407 }
408
409 template<typename BaseType>
410 void Attrib(const glm::detail::tvec4<BaseType> &val)
411 {
412 _detail::VerifyType<BaseType>(Owner()->GetVertexFormat(), m_currAttrib);
413 char theData[sizeof(GLdouble) * 4];
414 _detail::ExtractData(theData, val);
415 Owner()->WriteAttribute(theData, sizeof(BaseType), m_currAttrib);
416 IncrementCurrAttrib();
417 }
418
419
421
422 protected:
424 size_t GetCurrAttrib() const {return m_currAttrib;}
425
426 private:
427 void IncrementCurrAttrib()
428 {
429 ++m_currAttrib;
430 if(Owner()->GetVertexFormat().GetNumAttribs() == m_currAttrib)
431 m_currAttrib = 0;
432 }
433
434 Sink *Owner() {return static_cast<Sink*>(this);}
435 const Sink *Owner() const {return static_cast<const Sink*>(this);}
436
437 size_t m_currAttrib;
438 };
439
441 }
442
443
444
445 #endif //GLSDK_MESH_VERTEX_WRITER_H