+#include <limits>
#include "vertexarray.h"
#include "vertexarraybuilder.h"
+using namespace std;
+
namespace Msp {
namespace GL {
void VertexArrayBuilder::vertex_(const Vector4 &vtx)
{
- float *ptr = array.append();
- const VertexFormat &format = array.get_format();
- for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
+ char *ptr = array.append();
+ for(VertexAttribute a: array.get_format())
{
- unsigned sz = get_component_size(*c);
- unsigned t = get_component_type(*c);
- if(t>=attr.size())
- ptr += sz;
- else if(*c==COLOR4_UBYTE)
+ unsigned sem = get_attribute_semantic(a);
+ if(!is_padding(a) && sem<attr.size())
{
- union { unsigned char c[4]; float f; } u;
- u.c[0] = static_cast<unsigned char>(attr[t].x*255);
- u.c[1] = static_cast<unsigned char>(attr[t].y*255);
- u.c[2] = static_cast<unsigned char>(attr[t].z*255);
- u.c[3] = static_cast<unsigned char>(attr[t].w*255);
- *ptr++ = u.f;
+ bool integer = is_integer_attribute(a);
+ DataType type = get_attribute_source_type(a);
+ unsigned cc = get_attribute_component_count(a);
+
+ const Vector4 &value = (sem==0 ? vtx : attr[sem]);
+ if(type==UNSIGNED_BYTE)
+ store_attribute<uint8_t>(ptr, value, !integer, cc);
+ else if(type==BYTE)
+ store_attribute<int8_t>(ptr, value, !integer, cc);
+ else if(type==UNSIGNED_SHORT)
+ store_attribute<uint16_t>(ptr, value, !integer, cc);
+ else if(type==SHORT)
+ store_attribute<int16_t>(ptr, value, !integer, cc);
+ else if(type==UNSIGNED_INT)
+ store_attribute<uint32_t>(ptr, value, !integer, cc);
+ else if(type==INT)
+ store_attribute<int32_t>(ptr, value, !integer, cc);
+ else if(type==FLOAT)
+ store_attribute<float>(ptr, value, false, cc);
}
+
+ ptr += get_attribute_size(a);
+ }
+}
+
+template<typename T>
+void VertexArrayBuilder::store_attribute(char *ptr, const Vector4 &value, bool normalize, unsigned count)
+{
+ T *tptr = reinterpret_cast<T *>(ptr);
+ for(unsigned i=0; i<count; ++i)
+ {
+ if(!numeric_limits<T>::is_integer || !normalize)
+ *tptr++ = value[i];
+ else if(numeric_limits<T>::is_signed)
+ *tptr++ = round(min(max(value[i], -1.0f), 1.0f)*numeric_limits<T>::max());
else
- {
- const Vector4 &v = (t==0 ? vtx : attr[t]);
- *ptr++ = v.x;
- if(sz>=2) *ptr++ = v.y;
- if(sz>=3) *ptr++ = v.z;
- if(sz>=4) *ptr++ = v.w;
- }
+ *tptr++ = round(min(max(value[i], 0.0f), 1.0f)*numeric_limits<T>::max());
}
}