Skip to content

Commit 47b1dc2

Browse files
Merge pull request #1272 from proberts-cinesite/IECoreGL-accessors
IECoreGL Buffer/Primitive accessors
2 parents 6eaed51 + 8633da7 commit 47b1dc2

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

include/IECoreGL/Buffer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class IECOREGL_API Buffer : public IECore::RunTimeTyped
6262
/// Deletes the buffer with glDeleteBuffers().
6363
~Buffer() override;
6464

65+
/// Returns the GL handle for the buffer. Note that this is
66+
/// owned by the Buffer class and will be destroyed in the
67+
/// destructor - you must therefore not call glDeleteBuffers()
68+
/// yourself.
69+
GLuint buffer() const;
70+
6571
/// Returns the size of the buffer in bytes.
6672
size_t size() const;
6773

include/IECoreGL/Primitive.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include "IECoreGL/Export.h"
3939
#include "IECoreGL/GL.h"
40+
#include "IECoreGL/Buffer.h"
4041
#include "IECoreGL/Renderable.h"
4142
#include "IECoreGL/Shader.h"
4243
#include "IECoreGL/TypedStateComponent.h"
@@ -165,6 +166,12 @@ class IECOREGL_API Primitive : public Renderable
165166
IE_CORE_DECLAREPTR( TransparencySort );
166167
//@}
167168

169+
/// Call to retrieve the buffer for a previously registered vertex attribute.
170+
ConstBufferPtr getVertexBuffer( const std::string &name ) const;
171+
172+
/// Call to determine the number of vertices in each registered vertex attribute.
173+
size_t getVertexCount() const;
174+
168175
protected :
169176

170177
/// Called by derived classes to register a uniform attribute. There are no type or length checks on this call.

src/IECoreGL/Buffer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ Buffer::~Buffer()
9494
glDeleteBuffers( 1, &m_buffer );
9595
}
9696

97+
GLuint Buffer::buffer() const
98+
{
99+
return m_buffer;
100+
}
101+
97102
size_t Buffer::size() const
98103
{
99104
ScopedBinding binding( *this, GL_ARRAY_BUFFER );

src/IECoreGL/Primitive.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ const Shader::Setup *flatConstantShaderSetup( State *state, bool forIDRender )
153153
return shaderSetup.get();
154154
}
155155

156+
const std::string g_P( "P" );
157+
156158
} // namespace
157159

158160
//////////////////////////////////////////////////////////////////////////
@@ -380,6 +382,28 @@ void Primitive::addVertexAttribute( const std::string &name, IECore::ConstDataPt
380382
m_vertexAttributes[name] = data->copy();
381383
}
382384

385+
ConstBufferPtr Primitive::getVertexBuffer( const std::string &name ) const
386+
{
387+
const AttributeMap::const_iterator it = m_vertexAttributes.find( name );
388+
return ( it != m_vertexAttributes.end() )
389+
? IECore::runTimeCast< const Buffer >( CachedConverter::defaultCachedConverter()->convert( it->second.get() ) )
390+
: ConstBufferPtr();
391+
}
392+
393+
size_t Primitive::getVertexCount() const
394+
{
395+
// NOTE : It would perhaps be better if the vertex count was stored as a member variable, then we could
396+
// check the size of each registered vertex attribute as it is added. Derived classes would need
397+
// to set this value perhaps as an argument to the constructor. However that would break ABI so for
398+
// now determine the vertex count by inspecting the size of the "P" attribute which ALL primitives
399+
// should have registered.
400+
401+
const AttributeMap::const_iterator it = m_vertexAttributes.find( g_P );
402+
return ( ( it != m_vertexAttributes.end() ) && ( IECore::runTimeCast< const IECore::V3fVectorData >( it->second ) ) )
403+
? static_cast< size_t >( IECore::assertedStaticCast< const IECore::V3fVectorData >( it->second )->readable().size() )
404+
: static_cast< size_t >( 0 );
405+
}
406+
383407
bool Primitive::depthSortRequested( const State * state ) const
384408
{
385409
return state->get<Primitive::TransparencySort>()->value() &&

0 commit comments

Comments
 (0)