Commit 24446ffc authored by sebastian's avatar sebastian
Browse files

shader attributes are now bound by their occurence in the shader source (just...

shader attributes are now bound by their occurence in the shader source (just before linking the program)
parent d8dac36b
......@@ -6,6 +6,8 @@
#ifndef ACGL_RESOURCE_SHADER_HH
#define ACGL_RESOURCE_SHADER_HH
#include <vector>
#include <string>
#include <ACGL/GL.hh>
namespace ACGL{
......@@ -13,6 +15,11 @@ namespace Resource{
class Shader
{
// ===================================================================================================== \/
// ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/
public:
typedef std::vector<std::string> AttributeVec;
// ================================================================================================== \/
// ============================================================================================ ENUMS \/
// ================================================================================================== \/
......@@ -54,6 +61,8 @@ public:
inline GLuint getContext (void) const { return mContext; }
inline GLenum getType (void) const { return mType; }
inline const std::vector<std::string>& getAttributes() const { return mAttributes; }
// ==================================================================================================== \/
// ============================================================================================ METHODS \/
// ==================================================================================================== \/
......@@ -70,6 +79,7 @@ protected:
protected:
GLuint mContext;
GLenum mType;
AttributeVec mAttributes;
};
} // Resource
......
......@@ -108,6 +108,8 @@ public:
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
protected:
void bindAttributeLocations() const;
GLuint mContext;
SharedShaderVec mShaders;
};
......
......@@ -6,6 +6,7 @@
#include <ACGL/Resource/Shader.hh>
#include <ACGL/GLUtils/Tools.hh>
#include <ACGL/Utils/Log.hh>
#include <ACGL/Utils/StringOperations.hh>
#include <iostream>
#include <fstream>
......@@ -42,6 +43,27 @@ bool Shader::setFromFile(const std::string& _filename)
bool Shader::setSource(const std::string& _source)
{
std::istringstream stream(_source);
std::string line = "";
mAttributes.clear();
while (stream.good())
{
std::getline(stream,line);
if(StringOperations::startsWith(line, "attribute"))
{
std::vector<std::string> tokens = StringOperations::split(line, ' ');
if(tokens.size() > 2)
{
std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';');
if(tokensOfToken.size() > 0)
{
mAttributes.push_back(tokensOfToken[0]);
}
}
}
}
return compile(_source.c_str());
}
......
......@@ -12,8 +12,22 @@ using namespace ACGL::GLUtils::Tools;
using namespace ACGL::Utils;
using namespace ACGL::Resource;
void ShaderProgram::bindAttributeLocations() const
{
for (SharedShaderVec::size_type i = 0; i < mShaders.size(); ++i)
{
const Shader::AttributeVec& attribs = mShaders[i]->getAttributes();
for (Shader::AttributeVec::size_type k = 0; k < attribs.size(); ++k)
{
glBindAttribLocation(mContext, k, attribs[k].c_str());
}
}
}
bool ShaderProgram::link(void) const
{
bindAttributeLocations();
glLinkProgram(mContext);
if ( openGLRareErrorOccured() )
......
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