Commit 269a6c27 authored by Andreas Neu's avatar Andreas Neu
Browse files

Added the class State to the framework in order to manage combinations of...

Added the class State to the framework in order to manage combinations of VertexBuffer, FrameBuffer and ShaderProgram.
The class automatically creates a mapping between the attributes of vertexbuffers and shaders.
Furthermore a mapping between FragDatas of shaders and rendertargets of framebuffers are created.
parent 69276471
...@@ -130,7 +130,7 @@ public: ...@@ -130,7 +130,7 @@ public:
glBindBuffer(GL_ARRAY_BUFFER, mContext); glBindBuffer(GL_ARRAY_BUFFER, mContext);
} }
inline void setPointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const inline void setAttributePointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const
{ {
glVertexAttribPointer( glVertexAttribPointer(
_indexInShader, _indexInShader,
...@@ -141,13 +141,13 @@ public: ...@@ -141,13 +141,13 @@ public:
reinterpret_cast<GLvoid*>(mAttributes[_indexInArray].offset)); reinterpret_cast<GLvoid*>(mAttributes[_indexInArray].offset));
} }
inline bool setPointer(const std::string& _nameInArray, GLuint _indexInShader) const inline bool setAttributePointer(const std::string& _nameInArray, GLuint _indexInShader) const
{ {
int_t index = getAttributeIndexByName(_nameInArray); int_t index = getAttributeIndexByName(_nameInArray);
if(index == -1) if(index == -1)
return false; return false;
setPointer(index, _indexInShader); setAttributePointer(index, _indexInShader);
return true; return true;
} }
......
...@@ -83,6 +83,11 @@ public: ...@@ -83,6 +83,11 @@ public:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mContext); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mContext);
} }
void draw(GLint _first, GLsizei _count) const
{
glDrawElements(mMode, _count, mType, reinterpret_cast<GLvoid*>(_first));
}
void draw(void) const void draw(void) const
{ {
glDrawElements(mMode, mElements, mType, (GLvoid*)0); glDrawElements(mMode, mElements, mType, (GLvoid*)0);
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <ACGL/OpenGL/Objects/RenderBuffer.hh> #include <ACGL/OpenGL/Objects/RenderBuffer.hh>
#include <ACGL/OpenGL/Objects/Texture.hh> #include <ACGL/OpenGL/Objects/Texture.hh>
#include <vector>
#include <tr1/memory> #include <tr1/memory>
namespace ACGL{ namespace ACGL{
...@@ -106,6 +107,8 @@ public: ...@@ -106,6 +107,8 @@ public:
// ============================================================================================ WRAPPERS \/ // ============================================================================================ WRAPPERS \/
// ===================================================================================================== \/ // ===================================================================================================== \/
public: public:
int_t getColorAttachmentIndexByName(const std::string& _name) const;
/** /**
* Per default a FrameBuffer gets used for read/write operations, but we can * Per default a FrameBuffer gets used for read/write operations, but we can
* bind two different FrameBuffers for these operations! * bind two different FrameBuffers for these operations!
...@@ -118,16 +121,18 @@ public: ...@@ -118,16 +121,18 @@ public:
inline void bindAsRenderTarget(GLenum _type = GL_FRAMEBUFFER) const inline void bindAsRenderTarget(GLenum _type = GL_FRAMEBUFFER) const
{ {
bind(_type); glBindFramebuffer(_type, mContext);
setDrawBuffers(); glDrawBuffers(mDrawBuffers, msBuffers);
glViewport(0, 0, mWidth, mHeight); glViewport(0, 0, mWidth, mHeight);
openGLRareError();
} }
inline void bindAsRenderTarget(GLint _x, GLint _y, GLsizei _w, GLsizei _h, GLenum _type = GL_FRAMEBUFFER) const inline void bindAsRenderTarget(GLint _x, GLint _y, GLsizei _w, GLsizei _h, GLenum _type = GL_FRAMEBUFFER) const
{ {
bind(_type); glBindFramebuffer(_type, mContext);
setDrawBuffers(); glDrawBuffers(mDrawBuffers, msBuffers);
glViewport(0, 0, mWidth, mHeight); glViewport(0, 0, mWidth, mHeight);
openGLRareError();
} }
inline bool isFrameBufferComplete(void) const inline bool isFrameBufferComplete(void) const
...@@ -218,13 +223,6 @@ public: ...@@ -218,13 +223,6 @@ public:
return true; return true;
} }
private:
inline void setDrawBuffers(void) const
{
glDrawBuffers(mDrawBuffers, msBuffers);
openGLRareError();
}
// =================================================================================================== \/ // =================================================================================================== \/
// ============================================================================================ FIELDS \/ // ============================================================================================ FIELDS \/
// =================================================================================================== \/ // =================================================================================================== \/
......
...@@ -59,7 +59,8 @@ public: ...@@ -59,7 +59,8 @@ public:
// ===================================================================================================== \/ // ===================================================================================================== \/
public: public:
inline GLint getUniformLocation (const std::string& _nameInShader) const { return glGetUniformLocation(mContext, _nameInShader.c_str()); } inline GLint getUniformLocation (const std::string& _nameInShader) const { return glGetUniformLocation(mContext, _nameInShader.c_str()); }
inline GLint getAttribLocation (const std::string& _nameInShader) const { return glGetAttribLocation (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 attachShader(const ConstSharedShader& _shader) inline void attachShader(const ConstSharedShader& _shader)
{ {
......
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#ifndef ACGL_OPENGL_OBJECTS_STATE_HH
#define ACGL_OPENGL_OBJECTS_STATE_HH
#include <vector>
#include <string>
#include <tr1/memory>
#include <iostream>
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Macros.hh>
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Objects/VertexBuffer.hh>
#include <ACGL/OpenGL/Objects/FrameBuffer.hh>
#include <ACGL/OpenGL/Objects/ShaderProgram.hh>
namespace ACGL{
namespace OpenGL{
class State
{
ACGL_NOT_COPYABLE(State)
// ==================================================================================================== \/
// ============================================================================================ STRUCTS \/
// ==================================================================================================== \/
private:
struct AttributeMapping
{
int idInVertexBuffer;
int idInShaderProgram;
};
struct FragmentDataMapping
{
int idInFrameBuffer;
int idInShaderProgram;
};
// ===================================================================================================== \/
// ============================================================================================ TYPEDEFS \/
// ===================================================================================================== \/
private:
typedef std::vector< AttributeMapping > AttributeMappingVec;
typedef std::vector< FragmentDataMapping > FragmentDataMappingVec;
// ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
State(ConstSharedVertexBuffer _vertexBuffer,
ConstSharedFrameBuffer _frameBuffer,
ConstSharedShaderProgram _shaderProgram)
: mpVertexBuffer(_vertexBuffer),
mpFrameBuffer(_frameBuffer),
mpShaderProgram(_shaderProgram),
mAttributeMappings(),
mFragmentDataMappings(),
mpBuffers(NULL)
{
validate();
}
virtual ~State(void)
{
if(mpBuffers != NULL)
delete[](mpBuffers);
}
// ==================================================================================================== \/
// ============================================================================================ GETTERS \/
// ==================================================================================================== \/
public:
inline ConstSharedVertexBuffer getVertexBuffer (void) const { return mpVertexBuffer; }
inline ConstSharedFrameBuffer getFrameBuffer (void) const { return mpFrameBuffer; }
inline ConstSharedShaderProgram getShaderProgram (void) const { return mpShaderProgram; }
// ==================================================================================================== \/
// ============================================================================================ METHODS \/
// ==================================================================================================== \/
public:
void validate (void);
void render (void);
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
protected:
ConstSharedVertexBuffer mpVertexBuffer;
ConstSharedFrameBuffer mpFrameBuffer;
ConstSharedShaderProgram mpShaderProgram;
AttributeMappingVec mAttributeMappings;
FragmentDataMappingVec mFragmentDataMappings;
GLuint* mpBuffers;
};
ACGL_SHARED_TYPEDEF(State)
} // OpenGL
} // ACGL
#endif // ACGL_OPENGL_OBJECTS_STATE_HH
...@@ -73,6 +73,13 @@ public: ...@@ -73,6 +73,13 @@ public:
public: public:
int_t getAttributeIndexByName(const std::string& _name) const; int_t getAttributeIndexByName(const std::string& _name) const;
inline void setAttributePointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const
{
mArrayBuffers[mAttributes[_indexInArray].bufferID]->setAttributePointer(
mAttributes[_indexInArray].attributeID,
_indexInShader);
}
inline void setElements(GLsizei _elements) inline void setElements(GLsizei _elements)
{ {
mElements = _elements; mElements = _elements;
...@@ -116,13 +123,22 @@ public: ...@@ -116,13 +123,22 @@ public:
public: public:
void render(void) const; void render(void) const;
void drawElements(void) const
{
mpElementArrayBuffer->draw();
}
void drawArrays(void) const
{
glDrawArrays(mMode, 0, mElements);
}
void draw(void) const void draw(void) const
{ {
if(mpElementArrayBuffer) if(mpElementArrayBuffer)
mpElementArrayBuffer->draw(); drawElements();
else else
glDrawArrays(mMode, 0, mElements); drawArrays();
openGLRareError(); openGLRareError();
} }
......
...@@ -21,7 +21,7 @@ void ArrayBuffer::render(void) const ...@@ -21,7 +21,7 @@ void ArrayBuffer::render(void) const
glBindBuffer(GL_ARRAY_BUFFER, mContext); glBindBuffer(GL_ARRAY_BUFFER, mContext);
for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i)
{ {
setPointer(i, i); setAttributePointer(i, i);
glEnableVertexAttribArray(i); glEnableVertexAttribArray(i);
openGLRareError(); openGLRareError();
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <ACGL/OpenGL/Objects/FrameBuffer.hh> #include <ACGL/OpenGL/Objects/FrameBuffer.hh>
using namespace ACGL;
using namespace ACGL::OpenGL; using namespace ACGL::OpenGL;
/* /*
...@@ -22,3 +23,13 @@ using namespace ACGL::OpenGL; ...@@ -22,3 +23,13 @@ using namespace ACGL::OpenGL;
GL_COLOR_ATTACHMENT0+7}; GL_COLOR_ATTACHMENT0+7};
int_t FrameBuffer::getColorAttachmentIndexByName(const std::string& _name) const
{
for(AttachmentVec::size_type i = 0; i < mColorAttachments.size(); i++)
{
if(mColorAttachments[i].name == _name)
return i;
}
return -1;
}
...@@ -50,7 +50,7 @@ bool Shader::setSource(const std::string& _source) ...@@ -50,7 +50,7 @@ bool Shader::setSource(const std::string& _source)
while (stream.good()) while (stream.good())
{ {
std::getline(stream, line, ' '); std::getline(stream, line);
if(this->mType == GL_VERTEX_SHADER) if(this->mType == GL_VERTEX_SHADER)
{ {
......
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#include <ACGL/OpenGL/Objects/State.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/Base/StringOperations.hh>
#include <iostream>
#include <fstream>
using namespace ACGL::Base;
using namespace ACGL::Utils;
using namespace ACGL::OpenGL;
void State::validate(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.idInShaderProgram = mpShaderProgram->getAttributeLocation(attributeVec[k]);
mapping.idInVertexBuffer = mpVertexBuffer->getAttributeIndexByName(attributeVec[k]);
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.idInShaderProgram = mpShaderProgram->getFragmentDataLocation(fragmentDataVec[k]);
mapping.idInFrameBuffer = mpFrameBuffer->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].idInFrameBuffer;
}
void State::render(void)
{
//Setting the draw buffers of the framebuffer
mpFrameBuffer->bind();
glDrawBuffers(mFragmentDataMappings.size(), mpBuffers);
if(mpVertexBuffer->getElementArrayBuffer())
mpVertexBuffer->getElementArrayBuffer()->bind();
for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
{
mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].idInVertexBuffer]->bind();
mpVertexBuffer->setAttributePointer(mAttributeMappings[k].idInVertexBuffer, mAttributeMappings[k].idInShaderProgram);
glEnableVertexAttribArray(mAttributeMappings[k].idInShaderProgram);
openGLRareError();
}
mpVertexBuffer->draw();
for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
{
mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].idInVertexBuffer]->bind();
glDisableVertexAttribArray(mAttributeMappings[k].idInShaderProgram);
openGLRareError();
}
}
...@@ -29,7 +29,7 @@ void VertexBuffer::render(void) const ...@@ -29,7 +29,7 @@ void VertexBuffer::render(void) const
currentBufferID = mAttributes[i].bufferID; currentBufferID = mAttributes[i].bufferID;
mArrayBuffers[currentBufferID]->bind(); mArrayBuffers[currentBufferID]->bind();
} }
mArrayBuffers[currentBufferID]->setPointer(mAttributes[i].attributeID, i); mArrayBuffers[currentBufferID]->setAttributePointer(mAttributes[i].attributeID, i);
glEnableVertexAttribArray(i); glEnableVertexAttribArray(i);
openGLRareError(); openGLRareError();
} }
......
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