Commit ff41ea3a authored by Janis Born's avatar Janis Born

implement ShaderProgram::setAttributeLocationsByVAO

parent a9cebf32
...@@ -54,6 +54,9 @@ ...@@ -54,6 +54,9 @@
namespace ACGL{ namespace ACGL{
namespace OpenGL{ namespace OpenGL{
class VertexArrayObject;
ACGL_SHARED_TYPEDEF(VertexArrayObject)
class ShaderProgram class ShaderProgram
{ {
ACGL_NOT_COPYABLE(ShaderProgram) ACGL_NOT_COPYABLE(ShaderProgram)
...@@ -112,12 +115,17 @@ public: ...@@ -112,12 +115,17 @@ public:
bool link (void) const; bool link (void) const;
// ===================================================================================================== \/
// ============================================================================================= METHODS \/
// ===================================================================================================== \/
public:
//! Matches the attribute locations of this ShaderProgram to the names of the ArrayBuffer attributes currently attached to _vao
void setAttributeLocationsByVAO( const SharedVertexArrayObject& _vao );
// ===================================================================================================== \/ // ===================================================================================================== \/
// ============================================================================================ UNIFORMS \/ // ============================================================================================ UNIFORMS \/
// ===================================================================================================== \/ // ===================================================================================================== \/
public:
// int by location // int by location
inline void setUniform (GLint _location, GLint _v) const { glUniform1i (_location, _v); } inline void setUniform (GLint _location, GLint _v) const { glUniform1i (_location, _v); }
inline void setUniform (GLint _location, GLsizei _n, GLint *_v)const { glUniform1iv(_location, _n, _v); } inline void setUniform (GLint _location, GLsizei _n, GLint *_v)const { glUniform1iv(_location, _n, _v); }
...@@ -231,7 +239,7 @@ public: ...@@ -231,7 +239,7 @@ public:
// ======================================================================================================= \/ // ======================================================================================================= \/
// ============================================================================================ HIGH LEVEL \/ // ============================================================================================ HIGH LEVEL \/
// ======================================================================================================= \/ // ======================================================================================================= \/
public:
// normal: // normal:
template <typename T> template <typename T>
inline void setUniform (const std::string& _nameInShader, T _v) const inline void setUniform (const std::string& _nameInShader, T _v) const
...@@ -277,7 +285,6 @@ public: ...@@ -277,7 +285,6 @@ public:
inline void setTexture (const std::string& _nameInShader, const ConstSharedTexture& _texture, GLenum _unit = 0) const { setUniform( getUniformLocation(_nameInShader), (GLint) _unit); _texture->bind(_unit); } inline void setTexture (const std::string& _nameInShader, const ConstSharedTexture& _texture, GLenum _unit = 0) const { setUniform( getUniformLocation(_nameInShader), (GLint) _unit); _texture->bind(_unit); }
inline void setProgramTexture (GLint _location, const ConstSharedTexture& _texture, GLenum _unit = 0) const { glProgramUniform1i(mObjectName, _location, _unit); _texture->bind(_unit); } inline void setProgramTexture (GLint _location, const ConstSharedTexture& _texture, GLenum _unit = 0) const { glProgramUniform1i(mObjectName, _location, _unit); _texture->bind(_unit); }
// =================================================================================================== \/ // =================================================================================================== \/
// ============================================================================================ FIELDS \/ // ============================================================================================ FIELDS \/
// =================================================================================================== \/ // =================================================================================================== \/
......
...@@ -56,6 +56,12 @@ public: ...@@ -56,6 +56,12 @@ public:
// more Attribute properties can be looked up in the ArrayBuffer (like the name) // more Attribute properties can be looked up in the ArrayBuffer (like the name)
}; };
// ===================================================================================================== \/
// ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/
public:
typedef std::vector< Attribute > AttributeVec;
// ========================================================================================================= \/ // ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/ // ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/ // ========================================================================================================= \/
...@@ -90,7 +96,8 @@ public: ...@@ -90,7 +96,8 @@ public:
public: public:
inline GLuint operator() (void) const { return mObjectName; } inline GLuint operator() (void) const { return mObjectName; }
inline GLuint getObjectName(void) const { return mObjectName; } inline GLuint getObjectName(void) const { return mObjectName; }
inline GLenum getMode(void) const { return mMode; } inline const AttributeVec& getAttributes(void) const { return mAttributes; }
inline GLenum getMode (void) const { return mMode; }
// ==================================================================================================== \/ // ==================================================================================================== \/
// ============================================================================================ METHODS \/ // ============================================================================================ METHODS \/
...@@ -231,7 +238,7 @@ public: ...@@ -231,7 +238,7 @@ public:
* Return true if setAttributeMappingsByShaderProgram(_shaderProgram) would do nothing. * Return true if setAttributeMappingsByShaderProgram(_shaderProgram) would do nothing.
* That means the VAO and ShaderProgram have already matching layouts. * That means the VAO and ShaderProgram have already matching layouts.
*/ */
bool mappingsMatchShaderProgram( const ConstSharedShaderProgram &_shaderProgram ) const bool mappingsMatchShaderProgram( const SharedShaderProgram &_shaderProgram ) const
{ {
for (AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) for (AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{ {
...@@ -247,7 +254,7 @@ public: ...@@ -247,7 +254,7 @@ public:
* Query the attribute locations based on the attribute names in the ArrayBuffers and the ShaderProgram * Query the attribute locations based on the attribute names in the ArrayBuffers and the ShaderProgram
* If they match, use the location reported from the ShaderProgram. * If they match, use the location reported from the ShaderProgram.
*/ */
void setAttributeLocationsByShaderProgram( const ConstSharedShaderProgram &_shaderProgram ) void setAttributeLocationsByShaderProgram( const SharedShaderProgram &_shaderProgram )
{ {
bool fullUpdateNeeded = false; bool fullUpdateNeeded = false;
...@@ -394,12 +401,6 @@ public: ...@@ -394,12 +401,6 @@ public:
drawArrays(mAttributes[0].arrayBuffer->getElements()); drawArrays(mAttributes[0].arrayBuffer->getElements());
} }
// ===================================================================================================== \/
// ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/
protected:
typedef std::vector< Attribute > AttributeVec;
// =================================================================================================== \/ // =================================================================================================== \/
// ============================================================================================ FIELDS \/ // ============================================================================================ FIELDS \/
// =================================================================================================== \/ // =================================================================================================== \/
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include <ACGL/OpenGL/Objects/ShaderProgram.hh> #include <ACGL/OpenGL/Objects/ShaderProgram.hh>
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
#include <ACGL/OpenGL/Tools.hh> #include <ACGL/OpenGL/Tools.hh>
using namespace ACGL::OpenGL; using namespace ACGL::OpenGL;
...@@ -47,3 +48,33 @@ bool ShaderProgram::link(void) const ...@@ -47,3 +48,33 @@ bool ShaderProgram::link(void) const
#endif #endif
return true; return true;
} }
void ShaderProgram::setAttributeLocationsByVAO( const SharedVertexArrayObject& _vao )
{
bool needsRelink = false;
// search through all attributes of _vao
VertexArrayObject::AttributeVec vaoAttributes = _vao->getAttributes();
for(VertexArrayObject::AttributeVec::size_type i = 0; i < vaoAttributes.size(); ++i)
{
if(vaoAttributes[i].location == -1)
continue;
// find the name of the current attribute in its ArrayBuffer
std::string arrayBufferAttributeName = vaoAttributes[i].arrayBuffer->getAttributes()[vaoAttributes[i].attributeID].name;
// find out whether an attribute with a matching name exists in this ShaderProgram
GLint attribLocation = getAttributeLocation(arrayBufferAttributeName);
if(attribLocation != -1 // attribute with that name exists?
&& attribLocation != vaoAttributes[i].location) // attribute with that name not already bound correctly?
{
bindAttributeLocation(arrayBufferAttributeName, vaoAttributes[i].location);
needsRelink = true;
}
}
// re-link the program only if necessary
if(needsRelink)
link();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment