]> git.tdb.fi Git - libs/gl.git/blobdiff - source/vertexformat.cpp
Standard vertex components for tangent and binormal vectors
[libs/gl.git] / source / vertexformat.cpp
index bf2d9095d77220333ac205a608375450c8ada8c9..8cc0cec990157719b87ffe70e640b4c579829f77 100644 (file)
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <algorithm>
+#include <msp/strings/format.h>
+#include <msp/strings/lexicalcast.h>
+#include <msp/strings/utils.h>
+#include "error.h"
 #include "vertexformat.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GL {
 
-uint get_stride(VertexFormat f)
+VertexFormat::VertexFormat():
+       data(0)
+{ }
+
+VertexFormat::VertexFormat(VertexComponent c):
+       data(new unsigned char[8])
 {
-       uint stride=0;
-       for(uint fmt=f; fmt; fmt>>=4)
-               stride+=(fmt&3)+1;
-       return stride;
+       data[0] = 1;
+       data[1] = c;
 }
 
-std::istream &operator>>(std::istream &in, VertexFormat &f)
+VertexFormat::VertexFormat(const VertexFormat &f):
+       data(0)
 {
-       std::string str;
-       in>>str;
+       *this = f;
+}
 
-       unsigned start=0;
-       f=NODATA;
+VertexFormat &VertexFormat::operator=(const VertexFormat &f)
+{
+       delete[] data;
+       if(f.data)
+       {
+               data = new unsigned char[(f.data[0]&~7)+8];
+               copy(f.data, f.data+f.data[0]+1, data);
+       }
+       else
+               data = 0;
+
+       return *this;
+}
+
+VertexFormat::~VertexFormat()
+{
+       delete[] data;
+}
 
-       while(1)
+VertexFormat VertexFormat::operator,(VertexComponent c) const
+{
+       VertexFormat r = *this;
+       if(r.data)
        {
-               unsigned underscore=str.find('_', start);
-               if(!str.compare(start, underscore-start, "VERTEX2"))
-                       f=(f,VERTEX2);
-               else if(!str.compare(start, underscore-start, "VERTEX3"))
-                       f=(f,VERTEX3);
-               else if(!str.compare(start, underscore-start, "VERTEX4"))
-                       f=(f,VERTEX4);
-               else if(!str.compare(start, underscore-start, "NORMAL3"))
-                       f=(f,NORMAL3);
-               else if(!str.compare(start, underscore-start, "TEXCOORD1"))
-                       f=(f,TEXCOORD1);
-               else if(!str.compare(start, underscore-start, "TEXCOORD2"))
-                       f=(f,TEXCOORD2);
-               else if(!str.compare(start, underscore-start, "TEXCOORD3"))
-                       f=(f,TEXCOORD3);
-               else if(!str.compare(start, underscore-start, "TEXCOORD4"))
-                       f=(f,TEXCOORD4);
-               else if(!str.compare(start, underscore-start, "COLOR4UB"))
-                       f=(f,COLOR4_UBYTE);
-               else if(!str.compare(start, underscore-start, "COLOR3F"))
-                       f=(f,COLOR3_FLOAT);
-               else if(!str.compare(start, underscore-start, "COLOR4F"))
-                       f=(f,COLOR4_FLOAT);
-               else
+               const unsigned char n = ++r.data[0];
+               if((n&7)==0)
                {
-                       in.setstate(std::ios_base::failbit);
-                       break;
+                       unsigned char *newdt = new unsigned char[n+8];
+                       copy(r.data, r.data+n, newdt);
+                       delete r.data;
+                       r.data = newdt;
                }
+               r.data[n] = c;
+       }
+       else
+       {
+               r.data = new unsigned char[8];
+               r.data[0] = 1;
+               r.data[1] = c;
+       }
+
+       return r;
+}
 
-               if(underscore==std::string::npos)
-                       break;
-               start=underscore+1;
+VertexFormat VertexFormat::operator,(unsigned i) const
+{
+       if(!data)
+               throw invalid_operation("VertexFormat::operator,");
+       VertexFormat r = *this;
+       unsigned char *c = r.data+r.data[0];
+       *c = make_indexed_component(static_cast<VertexComponent>(*c), i);
+
+       return r;
+}
+
+unsigned VertexFormat::stride() const
+{
+       unsigned s = 0;
+       for(const unsigned char *i=begin(); i!=end(); ++i)
+               s += get_component_size(*i);
+       return s;
+}
+
+int VertexFormat::offset(VertexComponent comp) const
+{
+       unsigned type = get_component_type(comp);
+       unsigned size = get_component_size(comp);
+       unsigned offs = 0;
+       for(const unsigned char *i=begin(); i!=end(); ++i)
+       {
+               if(get_component_type(*i)==type)
+               {
+                       if(get_component_size(*i)>=size)
+                               return offs;
+                       else
+                               return -1;
+               }
+               else
+                       offs += get_component_size(*i);
        }
 
-       return in;
+       return -1;
+}
+
+VertexComponent make_indexed_component(VertexComponent comp, unsigned index)
+{
+       if(comp>=TEXCOORD1 && comp<=TEXCOORD4)
+       {
+               if(index>=4)
+                       throw out_of_range("make_indexed_component");
+       }
+       else if(comp>=ATTRIB1 && comp<=ATTRIB4)
+       {
+               if(index>=24)
+                       throw out_of_range("make_indexed_component");
+       }
+       else
+               throw invalid_argument("make_indexed_component");
+       return static_cast<VertexComponent>(comp+index*4);
+}
+
+void operator>>(const LexicalConverter &conv, VertexComponent &c)
+{
+       const string &str = conv.get();
+       if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
+               c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
+       else if(str=="NORMAL3")
+               c = NORMAL3;
+       else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
+               c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
+       else if(str=="COLOR4_UBYTE")
+               c = COLOR4_UBYTE;
+       else if(str=="TANGENT3")
+               c = TANGENT3;
+       else if(str=="BINORMAL3")
+               c = BINORMAL3;
+       else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
+       {
+               if(str.size()==9)
+                       c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
+               else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
+                       c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
+               else
+                       throw lexical_error(format("conversion of '%s' to VertexComponent", str));
+       }
+       else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
+       {
+               unsigned n;
+               try
+               {
+                       n = lexical_cast<unsigned>(str.substr(8));
+               }
+               catch(const lexical_error &)
+               {
+                       throw lexical_error(format("conversion of '%s' to VertexComponent", str));
+               }
+               c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
+       }
+       else
+               throw lexical_error(format("conversion of '%s' to VertexComponent", str));
 }
 
 } // namespace GL