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:\
#define ACGL_SHARED_TYPEDEF(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
......@@ -15,7 +15,7 @@
namespace ACGL{
namespace OpenGL{
class StateeControl : public Resource::BasicCreateController<Texture>
class StateControl : public Resource::BasicCreateController<Texture>
{
// ========================================================================================================= \/
// ============================================================================================ CONSTRUCTORS \/
......@@ -24,17 +24,19 @@ public:
StateControl(void)
: mpVertexBuffer(NULL),
mpFrameBuffer(NULL),
mpShaderProgram(NULL)
mpShaderProgram(NULL),
mpViewport(NULL)
{}
virtual ~StateControl(void) {}
// ==================================================================================================== \/
// ============================================================================================ METHODS \/
// ============================================================================================ GETTERS \/
// ==================================================================================================== \/
public:
inline StateControl& vertexBuffer (ConstSharedVertexBuffer _vertexBuffer) { mpVertexBuffer = _vertexBuffer; return *this; }
inline StateControl& frameBuffer (ConstSharedFrameBuffer _frameBuffer) { mpFrameBuffer = _frameBuffer; return *this; }
inline StateControl& shaderProgram (ConstSharedShaderProgram _shaderProgram) { mpShaderProgram = _shaderProgram; return *this; }
inline StateControl& vertexBuffer (const ConstSharedVertexBuffer& _vertexBuffer) { mpVertexBuffer = _vertexBuffer; return *this; }
inline StateControl& frameBuffer (const ConstSharedFrameBuffer& _frameBuffer) { mpFrameBuffer = _frameBuffer; return *this; }
inline StateControl& shaderProgram (const ConstSharedShaderProgram& _shaderProgram) { mpShaderProgram = _shaderProgram; return *this; }
inline StateControl& viewport (const ConstSharedShaderProgram& _viewport) { mpViewport = _viewport; return *this; }
// ===================================================================================================== \/
// ============================================================================================ OVERRIDE \/
......@@ -42,7 +44,8 @@ public:
public:
virtual SharedState create(void)
{
SharedState state(new State(mpVertexBuffer, mpFrameBuffer, mpShaderProgram));
SharedState state(new State(mpVertexBuffer, mpFrameBuffer, mpShaderProgram, mpViewport));
return state;
}
......@@ -53,6 +56,7 @@ protected:
ConstSharedVertexBuffer mpVertexBuffer;
ConstSharedFrameBuffer mpFrameBuffer;
ConstSharedShaderProgram mpShaderProgram;
ConstSharedViewport mpViewport;
};
} // OpenGL
......
......@@ -71,8 +71,6 @@ public:
public:
FrameBuffer(GLsizei _width, GLsizei _height)
: mContext(0),
mWidth(_width),
mHeight(_height),
mDrawBuffers(0),
mColorAttachments(),
mDepthAttachment()
......@@ -98,8 +96,6 @@ public:
// ==================================================================================================== \/
public:
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 Attachment& getDepthAttachment (void) const { return mDepthAttachment; }
......@@ -109,6 +105,8 @@ public:
public:
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
* bind two different FrameBuffers for these operations!
......@@ -123,7 +121,6 @@ public:
{
glBindFramebuffer(_type, mContext);
glDrawBuffers(mDrawBuffers, msBuffers);
glViewport(0, 0, mWidth, mHeight);
openGLRareError();
}
......@@ -131,7 +128,6 @@ public:
{
glBindFramebuffer(_type, mContext);
glDrawBuffers(mDrawBuffers, msBuffers);
glViewport(0, 0, mWidth, mHeight);
openGLRareError();
}
......@@ -147,11 +143,6 @@ public:
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());
if (openGLCommonErrorOccured()) {
Utils::error() << "Attaching of texture to the FBO failed" << std::endl;
......@@ -165,11 +156,6 @@ public:
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);
if (openGLCommonErrorOccured()) {
Utils::error() << "Attaching of texture to the FBO failed" << std::endl;
......@@ -183,11 +169,6 @@ public:
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());
#ifdef OPENGL_ES
if( _renderBuffer->getInternalFormat() == GL_DEPTH24_STENCIL8_OES ||
......@@ -204,11 +185,6 @@ public:
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);
#ifdef OPENGL_ES
if( _texture->getInternalFormat() == GL_DEPTH24_STENCIL8_OES ||
......@@ -228,8 +204,6 @@ public:
// =================================================================================================== \/
protected:
GLuint mContext;
GLsizei mWidth;
GLsizei mHeight;
GLsizei mDrawBuffers;
AttachmentVec mColorAttachments;
Attachment mDepthAttachment; // depth and stencil are combined
......
......@@ -19,6 +19,7 @@
#include <ACGL/OpenGL/Objects/VertexBuffer.hh>
#include <ACGL/OpenGL/Objects/FrameBuffer.hh>
#include <ACGL/OpenGL/Objects/ShaderProgram.hh>
#include <ACGL/OpenGL/Objects/Viewport.hh>
namespace ACGL{
namespace OpenGL{
......@@ -55,15 +56,17 @@ private:
public:
State(ConstSharedVertexBuffer _vertexBuffer,
ConstSharedFrameBuffer _frameBuffer,
ConstSharedShaderProgram _shaderProgram)
ConstSharedShaderProgram _shaderProgram,
ConstSharedViewport _viewport)
: mpVertexBuffer(_vertexBuffer),
mpFrameBuffer(_frameBuffer),
mpShaderProgram(_shaderProgram),
mpViewport(_viewport),
mAttributeMappings(),
mFragmentDataMappings(),
mpBuffers(NULL)
{
update();
updateMappings();
}
virtual ~State(void)
......@@ -79,15 +82,23 @@ public:
inline ConstSharedVertexBuffer getVertexBuffer (void) const { return mpVertexBuffer; }
inline ConstSharedFrameBuffer getFrameBuffer (void) const { return mpFrameBuffer; }
inline ConstSharedShaderProgram getShaderProgram (void) const { return mpShaderProgram; }
inline ConstSharedViewport getViewport (void) const { return mpViewport; }
// ==================================================================================================== \/
// ============================================================================================ METHODS \/
// ==================================================================================================== \/
public:
void enableVertexBuffer();
void enableFrameBuffer();
void disableVertexBuffer();
void update (void);
inline void validate (void) const
{
mpFrameBuffer->validate();
mpVertexBuffer->validate();
}
void enableViewport (void);
void enableVertexBuffer (void);
void enableFrameBuffer (void);
void disableVertexBuffer (void);
void updateMappings (void);
void render (void);
// =================================================================================================== \/
......@@ -97,6 +108,7 @@ protected:
ConstSharedVertexBuffer mpVertexBuffer;
ConstSharedFrameBuffer mpFrameBuffer;
ConstSharedShaderProgram mpShaderProgram;
ConstSharedViewport mpViewport;
AttributeMappingVec mAttributeMappings;
FragmentDataMappingVec mFragmentDataMappings;
......
......@@ -45,12 +45,8 @@ public:
// ============================================================================================ CONSTRUCTORS \/
// ========================================================================================================= \/
public:
VertexBuffer(
GLenum _mode,
GLsizei _elements)
: mMode(_mode),
mElements(_elements),
mpElementArrayBuffer(),
VertexBuffer(void)
: mpElementArrayBuffer(),
mArrayBuffers(),
mAttributes()
{}
......@@ -61,8 +57,6 @@ public:
// ============================================================================================ GETTERS \/
// ==================================================================================================== \/
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 ArrayBufferVec& getArrayBuffers (void) const { return mArrayBuffers; }
inline const AttributeVec& getAttributes (void) const { return mAttributes; }
......@@ -85,10 +79,7 @@ public:
_indexInShader);
}
inline void setElements(GLsizei _elements)
{
mElements = _elements;
}
void validate( void ) const;
inline void setElementArrayBuffer(const SharedElementArrayBuffer& _elementArrayBuffer)
{
......@@ -135,7 +126,9 @@ public:
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
......@@ -151,8 +144,6 @@ public:
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
protected:
GLenum mMode;
GLsizei mElements;
SharedElementArrayBuffer mpElementArrayBuffer;
ArrayBufferVec mArrayBuffers;
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)
elements = mArrayBuffers[0]->getElements();
}
SharedVertexBuffer vertexBuffer(new VertexBuffer(
mode,
elements));
SharedVertexBuffer vertexBuffer(new VertexBuffer());
vertexBuffer->setElementArrayBuffer(mpElementArrayBuffer);
......
......@@ -314,7 +314,7 @@ bool VertexBufferControlFileOBJ::loadOBJ(SharedVertexBuffer& _vertexBuffer)
arrayBuffer->bind();
arrayBuffer->setData(&interleavedDataVector[0], index);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
_vertexBuffer->setElements(indexVector.size());
//_vertexBuffer->setElements(indexVector.size());
return true;
}
......
......@@ -33,3 +33,38 @@ int_t FrameBuffer::getColorAttachmentIndexByName(const std::string& _name) const
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;
using namespace ACGL::Utils;
using namespace ACGL::OpenGL;
void State::enableViewport()
{
glViewport(0, 0, mpViewport->getWidth(), mpViewport->getHeight());
}
void State::enableFrameBuffer()
{
mpFrameBuffer->bind();
......@@ -56,7 +61,7 @@ void State::render(void)
mpVertexBuffer->draw();
}
void State::update (void)
void State::updateMappings (void)
{
ShaderProgram::ConstSharedShaderVec shaders = mpShaderProgram->getShaders();
......
......@@ -16,6 +16,37 @@ int_t VertexBuffer::getAttributeIndexByName(const std::string& _name) const
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
{
if(mpElementArrayBuffer)
......@@ -47,10 +78,4 @@ void VertexBuffer::render(void) const
glDisableVertexAttribArray(i);
openGLRareError();
}
/*
if(mpElementArrayBuffer)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
*/
}
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