]> git.tdb.fi Git - libs/gl.git/commitdiff
Make VertexFormat capable of storing type information
authorMikko Rasa <tdb@tdb.fi>
Wed, 15 Sep 2021 11:11:54 +0000 (14:11 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 15 Sep 2021 18:34:11 +0000 (21:34 +0300)
14 files changed:
source/builders/primitivebuilder.cpp
source/builders/vertexarraybuilder.cpp
source/builders/vertexarraybuilder.h
source/builders/vertexbuilder.h
source/core/mesh.cpp
source/core/mesh.h
source/core/vertexarray.cpp
source/core/vertexarray.h
source/core/vertexformat.cpp
source/core/vertexformat.h
source/core/vertexsetup.cpp
source/render/instancearray.cpp
source/render/object.cpp
source/render/text.cpp

index 1b36943031e027b25e79a6df170535a905a281fa..1a10af4beaa3794e8aa457a9b028b6ec83fd9bb1 100644 (file)
@@ -60,7 +60,7 @@ PrimitiveType PrimitiveBuilder::get_type() const
 void PrimitiveBuilder::vertex_(const Vector4 &v)
 {
        const VertexFormat &format = array.get_format();
-       for(const unsigned char *a=format.begin(); a!=format.end(); ++a)
+       for(const UInt16 *a=format.begin(); a!=format.end(); ++a)
        {
                unsigned sem = get_attribute_semantic(*a);
                if(sem<attr.size())
index 7bc31b22f9d50e3b11c2a4dbce22c9b4dfb0cfee..3afe9a40e2bf22c48869207ef9b73235c9dd784b 100644 (file)
@@ -1,6 +1,8 @@
 #include "vertexarray.h"
 #include "vertexarraybuilder.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GL {
 
@@ -10,31 +12,49 @@ VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
 
 void VertexArrayBuilder::vertex_(const Vector4 &vtx)
 {
-       float *ptr = array.append();
+       char *ptr = array.append();
        const VertexFormat &format = array.get_format();
-       for(const unsigned char *a=format.begin(); a!=format.end(); ++a)
+       for(const UInt16 *a=format.begin(); a!=format.end(); ++a)
        {
                unsigned sem = get_attribute_semantic(*a);
-               unsigned sz = get_attribute_size(*a);
-               if(sem>=attr.size())
-                       ptr += sz;
-               else if(*a==COLOR4_UBYTE)
+               DataType type = get_attribute_source_type(*a);
+               unsigned cc = get_attribute_component_count(*a);
+
+               if(sem<attr.size())
                {
-                       union { unsigned char c[4]; float f; } u;
-                       u.c[0] = static_cast<unsigned char>(attr[sem].x*255);
-                       u.c[1] = static_cast<unsigned char>(attr[sem].y*255);
-                       u.c[2] = static_cast<unsigned char>(attr[sem].z*255);
-                       u.c[3] = static_cast<unsigned char>(attr[sem].w*255);
-                       *ptr++ = u.f;
+                       const Vector4 &value = (sem==0 ? vtx : attr[sem]);
+                       if(type==UNSIGNED_BYTE)
+                               store_attribute<UInt8>(ptr, value, cc);
+                       else if(type==BYTE)
+                               store_attribute<Int8>(ptr, value, cc);
+                       else if(type==UNSIGNED_SHORT)
+                               store_attribute<UInt16>(ptr, value, cc);
+                       else if(type==SHORT)
+                               store_attribute<Int16>(ptr, value, cc);
+                       else if(type==UNSIGNED_INT)
+                               store_attribute<UInt32>(ptr, value, cc);
+                       else if(type==INT)
+                               store_attribute<Int32>(ptr, value, cc);
+                       else if(type==FLOAT)
+                               store_attribute<float>(ptr, value, cc);
                }
+
+               ptr += get_attribute_size(*a);
+       }
+}
+
+template<typename T>
+void VertexArrayBuilder::store_attribute(char *ptr, const Vector4 &value, unsigned count)
+{
+       T *tptr = reinterpret_cast<T *>(ptr);
+       for(unsigned i=0; i<count; ++i)
+       {
+               if(!numeric_limits<T>::is_integer)
+                       *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 = (sem==0 ? vtx : attr[sem]);
-                       *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());
        }
 }
 
index 575a47535e9a22ecc5bf5bad91e5343a836790d3..aa723540c5271b2ca3b0b5fbc2913ddca7b45ca2 100644 (file)
@@ -21,6 +21,9 @@ public:
 
 private:
        virtual void vertex_(const Vector4 &);
+
+       template<typename T>
+       void store_attribute(char *, const Vector4 &, unsigned);
 };
 
 } // namespace GL
index cfb4b31b46ba0d1ec003daac38ac6e57323d2175..49a20b5cede9fb872506f6b166c6ff84ed541d1f 100644 (file)
@@ -132,7 +132,7 @@ public:
        { color(Color(r, g, b, a)); }
 
        void color(const Color &c)
-       { attrib(get_attribute_semantic(COLOR4_FLOAT), Vector4(c.r, c.g, c.b, c.a)); }
+       { attrib(get_attribute_semantic(COLOR4), Vector4(c.r, c.g, c.b, c.a)); }
 
        void group(int g0)
        { group(g0, 0, 0, 0); }
index 77da81ebec4a3e51d286dc5477142a46191c9cf0..de05102a358a5665a51b12cd0c345c21441052d5 100644 (file)
@@ -107,7 +107,7 @@ unsigned Mesh::get_n_vertices() const
        return vertices.size();
 }
 
-float *Mesh::modify_vertex(unsigned i)
+char *Mesh::modify_vertex(unsigned i)
 {
        if(vertices.get_format().empty())
                throw invalid_operation("Mesh::modify_vertex");
index 57ffce4722e30efce84ff588741d9b845f7d108b..0111bb7798908348cfb09080631c29d7c3e1f6cc 100644 (file)
@@ -91,7 +91,7 @@ public:
        const VertexSetup &get_vertex_setup() const { return vtx_setup; }
        const Buffer *get_index_buffer() const { return ibuf; }
        unsigned get_n_vertices() const;
-       float *modify_vertex(unsigned);
+       char *modify_vertex(unsigned);
 
        void add_batch(const Batch &b);
        const std::vector<Batch> &get_batches() const { return batches; }
index b3bcb48118b291a114024bd86f7d9016d22a26b0..a13d79cc6b0abd3e0c42362b24dc56fc14e05d27 100644 (file)
@@ -40,7 +40,7 @@ void VertexArray::reserve(unsigned n)
        data.reserve(n*stride);
 }
 
-float *VertexArray::append()
+char *VertexArray::append()
 {
        if(format.empty())
                throw invalid_operation("VertexArray::append");
@@ -50,7 +50,7 @@ float *VertexArray::append()
        return &*(data.end()-stride);
 }
 
-float *VertexArray::modify(unsigned i)
+char *VertexArray::modify(unsigned i)
 {
        if(format.empty())
                throw invalid_operation("VertexArray::modify");
@@ -60,7 +60,7 @@ float *VertexArray::modify(unsigned i)
 
 unsigned VertexArray::get_data_size() const
 {
-       return data.size()*sizeof(float);
+       return data.size();
 }
 
 
index 107d1851f8a70b99a2d5bb24533034a86cc1c883..55ee8d24ae10cb1bff4a4259c3f32d4d1f88aec4 100644 (file)
@@ -37,7 +37,7 @@ public:
 
 private:
        VertexFormat format;
-       std::vector<float> data;
+       std::vector<char> data;
        unsigned stride;
 
        VertexArray(const VertexArray &);
@@ -60,18 +60,18 @@ public:
        void reserve(unsigned);
 
        /// Append a new vertex at the end of the array and return its location.
-       float *append();
+       char *append();
 
        /// Returns the location of a vertex for modification.
-       float *modify(unsigned);
+       char *modify(unsigned);
 private:
        virtual unsigned get_data_size() const;
        virtual const void *get_data_pointer() const { return &data[0]; }
 
 public:
        unsigned size() const { return data.size()/stride; }
-       const std::vector<float> &get_data() const { return data; }
-       const float *operator[](unsigned i) const { return &data[0]+i*stride; }
+       const std::vector<char> &get_data() const { return data; }
+       const char *operator[](unsigned i) const { return &data[0]+i*stride; }
 };
 
 } // namespace GL
index e529f4eb2632770e91ae68b3bd221cc913ca0382..5d0b078fe06f154b2821ab176a2bf9310132c253 100644 (file)
@@ -32,13 +32,25 @@ VertexFormat VertexFormat::operator,(VertexAttribute a) const
        return r;
 }
 
+VertexFormat VertexFormat::operator,(DataType t) const
+{
+       if(!count)
+               throw invalid_operation("VertexFormat::operator,");
+
+       VertexFormat r = *this;
+       UInt16 &a = r.attributes[r.count-1];
+       a = make_typed_attribute(static_cast<VertexAttribute>(a), t);
+
+       return r;
+}
+
 VertexFormat VertexFormat::operator,(unsigned i) const
 {
        if(!count)
                throw invalid_operation("VertexFormat::operator,");
 
        VertexFormat r = *this;
-       unsigned char &a = r.attributes[r.count-1];
+       UInt16 &a = r.attributes[r.count-1];
        a = make_indexed_attribute(static_cast<VertexAttribute>(a), i);
 
        return r;
@@ -54,7 +66,7 @@ bool VertexFormat::operator==(const VertexFormat &other) const
 unsigned VertexFormat::stride() const
 {
        unsigned s = 0;
-       for(const unsigned char *i=begin(); i!=end(); ++i)
+       for(const UInt16 *i=begin(); i!=end(); ++i)
                s += get_attribute_size(*i);
        return s;
 }
@@ -62,13 +74,13 @@ unsigned VertexFormat::stride() const
 int VertexFormat::offset(VertexAttribute attr) const
 {
        unsigned sem = get_attribute_semantic(attr);
-       unsigned sz = get_attribute_size(attr);
        unsigned offs = 0;
-       for(const unsigned char *i=begin(); i!=end(); ++i)
+       for(const UInt16 *i=begin(); i!=end(); ++i)
        {
                if(get_attribute_semantic(*i)==sem)
                {
-                       if(get_attribute_size(*i)>=sz)
+                       if(get_attribute_source_type(*i)==get_attribute_source_type(attr) &&
+                               get_attribute_component_count(*i)>=get_attribute_component_count(attr))
                                return offs;
                        else
                                return -1;
@@ -81,26 +93,47 @@ int VertexFormat::offset(VertexAttribute attr) const
 }
 
 
+VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type)
+{
+       if(is_matrix(type) || is_vector(type) || is_image(type))
+               throw invalid_argument("make_typed_attribute");
+
+       return static_cast<VertexAttribute>((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1);
+}
+
 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
 {
        unsigned base = attr;
-       if(attr>=TEXCOORD1 && attr<=TEXCOORD4)
+       if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
        {
                if(index>=4)
                        throw out_of_range("make_indexed_attribute");
        }
-       else if(attr>=RAW_ATTRIB1 && attr<=RAW_ATTRIB4)
-               base &= 7;
-       else if(attr<GENERIC1 || attr>GENERIC4)
+       else if(get_attribute_semantic(attr)==get_attribute_semantic(RAW_ATTRIB1))
+               base &= 0x3FF;
+       else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1))
                throw invalid_argument("make_indexed_attribute");
 
-       if(static_cast<int>((base>>3)+index)>=31)
+       if(get_attribute_semantic(base)+index>=63)
                throw out_of_range("make_indexed_attribute");
 
-       return static_cast<VertexAttribute>(base+index*8);
+       return static_cast<VertexAttribute>(base+(index<<10));
 }
 
 
+bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
+{
+       string::const_iterator j = i;
+       for(; (j!=end && *j!='_' && *name); ++name, ++j)
+               if(*j!=*name)
+                       return false;
+
+       attr = make_typed_attribute(attr, type);
+       i = j;
+
+       return true;
+}
+
 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
 {
        string::const_iterator i = str.begin();
@@ -109,21 +142,30 @@ bool convert_attribute(const string &str, const char *name, int min_size, int ma
                        return false;
        if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
                return false;
-       VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i-'0'-min_size));
+       VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i++-'0'-min_size));
 
-       if(++i!=str.end())
+       while(i!=str.end())
        {
                if(*i!='_' || ++i==str.end())
                        return false;
-               try
+
+               if(isdigit(*i))
                {
-                       result = make_indexed_attribute(result, lexical_cast<unsigned>(string(i, str.end())));
+                       unsigned index = 0;
+                       for(; (i!=str.end() && isdigit(*i)); ++i)
+                               index = index*10+(*i-'0');
+                       result = make_indexed_attribute(result, index);
                }
-               catch(...)
-               {
-                       // The operator>> will throw a more appropriate exception
+               else if(convert_attribute_type(i, str.end(), "UBYTE", result, UNSIGNED_BYTE) ||
+                       convert_attribute_type(i, str.end(), "BYTE", result, BYTE) ||
+                       convert_attribute_type(i, str.end(), "USHORT", result, UNSIGNED_SHORT) ||
+                       convert_attribute_type(i, str.end(), "SHORT", result, SHORT) ||
+                       convert_attribute_type(i, str.end(), "UINT", result, UNSIGNED_INT) ||
+                       convert_attribute_type(i, str.end(), "INT", result, INT) ||
+                       convert_attribute_type(i, str.end(), "FLOAT", result, FLOAT))
+                       ;
+               else
                        return false;
-               }
        }
 
        attr = result;
@@ -133,25 +175,22 @@ bool convert_attribute(const string &str, const char *name, int min_size, int ma
 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
 {
        const string &str = conv.get();
-       if(str=="NORMAL3")
-               a = NORMAL3;
-       else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
-               a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
-       else if(str=="COLOR4_UBYTE")
-               a = COLOR4_UBYTE;
-       else if(str=="TANGENT3")
-               a = TANGENT3;
-       else if(str=="BINORMAL3")
+       try
        {
-               IO::print(IO::cerr, "BINORMAL3 attribute is deprecated\n");
-               a = make_indexed_attribute(GENERIC3, 5);
+               if(convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) ||
+                       convert_attribute(str, "COLOR", 3, 4, a, COLOR3) ||
+                       convert_attribute(str, "NORMAL", 3, 3, a, NORMAL3) ||
+                       convert_attribute(str, "TANGENT", 3, 3, a, TANGENT3) ||
+                       convert_attribute(str, "GROUP", 1, 4, a, GROUP1) ||
+                       convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) ||
+                       convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) ||
+                       convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
+                       return;
        }
-       else if(!convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) &&
-               !convert_attribute(str, "GROUP", 1, 4, a, GROUP1) &&
-               !convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) &&
-               !convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) &&
-               !convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
-               throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
+       catch(...)
+       { }
+
+       throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
 }
 
 } // namespace GL
index 4c485b0423b9568a89b07b5ba164586b94dfa1fc..a503aff83f84f19bcbbb1d9e3009c77fd526d4bb 100644 (file)
@@ -1,7 +1,9 @@
 #ifndef MSP_GL_VERTEXFORMAT_H_
 #define MSP_GL_VERTEXFORMAT_H_
 
+#include <msp/core/inttypes.h>
 #include <msp/strings/lexicalcast.h>
+#include "datatype.h"
 
 namespace Msp {
 namespace GL {
@@ -14,37 +16,49 @@ depends on implementation limits, but is at least five.
 
 RAW_ATTRIB is handled in a special way; creating an indexed attribute based on
 it uses the index as raw attribute number.  Only use it if you know what you
-are doing. */
+are doing.
+
+The values are bitfields laid as follows:
+
+nnnn nn_f gsss _ccc
+      │ │ │  │    └╴Number of components
+      │ │ │  └─────╴Size of one component
+      │ │ └────────╴Signed flag
+      │ └──────────╴Floating-point flag
+      └────────────╴Attribute index (semantic)
+
+This information is presented for internal documentation purposes only; it is
+inadvisable for programs to rely on it.
+*/
 enum VertexAttribute
 {
-       VERTEX2 = 1,
-       VERTEX3,
-       VERTEX4,
-       COLOR4_UBYTE = 8,
-       COLOR3_FLOAT = 10,
-       COLOR4_FLOAT,
-       NORMAL3 = 18,
-       TANGENT3 = 26,
-       GROUP1 = 32,
-       GROUP2,
-       GROUP3,
-       GROUP4,
-       WEIGHT1 = 40,
-       WEIGHT2,
-       WEIGHT3,
-       WEIGHT4,
-       TEXCOORD1 = 48,
-       TEXCOORD2,
-       TEXCOORD3,
-       TEXCOORD4,
-       GENERIC1 = 80,
-       GENERIC2,
-       GENERIC3,
-       GENERIC4,
-       RAW_ATTRIB1 = 248,
-       RAW_ATTRIB2,
-       RAW_ATTRIB3,
-       RAW_ATTRIB4
+       VERTEX2 = 0x01C2,
+       VERTEX3 = 0x01C3,
+       VERTEX4 = 0x01C4,
+       COLOR3 = 0x05C3,
+       COLOR4 = 0x05C4,
+       NORMAL3 = 0x09C3,
+       TANGENT3 = 0x0DC3,
+       GROUP1 = 0x11C1,
+       GROUP2 = 0x11C2,
+       GROUP3 = 0x11C3,
+       GROUP4 = 0x11C4,
+       WEIGHT1 = 0x15C1,
+       WEIGHT2 = 0x15C2,
+       WEIGHT3 = 0x15C3,
+       WEIGHT4 = 0x15C4,
+       TEXCOORD1 = 0x19C1,
+       TEXCOORD2 = 0x19C2,
+       TEXCOORD3 = 0x19C3,
+       TEXCOORD4 = 0x19C4,
+       GENERIC1 = 0x29C1,
+       GENERIC2 = 0x29C2,
+       GENERIC3 = 0x29C3,
+       GENERIC4 = 0x29C4,
+       RAW_ATTRIB1 = 0xFDC1,
+       RAW_ATTRIB2 = 0xFDC2,
+       RAW_ATTRIB3 = 0xFDC3,
+       RAW_ATTRIB4 = 0xFDC4
 };
 
 class VertexFormat
@@ -52,21 +66,22 @@ class VertexFormat
 private:
        enum { MAX_ATTRIBUTES = 15 };
 
-       unsigned char count;
-       unsigned char attributes[MAX_ATTRIBUTES];
+       UInt8 count;
+       UInt16 attributes[MAX_ATTRIBUTES];
 
 public:
        VertexFormat();
        VertexFormat(VertexAttribute);
 
        VertexFormat operator,(VertexAttribute) const;
+       VertexFormat operator,(DataType) const;
        VertexFormat operator,(unsigned) const;
        bool operator==(const VertexFormat &) const;
        bool operator!=(const VertexFormat &other) const { return !(*this==other); }
 
        bool empty() const { return !count; }
-       const unsigned char *begin() const { return attributes; }
-       const unsigned char *end() const { return attributes+count; }
+       const UInt16 *begin() const { return attributes; }
+       const UInt16 *end() const { return attributes+count; }
        unsigned stride() const;
        int offset(VertexAttribute) const;
 };
@@ -74,16 +89,27 @@ public:
 inline VertexFormat operator,(VertexAttribute a1, VertexAttribute a2)
 { return (VertexFormat(a1), a2); }
 
+VertexAttribute make_typed_attribute(VertexAttribute, DataType);
+
+inline VertexAttribute operator,(VertexAttribute a, DataType t)
+{ return make_typed_attribute(a, t); }
+
 VertexAttribute make_indexed_attribute(VertexAttribute, unsigned);
 
 inline VertexAttribute operator,(VertexAttribute a, unsigned i)
 { return make_indexed_attribute(a, i); }
 
-inline unsigned get_attribute_semantic(unsigned char a)
-{ return a>>3; }
+inline unsigned get_attribute_semantic(UInt16 a)
+{ return a>>10; }
+
+inline DataType get_attribute_source_type(UInt16 a)
+{ return static_cast<DataType>((a&0x70)>>4 | (a&0x180)<<1); }
+
+inline unsigned get_attribute_component_count(UInt16 a)
+{ return a&7; }
 
-inline unsigned get_attribute_size(unsigned char a)
-{ return (a&3)+1; }
+inline unsigned get_attribute_size(UInt16 a)
+{ return get_attribute_component_count(a)*get_type_size(get_attribute_source_type(a)); }
 
 void operator>>(const LexicalConverter &, VertexAttribute &);
 
index 86c9f0f237f5cbc9c178da7ebcf082a99454fc9c..8498e043dde57d48c758e4a83957caf2f77e384b 100644 (file)
@@ -101,7 +101,7 @@ bool VertexSetup::verify_format(const VertexFormat &fmt)
 
        unsigned max_attribs = Limits::get_global().max_vertex_attributes;
 
-       for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
+       for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
                if(get_attribute_semantic(*a)>=max_attribs)
                        return false;
 
@@ -135,7 +135,7 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding
                glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id());
 
        const VertexFormat &fmt = array.get_format();
-       unsigned stride = fmt.stride()*sizeof(float);
+       unsigned stride = fmt.stride();
        if(direct)
        {
                glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
@@ -143,30 +143,25 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding
        }
 
        unsigned offset = 0;
-       for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
+       for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
        {
                unsigned sem = get_attribute_semantic(*a);
-               unsigned sz = get_attribute_size(*a);
+               GLenum type = get_gl_type(get_attribute_source_type(*a));
+               unsigned cc = get_attribute_component_count(*a);
                if(direct)
                {
-                       if(*a==COLOR4_UBYTE)
-                               glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
-                       else
-                               glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
+                       glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
                        glVertexArrayAttribBinding(id, sem, binding);
                        glEnableVertexArrayAttrib(id, sem);
                }
                else
                {
-                       if(*a==COLOR4_UBYTE)
-                               glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
-                       else
-                               glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
+                       glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
                        if(ARB_instanced_arrays)
                                glVertexAttribDivisor(sem, divisor);
                        glEnableVertexAttribArray(sem);
                }
-               offset += sz*sizeof(float);
+               offset += get_attribute_size(*a);
        }
 
        if(!direct)
@@ -186,13 +181,13 @@ void VertexSetup::unload()
                glBindVertexArray(id);
                glBindBuffer(GL_ARRAY_BUFFER, 0);
 
-               for(const unsigned char *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
+               for(const UInt16 *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
                {
                        unsigned sem = get_attribute_semantic(*a);
                        glDisableVertexAttribArray(sem);
                        glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
                }
-               for(const unsigned char *a=inst_format.begin(); a!=inst_format.end(); ++a)
+               for(const UInt16 *a=inst_format.begin(); a!=inst_format.end(); ++a)
                {
                        unsigned sem = get_attribute_semantic(*a);
                        glDisableVertexAttribArray(sem);
index 427976129d65b1a4e5f0c82caf725822111a44b7..293fa6cdad8e1ee55384df261a50f22b58997a25 100644 (file)
@@ -110,9 +110,9 @@ void InstanceArray::update_instance_matrix(unsigned index)
 
        const Matrix &m = *instances[index]->get_matrix();
 
-       float *d = instance_data->modify(instances.size()-1);
+       float *d = reinterpret_cast<float *>(instance_data->modify(instances.size()-1)+matrix_offset);
        for(unsigned i=0; i<12; ++i)
-               d[matrix_offset+i] = m(i/4, i%4);
+               d[i] = m(i/4, i%4);
 }
 
 void InstanceArray::render(Renderer &renderer, Tag tag) const
index 38d2e6f282cd3b5378a15c561e1ac91c302dcd0a..067686d205cee62f8b117ce429c75f58c2e1b21a 100644 (file)
@@ -97,8 +97,8 @@ void Object::update_bounding_sphere()
                points.reserve(points.size()+n_vertices);
                for(unsigned j=0; j<n_vertices; ++j)
                {
-                       const float *v = vertices[j];
-                       points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
+                       const float *v = reinterpret_cast<const float *>(vertices[j]+offset);
+                       points.push_back(Vector3(v[0], v[1], (three ? v[2] : 0.0f)));
                }
        }
 
index b43061c522a5addbbf2ff474742efd97fb78fbef..b3d85deb899a5f837d9e0d64d8720c89925b14bc 100644 (file)
@@ -102,7 +102,7 @@ void Text::set_alignment(float h, float v)
        unsigned n_vertices = mesh.get_n_vertices();
        for(unsigned i=0; i<n_vertices; ++i)
        {
-               float *pos = mesh.modify_vertex(i)+pos_offset;
+               float *pos = reinterpret_cast<float *>(mesh.modify_vertex(i)+pos_offset);
                pos[0] += horz_adjust;
                pos[1] += vert_adjust;
        }