Browse Source

Don't differentiate between vertex and index count in Mesh.

Only one value from these two was used in the end, wasting precious
bytes. Also these two values were used to differentiate between indexed
and non-indexed mesh (instead of relying on actual index buffer being
bound), which was very confusing. This approach looks more clean. The
MeshView class is not yet updated, as the change would expose some
features that aren't possible in current implementation (base vertex
specification).

Merged Mesh::setVertexCount() and Mesh::setIndexCount() into one
Mesh::setCount(), the two original functions are now guarded aliases to
the new one, are marked as deprecated and will be removed in future
release, similarly for the getters.

In particular, if the mesh is indexed, setVertexCount() does nothing and
vertexCount() returns 0. The setIndexCount() and indexCount() do and
return the same regardless of whether the mesh is indexed or not.
pull/51/head
Vladimír Vondruš 12 years ago
parent
commit
bfa2a42638
  1. 2
      src/Magnum/DebugTools/ForceRenderer.cpp
  2. 10
      src/Magnum/DebugTools/Implementation/AbstractShapeRenderer.cpp
  3. 2
      src/Magnum/DebugTools/ObjectRenderer.cpp
  4. 28
      src/Magnum/Mesh.cpp
  5. 138
      src/Magnum/Mesh.h
  6. 16
      src/Magnum/MeshTools/Compile.cpp
  7. 2
      src/Magnum/MeshTools/CompressIndices.cpp
  8. 6
      src/Magnum/MeshTools/CompressIndices.h
  9. 2
      src/Magnum/MeshTools/FullScreenTriangle.cpp
  10. 13
      src/Magnum/MeshTools/Interleave.h
  11. 10
      src/Magnum/MeshView.cpp
  12. 6
      src/Magnum/MeshView.h
  13. 8
      src/Magnum/Test/MeshGLTest.cpp
  14. 2
      src/Magnum/Test/PrimitiveQueryGLTest.cpp
  15. 4
      src/Magnum/Test/SampleQueryGLTest.cpp
  16. 8
      src/Magnum/Text/Renderer.cpp
  17. 2
      src/Magnum/TextureTools/DistanceField.cpp

2
src/Magnum/DebugTools/ForceRenderer.cpp

@ -88,7 +88,7 @@ template<UnsignedInt dimensions> ForceRenderer<dimensions>::ForceRenderer(SceneG
Mesh* mesh = new Mesh; Mesh* mesh = new Mesh;
mesh->setPrimitive(MeshPrimitive::Lines) mesh->setPrimitive(MeshPrimitive::Lines)
.setIndexCount(indices.size()) .setCount(indices.size())
.addVertexBuffer(*vertexBuffer, 0, .addVertexBuffer(*vertexBuffer, 0,
typename Shaders::Flat<dimensions>::Position(Shaders::Flat<dimensions>::Position::Components::Two)) typename Shaders::Flat<dimensions>::Position(Shaders::Flat<dimensions>::Position::Components::Two))
.setIndexBuffer(*indexBuffer, 0, Mesh::IndexType::UnsignedByte, 0, positions.size()); .setIndexBuffer(*indexBuffer, 0, Mesh::IndexType::UnsignedByte, 0, positions.size());

10
src/Magnum/DebugTools/Implementation/AbstractShapeRenderer.cpp

@ -53,7 +53,6 @@ template<> void create<2>(Trade::MeshData2D& data, Resource<Mesh>& meshResource,
/* Mesh configuration */ /* Mesh configuration */
Mesh* mesh = new Mesh; Mesh* mesh = new Mesh;
mesh->setPrimitive(data.primitive()) mesh->setPrimitive(data.primitive())
.setVertexCount(data.positions(0).size())
.addVertexBuffer(*buffer, 0, Shaders::Flat2D::Position()); .addVertexBuffer(*buffer, 0, Shaders::Flat2D::Position());
ResourceManager::instance().set(meshResource.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual); ResourceManager::instance().set(meshResource.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
@ -63,7 +62,9 @@ template<> void create<2>(Trade::MeshData2D& data, Resource<Mesh>& meshResource,
Buffer* indexBuffer = new Buffer(Buffer::Target::ElementArray); Buffer* indexBuffer = new Buffer(Buffer::Target::ElementArray);
MeshTools::compressIndices(*mesh, *indexBuffer, BufferUsage::StaticDraw, data.indices()); MeshTools::compressIndices(*mesh, *indexBuffer, BufferUsage::StaticDraw, data.indices());
ResourceManager::instance().set(indexBufferResource.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual); ResourceManager::instance().set(indexBufferResource.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual);
}
/* The mesh is not indexed, set proper vertex count */
} else mesh->setCount(data.positions(0).size());
} }
template<> void create<3>(Trade::MeshData3D& data, Resource<Mesh>& meshResource, Resource<Buffer>& vertexBufferResource, Resource<Buffer>& indexBufferResource) { template<> void create<3>(Trade::MeshData3D& data, Resource<Mesh>& meshResource, Resource<Buffer>& vertexBufferResource, Resource<Buffer>& indexBufferResource) {
@ -75,7 +76,6 @@ template<> void create<3>(Trade::MeshData3D& data, Resource<Mesh>& meshResource,
/* Mesh configuration */ /* Mesh configuration */
Mesh* mesh = new Mesh; Mesh* mesh = new Mesh;
mesh->setPrimitive(data.primitive()) mesh->setPrimitive(data.primitive())
.setVertexCount(data.positions(0).size())
.addVertexBuffer(*vertexBuffer, 0, Shaders::Flat3D::Position()); .addVertexBuffer(*vertexBuffer, 0, Shaders::Flat3D::Position());
ResourceManager::instance().set(meshResource.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual); ResourceManager::instance().set(meshResource.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
@ -85,7 +85,9 @@ template<> void create<3>(Trade::MeshData3D& data, Resource<Mesh>& meshResource,
Buffer* indexBuffer = new Buffer(Buffer::Target::ElementArray); Buffer* indexBuffer = new Buffer(Buffer::Target::ElementArray);
MeshTools::compressIndices(*mesh, *indexBuffer, BufferUsage::StaticDraw, data.indices()); MeshTools::compressIndices(*mesh, *indexBuffer, BufferUsage::StaticDraw, data.indices());
ResourceManager::instance().set(indexBufferResource.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual); ResourceManager::instance().set(indexBufferResource.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual);
}
/* The mesh is not indexed, set proper vertex count */
} else mesh->setCount(data.positions(0).size());
} }
} }

2
src/Magnum/DebugTools/ObjectRenderer.cpp

@ -166,7 +166,7 @@ template<UnsignedInt dimensions> ObjectRenderer<dimensions>::ObjectRenderer(Scen
ResourceManager::instance().set(this->indexBuffer.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual); ResourceManager::instance().set(this->indexBuffer.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual);
mesh->setPrimitive(MeshPrimitive::Lines) mesh->setPrimitive(MeshPrimitive::Lines)
.setIndexCount(Renderer<dimensions>::indices.size()) .setCount(Renderer<dimensions>::indices.size())
.addVertexBuffer(*vertexBuffer, 0, .addVertexBuffer(*vertexBuffer, 0,
typename Shaders::VertexColor<dimensions>::Position(), typename Shaders::VertexColor<dimensions>::Position(),
typename Shaders::VertexColor<dimensions>::Color()) typename Shaders::VertexColor<dimensions>::Color())

28
src/Magnum/Mesh.cpp

@ -72,7 +72,7 @@ std::size_t Mesh::indexSize(IndexType type) {
CORRADE_ASSERT_UNREACHABLE(); CORRADE_ASSERT_UNREACHABLE();
} }
Mesh::Mesh(MeshPrimitive primitive): _primitive(primitive), _vertexCount(0), _indexCount(0) Mesh::Mesh(MeshPrimitive primitive): _primitive(primitive), _count(0)
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
, _indexStart(0), _indexEnd(0) , _indexStart(0), _indexEnd(0)
#endif #endif
@ -92,7 +92,7 @@ Mesh::~Mesh() {
(this->*Context::current()->state().mesh->destroyImplementation)(); (this->*Context::current()->state().mesh->destroyImplementation)();
} }
Mesh::Mesh(Mesh&& other) noexcept: _id(other._id), _primitive(other._primitive), _vertexCount(other._vertexCount), _indexCount(other._indexCount) Mesh::Mesh(Mesh&& other) noexcept: _id(other._id), _primitive(other._primitive), _count(other._count)
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
, _indexStart(other._indexStart), _indexEnd(other._indexEnd) , _indexStart(other._indexStart), _indexEnd(other._indexEnd)
#endif #endif
@ -110,8 +110,7 @@ Mesh::Mesh(Mesh&& other) noexcept: _id(other._id), _primitive(other._primitive),
Mesh& Mesh::operator=(Mesh&& other) noexcept { Mesh& Mesh::operator=(Mesh&& other) noexcept {
std::swap(_id, other._id); std::swap(_id, other._id);
std::swap(_primitive, other._primitive); std::swap(_primitive, other._primitive);
std::swap(_vertexCount, other._vertexCount); std::swap(_count, other._count);
std::swap(_indexCount, other._indexCount);
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
std::swap(_indexStart, other._indexStart); std::swap(_indexStart, other._indexStart);
std::swap(_indexEnd, other._indexEnd); std::swap(_indexEnd, other._indexEnd);
@ -153,6 +152,7 @@ Mesh& Mesh::setIndexBuffer(Buffer& buffer, GLintptr offset, IndexType type, Unsi
"Mesh::setIndexBuffer(): the buffer has unexpected target hint, expected" << Buffer::Target::ElementArray << "but got" << buffer.targetHint(), *this); "Mesh::setIndexBuffer(): the buffer has unexpected target hint, expected" << Buffer::Target::ElementArray << "but got" << buffer.targetHint(), *this);
#endif #endif
_indexBuffer = &buffer;
_indexOffset = offset; _indexOffset = offset;
_indexType = type; _indexType = type;
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
@ -167,29 +167,29 @@ Mesh& Mesh::setIndexBuffer(Buffer& buffer, GLintptr offset, IndexType type, Unsi
} }
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
void Mesh::drawInternal(Int firstVertex, Int vertexCount, GLintptr indexOffset, Int indexCount, Int indexStart, Int indexEnd) void Mesh::drawInternal(Int count, Int firstVertex, GLintptr indexOffset, Int indexStart, Int indexEnd)
#else #else
void Mesh::drawInternal(Int firstVertex, Int vertexCount, GLintptr indexOffset, Int indexCount) void Mesh::drawInternal(Int count, Int firstVertex, GLintptr indexOffset)
#endif #endif
{ {
/* Nothing to draw */ /* Nothing to draw */
if(!vertexCount && !indexCount) return; if(!count) return;
(this->*Context::current()->state().mesh->bindImplementation)(); (this->*Context::current()->state().mesh->bindImplementation)();
/* Non-indexed mesh */ /* Non-indexed mesh */
if(!indexCount) if(!_indexBuffer)
glDrawArrays(GLenum(_primitive), firstVertex, vertexCount); glDrawArrays(GLenum(_primitive), firstVertex, count);
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
/* Indexed mesh with specified range */ /* Indexed mesh with specified range */
else if(indexEnd) else if(indexEnd)
glDrawRangeElements(GLenum(_primitive), indexStart, indexEnd, indexCount, GLenum(_indexType), reinterpret_cast<GLvoid*>(indexOffset)); glDrawRangeElements(GLenum(_primitive), indexStart, indexEnd, count, GLenum(_indexType), reinterpret_cast<GLvoid*>(indexOffset));
#endif #endif
/* Indexed mesh without specified range */ /* Indexed mesh without specified range */
else else
glDrawElements(GLenum(_primitive), indexCount, GLenum(_indexType), reinterpret_cast<GLvoid*>(indexOffset)); glDrawElements(GLenum(_primitive), count, GLenum(_indexType), reinterpret_cast<GLvoid*>(indexOffset));
(this->*Context::current()->state().mesh->unbindImplementation)(); (this->*Context::current()->state().mesh->unbindImplementation)();
} }
@ -329,9 +329,7 @@ void Mesh::attributePointerImplementationDSA(const LongAttribute& attribute) {
#endif #endif
#endif #endif
void Mesh::bindIndexBufferImplementationDefault(Buffer& buffer) { void Mesh::bindIndexBufferImplementationDefault(Buffer&) {}
_indexBuffer = &buffer;
}
void Mesh::bindIndexBufferImplementationVAO(Buffer& buffer) { void Mesh::bindIndexBufferImplementationVAO(Buffer& buffer) {
bindVAO(_id); bindVAO(_id);
@ -359,7 +357,7 @@ void Mesh::bindImplementationDefault() {
#endif #endif
/* Bind index buffer, if the mesh is indexed */ /* Bind index buffer, if the mesh is indexed */
if(_indexCount) _indexBuffer->bind(Buffer::Target::ElementArray); if(_indexBuffer) _indexBuffer->bind(Buffer::Target::ElementArray);
} }
void Mesh::bindImplementationVAO() { void Mesh::bindImplementationVAO() {

138
src/Magnum/Mesh.h

@ -122,29 +122,30 @@ namespace Implementation { struct MeshState; }
@section Mesh-configuration Mesh configuration @section Mesh-configuration Mesh configuration
You have to specify at least primitive and vertex count using @ref setPrimitive() You have to specify at least primitive and vertex/index count using
and @ref setVertexCount(). Then fill your vertex buffers with data, add them to @ref setPrimitive() and @ref setCount(). Then fill your vertex buffers with
the mesh and specify @ref AbstractShaderProgram::Attribute "shader attribute" data, add them to the mesh and specify
layout inside the buffers using @ref addVertexBuffer(). You can also @ref AbstractShaderProgram::Attribute "shader attribute" layout inside the
use @ref MeshTools::interleave() conveniently fill interleaved vertex buffer. buffers using @ref addVertexBuffer(). You can also use
The function itself calls @ref setVertexCount(), so you don't have to do it @ref MeshTools::interleave() conveniently fill interleaved vertex buffer.
again, but you still have to specify the layout using @ref addVertexBuffer(). The function itself calls @ref setCount(), so you don't have to, but you still
have to specify the primitive using @ref setPrimitive() and the layout using
If you have indexed mesh, you need to call @ref setIndexCount() instead of @ref addVertexBuffer().
@ref setVertexCount(). Then fill your index buffer with data and specify its
If you want indexed mesh, fill your index buffer with data and specify its
layout using @ref setIndexBuffer(). You can also use @ref MeshTools::compressIndices() layout using @ref setIndexBuffer(). You can also use @ref MeshTools::compressIndices()
to conveniently compress the indices, fill the index buffer and configure the to conveniently compress the indices, fill the index buffer and configure the
mesh instead of calling @ref setIndexCount() and @ref setIndexBuffer() manually. mesh. It will call @ref setCount() and @ref setIndexBuffer(), so you don't have
to do anything else.
Note that neither vertex buffers nor index buffer is managed (e.g. deleted on Note that neither vertex buffers nor index buffer is managed (e.g. deleted on
destruction) by the mesh, so you have to manage them on your own and ensure destruction) by the mesh, so you have to manage them on your own and ensure
that they are available for whole mesh lifetime. On the other hand it allows that they are available for whole mesh lifetime. On the other hand it allows
you to use one buffer for more meshes (each mesh for example configured for you to use one buffer for more meshes (each mesh for example configured for
different shader) or store data for more meshes in one buffer. different usage) or store data for more meshes in one buffer.
If the mesh has non-zero index count, it is treated as indexed mesh, otherwise If vertex/index count is zero, the mesh is empty and no draw commands are
it is treated as non-indexed mesh. If both index and vertex count is zero, the issued when calling @ref draw().
mesh is empty and no draw commands are issued when calling @ref draw().
@subsection Mesh-configuration-examples Example mesh configuration @subsection Mesh-configuration-examples Example mesh configuration
@ -169,7 +170,7 @@ vertexBuffer.setData(positions, BufferUsage::StaticDraw);
// Set primitive and vertex count, add the buffer and specify its layout // Set primitive and vertex count, add the buffer and specify its layout
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(MeshPrimitive::Triangles)
.setVertexCount(30) .setCount(30)
.addVertexBuffer(vertexBuffer, 0, MyShader::Position()); .addVertexBuffer(vertexBuffer, 0, MyShader::Position());
@endcode @endcode
@ -218,7 +219,7 @@ indexBuffer.setData(indices, BufferUsage::StaticDraw);
// Set primitive, index count, specify the buffers // Set primitive, index count, specify the buffers
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(MeshPrimitive::Triangles)
.setIndexCount(75) .setCount(75)
.addVertexBuffer(vertexBuffer, 0, MyShader::Position()) .addVertexBuffer(vertexBuffer, 0, MyShader::Position())
.setIndexBuffer(indexBuffer, 0, Mesh::IndexType::UnsignedByte, 176, 229); .setIndexBuffer(indexBuffer, 0, Mesh::IndexType::UnsignedByte, 176, 229);
@endcode @endcode
@ -395,7 +396,7 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
* @param primitive Primitive type * @param primitive Primitive type
* *
* Creates mesh with no vertex buffers and zero vertex count. * Creates mesh with no vertex buffers and zero vertex count.
* @see @ref setPrimitive(), @ref setVertexCount(), @fn_gl{GenVertexArrays} * @see @ref setPrimitive(), @ref setCount(), @fn_gl{GenVertexArrays}
* (if @extension{APPLE,vertex_array_object} is available) * (if @extension{APPLE,vertex_array_object} is available)
*/ */
explicit Mesh(MeshPrimitive primitive = MeshPrimitive::Triangles); explicit Mesh(MeshPrimitive primitive = MeshPrimitive::Triangles);
@ -454,6 +455,13 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
*/ */
Mesh& setLabel(const std::string& label); Mesh& setLabel(const std::string& label);
/**
* @brief Whether the mesh is indexed
*
* @see @ref setIndexBuffer(), @ref setCount()
*/
bool isIndexed() const { return _indexBuffer; }
/** /**
* @brief Index size * @brief Index size
* *
@ -469,44 +477,63 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
* @return Reference to self (for method chaining) * @return Reference to self (for method chaining)
* *
* Default is @ref MeshPrimitive::Triangles. * Default is @ref MeshPrimitive::Triangles.
* @see @ref setVertexCount(), @ref addVertexBuffer() * @see @ref setCount()
*/ */
Mesh& setPrimitive(MeshPrimitive primitive) { Mesh& setPrimitive(MeshPrimitive primitive) {
_primitive = primitive; _primitive = primitive;
return *this; return *this;
} }
/** @brief Vertex count */ /** @brief Vertex/index count */
Int vertexCount() const { return _vertexCount; } Int count() const { return _count; }
/** /**
* @brief Set vertex count * @brief Set vertex/index count
* @return Reference to self (for method chaining)
* *
* Default is zero. * If the mesh is indexed, the value is treated as index count,
* @see @ref setPrimitive(), @ref addVertexBuffer(), * otherwise the value is vertex count. Default is `0`. This value is
* @ref MeshTools::interleave() * set automatically by @ref MeshTools::interleave() and
* @ref MeshTools::compressIndices() functions.
* @see @ref isIndexed()
*/ */
Mesh& setVertexCount(Int vertexCount) { Mesh& setCount(Int count) {
_vertexCount = vertexCount; _count = count;
return *this; return *this;
} }
/** @brief Index count */ #ifdef MAGNUM_BUILD_DEPRECATED
Int indexCount() const { return _indexCount; } /**
* @copybrief count()
* @deprecated Use @ref Magnum::Mesh::count() "count()" instead.
*/
CORRADE_DEPRECATED("use count() instead") Int vertexCount() const {
return isIndexed() ? 0 : count();
}
/** /**
* @brief Set index count * @copybrief setCount()
* @return Reference to self (for method chaining) * @deprecated Use @ref Magnum::Mesh::setCount() "setCount()" instead.
*
* Default is zero.
* @see @ref setIndexBuffer(), @ref MeshTools::compressIndices()
*/ */
Mesh& setIndexCount(Int count) { CORRADE_DEPRECATED("use setCount() instead") Mesh& setVertexCount(Int count) {
_indexCount = count; if(!isIndexed()) setCount(count);
return *this; return *this;
} }
/**
* @copybrief count()
* @deprecated Use @ref Magnum::Mesh::count() "count()" instead.
*/
CORRADE_DEPRECATED("use count() instead") Int indexCount() const {
return count();
}
/**
* @copybrief setCount()
* @deprecated Use @ref Magnum::Mesh::setCount() "setCount()" instead.
*/
CORRADE_DEPRECATED("use setCount() instead") Mesh& setIndexCount(Int count) { return setCount(count); }
#endif
/** /**
* @brief Add buffer with (interleaved) vertex attributes for use with given shader * @brief Add buffer with (interleaved) vertex attributes for use with given shader
* @return Reference to self (for method chaining) * @return Reference to self (for method chaining)
@ -565,7 +592,7 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
* but doing so may have performance benefits. * but doing so may have performance benefits.
* *
* @see @ref maxVertexAttributes(), @ref setPrimitive(), * @see @ref maxVertexAttributes(), @ref setPrimitive(),
* @ref setVertexCount(), @fn_gl{BindVertexArray}, * @ref setCount(), @fn_gl{BindVertexArray},
* @fn_gl{EnableVertexAttribArray}, @fn_gl{BindBuffer}, * @fn_gl{EnableVertexAttribArray}, @fn_gl{BindBuffer},
* @fn_gl{VertexAttribPointer} or * @fn_gl{VertexAttribPointer} or
* @fn_gl_extension{EnableVertexArrayAttrib,EXT,direct_state_access}, * @fn_gl_extension{EnableVertexArrayAttrib,EXT,direct_state_access},
@ -594,9 +621,10 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
* @ref setIndexBuffer(Buffer&, GLintptr, IndexType), as this * @ref setIndexBuffer(Buffer&, GLintptr, IndexType), as this
* functionality is not available there. * functionality is not available there.
* @see @ref maxElementsIndices(), @ref maxElementsVertices(), * @see @ref maxElementsIndices(), @ref maxElementsVertices(),
* @ref setIndexCount(), @ref MeshTools::compressIndices(), * @ref setCount(), @ref isIndexed(),
* @fn_gl{BindVertexArray}, @fn_gl{BindBuffer} (if * @ref MeshTools::compressIndices(), @fn_gl{BindVertexArray},
* @extension{APPLE,vertex_array_object} is available) * @fn_gl{BindBuffer} (if @extension{APPLE,vertex_array_object} is
* available)
*/ */
Mesh& setIndexBuffer(Buffer& buffer, GLintptr offset, IndexType type, UnsignedInt start, UnsignedInt end); Mesh& setIndexBuffer(Buffer& buffer, GLintptr offset, IndexType type, UnsignedInt start, UnsignedInt end);
@ -607,11 +635,9 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
* @param type Index data type * @param type Index data type
* @return Reference to self (for method chaining) * @return Reference to self (for method chaining)
* *
* Prefer to use @ref setIndexBuffer(Buffer&, GLintptr, IndexType, UnsignedInt, UnsignedInt) * Alternative to @ref setIndexBuffer(Buffer&, GLintptr, IndexType, UnsignedInt, UnsignedInt)
* for better performance. * with unspecified index limits, see its documentation for more
* @see @ref setIndexCount(), @ref MeshTools::compressIndices(), * information. Prefer to set index limits for better performance.
* @fn_gl{BindVertexArray}, @fn_gl{BindBuffer} (if
* @extension{APPLE,vertex_array_object} is available)
*/ */
Mesh& setIndexBuffer(Buffer& buffer, GLintptr offset, IndexType type) { Mesh& setIndexBuffer(Buffer& buffer, GLintptr offset, IndexType type) {
return setIndexBuffer(buffer, offset, type, 0, 0); return setIndexBuffer(buffer, offset, type, 0, 0);
@ -629,15 +655,15 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
* @fn_gl{BindBuffer}, @fn_gl{VertexAttribPointer}, * @fn_gl{BindBuffer}, @fn_gl{VertexAttribPointer},
* @fn_gl{DisableVertexAttribArray} or @fn_gl{BindVertexArray} (if * @fn_gl{DisableVertexAttribArray} or @fn_gl{BindVertexArray} (if
* @extension{APPLE,vertex_array_object} is available), @fn_gl{DrawArrays} * @extension{APPLE,vertex_array_object} is available), @fn_gl{DrawArrays}
* or @fn_gl{DrawElements}/@fn_gl{DrawRangeElements}. * or @fn_gl{DrawElements}/@fn_gl{DrawRangeElements}
*/ */
void draw(AbstractShaderProgram& shader) { void draw(AbstractShaderProgram& shader) {
shader.use(); shader.use();
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
drawInternal(0, _vertexCount, _indexOffset, _indexCount, _indexStart, _indexEnd); drawInternal(_count, 0, _indexOffset, _indexStart, _indexEnd);
#else #else
drawInternal(0, _vertexCount, _indexOffset, _indexCount); drawInternal(_count, 0, _indexOffset);
#endif #endif
} }
@ -649,11 +675,11 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
* @deprecated Use @ref Magnum::Mesh::draw(AbstractShaderProgram&) "draw(AbstractShaderProgram&)" * @deprecated Use @ref Magnum::Mesh::draw(AbstractShaderProgram&) "draw(AbstractShaderProgram&)"
* instead. * instead.
*/ */
void draw() { CORRADE_DEPRECATED("use draw(AbstractShaderProgram&) instead") void draw() {
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
drawInternal(0, _vertexCount, _indexOffset, _indexCount, _indexStart, _indexEnd); drawInternal(_count, 0, _indexOffset, _indexStart, _indexEnd);
#else #else
drawInternal(0, _vertexCount, _indexOffset, _indexCount); drawInternal(_count, 0, _indexOffset);
#endif #endif
} }
#endif #endif
@ -774,9 +800,9 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
#endif #endif
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
void drawInternal(Int firstVertex, Int vertexCount, GLintptr indexOffset, Int indexCount, Int indexStart, Int indexEnd); void drawInternal(Int count, Int firstVertex, GLintptr indexOffset, Int indexStart, Int indexEnd);
#else #else
void drawInternal(Int firstVertex, Int vertexCount, GLintptr indexOffset, Int indexCount); void drawInternal(Int count, Int firstVertex, GLintptr indexOffset);
#endif #endif
void MAGNUM_LOCAL createImplementationDefault(); void MAGNUM_LOCAL createImplementationDefault();
@ -805,7 +831,7 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
#endif #endif
#endif #endif
void MAGNUM_LOCAL bindIndexBufferImplementationDefault(Buffer& buffer); void MAGNUM_LOCAL bindIndexBufferImplementationDefault(Buffer&);
void MAGNUM_LOCAL bindIndexBufferImplementationVAO(Buffer& buffer); void MAGNUM_LOCAL bindIndexBufferImplementationVAO(Buffer& buffer);
void MAGNUM_LOCAL bindImplementationDefault(); void MAGNUM_LOCAL bindImplementationDefault();
@ -816,7 +842,7 @@ class MAGNUM_EXPORT Mesh: public AbstractObject {
GLuint _id; GLuint _id;
MeshPrimitive _primitive; MeshPrimitive _primitive;
Int _vertexCount, _indexCount; Int _count;
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
UnsignedInt _indexStart, _indexEnd; UnsignedInt _indexStart, _indexEnd;
#endif #endif

16
src/Magnum/MeshTools/Compile.cpp

@ -72,17 +72,17 @@ std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const
stride - normalOffset - sizeof(Shaders::Generic2D::TextureCoordinates::Type)); stride - normalOffset - sizeof(Shaders::Generic2D::TextureCoordinates::Type));
} }
/* Fill vertex buffer with interleaved data and finalize mesh /* Fill vertex buffer with interleaved data */
configuration */
vertexBuffer->setData(data, BufferUsage::StaticDraw); vertexBuffer->setData(data, BufferUsage::StaticDraw);
mesh.setVertexCount(vertexCount);
/* Fill index buffer */ /* Fill index buffer */
std::unique_ptr<Buffer> indexBuffer; std::unique_ptr<Buffer> indexBuffer;
if(meshData.isIndexed()) { if(meshData.isIndexed()) {
indexBuffer.reset(new Buffer{Buffer::Target::ElementArray}); indexBuffer.reset(new Buffer{Buffer::Target::ElementArray});
MeshTools::compressIndices(mesh, *indexBuffer, usage, meshData.indices()); MeshTools::compressIndices(mesh, *indexBuffer, usage, meshData.indices());
}
/* Else set proper vertex count */
} else mesh.setCount(vertexCount);
return std::make_tuple(std::move(mesh), std::move(vertexBuffer), std::move(indexBuffer)); return std::make_tuple(std::move(mesh), std::move(vertexBuffer), std::move(indexBuffer));
} }
@ -139,17 +139,17 @@ std::tuple<Mesh, std::unique_ptr<Buffer>, std::unique_ptr<Buffer>> compile(const
stride - textureCoordsOffset - sizeof(Shaders::Generic3D::TextureCoordinates::Type)); stride - textureCoordsOffset - sizeof(Shaders::Generic3D::TextureCoordinates::Type));
} }
/* Fill vertex buffer with interleaved data and finalize mesh /* Fill vertex buffer with interleaved data */
configuration */
vertexBuffer->setData(data, BufferUsage::StaticDraw); vertexBuffer->setData(data, BufferUsage::StaticDraw);
mesh.setVertexCount(vertexCount);
/* Fill index buffer */ /* Fill index buffer */
std::unique_ptr<Buffer> indexBuffer; std::unique_ptr<Buffer> indexBuffer;
if(meshData.isIndexed()) { if(meshData.isIndexed()) {
indexBuffer.reset(new Buffer{Buffer::Target::ElementArray}); indexBuffer.reset(new Buffer{Buffer::Target::ElementArray});
MeshTools::compressIndices(mesh, *indexBuffer, usage, meshData.indices()); MeshTools::compressIndices(mesh, *indexBuffer, usage, meshData.indices());
}
/* Else set proper vertex count */
} mesh.setCount(vertexCount);
return std::make_tuple(std::move(mesh), std::move(vertexBuffer), std::move(indexBuffer)); return std::make_tuple(std::move(mesh), std::move(vertexBuffer), std::move(indexBuffer));
} }

2
src/Magnum/MeshTools/CompressIndices.cpp

@ -82,7 +82,7 @@ void compressIndices(Mesh& mesh, Buffer& buffer, BufferUsage usage, const std::v
Containers::Array<char> data; Containers::Array<char> data;
std::tie(indexCount, indexType, data) = compressIndicesInternal(indices, *minmax.second); std::tie(indexCount, indexType, data) = compressIndicesInternal(indices, *minmax.second);
mesh.setIndexCount(indices.size()) mesh.setCount(indices.size())
.setIndexBuffer(buffer, 0, indexType, *minmax.first, *minmax.second); .setIndexBuffer(buffer, 0, indexType, *minmax.first, *minmax.second);
buffer.setData(data, usage); buffer.setData(data, usage);
} }

6
src/Magnum/MeshTools/CompressIndices.h

@ -65,9 +65,9 @@ std::tuple<std::size_t, Mesh::IndexType, Containers::Array<char>> MAGNUM_MESHTOO
@param indices Index array @param indices Index array
The same as @ref compressIndices(const std::vector<UnsignedInt>&), but this The same as @ref compressIndices(const std::vector<UnsignedInt>&), but this
function writes the output to given buffer and calls @ref Mesh::setIndexCount() function writes the output to given buffer and calls @ref Mesh::setCount() and
and @ref Mesh::setIndexBuffer(), thus you don't need to do anything else for @ref Mesh::setIndexBuffer(), thus you don't need to do anything else for mesh
mesh index configuration. index configuration.
@see @ref MeshTools::interleave(), @ref MeshTools::compile() @see @ref MeshTools::interleave(), @ref MeshTools::compile()
*/ */

2
src/Magnum/MeshTools/FullScreenTriangle.cpp

@ -37,7 +37,7 @@ namespace Magnum { namespace MeshTools {
std::pair<std::unique_ptr<Buffer>, Mesh> fullScreenTriangle(Version version) { std::pair<std::unique_ptr<Buffer>, Mesh> fullScreenTriangle(Version version) {
Mesh mesh; Mesh mesh;
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(MeshPrimitive::Triangles)
.setVertexCount(3); .setCount(3);
std::unique_ptr<Buffer> buffer; std::unique_ptr<Buffer> buffer;
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES

13
src/Magnum/MeshTools/Interleave.h

@ -184,9 +184,10 @@ template<class T, class ...U> std::tuple<std::size_t, std::size_t> interleaveInt
@param usage Vertex buffer usage @param usage Vertex buffer usage
@param attributes Attribute arrays and gaps @param attributes Attribute arrays and gaps
The same as @ref interleave(const T&, const U&...), but this function writes the The same as @ref interleave(const T&, const U&...), but this function also
output to given array buffer and updates vertex count in the mesh accordingly, writes the output to given array buffer. If given mesh is not indexed, it also
so you don't have to call @ref Mesh::setVertexCount() on your own. updates vertex count in the mesh accordingly, so you don't have to call
@ref Mesh::setCount() on your own.
@attention You still must call @ref Mesh::setPrimitive() and @attention You still must call @ref Mesh::setPrimitive() and
@ref Mesh::addVertexBuffer() on the mesh afterwards. @ref Mesh::addVertexBuffer() on the mesh afterwards.
@ -199,7 +200,7 @@ template<class ...T> void interleave(Mesh& mesh, Buffer& buffer, BufferUsage usa
std::size_t attributeCount; std::size_t attributeCount;
std::tie(attributeCount, std::ignore, data) = interleave(attributes...); std::tie(attributeCount, std::ignore, data) = interleave(attributes...);
mesh.setVertexCount(attributeCount); if(!mesh.isIndexed()) mesh.setCount(attributeCount);
buffer.setData(data, usage); buffer.setData(data, usage);
} }
@ -210,11 +211,11 @@ Simplified specialization of the above function for only one attribute array,
equivalent to the following: equivalent to the following:
@code @code
buffer.setData(attribute, usage); buffer.setData(attribute, usage);
mesh.setVertexCount(attribute.size()); if(!mesh.isIndexed()) mesh.setVertexCount(attribute.size());
@endcode @endcode
*/ */
template<class T> typename std::enable_if<!std::is_convertible<T, std::size_t>::value, void>::type interleave(Mesh& mesh, Buffer& buffer, BufferUsage usage, const T& attribute) { template<class T> typename std::enable_if<!std::is_convertible<T, std::size_t>::value, void>::type interleave(Mesh& mesh, Buffer& buffer, BufferUsage usage, const T& attribute) {
mesh.setVertexCount(attribute.size()); if(!mesh.isIndexed()) mesh.setCount(attribute.size());
buffer.setData(attribute, usage); buffer.setData(attribute, usage);
} }

10
src/Magnum/MeshView.cpp

@ -36,7 +36,7 @@ MeshView& MeshView::setIndexRange(Int first, Int count, UnsignedInt, UnsignedInt
#endif #endif
{ {
_indexOffset = _original->_indexOffset + first*_original->indexSize(); _indexOffset = _original->_indexOffset + first*_original->indexSize();
_indexCount = count; _count = count;
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
_indexStart = start; _indexStart = start;
_indexEnd = end; _indexEnd = end;
@ -48,18 +48,18 @@ void MeshView::draw(AbstractShaderProgram& shader) {
shader.use(); shader.use();
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
_original->drawInternal(_firstVertex, _vertexCount, _indexOffset, _indexCount, _indexStart, _indexEnd); _original->drawInternal(_count, _firstVertex, _indexOffset, _indexStart, _indexEnd);
#else #else
_original->drawInternal(_firstVertex, _vertexCount, _indexOffset, _indexCount); _original->drawInternal(_count, _firstVertex, _indexOffset);
#endif #endif
} }
#ifdef MAGNUM_BUILD_DEPRECATED #ifdef MAGNUM_BUILD_DEPRECATED
void MeshView::draw() { void MeshView::draw() {
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
_original->drawInternal(_firstVertex, _vertexCount, _indexOffset, _indexCount, _indexStart, _indexEnd); _original->drawInternal(_count, _firstVertex, _indexOffset, _indexStart, _indexEnd);
#else #else
_original->drawInternal(_firstVertex, _vertexCount, _indexOffset, _indexCount); _original->drawInternal(_count, _firstVertex, _indexOffset);
#endif #endif
} }
#endif #endif

6
src/Magnum/MeshView.h

@ -83,7 +83,7 @@ class MAGNUM_EXPORT MeshView {
*/ */
MeshView& setVertexRange(Int first, Int count) { MeshView& setVertexRange(Int first, Int count) {
_firstVertex = first; _firstVertex = first;
_vertexCount = count; _count = count;
return *this; return *this;
} }
@ -135,14 +135,14 @@ class MAGNUM_EXPORT MeshView {
private: private:
Mesh* _original; Mesh* _original;
Int _firstVertex, _vertexCount, _indexCount; Int _firstVertex, _count;
GLintptr _indexOffset; GLintptr _indexOffset;
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
UnsignedInt _indexStart, _indexEnd; UnsignedInt _indexStart, _indexEnd;
#endif #endif
}; };
inline MeshView::MeshView(Mesh& original): _original(&original), _firstVertex(0), _vertexCount(0), _indexCount(0), _indexOffset(0) inline MeshView::MeshView(Mesh& original): _original(&original), _firstVertex(0), _count(0), _indexOffset(0)
#ifndef MAGNUM_TARGET_GLES2 #ifndef MAGNUM_TARGET_GLES2
, _indexStart(0), _indexEnd(0) , _indexStart(0), _indexEnd(0)
#endif #endif

8
src/Magnum/Test/MeshGLTest.cpp

@ -393,8 +393,8 @@ Checker::Checker(AbstractShaderProgram&& shader, RenderbufferFormat format, Mesh
framebuffer.attachRenderbuffer(Framebuffer::ColorAttachment(0), renderbuffer); framebuffer.attachRenderbuffer(Framebuffer::ColorAttachment(0), renderbuffer);
framebuffer.bind(FramebufferTarget::ReadDraw); framebuffer.bind(FramebufferTarget::ReadDraw);
mesh.setVertexCount(2) mesh.setPrimitive(MeshPrimitive::Points)
.setPrimitive(MeshPrimitive::Points); .setCount(2);
/* Skip first vertex so we test also offsets */ /* Skip first vertex so we test also offsets */
MeshView(mesh).setVertexRange(1, 1).draw(shader); MeshView(mesh).setVertexRange(1, 1).draw(shader);
@ -1147,8 +1147,8 @@ IndexChecker::IndexChecker(Mesh& mesh): framebuffer({{}, Vector2i(1)}) {
framebuffer.attachRenderbuffer(Framebuffer::ColorAttachment(0), renderbuffer); framebuffer.attachRenderbuffer(Framebuffer::ColorAttachment(0), renderbuffer);
framebuffer.bind(FramebufferTarget::ReadDraw); framebuffer.bind(FramebufferTarget::ReadDraw);
mesh.setIndexCount(2) mesh.setPrimitive(MeshPrimitive::Points)
.setPrimitive(MeshPrimitive::Points); .setCount(2);
/* Skip first vertex so we test also offsets */ /* Skip first vertex so we test also offsets */
MeshView(mesh).setIndexRange(1, 1).draw(MultipleShader{}); MeshView(mesh).setIndexRange(1, 1).draw(MultipleShader{});

2
src/Magnum/Test/PrimitiveQueryGLTest.cpp

@ -88,7 +88,7 @@ void PrimitiveQueryGLTest::query() {
Mesh mesh; Mesh mesh;
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(MeshPrimitive::Triangles)
.setVertexCount(9) .setCount(9)
.addVertexBuffer(vertices, 0, MyShader::Position()); .addVertexBuffer(vertices, 0, MyShader::Position());
MAGNUM_VERIFY_NO_ERROR(); MAGNUM_VERIFY_NO_ERROR();

4
src/Magnum/Test/SampleQueryGLTest.cpp

@ -112,7 +112,7 @@ void SampleQueryGLTest::querySamplesPassed() {
Mesh mesh; Mesh mesh;
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(MeshPrimitive::Triangles)
.setVertexCount(3) .setCount(3)
.addVertexBuffer(buffer, 0, AbstractShaderProgram::Attribute<0, Vector2>()); .addVertexBuffer(buffer, 0, AbstractShaderProgram::Attribute<0, Vector2>());
MyShader shader; MyShader shader;
@ -161,7 +161,7 @@ void SampleQueryGLTest::conditionalRender() {
Mesh mesh; Mesh mesh;
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(MeshPrimitive::Triangles)
.setVertexCount(3) .setCount(3)
.addVertexBuffer(buffer, 0, AbstractShaderProgram::Attribute<0, Vector2>()); .addVertexBuffer(buffer, 0, AbstractShaderProgram::Attribute<0, Vector2>());
MyShader shader; MyShader shader;

8
src/Magnum/Text/Renderer.cpp

@ -215,7 +215,7 @@ std::tuple<Mesh, Range2D> renderInternal(AbstractFont& font, const GlyphCache& c
in subclass) */ in subclass) */
Mesh mesh; Mesh mesh;
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(MeshPrimitive::Triangles)
.setIndexCount(indexCount) .setCount(indexCount)
.setIndexBuffer(indexBuffer, 0, indexType, 0, vertices.size()); .setIndexBuffer(indexBuffer, 0, indexType, 0, vertices.size());
return std::make_tuple(std::move(mesh), rectangle); return std::make_tuple(std::move(mesh), rectangle);
@ -342,7 +342,7 @@ void AbstractRenderer::reserve(const uint32_t glyphCount, const BufferUsage vert
#ifdef CORRADE_TARGET_EMSCRIPTEN #ifdef CORRADE_TARGET_EMSCRIPTEN
_vertexBufferData = Containers::Array<UnsignedByte>(vertexCount*sizeof(Vertex)); _vertexBufferData = Containers::Array<UnsignedByte>(vertexCount*sizeof(Vertex));
#endif #endif
_mesh.setVertexCount(0); _mesh.setCount(0);
/* Render indices */ /* Render indices */
Containers::Array<unsigned char> indexData; Containers::Array<unsigned char> indexData;
@ -354,7 +354,7 @@ void AbstractRenderer::reserve(const uint32_t glyphCount, const BufferUsage vert
#ifdef CORRADE_TARGET_EMSCRIPTEN #ifdef CORRADE_TARGET_EMSCRIPTEN
_indexBufferData = Containers::Array<UnsignedByte>(indexData.size()); _indexBufferData = Containers::Array<UnsignedByte>(indexData.size());
#endif #endif
_mesh.setIndexCount(0) _mesh.setCount(0)
.setIndexBuffer(_indexBuffer, 0, indexType, 0, vertexCount); .setIndexBuffer(_indexBuffer, 0, indexType, 0, vertexCount);
/* Prefill index buffer */ /* Prefill index buffer */
@ -386,7 +386,7 @@ void AbstractRenderer::render(const std::string& text) {
bufferUnmapImplementation(_vertexBuffer); bufferUnmapImplementation(_vertexBuffer);
/* Update index count */ /* Update index count */
_mesh.setIndexCount(indexCount); _mesh.setCount(indexCount);
} }
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT

2
src/Magnum/TextureTools/DistanceField.cpp

@ -179,7 +179,7 @@ void distanceField(Texture2D& input, Texture2D& output, const Range2Di& rectangl
Mesh mesh; Mesh mesh;
mesh.setPrimitive(MeshPrimitive::Triangles) mesh.setPrimitive(MeshPrimitive::Triangles)
.setVertexCount(3); .setCount(3);
/* Older GLSL doesn't have gl_VertexID, vertices must be supplied explicitly */ /* Older GLSL doesn't have gl_VertexID, vertices must be supplied explicitly */
Buffer buffer; Buffer buffer;

Loading…
Cancel
Save