Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfVertexFormat.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifndef __glfVertexFormat_hh__
26 #define __glfVertexFormat_hh__
27 
28 #include "glfCommon.hh"
29 #include <vector>
30 #include <string>
31 
32 namespace BIAS {
33 
34  /**
35  * \brief A vertex format describes the attributes of a vertex. A list of
36  * vertices with the same format are stored in a vertex buffer.
37  * \ingroup g_openglframework
38  * \author jkollmann
39  */
40  class BIASOpenGLFramework_EXPORT glfVertexFormat {
41  public:
43  ~glfVertexFormat();
44 
45  /**
46  * Possible attributes of a vertex. Note that ATTRIB_TEXCOORD
47  * and ATTRIB_GENERIC require an additional index, i.e. there
48  * can be multiple of them.
49  */
50  enum Attribute
51  {
56  ATTRIB_GENERIC
57  };
58 
59  /**
60  * Adds a vertex attribute to the format.
61  * \param attrib The attribute to be added.
62  * \param index Additional index for ATTRIB_TEXCOORD and ATTRIB_GENERIC.
63  * \param type Data type for this attribute. Can be GL_BYTE,
64  * GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT,
65  * GL_UNSIGNED_INT, GL_FLOAT, or GL_DOUBLE.
66  * \param size Number of components for this attribute, i.e. 3 for a
67  * 3-dimensional vector.
68  * \param normalized True if values should be normalized. Only for ATTRIB_GENERIC.
69  *
70  * \note Not all combinations of attribute, type and size are supported by
71  * OpenGL or may be unsupported by the current OpenGL implementation. In this
72  * case an exception will be thrown.
73  */
74  void AddAttribute(Attribute attrib, int index, GLenum type, int size, bool normalized = false);
75 
76  /**
77  * Convenience wrapper for other glfVertexFormat::AddAttribute method if no additional attribute
78  * index is required. This can be used for all attributes except ATTRIB_TEXCOORD and ATTRIB_GENERIC.
79  */
80  void AddAttribute(Attribute attrib, GLenum type, int size) {
81  AddAttribute(attrib, 0, type, size);
82  }
83 
84  /**
85  * Returns the size in bytes of a single vertex.
86  */
87  int GetVertexSize() const;
88 
89  /**
90  * Returns <code>true</code> iff the given attribute is part of the format.
91  */
92  bool HasAttribute(Attribute attrib, int index = 0) const;
93 
94  /**
95  * Retrieves information about a vertex attribute inside the vertex
96  * structure. Returns <code>false</code> if the vertex attribute is not
97  * part of this format.
98  */
99  bool GetAttributeInfo(Attribute attrib, int index, int& offset, GLenum& type, int& size) const;
100 
101  /**
102  * Binds a vertex buffer or vertex array in OpenGL using this vertex format.
103  * \attention For internal usage inside OpenGLFramework Base library only.
104  */
105  void Bind(const void* data) const;
106 
107  glfVertexFormat& operator=(const glfVertexFormat& v2);
108 
109  private:
110 
111  // Checks if an attribute is supported with the given parameters.
112  bool IsAttributeSupported(Attribute attrib, int index, GLenum type, int size, std::string& reason);
113 
114  // description of a vertex attribute inside the vertex format.
115  struct AttributeDesc {
116  Attribute attrib; // which attribute
117  int index; // additional index for ATTRIB_TEXCOORD and ATTRIB_GENERIC
118  int offset; // byte offset in vertex structure
119  GLenum type; // data type
120  int size; // number of components
121  GLboolean normalized; // for ATTRIB_GENERIC
122  };
123 
124  std::vector<AttributeDesc> attribDesc_; // description of all used attributes.
125  int vertexSize_; // total size of a vertex in bytes.
126  };
127 
128 } // namespace BIAS
129 
130 #endif // __glfVertexFormat_hh__
void AddAttribute(Attribute attrib, GLenum type, int size)
Convenience wrapper for other glfVertexFormat::AddAttribute method if no additional attribute index i...
A vertex format describes the attributes of a vertex.
Attribute
Possible attributes of a vertex.