25 #include "glfVertexFormat.hh"
26 #include "glfException.hh"
42 vertexSize_ = v2.vertexSize_;
43 for(
unsigned int i=0; i<v2.attribDesc_.size(); i++) {
44 attribDesc_.push_back(v2.attribDesc_[i]);
50 int index, GLenum type,
51 int size,
bool normalized)
54 BIASASSERT(index >= 0);
59 BIASASSERT(
sizeof(GLubyte) == 1);
63 if (!IsAttributeSupported(attrib, index, type, size, reason)) {
64 GLF_THROW_EXCEPTION(
"Vertex attribute is not supported: " << reason);
71 desc.offset = vertexSize_;
74 desc.normalized = normalized ? GL_TRUE : GL_FALSE;
75 attribDesc_.push_back(desc);
80 case GL_BYTE: typeSize =
sizeof(GLbyte);
break;
81 case GL_UNSIGNED_BYTE: typeSize =
sizeof(GLubyte);
break;
82 case GL_SHORT: typeSize =
sizeof(GLshort);
break;
83 case GL_UNSIGNED_SHORT: typeSize =
sizeof(GLushort);
break;
84 case GL_INT: typeSize =
sizeof(GLint);
break;
85 case GL_UNSIGNED_INT: typeSize =
sizeof(GLuint);
break;
86 case GL_FLOAT: typeSize =
sizeof(GLfloat);
break;
87 case GL_DOUBLE: typeSize =
sizeof(GLdouble);
break;
89 vertexSize_ += typeSize * size;
90 BIASASSERT(vertexSize_ > 0);
102 for (
unsigned int i = 0; i < attribDesc_.size(); i++) {
103 if (attribDesc_[i].attrib == attrib && attribDesc_[i].index == index) {
114 for (
unsigned int i = 0; i < attribDesc_.size(); i++) {
115 if (attribDesc_[i].attrib == attrib && attribDesc_[i].index == index) {
116 offset = attribDesc_[i].offset;
117 type = attribDesc_[i].type;
118 size = attribDesc_[i].size;
125 bool glfVertexFormat::IsAttributeSupported(Attribute attrib,
int index, GLenum type,
126 int size, std::string& reason)
131 if (type != GL_SHORT &&
135 reason =
"Type of ATTRIBUTE_POSITION must be GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE.";
138 if (size < 2 || size > 4) {
139 reason =
"Size of ATTRIBUTE_POSITION must be 2, 3, or 4.";
145 if (type != GL_BYTE &&
150 reason =
"Type of ATTRIBUTE_NORMAL must be GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE.";
154 reason =
"Size of ATTRIBUTE_NORMAL must be 3.";
160 if (type != GL_BYTE &&
161 type != GL_UNSIGNED_BYTE &&
163 type != GL_UNSIGNED_SHORT &&
165 type != GL_UNSIGNED_INT &&
168 reason =
"Type of ATTRIBUTE_COLOR must be GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, "
169 "GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, or GL_DOUBLE.";
172 if (size < 3 || size > 4) {
173 reason =
"Size of ATTRIBUTE_COLOR must be 3 or 4.";
179 if (type != GL_SHORT &&
183 reason =
"Type of ATTRIBUTE_TEXCOORD must be GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE";
186 if (size < 1 || size > 4) {
187 reason =
"Size of ATTRIBUTE_TEXCOORD must be 1, 2, 3, or 4.";
190 GLint maxTextureCoords;
191 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &maxTextureCoords);
192 if (index >= maxTextureCoords) {
194 s <<
"Index of ATTRIBUTE_TEXCOORD must be < " << maxTextureCoords;
201 if (type != GL_BYTE &&
202 type != GL_UNSIGNED_BYTE &&
204 type != GL_UNSIGNED_SHORT &&
206 type != GL_UNSIGNED_INT &&
209 reason =
"Type of ATTRIBUTE_GENERIC must be GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, "
210 "GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, or GL_DOUBLE.";
213 if (size < 1 || size > 4) {
214 reason =
"Size of ATTRIBUTE_GENERIC must be 1, 2, 3, or 4.";
217 GLint maxVertexAttribs;
218 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);
219 if (index >= maxVertexAttribs) {
221 s <<
"Index of ATTRIBUTE_GENERIC must be < " << maxVertexAttribs;
228 reason =
"Invalid vertex attribute.";
239 GLint maxTextureCoords;
240 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &maxTextureCoords);
241 GLint maxVertexAttribs;
242 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);
245 glDisableClientState(GL_VERTEX_ARRAY);
246 glDisableClientState(GL_NORMAL_ARRAY);
247 glDisableClientState(GL_COLOR_ARRAY);
248 for (
int i = 0; i < maxTextureCoords; i++) {
249 glClientActiveTexture(GL_TEXTURE0 + i);
250 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
252 for (
int i = 0; i < maxVertexAttribs; i++) {
253 glDisableVertexAttribArray(i);
256 for (
unsigned int i = 0; i < attribDesc_.size(); i++) {
257 const AttributeDesc& desc = attribDesc_[i];
258 switch (desc.attrib) {
260 glEnableClientState(GL_VERTEX_ARRAY);
261 glVertexPointer(desc.size, desc.type, vertexSize_, (GLubyte*)data + desc.offset);
264 glEnableClientState(GL_NORMAL_ARRAY);
265 glNormalPointer(desc.type, vertexSize_, (GLubyte*)data + desc.offset);
268 glEnableClientState(GL_COLOR_ARRAY);
269 glColorPointer(desc.size, desc.type, vertexSize_, (GLubyte*)data + desc.offset);
272 glClientActiveTexture(GL_TEXTURE0 + desc.index);
273 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
274 glTexCoordPointer(desc.size, desc.type, vertexSize_, (GLubyte*)data + desc.offset);
277 glEnableVertexAttribArray(desc.index);
278 glVertexAttribPointer(desc.index, desc.size, desc.type, desc.normalized,
279 vertexSize_, (GLubyte*)data + desc.offset);
284 GLF_THROW_ON_OPENGL_ERROR;