Browse Source

Class for managing indexed meshes.

vectorfields
Vladimír Vondruš 16 years ago
parent
commit
524067a9ca
  1. 1
      src/CMakeLists.txt
  2. 64
      src/IndexedMesh.cpp
  3. 68
      src/IndexedMesh.h

1
src/CMakeLists.txt

@ -9,6 +9,7 @@ set(Magnum_SRCS
AbstractObject.cpp
AbstractShaderProgram.cpp
Camera.cpp
IndexedMesh.cpp
Mesh.cpp
Scene.cpp
Shader.cpp

64
src/IndexedMesh.cpp

@ -0,0 +1,64 @@
/*
Copyright © 2010 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "IndexedMesh.h"
using namespace std;
namespace Magnum {
void IndexedMesh::draw() {
/* Finalize the mesh, if it is not already */
finalize();
/* Enable vertex arrays for all attributes */
for(set<GLuint>::const_iterator it = attributes().begin(); it != attributes().end(); ++it)
glEnableVertexAttribArray(*it);
/* Bind attributes to vertex buffers */
for(map<Buffer*, pair<bool, vector<Attribute> > >::const_iterator it = buffers().begin(); it != buffers().end(); ++it) {
/* Bind buffer */
it->first->bind();
/* Bind all attributes to this buffer */
for(vector<Attribute>::const_iterator ait = it->second.second.begin(); ait != it->second.second.end(); ++ait)
switch(ait->type) {
case GL_BYTE:
case GL_UNSIGNED_BYTE:
case GL_SHORT:
case GL_UNSIGNED_SHORT:
case GL_INT:
case GL_UNSIGNED_INT:
glVertexAttribIPointer(ait->location, ait->size, ait->type, ait->stride, ait->pointer);
break;
default:
glVertexAttribPointer(ait->location, ait->size, ait->type, GL_FALSE, ait->stride, ait->pointer);
}
/* Unbind buffer */
it->first->unbind();
}
/* Bind index array, draw the elements and unbind */
_indexBuffer.bind();
glDrawElements(primitive(), indexCount, indexType, 0);
_indexBuffer.unbind();
/* Disable vertex arrays for all attributes */
for(set<GLuint>::const_iterator it = attributes().begin(); it != attributes().end(); ++it)
glDisableVertexAttribArray(*it);
}
}

68
src/IndexedMesh.h

@ -0,0 +1,68 @@
#ifndef Magnum_IndexedMesh_h
#define Magnum_IndexedMesh_h
/*
Copyright © 2010 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
/** @file
* @brief Class Magnum::IndexedMesh
*/
#include "Mesh.h"
#include "Buffer.h"
namespace Magnum {
/**
* @brief Indexed mesh
*/
class IndexedMesh: public Mesh {
DISABLE_COPY(IndexedMesh)
public:
/**
* @brief Constructor
* @param _primitive Primitive type
* @param _vertexCount Count of unique vertices
* @param _indexCount Count of indices
* @param _indexType Type of indices (GL_UNSIGNED_BYTE,
* GL_UNSIGNED_SHORT or GL_UNSIGNED_INT)
*/
inline IndexedMesh(Primitive _primitive, GLsizei _vertexCount, GLsizei _indexCount, GLenum _indexType = GL_UNSIGNED_SHORT): Mesh(_primitive, _vertexCount), _indexBuffer(Buffer::ElementArrayBuffer), indexCount(_indexCount), indexType(_indexType) {}
/**
* @brief Index buffer
*
* Returns pointer to index buffer, which should be then filled with
* indices (of type specified in constructor).
*/
inline Buffer* indexBuffer() { return &_indexBuffer; }
/**
* @brief Draw the mesh
*
* Binds attributes to buffers, bind index buffer and draws the mesh.
* Expects an active shader with all uniforms set.
*/
virtual void draw();
private:
Buffer _indexBuffer;
GLsizei indexCount;
GLenum indexType;
};
}
#endif
Loading…
Cancel
Save