Commit 1aca7872 authored by Lars Krecklau's avatar Lars Krecklau
Browse files

-Added ShaderProgramObject and Uniform classes. Those are not used by the state now

-Changed the state controller forcing the user to specify the needed pointers
parent 557b6d6d
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#ifndef ACGL_OPENGL_CONTROLLER_SHADERPROGRAMOBJECTCONTROL_HH
#define ACGL_OPENGL_CONTROLLER_SHADERPROGRAMOBJECTCONTROL_HH
#include <ACGL/ACGL.hh>
#include <ACGL/Resource/FileController.hh>
#include <ACGL/OpenGL/Objects/ShaderProgramObject.hh>
#include <ACGL/OpenGL/GL.hh>
#include <vector>
namespace ACGL{
namespace OpenGL{
class ShaderProgramObjectControl : public Resource::BasicCreateController<ShaderProgramObject>
{
// ==================================================================================================== \/
// ============================================================================================ STRUCTS \/
// ==================================================================================================== \/
public:
struct UniformAttachmentDefine
{
std::string name;
ConstSharedUniform uniform;
};
struct UniformTextureAttachmentDefine
{
std::string name;
ConstSharedUniformTexture uniformTexture;
};
// ===================================================================================================== \/
// ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/
public:
typedef std::vector< UniformAttachmentDefine > UniformAttachmentDefineVec;
typedef std::vector< UniformTextureAttachmentDefine > UniformTextureAttachmentDefineVec;
// ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
ShaderProgramObjectControl(const ConstSharedShaderProgram& _shaderProgram)
: Resource::BasicCreateController<ShaderProgramObject>(),
mShaderProgram(_shaderProgram),
mUniformAttachmentDefines(),
mUniformTextureAttachmentDefines()
{}
virtual ~ShaderProgramObjectControl(void) {}
// ==================================================================================================== \/
// ============================================================================================ METHODS \/
// ==================================================================================================== \/
public:
inline ShaderProgramObjectControl& shaderProgram (const ConstSharedShaderProgram& _shaderProgram) { mShaderProgram = _shaderProgram; return *this; }
inline ShaderProgramObjectControl& uniform (const std::string& _name, const ConstSharedUniform& _uniform)
{
UniformAttachmentDefine u = {_name, _uniform};
mUniformAttachmentDefines.push_back(u);
return *this;
}
inline ShaderProgramObjectControl& uniformTexture (const std::string& _name, const ConstSharedUniformTexture& _uniformTexture)
{
UniformTextureAttachmentDefine u = {_name, _uniformTexture};
mUniformTextureAttachmentDefines.push_back(u);
return *this;
}
// ===================================================================================================== \/
// ============================================================================================ OVERRIDE \/
// ===================================================================================================== \/
public:
virtual SharedShaderProgramObject create(void);
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
protected:
ConstSharedShaderProgram mShaderProgram;
UniformAttachmentDefineVec mUniformAttachmentDefines;
UniformTextureAttachmentDefineVec mUniformTextureAttachmentDefines;
};
} // OpenGL
} // ACGL
#endif // ACGL_OPENGL_CONTROLLER_ShaderProgramObjectControl_HH
......@@ -21,11 +21,15 @@ class StateControl : public Resource::BasicCreateController<Texture>
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
StateControl(void)
: mpVertexBuffer(NULL),
mpFrameBuffer(NULL),
mpShaderProgram(NULL),
mpViewport(NULL)
StateControl(
const ConstSharedVertexBuffer& _vertexBuffer,
const ConstSharedVertexBuffer& _frameBuffer,
const ConstSharedVertexBuffer& _shaderProgram,
const ConstSharedVertexBuffer& _viewport)
: mpVertexBuffer(_vertexBuffer),
mpFrameBuffer(_frameBuffer),
mpShaderProgram(_shaderProgram),
mpViewport(_viewport)
{}
virtual ~StateControl(void) {}
......
......@@ -58,9 +58,11 @@ public:
// ============================================================================================ WRAPPERS \/
// ===================================================================================================== \/
public:
inline GLint getUniformLocation (const std::string& _nameInShader) const { return glGetUniformLocation(mContext, _nameInShader.c_str()); }
inline GLint getAttributeLocation (const std::string& _nameInShader) const { return glGetAttribLocation (mContext, _nameInShader.c_str()); }
inline GLint getFragmentDataLocation (const std::string& _nameInShader) const { return glGetFragDataLocation(mContext, _nameInShader.c_str()); };
inline GLint getUniformLocation (const std::string& _nameInShader) const { return glGetUniformLocation (mContext, _nameInShader.c_str()); }
inline GLint getAttributeLocation (const std::string& _nameInShader) const { return glGetAttribLocation (mContext, _nameInShader.c_str()); }
inline GLint getFragmentDataLocation (const std::string& _nameInShader) const { return glGetFragDataLocation(mContext, _nameInShader.c_str()); }
inline void use(void) const { glUseProgram(mContext); }
inline void attachShader(const ConstSharedShader& _shader)
{
......@@ -83,13 +85,6 @@ public:
inline void setTexture (GLint _location, const ConstSharedTexture& _texture, GLenum _unit = 0) const { glUniform1i(_location, _unit); _texture->bind(_unit); }
inline void use(void) const
{
glUseProgram(mContext);
}
inline GLint getUniformLocation( const char *_name ) const { return glGetUniformLocation( mContext, _name ); }
// =================================================================================================== \/
// ============================================================================== HIGH LEVEL FUNCTIONS \/
// =================================================================================================== \/
......
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#ifndef ACGL_OPENGL_OBJECTS_SHADERPROGRAMOBJECT_HH
#define ACGL_OPENGL_OBJECTS_SHADERPROGRAMOBJECT_HH
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Macros.hh>
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Objects/ShaderProgram.hh>
#include <ACGL/OpenGL/Objects/Uniform.hh>
#include <tr1/memory>
namespace ACGL{
namespace OpenGL{
class ShaderProgramObject
{
ACGL_NOT_COPYABLE(ShaderProgramObject)
// ==================================================================================================== \/
// ============================================================================================ STRUCTS \/
// ==================================================================================================== \/
public:
struct UniformAttachment
{
GLint location;
ConstSharedUniform uniform;
};
struct UniformTextureAttachment
{
GLint location;
ConstSharedUniformTexture uniformTexture;
};
// ===================================================================================================== \/
// ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/
public:
typedef std::vector< UniformAttachment > UniformAttachmentVec;
typedef std::vector< UniformTextureAttachment > UniformTextureAttachmentVec;
// ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
ShaderProgramObject(const ConstSharedShaderProgram& _shaderProgram)
: mpShaderProgram(_shaderProgram),
mUniformAttachments(),
mUniformTextureAttachments()
{}
virtual ~ShaderProgramObject(void) {}
// ==================================================================================================== \/
// ============================================================================================ GETTERS \/
// ==================================================================================================== \/
public:
inline const ConstSharedShaderProgram& getShaderProgram (void) const { return mpShaderProgram; }
inline const UniformAttachmentVec& getUniformAttachments (void) const { return mUniformAttachments; }
inline const UniformTextureAttachmentVec& getUniformTextureAttachments (void) const { return mUniformTextureAttachments; }
// ===================================================================================================== \/
// ============================================================================================ WRAPPERS \/
// ===================================================================================================== \/
public:
void use (void) const;
inline void attachUniform(
const std::string& _name,
const ConstSharedUniform& _uniform)
{
UniformAttachment uniformAttachment = {
mpShaderProgram->getUniformLocation(_name),
_uniform};
mUniformAttachments.push_back(uniformAttachment);
}
inline void attachUniformTexture(
const std::string& _name,
const ConstSharedUniformTexture& _uniformTexture)
{
UniformTextureAttachment uniformTextureAttachment = {
mpShaderProgram->getUniformLocation(_name),
_uniformTexture};
mUniformTextureAttachments.push_back(uniformTextureAttachment);
}
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
protected:
ConstSharedShaderProgram mpShaderProgram;
UniformAttachmentVec mUniformAttachments;
UniformTextureAttachmentVec mUniformTextureAttachments;
};
ACGL_SHARED_TYPEDEF(ShaderProgramObject)
} // OpenGL
} // ACGL
#endif // ACGL_OPENGL_OBJECTS_ShaderProgramObject_HH
......@@ -8,48 +8,39 @@
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Macros.hh>
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/OpenGL/ResourceTypes.hh>
#include <ACGL/Math/Math.hh>
#include <ACGL/OpenGL/Objects/Texture.hh>
#include <ACGL/Base/StringOperations.hh>
#include <tr1/memory>
#include <vector>
/*
namespace ACGL{
namespace OpenGL{
namespace Objects{
class Uniform : public BasicResource
// ================================================================================================== \/
// ================================================================================== BASIC INTERFACE \/
// ================================================================================================== \/
class Uniform
{
public:
Uniform(void) : BasicResource() {}
virtual ~Uniform(void) {}
//! Apply the uniform to a specified location
virtual void apply(GLint) const = 0;
};
ACGL_SHARED_TYPEDEF(Uniform)
// ================================================================================================= \/
// ===================================================================================== C-SIDE DATA \/
// ================================================================================================= \/
template<typename T>
class UniformData : public Uniform
class UniformData
{
// ========================================================================================================= \/
// ============================================================================================ INITIALIZERS \/
// ========================================================================================================= \/
public:
class Init
{
friend class UniformData<T>;
public:
Init(const T& _value) : mValue(_value) {}
private:
T mValue;
};
// ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
UniformData(const Init& _init) : Uniform(), mValue(_init.mValue) { setValid(); }
UniformData(void) : mValue() {}
virtual ~UniformData(void) {}
// ==================================================================================================== \/
......@@ -63,16 +54,7 @@ public:
// ==================================================================================================== \/
public:
inline const T& getValue (void) const { return mValue; }
// ===================================================================================================== \/
// ============================================================================================ OVERLOAD \/
// ===================================================================================================== \/
public:
std::string toString (const std::string& _linePrefix = "") const
{
return _linePrefix + "Uniform:\n Value:" + Utils::StringOperations::toString(mValue) + "\n";
}
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
......@@ -80,17 +62,116 @@ protected:
T mValue;
};
class Uniform1f : public UniformData<float>
// ===================================================================================================== \/
// ===================================================================================== IMPLEMENTATIONS \/
// ===================================================================================================== \/
class Uniform1i : public UniformData<GLint>, public Uniform
{
public:
Uniform1i(void) : UniformData<GLint>() {}
virtual ~Uniform1i(void) {}
void apply(GLint _location) const { glUniform1i(_location, mValue); }
};
ACGL_SHARED_TYPEDEF(Uniform1i)
//=========================
class Uniform1f : public UniformData<GLfloat>, public Uniform
{
public:
Uniform1f(const Init& _init) : UniformData<float>(_init) {}
Uniform1f(void) : UniformData<GLfloat>() {}
virtual ~Uniform1f(void) {}
void apply(GLint _location) const { glUniform1f(_location, mValue); }
};
} // Objects
ACGL_SHARED_TYPEDEF(Uniform1f)
//=========================
class Uniform2f : public UniformData<glm::vec2>, public Uniform
{
public:
Uniform2f(void) : UniformData<glm::vec2>() {}
virtual ~Uniform2f(void) {}
void apply(GLint _location) const { glUniform2fv(_location, 1, glm::value_ptr(mValue)); }
};
ACGL_SHARED_TYPEDEF(Uniform2f)
//=========================
class Uniform3f : public UniformData<glm::vec3>, public Uniform
{
public:
Uniform3f(void) : UniformData<glm::vec3>() {}
virtual ~Uniform3f(void) {}
void apply(GLint _location) const { glUniform3fv(_location, 1, glm::value_ptr(mValue)); }
};
ACGL_SHARED_TYPEDEF(Uniform3f)
//=========================
class Uniform4f : public UniformData<glm::vec4>, public Uniform
{
public:
Uniform4f(void) : UniformData<glm::vec4>() {}
virtual ~Uniform4f(void) {}
void apply(GLint _location) const { glUniform4fv(_location, 1, glm::value_ptr(mValue)); }
};
ACGL_SHARED_TYPEDEF(Uniform4f)
//=========================
class UniformMatrix2f : public UniformData<glm::mat2>, public Uniform
{
public:
UniformMatrix2f(void) : UniformData<glm::mat2>() {}
virtual ~UniformMatrix2f(void) {}
void apply(GLint _location) const { glUniformMatrix2fv(_location, 1, GL_FALSE, glm::value_ptr(mValue)); }
};
ACGL_SHARED_TYPEDEF(UniformMatrix2f)
//=========================
class UniformMatrix3f : public UniformData<glm::mat3>, public Uniform
{
public:
UniformMatrix3f(void) : UniformData<glm::mat3>() {}
virtual ~UniformMatrix3f(void) {}
void apply(GLint _location) const { glUniformMatrix3fv(_location, 1, GL_FALSE, glm::value_ptr(mValue)); }
};
ACGL_SHARED_TYPEDEF(UniformMatrix3f)
//=========================
class UniformMatrix4f : public UniformData<glm::mat4>, public Uniform
{
public:
UniformMatrix4f(void) : UniformData<glm::mat4>() {}
virtual ~UniformMatrix4f(void) {}
void apply(GLint _location) const { glUniformMatrix4fv(_location, 1, GL_FALSE, glm::value_ptr(mValue)); }
};
ACGL_SHARED_TYPEDEF(UniformMatrix4f)
//=========================
class UniformTexture : public UniformData<ConstSharedTexture>
{
public:
UniformTexture(void) : UniformData<ConstSharedTexture>() {}
virtual ~UniformTexture(void) {}
void apply(GLint _location, GLenum _unit) const { glUniform1i(_location, _unit); mValue->bind(_unit); }
};
ACGL_SHARED_TYPEDEF(UniformTexture)
} // OpenGL
} // ACGL
*/
#endif // ACGL_OPENGL_OBJECTS_UNIFORM_HH
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#include <ACGL/OpenGL/Controller/ShaderProgramObjectControl.hh>
using namespace ACGL::OpenGL;
SharedShaderProgramObject ShaderProgramObjectControl::create(void)
{
SharedShaderProgramObject shaderProgramObject(new ShaderProgramObject(mShaderProgram));
for(UniformAttachmentDefineVec::size_type i = 0; i < mUniformAttachmentDefines.size(); i++)
shaderProgramObject->attachUniform(mUniformAttachmentDefines[i].name, mUniformAttachmentDefines[i].uniform);
for(UniformTextureAttachmentDefineVec::size_type i = 0; i < mUniformTextureAttachmentDefines.size(); i++)
shaderProgramObject->attachUniformTexture(mUniformTextureAttachmentDefines[i].name, mUniformTextureAttachmentDefines[i].uniformTexture);
return shaderProgramObject;
}
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#include <ACGL/OpenGL/Objects/ShaderProgramObject.hh>
using namespace ACGL::OpenGL;
void ShaderProgramObject::use(void) const
{
mpShaderProgram->use();
for(UniformAttachmentVec::size_type i = 0; i < mUniformAttachments.size(); ++i)
mUniformAttachments[i].uniform->apply(mUniformAttachments[i].location);
for(UniformTextureAttachmentVec::size_type i = 0; i < mUniformTextureAttachments.size(); ++i)
mUniformTextureAttachments[i].uniformTexture->apply(mUniformTextureAttachments[i].location, i);
}
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