Commit 0545b116 authored by Lars Krecklau's avatar Lars Krecklau

-Added controller for the uniforms

-Added SPOs to the state object
parent 0a3c77c3
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <ACGL/OpenGL/Controller/StateControl.hh> #include <ACGL/OpenGL/Controller/StateControl.hh>
#include <ACGL/OpenGL/Controller/TextureControl.hh> #include <ACGL/OpenGL/Controller/TextureControl.hh>
#include <ACGL/OpenGL/Controller/TextureControlFileJPG.hh> #include <ACGL/OpenGL/Controller/TextureControlFileJPG.hh>
#include <ACGL/OpenGL/Controller/UniformControl.hh>
#include <ACGL/OpenGL/Controller/VertexBufferObjectControl.hh> #include <ACGL/OpenGL/Controller/VertexBufferObjectControl.hh>
#include <ACGL/OpenGL/Controller/VertexBufferObjectControlFileOBJ.hh> #include <ACGL/OpenGL/Controller/VertexBufferObjectControlFileOBJ.hh>
......
...@@ -24,11 +24,11 @@ public: ...@@ -24,11 +24,11 @@ public:
StateControl( StateControl(
const ConstSharedVertexBufferObject& _vertexBufferObject, const ConstSharedVertexBufferObject& _vertexBufferObject,
const ConstSharedFrameBufferObject& _frameBufferObject, const ConstSharedFrameBufferObject& _frameBufferObject,
const ConstSharedShaderProgram& _shaderProgram, const ConstSharedShaderProgramObject& _shaderProgramObject,
const ConstSharedViewport& _viewport) const ConstSharedViewport& _viewport)
: mpVertexBufferObject(_vertexBufferObject), : mpVertexBufferObject(_vertexBufferObject),
mpFrameBufferObject(_frameBufferObject), mpFrameBufferObject(_frameBufferObject),
mpShaderProgram(_shaderProgram), mpShaderProgramObject(_shaderProgramObject),
mpViewport(_viewport) mpViewport(_viewport)
{} {}
virtual ~StateControl(void) {} virtual ~StateControl(void) {}
...@@ -37,10 +37,10 @@ public: ...@@ -37,10 +37,10 @@ public:
// ============================================================================================ GETTERS \/ // ============================================================================================ GETTERS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
inline StateControl& vertexBuffer (const ConstSharedVertexBufferObject& _vertexBufferObject) { mpVertexBufferObject = _vertexBufferObject; return *this; } inline StateControl& vertexBufferObject (const ConstSharedVertexBufferObject& _vertexBufferObject) { mpVertexBufferObject = _vertexBufferObject; return *this; }
inline StateControl& frameBuffer (const ConstSharedFrameBufferObject& _frameBufferObject) { mpFrameBufferObject = _frameBufferObject; return *this; } inline StateControl& frameBufferObject (const ConstSharedFrameBufferObject& _frameBufferObject) { mpFrameBufferObject = _frameBufferObject; return *this; }
inline StateControl& shaderProgram (const ConstSharedShaderProgram& _shaderProgram) { mpShaderProgram = _shaderProgram; return *this; } inline StateControl& shaderProgramObject (const ConstSharedShaderProgramObject& _shaderProgramObject) { mpShaderProgramObject = _shaderProgramObject; return *this; }
inline StateControl& viewport (const ConstSharedViewport& _viewport) { mpViewport = _viewport; return *this; } inline StateControl& viewport (const ConstSharedViewport& _viewport) { mpViewport = _viewport; return *this; }
// ===================================================================================================== \/ // ===================================================================================================== \/
// ============================================================================================ OVERRIDE \/ // ============================================================================================ OVERRIDE \/
...@@ -48,7 +48,7 @@ public: ...@@ -48,7 +48,7 @@ public:
public: public:
virtual SharedState create(void) virtual SharedState create(void)
{ {
SharedState state(new State(mpVertexBufferObject, mpFrameBufferObject, mpShaderProgram, mpViewport)); SharedState state(new State(mpVertexBufferObject, mpFrameBufferObject, mpShaderProgramObject, mpViewport));
return state; return state;
} }
...@@ -57,10 +57,10 @@ public: ...@@ -57,10 +57,10 @@ public:
// ============================================================================================ FIELDS \/ // ============================================================================================ FIELDS \/
// =================================================================================================== \/ // =================================================================================================== \/
protected: protected:
ConstSharedVertexBufferObject mpVertexBufferObject; ConstSharedVertexBufferObject mpVertexBufferObject;
ConstSharedFrameBufferObject mpFrameBufferObject; ConstSharedFrameBufferObject mpFrameBufferObject;
ConstSharedShaderProgram mpShaderProgram; ConstSharedShaderProgramObject mpShaderProgramObject;
ConstSharedViewport mpViewport; ConstSharedViewport mpViewport;
}; };
} // OpenGL } // OpenGL
......
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#ifndef ACGL_OPENGL_CONTROLLER_UNIFORMCONTROL_HH
#define ACGL_OPENGL_CONTROLLER_UNIFORMCONTROL_HH
#include <ACGL/ACGL.hh>
#include <ACGL/Resource/BasicCreateController.hh>
#include <ACGL/OpenGL/Objects/Uniform.hh>
#include <ACGL/OpenGL/GL.hh>
namespace ACGL{
namespace OpenGL{
template<class UNIFORM>
class UniformControl : public Resource::BasicCreateController<UNIFORM>
{
// ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
UniformControl(const typename UNIFORM::DATA_TYPE& _value)
: mValue(_value)
{}
virtual ~UniformControl() {}
// ==================================================================================================== \/
// ============================================================================================ METHODS \/
// ==================================================================================================== \/
public:
inline UniformControl& value (const typename UNIFORM::DATA_TYPE& _value) { mValue = _value; return *this; }
// ===================================================================================================== \/
// ============================================================================================ OVERRIDE \/
// ===================================================================================================== \/
public:
virtual std::tr1::shared_ptr<UNIFORM> create(void)
{
std::tr1::shared_ptr<UNIFORM> uniform(new UNIFORM());
uniform->setValue(mValue);
return uniform;
}
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
protected:
typename UNIFORM::DATA_TYPE mValue;
};
typedef UniformControl<Uniform1i> Uniform1iControl;
typedef UniformControl<Uniform1f> Uniform1fControl;
typedef UniformControl<Uniform2f> Uniform2fControl;
typedef UniformControl<Uniform3f> Uniform3fControl;
typedef UniformControl<Uniform4f> Uniform4fControl;
typedef UniformControl<UniformMatrix2f> UniformMatrix2fControl;
typedef UniformControl<UniformMatrix3f> UniformMatrix3fControl;
typedef UniformControl<UniformMatrix4f> UniformMatrix4fControl;
typedef UniformControl<UniformTexture> UniformTextureControl;
} // OpenGL
} // ACGL
#endif // ACGL_OPENGL_CONTROLLER_ARRAYBUFFERCONTROL_HH
...@@ -26,8 +26,8 @@ class Shader ...@@ -26,8 +26,8 @@ class Shader
// ============================================================================================ TYPEDEFS \/ // ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/ // ===================================================================================================== \/
public: public:
typedef std::vector<std::string> AttributeVec; typedef std::vector<std::string> AttributeNameVec;
typedef std::vector<std::string> FragmentDataVec; typedef std::vector<std::string> FragmentDataNameVec;
// ========================================================================================================= \/ // ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/ // ============================================================================================ CONSTRUCTORS \/
...@@ -53,8 +53,9 @@ public: ...@@ -53,8 +53,9 @@ public:
inline GLuint getContext (void) const { return mContext; } inline GLuint getContext (void) const { return mContext; }
inline GLenum getType (void) const { return mType; } inline GLenum getType (void) const { return mType; }
inline const std::vector<std::string>& getAttributes() const { return mAttributes; } inline const std::vector<std::string>& getAttributeNames (void) const { return mAttributeNames; }
inline const std::vector<std::string>& getFragmentData() const { return mFragmentData; } inline const std::vector<std::string>& getFragmentDataNames (void) const { return mFragmentDataNames; }
// ==================================================================================================== \/ // ==================================================================================================== \/
// ============================================================================================ METHODS \/ // ============================================================================================ METHODS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
...@@ -69,10 +70,10 @@ protected: ...@@ -69,10 +70,10 @@ protected:
// ============================================================================================ FIELDS \/ // ============================================================================================ FIELDS \/
// =================================================================================================== \/ // =================================================================================================== \/
protected: protected:
GLuint mContext; GLuint mContext;
GLenum mType; GLenum mType;
AttributeVec mAttributes; AttributeNameVec mAttributeNames;
FragmentDataVec mFragmentData; FragmentDataNameVec mFragmentDataNames;
}; };
ACGL_SHARED_TYPEDEF(Shader) ACGL_SHARED_TYPEDEF(Shader)
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include <ACGL/OpenGL/Objects/VertexBufferObject.hh> #include <ACGL/OpenGL/Objects/VertexBufferObject.hh>
#include <ACGL/OpenGL/Objects/FrameBufferObject.hh> #include <ACGL/OpenGL/Objects/FrameBufferObject.hh>
#include <ACGL/OpenGL/Objects/ShaderProgram.hh> #include <ACGL/OpenGL/Objects/ShaderProgramObject.hh>
#include <ACGL/OpenGL/Objects/Viewport.hh> #include <ACGL/OpenGL/Objects/Viewport.hh>
namespace ACGL{ namespace ACGL{
...@@ -54,13 +54,13 @@ private: ...@@ -54,13 +54,13 @@ private:
// ============================================================================================ CONSTRUCTORS \/ // ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/ // ========================================================================================================= \/
public: public:
State(ConstSharedVertexBufferObject _VertexBufferObject, State(ConstSharedVertexBufferObject _vertexBufferObject,
ConstSharedFrameBufferObject _FrameBufferObject, ConstSharedFrameBufferObject _frameBufferObject,
ConstSharedShaderProgram _shaderProgram, ConstSharedShaderProgramObject _shaderProgram,
ConstSharedViewport _viewport) ConstSharedViewport _viewport)
: mpVertexBufferObject(_VertexBufferObject), : mpVertexBufferObject(_vertexBufferObject),
mpFrameBufferObject(_FrameBufferObject), mpFrameBufferObject(_frameBufferObject),
mpShaderProgram(_shaderProgram), mpShaderProgramObject(_shaderProgram),
mpViewport(_viewport), mpViewport(_viewport),
mAttributeMappings(), mAttributeMappings(),
mFragmentDataMappings(), mFragmentDataMappings(),
...@@ -81,18 +81,20 @@ public: ...@@ -81,18 +81,20 @@ public:
public: public:
inline ConstSharedVertexBufferObject getVertexBufferObject (void) const { return mpVertexBufferObject; } inline ConstSharedVertexBufferObject getVertexBufferObject (void) const { return mpVertexBufferObject; }
inline ConstSharedFrameBufferObject getFrameBufferObject (void) const { return mpFrameBufferObject; } inline ConstSharedFrameBufferObject getFrameBufferObject (void) const { return mpFrameBufferObject; }
inline ConstSharedShaderProgram getShaderProgram (void) const { return mpShaderProgram; } inline ConstSharedShaderProgramObject getShaderProgramObject (void) const { return mpShaderProgramObject; }
inline ConstSharedViewport getViewport (void) const { return mpViewport; } inline ConstSharedViewport getViewport (void) const { return mpViewport; }
// ==================================================================================================== \/ // ==================================================================================================== \/
// ============================================================================================ METHODS \/ // ============================================================================================ METHODS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
void useViewport (void); void updateMappings (void);
void enableVertexBufferObject (void);
void bindFrameBufferObject (void); void useViewport (void) const;
void disableVertexBufferObject (void); void useShaderProgramObject (void) const;
void updateMappings (void); void bindFrameBufferObject (void) const;
void enableVertexBufferObject (void) const;
void disableVertexBufferObject (void) const;
inline void validate (void) const inline void validate (void) const
{ {
...@@ -100,14 +102,15 @@ public: ...@@ -100,14 +102,15 @@ public:
mpVertexBufferObject->validate(); mpVertexBufferObject->validate();
} }
inline void draw(void) inline void draw(void) const
{ {
mpVertexBufferObject->draw(); mpVertexBufferObject->draw();
} }
inline void render(void) inline void render(void) const
{ {
useViewport(); useViewport();
useShaderProgramObject();
bindFrameBufferObject(); bindFrameBufferObject();
enableVertexBufferObject(); enableVertexBufferObject();
draw(); draw();
...@@ -120,8 +123,8 @@ public: ...@@ -120,8 +123,8 @@ public:
protected: protected:
ConstSharedVertexBufferObject mpVertexBufferObject; ConstSharedVertexBufferObject mpVertexBufferObject;
ConstSharedFrameBufferObject mpFrameBufferObject; ConstSharedFrameBufferObject mpFrameBufferObject;
ConstSharedShaderProgram mpShaderProgram; ConstSharedShaderProgramObject mpShaderProgramObject;
ConstSharedViewport mpViewport; ConstSharedViewport mpViewport;
AttributeMappingVec mAttributeMappings; AttributeMappingVec mAttributeMappings;
FragmentDataMappingVec mFragmentDataMappings; FragmentDataMappingVec mFragmentDataMappings;
......
...@@ -36,6 +36,12 @@ ACGL_SHARED_TYPEDEF(Uniform) ...@@ -36,6 +36,12 @@ ACGL_SHARED_TYPEDEF(Uniform)
template<typename T> template<typename T>
class UniformData class UniformData
{ {
// ===================================================================================================== \/
// ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/
public:
typedef T DATA_TYPE;
// ========================================================================================================= \/ // ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/ // ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/ // ========================================================================================================= \/
......
...@@ -46,7 +46,8 @@ bool Shader::setSource(const std::string& _source) ...@@ -46,7 +46,8 @@ bool Shader::setSource(const std::string& _source)
std::istringstream stream(_source); std::istringstream stream(_source);
std::string line = ""; std::string line = "";
mAttributes.clear(); mAttributeNames.clear();
mFragmentDataNames.clear();
while (stream.good()) while (stream.good())
{ {
...@@ -62,7 +63,7 @@ bool Shader::setSource(const std::string& _source) ...@@ -62,7 +63,7 @@ bool Shader::setSource(const std::string& _source)
std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';'); std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';');
if(tokensOfToken.size() > 0) if(tokensOfToken.size() > 0)
{ {
mAttributes.push_back(tokensOfToken[0]); mAttributeNames.push_back(tokensOfToken[0]);
} }
} }
} }
...@@ -77,7 +78,7 @@ bool Shader::setSource(const std::string& _source) ...@@ -77,7 +78,7 @@ bool Shader::setSource(const std::string& _source)
std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';'); std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';');
if(tokensOfToken.size() > 0) if(tokensOfToken.size() > 0)
{ {
mFragmentData.push_back(tokensOfToken[0]); mFragmentDataNames.push_back(tokensOfToken[0]);
} }
} }
} }
......
...@@ -14,10 +14,9 @@ void ShaderProgram::bindAttributeLocations() const ...@@ -14,10 +14,9 @@ void ShaderProgram::bindAttributeLocations() const
{ {
for (ConstSharedShaderVec::size_type i = 0; i < mShaders.size(); ++i) for (ConstSharedShaderVec::size_type i = 0; i < mShaders.size(); ++i)
{ {
const Shader::AttributeVec& attribs = mShaders[i]->getAttributes(); for (Shader::AttributeNameVec::size_type k = 0; k < mShaders[i]->getAttributeNames().size(); ++k)
for (Shader::AttributeVec::size_type k = 0; k < attribs.size(); ++k)
{ {
glBindAttribLocation(mContext, k, attribs[k].c_str()); glBindAttribLocation(mContext, k, mShaders[i]->getAttributeNames()[k].c_str());
} }
} }
} }
...@@ -26,10 +25,9 @@ void ShaderProgram::bindFragmentDataLocations() const ...@@ -26,10 +25,9 @@ void ShaderProgram::bindFragmentDataLocations() const
{ {
for (ConstSharedShaderVec::size_type i = 0; i < mShaders.size(); ++i) for (ConstSharedShaderVec::size_type i = 0; i < mShaders.size(); ++i)
{ {
const Shader::FragmentDataVec& fragData = mShaders[i]->getFragmentData(); for (Shader::FragmentDataNameVec::size_type k = 0; k < mShaders[i]->getFragmentDataNames().size(); ++k)
for (Shader::FragmentDataVec::size_type k = 0; k < fragData.size(); ++k)
{ {
glBindFragDataLocation(mContext, k, fragData[k].c_str()); glBindFragDataLocation(mContext, k, mShaders[i]->getFragmentDataNames()[k].c_str());
} }
} }
} }
......
...@@ -14,18 +14,65 @@ using namespace ACGL::Base; ...@@ -14,18 +14,65 @@ using namespace ACGL::Base;
using namespace ACGL::Utils; using namespace ACGL::Utils;
using namespace ACGL::OpenGL; using namespace ACGL::OpenGL;
void State::useViewport() void State::updateMappings (void)
{
ShaderProgram::ConstSharedShaderVec shaders = mpShaderProgramObject->getShaderProgram()->getShaders();
for(ShaderProgram::ConstSharedShaderVec::size_type shader = 0; shader < shaders.size(); shader++)
{
for(Shader::AttributeNameVec::size_type k = 0; k < shaders[shader]->getAttributeNames().size(); k++)
{
AttributeMapping mapping;
mapping.shaderProgramAttributeLocation = mpShaderProgramObject->getShaderProgram()->getAttributeLocation(shaders[shader]->getAttributeNames()[k]);
mapping.vertexBufferObjectAttribute = mpVertexBufferObject->getAttributeIndexByName(shaders[shader]->getAttributeNames()[k]);
mapping.arrayBufferID = mpVertexBufferObject->getArrayBufferIDByAttributeIndex(mapping.vertexBufferObjectAttribute);
bool inserted = false;
for (AttributeMappingVec::iterator it = mAttributeMappings.begin(); it < mAttributeMappings.end() && !inserted; it++)
{
if((*it).arrayBufferID > mapping.arrayBufferID)
{
mAttributeMappings.insert(it, mapping);
inserted = true;
}
}
if(!inserted)
mAttributeMappings.push_back(mapping);
}
for(Shader::FragmentDataNameVec::size_type k = 0; k < shaders[shader]->getFragmentDataNames().size(); k++)
{
FragmentDataMapping mapping;
mapping.shaderProgramFragmentDataLocation = mpShaderProgramObject->getShaderProgram()->getFragmentDataLocation(shaders[shader]->getFragmentDataNames()[k]);
mapping.frameBufferObjectColorAttachment = mpFrameBufferObject->getColorAttachmentIndexByName(shaders[shader]->getFragmentDataNames()[k]);
mFragmentDataMappings.push_back(mapping);
}
}
mpBuffers = new GLuint[mFragmentDataMappings.size()];
for(FragmentDataMappingVec::size_type k = 0; k < mFragmentDataMappings.size(); k++)
mpBuffers[k] = GL_COLOR_ATTACHMENT0 + mFragmentDataMappings[k].frameBufferObjectColorAttachment;
}
void State::useViewport (void) const
{ {
mpViewport->use(); mpViewport->use();
} }
void State::bindFrameBufferObject() void State::useShaderProgramObject (void) const
{
mpShaderProgramObject->use();
}
void State::bindFrameBufferObject (void) const
{ {
mpFrameBufferObject->bind(); mpFrameBufferObject->bind();
glDrawBuffers(mFragmentDataMappings.size(), mpBuffers); glDrawBuffers(mFragmentDataMappings.size(), mpBuffers);
} }
void State::enableVertexBufferObject() void State::enableVertexBufferObject (void) const
{ {
if(mpVertexBufferObject->getElementArrayBuffer()) if(mpVertexBufferObject->getElementArrayBuffer())
mpVertexBufferObject->getElementArrayBuffer()->bind(); mpVertexBufferObject->getElementArrayBuffer()->bind();
...@@ -45,7 +92,7 @@ void State::enableVertexBufferObject() ...@@ -45,7 +92,7 @@ void State::enableVertexBufferObject()
} }
} }
void State::disableVertexBufferObject() void State::disableVertexBufferObject (void) const
{ {
int currentArrayBufferID = -1; int currentArrayBufferID = -1;
for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++) for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
...@@ -59,49 +106,3 @@ void State::disableVertexBufferObject() ...@@ -59,49 +106,3 @@ void State::disableVertexBufferObject()
openGLRareError(); openGLRareError();
} }
} }
void State::updateMappings (void)
{
ShaderProgram::ConstSharedShaderVec shaders = mpShaderProgram->getShaders();
for(ShaderProgram::ConstSharedShaderVec::size_type shader = 0; shader < shaders.size(); shader++)
{
const Shader::AttributeVec& attributeVec = shaders[shader]->getAttributes();
for(Shader::AttributeVec::size_type k = 0; k < attributeVec.size(); k++)
{
AttributeMapping mapping;
mapping.shaderProgramAttributeLocation = mpShaderProgram->getAttributeLocation(attributeVec[k]);
mapping.vertexBufferObjectAttribute = mpVertexBufferObject->getAttributeIndexByName(attributeVec[k]);
mapping.arrayBufferID = mpVertexBufferObject->getArrayBufferIDByAttributeIndex(mapping.vertexBufferObjectAttribute);
bool inserted = false;
for (AttributeMappingVec::iterator it = mAttributeMappings.begin(); it < mAttributeMappings.end() && !inserted; it++)
{
if((*it).arrayBufferID > mapping.arrayBufferID)
{
mAttributeMappings.insert(it, mapping);
inserted = true;
}
}
if(!inserted)
mAttributeMappings.push_back(mapping);
}
const Shader::FragmentDataVec& fragmentDataVec = shaders[shader]->getFragmentData();
for(Shader::FragmentDataVec::size_type k = 0; k < fragmentDataVec.size(); k++)
{
FragmentDataMapping mapping;
mapping.shaderProgramFragmentDataLocation = mpShaderProgram->getFragmentDataLocation(fragmentDataVec[k]);
mapping.frameBufferObjectColorAttachment = mpFrameBufferObject->getColorAttachmentIndexByName(fragmentDataVec[k]);
mFragmentDataMappings.push_back(mapping);
}
}
mpBuffers = new GLuint[mFragmentDataMappings.size()];
for(FragmentDataMappingVec::size_type k = 0; k < mFragmentDataMappings.size(); k++)
mpBuffers[k] = GL_COLOR_ATTACHMENT0 + mFragmentDataMappings[k].frameBufferObjectColorAttachment;
}
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