diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 73a8d7ea4..829209029 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ set(Magnum_SRCS AbstractObject.cpp AbstractShaderProgram.cpp Camera.cpp + IndexedMesh.cpp Mesh.cpp Scene.cpp Shader.cpp diff --git a/src/IndexedMesh.cpp b/src/IndexedMesh.cpp new file mode 100644 index 000000000..d02d77711 --- /dev/null +++ b/src/IndexedMesh.cpp @@ -0,0 +1,64 @@ +/* + Copyright © 2010 Vladimír Vondruš + + 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::const_iterator it = attributes().begin(); it != attributes().end(); ++it) + glEnableVertexAttribArray(*it); + + /* Bind attributes to vertex buffers */ + for(map > >::const_iterator it = buffers().begin(); it != buffers().end(); ++it) { + /* Bind buffer */ + it->first->bind(); + + /* Bind all attributes to this buffer */ + for(vector::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::const_iterator it = attributes().begin(); it != attributes().end(); ++it) + glDisableVertexAttribArray(*it); +} + +} diff --git a/src/IndexedMesh.h b/src/IndexedMesh.h new file mode 100644 index 000000000..981d1d92f --- /dev/null +++ b/src/IndexedMesh.h @@ -0,0 +1,68 @@ +#ifndef Magnum_IndexedMesh_h +#define Magnum_IndexedMesh_h +/* + Copyright © 2010 Vladimír Vondruš + + 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