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