]> git.tdb.fi Git - libs/gl.git/blob - source/builders/vertexarraybuilder.cpp
Make VertexFormat capable of storing type information
[libs/gl.git] / source / builders / vertexarraybuilder.cpp
1 #include "vertexarray.h"
2 #include "vertexarraybuilder.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8
9 VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
10         array(a)
11 { }
12
13 void VertexArrayBuilder::vertex_(const Vector4 &vtx)
14 {
15         char *ptr = array.append();
16         const VertexFormat &format = array.get_format();
17         for(const UInt16 *a=format.begin(); a!=format.end(); ++a)
18         {
19                 unsigned sem = get_attribute_semantic(*a);
20                 DataType type = get_attribute_source_type(*a);
21                 unsigned cc = get_attribute_component_count(*a);
22
23                 if(sem<attr.size())
24                 {
25                         const Vector4 &value = (sem==0 ? vtx : attr[sem]);
26                         if(type==UNSIGNED_BYTE)
27                                 store_attribute<UInt8>(ptr, value, cc);
28                         else if(type==BYTE)
29                                 store_attribute<Int8>(ptr, value, cc);
30                         else if(type==UNSIGNED_SHORT)
31                                 store_attribute<UInt16>(ptr, value, cc);
32                         else if(type==SHORT)
33                                 store_attribute<Int16>(ptr, value, cc);
34                         else if(type==UNSIGNED_INT)
35                                 store_attribute<UInt32>(ptr, value, cc);
36                         else if(type==INT)
37                                 store_attribute<Int32>(ptr, value, cc);
38                         else if(type==FLOAT)
39                                 store_attribute<float>(ptr, value, cc);
40                 }
41
42                 ptr += get_attribute_size(*a);
43         }
44 }
45
46 template<typename T>
47 void VertexArrayBuilder::store_attribute(char *ptr, const Vector4 &value, unsigned count)
48 {
49         T *tptr = reinterpret_cast<T *>(ptr);
50         for(unsigned i=0; i<count; ++i)
51         {
52                 if(!numeric_limits<T>::is_integer)
53                         *tptr++ = value[i];
54                 else if(numeric_limits<T>::is_signed)
55                         *tptr++ = round(min(max(value[i], -1.0f), 1.0f)*numeric_limits<T>::max());
56                 else
57                         *tptr++ = round(min(max(value[i], 0.0f), 1.0f)*numeric_limits<T>::max());
58         }
59 }
60
61 } // namespace GL
62 } // namespace Msp