Commit 557b6d6d authored by Andreas Neu's avatar Andreas Neu

Added Viewport Class to ACGL, which is now used as a member of State.

Validation methods have been added VertexBuffer::validate() and FrameBuffer::validate().
These are called by State::validate()
parent ca9267e7
...@@ -14,7 +14,9 @@ private:\ ...@@ -14,7 +14,9 @@ private:\
#define ACGL_SHARED_TYPEDEF(Class) \ #define ACGL_SHARED_TYPEDEF(Class) \
typedef std::tr1::shared_ptr<Class> Shared ## Class; \ typedef std::tr1::shared_ptr<Class> Shared ## Class; \
typedef std::tr1::shared_ptr<const Class> ConstShared ## Class; typedef std::tr1::shared_ptr<const Class> ConstShared ## Class; \
typedef std::tr1::weak_ptr<Class> Weak ## Class; \
typedef std::tr1::weak_ptr<const Class> ConstWeak ## Class;
#endif // MACROS_HH #endif // MACROS_HH
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
namespace ACGL{ namespace ACGL{
namespace OpenGL{ namespace OpenGL{
class StateeControl : public Resource::BasicCreateController<Texture> class StateControl : public Resource::BasicCreateController<Texture>
{ {
// ========================================================================================================= \/ // ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/ // ============================================================================================ CONSTRUCTORS \/
...@@ -24,17 +24,19 @@ public: ...@@ -24,17 +24,19 @@ public:
StateControl(void) StateControl(void)
: mpVertexBuffer(NULL), : mpVertexBuffer(NULL),
mpFrameBuffer(NULL), mpFrameBuffer(NULL),
mpShaderProgram(NULL) mpShaderProgram(NULL),
mpViewport(NULL)
{} {}
virtual ~StateControl(void) {} virtual ~StateControl(void) {}
// ==================================================================================================== \/ // ==================================================================================================== \/
// ============================================================================================ METHODS \/ // ============================================================================================ GETTERS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
inline StateControl& vertexBuffer (ConstSharedVertexBuffer _vertexBuffer) { mpVertexBuffer = _vertexBuffer; return *this; } inline StateControl& vertexBuffer (const ConstSharedVertexBuffer& _vertexBuffer) { mpVertexBuffer = _vertexBuffer; return *this; }
inline StateControl& frameBuffer (ConstSharedFrameBuffer _frameBuffer) { mpFrameBuffer = _frameBuffer; return *this; } inline StateControl& frameBuffer (const ConstSharedFrameBuffer& _frameBuffer) { mpFrameBuffer = _frameBuffer; return *this; }
inline StateControl& shaderProgram (ConstSharedShaderProgram _shaderProgram) { mpShaderProgram = _shaderProgram; return *this; } inline StateControl& shaderProgram (const ConstSharedShaderProgram& _shaderProgram) { mpShaderProgram = _shaderProgram; return *this; }
inline StateControl& viewport (const ConstSharedShaderProgram& _viewport) { mpViewport = _viewport; return *this; }
// ===================================================================================================== \/ // ===================================================================================================== \/
// ============================================================================================ OVERRIDE \/ // ============================================================================================ OVERRIDE \/
...@@ -42,7 +44,8 @@ public: ...@@ -42,7 +44,8 @@ public:
public: public:
virtual SharedState create(void) virtual SharedState create(void)
{ {
SharedState state(new State(mpVertexBuffer, mpFrameBuffer, mpShaderProgram)); SharedState state(new State(mpVertexBuffer, mpFrameBuffer, mpShaderProgram, mpViewport));
return state; return state;
} }
...@@ -53,6 +56,7 @@ protected: ...@@ -53,6 +56,7 @@ protected:
ConstSharedVertexBuffer mpVertexBuffer; ConstSharedVertexBuffer mpVertexBuffer;
ConstSharedFrameBuffer mpFrameBuffer; ConstSharedFrameBuffer mpFrameBuffer;
ConstSharedShaderProgram mpShaderProgram; ConstSharedShaderProgram mpShaderProgram;
ConstSharedViewport mpViewport;
}; };
} // OpenGL } // OpenGL
......
...@@ -71,8 +71,6 @@ public: ...@@ -71,8 +71,6 @@ public:
public: public:
FrameBuffer(GLsizei _width, GLsizei _height) FrameBuffer(GLsizei _width, GLsizei _height)
: mContext(0), : mContext(0),
mWidth(_width),
mHeight(_height),
mDrawBuffers(0), mDrawBuffers(0),
mColorAttachments(), mColorAttachments(),
mDepthAttachment() mDepthAttachment()
...@@ -98,8 +96,6 @@ public: ...@@ -98,8 +96,6 @@ public:
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
inline GLuint getContext (void) const { return mContext; } inline GLuint getContext (void) const { return mContext; }
inline GLsizei getWidth (void) const { return mWidth; }
inline GLsizei getHeight (void) const { return mHeight; }
inline const AttachmentVec& getColorAttachments (void) const { return mColorAttachments; } inline const AttachmentVec& getColorAttachments (void) const { return mColorAttachments; }
inline const Attachment& getDepthAttachment (void) const { return mDepthAttachment; } inline const Attachment& getDepthAttachment (void) const { return mDepthAttachment; }
...@@ -109,6 +105,8 @@ public: ...@@ -109,6 +105,8 @@ public:
public: public:
int_t getColorAttachmentIndexByName(const std::string& _name) const; int_t getColorAttachmentIndexByName(const std::string& _name) const;
void validate( void ) 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!
...@@ -123,7 +121,6 @@ public: ...@@ -123,7 +121,6 @@ public:
{ {
glBindFramebuffer(_type, mContext); glBindFramebuffer(_type, mContext);
glDrawBuffers(mDrawBuffers, msBuffers); glDrawBuffers(mDrawBuffers, msBuffers);
glViewport(0, 0, mWidth, mHeight);
openGLRareError(); openGLRareError();
} }
...@@ -131,7 +128,6 @@ public: ...@@ -131,7 +128,6 @@ public:
{ {
glBindFramebuffer(_type, mContext); glBindFramebuffer(_type, mContext);
glDrawBuffers(mDrawBuffers, msBuffers); glDrawBuffers(mDrawBuffers, msBuffers);
glViewport(0, 0, mWidth, mHeight);
openGLRareError(); openGLRareError();
} }
...@@ -147,11 +143,6 @@ public: ...@@ -147,11 +143,6 @@ public:
inline bool attachColorRenderBuffer(const std::string _name, const SharedRenderBuffer& _renderBuffer) inline bool attachColorRenderBuffer(const std::string _name, const SharedRenderBuffer& _renderBuffer)
{ {
if(_renderBuffer->getWidth() != mWidth && _renderBuffer->getHeight() != mHeight)
{
Utils::error() << "Attached render buffer for color has a wrong size!" << std::endl;
return false;
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + mDrawBuffers, GL_RENDERBUFFER, _renderBuffer->getContext()); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + mDrawBuffers, GL_RENDERBUFFER, _renderBuffer->getContext());
if (openGLCommonErrorOccured()) { if (openGLCommonErrorOccured()) {
Utils::error() << "Attaching of texture to the FBO failed" << std::endl; Utils::error() << "Attaching of texture to the FBO failed" << std::endl;
...@@ -165,11 +156,6 @@ public: ...@@ -165,11 +156,6 @@ public:
inline bool attachColorTexture(const std::string _name, const SharedTexture& _texture) inline bool attachColorTexture(const std::string _name, const SharedTexture& _texture)
{ {
if(_texture->getWidth() != mWidth && _texture->getHeight() != mHeight)
{
Utils::error() << "Attached texture for color has a wrong size!" << std::endl;
return false;
}
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + mDrawBuffers, _texture->getTarget(), _texture->getContext(), 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + mDrawBuffers, _texture->getTarget(), _texture->getContext(), 0);
if (openGLCommonErrorOccured()) { if (openGLCommonErrorOccured()) {
Utils::error() << "Attaching of texture to the FBO failed" << std::endl; Utils::error() << "Attaching of texture to the FBO failed" << std::endl;
...@@ -183,11 +169,6 @@ public: ...@@ -183,11 +169,6 @@ public:
inline bool setDepthRenderBuffer(const SharedRenderBuffer& _renderBuffer) inline bool setDepthRenderBuffer(const SharedRenderBuffer& _renderBuffer)
{ {
if(_renderBuffer->getWidth() != mWidth && _renderBuffer->getHeight() != mHeight)
{
Utils::error() << "Attached render buffer for depth has a wrong size!" << std::endl;
return false;
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext()); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext());
#ifdef OPENGL_ES #ifdef OPENGL_ES
if( _renderBuffer->getInternalFormat() == GL_DEPTH24_STENCIL8_OES || if( _renderBuffer->getInternalFormat() == GL_DEPTH24_STENCIL8_OES ||
...@@ -204,11 +185,6 @@ public: ...@@ -204,11 +185,6 @@ public:
inline bool setDepthTexture(const SharedTexture& _texture) inline bool setDepthTexture(const SharedTexture& _texture)
{ {
if(_texture->getWidth() != mWidth && _texture->getHeight() != mHeight)
{
Utils::error() << "Attached texture for depth has a wrong size!" << std::endl;
return false;
}
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0);
#ifdef OPENGL_ES #ifdef OPENGL_ES
if( _texture->getInternalFormat() == GL_DEPTH24_STENCIL8_OES || if( _texture->getInternalFormat() == GL_DEPTH24_STENCIL8_OES ||
...@@ -228,8 +204,6 @@ public: ...@@ -228,8 +204,6 @@ public:
// =================================================================================================== \/ // =================================================================================================== \/
protected: protected:
GLuint mContext; GLuint mContext;
GLsizei mWidth;
GLsizei mHeight;
GLsizei mDrawBuffers; GLsizei mDrawBuffers;
AttachmentVec mColorAttachments; AttachmentVec mColorAttachments;
Attachment mDepthAttachment; // depth and stencil are combined Attachment mDepthAttachment; // depth and stencil are combined
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <ACGL/OpenGL/Objects/VertexBuffer.hh> #include <ACGL/OpenGL/Objects/VertexBuffer.hh>
#include <ACGL/OpenGL/Objects/FrameBuffer.hh> #include <ACGL/OpenGL/Objects/FrameBuffer.hh>
#include <ACGL/OpenGL/Objects/ShaderProgram.hh> #include <ACGL/OpenGL/Objects/ShaderProgram.hh>
#include <ACGL/OpenGL/Objects/Viewport.hh>
namespace ACGL{ namespace ACGL{
namespace OpenGL{ namespace OpenGL{
...@@ -55,15 +56,17 @@ private: ...@@ -55,15 +56,17 @@ private:
public: public:
State(ConstSharedVertexBuffer _vertexBuffer, State(ConstSharedVertexBuffer _vertexBuffer,
ConstSharedFrameBuffer _frameBuffer, ConstSharedFrameBuffer _frameBuffer,
ConstSharedShaderProgram _shaderProgram) ConstSharedShaderProgram _shaderProgram,
ConstSharedViewport _viewport)
: mpVertexBuffer(_vertexBuffer), : mpVertexBuffer(_vertexBuffer),
mpFrameBuffer(_frameBuffer), mpFrameBuffer(_frameBuffer),
mpShaderProgram(_shaderProgram), mpShaderProgram(_shaderProgram),
mpViewport(_viewport),
mAttributeMappings(), mAttributeMappings(),
mFragmentDataMappings(), mFragmentDataMappings(),
mpBuffers(NULL) mpBuffers(NULL)
{ {
update(); updateMappings();
} }
virtual ~State(void) virtual ~State(void)
...@@ -79,16 +82,24 @@ public: ...@@ -79,16 +82,24 @@ public:
inline ConstSharedVertexBuffer getVertexBuffer (void) const { return mpVertexBuffer; } inline ConstSharedVertexBuffer getVertexBuffer (void) const { return mpVertexBuffer; }
inline ConstSharedFrameBuffer getFrameBuffer (void) const { return mpFrameBuffer; } inline ConstSharedFrameBuffer getFrameBuffer (void) const { return mpFrameBuffer; }
inline ConstSharedShaderProgram getShaderProgram (void) const { return mpShaderProgram; } inline ConstSharedShaderProgram getShaderProgram (void) const { return mpShaderProgram; }
inline ConstSharedViewport getViewport (void) const { return mpViewport; }
// ==================================================================================================== \/ // ==================================================================================================== \/
// ============================================================================================ METHODS \/ // ============================================================================================ METHODS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
void enableVertexBuffer(); inline void validate (void) const
void enableFrameBuffer(); {
void disableVertexBuffer(); mpFrameBuffer->validate();
void update (void); mpVertexBuffer->validate();
void render (void); }
void enableViewport (void);
void enableVertexBuffer (void);
void enableFrameBuffer (void);
void disableVertexBuffer (void);
void updateMappings (void);
void render (void);
// =================================================================================================== \/ // =================================================================================================== \/
// ============================================================================================ FIELDS \/ // ============================================================================================ FIELDS \/
...@@ -97,6 +108,7 @@ protected: ...@@ -97,6 +108,7 @@ protected:
ConstSharedVertexBuffer mpVertexBuffer; ConstSharedVertexBuffer mpVertexBuffer;
ConstSharedFrameBuffer mpFrameBuffer; ConstSharedFrameBuffer mpFrameBuffer;
ConstSharedShaderProgram mpShaderProgram; ConstSharedShaderProgram mpShaderProgram;
ConstSharedViewport mpViewport;
AttributeMappingVec mAttributeMappings; AttributeMappingVec mAttributeMappings;
FragmentDataMappingVec mFragmentDataMappings; FragmentDataMappingVec mFragmentDataMappings;
......
...@@ -45,12 +45,8 @@ public: ...@@ -45,12 +45,8 @@ public:
// ============================================================================================ CONSTRUCTORS \/ // ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/ // ========================================================================================================= \/
public: public:
VertexBuffer( VertexBuffer(void)
GLenum _mode, : mpElementArrayBuffer(),
GLsizei _elements)
: mMode(_mode),
mElements(_elements),
mpElementArrayBuffer(),
mArrayBuffers(), mArrayBuffers(),
mAttributes() mAttributes()
{} {}
...@@ -61,8 +57,6 @@ public: ...@@ -61,8 +57,6 @@ public:
// ============================================================================================ GETTERS \/ // ============================================================================================ GETTERS \/
// ==================================================================================================== \/ // ==================================================================================================== \/
public: public:
inline GLenum getMode (void) const { return mMode; }
inline GLsizei getElements (void) const { return mElements; }
inline const SharedElementArrayBuffer& getElementArrayBuffer (void) const { return mpElementArrayBuffer; } inline const SharedElementArrayBuffer& getElementArrayBuffer (void) const { return mpElementArrayBuffer; }
inline const ArrayBufferVec& getArrayBuffers (void) const { return mArrayBuffers; } inline const ArrayBufferVec& getArrayBuffers (void) const { return mArrayBuffers; }
inline const AttributeVec& getAttributes (void) const { return mAttributes; } inline const AttributeVec& getAttributes (void) const { return mAttributes; }
...@@ -85,10 +79,7 @@ public: ...@@ -85,10 +79,7 @@ public:
_indexInShader); _indexInShader);
} }
inline void setElements(GLsizei _elements) void validate( void ) const;
{
mElements = _elements;
}
inline void setElementArrayBuffer(const SharedElementArrayBuffer& _elementArrayBuffer) inline void setElementArrayBuffer(const SharedElementArrayBuffer& _elementArrayBuffer)
{ {
...@@ -135,7 +126,9 @@ public: ...@@ -135,7 +126,9 @@ public:
void drawArrays(void) const void drawArrays(void) const
{ {
glDrawArrays(mMode, 0, mElements); //If no ElementArrayBuffer is specified we use the convention that
//the first ArrayBuffers determines the mode and the number of elements.
mArrayBuffers[0]->draw();
} }
void draw(void) const void draw(void) const
...@@ -151,8 +144,6 @@ public: ...@@ -151,8 +144,6 @@ public:
// ============================================================================================ FIELDS \/ // ============================================================================================ FIELDS \/
// =================================================================================================== \/ // =================================================================================================== \/
protected: protected:
GLenum mMode;
GLsizei mElements;
SharedElementArrayBuffer mpElementArrayBuffer; SharedElementArrayBuffer mpElementArrayBuffer;
ArrayBufferVec mArrayBuffers; ArrayBufferVec mArrayBuffers;
AttributeVec mAttributes; AttributeVec mAttributes;
......
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#ifndef ACGL_OPENGL_OBJECTS_VIEWPORT_HH
#define ACGL_OPENGL_OBJECTS_VIEWPORT_HH
#include <vector>
#include <string>
#include <tr1/memory>
#include <iostream>
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Macros.hh>
#include <ACGL/OpenGL/GL.hh>
namespace ACGL{
namespace OpenGL{
class Viewport
{
ACGL_NOT_COPYABLE(Viewport)
// ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
Viewport(int _width,
int _height)
: mWidth(_width),
mHeight(_height)
{ }
virtual ~Viewport(void) { }
// ==================================================================================================== \/
// ============================================================================================ GETTERS \/
// ==================================================================================================== \/
public:
inline int getWidth (void) const { return mWidth; }
inline int getHeight (void) const { return mHeight; }
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
protected:
int mWidth;
int mHeight;
};
ACGL_SHARED_TYPEDEF(Viewport)
} // OpenGL
} // ACGL
#endif // ACGL_OPENGL_OBJECTS_STATE_HH
...@@ -22,9 +22,7 @@ SharedVertexBuffer VertexBufferControl::create(void) ...@@ -22,9 +22,7 @@ SharedVertexBuffer VertexBufferControl::create(void)
elements = mArrayBuffers[0]->getElements(); elements = mArrayBuffers[0]->getElements();
} }
SharedVertexBuffer vertexBuffer(new VertexBuffer( SharedVertexBuffer vertexBuffer(new VertexBuffer());
mode,
elements));
vertexBuffer->setElementArrayBuffer(mpElementArrayBuffer); vertexBuffer->setElementArrayBuffer(mpElementArrayBuffer);
......
...@@ -314,7 +314,7 @@ bool VertexBufferControlFileOBJ::loadOBJ(SharedVertexBuffer& _vertexBuffer) ...@@ -314,7 +314,7 @@ bool VertexBufferControlFileOBJ::loadOBJ(SharedVertexBuffer& _vertexBuffer)
arrayBuffer->bind(); arrayBuffer->bind();
arrayBuffer->setData(&interleavedDataVector[0], index); arrayBuffer->setData(&interleavedDataVector[0], index);
//glBindBuffer(GL_ARRAY_BUFFER, 0); //glBindBuffer(GL_ARRAY_BUFFER, 0);
_vertexBuffer->setElements(indexVector.size()); //_vertexBuffer->setElements(indexVector.size());
return true; return true;
} }
......
...@@ -33,3 +33,38 @@ int_t FrameBuffer::getColorAttachmentIndexByName(const std::string& _name) const ...@@ -33,3 +33,38 @@ int_t FrameBuffer::getColorAttachmentIndexByName(const std::string& _name) const
return -1; return -1;
} }
void FrameBuffer::validate(void) const
{
if(mColorAttachments.size() > 0)
{
int width = -1;
int height = -1;
if(mColorAttachments[0].texture)
{
width = mColorAttachments[0].texture->getWidth();
height = mColorAttachments[0].texture->getHeight();
}
else
{
width = mColorAttachments[0].renderBuffer->getWidth();
height = mColorAttachments[0].renderBuffer->getHeight();
}
for(AttachmentVec::size_type k = 0; k < mColorAttachments.size(); k++)
{
bool fail = false;
if(mColorAttachments[k].texture)
fail = (mColorAttachments[k].texture->getWidth() != width || mColorAttachments[k].texture->getHeight() != height);
else //otherwise its a RenderBuffer
fail = (mColorAttachments[k].renderBuffer->getWidth() != width || mColorAttachments[k].renderBuffer->getHeight() != height);
if(fail)
Utils::error() << "FrameBuffer validation failed: Color attachment "<< k << " has different size." << std::endl;
}
}
else
Utils::error() << "FrameBuffer validation failed: No color attachments."<< std::endl;
}
...@@ -14,6 +14,11 @@ using namespace ACGL::Base; ...@@ -14,6 +14,11 @@ using namespace ACGL::Base;
using namespace ACGL::Utils; using namespace ACGL::Utils;
using namespace ACGL::OpenGL; using namespace ACGL::OpenGL;
void State::enableViewport()
{
glViewport(0, 0, mpViewport->getWidth(), mpViewport->getHeight());
}
void State::enableFrameBuffer() void State::enableFrameBuffer()
{ {
mpFrameBuffer->bind(); mpFrameBuffer->bind();
...@@ -56,7 +61,7 @@ void State::render(void) ...@@ -56,7 +61,7 @@ void State::render(void)
mpVertexBuffer->draw(); mpVertexBuffer->draw();
} }
void State::update (void) void State::updateMappings (void)
{ {
ShaderProgram::ConstSharedShaderVec shaders = mpShaderProgram->getShaders(); ShaderProgram::ConstSharedShaderVec shaders = mpShaderProgram->getShaders();
......
...@@ -16,6 +16,37 @@ int_t VertexBuffer::getAttributeIndexByName(const std::string& _name) const ...@@ -16,6 +16,37 @@ int_t VertexBuffer::getAttributeIndexByName(const std::string& _name) const
return -1; return -1;
} }
void VertexBuffer::validate(void) const
{
int mode = -1;
int elements = -1;
if(mpElementArrayBuffer)
{
mode = mpElementArrayBuffer->getMode();
elements = mpElementArrayBuffer->getElements();
}
else if(mArrayBuffers.size() > 0)
{
mode = mArrayBuffers[0]->getMode();
elements = mArrayBuffers[0]->getElements();
}
else
{
Utils::error() << "VertexBuffer validation failed: No ArrayBuffer or ElementArrayBuffer specified."<< std::endl;
return;
}
for(ArrayBufferVec::size_type k = 0; k < mArrayBuffers.size(); k++)
{
if(mArrayBuffers[k]->getMode() != mode)
Utils::error() << "VertexBuffer validation failed: ArrayBuffer "<< k << " has different mode."<< std::endl;
if(mArrayBuffers[k]->getElements() != elements)
Utils::error() << "VertexBuffer validation failed: ArrayBuffer "<< k << " has different number of elements."<< std::endl;
}
}
void VertexBuffer::render(void) const