]> git.tdb.fi Git - libs/gl.git/blobdiff - source/vertexformat.h
Add a utility class for switching renderables
[libs/gl.git] / source / vertexformat.h
index 2432d7b60d54aec2fbf8e478cdfec5ff8971f90f..7edca1f3dcf74782d8fd5a94c7bfb25cd079c72d 100644 (file)
@@ -1,63 +1,75 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_GL_VERTEXFORMAT_H_
 #define MSP_GL_VERTEXFORMAT_H_
 
-#include <istream>
-#include "types.h"
+#include <msp/strings/lexicalcast.h>
 
 namespace Msp {
 namespace GL {
 
+/** A single vertex component.  Nvidia drivers have aliasing between the
+fixed-functions and generic vertex attributes, despite the standard not
+allowing it.  We use the same attribute indices here to avoid problems. */
 enum VertexComponent
 {
-       VERTEX2=1,
+       VERTEX2 = 1,
        VERTEX3,
        VERTEX4,
-       NORMAL3=6,
-       TEXCOORD1=8,
+       NORMAL3 = 10,
+       COLOR4_UBYTE = 12,
+       COLOR3_FLOAT = 14,
+       COLOR4_FLOAT,
+       TANGENT3 = 18,
+       BINORMAL3 = 22,
+       TEXCOORD1 = 32,
        TEXCOORD2,
        TEXCOORD3,
        TEXCOORD4,
-       COLOR4_UBYTE=12,
-       COLOR3_FLOAT=14,
-       COLOR4_FLOAT,
-       ATTRIB1=16,
+       ATTRIB1 = 64,
        ATTRIB2,
        ATTRIB3,
        ATTRIB4
 };
 
-struct VertexFormat
+class VertexFormat
 {
-       unsigned char *data;
+private:
+       enum { MAX_COMPONENTS = 15 };
+
+       unsigned char count;
+       unsigned char components[MAX_COMPONENTS];
 
+public:
        VertexFormat();
        VertexFormat(VertexComponent);
-       VertexFormat(const VertexFormat &);
-       VertexFormat &operator=(const VertexFormat &);
-       ~VertexFormat();
 
-       bool empty() const { return !data || !data[0]; }
-       const unsigned char *begin() const { return data ? data+1 : 0; }
-       const unsigned char *end() const { return data ? data+1+data[0] : 0; }
+       VertexFormat operator,(VertexComponent c) const;
+       VertexFormat operator,(unsigned i) const;
+
+       bool empty() const { return !count; }
+       const unsigned char *begin() const { return components; }
+       const unsigned char *end() const { return components+count; }
+       unsigned stride() const;
+       int offset(VertexComponent) const;
 };
 
-VertexFormat operator,(const VertexFormat &f, VertexComponent c);
 inline VertexFormat operator,(VertexComponent c1, VertexComponent c2)
 { return (VertexFormat(c1), c2); }
 
-VertexFormat operator,(const VertexFormat &f, unsigned i);
 inline VertexFormat operator,(VertexComponent c, unsigned i)
 { return (VertexFormat(c), i); }
 
-uint get_stride(const VertexFormat &);
-std::istream &operator>>(std::istream &, VertexFormat &);
+VertexComponent make_indexed_component(VertexComponent, unsigned);
+
+inline unsigned get_component_type(unsigned char c)
+{ return c>>2; }
+
+inline unsigned get_component_size(unsigned char c)
+{ return (c&3)+1; }
+
+inline unsigned get_stride(const VertexFormat &f)
+{ return f.stride(); }
+
+void operator>>(const LexicalConverter &, VertexComponent &);
 
 } // namespace GL
 } // namespace Msp