diff --git a/CMakeLists.txt b/CMakeLists.txt index 5026b805c902f0c0cb0376643b0833c99240f33f..91947bcae5e8ee5a37d74ae64c98a7730985f99b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,87 +1,87 @@ -CMAKE_MINIMUM_REQUIRED (VERSION 2.6) - -################################################################################ -# Global Settings -################################################################################ - -# Name of the Project -PROJECT(ACGL) -INCLUDE(${CMAKE_SOURCE_DIR}/cmake/CompileOptions.txt) - -# Specify dependencies -SET(NEED_ACGL_GLOBAL_EXTERN_DIR TRUE) -INCLUDE(${CMAKE_SOURCE_DIR}/cmake/GlobalAndLocalExternACGL.txt) - -# OpenGL Support -IF(NOT DEFINED OPENGL_SUPPORT) - IF(NOT DEFINED OPENGL_SUPPORT_CORE_4_0) - SET(OPENGL_SUPPORT_CORE_4_0 TRUE CACHE BOOL "") - ENDIF() - - IF(NOT DEFINED OPENGL_SUPPORT_ES_2_1) - SET(OPENGL_SUPPORT_ES_2_1 TRUE CACHE BOOL "") - ENDIF() - IF(OPENGL_SUPPORT_CORE_4_0) - SET(OPENGL_SUPPORT_LIST ${OPENGL_SUPPORT_LIST} GL_CORE_4_0) - ENDIF() - - IF(OPENGL_SUPPORT_ES_2_1) - SET(OPENGL_SUPPORT_LIST ${OPENGL_SUPPORT_LIST} GL_ES_2_1) - ENDIF() -ELSE() - SET(OPENGL_SUPPORT_LIST ${OPENGL_SUPPORT}) -ENDIF() - -FOREACH(OPENGL_SUPPORT ${OPENGL_SUPPORT_LIST} ) - ################################################################################ - # Basic Configuration - ################################################################################ - - # Name of the Project - PROJECT(AC${OPENGL_SUPPORT}) - - # Where to find the cmake finders? - SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) - - # Target directory - SET(LIBRARY_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib") - - ################################################################################ - # Project Files - ################################################################################ - - # Find all used files of certain file-types - FILE(GLOB_RECURSE SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc") - FILE(GLOB_RECURSE INCLUDE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hh") - - # Add ACGL to the include path - INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/include") - - ################################################################################ - # External Libraries - ################################################################################ - - # Qt - SET(QT_USE_QTOPENGL TRUE) - FIND_PACKAGE(Qt4 REQUIRED) - INCLUDE(${QT_USE_FILE}) - SET(LIBRARIES ${LIBRARIES} ${QT_LIBRARIES}) - - # GLM - FIND_PACKAGE(GLM REQUIRED) - INCLUDE_DIRECTORIES(${GLM_INCLUDE_DIR}) - - # GLEW - FIND_PACKAGE(GLEW REQUIRED) - INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR}) - SET(LIBRARIES ${LIBRARIES} ${GLEW_LIBRARIES}) - - ################################################################################ - # Final Definition - ################################################################################ - - # Define the Project, add all required sources and dependencies to it - ADD_LIBRARY(AC${OPENGL_SUPPORT}${COMPILE_POSTFIX} STATIC ${SOURCE_FILES} ${INCLUDE_FILES}) - TARGET_LINK_LIBRARIES(AC${OPENGL_SUPPORT}${COMPILE_POSTFIX} ${LIBRARIES}) -ENDFOREACH() - +CMAKE_MINIMUM_REQUIRED (VERSION 2.6) + +################################################################################ +# Global Settings +################################################################################ + +# Name of the Project +PROJECT(ACGL) +INCLUDE(${CMAKE_SOURCE_DIR}/cmake/CompileOptions.txt) + +# Specify dependencies +SET(NEED_ACGL_GLOBAL_EXTERN_DIR TRUE) +INCLUDE(${CMAKE_SOURCE_DIR}/cmake/GlobalAndLocalExternACGL.txt) + +# OpenGL Support +IF(NOT DEFINED OPENGL_SUPPORT) + IF(NOT DEFINED OPENGL_SUPPORT_CORE_4_0) + SET(OPENGL_SUPPORT_CORE_4_0 TRUE CACHE BOOL "") + ENDIF() + + IF(NOT DEFINED OPENGL_SUPPORT_ES_2_1) + SET(OPENGL_SUPPORT_ES_2_1 TRUE CACHE BOOL "") + ENDIF() + IF(OPENGL_SUPPORT_CORE_4_0) + SET(OPENGL_SUPPORT_LIST ${OPENGL_SUPPORT_LIST} GL_CORE_4_0) + ENDIF() + + IF(OPENGL_SUPPORT_ES_2_1) + SET(OPENGL_SUPPORT_LIST ${OPENGL_SUPPORT_LIST} GL_ES_2_1) + ENDIF() +ELSE() + SET(OPENGL_SUPPORT_LIST ${OPENGL_SUPPORT}) +ENDIF() + +FOREACH(OPENGL_SUPPORT ${OPENGL_SUPPORT_LIST} ) + ################################################################################ + # Basic Configuration + ################################################################################ + + # Name of the Project + PROJECT(AC${OPENGL_SUPPORT}) + + # Where to find the cmake finders? + SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) + + # Target directory + SET(LIBRARY_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib") + + ################################################################################ + # Project Files + ################################################################################ + + # Find all used files of certain file-types + FILE(GLOB_RECURSE SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc") + FILE(GLOB_RECURSE INCLUDE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hh") + + # Add ACGL to the include path + INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/include") + + ################################################################################ + # External Libraries + ################################################################################ + + # Qt + SET(QT_USE_QTOPENGL TRUE) + FIND_PACKAGE(Qt4 REQUIRED) + INCLUDE(${QT_USE_FILE}) + SET(LIBRARIES ${LIBRARIES} ${QT_LIBRARIES}) + + # GLM + FIND_PACKAGE(GLM REQUIRED) + INCLUDE_DIRECTORIES(${GLM_INCLUDE_DIR}) + + # GLEW + FIND_PACKAGE(GLEW REQUIRED) + INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR}) + SET(LIBRARIES ${LIBRARIES} ${GLEW_LIBRARIES}) + + ################################################################################ + # Final Definition + ################################################################################ + + # Define the Project, add all required sources and dependencies to it + ADD_LIBRARY(AC${OPENGL_SUPPORT}${COMPILE_POSTFIX} STATIC ${SOURCE_FILES} ${INCLUDE_FILES}) + TARGET_LINK_LIBRARIES(AC${OPENGL_SUPPORT}${COMPILE_POSTFIX} ${LIBRARIES}) +ENDFOREACH() + diff --git a/include/ACGL/GLUtils/Tools.hh b/include/ACGL/GLUtils/Tools.hh index a73114d34d6ad5e84d5702ce53939f5f454ed97f..0a8651b9e24860981c7810d2301c2b3f22327d77 100644 --- a/include/ACGL/GLUtils/Tools.hh +++ b/include/ACGL/GLUtils/Tools.hh @@ -3,112 +3,114 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_GLUTILS_TOOLS_HH -#define ACGL_GLUTILS_TOOLS_HH - -#include <ACGL/ACGL.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace GLUtils{ -namespace Tools{ - -inline GLint getGLTypeSize ( GLenum _type ) -{ - switch(_type) - { - case GL_BYTE: return sizeof(GLubyte); - case GL_UNSIGNED_BYTE: return sizeof(GLbyte); - case GL_SHORT: return sizeof(GLshort); - case GL_UNSIGNED_SHORT: return sizeof(GLushort); - case GL_INT: return sizeof(GLint); - case GL_UNSIGNED_INT: return sizeof(GLuint); - case GL_FLOAT: return sizeof(GLfloat); - case GL_DOUBLE: return sizeof(GLdouble); - } - return 0; -} - -// for every OpenGL error enum this will return a human readable version of it -// similar to gluErrorString, but that function is not available on all plattforms -// (read: iOS) -const GLubyte* acglErrorString( GLenum err ); - -/* - * This function can be used outside of the ACGL framework to check always(!) for - * OpenGL errors. It will print the errors and return the error code of the last one. - * Each OpenGL command can only throw one error, errors can only stack up if this - * function or glGetError was not called often enough (and some OpenGL implementations - * will forget old errors if new ones occur). - */ -#define openGLError() openGLError_( __FILE__, __LINE__ ) - -/* - * This function is used internally in ACGL - but not directly. It gets called from all - * other rare/common/critical error checks. The __FILE__ __LINE macros have to be used in - * those to get the correct values from the caller file, if we would use the macro - * above we could get the file/line from this file, which isn't helping. - */ -GLenum openGLError_( const char *_fileName, const unsigned long _lineNumber ); - -/* - * Inside of ACGL we distinguish between rare, common and critical errors. Each kind can be - * switched off which turns the function into nothing after compiler optimization. If an - * error check is turned off it will always behave as if there was no error, even if there - * is one... - * - * Per default a debug build enables critical and common errors, a release build only critical - * ones. - * - * We have two functions for each kind: - * openGL____Error() will print error messages and return the last error (or GL_NO_ERROR) - * openGL____ErrorOccured() will print error messages and retrun true if there was an error - * - * The definition of rare/common/critical is a bit fuzzy: - * - * critical: Errors which can occur even in a bug-free app, like out-of-memory errors. - * These checks are rare, stuff like glCreate calls get checked here. - * Tests for critical errors should not impact the performance of the app measurably. - * - * common: Errors which are quite common while developing. Misuse of the library etc. - * Setting uniforms which don't exist or shader compile errors are candidates for - * common errors. Places where OpenGL resources are calles by a string etc. - * - * rare: Errors which are so uncommon that we don't even check those ina normal debug build. - * Switching these on can impact performance as there can be alot of them. If theres a - * OpenGL error somewhere in the code, switch these checks on to find the correct spot. - * - * OpenGL error checks that are more complicated than just a glGetError call will be wrapped in the - * same defines, this can be used in application code as well. - * - */ -#ifdef ACGL_CHECK_CRITICAL_GL_ERRORS -# define openGLCriticalError() openGLError_( __FILE__, __LINE__ ) -# define openGLCriticalErrorOccured() (openGLError_( __FILE__, __LINE__ ) != GL_NO_ERROR) -#else - inline GLenum openGLCriticalError() { return GL_NO_ERROR; } - inline bool openGLCriticalErrorOccured() { return false; } -#endif - -#ifdef ACGL_CHECK_COMMON_GL_ERRORS -# define openGLCommonError() openGLError_( __FILE__, __LINE__ ) -# define openGLCommonErrorOccured() (openGLError_( __FILE__, __LINE__ ) != GL_NO_ERROR) -#else - inline GLenum openGLCommonError() { return GL_NO_ERROR; } - inline bool openGLCommonErrorOccured() { return false; } -#endif - -#ifdef ACGL_CHECK_RARE_GL_ERRORS -# define openGLRareError() openGLError_( __FILE__, __LINE__ ) -# define openGLRareErrorOccured() (openGLError_( __FILE__, __LINE__ ) != GL_NO_ERROR) -#else - inline GLenum openGLRareError() { return GL_NO_ERROR; } - inline bool openGLRareErrorOccured() { return false; } -#endif - - -} // Tools -} // GLUtils -} // ACGL - -#endif // ACGL_GLUTILS_TOOLS_HH +#ifndef ACGL_GLUTILS_TOOLS_HH +#define ACGL_GLUTILS_TOOLS_HH + +#include <ACGL/ACGL.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace GLUtils{ +namespace Tools{ + +inline GLint getGLTypeSize ( GLenum _type ) +{ + switch(_type) + { + case GL_BYTE: return sizeof(GLubyte); + case GL_UNSIGNED_BYTE: return sizeof(GLbyte); + case GL_SHORT: return sizeof(GLshort); + case GL_UNSIGNED_SHORT: return sizeof(GLushort); + case GL_INT: return sizeof(GLint); + case GL_UNSIGNED_INT: return sizeof(GLuint); + case GL_FLOAT: return sizeof(GLfloat); + case GL_DOUBLE: return sizeof(GLdouble); + } + return 0; +} + +// for every OpenGL error enum this will return a human readable version of it +// similar to gluErrorString, but that function is not available on all plattforms +// (read: iOS) +const GLubyte* acglErrorString( GLenum err ); + +/* + * This function can be used outside of the ACGL framework to check always(!) for + * OpenGL errors. It will print the errors and return the error code of the last one. + * Each OpenGL command can only throw one error, errors can only stack up if this + * function or glGetError was not called often enough (and some OpenGL implementations + * will forget old errors if new ones occur). + */ +#define openGLError() openGLError_( __FILE__, __LINE__ ) + +/* + * This function is used internally in ACGL - but not directly. It gets called from all + * other rare/common/critical error checks. The __FILE__ __LINE macros have to be used in + * those to get the correct values from the caller file, if we would use the macro + * above we could get the file/line from this file, which isn't helping. + */ +GLenum openGLError_( const char *_fileName, const unsigned long _lineNumber ); + +/* + * Inside of ACGL we distinguish between rare, common and critical errors. Each kind can be + * switched off which turns the function into nothing after compiler optimization. If an + * error check is turned off it will always behave as if there was no error, even if there + * is one... + * + * Per default a debug build enables critical and common errors, a release build only critical + * ones. + * + * We have two functions for each kind: + * openGL____Error() will print error messages and return the last error (or GL_NO_ERROR) + * openGL____ErrorOccured() will print error messages and retrun true if there was an error + * + * The definition of rare/common/critical is a bit fuzzy: + * + * critical: Errors which can occur even in a bug-free app, like out-of-memory errors. + * These checks are rare, stuff like glCreate calls get checked here. + * Tests for critical errors should not impact the performance of the app measurably. + * + * common: + * Errors which are quite common while developing. Misuse of the library etc. + * Setting uniforms which don't exist or shader compile errors are candidates for + * common errors. Places where OpenGL resources are calles by a string etc. + * + * rare: Errors which are so uncommon that we don't even check those ina normal debug build. + * Switching these on can impact performance as there can be alot of them. If theres a + * OpenGL error somewhere in the code, switch these checks on to find the correct spot. + * + * OpenGL error checks that are more complicated than just a glGetError call will be wrapped in the + * same defines, this can be used in application code as well. + * + */ +#ifdef ACGL_CHECK_CRITICAL_GL_ERRORS +# define openGLCriticalError() openGLError_( __FILE__, __LINE__ ) +# define openGLCriticalErrorOccured() (openGLError_( __FILE__, __LINE__ ) != GL_NO_ERROR) +#else + inline GLenum openGLCriticalError() { return GL_NO_ERROR; } + inline bool openGLCriticalErrorOccured() { return false; } +#endif + +#ifdef ACGL_CHECK_COMMON_GL_ERRORS +# define openGLCommonError() openGLError_( __FILE__, __LINE__ ) +# define openGLCommonErrorOccured() (openGLError_( __FILE__, __LINE__ ) != GL_NO_ERROR) +#else + inline GLenum openGLCommonError() { return GL_NO_ERROR; } + inline bool openGLCommonErrorOccured() { return false; } +#endif + +#ifdef ACGL_CHECK_RARE_GL_ERRORS +# define openGLRareError() openGLError_( __FILE__, __LINE__ ) +# define openGLRareErrorOccured() (openGLError_( __FILE__, __LINE__ ) != GL_NO_ERROR) +#else + inline GLenum openGLRareError() { return GL_NO_ERROR; } + inline bool openGLRareErrorOccured() { return false; } +#endif + + +} // Tools +} // GLUtils +} // ACGL + +#endif // ACGL_GLUTILS_TOOLS_HH + diff --git a/include/ACGL/Resource/ArrayBuffer.hh b/include/ACGL/Resource/ArrayBuffer.hh index 780d367a168427cb42c8bc2caf210fe7dda2e58f..ea08e1b931cd038f9fc3351f02bacafc30ee3d8f 100644 --- a/include/ACGL/Resource/ArrayBuffer.hh +++ b/include/ACGL/Resource/ArrayBuffer.hh @@ -3,179 +3,179 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_ARRAYBUFFER_HH -#define ACGL_RESOURCE_ARRAYBUFFER_HH - -#include <ACGL/GLUtils/Tools.hh> -#include <ACGL/Types.hh> -#include <ACGL/GL.hh> - -#include <string> -#include <vector> - -namespace ACGL{ -namespace Resource{ - -class ArrayBuffer -{ - // ==================================================================================================== \/ - // ============================================================================================ STRUCTS \/ - // ==================================================================================================== \/ -public: - //! Each attribute has a dimension (#components, e.g. normal with x/y/z => 3) and an offset in the stride (in bytes) - struct Attribute - { - std::string name; - GLenum type; - GLint dimension; - GLint offset; - }; - - // ===================================================================================================== \/ - // ============================================================================================ TYPEDEFS \/ - // ===================================================================================================== \/ -public: - typedef std::vector< Attribute > AttributeVec; - - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - ArrayBuffer( - GLenum _usage = GL_STATIC_DRAW, - GLenum _mode = GL_TRIANGLES) - : mContext(0), - mUsage(_usage), - mMode(_mode), - mElements(0), - mStride(0), - mAttributes() - { - glGenBuffers(1, &mContext); - } - - virtual ~ArrayBuffer(void) - { - if(mContext != 0) - glDeleteBuffers(1, &mContext); - } - - // ==================================================================================================== \/ - // ============================================================================================ GETTERS \/ - // ==================================================================================================== \/ -public: - inline GLuint getContext (void) const { return mContext; } - inline GLenum getUsage (void) const { return mUsage; } - inline GLenum getMode (void) const { return mMode; } - inline GLsizei getElements (void) const { return mElements; } - inline GLsizei getStride (void) const { return mStride; } - inline const AttributeVec& getAttributes (void) const { return mAttributes; } - - // ==================================================================================================== \/ - // ============================================================================================ SETTERS \/ - // ==================================================================================================== \/ -public: - inline void setUsage (GLenum _usage) { mUsage = _usage; } - inline void setMode (GLenum _mode) { mMode = _mode; } - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - int_t getAttributeIndexByName(const std::string& _nameInArray) const; - - inline void attachAttribute( - const std::string& _name, - GLenum _type, - GLint _dimension) - { - Attribute attribute = { - _name, - _type, - _dimension, - mStride}; - mStride += GLUtils::Tools::getGLTypeSize(_type) * _dimension; - mAttributes.push_back(attribute); - } - - inline void removeAttributes(void) - { - mStride = 0; - mAttributes.clear(); - } - - // ===================================================================================================== \/ - // ============================================================================================ WRAPPERS \/ - // ===================================================================================================== \/ -public: - void render(void) const; - - void draw(void) const - { - glDrawArrays(mMode, 0, mElements); - } - - //! Bind this buffer - inline void bind(void) const - { - glBindBuffer(GL_ARRAY_BUFFER, mContext); - } - - inline void setPointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const - { - glVertexAttribPointer( - _indexInShader, - mAttributes[_indexInArray].dimension, - mAttributes[_indexInArray].type, - GL_FALSE, - mStride, - reinterpret_cast<GLvoid*>(mAttributes[_indexInArray].offset)); - } - - inline bool setPointer(const std::string& _nameInArray, GLuint _indexInShader) const - { - int_t index = getAttributeIndexByName(_nameInArray); - if(index == -1) - return false; - - setPointer(index, _indexInShader); - return true; - } - - //! Set data for this buffer - inline void setData(const GLvoid* _pData) const - { - glBufferData( - GL_ARRAY_BUFFER, - mStride * mElements, - _pData, - mUsage); - } - - //! Set data for this buffer and change its size - inline void setData(const GLvoid* _pData, GLsizei _elements) - { - mElements = _elements; - glBufferData( - GL_ARRAY_BUFFER, - mStride * mElements, - _pData, - mUsage); - } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLuint mContext; - GLenum mUsage; - GLenum mMode; - GLsizei mElements; - GLsizei mStride; - AttributeVec mAttributes; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_ARRAYBUFFER_HH +#ifndef ACGL_RESOURCE_ARRAYBUFFER_HH +#define ACGL_RESOURCE_ARRAYBUFFER_HH + +#include <ACGL/GLUtils/Tools.hh> +#include <ACGL/Types.hh> +#include <ACGL/GL.hh> + +#include <string> +#include <vector> + +namespace ACGL{ +namespace Resource{ + +class ArrayBuffer +{ + // ==================================================================================================== \/ + // ============================================================================================ STRUCTS \/ + // ==================================================================================================== \/ +public: + //! Each attribute has a dimension (#components, e.g. normal with x/y/z => 3) and an offset in the stride (in bytes) + struct Attribute + { + std::string name; + GLenum type; + GLint dimension; + GLint offset; + }; + + // ===================================================================================================== \/ + // ============================================================================================ TYPEDEFS \/ + // ===================================================================================================== \/ +public: + typedef std::vector< Attribute > AttributeVec; + + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + ArrayBuffer( + GLenum _usage = GL_STATIC_DRAW, + GLenum _mode = GL_TRIANGLES) + : mContext(0), + mUsage(_usage), + mMode(_mode), + mElements(0), + mStride(0), + mAttributes() + { + glGenBuffers(1, &mContext); + } + + virtual ~ArrayBuffer(void) + { + if(mContext != 0) + glDeleteBuffers(1, &mContext); + } + + // ==================================================================================================== \/ + // ============================================================================================ GETTERS \/ + // ==================================================================================================== \/ +public: + inline GLuint getContext (void) const { return mContext; } + inline GLenum getUsage (void) const { return mUsage; } + inline GLenum getMode (void) const { return mMode; } + inline GLsizei getElements (void) const { return mElements; } + inline GLsizei getStride (void) const { return mStride; } + inline const AttributeVec& getAttributes (void) const { return mAttributes; } + + // ==================================================================================================== \/ + // ============================================================================================ SETTERS \/ + // ==================================================================================================== \/ +public: + inline void setUsage (GLenum _usage) { mUsage = _usage; } + inline void setMode (GLenum _mode) { mMode = _mode; } + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + int_t getAttributeIndexByName(const std::string& _nameInArray) const; + + inline void attachAttribute( + const std::string& _name, + GLenum _type, + GLint _dimension) + { + Attribute attribute = { + _name, + _type, + _dimension, + mStride}; + mStride += GLUtils::Tools::getGLTypeSize(_type) * _dimension; + mAttributes.push_back(attribute); + } + + inline void removeAttributes(void) + { + mStride = 0; + mAttributes.clear(); + } + + // ===================================================================================================== \/ + // ============================================================================================ WRAPPERS \/ + // ===================================================================================================== \/ +public: + void render(void) const; + + void draw(void) const + { + glDrawArrays(mMode, 0, mElements); + } + + //! Bind this buffer + inline void bind(void) const + { + glBindBuffer(GL_ARRAY_BUFFER, mContext); + } + + inline void setPointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const + { + glVertexAttribPointer( + _indexInShader, + mAttributes[_indexInArray].dimension, + mAttributes[_indexInArray].type, + GL_FALSE, + mStride, + reinterpret_cast<GLvoid*>(mAttributes[_indexInArray].offset)); + } + + inline bool setPointer(const std::string& _nameInArray, GLuint _indexInShader) const + { + int_t index = getAttributeIndexByName(_nameInArray); + if(index == -1) + return false; + + setPointer(index, _indexInShader); + return true; + } + + //! Set data for this buffer + inline void setData(const GLvoid* _pData) const + { + glBufferData( + GL_ARRAY_BUFFER, + mStride * mElements, + _pData, + mUsage); + } + + //! Set data for this buffer and change its size + inline void setData(const GLvoid* _pData, GLsizei _elements) + { + mElements = _elements; + glBufferData( + GL_ARRAY_BUFFER, + mStride * mElements, + _pData, + mUsage); + } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLuint mContext; + GLenum mUsage; + GLenum mMode; + GLsizei mElements; + GLsizei mStride; + AttributeVec mAttributes; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_ARRAYBUFFER_HH diff --git a/include/ACGL/Resource/ArrayBufferControl.hh b/include/ACGL/Resource/ArrayBufferControl.hh index a0e8959c851e1ee2a17e89f2e87b96044e5adff2..eaf5dcebb83e1b17e9e3c0a423bd742cbbd3a9cd 100644 --- a/include/ACGL/Resource/ArrayBufferControl.hh +++ b/include/ACGL/Resource/ArrayBufferControl.hh @@ -3,88 +3,88 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_ARRAYBUFFERCONTROL_HH -#define ACGL_RESOURCE_ARRAYBUFFERCONTROL_HH - -#include <ACGL/Resource/BasicController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/ArrayBuffer.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class ArrayBufferControl : public BasicController<ArrayBuffer> -{ - // ==================================================================================================== \/ - // ============================================================================================ STRUCTS \/ - // ==================================================================================================== \/ -public: - struct AttributeDefine - { - std::string name; - GLenum type; - GLint dimension; - }; - - // ===================================================================================================== \/ - // ============================================================================================ TYPEDEFS \/ - // ===================================================================================================== \/ -public: - typedef std::vector< AttributeDefine > AttributeDefineVec; - - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - ArrayBufferControl(void) - : mUsage(GL_STATIC_DRAW), - mMode(GL_TRIANGLES), - mElements(0), - mpData(NULL), - mAttributeDefines() - {} - virtual ~ArrayBufferControl() {} - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - inline ArrayBufferControl& usage (GLenum _usage) { mUsage = _usage; return *this; } - inline ArrayBufferControl& mode (GLenum _mode) { mMode = _mode; return *this; } - - inline ArrayBufferControl& data (const GLvoid* _pData, GLsizei _elements) - { - mpData = _pData; - mElements = _elements; - return *this; - } - - inline ArrayBufferControl& attribute (const std::string& _name, GLenum _type, GLint _dimension) - { - AttributeDefine a = {_name, _type, _dimension}; - mAttributeDefines.push_back(a); - return *this; - } - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedArrayBuffer create(void); - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLenum mUsage; - GLenum mMode; - GLsizei mElements; - const GLvoid* mpData; - AttributeDefineVec mAttributeDefines; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_ARRAYBUFFERCONTROL_HH +#define ACGL_RESOURCE_ARRAYBUFFERCONTROL_HH + +#include <ACGL/Resource/BasicController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/ArrayBuffer.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class ArrayBufferControl : public BasicController<ArrayBuffer> +{ + // ==================================================================================================== \/ + // ============================================================================================ STRUCTS \/ + // ==================================================================================================== \/ +public: + struct AttributeDefine + { + std::string name; + GLenum type; + GLint dimension; + }; + + // ===================================================================================================== \/ + // ============================================================================================ TYPEDEFS \/ + // ===================================================================================================== \/ +public: + typedef std::vector< AttributeDefine > AttributeDefineVec; + + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + ArrayBufferControl(void) + : mUsage(GL_STATIC_DRAW), + mMode(GL_TRIANGLES), + mElements(0), + mpData(NULL), + mAttributeDefines() + {} + virtual ~ArrayBufferControl() {} + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + inline ArrayBufferControl& usage (GLenum _usage) { mUsage = _usage; return *this; } + inline ArrayBufferControl& mode (GLenum _mode) { mMode = _mode; return *this; } + + inline ArrayBufferControl& data (const GLvoid* _pData, GLsizei _elements) + { + mpData = _pData; + mElements = _elements; + return *this; + } + + inline ArrayBufferControl& attribute (const std::string& _name, GLenum _type, GLint _dimension) + { + AttributeDefine a = {_name, _type, _dimension}; + mAttributeDefines.push_back(a); + return *this; + } + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedArrayBuffer create(void); + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLenum mUsage; + GLenum mMode; + GLsizei mElements; + const GLvoid* mpData; + AttributeDefineVec mAttributeDefines; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/ElementArrayBuffer.hh b/include/ACGL/Resource/ElementArrayBuffer.hh index 19c86c95c9473147aba3fd5575ec5cabd90794b7..179ac24dc66d07a5880fd0f74c39b1e6444b910c 100644 --- a/include/ACGL/Resource/ElementArrayBuffer.hh +++ b/include/ACGL/Resource/ElementArrayBuffer.hh @@ -3,117 +3,117 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_ELEMENTBUFFERDATA_HH -#define ACGL_RESOURCE_ELEMENTBUFFERDATA_HH - -#include <ACGL/GLUtils/Tools.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class ElementArrayBuffer -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - ElementArrayBuffer( - GLenum _usage = GL_STATIC_DRAW, - GLenum _mode = GL_TRIANGLES, - GLenum _type = GL_UNSIGNED_INT) - : mContext(0), - mUsage(_usage), - mMode(_mode), - mElements(0), - mType(_type), - mSizeOfType(GLUtils::Tools::getGLTypeSize(_type)) - { - glGenBuffers(1, &mContext); - } - - virtual ~ElementArrayBuffer(void) - { - if(mContext != 0) - glDeleteBuffers(1, &mContext); - } - - // ==================================================================================================== \/ - // ============================================================================================ GETTERS \/ - // ==================================================================================================== \/ -public: - inline GLuint getContext (void) const { return mContext; } - inline GLenum getUsage (void) const { return mUsage; } - inline GLenum getMode (void) const { return mMode; } - inline GLsizei getElements (void) const { return mElements; } - inline GLenum getType (void) const { return mType; } - inline GLint getSizeOfType (void) const { return mSizeOfType; } - - // ==================================================================================================== \/ - // ============================================================================================ SETTERS \/ - // ==================================================================================================== \/ -public: - inline void setUsage (GLenum _usage) { mUsage = _usage; } - inline void setMode (GLenum _mode) { mMode = _mode; } - - inline void setType (GLenum _type) - { - mType = _type; - mSizeOfType = GLUtils::Tools::getGLTypeSize(mType); - } - - // ===================================================================================================== \/ - // ============================================================================================ WRAPPERS \/ - // ===================================================================================================== \/ -public: - //! Bind this buffer - inline void bind(void) const - { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mContext); - } - - void draw(void) const - { - glDrawElements(mMode, mElements, mType, (GLvoid*)0); - } - - //! Set data for this buffer - inline void setData( - const GLvoid* _pData) - { - glBufferData( - GL_ELEMENT_ARRAY_BUFFER, - mElements * mSizeOfType, - _pData, - mUsage); - } - - //! Set data for this buffer - inline void setData( - const GLvoid* _pData, - GLsizei _elements) - { - mElements = _elements; - glBufferData( - GL_ELEMENT_ARRAY_BUFFER, - mElements * mSizeOfType, - _pData, - mUsage); - } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLuint mContext; - GLenum mUsage; - GLenum mMode; - GLsizei mElements; - GLenum mType; - GLint mSizeOfType; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_ELEMENTBUFFERDATA_HH +#ifndef ACGL_RESOURCE_ELEMENTBUFFERDATA_HH +#define ACGL_RESOURCE_ELEMENTBUFFERDATA_HH + +#include <ACGL/GLUtils/Tools.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class ElementArrayBuffer +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + ElementArrayBuffer( + GLenum _usage = GL_STATIC_DRAW, + GLenum _mode = GL_TRIANGLES, + GLenum _type = GL_UNSIGNED_INT) + : mContext(0), + mUsage(_usage), + mMode(_mode), + mElements(0), + mType(_type), + mSizeOfType(GLUtils::Tools::getGLTypeSize(_type)) + { + glGenBuffers(1, &mContext); + } + + virtual ~ElementArrayBuffer(void) + { + if(mContext != 0) + glDeleteBuffers(1, &mContext); + } + + // ==================================================================================================== \/ + // ============================================================================================ GETTERS \/ + // ==================================================================================================== \/ +public: + inline GLuint getContext (void) const { return mContext; } + inline GLenum getUsage (void) const { return mUsage; } + inline GLenum getMode (void) const { return mMode; } + inline GLsizei getElements (void) const { return mElements; } + inline GLenum getType (void) const { return mType; } + inline GLint getSizeOfType (void) const { return mSizeOfType; } + + // ==================================================================================================== \/ + // ============================================================================================ SETTERS \/ + // ==================================================================================================== \/ +public: + inline void setUsage (GLenum _usage) { mUsage = _usage; } + inline void setMode (GLenum _mode) { mMode = _mode; } + + inline void setType (GLenum _type) + { + mType = _type; + mSizeOfType = GLUtils::Tools::getGLTypeSize(mType); + } + + // ===================================================================================================== \/ + // ============================================================================================ WRAPPERS \/ + // ===================================================================================================== \/ +public: + //! Bind this buffer + inline void bind(void) const + { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mContext); + } + + void draw(void) const + { + glDrawElements(mMode, mElements, mType, (GLvoid*)0); + } + + //! Set data for this buffer + inline void setData( + const GLvoid* _pData) + { + glBufferData( + GL_ELEMENT_ARRAY_BUFFER, + mElements * mSizeOfType, + _pData, + mUsage); + } + + //! Set data for this buffer + inline void setData( + const GLvoid* _pData, + GLsizei _elements) + { + mElements = _elements; + glBufferData( + GL_ELEMENT_ARRAY_BUFFER, + mElements * mSizeOfType, + _pData, + mUsage); + } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLuint mContext; + GLenum mUsage; + GLenum mMode; + GLsizei mElements; + GLenum mType; + GLint mSizeOfType; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_ELEMENTBUFFERDATA_HH diff --git a/include/ACGL/Resource/ElementArrayBufferControl.hh b/include/ACGL/Resource/ElementArrayBufferControl.hh index 11e9e431ca4bcfb221e8d55a428b430937b5e0a3..30e232935ac8861edf65177767b96e4bfddbd686 100644 --- a/include/ACGL/Resource/ElementArrayBufferControl.hh +++ b/include/ACGL/Resource/ElementArrayBufferControl.hh @@ -3,78 +3,78 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_ELEMENTARRAYBUFFERCONTROL_HH -#define ACGL_RESOURCE_ELEMENTARRAYBUFFERCONTROL_HH - -#include <ACGL/Resource/BasicController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/ElementArrayBuffer.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class ElementArrayBufferControl : public BasicController<ElementArrayBuffer> -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - ElementArrayBufferControl(void) - : mUsage(GL_STATIC_DRAW), - mMode(GL_TRIANGLES), - mType(GL_UNSIGNED_INT), - mElements(0), - mpData(NULL) - {} - virtual ~ElementArrayBufferControl() {} - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - inline ElementArrayBufferControl& usage (GLenum _usage) { mUsage = _usage; return *this; } - inline ElementArrayBufferControl& mode (GLenum _mode) { mMode = _mode; return *this; } - inline ElementArrayBufferControl& type (GLenum _type) { mType = _type; return *this; } - - inline ElementArrayBufferControl& data (const GLvoid* _pData, GLsizei _elements) - { - mpData = _pData; - mElements = _elements; - return *this; - } - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedElementArrayBuffer create(void) - { - SharedElementArrayBuffer elementArrayBuffer(new ElementArrayBuffer( - mUsage, - mMode, - mType)); - if(mpData != NULL) - { - elementArrayBuffer->bind(); - elementArrayBuffer->setData(mpData, mElements); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - } - return elementArrayBuffer; - } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLenum mUsage; - GLenum mMode; - GLenum mType; - GLsizei mElements; - const GLvoid* mpData; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_ELEMENTARRAYBUFFERCONTROL_HH +#define ACGL_RESOURCE_ELEMENTARRAYBUFFERCONTROL_HH + +#include <ACGL/Resource/BasicController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/ElementArrayBuffer.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class ElementArrayBufferControl : public BasicController<ElementArrayBuffer> +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + ElementArrayBufferControl(void) + : mUsage(GL_STATIC_DRAW), + mMode(GL_TRIANGLES), + mType(GL_UNSIGNED_INT), + mElements(0), + mpData(NULL) + {} + virtual ~ElementArrayBufferControl() {} + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + inline ElementArrayBufferControl& usage (GLenum _usage) { mUsage = _usage; return *this; } + inline ElementArrayBufferControl& mode (GLenum _mode) { mMode = _mode; return *this; } + inline ElementArrayBufferControl& type (GLenum _type) { mType = _type; return *this; } + + inline ElementArrayBufferControl& data (const GLvoid* _pData, GLsizei _elements) + { + mpData = _pData; + mElements = _elements; + return *this; + } + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedElementArrayBuffer create(void) + { + SharedElementArrayBuffer elementArrayBuffer(new ElementArrayBuffer( + mUsage, + mMode, + mType)); + if(mpData != NULL) + { + elementArrayBuffer->bind(); + elementArrayBuffer->setData(mpData, mElements); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + return elementArrayBuffer; + } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLenum mUsage; + GLenum mMode; + GLenum mType; + GLsizei mElements; + const GLvoid* mpData; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/FileManager.hh b/include/ACGL/Resource/FileManager.hh index fc25b07743b0d21bbe41163e3b54f25b88e3f35c..83da44e659ee791b29e17d8217599882713f23a7 100644 --- a/include/ACGL/Resource/FileManager.hh +++ b/include/ACGL/Resource/FileManager.hh @@ -3,144 +3,144 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_FILEMANAGER_HH -#define ACGL_RESOURCE_FILEMANAGER_HH - -#include <ACGL/Types.hh> -#include <ACGL/Base/Singleton.hh> -#include <ACGL/Utils/Log.hh> - -#include <ACGL/Resource/FileController.hh> - -#include <tr1/memory> -#include <map> -#include <string> - -namespace ACGL{ -namespace Resource{ - -template<typename RESOURCE> -class FileManager : public Base::Singleton< FileManager<RESOURCE> > -{ - friend class Base::Singleton< FileManager<RESOURCE> >; - -public: - typedef std::tr1::shared_ptr< FileController<RESOURCE> > SharedController; - typedef std::tr1::shared_ptr<RESOURCE> SharedResource; - struct Resource - { - SharedController controller; - SharedResource resource; - }; - - typedef std::map<std::string, Resource> ResourceMap; - typedef ResourceMap ResourceContainer; - - virtual ~FileManager(void) {} - - template<typename CONTROLLER> - SharedResource get(const CONTROLLER& _controller); - SharedResource query(const std::string& _filename); - bool exists(const std::string& _key); - bool erase(const std::string& key); - void eraseAll(void); - bool update(const std::string& key); - void updateAll(void); - - typename ResourceMap::const_iterator begin(void) const { return mResourceMap.begin(); } - typename ResourceMap::const_iterator end(void) const { return mResourceMap.end(); } - -protected: - FileManager(void) - : mResourceMap() - {} -private: - FileManager(const FileManager&) {} - -private: - ResourceMap mResourceMap; -}; - -template<typename RESOURCE> template<typename CONTROLLER> -typename FileManager<RESOURCE>::SharedResource FileManager<RESOURCE>::get(const CONTROLLER& _controller) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_controller.getFilename()); - if(existingResource != mResourceMap.end()) - return existingResource->second.resource; - - SharedController pController(new CONTROLLER(_controller)); - SharedResource pResource = pController->create(); - if(pResource) - { - Resource resource = { pController, pResource }; - mResourceMap[_controller.getFilename()] = resource; - Utils::debug() << "FileManager::getResource: Resource loaded: " << _controller.getFilename() << std::endl; - return pResource; - } - else - { - Utils::error() << "FileManager::getResource: Resource could not be loaded: " << std::endl; - } - return SharedResource(); -} - -template<typename RESOURCE> -typename FileManager<RESOURCE>::SharedResource FileManager<RESOURCE>::query(const std::string& _key) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_key); - if(existingResource != mResourceMap.end()) - return existingResource->second.resource; - return SharedResource(); -} - -template<typename RESOURCE> -bool FileManager<RESOURCE>::exists(const std::string& _key) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_key); - if(existingResource != mResourceMap.end()) - return true; - return false; -} - -template<typename RESOURCE> -bool FileManager<RESOURCE>::erase(const std::string& _key) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_key); - if(existingResource != mResourceMap.end()) - { - mResourceMap.erase(existingResource); - Utils::debug() << "FileManager::Resource deleted: " << _key << std::endl; - return true; - } - Utils::warning() << "FileManager::Resource not found for deletion! " << std::endl; - return false; -} - -template<typename RESOURCE> -void FileManager<RESOURCE>::eraseAll(void) -{ - mResourceMap.clear(); -} - -template<typename RESOURCE> -bool FileManager<RESOURCE>::update(const std::string& _key) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_key); - if(existingResource != mResourceMap.end()) - return existingResource->second.controller->update(existingResource->second.resource); - return false; -} - -template<typename RESOURCE> -void FileManager<RESOURCE>::updateAll(void) -{ - for(typename ResourceMap::iterator i = mResourceMap.begin(); - i != mResourceMap.end(); - ++i) - i->second.controller->update(i->second.resource); -} - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_NAMEMANAGER_HH +#ifndef ACGL_RESOURCE_FILEMANAGER_HH +#define ACGL_RESOURCE_FILEMANAGER_HH + +#include <ACGL/Types.hh> +#include <ACGL/Base/Singleton.hh> +#include <ACGL/Utils/Log.hh> + +#include <ACGL/Resource/FileController.hh> + +#include <tr1/memory> +#include <map> +#include <string> + +namespace ACGL{ +namespace Resource{ + +template<typename RESOURCE> +class FileManager : public Base::Singleton< FileManager<RESOURCE> > +{ + friend class Base::Singleton< FileManager<RESOURCE> >; + +public: + typedef std::tr1::shared_ptr< FileController<RESOURCE> > SharedController; + typedef std::tr1::shared_ptr<RESOURCE> SharedResource; + struct Resource + { + SharedController controller; + SharedResource resource; + }; + + typedef std::map<std::string, Resource> ResourceMap; + typedef ResourceMap ResourceContainer; + + virtual ~FileManager(void) {} + + template<typename CONTROLLER> + SharedResource get(const CONTROLLER& _controller); + SharedResource query(const std::string& _filename); + bool exists(const std::string& _key); + bool erase(const std::string& key); + void eraseAll(void); + bool update(const std::string& key); + void updateAll(void); + + typename ResourceMap::const_iterator begin(void) const { return mResourceMap.begin(); } + typename ResourceMap::const_iterator end(void) const { return mResourceMap.end(); } + +protected: + FileManager(void) + : mResourceMap() + {} +private: + FileManager(const FileManager&) {} + +private: + ResourceMap mResourceMap; +}; + +template<typename RESOURCE> template<typename CONTROLLER> +typename FileManager<RESOURCE>::SharedResource FileManager<RESOURCE>::get(const CONTROLLER& _controller) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_controller.getFilename()); + if(existingResource != mResourceMap.end()) + return existingResource->second.resource; + + SharedController pController(new CONTROLLER(_controller)); + SharedResource pResource = pController->create(); + if(pResource) + { + Resource resource = { pController, pResource }; + mResourceMap[_controller.getFilename()] = resource; + Utils::debug() << "FileManager::getResource: Resource loaded: " << _controller.getFilename() << std::endl; + return pResource; + } + else + { + Utils::error() << "FileManager::getResource: Resource could not be loaded: " << std::endl; + } + return SharedResource(); +} + +template<typename RESOURCE> +typename FileManager<RESOURCE>::SharedResource FileManager<RESOURCE>::query(const std::string& _key) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_key); + if(existingResource != mResourceMap.end()) + return existingResource->second.resource; + return SharedResource(); +} + +template<typename RESOURCE> +bool FileManager<RESOURCE>::exists(const std::string& _key) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_key); + if(existingResource != mResourceMap.end()) + return true; + return false; +} + +template<typename RESOURCE> +bool FileManager<RESOURCE>::erase(const std::string& _key) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_key); + if(existingResource != mResourceMap.end()) + { + mResourceMap.erase(existingResource); + Utils::debug() << "FileManager::Resource deleted: " << _key << std::endl; + return true; + } + Utils::warning() << "FileManager::Resource not found for deletion! " << std::endl; + return false; +} + +template<typename RESOURCE> +void FileManager<RESOURCE>::eraseAll(void) +{ + mResourceMap.clear(); +} + +template<typename RESOURCE> +bool FileManager<RESOURCE>::update(const std::string& _key) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_key); + if(existingResource != mResourceMap.end()) + return existingResource->second.controller->update(existingResource->second.resource); + return false; +} + +template<typename RESOURCE> +void FileManager<RESOURCE>::updateAll(void) +{ + for(typename ResourceMap::iterator i = mResourceMap.begin(); + i != mResourceMap.end(); + ++i) + i->second.controller->update(i->second.resource); +} + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_NAMEMANAGER_HH diff --git a/include/ACGL/Resource/FrameBuffer.hh b/include/ACGL/Resource/FrameBuffer.hh index 54cc5a52366944c99cbbc8999b70c6f39dbd7780..6f152db0cc34210c9e5a95faf75d14c12bb96dba 100644 --- a/include/ACGL/Resource/FrameBuffer.hh +++ b/include/ACGL/Resource/FrameBuffer.hh @@ -3,204 +3,204 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_FRAMEBUFFER_HH -#define ACGL_RESOURCE_FRAMEBUFFER_HH - -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/RenderBuffer.hh> -#include <ACGL/Resource/Texture.hh> -#include <ACGL/Utils/Log.hh> -#include <ACGL/Utils/StringOperations.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class FrameBuffer -{ - // =================================================================================================== \/ - // ============================================================================================ STATIC \/ - // =================================================================================================== \/ -private: - static GLuint msBuffers[8]; - - // ==================================================================================================== \/ - // ============================================================================================ STRUCTS \/ - // ==================================================================================================== \/ -public: - //! An attachment can be a texture or a render buffer - struct Attachment - { - std::string name; - SharedTexture texture; - SharedRenderBuffer renderBuffer; - }; - - // ===================================================================================================== \/ - // ============================================================================================ TYPEDEFS \/ - // ===================================================================================================== \/ -public: - typedef std::vector< Attachment > AttachmentVec; - - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - FrameBuffer(GLsizei _width, GLsizei _height) - : mContext(0), - mWidth(_width), - mHeight(_height), - mDrawBuffers(0), - mColorAttachments(), - mDepthAttachment() - { - glGenFramebuffers(1, &mContext); - mDepthAttachment.name = ""; - mDepthAttachment.texture = SharedTexture(); - mDepthAttachment.renderBuffer = SharedRenderBuffer(); - } - - virtual ~FrameBuffer(void) - { - if(mContext != 0) - glDeleteFramebuffers(1, &mContext); - } - - // ==================================================================================================== \/ - // ============================================================================================ GETTERS \/ - // ==================================================================================================== \/ -public: - inline GLuint getContext (void) const { return mContext; } - inline GLsizei getWidth (void) const { return mWidth; } - inline GLsizei getHeight (void) const { return mHeight; } - inline GLsizei getDrawBuffers (void) const { return mDrawBuffers; } - inline const AttachmentVec& getColorAttachments (void) const { return mColorAttachments; } - inline const Attachment& getDepthAttachment (void) const { return mDepthAttachment; } - - // ===================================================================================================== \/ - // ============================================================================================ WRAPPERS \/ - // ===================================================================================================== \/ -public: - inline void bindAsRenderTarget(void) const - { - glBindFramebuffer(GL_FRAMEBUFFER, mContext); - glDrawBuffers(mDrawBuffers, msBuffers); - } - - inline void bind(void) const - { - glBindFramebuffer(GL_FRAMEBUFFER, mContext); - } - - inline void setDrawBuffers(void) const - { - glDrawBuffers(mDrawBuffers, msBuffers); - } - - inline void setViewport(void) const - { - glViewport(0, 0, mWidth, mHeight); - } - - inline bool attachColorRenderBuffer(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()); - mDrawBuffers++; - Attachment attachment = {"", SharedTexture(), _renderBuffer}; - mColorAttachments.push_back(attachment); - return true; - } - - inline bool attachColorTexture(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); - mDrawBuffers++; - Attachment attachment = {"", _texture, SharedRenderBuffer()}; - mColorAttachments.push_back(attachment); - return true; - } - - 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 || - _renderBuffer->getInternalFormat() == GL_DEPTH_STENCIL_OES) - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext()); - #else - if( _renderBuffer->getInternalFormat() == GL_DEPTH24_STENCIL8 || - _renderBuffer->getInternalFormat() == GL_DEPTH_STENCIL) - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext()); - #endif - mDepthAttachment.renderBuffer = _renderBuffer; - return true; - } - - 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 || - _texture->getInternalFormat() == GL_DEPTH_STENCIL_OES) - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0); - #else - if( _texture->getInternalFormat() == GL_DEPTH24_STENCIL8 || - _texture->getInternalFormat() == GL_DEPTH_STENCIL) - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0); - #endif - mDepthAttachment.texture = _texture; - return true; - } - - inline bool isFrameBufferComplete(void) - { - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - { - Utils::error() << "Failed to make complete FrameBuffer object: " << (glCheckFramebufferStatus(GL_FRAMEBUFFER)) << std::endl; - return false; - } - return true; - } - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -protected: - void clean(void); - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLuint mContext; - GLsizei mWidth; - GLsizei mHeight; - GLsizei mDrawBuffers; - AttachmentVec mColorAttachments; - Attachment mDepthAttachment; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_FRAMEBUFFER_HH +#ifndef ACGL_RESOURCE_FRAMEBUFFER_HH +#define ACGL_RESOURCE_FRAMEBUFFER_HH + +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/RenderBuffer.hh> +#include <ACGL/Resource/Texture.hh> +#include <ACGL/Utils/Log.hh> +#include <ACGL/Utils/StringOperations.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class FrameBuffer +{ + // =================================================================================================== \/ + // ============================================================================================ STATIC \/ + // =================================================================================================== \/ +private: + static GLuint msBuffers[8]; + + // ==================================================================================================== \/ + // ============================================================================================ STRUCTS \/ + // ==================================================================================================== \/ +public: + //! An attachment can be a texture or a render buffer + struct Attachment + { + std::string name; + SharedTexture texture; + SharedRenderBuffer renderBuffer; + }; + + // ===================================================================================================== \/ + // ============================================================================================ TYPEDEFS \/ + // ===================================================================================================== \/ +public: + typedef std::vector< Attachment > AttachmentVec; + + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + FrameBuffer(GLsizei _width, GLsizei _height) + : mContext(0), + mWidth(_width), + mHeight(_height), + mDrawBuffers(0), + mColorAttachments(), + mDepthAttachment() + { + glGenFramebuffers(1, &mContext); + mDepthAttachment.name = ""; + mDepthAttachment.texture = SharedTexture(); + mDepthAttachment.renderBuffer = SharedRenderBuffer(); + } + + virtual ~FrameBuffer(void) + { + if(mContext != 0) + glDeleteFramebuffers(1, &mContext); + } + + // ==================================================================================================== \/ + // ============================================================================================ GETTERS \/ + // ==================================================================================================== \/ +public: + inline GLuint getContext (void) const { return mContext; } + inline GLsizei getWidth (void) const { return mWidth; } + inline GLsizei getHeight (void) const { return mHeight; } + inline GLsizei getDrawBuffers (void) const { return mDrawBuffers; } + inline const AttachmentVec& getColorAttachments (void) const { return mColorAttachments; } + inline const Attachment& getDepthAttachment (void) const { return mDepthAttachment; } + + // ===================================================================================================== \/ + // ============================================================================================ WRAPPERS \/ + // ===================================================================================================== \/ +public: + inline void bindAsRenderTarget(void) const + { + glBindFramebuffer(GL_FRAMEBUFFER, mContext); + glDrawBuffers(mDrawBuffers, msBuffers); + } + + inline void bind(void) const + { + glBindFramebuffer(GL_FRAMEBUFFER, mContext); + } + + inline void setDrawBuffers(void) const + { + glDrawBuffers(mDrawBuffers, msBuffers); + } + + inline void setViewport(void) const + { + glViewport(0, 0, mWidth, mHeight); + } + + inline bool attachColorRenderBuffer(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()); + mDrawBuffers++; + Attachment attachment = {"", SharedTexture(), _renderBuffer}; + mColorAttachments.push_back(attachment); + return true; + } + + inline bool attachColorTexture(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); + mDrawBuffers++; + Attachment attachment = {"", _texture, SharedRenderBuffer()}; + mColorAttachments.push_back(attachment); + return true; + } + + 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 || + _renderBuffer->getInternalFormat() == GL_DEPTH_STENCIL_OES) + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext()); + #else + if( _renderBuffer->getInternalFormat() == GL_DEPTH24_STENCIL8 || + _renderBuffer->getInternalFormat() == GL_DEPTH_STENCIL) + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext()); + #endif + mDepthAttachment.renderBuffer = _renderBuffer; + return true; + } + + 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 || + _texture->getInternalFormat() == GL_DEPTH_STENCIL_OES) + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0); + #else + if( _texture->getInternalFormat() == GL_DEPTH24_STENCIL8 || + _texture->getInternalFormat() == GL_DEPTH_STENCIL) + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0); + #endif + mDepthAttachment.texture = _texture; + return true; + } + + inline bool isFrameBufferComplete(void) + { + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + { + Utils::error() << "Failed to make complete FrameBuffer object: " << (glCheckFramebufferStatus(GL_FRAMEBUFFER)) << std::endl; + return false; + } + return true; + } + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +protected: + void clean(void); + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLuint mContext; + GLsizei mWidth; + GLsizei mHeight; + GLsizei mDrawBuffers; + AttachmentVec mColorAttachments; + Attachment mDepthAttachment; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_FRAMEBUFFER_HH diff --git a/include/ACGL/Resource/FrameBufferControl.hh b/include/ACGL/Resource/FrameBufferControl.hh index c4f1180a47fcbaaa18f1179baf1f43bdb424227a..ccc09bc69827cd40c54849344a43de1d37e7d7ae 100644 --- a/include/ACGL/Resource/FrameBufferControl.hh +++ b/include/ACGL/Resource/FrameBufferControl.hh @@ -3,73 +3,73 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_FRAMEBUFFERCONTROL_HH -#define ACGL_RESOURCE_FRAMEBUFFERCONTROL_HH - -#include <ACGL/Resource/BasicController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/FrameBuffer.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class FrameBufferControl : public BasicController<FrameBuffer> -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - FrameBufferControl(void) : - mWidth(0), - mHeight(0), - mColorAttachments(), - mDepthAttachment() - { - mDepthAttachment.name = ""; - mDepthAttachment.texture = SharedTexture(); - mDepthAttachment.renderBuffer = SharedRenderBuffer(); - } - virtual ~FrameBufferControl() {} - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - inline FrameBufferControl& size (GLsizei _width, GLsizei _height) { mWidth = _width; mHeight = _height; return *this; } - inline FrameBufferControl& depthTexture (const SharedTexture& _pDepthTexture) { mDepthAttachment.texture = _pDepthTexture; return *this; } - inline FrameBufferControl& depthRenderBuffer (const SharedRenderBuffer& _pDepthRenderBuffer) { mDepthAttachment.renderBuffer = _pDepthRenderBuffer; return *this; } - - inline FrameBufferControl& colorTexture(const SharedTexture& _texture) - { - FrameBuffer::Attachment attachment = {"", _texture, SharedRenderBuffer()}; - mColorAttachments.push_back(attachment); - return *this; - } - inline FrameBufferControl& colorRenderBuffer(const SharedRenderBuffer& _renderBuffer) - { - FrameBuffer::Attachment attachment = {"", SharedTexture(), _renderBuffer}; - mColorAttachments.push_back(attachment); - return *this; - } - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedFrameBuffer create (void); - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLsizei mWidth; - GLsizei mHeight; - FrameBuffer::AttachmentVec mColorAttachments; - FrameBuffer::Attachment mDepthAttachment; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_FRAMEBUFFERCONTROL_HH +#define ACGL_RESOURCE_FRAMEBUFFERCONTROL_HH + +#include <ACGL/Resource/BasicController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/FrameBuffer.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class FrameBufferControl : public BasicController<FrameBuffer> +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + FrameBufferControl(void) : + mWidth(0), + mHeight(0), + mColorAttachments(), + mDepthAttachment() + { + mDepthAttachment.name = ""; + mDepthAttachment.texture = SharedTexture(); + mDepthAttachment.renderBuffer = SharedRenderBuffer(); + } + virtual ~FrameBufferControl() {} + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + inline FrameBufferControl& size (GLsizei _width, GLsizei _height) { mWidth = _width; mHeight = _height; return *this; } + inline FrameBufferControl& depthTexture (const SharedTexture& _pDepthTexture) { mDepthAttachment.texture = _pDepthTexture; return *this; } + inline FrameBufferControl& depthRenderBuffer (const SharedRenderBuffer& _pDepthRenderBuffer) { mDepthAttachment.renderBuffer = _pDepthRenderBuffer; return *this; } + + inline FrameBufferControl& colorTexture(const SharedTexture& _texture) + { + FrameBuffer::Attachment attachment = {"", _texture, SharedRenderBuffer()}; + mColorAttachments.push_back(attachment); + return *this; + } + inline FrameBufferControl& colorRenderBuffer(const SharedRenderBuffer& _renderBuffer) + { + FrameBuffer::Attachment attachment = {"", SharedTexture(), _renderBuffer}; + mColorAttachments.push_back(attachment); + return *this; + } + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedFrameBuffer create (void); + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLsizei mWidth; + GLsizei mHeight; + FrameBuffer::AttachmentVec mColorAttachments; + FrameBuffer::Attachment mDepthAttachment; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/NameManager.hh b/include/ACGL/Resource/NameManager.hh index 95c0d43e3ace4647a19a40158d47dde215357149..f6158470204151cac69845ca335a2ccbbae13e17 100644 --- a/include/ACGL/Resource/NameManager.hh +++ b/include/ACGL/Resource/NameManager.hh @@ -3,114 +3,114 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_NAMEMANAGER_HH -#define ACGL_RESOURCE_NAMEMANAGER_HH - -#include <ACGL/Types.hh> -#include <ACGL/Base/Singleton.hh> -#include <ACGL/Utils/Log.hh> - -#include <ACGL/Resource/BasicController.hh> - -#include <tr1/memory> -#include <map> -#include <string> - -namespace ACGL{ -namespace Resource{ - -template<typename RESOURCE> -class NameManager : public Base::Singleton< NameManager<RESOURCE> > -{ - friend class Base::Singleton< NameManager<RESOURCE> >; - -public: - typedef std::tr1::shared_ptr<RESOURCE> SharedResource; - typedef std::map<std::string, SharedResource> ResourceMap; - typedef ResourceMap ResourceContainer; - - virtual ~NameManager(void) {} - - SharedResource get(const std::string& _key, BasicController<RESOURCE>& _controller); - SharedResource query(const std::string& _key); - bool exists(const std::string& _key); - bool deleteResource(const std::string& key); - void clearAllResources(void); - - typename ResourceMap::const_iterator begin(void) const { return mResourceMap.begin(); } - typename ResourceMap::const_iterator end(void) const { return mResourceMap.end(); } - -protected: - NameManager(void) - : mResourceMap() - {} -private: - NameManager(const NameManager&) {} - -private: - ResourceMap mResourceMap; -}; - -template<typename RESOURCE> -typename NameManager<RESOURCE>::SharedResource NameManager<RESOURCE>::get(const std::string& _key, BasicController<RESOURCE>& _controller) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_key); - if(existingResource != mResourceMap.end()) - return existingResource->second; - - SharedResource pResource = _controller.create(); - if(pResource) - { - mResourceMap[_key] = pResource; - Utils::debug() << "NameManager::getResource: Resource loaded: " << _key << std::endl; - return pResource; - } - else - { - Utils::warning() << "NameManager::getResource: Resource could not be loaded: " << _key << std::endl; - } - return SharedResource(); -} - -template<typename RESOURCE> -typename NameManager<RESOURCE>::SharedResource NameManager<RESOURCE>::query(const std::string& _key) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_key); - if(existingResource != mResourceMap.end()) - return existingResource->second; - return SharedResource(); -} - -template<typename RESOURCE> -bool NameManager<RESOURCE>::exists(const std::string& _key) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_key); - if(existingResource != mResourceMap.end()) - return true; - return false; -} - -template<typename RESOURCE> -bool NameManager<RESOURCE>::deleteResource(const std::string& _key) -{ - typename ResourceMap::iterator existingResource = mResourceMap.find(_key); - if(existingResource != mResourceMap.end()) - { - mResourceMap.erase(existingResource); - Utils::debug() << "NameManager::Resource deleted: " << _key << std::endl; - return true; - } - Utils::warning() << "NameManager::Resource not found for deletion! " << std::endl; - return false; -} - -template<typename RESOURCE> -void NameManager<RESOURCE>::clearAllResources(void) -{ - mResourceMap.clear(); -} - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_NAMEMANAGER_HH +#ifndef ACGL_RESOURCE_NAMEMANAGER_HH +#define ACGL_RESOURCE_NAMEMANAGER_HH + +#include <ACGL/Types.hh> +#include <ACGL/Base/Singleton.hh> +#include <ACGL/Utils/Log.hh> + +#include <ACGL/Resource/BasicController.hh> + +#include <tr1/memory> +#include <map> +#include <string> + +namespace ACGL{ +namespace Resource{ + +template<typename RESOURCE> +class NameManager : public Base::Singleton< NameManager<RESOURCE> > +{ + friend class Base::Singleton< NameManager<RESOURCE> >; + +public: + typedef std::tr1::shared_ptr<RESOURCE> SharedResource; + typedef std::map<std::string, SharedResource> ResourceMap; + typedef ResourceMap ResourceContainer; + + virtual ~NameManager(void) {} + + SharedResource get(const std::string& _key, BasicController<RESOURCE>& _controller); + SharedResource query(const std::string& _key); + bool exists(const std::string& _key); + bool deleteResource(const std::string& key); + void clearAllResources(void); + + typename ResourceMap::const_iterator begin(void) const { return mResourceMap.begin(); } + typename ResourceMap::const_iterator end(void) const { return mResourceMap.end(); } + +protected: + NameManager(void) + : mResourceMap() + {} +private: + NameManager(const NameManager&) {} + +private: + ResourceMap mResourceMap; +}; + +template<typename RESOURCE> +typename NameManager<RESOURCE>::SharedResource NameManager<RESOURCE>::get(const std::string& _key, BasicController<RESOURCE>& _controller) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_key); + if(existingResource != mResourceMap.end()) + return existingResource->second; + + SharedResource pResource = _controller.create(); + if(pResource) + { + mResourceMap[_key] = pResource; + Utils::debug() << "NameManager::getResource: Resource loaded: " << _key << std::endl; + return pResource; + } + else + { + Utils::warning() << "NameManager::getResource: Resource could not be loaded: " << _key << std::endl; + } + return SharedResource(); +} + +template<typename RESOURCE> +typename NameManager<RESOURCE>::SharedResource NameManager<RESOURCE>::query(const std::string& _key) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_key); + if(existingResource != mResourceMap.end()) + return existingResource->second; + return SharedResource(); +} + +template<typename RESOURCE> +bool NameManager<RESOURCE>::exists(const std::string& _key) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_key); + if(existingResource != mResourceMap.end()) + return true; + return false; +} + +template<typename RESOURCE> +bool NameManager<RESOURCE>::deleteResource(const std::string& _key) +{ + typename ResourceMap::iterator existingResource = mResourceMap.find(_key); + if(existingResource != mResourceMap.end()) + { + mResourceMap.erase(existingResource); + Utils::debug() << "NameManager::Resource deleted: " << _key << std::endl; + return true; + } + Utils::warning() << "NameManager::Resource not found for deletion! " << std::endl; + return false; +} + +template<typename RESOURCE> +void NameManager<RESOURCE>::clearAllResources(void) +{ + mResourceMap.clear(); +} + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_NAMEMANAGER_HH diff --git a/include/ACGL/Resource/RenderBuffer.hh b/include/ACGL/Resource/RenderBuffer.hh index 09a41f7f74aee162253d8319f7893c1c5bb3fd84..f3276ad2bf5b2ef9ef7f560ba216cd90f8dd62c4 100644 --- a/include/ACGL/Resource/RenderBuffer.hh +++ b/include/ACGL/Resource/RenderBuffer.hh @@ -3,61 +3,61 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_RENDERBUFFER_HH -#define ACGL_RESOURCE_RENDERBUFFER_HH - -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class RenderBuffer -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - RenderBuffer( - GLsizei _width, - GLsizei _height, - GLenum _internalFormat) - : mContext(0), - mInternalFormat(_internalFormat), - mWidth(_width), - mHeight(_height) - { - glGenRenderbuffers(1, &mContext); - glBindRenderbuffer(GL_RENDERBUFFER, mContext); - - glRenderbufferStorage(GL_RENDERBUFFER, mInternalFormat, mWidth, mHeight); - } - - virtual ~RenderBuffer(void) - { - if(mContext != 0) - glDeleteRenderbuffers(1, &mContext); - } - - // ==================================================================================================== \/ - // ============================================================================================ GETTERS \/ - // ==================================================================================================== \/ -public: - inline GLuint getContext (void) const { return mContext; } - inline GLenum getInternalFormat (void) const { return mInternalFormat; } - inline GLsizei getWidth (void) const { return mWidth; } - inline GLsizei getHeight (void) const { return mHeight; } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLuint mContext; - GLenum mInternalFormat; - GLsizei mWidth; - GLsizei mHeight; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_FRAMEBUFFER_HH +#ifndef ACGL_RESOURCE_RENDERBUFFER_HH +#define ACGL_RESOURCE_RENDERBUFFER_HH + +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class RenderBuffer +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + RenderBuffer( + GLsizei _width, + GLsizei _height, + GLenum _internalFormat) + : mContext(0), + mInternalFormat(_internalFormat), + mWidth(_width), + mHeight(_height) + { + glGenRenderbuffers(1, &mContext); + glBindRenderbuffer(GL_RENDERBUFFER, mContext); + + glRenderbufferStorage(GL_RENDERBUFFER, mInternalFormat, mWidth, mHeight); + } + + virtual ~RenderBuffer(void) + { + if(mContext != 0) + glDeleteRenderbuffers(1, &mContext); + } + + // ==================================================================================================== \/ + // ============================================================================================ GETTERS \/ + // ==================================================================================================== \/ +public: + inline GLuint getContext (void) const { return mContext; } + inline GLenum getInternalFormat (void) const { return mInternalFormat; } + inline GLsizei getWidth (void) const { return mWidth; } + inline GLsizei getHeight (void) const { return mHeight; } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLuint mContext; + GLenum mInternalFormat; + GLsizei mWidth; + GLsizei mHeight; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_FRAMEBUFFER_HH diff --git a/include/ACGL/Resource/RenderBufferControl.hh b/include/ACGL/Resource/RenderBufferControl.hh index 3f864337cafc2854217825c14862d6f10a63551c..66aa7af9e502c2aab78818f92a95c47f16adad12 100644 --- a/include/ACGL/Resource/RenderBufferControl.hh +++ b/include/ACGL/Resource/RenderBufferControl.hh @@ -3,59 +3,59 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_RENDERBUFFERCONTROL_HH -#define ACGL_RESOURCE_RENDERBUFFERCONTROL_HH - -#include <ACGL/Resource/BasicController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/RenderBuffer.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class RenderBufferControl : public BasicController<RenderBuffer> -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - RenderBufferControl(void) : - mWidth(0), - mHeight(0), - mInternalFormat() - {} - virtual ~RenderBufferControl() {} - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - inline RenderBufferControl& size (GLsizei _width, GLsizei _height) { mWidth = _width; mHeight = _height; return *this; } - inline RenderBufferControl& internalFormat (GLenum _internalFormat) { mInternalFormat = _internalFormat; return *this; } - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedRenderBuffer create(void) - { - return SharedRenderBuffer(new RenderBuffer( - mWidth, - mHeight, - mInternalFormat)); - } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLsizei mWidth; - GLsizei mHeight; - GLenum mInternalFormat; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_RENDERBUFFERCONTROL_HH +#define ACGL_RESOURCE_RENDERBUFFERCONTROL_HH + +#include <ACGL/Resource/BasicController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/RenderBuffer.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class RenderBufferControl : public BasicController<RenderBuffer> +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + RenderBufferControl(void) : + mWidth(0), + mHeight(0), + mInternalFormat() + {} + virtual ~RenderBufferControl() {} + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + inline RenderBufferControl& size (GLsizei _width, GLsizei _height) { mWidth = _width; mHeight = _height; return *this; } + inline RenderBufferControl& internalFormat (GLenum _internalFormat) { mInternalFormat = _internalFormat; return *this; } + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedRenderBuffer create(void) + { + return SharedRenderBuffer(new RenderBuffer( + mWidth, + mHeight, + mInternalFormat)); + } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLsizei mWidth; + GLsizei mHeight; + GLenum mInternalFormat; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/Shader.hh b/include/ACGL/Resource/Shader.hh index b69c7e53cb6ea71416f5240163afa7169beb4662..6ab86044c1678ce0949750090b778fd9d23908a9 100644 --- a/include/ACGL/Resource/Shader.hh +++ b/include/ACGL/Resource/Shader.hh @@ -1,88 +1,88 @@ -//////////////////////////////////////////////////////////////////////////////// -// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // -// All rights reserved. // -//////////////////////////////////////////////////////////////////////////////// - -#ifndef ACGL_RESOURCE_SHADER_HH -#define ACGL_RESOURCE_SHADER_HH - -#include <vector> -#include <string> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class Shader -{ - // ===================================================================================================== \/ - // ============================================================================================ TYPEDEFS \/ - // ===================================================================================================== \/ -public: - typedef std::vector<std::string> AttributeVec; - // ================================================================================================== \/ - // ============================================================================================ ENUMS \/ - // ================================================================================================== \/ -public: - //! Different types of a shader (i.e. vertex or fragment shaders) - enum ShaderType - { - ShaderTypeInvalid = 0, - ShaderTypeVertex = GL_VERTEX_SHADER, - ShaderTypeFragment = GL_FRAGMENT_SHADER, -#ifndef OPENGL_ES - ShaderTypeGeometry = GL_GEOMETRY_SHADER, - ShaderTypeControl = GL_TESS_CONTROL_SHADER, - ShaderTypeEvaluation = GL_TESS_EVALUATION_SHADER, -#endif - }; - - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - Shader(GLenum _type) - : mContext(0), - mType(_type) - { - mContext = glCreateShader(mType); - } - - virtual ~Shader(void) - { - // "DeleteShader will silently ignore the value zero." - GL Spec - glDeleteShader(mContext); - } - - // ==================================================================================================== \/ - // ============================================================================================ GETTERS \/ - // ==================================================================================================== \/ -public: - inline GLuint getContext (void) const { return mContext; } - inline GLenum getType (void) const { return mType; } - - inline const std::vector<std::string>& getAttributes() const { return mAttributes; } - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - bool setFromFile (const std::string &_filename); - bool setSource (const std::string &_source ); - -protected: - bool compile (const char *_pProgramText) const; - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLuint mContext; - GLenum mType; - AttributeVec mAttributes; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_SHADER_HH +//////////////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // +// All rights reserved. // +//////////////////////////////////////////////////////////////////////////////// + +#ifndef ACGL_RESOURCE_SHADER_HH +#define ACGL_RESOURCE_SHADER_HH + +#include <vector> +#include <string> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class Shader +{ + // ===================================================================================================== \/ + // ============================================================================================ TYPEDEFS \/ + // ===================================================================================================== \/ +public: + typedef std::vector<std::string> AttributeVec; + // ================================================================================================== \/ + // ============================================================================================ ENUMS \/ + // ================================================================================================== \/ +public: + //! Different types of a shader (i.e. vertex or fragment shaders) + enum ShaderType + { + ShaderTypeInvalid = 0, + ShaderTypeVertex = GL_VERTEX_SHADER, + ShaderTypeFragment = GL_FRAGMENT_SHADER, +#ifndef OPENGL_ES + ShaderTypeGeometry = GL_GEOMETRY_SHADER, + ShaderTypeControl = GL_TESS_CONTROL_SHADER, + ShaderTypeEvaluation = GL_TESS_EVALUATION_SHADER, +#endif + }; + + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + Shader(GLenum _type) + : mContext(0), + mType(_type) + { + mContext = glCreateShader(mType); + } + + virtual ~Shader(void) + { + // "DeleteShader will silently ignore the value zero." - GL Spec + glDeleteShader(mContext); + } + + // ==================================================================================================== \/ + // ============================================================================================ GETTERS \/ + // ==================================================================================================== \/ +public: + inline GLuint getContext (void) const { return mContext; } + inline GLenum getType (void) const { return mType; } + + inline const std::vector<std::string>& getAttributes() const { return mAttributes; } + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + bool setFromFile (const std::string &_filename); + bool setSource (const std::string &_source ); + +protected: + bool compile (const char *_pProgramText) const; + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLuint mContext; + GLenum mType; + AttributeVec mAttributes; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_SHADER_HH diff --git a/include/ACGL/Resource/ShaderControlFile.hh b/include/ACGL/Resource/ShaderControlFile.hh index 2716965480caf2ac2c9bf640017f219cfee21084..de61fb7a55bb4c394615ed5ad8b7da13f11017ed 100644 --- a/include/ACGL/Resource/ShaderControlFile.hh +++ b/include/ACGL/Resource/ShaderControlFile.hh @@ -3,51 +3,51 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_SHADERCONTROLFILE_HH -#define ACGL_RESOURCE_SHADERCONTROLFILE_HH - -#include <ACGL/Resource/FileController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/Shader.hh> -#include <ACGL/Base/Settings.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class ShaderControlFile : public FileController<Shader> -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - ShaderControlFile(const std::string& _filename) - : FileController<Shader>(_filename, Base::Settings::the()->getFullShaderPath()), - mType(Shader::ShaderTypeInvalid) - {} - virtual ~ShaderControlFile(void) {} - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - inline ShaderControlFile& type (GLenum _type) { mType = _type; return *this; } - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedShader create(void); - virtual bool update(SharedShader& shader); - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLenum mType; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_SHADERCONTROLFILE_HH +#define ACGL_RESOURCE_SHADERCONTROLFILE_HH + +#include <ACGL/Resource/FileController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/Shader.hh> +#include <ACGL/Base/Settings.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class ShaderControlFile : public FileController<Shader> +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + ShaderControlFile(const std::string& _filename) + : FileController<Shader>(_filename, Base::Settings::the()->getFullShaderPath()), + mType(Shader::ShaderTypeInvalid) + {} + virtual ~ShaderControlFile(void) {} + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + inline ShaderControlFile& type (GLenum _type) { mType = _type; return *this; } + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedShader create(void); + virtual bool update(SharedShader& shader); + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLenum mType; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/ShaderProgram.hh b/include/ACGL/Resource/ShaderProgram.hh index f78a02c16f71a9dc876c49ec7ed900fa4ffff25a..63c8e70f5ed41660f494d7b11939e4ccf3c423d2 100644 --- a/include/ACGL/Resource/ShaderProgram.hh +++ b/include/ACGL/Resource/ShaderProgram.hh @@ -1,120 +1,120 @@ -//////////////////////////////////////////////////////////////////////////////// -// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // -// All rights reserved. // -//////////////////////////////////////////////////////////////////////////////// - -#ifndef ACGL_RESOURCE_SHADERPROGRAM_HH -#define ACGL_RESOURCE_SHADERPROGRAM_HH - -#include "ResourceTypes.hh" -#include "Shader.hh" -#include "Texture.hh" -#include <ACGL/Math/Math.hh> -#include <ACGL/Types.hh> -#include <ACGL/GL.hh> - -#include <vector> - -namespace ACGL{ -namespace Resource{ - -class ShaderProgram -{ - // ===================================================================================================== \/ - // ============================================================================================ TYPEDEFS \/ - // ===================================================================================================== \/ -public: - typedef std::vector< SharedShader > SharedShaderVec; - - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - ShaderProgram(void) - : mContext(0), - mShaders() - { - mContext = glCreateProgram(); - } - - virtual ~ShaderProgram(void) - { - for(SharedShaderVec::size_type i = 0; i < mShaders.size(); ++i) { - if(mShaders[i]) { - // we have to detach all shaders, otherwise the attached shaders will get deleted by - // OpenGL: "When a program object is deleted, all shader objects attached to it are detached" - // - GL Spec - glDetachShader(mContext, mShaders[i]->getContext()); - } - } - - // "DeleteProgram will silently ignore the value zero." - GL Spec - glDeleteProgram(mContext); - } - - // ==================================================================================================== \/ - // ============================================================================================ GETTERS \/ - // ==================================================================================================== \/ -public: - inline GLuint getContext (void) const { return mContext; } - inline const SharedShaderVec& getShaders (void) const { return mShaders; } - - // ===================================================================================================== \/ - // ============================================================================================ WRAPPERS \/ - // ===================================================================================================== \/ -public: - 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 void attachShader(const SharedShader& _shader) - { - mShaders.push_back(_shader); - glAttachShader(mContext, _shader->getContext()); - } - - bool link (void) const; - - inline void setUniform (GLint _location, GLint _v) const { glUniform1i (_location, _v); } - inline void setUniform (GLint _location, GLfloat _v) const { glUniform1f (_location, _v); } - inline void setUniform (GLint _location, const glm::vec2& _v) const { glUniform2fv(_location, 1, glm::value_ptr(_v)); } - inline void setUniform (GLint _location, const glm::vec3& _v) const { glUniform3fv(_location, 1, glm::value_ptr(_v)); } - inline void setUniform (GLint _location, const glm::vec4& _v) const { glUniform4fv(_location, 1, glm::value_ptr(_v)); } - inline void setUniform (GLint _location, const glm::mat2& _v) const { glUniformMatrix2fv(_location, 1, GL_FALSE, glm::value_ptr(_v)); } - inline void setUniform (GLint _location, const glm::mat3& _v) const { glUniformMatrix3fv(_location, 1, GL_FALSE, glm::value_ptr(_v)); } - inline void setUniform (GLint _location, const glm::mat4& _v) const { glUniformMatrix4fv(_location, 1, GL_FALSE, glm::value_ptr(_v)); } - - inline void setTexture (GLint _location, const SharedTexture& _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 \/ - // =================================================================================================== \/ - - inline void setUniform (const std::string& _nameInShader, GLint _v) const { glUniform1i (getUniformLocation(_nameInShader), _v); } - inline void setUniform (const std::string& _nameInShader, GLfloat _v) const { glUniform1f (getUniformLocation(_nameInShader), _v); } - inline void setUniform (const std::string& _nameInShader, const glm::vec2& _v) const { glUniform2fv(getUniformLocation(_nameInShader), 1, glm::value_ptr(_v)); } - inline void setUniform (const std::string& _nameInShader, const glm::vec3& _v) const { glUniform3fv(getUniformLocation(_nameInShader), 1, glm::value_ptr(_v)); } - inline void setUniform (const std::string& _nameInShader, const glm::vec4& _v) const { glUniform4fv(getUniformLocation(_nameInShader), 1, glm::value_ptr(_v)); } - inline void setUniform (const std::string& _nameInShader, const glm::mat2& _v) const { glUniformMatrix2fv(getUniformLocation(_nameInShader), 1, GL_FALSE, glm::value_ptr(_v)); } - inline void setUniform (const std::string& _nameInShader, const glm::mat3& _v) const { glUniformMatrix3fv(getUniformLocation(_nameInShader), 1, GL_FALSE, glm::value_ptr(_v)); } - inline void setUniform (const std::string& _nameInShader, const glm::mat4& _v) const { glUniformMatrix4fv(getUniformLocation(_nameInShader), 1, GL_FALSE, glm::value_ptr(_v)); } - - inline void setTexture (const std::string& _nameInShader, const SharedTexture& _texture, GLenum _unit = 0) const { glUniform1i(getUniformLocation(_nameInShader), _unit); _texture->bind(_unit); } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - void bindAttributeLocations() const; - - GLuint mContext; - SharedShaderVec mShaders; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_SHADERPROGRAM_HH +//////////////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // +// All rights reserved. // +//////////////////////////////////////////////////////////////////////////////// + +#ifndef ACGL_RESOURCE_SHADERPROGRAM_HH +#define ACGL_RESOURCE_SHADERPROGRAM_HH + +#include "ResourceTypes.hh" +#include "Shader.hh" +#include "Texture.hh" +#include <ACGL/Math/Math.hh> +#include <ACGL/Types.hh> +#include <ACGL/GL.hh> + +#include <vector> + +namespace ACGL{ +namespace Resource{ + +class ShaderProgram +{ + // ===================================================================================================== \/ + // ============================================================================================ TYPEDEFS \/ + // ===================================================================================================== \/ +public: + typedef std::vector< SharedShader > SharedShaderVec; + + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + ShaderProgram(void) + : mContext(0), + mShaders() + { + mContext = glCreateProgram(); + } + + virtual ~ShaderProgram(void) + { + for(SharedShaderVec::size_type i = 0; i < mShaders.size(); ++i) { + if(mShaders[i]) { + // we have to detach all shaders, otherwise the attached shaders will get deleted by + // OpenGL: "When a program object is deleted, all shader objects attached to it are detached" + // - GL Spec + glDetachShader(mContext, mShaders[i]->getContext()); + } + } + + // "DeleteProgram will silently ignore the value zero." - GL Spec + glDeleteProgram(mContext); + } + + // ==================================================================================================== \/ + // ============================================================================================ GETTERS \/ + // ==================================================================================================== \/ +public: + inline GLuint getContext (void) const { return mContext; } + inline const SharedShaderVec& getShaders (void) const { return mShaders; } + + // ===================================================================================================== \/ + // ============================================================================================ WRAPPERS \/ + // ===================================================================================================== \/ +public: + 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 void attachShader(const SharedShader& _shader) + { + mShaders.push_back(_shader); + glAttachShader(mContext, _shader->getContext()); + } + + bool link (void) const; + + inline void setUniform (GLint _location, GLint _v) const { glUniform1i (_location, _v); } + inline void setUniform (GLint _location, GLfloat _v) const { glUniform1f (_location, _v); } + inline void setUniform (GLint _location, const glm::vec2& _v) const { glUniform2fv(_location, 1, glm::value_ptr(_v)); } + inline void setUniform (GLint _location, const glm::vec3& _v) const { glUniform3fv(_location, 1, glm::value_ptr(_v)); } + inline void setUniform (GLint _location, const glm::vec4& _v) const { glUniform4fv(_location, 1, glm::value_ptr(_v)); } + inline void setUniform (GLint _location, const glm::mat2& _v) const { glUniformMatrix2fv(_location, 1, GL_FALSE, glm::value_ptr(_v)); } + inline void setUniform (GLint _location, const glm::mat3& _v) const { glUniformMatrix3fv(_location, 1, GL_FALSE, glm::value_ptr(_v)); } + inline void setUniform (GLint _location, const glm::mat4& _v) const { glUniformMatrix4fv(_location, 1, GL_FALSE, glm::value_ptr(_v)); } + + inline void setTexture (GLint _location, const SharedTexture& _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 \/ + // =================================================================================================== \/ + + inline void setUniform (const std::string& _nameInShader, GLint _v) const { glUniform1i (getUniformLocation(_nameInShader), _v); } + inline void setUniform (const std::string& _nameInShader, GLfloat _v) const { glUniform1f (getUniformLocation(_nameInShader), _v); } + inline void setUniform (const std::string& _nameInShader, const glm::vec2& _v) const { glUniform2fv(getUniformLocation(_nameInShader), 1, glm::value_ptr(_v)); } + inline void setUniform (const std::string& _nameInShader, const glm::vec3& _v) const { glUniform3fv(getUniformLocation(_nameInShader), 1, glm::value_ptr(_v)); } + inline void setUniform (const std::string& _nameInShader, const glm::vec4& _v) const { glUniform4fv(getUniformLocation(_nameInShader), 1, glm::value_ptr(_v)); } + inline void setUniform (const std::string& _nameInShader, const glm::mat2& _v) const { glUniformMatrix2fv(getUniformLocation(_nameInShader), 1, GL_FALSE, glm::value_ptr(_v)); } + inline void setUniform (const std::string& _nameInShader, const glm::mat3& _v) const { glUniformMatrix3fv(getUniformLocation(_nameInShader), 1, GL_FALSE, glm::value_ptr(_v)); } + inline void setUniform (const std::string& _nameInShader, const glm::mat4& _v) const { glUniformMatrix4fv(getUniformLocation(_nameInShader), 1, GL_FALSE, glm::value_ptr(_v)); } + + inline void setTexture (const std::string& _nameInShader, const SharedTexture& _texture, GLenum _unit = 0) const { glUniform1i(getUniformLocation(_nameInShader), _unit); _texture->bind(_unit); } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + void bindAttributeLocations() const; + + GLuint mContext; + SharedShaderVec mShaders; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_SHADERPROGRAM_HH diff --git a/include/ACGL/Resource/ShaderProgramControlAutoFiles.hh b/include/ACGL/Resource/ShaderProgramControlAutoFiles.hh index dc9f9359f1d6b94208a9588a2f1e72106dc70e9a..1c3c35408ae50a714dad5a94aa77edb1888237a0 100644 --- a/include/ACGL/Resource/ShaderProgramControlAutoFiles.hh +++ b/include/ACGL/Resource/ShaderProgramControlAutoFiles.hh @@ -3,38 +3,38 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_SHADERPROGRAMCONTROLAUTOFILES_HH -#define ACGL_RESOURCE_SHADERPROGRAMCONTROLAUTOFILES_HH - -#include <ACGL/Resource/FileController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/ShaderProgram.hh> - -#include <vector> - -namespace ACGL{ -namespace Resource{ - -class ShaderProgramControlAutoFiles : public FileController<ShaderProgram> -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - ShaderProgramControlAutoFiles(const std::string& _filename) - : FileController<ShaderProgram>(_filename) - {} - virtual ~ShaderProgramControlAutoFiles(void) {} - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedShaderProgram create(void); - virtual bool update(SharedShaderProgram& shaderProgram); -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_SHADERPROGRAMCONTROLAUTOFILES_HH +#define ACGL_RESOURCE_SHADERPROGRAMCONTROLAUTOFILES_HH + +#include <ACGL/Resource/FileController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/ShaderProgram.hh> + +#include <vector> + +namespace ACGL{ +namespace Resource{ + +class ShaderProgramControlAutoFiles : public FileController<ShaderProgram> +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + ShaderProgramControlAutoFiles(const std::string& _filename) + : FileController<ShaderProgram>(_filename) + {} + virtual ~ShaderProgramControlAutoFiles(void) {} + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedShaderProgram create(void); + virtual bool update(SharedShaderProgram& shaderProgram); +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/Texture.hh b/include/ACGL/Resource/Texture.hh index 716ad2d34b61b1ebc6ec78766d3cff5358fb2fcd..084796177d5e89303b6b214c6319b2850b23871c 100644 --- a/include/ACGL/Resource/Texture.hh +++ b/include/ACGL/Resource/Texture.hh @@ -3,239 +3,239 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_TEXTURE_HH -#define ACGL_RESOURCE_TEXTURE_HH - -#include <ACGL/Math/Math.hh> -#include <ACGL/GL.hh> - -#include <vector> - -namespace ACGL{ -namespace Resource{ - -class Texture -{ - // ======================================================================================================== \/ - // ============================================================================================ GPU CONTEXT \/ - // ======================================================================================================== \/ -private: - static GLuint sTextureContext; - - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - Texture( - GLenum _internalFormat = GL_RGBA, - GLenum _target = GL_TEXTURE_2D, - GLenum _format = GL_RGBA, - GLenum _type = GL_UNSIGNED_BYTE) - : mContext(0), - mWidth(0), - mHeight(0), - mDepth(0), - mInternalFormat(_internalFormat), - mTarget(_target), - mFormat(_format), - mType(_type) - { - glGenTextures(1, &mContext); - } - - virtual ~Texture(void) - { - // context 0 will get ignored by OpenGL - glDeleteTextures(1, &mContext); - mContext = 0; - } - - // ==================================================================================================== \/ - // ============================================================================================ GETTERS \/ - // ==================================================================================================== \/ -public: - inline GLuint getContext (void) const { return mContext; } - inline GLsizei getWidth (void) const { return mWidth; } - inline GLsizei getHeight (void) const { return mHeight; } - inline GLsizei getDepth (void) const { return mDepth; } - inline GLenum getInternalFormat (void) const { return mInternalFormat; } - inline GLenum getTarget (void) const { return mTarget; } - inline GLenum getFormat (void) const { return mFormat; } - inline GLenum getType (void) const { return mType; } - - // ==================================================================================================== \/ - // ============================================================================================ SETTERS \/ - // ==================================================================================================== \/ -public: - inline void setInternalFormat (GLenum _internalFormat) { mInternalFormat = _internalFormat; } - inline void setTarget (GLenum _target) { mTarget = _target; } - inline void setFormat (GLenum _format) { mFormat = _format; } - inline void setType (GLenum _type) { mType = _type; } - - inline void setSize( - GLsizei _width = 0, - GLsizei _height = 0, - GLsizei _depth = 0) - { - mWidth = _width; - mHeight = _height; - mDepth = _depth; - } - - // ===================================================================================================== \/ - // ============================================================================================ WRAPPERS \/ - // ===================================================================================================== \/ -public: - //! Activate texture unit and bind this texture. - inline void bind(GLenum _textureUnit) const - { - if(sTextureContext == mContext) - return; - glActiveTexture(GL_TEXTURE0 + _textureUnit); - glBindTexture(mTarget, mContext); - sTextureContext = mContext; - } - - //! Bind this texture. - inline void bind(void) const - { - if(sTextureContext == mContext) - return; - glBindTexture(mTarget, mContext); - sTextureContext = mContext; - } - - inline void setParameter(GLenum _parameter, GLint _value) - { - bind(); - glTexParameteri(mTarget, _parameter, _value); - } - - inline void setParameter(GLenum _parameter, GLfloat _value) - { - bind(); - glTexParameterf(mTarget, _parameter, _value); - } - - //! Set texture size for 1D, 2D or 3D textures and NULL data - inline void setRenderTarget( - GLsizei _width, - GLsizei _height = 0, - GLsizei _depth = 0) - { - bind(); - mWidth = _width; - mHeight = _height; - mDepth = _depth; - if(mWidth > 0 && mHeight > 0 && mDepth > 0) - { - //3D - } - else if(mWidth > 0 && mHeight > 0) - { - glTexImage2D( - mTarget, - 0, - mInternalFormat, - mWidth, - mHeight, - 0,//no border - mFormat, - mType, - NULL); - } - else if(mWidth > 0) - { - //1D - } - } - - //! Set texture size and NULL data - inline void setRenderTarget2D( - GLsizei _width, - GLsizei _height) - { - bind(); - mWidth = _width; - mHeight = _height; - glTexImage2D( - mTarget, - 0, - mInternalFormat, - mWidth, - mHeight, - 0,//no border - mFormat, - mType, - NULL); - } - - //! Set texture data for some mipmap level. - /*! - If you upload a texture for a higher mipmap level, the function will automatically determine - its size calculating (w,h)/2^level where w and h are the width and height of the base texture. - */ - inline void setTexImage2D( - const GLvoid* _pData) - { - bind(); - glTexImage2D( - mTarget, - 0, - mInternalFormat, - mWidth, - mHeight, - 0,//no border - mFormat, - mType, - _pData); - } - - //! Set texture data for some mipmap level. - /*! - If you upload a texture for a higher mipmap level, the function will automatically determine - its size calculating (w,h)/2^level where w and h are the width and height of the base texture. - */ - inline void setTexImage2D( - const GLvoid* _pData, - GLint _mipmapLevel) - { - bind(); - glTexImage2D( - mTarget, - _mipmapLevel, - mInternalFormat, - mWidth / GLsizei(Math::Functions::pow(2.0f, float(_mipmapLevel))), - mHeight / GLsizei(Math::Functions::pow(2.0f, float(_mipmapLevel))), - 0,//no border - mFormat, - mType, - _pData); - } - - //! Generate mipmaps from the current base texture (i.e. the texture from level 0). - void generateMipmaps(void) const - { - bind(); - glEnable(mTarget); - glGenerateMipmap(mTarget); - } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -private: - GLuint mContext; - GLsizei mWidth; - GLsizei mHeight; - GLsizei mDepth; - GLenum mInternalFormat; - GLenum mTarget; - GLenum mFormat; - GLenum mType; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_TEXTURE_HH +#define ACGL_RESOURCE_TEXTURE_HH + +#include <ACGL/Math/Math.hh> +#include <ACGL/GL.hh> + +#include <vector> + +namespace ACGL{ +namespace Resource{ + +class Texture +{ + // ======================================================================================================== \/ + // ============================================================================================ GPU CONTEXT \/ + // ======================================================================================================== \/ +private: + static GLuint sTextureContext; + + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + Texture( + GLenum _internalFormat = GL_RGBA, + GLenum _target = GL_TEXTURE_2D, + GLenum _format = GL_RGBA, + GLenum _type = GL_UNSIGNED_BYTE) + : mContext(0), + mWidth(0), + mHeight(0), + mDepth(0), + mInternalFormat(_internalFormat), + mTarget(_target), + mFormat(_format), + mType(_type) + { + glGenTextures(1, &mContext); + } + + virtual ~Texture(void) + { + // context 0 will get ignored by OpenGL + glDeleteTextures(1, &mContext); + mContext = 0; + } + + // ==================================================================================================== \/ + // ============================================================================================ GETTERS \/ + // ==================================================================================================== \/ +public: + inline GLuint getContext (void) const { return mContext; } + inline GLsizei getWidth (void) const { return mWidth; } + inline GLsizei getHeight (void) const { return mHeight; } + inline GLsizei getDepth (void) const { return mDepth; } + inline GLenum getInternalFormat (void) const { return mInternalFormat; } + inline GLenum getTarget (void) const { return mTarget; } + inline GLenum getFormat (void) const { return mFormat; } + inline GLenum getType (void) const { return mType; } + + // ==================================================================================================== \/ + // ============================================================================================ SETTERS \/ + // ==================================================================================================== \/ +public: + inline void setInternalFormat (GLenum _internalFormat) { mInternalFormat = _internalFormat; } + inline void setTarget (GLenum _target) { mTarget = _target; } + inline void setFormat (GLenum _format) { mFormat = _format; } + inline void setType (GLenum _type) { mType = _type; } + + inline void setSize( + GLsizei _width = 0, + GLsizei _height = 0, + GLsizei _depth = 0) + { + mWidth = _width; + mHeight = _height; + mDepth = _depth; + } + + // ===================================================================================================== \/ + // ============================================================================================ WRAPPERS \/ + // ===================================================================================================== \/ +public: + //! Activate texture unit and bind this texture. + inline void bind(GLenum _textureUnit) const + { + if(sTextureContext == mContext) + return; + glActiveTexture(GL_TEXTURE0 + _textureUnit); + glBindTexture(mTarget, mContext); + sTextureContext = mContext; + } + + //! Bind this texture. + inline void bind(void) const + { + if(sTextureContext == mContext) + return; + glBindTexture(mTarget, mContext); + sTextureContext = mContext; + } + + inline void setParameter(GLenum _parameter, GLint _value) + { + bind(); + glTexParameteri(mTarget, _parameter, _value); + } + + inline void setParameter(GLenum _parameter, GLfloat _value) + { + bind(); + glTexParameterf(mTarget, _parameter, _value); + } + + //! Set texture size for 1D, 2D or 3D textures and NULL data + inline void setRenderTarget( + GLsizei _width, + GLsizei _height = 0, + GLsizei _depth = 0) + { + bind(); + mWidth = _width; + mHeight = _height; + mDepth = _depth; + if(mWidth > 0 && mHeight > 0 && mDepth > 0) + { + //3D + } + else if(mWidth > 0 && mHeight > 0) + { + glTexImage2D( + mTarget, + 0, + mInternalFormat, + mWidth, + mHeight, + 0,//no border + mFormat, + mType, + NULL); + } + else if(mWidth > 0) + { + //1D + } + } + + //! Set texture size and NULL data + inline void setRenderTarget2D( + GLsizei _width, + GLsizei _height) + { + bind(); + mWidth = _width; + mHeight = _height; + glTexImage2D( + mTarget, + 0, + mInternalFormat, + mWidth, + mHeight, + 0,//no border + mFormat, + mType, + NULL); + } + + //! Set texture data for some mipmap level. + /*! + If you upload a texture for a higher mipmap level, the function will automatically determine + its size calculating (w,h)/2^level where w and h are the width and height of the base texture. + */ + inline void setTexImage2D( + const GLvoid* _pData) + { + bind(); + glTexImage2D( + mTarget, + 0, + mInternalFormat, + mWidth, + mHeight, + 0,//no border + mFormat, + mType, + _pData); + } + + //! Set texture data for some mipmap level. + /*! + If you upload a texture for a higher mipmap level, the function will automatically determine + its size calculating (w,h)/2^level where w and h are the width and height of the base texture. + */ + inline void setTexImage2D( + const GLvoid* _pData, + GLint _mipmapLevel) + { + bind(); + glTexImage2D( + mTarget, + _mipmapLevel, + mInternalFormat, + mWidth / GLsizei(Math::Functions::pow(2.0f, float(_mipmapLevel))), + mHeight / GLsizei(Math::Functions::pow(2.0f, float(_mipmapLevel))), + 0,//no border + mFormat, + mType, + _pData); + } + + //! Generate mipmaps from the current base texture (i.e. the texture from level 0). + void generateMipmaps(void) const + { + bind(); + glEnable(mTarget); + glGenerateMipmap(mTarget); + } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +private: + GLuint mContext; + GLsizei mWidth; + GLsizei mHeight; + GLsizei mDepth; + GLenum mInternalFormat; + GLenum mTarget; + GLenum mFormat; + GLenum mType; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/TextureControl.hh b/include/ACGL/Resource/TextureControl.hh index ad76a11cd5dc03da9e09cbc943811cca95d007af..34f025d2cb5a1cd3aeb3d60704018c54408c9d14 100644 --- a/include/ACGL/Resource/TextureControl.hh +++ b/include/ACGL/Resource/TextureControl.hh @@ -3,90 +3,90 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_TEXTURECONTROL_HH -#define ACGL_RESOURCE_TEXTURECONTROL_HH - -#include <ACGL/Resource/BasicController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/Texture.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class TextureControl : public BasicController<Texture> -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - TextureControl(void) - : BasicController<Texture>(), - mWidth(0), - mHeight(0), - mDepth(0), - mInternalFormat(GL_RGBA), - mTarget(GL_TEXTURE_2D), - mFormat(GL_RGBA), - mType(GL_UNSIGNED_BYTE), - mMinFilter(0), - mMagFilter(0) - {} - virtual ~TextureControl(void) {} - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - inline TextureControl& size (GLsizei _width, GLsizei _height = 0, GLsizei _depth = 0) - { - mWidth = _width; - mHeight = _height; - mDepth = _depth; - return *this; - } - - inline TextureControl& internalFormat (GLenum _internalFormat) { mInternalFormat = _internalFormat; return *this; } - inline TextureControl& target (GLenum _target) { mTarget = _target; return *this; } - inline TextureControl& format (GLenum _format) { mFormat = _format; return *this; } - inline TextureControl& type (GLenum type) { mType = type; return *this; } - inline TextureControl& minFilter (GLint _minFilter) { mMinFilter = _minFilter; return *this; } - inline TextureControl& magFilter (GLint _magFilter) { mMagFilter = _magFilter; return *this; } - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedTexture create(void) - { - SharedTexture texture(new Texture( - mInternalFormat, - mTarget, - mFormat, - mType)); - texture->bind(); - texture->setParameter(GL_TEXTURE_MIN_FILTER, mMinFilter == 0 ? GL_NEAREST : mMinFilter); - texture->setParameter(GL_TEXTURE_MAG_FILTER, mMagFilter == 0 ? GL_NEAREST : mMagFilter); - texture->setRenderTarget(mWidth, mHeight, mDepth); - return texture; - } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLsizei mWidth; - GLsizei mHeight; - GLsizei mDepth; - GLenum mInternalFormat; - GLenum mTarget; - GLenum mFormat; - GLenum mType; - GLint mMinFilter; - GLint mMagFilter; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_TEXTURECONTROL_HH +#define ACGL_RESOURCE_TEXTURECONTROL_HH + +#include <ACGL/Resource/BasicController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/Texture.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class TextureControl : public BasicController<Texture> +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + TextureControl(void) + : BasicController<Texture>(), + mWidth(0), + mHeight(0), + mDepth(0), + mInternalFormat(GL_RGBA), + mTarget(GL_TEXTURE_2D), + mFormat(GL_RGBA), + mType(GL_UNSIGNED_BYTE), + mMinFilter(0), + mMagFilter(0) + {} + virtual ~TextureControl(void) {} + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + inline TextureControl& size (GLsizei _width, GLsizei _height = 0, GLsizei _depth = 0) + { + mWidth = _width; + mHeight = _height; + mDepth = _depth; + return *this; + } + + inline TextureControl& internalFormat (GLenum _internalFormat) { mInternalFormat = _internalFormat; return *this; } + inline TextureControl& target (GLenum _target) { mTarget = _target; return *this; } + inline TextureControl& format (GLenum _format) { mFormat = _format; return *this; } + inline TextureControl& type (GLenum type) { mType = type; return *this; } + inline TextureControl& minFilter (GLint _minFilter) { mMinFilter = _minFilter; return *this; } + inline TextureControl& magFilter (GLint _magFilter) { mMagFilter = _magFilter; return *this; } + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedTexture create(void) + { + SharedTexture texture(new Texture( + mInternalFormat, + mTarget, + mFormat, + mType)); + texture->bind(); + texture->setParameter(GL_TEXTURE_MIN_FILTER, mMinFilter == 0 ? GL_NEAREST : mMinFilter); + texture->setParameter(GL_TEXTURE_MAG_FILTER, mMagFilter == 0 ? GL_NEAREST : mMagFilter); + texture->setRenderTarget(mWidth, mHeight, mDepth); + return texture; + } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLsizei mWidth; + GLsizei mHeight; + GLsizei mDepth; + GLenum mInternalFormat; + GLenum mTarget; + GLenum mFormat; + GLenum mType; + GLint mMinFilter; + GLint mMagFilter; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/TextureControlFileJPG.hh b/include/ACGL/Resource/TextureControlFileJPG.hh index 0a0b13be4cd6909f8ce3e9402493a6cc69a31174..24080a22c4c0e5dbdcd6fa8400f1912369a5a75e 100644 --- a/include/ACGL/Resource/TextureControlFileJPG.hh +++ b/include/ACGL/Resource/TextureControlFileJPG.hh @@ -3,57 +3,57 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_TEXTURECONTROLFILEJPG_HH -#define ACGL_RESOURCE_TEXTURECONTROLFILEJPG_HH - -#include <ACGL/Resource/FileController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/Texture.hh> -#include <ACGL/Base/Settings.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class TextureControlFileJPG : public FileController<Texture> -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - TextureControlFileJPG(const std::string& _filename) - : FileController<Texture>(_filename, Base::Settings::the()->getFullTexturePath()), - mMinFilter(0), - mMagFilter(0) - {} - virtual ~TextureControlFileJPG(void) {} - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - inline TextureControlFileJPG& minFilter (GLint _minFilter) { mMinFilter = _minFilter; return *this; } - inline TextureControlFileJPG& magFilter (GLint _magFilter) { mMagFilter = _magFilter; return *this; } - -private: - bool loadJPG(SharedTexture& texture); - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedTexture create(void); - virtual bool update(SharedTexture& texture); - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLint mMinFilter; - GLint mMagFilter; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_TEXTURECONTROLFILEJPG_HH +#define ACGL_RESOURCE_TEXTURECONTROLFILEJPG_HH + +#include <ACGL/Resource/FileController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/Texture.hh> +#include <ACGL/Base/Settings.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class TextureControlFileJPG : public FileController<Texture> +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + TextureControlFileJPG(const std::string& _filename) + : FileController<Texture>(_filename, Base::Settings::the()->getFullTexturePath()), + mMinFilter(0), + mMagFilter(0) + {} + virtual ~TextureControlFileJPG(void) {} + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + inline TextureControlFileJPG& minFilter (GLint _minFilter) { mMinFilter = _minFilter; return *this; } + inline TextureControlFileJPG& magFilter (GLint _magFilter) { mMagFilter = _magFilter; return *this; } + +private: + bool loadJPG(SharedTexture& texture); + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedTexture create(void); + virtual bool update(SharedTexture& texture); + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLint mMinFilter; + GLint mMagFilter; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/Uniform.hh b/include/ACGL/Resource/Uniform.hh index ae035983fcccd1494a5eedf5db579d132c4fea5c..9495846eecac705c2ace07d659dac671e2d0feca 100644 --- a/include/ACGL/Resource/Uniform.hh +++ b/include/ACGL/Resource/Uniform.hh @@ -3,89 +3,89 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_UNIFORM_HH -#define ACGL_RESOURCE_UNIFORM_HH - -#include <ACGL/Resource/BasicResource.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Utils/StringOperations.hh> -#include <ACGL/Math/Math.hh> -#include <ACGL/Types.hh> -#include <ACGL/GL.hh> - -#include <vector> - -namespace ACGL{ -namespace Resource{ - -class Uniform : public BasicResource -{ -public: - Uniform(void) : BasicResource() {} - virtual ~Uniform(void) {} - virtual void apply(GLint) const = 0; -}; - -template<typename T> -class UniformData : public Uniform -{ - // ========================================================================================================= \/ - // ============================================================================================ 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(); } - virtual ~UniformData(void) {} - - // ==================================================================================================== \/ - // ============================================================================================ SETTERS \/ - // ==================================================================================================== \/ -public: - inline void setValue (const T& _value) { mValue = _value; } - - // ==================================================================================================== \/ - // ============================================================================================ GETTERS \/ - // ==================================================================================================== \/ -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 \/ - // =================================================================================================== \/ -protected: - T mValue; -}; - -class Uniform1f : public UniformData<float> -{ -public: - Uniform1f(const Init& _init) : UniformData<float>(_init) {} - virtual ~Uniform1f(void) {} - void apply(GLint _location) const { glUniform1f(_location, mValue); } -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_SHADERPROGRAM_HH +#ifndef ACGL_RESOURCE_UNIFORM_HH +#define ACGL_RESOURCE_UNIFORM_HH + +#include <ACGL/Resource/BasicResource.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Utils/StringOperations.hh> +#include <ACGL/Math/Math.hh> +#include <ACGL/Types.hh> +#include <ACGL/GL.hh> + +#include <vector> + +namespace ACGL{ +namespace Resource{ + +class Uniform : public BasicResource +{ +public: + Uniform(void) : BasicResource() {} + virtual ~Uniform(void) {} + virtual void apply(GLint) const = 0; +}; + +template<typename T> +class UniformData : public Uniform +{ + // ========================================================================================================= \/ + // ============================================================================================ 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(); } + virtual ~UniformData(void) {} + + // ==================================================================================================== \/ + // ============================================================================================ SETTERS \/ + // ==================================================================================================== \/ +public: + inline void setValue (const T& _value) { mValue = _value; } + + // ==================================================================================================== \/ + // ============================================================================================ GETTERS \/ + // ==================================================================================================== \/ +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 \/ + // =================================================================================================== \/ +protected: + T mValue; +}; + +class Uniform1f : public UniformData<float> +{ +public: + Uniform1f(const Init& _init) : UniformData<float>(_init) {} + virtual ~Uniform1f(void) {} + void apply(GLint _location) const { glUniform1f(_location, mValue); } +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_SHADERPROGRAM_HH diff --git a/include/ACGL/Resource/VertexBuffer.hh b/include/ACGL/Resource/VertexBuffer.hh index 57e17181097da96d1d3342d671e5ea96c807c16a..ca67a8cf0248212e1a1f8786c3eb18db2a1e85e1 100644 --- a/include/ACGL/Resource/VertexBuffer.hh +++ b/include/ACGL/Resource/VertexBuffer.hh @@ -3,134 +3,134 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_VERTEXBUFFER_HH -#define ACGL_RESOURCE_VERTEXBUFFER_HH - -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/ArrayBuffer.hh> -#include <ACGL/Resource/ElementArrayBuffer.hh> -#include <ACGL/GL.hh> - -#include <vector> - -namespace ACGL{ -namespace Resource{ - -class VertexBuffer -{ - // ==================================================================================================== \/ - // ============================================================================================ STRUCTS \/ - // ==================================================================================================== \/ -public: - struct Attribute - { - std::string name; - int_t bufferID; - int_t attributeID; - }; - - // ===================================================================================================== \/ - // ============================================================================================ TYPEDEFS \/ - // ===================================================================================================== \/ -public: - typedef std::vector< SharedArrayBuffer > ArrayBufferVec; - typedef std::vector< Attribute > AttributeVec; - - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - VertexBuffer( - GLenum _mode, - GLsizei _elements) - : mMode(_mode), - mElements(_elements), - mpElementArrayBuffer(), - mArrayBuffers(), - mAttributes() - {} - - virtual ~VertexBuffer(void) {}; - - // ==================================================================================================== \/ - // ============================================================================================ 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; } - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - int_t getAttributeIndexByName(const std::string& _name) const; - - inline void setElements(GLsizei _elements) - { - mElements = _elements; - } - - inline void setElementArrayBuffer(const SharedElementArrayBuffer& _elementArrayBuffer) - { - mpElementArrayBuffer = _elementArrayBuffer; - } - - inline void attachArrayBuffer(const SharedArrayBuffer& _arrayBuffer) - { - mArrayBuffers.push_back(_arrayBuffer); - } - - inline void removeArrayBuffers(void) - { - mArrayBuffers.clear(); - } - - inline void attachAttribute( - const std::string& _name, - int_t _bufferID, - const std::string& _attributeName) - { - Attribute attribute = { - _name, - _bufferID, - mArrayBuffers[_bufferID]->getAttributeIndexByName(_attributeName)}; - mAttributes.push_back(attribute); - } - - inline void removeAttributes(void) - { - mAttributes.clear(); - } - - // ===================================================================================================== \/ - // ============================================================================================ WRAPPERS \/ - // ===================================================================================================== \/ -public: - void render(void) const; - - void draw(void) const - { - if(mpElementArrayBuffer) - mpElementArrayBuffer->draw(); - else - glDrawArrays(mMode, 0, mElements); - } - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - GLenum mMode; - GLsizei mElements; - SharedElementArrayBuffer mpElementArrayBuffer; - ArrayBufferVec mArrayBuffers; - AttributeVec mAttributes; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_VERTEXBUFFER_HH +#ifndef ACGL_RESOURCE_VERTEXBUFFER_HH +#define ACGL_RESOURCE_VERTEXBUFFER_HH + +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/ArrayBuffer.hh> +#include <ACGL/Resource/ElementArrayBuffer.hh> +#include <ACGL/GL.hh> + +#include <vector> + +namespace ACGL{ +namespace Resource{ + +class VertexBuffer +{ + // ==================================================================================================== \/ + // ============================================================================================ STRUCTS \/ + // ==================================================================================================== \/ +public: + struct Attribute + { + std::string name; + int_t bufferID; + int_t attributeID; + }; + + // ===================================================================================================== \/ + // ============================================================================================ TYPEDEFS \/ + // ===================================================================================================== \/ +public: + typedef std::vector< SharedArrayBuffer > ArrayBufferVec; + typedef std::vector< Attribute > AttributeVec; + + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + VertexBuffer( + GLenum _mode, + GLsizei _elements) + : mMode(_mode), + mElements(_elements), + mpElementArrayBuffer(), + mArrayBuffers(), + mAttributes() + {} + + virtual ~VertexBuffer(void) {}; + + // ==================================================================================================== \/ + // ============================================================================================ 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; } + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + int_t getAttributeIndexByName(const std::string& _name) const; + + inline void setElements(GLsizei _elements) + { + mElements = _elements; + } + + inline void setElementArrayBuffer(const SharedElementArrayBuffer& _elementArrayBuffer) + { + mpElementArrayBuffer = _elementArrayBuffer; + } + + inline void attachArrayBuffer(const SharedArrayBuffer& _arrayBuffer) + { + mArrayBuffers.push_back(_arrayBuffer); + } + + inline void removeArrayBuffers(void) + { + mArrayBuffers.clear(); + } + + inline void attachAttribute( + const std::string& _name, + int_t _bufferID, + const std::string& _attributeName) + { + Attribute attribute = { + _name, + _bufferID, + mArrayBuffers[_bufferID]->getAttributeIndexByName(_attributeName)}; + mAttributes.push_back(attribute); + } + + inline void removeAttributes(void) + { + mAttributes.clear(); + } + + // ===================================================================================================== \/ + // ============================================================================================ WRAPPERS \/ + // ===================================================================================================== \/ +public: + void render(void) const; + + void draw(void) const + { + if(mpElementArrayBuffer) + mpElementArrayBuffer->draw(); + else + glDrawArrays(mMode, 0, mElements); + } + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + GLenum mMode; + GLsizei mElements; + SharedElementArrayBuffer mpElementArrayBuffer; + ArrayBufferVec mArrayBuffers; + AttributeVec mAttributes; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_VERTEXBUFFER_HH diff --git a/include/ACGL/Resource/VertexBufferControl.hh b/include/ACGL/Resource/VertexBufferControl.hh index 8f1977889a1de0815495a5c2b35d396639ca38e5..8551966c358d48ae701dc22aae4e8dc407c96a77 100644 --- a/include/ACGL/Resource/VertexBufferControl.hh +++ b/include/ACGL/Resource/VertexBufferControl.hh @@ -3,85 +3,85 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_VERTEXBUFFERCONTROL_HH -#define ACGL_RESOURCE_VERTEXBUFFERCONTROL_HH - -#include <ACGL/Resource/BasicController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/VertexBuffer.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class VertexBufferControl : public BasicController<VertexBuffer> -{ - // ==================================================================================================== \/ - // ============================================================================================ STRUCTS \/ - // ==================================================================================================== \/ -public: - struct AttributeDefine - { - std::string name; - int_t bufferID; - std::string attributeName; - }; - - // ===================================================================================================== \/ - // ============================================================================================ TYPEDEFS \/ - // ===================================================================================================== \/ -public: - typedef std::vector< AttributeDefine > AttributeDefineVec; - - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - VertexBufferControl(void) - : BasicController<VertexBuffer>(), - mpElementArrayBuffer(), - mArrayBuffers(), - mAttributeDefines() - {} - virtual ~VertexBufferControl() {} - - // ==================================================================================================== \/ - // ============================================================================================ METHODS \/ - // ==================================================================================================== \/ -public: - inline VertexBufferControl& index (const SharedElementArrayBuffer& _elementArrayBuffer) { mpElementArrayBuffer = _elementArrayBuffer; return *this; } - inline VertexBufferControl& data (const SharedArrayBuffer& _vertexBuffer) { mArrayBuffers.push_back(_vertexBuffer); return *this; } - - inline VertexBufferControl& attribute (const std::string& _name, const std::string& _nameInArray) - { - AttributeDefine a = {_name, mArrayBuffers.size()-1, _nameInArray}; - mAttributeDefines.push_back(a); - return *this; - } - - inline VertexBufferControl& attribute (const std::string& _name, int_t _bufferID, const std::string& _nameInArray) - { - AttributeDefine a = {_name, _bufferID, _nameInArray}; - mAttributeDefines.push_back(a); - return *this; - } - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedVertexBuffer create(void); - - // =================================================================================================== \/ - // ============================================================================================ FIELDS \/ - // =================================================================================================== \/ -protected: - SharedElementArrayBuffer mpElementArrayBuffer; - VertexBuffer::ArrayBufferVec mArrayBuffers; - AttributeDefineVec mAttributeDefines; -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_VERTEXBUFFERCONTROL_HH +#define ACGL_RESOURCE_VERTEXBUFFERCONTROL_HH + +#include <ACGL/Resource/BasicController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/VertexBuffer.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class VertexBufferControl : public BasicController<VertexBuffer> +{ + // ==================================================================================================== \/ + // ============================================================================================ STRUCTS \/ + // ==================================================================================================== \/ +public: + struct AttributeDefine + { + std::string name; + int_t bufferID; + std::string attributeName; + }; + + // ===================================================================================================== \/ + // ============================================================================================ TYPEDEFS \/ + // ===================================================================================================== \/ +public: + typedef std::vector< AttributeDefine > AttributeDefineVec; + + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + VertexBufferControl(void) + : BasicController<VertexBuffer>(), + mpElementArrayBuffer(), + mArrayBuffers(), + mAttributeDefines() + {} + virtual ~VertexBufferControl() {} + + // ==================================================================================================== \/ + // ============================================================================================ METHODS \/ + // ==================================================================================================== \/ +public: + inline VertexBufferControl& index (const SharedElementArrayBuffer& _elementArrayBuffer) { mpElementArrayBuffer = _elementArrayBuffer; return *this; } + inline VertexBufferControl& data (const SharedArrayBuffer& _vertexBuffer) { mArrayBuffers.push_back(_vertexBuffer); return *this; } + + inline VertexBufferControl& attribute (const std::string& _name, const std::string& _nameInArray) + { + AttributeDefine a = {_name, mArrayBuffers.size()-1, _nameInArray}; + mAttributeDefines.push_back(a); + return *this; + } + + inline VertexBufferControl& attribute (const std::string& _name, int_t _bufferID, const std::string& _nameInArray) + { + AttributeDefine a = {_name, _bufferID, _nameInArray}; + mAttributeDefines.push_back(a); + return *this; + } + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedVertexBuffer create(void); + + // =================================================================================================== \/ + // ============================================================================================ FIELDS \/ + // =================================================================================================== \/ +protected: + SharedElementArrayBuffer mpElementArrayBuffer; + VertexBuffer::ArrayBufferVec mArrayBuffers; + AttributeDefineVec mAttributeDefines; +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Resource/VertexBufferControlFileOBJ.hh b/include/ACGL/Resource/VertexBufferControlFileOBJ.hh index 770f321d05e95746a66ae3284be01cdc53d4171a..705e4682d7536f89bc2d18df29b327377e21e7d5 100644 --- a/include/ACGL/Resource/VertexBufferControlFileOBJ.hh +++ b/include/ACGL/Resource/VertexBufferControlFileOBJ.hh @@ -3,41 +3,41 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_RESOURCE_VERTEXBUFFERCONTROLFILEOBJ_HH -#define ACGL_RESOURCE_VERTEXBUFFERCONTROLFILEOBJ_HH - -#include <ACGL/Resource/FileController.hh> -#include <ACGL/Resource/ResourceTypes.hh> -#include <ACGL/Resource/VertexBuffer.hh> -#include <ACGL/Base/Settings.hh> -#include <ACGL/GL.hh> - -namespace ACGL{ -namespace Resource{ - -class VertexBufferControlFileOBJ : public FileController<VertexBuffer> -{ - // ========================================================================================================= \/ - // ============================================================================================ CONSTRUCTORS \/ - // ========================================================================================================= \/ -public: - VertexBufferControlFileOBJ(const std::string& _filename) - : FileController<VertexBuffer>(_filename, Base::Settings::the()->getFullGeometryPath()) - {} - virtual ~VertexBufferControlFileOBJ() {} - -private: - bool loadOBJ(SharedVertexBuffer& vertexBuffer); - - // ===================================================================================================== \/ - // ============================================================================================ OVERRIDE \/ - // ===================================================================================================== \/ -public: - virtual SharedVertexBuffer create(void); - virtual bool update(SharedVertexBuffer& vertexBuffer); -}; - -} // Resource -} // ACGL - -#endif // ACGL_RESOURCE_TEXTURE_HH +#ifndef ACGL_RESOURCE_VERTEXBUFFERCONTROLFILEOBJ_HH +#define ACGL_RESOURCE_VERTEXBUFFERCONTROLFILEOBJ_HH + +#include <ACGL/Resource/FileController.hh> +#include <ACGL/Resource/ResourceTypes.hh> +#include <ACGL/Resource/VertexBuffer.hh> +#include <ACGL/Base/Settings.hh> +#include <ACGL/GL.hh> + +namespace ACGL{ +namespace Resource{ + +class VertexBufferControlFileOBJ : public FileController<VertexBuffer> +{ + // ========================================================================================================= \/ + // ============================================================================================ CONSTRUCTORS \/ + // ========================================================================================================= \/ +public: + VertexBufferControlFileOBJ(const std::string& _filename) + : FileController<VertexBuffer>(_filename, Base::Settings::the()->getFullGeometryPath()) + {} + virtual ~VertexBufferControlFileOBJ() {} + +private: + bool loadOBJ(SharedVertexBuffer& vertexBuffer); + + // ===================================================================================================== \/ + // ============================================================================================ OVERRIDE \/ + // ===================================================================================================== \/ +public: + virtual SharedVertexBuffer create(void); + virtual bool update(SharedVertexBuffer& vertexBuffer); +}; + +} // Resource +} // ACGL + +#endif // ACGL_RESOURCE_TEXTURE_HH diff --git a/include/ACGL/Utils/Log.hh b/include/ACGL/Utils/Log.hh index 554927bfcb65ea7166e430d475f3dcdad9547e2a..d68859f7ace8164dce30ba788860ce2b60f8901d 100644 --- a/include/ACGL/Utils/Log.hh +++ b/include/ACGL/Utils/Log.hh @@ -3,123 +3,123 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#ifndef ACGL_UTILS_LOG_HH -#define ACGL_UTILS_LOG_HH - -#include <ACGL/Base/Singleton.hh> -#include <ACGL/Types.hh> - -#include <string> -#include <cstdarg> -#include <iostream> -#include <fstream> -#include <sstream> - -namespace ACGL{ -namespace Utils{ - - -/* - * Can be used excactly like std::cout - * - * message stream ACGL::Utils::log() << "foo" << "bar" << var << std::endl; - * warning stream ACGL::Utils::warning() << - * error stream ACGL::Utils::error() << - * debug stream ACGL::Utils::debug() << - */ - -class CoutLikeStreamBuffer : public std::basic_streambuf<char, std::char_traits<char> > -{ - typedef std::basic_streambuf<char, std::char_traits<char> > base_type; - -public: - CoutLikeStreamBuffer(); - - ~CoutLikeStreamBuffer(); - - void setPrefix( const std::string &_prefix ); - -private: - - //virtual std::streamsize xsputn(const base_type::char_type* s, std::streamsize n) - //{ - // TODO: implement me for better performance - //} - - virtual base_type::int_type overflow(base_type::int_type ch); - - // for each endl: - virtual int sync(); - -private: - char *mBuffer; - int mBufferSize; // how many bytes are used - int mBufferMaxSize; // size of the buffer - - std::string mPrefix; - - bool mNewLineIsAboutToStart; -}; - - -template < unsigned int DEBUG_LEVEL > -class CoutLikeStream : public std::ostream, public Base::Singleton<CoutLikeStream<DEBUG_LEVEL> > -{ -public: - CoutLikeStream() : std::ostream( NULL ), mStreamBuffer(NULL) { - mStreamBuffer = new CoutLikeStreamBuffer(); - switch (DEBUG_LEVEL) { - case 0: mStreamBuffer->setPrefix("Debug: "); - break; - case 1: mStreamBuffer->setPrefix("Message: "); - break; - case 2: mStreamBuffer->setPrefix("Warning: "); - break; - case 3: mStreamBuffer->setPrefix("Error: "); - break; - default: { - mStreamBuffer->setPrefix("> "); - std::ostringstream streamName; - - streamName << DEBUG_LEVEL << ": "; - mStreamBuffer->setPrefix( streamName.str() ); - } - } - - unmute(); - } - - ~CoutLikeStream() { - delete mStreamBuffer; - } - - void setPrefix( const std::string &_prefix ) { - if (mStreamBuffer) { - mStreamBuffer->setPrefix(_prefix); - } - } - void mute() { rdbuf( NULL ); } - void unmute() { rdbuf( mStreamBuffer ); } -private: - CoutLikeStreamBuffer *mStreamBuffer; -}; - -inline CoutLikeStream<0>& debug() { return (*CoutLikeStream<0>::the()); } -inline CoutLikeStream<1>& message() { return (*CoutLikeStream<1>::the()); } -inline CoutLikeStream<2>& warning() { return (*CoutLikeStream<2>::the()); } -inline CoutLikeStream<3>& error() { return (*CoutLikeStream<3>::the()); } - -#define ACGL_DEBUG(STREAM) ACGL::Utils::debug() << STREAM; -#define ACGL_MESSAGE(STREAM) ACGL::Utils::message() << STREAM; -#define ACGL_WARNING(STREAM) ACGL::Utils::warning() << STREAM; -#define ACGL_ERROR(STREAM) ACGL::Utils::error() << STREAM; - -template < unsigned int N > -inline CoutLikeStream<N>& log() { return (*CoutLikeStream<N>::the()); } - -#define ACGL_LOG(N,STREAM) ACGL::Utils::log<N>() << STREAM; - -} // Utils -} // ACGL - -#endif // ACGL_UTILS_LOG_HH +#ifndef ACGL_UTILS_LOG_HH +#define ACGL_UTILS_LOG_HH + +#include <ACGL/Base/Singleton.hh> +#include <ACGL/Types.hh> + +#include <string> +#include <cstdarg> +#include <iostream> +#include <fstream> +#include <sstream> + +namespace ACGL{ +namespace Utils{ + + +/* + * Can be used excactly like std::cout + * + * message stream ACGL::Utils::log() << "foo" << "bar" << var << std::endl; + * warning stream ACGL::Utils::warning() << + * error stream ACGL::Utils::error() << + * debug stream ACGL::Utils::debug() << + */ + +class CoutLikeStreamBuffer : public std::basic_streambuf<char, std::char_traits<char> > +{ + typedef std::basic_streambuf<char, std::char_traits<char> > base_type; + +public: + CoutLikeStreamBuffer(); + + ~CoutLikeStreamBuffer(); + + void setPrefix( const std::string &_prefix ); + +private: + + //virtual std::streamsize xsputn(const base_type::char_type* s, std::streamsize n) + //{ + // TODO: implement me for better performance + //} + + virtual base_type::int_type overflow(base_type::int_type ch); + + // for each endl: + virtual int sync(); + +private: + char *mBuffer; + int mBufferSize; // how many bytes are used + int mBufferMaxSize; // size of the buffer + + std::string mPrefix; + + bool mNewLineIsAboutToStart; +}; + + +template < unsigned int DEBUG_LEVEL > +class CoutLikeStream : public std::ostream, public Base::Singleton<CoutLikeStream<DEBUG_LEVEL> > +{ +public: + CoutLikeStream() : std::ostream( NULL ), mStreamBuffer(NULL) { + mStreamBuffer = new CoutLikeStreamBuffer(); + switch (DEBUG_LEVEL) { + case 0: mStreamBuffer->setPrefix("Debug: "); + break; + case 1: mStreamBuffer->setPrefix("Message: "); + break; + case 2: mStreamBuffer->setPrefix("Warning: "); + break; + case 3: mStreamBuffer->setPrefix("Error: "); + break; + default: { + mStreamBuffer->setPrefix("> "); + std::ostringstream streamName; + + streamName << DEBUG_LEVEL << ": "; + mStreamBuffer->setPrefix( streamName.str() ); + } + } + + unmute(); + } + + ~CoutLikeStream() { + delete mStreamBuffer; + } + + void setPrefix( const std::string &_prefix ) { + if (mStreamBuffer) { + mStreamBuffer->setPrefix(_prefix); + } + } + void mute() { rdbuf( NULL ); } + void unmute() { rdbuf( mStreamBuffer ); } +private: + CoutLikeStreamBuffer *mStreamBuffer; +}; + +inline CoutLikeStream<0>& debug() { return (*CoutLikeStream<0>::the()); } +inline CoutLikeStream<1>& message() { return (*CoutLikeStream<1>::the()); } +inline CoutLikeStream<2>& warning() { return (*CoutLikeStream<2>::the()); } +inline CoutLikeStream<3>& error() { return (*CoutLikeStream<3>::the()); } + +#define ACGL_DEBUG(STREAM) ACGL::Utils::debug() << STREAM; +#define ACGL_MESSAGE(STREAM) ACGL::Utils::message() << STREAM; +#define ACGL_WARNING(STREAM) ACGL::Utils::warning() << STREAM; +#define ACGL_ERROR(STREAM) ACGL::Utils::error() << STREAM; + +template < unsigned int N > +inline CoutLikeStream<N>& log() { return (*CoutLikeStream<N>::the()); } + +#define ACGL_LOG(N,STREAM) ACGL::Utils::log<N>() << STREAM; + +} // Utils +} // ACGL + +#endif // ACGL_UTILS_LOG_HH diff --git a/src/ACGL/GLUtils/Tools.cc b/src/ACGL/GLUtils/Tools.cc index 03fb180ddf4cf3e5c1d60ea35f4d2e04560b26a3..3612d52f122b2fe22440cef3a07ba8c88736d017 100644 --- a/src/ACGL/GLUtils/Tools.cc +++ b/src/ACGL/GLUtils/Tools.cc @@ -3,53 +3,53 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/ACGL.hh> -#include <ACGL/GLUtils/Tools.hh> - -namespace ACGL{ -namespace GLUtils{ -namespace Tools{ - -const GLubyte* acglErrorString( GLenum err ) -{ -#ifdef PLATFORM_IOS - // no gluErrorString on iOS - // this should only get used on OpenGL ES plattforms, so error strings from the compatibility profile - // are ignored. Only 3.2+ Core and ES 2.0+ errors belong here: - if (err == GL_INVALID_ENUM) { return "GL_INVALID_ENUM"; } - else if (err == GL_INVALID_VALUE) { return "GL_INVALID_VALUE"; } - else if (err == GL_INVALID_OPERATION) { return "GL_INVALID_OPERATION"l; } - else if (err == GL_INVALID_FRAMEBUFFER_OPERATION) { return "GL_INVALID_FRAMEBUFFER_OPERATION"; } - else if (err == GL_OUT_OF_MEMORY) { return "GL_OUT_OF_MEMORY" }; - else if (err == GL_NO_ERROR) { return "GL_NO_ERROR"; } - else { - return "unknown error"; - } -#else - return gluErrorString( err ); -#endif -} - - - -GLenum openGLError_( const char *_fileName, const unsigned long _lineNumber ) -{ - GLenum err = glGetError(); - - if (err == GL_NO_ERROR) return GL_NO_ERROR; - - GLenum lastError = err; - while ( err != GL_NO_ERROR) { - ACGL::Utils::error() << "GL error in file " << _fileName << ":" << _lineNumber << " - " << acglErrorString( err ) << std::endl; - lastError = err; - err = glGetError(); - } - - return lastError; -} - - -} // Tools -} // GLUtils -} // ACGL - +#include <ACGL/ACGL.hh> +#include <ACGL/GLUtils/Tools.hh> + +namespace ACGL{ +namespace GLUtils{ +namespace Tools{ + +const GLubyte* acglErrorString( GLenum err ) +{ +#ifdef PLATFORM_IOS + // no gluErrorString on iOS + // this should only get used on OpenGL ES plattforms, so error strings from the compatibility profile + // are ignored. Only 3.2+ Core and ES 2.0+ errors belong here: + if (err == GL_INVALID_ENUM) { return "GL_INVALID_ENUM"; } + else if (err == GL_INVALID_VALUE) { return "GL_INVALID_VALUE"; } + else if (err == GL_INVALID_OPERATION) { return "GL_INVALID_OPERATION"l; } + else if (err == GL_INVALID_FRAMEBUFFER_OPERATION) { return "GL_INVALID_FRAMEBUFFER_OPERATION"; } + else if (err == GL_OUT_OF_MEMORY) { return "GL_OUT_OF_MEMORY" }; + else if (err == GL_NO_ERROR) { return "GL_NO_ERROR"; } + else { + return "unknown error"; + } +#else + return gluErrorString( err ); +#endif +} + + + +GLenum openGLError_( const char *_fileName, const unsigned long _lineNumber ) +{ + GLenum err = glGetError(); + + if (err == GL_NO_ERROR) return GL_NO_ERROR; + + GLenum lastError = err; + while ( err != GL_NO_ERROR) { + ACGL::Utils::error() << "GL error in file " << _fileName << ":" << _lineNumber << " - " << acglErrorString( err ) << std::endl; + lastError = err; + err = glGetError(); + } + + return lastError; +} + + +} // Tools +} // GLUtils +} // ACGL + diff --git a/src/ACGL/Resource/ArrayBuffer.cc b/src/ACGL/Resource/ArrayBuffer.cc index da124ec21ee5296c454d2a499284c2bd70ba1238..7e2c348f473bbf10f16a52b44f31429bafd6c69f 100644 --- a/src/ACGL/Resource/ArrayBuffer.cc +++ b/src/ACGL/Resource/ArrayBuffer.cc @@ -3,29 +3,29 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/ArrayBuffer.hh> - -using namespace ACGL; -using namespace ACGL::Resource; - -int_t ArrayBuffer::getAttributeIndexByName(const std::string& _nameInArray) const -{ - for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) - if(mAttributes[i].name == _nameInArray) - return i; - return -1; -} - -void ArrayBuffer::render(void) const -{ - bind(); - for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) - { - setPointer(i, i); - glEnableVertexAttribArray(i); - } - draw(); - for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) - glDisableVertexAttribArray(i); - glBindBuffer(GL_ARRAY_BUFFER, 0); -} +#include <ACGL/Resource/ArrayBuffer.hh> + +using namespace ACGL; +using namespace ACGL::Resource; + +int_t ArrayBuffer::getAttributeIndexByName(const std::string& _nameInArray) const +{ + for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) + if(mAttributes[i].name == _nameInArray) + return i; + return -1; +} + +void ArrayBuffer::render(void) const +{ + bind(); + for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) + { + setPointer(i, i); + glEnableVertexAttribArray(i); + } + draw(); + for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) + glDisableVertexAttribArray(i); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} diff --git a/src/ACGL/Resource/ArrayBufferControl.cc b/src/ACGL/Resource/ArrayBufferControl.cc index 1994dd925b05b16650672b7b61caf092d0c0ea46..c7cc1a2ce6ffe05475425bd73aa226e3eabdeeab 100644 --- a/src/ACGL/Resource/ArrayBufferControl.cc +++ b/src/ACGL/Resource/ArrayBufferControl.cc @@ -3,27 +3,27 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/ArrayBufferControl.hh> - -using namespace ACGL::Resource; - -SharedArrayBuffer ArrayBufferControl::create(void) -{ - SharedArrayBuffer arrayBuffer(new ArrayBuffer( - mUsage, - mMode)); - for(AttributeDefineVec::size_type i = 0; i < mAttributeDefines.size(); i++) - { - arrayBuffer->attachAttribute( - mAttributeDefines[i].name, - mAttributeDefines[i].type, - mAttributeDefines[i].dimension); - } - if(mpData != NULL) - { - arrayBuffer->bind(); - arrayBuffer->setData(mpData, mElements); - glBindBuffer(GL_ARRAY_BUFFER, 0); - } - return arrayBuffer; -} +#include <ACGL/Resource/ArrayBufferControl.hh> + +using namespace ACGL::Resource; + +SharedArrayBuffer ArrayBufferControl::create(void) +{ + SharedArrayBuffer arrayBuffer(new ArrayBuffer( + mUsage, + mMode)); + for(AttributeDefineVec::size_type i = 0; i < mAttributeDefines.size(); i++) + { + arrayBuffer->attachAttribute( + mAttributeDefines[i].name, + mAttributeDefines[i].type, + mAttributeDefines[i].dimension); + } + if(mpData != NULL) + { + arrayBuffer->bind(); + arrayBuffer->setData(mpData, mElements); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + return arrayBuffer; +} diff --git a/src/ACGL/Resource/FrameBuffer.cc b/src/ACGL/Resource/FrameBuffer.cc index d0b24d4f5492cc487e9cd7f2eda0be3f238bd4f9..34857c2a7f4ce8b84624dea07af7f20e84815ea3 100644 --- a/src/ACGL/Resource/FrameBuffer.cc +++ b/src/ACGL/Resource/FrameBuffer.cc @@ -3,16 +3,16 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/FrameBuffer.hh> - -using namespace ACGL::Resource; - -GLuint FrameBuffer::msBuffers[8] = { - GL_COLOR_ATTACHMENT0, - GL_COLOR_ATTACHMENT1, - GL_COLOR_ATTACHMENT2, - GL_COLOR_ATTACHMENT3, - GL_COLOR_ATTACHMENT4, - GL_COLOR_ATTACHMENT5, - GL_COLOR_ATTACHMENT6, - GL_COLOR_ATTACHMENT7}; +#include <ACGL/Resource/FrameBuffer.hh> + +using namespace ACGL::Resource; + +GLuint FrameBuffer::msBuffers[8] = { + GL_COLOR_ATTACHMENT0, + GL_COLOR_ATTACHMENT1, + GL_COLOR_ATTACHMENT2, + GL_COLOR_ATTACHMENT3, + GL_COLOR_ATTACHMENT4, + GL_COLOR_ATTACHMENT5, + GL_COLOR_ATTACHMENT6, + GL_COLOR_ATTACHMENT7}; diff --git a/src/ACGL/Resource/FrameBufferControl.cc b/src/ACGL/Resource/FrameBufferControl.cc index a9d6d8e6e469d69a09e1ce89e7d07e41bdcfcc8d..ddc40c49787ddff0ac197dc28d43bc7246d72fc6 100644 --- a/src/ACGL/Resource/FrameBufferControl.cc +++ b/src/ACGL/Resource/FrameBufferControl.cc @@ -3,25 +3,25 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/FrameBufferControl.hh> - -using namespace ACGL::Resource; - -SharedFrameBuffer FrameBufferControl::create(void) -{ - SharedFrameBuffer frameBuffer(new FrameBuffer(mWidth, mHeight)); - frameBuffer->bind(); - - for(FrameBuffer::AttachmentVec::size_type i = 0; i < mColorAttachments.size(); ++i) - { - if(mColorAttachments[i].texture) - frameBuffer->attachColorTexture(mColorAttachments[i].texture); - else if(mColorAttachments[i].renderBuffer) - frameBuffer->attachColorRenderBuffer(mColorAttachments[i].renderBuffer); - } - if(mDepthAttachment.texture) - frameBuffer->setDepthTexture(mDepthAttachment.texture); - else if(mDepthAttachment.renderBuffer) - frameBuffer->setDepthRenderBuffer(mDepthAttachment.renderBuffer); - return frameBuffer; -} +#include <ACGL/Resource/FrameBufferControl.hh> + +using namespace ACGL::Resource; + +SharedFrameBuffer FrameBufferControl::create(void) +{ + SharedFrameBuffer frameBuffer(new FrameBuffer(mWidth, mHeight)); + frameBuffer->bind(); + + for(FrameBuffer::AttachmentVec::size_type i = 0; i < mColorAttachments.size(); ++i) + { + if(mColorAttachments[i].texture) + frameBuffer->attachColorTexture(mColorAttachments[i].texture); + else if(mColorAttachments[i].renderBuffer) + frameBuffer->attachColorRenderBuffer(mColorAttachments[i].renderBuffer); + } + if(mDepthAttachment.texture) + frameBuffer->setDepthTexture(mDepthAttachment.texture); + else if(mDepthAttachment.renderBuffer) + frameBuffer->setDepthRenderBuffer(mDepthAttachment.renderBuffer); + return frameBuffer; +} diff --git a/src/ACGL/Resource/Shader.cc b/src/ACGL/Resource/Shader.cc index a423fbcb3e51abb0119ce72971f3f44bc816fceb..645ad6c7add0303a69b7f48fa6f0d77c5e38064b 100644 --- a/src/ACGL/Resource/Shader.cc +++ b/src/ACGL/Resource/Shader.cc @@ -1,101 +1,101 @@ -//////////////////////////////////////////////////////////////////////////////// -// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // -// All rights reserved. // -//////////////////////////////////////////////////////////////////////////////// - -#include <ACGL/Resource/Shader.hh> -#include <ACGL/GLUtils/Tools.hh> -#include <ACGL/Utils/Log.hh> -#include <ACGL/Utils/StringOperations.hh> - -#include <iostream> -#include <fstream> - -using namespace ACGL::Utils; -using namespace ACGL::GLUtils; -using namespace ACGL::GLUtils::Tools; -using namespace ACGL::Resource; - -bool Shader::setFromFile(const std::string& _filename) -{ - std::string line = ""; - std::string fileContent = ""; - - std::ifstream fileStream(_filename.c_str(), std::ifstream::in); - - if(fileStream.is_open()) - { - while (fileStream.good()) - { - std::getline(fileStream,line); - fileContent += line + "\n"; - } - fileStream.close(); - } - else - { - error() << "Failed to open file: " << _filename << std::endl; - return false; - } - - return setSource(fileContent); -} - -bool Shader::setSource(const std::string& _source) -{ - std::istringstream stream(_source); - std::string line = ""; - - mAttributes.clear(); - - while (stream.good()) - { - std::getline(stream,line); - if(StringOperations::startsWith(line, "attribute")) - { - std::vector<std::string> tokens = StringOperations::split(line, ' '); - if(tokens.size() > 2) - { - std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';'); - if(tokensOfToken.size() > 0) - { - mAttributes.push_back(tokensOfToken[0]); - } - } - } - } - return compile(_source.c_str()); -} - -bool Shader::compile(const char* _pProgramText) const -{ - glShaderSource(mContext, 1, &_pProgramText, NULL); - openGLRareError(); - glCompileShader(mContext); - - if ( openGLCommonErrorOccured() ) - { - return false; - } - - // check for shader compile errors: - GLint shaderError; - glGetShaderiv(mContext, GL_COMPILE_STATUS, &shaderError); - if(shaderError != GL_TRUE) - { - // yes, errors - error() << "Shader compile error: " << std::endl; - } - - GLsizei length = 0; - glGetShaderiv(mContext, GL_INFO_LOG_LENGTH, &length); - if(length > 1) - { - // a compile log can get produced even if there were no errors, but warnings! - GLchar* pInfo = new char[length + 1]; - glGetShaderInfoLog(mContext, length, &length, pInfo); - error() << "Compile log: " << std::string(pInfo) << std::endl; - delete[] pInfo; - } - return (shaderError == GL_TRUE); // return true if we encountered no errors -} +//////////////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // +// All rights reserved. // +//////////////////////////////////////////////////////////////////////////////// + +#include <ACGL/Resource/Shader.hh> +#include <ACGL/GLUtils/Tools.hh> +#include <ACGL/Utils/Log.hh> +#include <ACGL/Utils/StringOperations.hh> + +#include <iostream> +#include <fstream> + +using namespace ACGL::Utils; +using namespace ACGL::GLUtils; +using namespace ACGL::GLUtils::Tools; +using namespace ACGL::Resource; + +bool Shader::setFromFile(const std::string& _filename) +{ + std::string line = ""; + std::string fileContent = ""; + + std::ifstream fileStream(_filename.c_str(), std::ifstream::in); + + if(fileStream.is_open()) + { + while (fileStream.good()) + { + std::getline(fileStream,line); + fileContent += line + "\n"; + } + fileStream.close(); + } + else + { + error() << "Failed to open file: " << _filename << std::endl; + return false; + } + + return setSource(fileContent); +} + +bool Shader::setSource(const std::string& _source) +{ + std::istringstream stream(_source); + std::string line = ""; + + mAttributes.clear(); + + while (stream.good()) + { + std::getline(stream,line); + if(StringOperations::startsWith(line, "attribute")) + { + std::vector<std::string> tokens = StringOperations::split(line, ' '); + if(tokens.size() > 2) + { + std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';'); + if(tokensOfToken.size() > 0) + { + mAttributes.push_back(tokensOfToken[0]); + } + } + } + } + return compile(_source.c_str()); +} + +bool Shader::compile(const char* _pProgramText) const +{ + glShaderSource(mContext, 1, &_pProgramText, NULL); + openGLRareError(); + glCompileShader(mContext); + + if ( openGLCommonErrorOccured() ) + { + return false; + } + + // check for shader compile errors: + GLint shaderError; + glGetShaderiv(mContext, GL_COMPILE_STATUS, &shaderError); + if(shaderError != GL_TRUE) + { + // yes, errors + error() << "Shader compile error: " << std::endl; + } + + GLsizei length = 0; + glGetShaderiv(mContext, GL_INFO_LOG_LENGTH, &length); + if(length > 1) + { + // a compile log can get produced even if there were no errors, but warnings! + GLchar* pInfo = new char[length + 1]; + glGetShaderInfoLog(mContext, length, &length, pInfo); + error() << "Compile log: " << std::string(pInfo) << std::endl; + delete[] pInfo; + } + return (shaderError == GL_TRUE); // return true if we encountered no errors +} diff --git a/src/ACGL/Resource/ShaderControlFile.cc b/src/ACGL/Resource/ShaderControlFile.cc index 47de55446ed55510c9971589a2c674bde8ac2996..19bca43bb6d1897550ddeff41940be028acd5ec3 100644 --- a/src/ACGL/Resource/ShaderControlFile.cc +++ b/src/ACGL/Resource/ShaderControlFile.cc @@ -3,45 +3,45 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/ShaderControlFile.hh> -#include <ACGL/Utils/StringOperations.hh> - -using namespace ACGL::Utils; -using namespace ACGL::Resource; - -SharedShader ShaderControlFile::create(void) -{ - updateFileModificationTime(); - - if(mType == Shader::ShaderTypeInvalid) - { - std::string filename; - std::string extension; - if(!StringOperations::splitFileExtension(mFilename, filename, extension)) - return SharedShader(); - - if(extension == "vsh") - mType = Shader::ShaderTypeVertex; - else if(extension == "fsh") - mType = Shader::ShaderTypeFragment; - else - return SharedShader(); - } - - SharedShader shader(new Shader(mType)); - if(shader->setFromFile(mFullFilePath)) - return shader; - return SharedShader(); -} - -bool ShaderControlFile::update(SharedShader& shader) -{ - if(fileIsUpToDate()) - return false; - - if(!shader->setFromFile(mFullFilePath)) - return false; - - updateFileModificationTime(); - return true; -} +#include <ACGL/Resource/ShaderControlFile.hh> +#include <ACGL/Utils/StringOperations.hh> + +using namespace ACGL::Utils; +using namespace ACGL::Resource; + +SharedShader ShaderControlFile::create(void) +{ + updateFileModificationTime(); + + if(mType == Shader::ShaderTypeInvalid) + { + std::string filename; + std::string extension; + if(!StringOperations::splitFileExtension(mFilename, filename, extension)) + return SharedShader(); + + if(extension == "vsh") + mType = Shader::ShaderTypeVertex; + else if(extension == "fsh") + mType = Shader::ShaderTypeFragment; + else + return SharedShader(); + } + + SharedShader shader(new Shader(mType)); + if(shader->setFromFile(mFullFilePath)) + return shader; + return SharedShader(); +} + +bool ShaderControlFile::update(SharedShader& shader) +{ + if(fileIsUpToDate()) + return false; + + if(!shader->setFromFile(mFullFilePath)) + return false; + + updateFileModificationTime(); + return true; +} diff --git a/src/ACGL/Resource/ShaderProgram.cc b/src/ACGL/Resource/ShaderProgram.cc index 505060e4613886449dc743dda6dbdadafaf662bf..84055504715063cbe866bcd20dfc89957cb534ff 100644 --- a/src/ACGL/Resource/ShaderProgram.cc +++ b/src/ACGL/Resource/ShaderProgram.cc @@ -1,60 +1,60 @@ -//////////////////////////////////////////////////////////////////////////////// -// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // -// All rights reserved. // -//////////////////////////////////////////////////////////////////////////////// - -#include <ACGL/Resource/ShaderProgram.hh> -#include <ACGL/GLUtils/Tools.hh> -#include <ACGL/Utils/Log.hh> - -using namespace ACGL::GLUtils; -using namespace ACGL::GLUtils::Tools; -using namespace ACGL::Utils; -using namespace ACGL::Resource; - -void ShaderProgram::bindAttributeLocations() const -{ - for (SharedShaderVec::size_type i = 0; i < mShaders.size(); ++i) - { - const Shader::AttributeVec& attribs = mShaders[i]->getAttributes(); - for (Shader::AttributeVec::size_type k = 0; k < attribs.size(); ++k) - { - glBindAttribLocation(mContext, k, attribs[k].c_str()); - } - } -} - -bool ShaderProgram::link(void) const -{ - bindAttributeLocations(); - - glLinkProgram(mContext); - - if ( openGLRareErrorOccured() ) - { - // it's uncommon that this fails, compile errors (the most common errors) - // are not checked here! - return false; - } - - // check for shader compile errors: - GLint error; - glGetProgramiv(mContext, GL_LINK_STATUS, &error); - - if (error != GL_TRUE) - { - // yes, errors :-( - GLsizei length = 0; - glGetProgramiv(mContext, GL_INFO_LOG_LENGTH, &length); - if (length > 1) - { - GLchar* pInfo = new char[length + 1]; - glGetProgramInfoLog(mContext, length, &length, pInfo); - openGLRareError(); - message() << "Linker Error: " << std::string(pInfo) << std::endl; - delete[] pInfo; - return false; - } - } - return true; -} +//////////////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University // +// All rights reserved. // +//////////////////////////////////////////////////////////////////////////////// + +#include <ACGL/Resource/ShaderProgram.hh> +#include <ACGL/GLUtils/Tools.hh> +#include <ACGL/Utils/Log.hh> + +using namespace ACGL::GLUtils; +using namespace ACGL::GLUtils::Tools; +using namespace ACGL::Utils; +using namespace ACGL::Resource; + +void ShaderProgram::bindAttributeLocations() const +{ + for (SharedShaderVec::size_type i = 0; i < mShaders.size(); ++i) + { + const Shader::AttributeVec& attribs = mShaders[i]->getAttributes(); + for (Shader::AttributeVec::size_type k = 0; k < attribs.size(); ++k) + { + glBindAttribLocation(mContext, k, attribs[k].c_str()); + } + } +} + +bool ShaderProgram::link(void) const +{ + bindAttributeLocations(); + + glLinkProgram(mContext); + + if ( openGLRareErrorOccured() ) + { + // it's uncommon that this fails, compile errors (the most common errors) + // are not checked here! + return false; + } + + // check for shader compile errors: + GLint error; + glGetProgramiv(mContext, GL_LINK_STATUS, &error); + + if (error != GL_TRUE) + { + // yes, errors :-( + GLsizei length = 0; + glGetProgramiv(mContext, GL_INFO_LOG_LENGTH, &length); + if (length > 1) + { + GLchar* pInfo = new char[length + 1]; + glGetProgramInfoLog(mContext, length, &length, pInfo); + openGLRareError(); + message() << "Linker Error: " << std::string(pInfo) << std::endl; + delete[] pInfo; + return false; + } + } + return true; +} diff --git a/src/ACGL/Resource/ShaderProgramControlAutoFiles.cc b/src/ACGL/Resource/ShaderProgramControlAutoFiles.cc index 44953e07177ffa3d8eb0f7ec128389d22f31fbc3..1d096bcf861013d06d6753515c910fd11c4b2b6b 100644 --- a/src/ACGL/Resource/ShaderProgramControlAutoFiles.cc +++ b/src/ACGL/Resource/ShaderProgramControlAutoFiles.cc @@ -3,32 +3,32 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/ShaderProgramControlAutoFiles.hh> -#include <ACGL/Resource/ShaderControlFile.hh> -#include <ACGL/Resource/FileManager.hh> -#include <ACGL/Base/Settings.hh> - -using namespace ACGL::Base; -using namespace ACGL::Resource; - -SharedShaderProgram ShaderProgramControlAutoFiles::create(void) -{ - SharedShaderProgram shaderProgram(new ShaderProgram()); - if(SharedShader shader = ShaderFileManager::the()->get(ShaderControlFile(mFilename + ".vsh").type(Shader::ShaderTypeVertex))) - shaderProgram->attachShader(shader); - if(SharedShader shader = ShaderFileManager::the()->get(ShaderControlFile(mFilename + ".fsh").type(Shader::ShaderTypeFragment))) - shaderProgram->attachShader(shader); - if(shaderProgram->link()) - return shaderProgram; - return SharedShaderProgram(); -} - -bool ShaderProgramControlAutoFiles::update(SharedShaderProgram& shaderProgram) -{ - bool update = false; - update |= ShaderFileManager::the()->update(mFilename + ".vsh"); - update |= ShaderFileManager::the()->update(mFilename + ".fsh"); - if(update) - return shaderProgram->link(); - return false; -} +#include <ACGL/Resource/ShaderProgramControlAutoFiles.hh> +#include <ACGL/Resource/ShaderControlFile.hh> +#include <ACGL/Resource/FileManager.hh> +#include <ACGL/Base/Settings.hh> + +using namespace ACGL::Base; +using namespace ACGL::Resource; + +SharedShaderProgram ShaderProgramControlAutoFiles::create(void) +{ + SharedShaderProgram shaderProgram(new ShaderProgram()); + if(SharedShader shader = ShaderFileManager::the()->get(ShaderControlFile(mFilename + ".vsh").type(Shader::ShaderTypeVertex))) + shaderProgram->attachShader(shader); + if(SharedShader shader = ShaderFileManager::the()->get(ShaderControlFile(mFilename + ".fsh").type(Shader::ShaderTypeFragment))) + shaderProgram->attachShader(shader); + if(shaderProgram->link()) + return shaderProgram; + return SharedShaderProgram(); +} + +bool ShaderProgramControlAutoFiles::update(SharedShaderProgram& shaderProgram) +{ + bool update = false; + update |= ShaderFileManager::the()->update(mFilename + ".vsh"); + update |= ShaderFileManager::the()->update(mFilename + ".fsh"); + if(update) + return shaderProgram->link(); + return false; +} diff --git a/src/ACGL/Resource/TextureControlFileJPG.cc b/src/ACGL/Resource/TextureControlFileJPG.cc index 4b8d26f7055b8bd3f7cd685d97767ff69bebca94..74e76f8024953e35a1d0a38fc105baf7ae20f521 100644 --- a/src/ACGL/Resource/TextureControlFileJPG.cc +++ b/src/ACGL/Resource/TextureControlFileJPG.cc @@ -3,38 +3,37 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/TextureControlFileJPG.hh> - -using namespace ACGL::Resource; - -SharedTexture TextureControlFileJPG::create(void) -{ - updateFileModificationTime(); - - SharedTexture texture(new Texture( - GL_RGBA, - GL_TEXTURE_2D, - GL_RGBA, - GL_UNSIGNED_BYTE)); - - texture->setParameter(GL_TEXTURE_MIN_FILTER, mMinFilter == 0 ? GL_LINEAR_MIPMAP_LINEAR : mMinFilter); - texture->setParameter(GL_TEXTURE_MAG_FILTER, mMagFilter == 0 ? GL_LINEAR : mMagFilter); - - if(loadJPG(texture)) - return texture; - - return SharedTexture(); -} - -bool TextureControlFileJPG::update(SharedTexture& texture) -{ - if(fileIsUpToDate()) - return false; - - if(!loadJPG(texture)) - return false; - - updateFileModificationTime(); - return true; -} - +#include <ACGL/Resource/TextureControlFileJPG.hh> + +using namespace ACGL::Resource; + +SharedTexture TextureControlFileJPG::create(void) +{ + updateFileModificationTime(); + + SharedTexture texture(new Texture( + GL_RGBA, + GL_TEXTURE_2D, + GL_RGBA, + GL_UNSIGNED_BYTE)); + + texture->setParameter(GL_TEXTURE_MIN_FILTER, mMinFilter == 0 ? GL_LINEAR_MIPMAP_LINEAR : mMinFilter); + texture->setParameter(GL_TEXTURE_MAG_FILTER, mMagFilter == 0 ? GL_LINEAR : mMagFilter); + + if(loadJPG(texture)) + return texture; + + return SharedTexture(); +} + +bool TextureControlFileJPG::update(SharedTexture& texture) +{ + if(fileIsUpToDate()) + return false; + + if(!loadJPG(texture)) + return false; + + updateFileModificationTime(); + return true; +} diff --git a/src/ACGL/Resource/TextureControlFileJPG_QImage.cc b/src/ACGL/Resource/TextureControlFileJPG_QImage.cc index ab8a37e2e70144ec6da6ebe7be3ec7cfca68365b..a5978c24af3530a669afc434410098b298faafe5 100644 --- a/src/ACGL/Resource/TextureControlFileJPG_QImage.cc +++ b/src/ACGL/Resource/TextureControlFileJPG_QImage.cc @@ -3,28 +3,28 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/TextureControlFileJPG.hh> -#include <ACGL/Utils/Log.hh> - -#include <QtGui/QImage> -#include <QtOpenGL/QGLWidget> - -using namespace ACGL::Utils; -using namespace ACGL::Resource; - -bool TextureControlFileJPG::loadJPG(SharedTexture& texture) -{ - QImage image = QImage(QString(getFullFilePath().c_str())); - - if(image.isNull()) - { - error() << "Loading image " << getFullFilePath() << " has failed!" << std::endl; - return false; - } - - texture->setSize(image.width(), image.height()); - image = QGLWidget::convertToGLFormat(image); - texture->setTexImage2D(image.bits()); - texture->generateMipmaps(); - return true; -} +#include <ACGL/Resource/TextureControlFileJPG.hh> +#include <ACGL/Utils/Log.hh> + +#include <QtGui/QImage> +#include <QtOpenGL/QGLWidget> + +using namespace ACGL::Utils; +using namespace ACGL::Resource; + +bool TextureControlFileJPG::loadJPG(SharedTexture& texture) +{ + QImage image = QImage(QString(getFullFilePath().c_str())); + + if(image.isNull()) + { + error() << "Loading image " << getFullFilePath() << " has failed!" << std::endl; + return false; + } + + texture->setSize(image.width(), image.height()); + image = QGLWidget::convertToGLFormat(image); + texture->setTexImage2D(image.bits()); + texture->generateMipmaps(); + return true; +} diff --git a/src/ACGL/Resource/VertexBuffer.cc b/src/ACGL/Resource/VertexBuffer.cc index b9a1716a8048c27a1f7bab6a96e73e33f08d28d8..1183761f5657718a465c8dc71eb1188aa6c1faba 100644 --- a/src/ACGL/Resource/VertexBuffer.cc +++ b/src/ACGL/Resource/VertexBuffer.cc @@ -3,51 +3,51 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/VertexBuffer.hh> -#include <ACGL/Types.hh> - -using namespace ACGL; -using namespace ACGL::Resource; - -int_t VertexBuffer::getAttributeIndexByName(const std::string& _name) const -{ - for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) - if(mAttributes[i].name == _name) - return i; - return -1; -} - -void VertexBuffer::render(void) const -{ - if(mpElementArrayBuffer) - mpElementArrayBuffer->bind(); - - int_t currentBufferID = -1; - for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) - { - if(mAttributes[i].bufferID != currentBufferID) - { - currentBufferID = mAttributes[i].bufferID; - mArrayBuffers[currentBufferID]->bind(); - } - mArrayBuffers[currentBufferID]->setPointer(mAttributes[i].attributeID, i); - glEnableVertexAttribArray(i); - } - - draw(); - - currentBufferID = -1; - for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) - { - if(mAttributes[i].bufferID != currentBufferID) - { - currentBufferID = mAttributes[i].bufferID; - mArrayBuffers[currentBufferID]->bind(); - } - glDisableVertexAttribArray(i); - } - - if(mpElementArrayBuffer) - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); -} +#include <ACGL/Resource/VertexBuffer.hh> +#include <ACGL/Types.hh> + +using namespace ACGL; +using namespace ACGL::Resource; + +int_t VertexBuffer::getAttributeIndexByName(const std::string& _name) const +{ + for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) + if(mAttributes[i].name == _name) + return i; + return -1; +} + +void VertexBuffer::render(void) const +{ + if(mpElementArrayBuffer) + mpElementArrayBuffer->bind(); + + int_t currentBufferID = -1; + for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) + { + if(mAttributes[i].bufferID != currentBufferID) + { + currentBufferID = mAttributes[i].bufferID; + mArrayBuffers[currentBufferID]->bind(); + } + mArrayBuffers[currentBufferID]->setPointer(mAttributes[i].attributeID, i); + glEnableVertexAttribArray(i); + } + + draw(); + + currentBufferID = -1; + for(AttributeVec::size_type i = 0; i < mAttributes.size(); ++i) + { + if(mAttributes[i].bufferID != currentBufferID) + { + currentBufferID = mAttributes[i].bufferID; + mArrayBuffers[currentBufferID]->bind(); + } + glDisableVertexAttribArray(i); + } + + if(mpElementArrayBuffer) + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} diff --git a/src/ACGL/Resource/VertexBufferControl.cc b/src/ACGL/Resource/VertexBufferControl.cc index cdcc50de64b208431306464e3d8a56ff6bed062f..23509717d72e060ff6a3c6ae277b2d5d424a2aa3 100644 --- a/src/ACGL/Resource/VertexBufferControl.cc +++ b/src/ACGL/Resource/VertexBufferControl.cc @@ -3,43 +3,43 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/VertexBufferControl.hh> - -using namespace ACGL::Resource; - -SharedVertexBuffer VertexBufferControl::create(void) -{ - GLenum mode = GL_TRIANGLES; - GLsizei elements = 0; - if(mpElementArrayBuffer) - { - mode = mpElementArrayBuffer->getMode(); - elements = mpElementArrayBuffer->getElements(); - } - else if(mArrayBuffers.size() > 0 && mArrayBuffers[0]) - { - mode = mArrayBuffers[0]->getMode(); - elements = mArrayBuffers[0]->getElements(); - } - - SharedVertexBuffer vertexBuffer(new VertexBuffer( - mode, - elements)); - - vertexBuffer->setElementArrayBuffer(mpElementArrayBuffer); - - for(VertexBuffer::ArrayBufferVec::size_type i = 0; i < mArrayBuffers.size(); i++) - { - vertexBuffer->attachArrayBuffer(mArrayBuffers[i]); - } - - for(AttributeDefineVec::size_type i = 0; i < mAttributeDefines.size(); i++) - { - vertexBuffer->attachAttribute( - mAttributeDefines[i].name, - mAttributeDefines[i].bufferID, - mAttributeDefines[i].attributeName); - } - - return vertexBuffer; -} +#include <ACGL/Resource/VertexBufferControl.hh> + +using namespace ACGL::Resource; + +SharedVertexBuffer VertexBufferControl::create(void) +{ + GLenum mode = GL_TRIANGLES; + GLsizei elements = 0; + if(mpElementArrayBuffer) + { + mode = mpElementArrayBuffer->getMode(); + elements = mpElementArrayBuffer->getElements(); + } + else if(mArrayBuffers.size() > 0 && mArrayBuffers[0]) + { + mode = mArrayBuffers[0]->getMode(); + elements = mArrayBuffers[0]->getElements(); + } + + SharedVertexBuffer vertexBuffer(new VertexBuffer( + mode, + elements)); + + vertexBuffer->setElementArrayBuffer(mpElementArrayBuffer); + + for(VertexBuffer::ArrayBufferVec::size_type i = 0; i < mArrayBuffers.size(); i++) + { + vertexBuffer->attachArrayBuffer(mArrayBuffers[i]); + } + + for(AttributeDefineVec::size_type i = 0; i < mAttributeDefines.size(); i++) + { + vertexBuffer->attachAttribute( + mAttributeDefines[i].name, + mAttributeDefines[i].bufferID, + mAttributeDefines[i].attributeName); + } + + return vertexBuffer; +} diff --git a/src/ACGL/Resource/VertexBufferControlFileOBJ.cc b/src/ACGL/Resource/VertexBufferControlFileOBJ.cc index fceb64bcbaf608eb6d2a0f6cd494f300ef298b3e..e055a1fe6e204fcc03c0a867ac6a1c2522e6e55f 100644 --- a/src/ACGL/Resource/VertexBufferControlFileOBJ.cc +++ b/src/ACGL/Resource/VertexBufferControlFileOBJ.cc @@ -3,323 +3,323 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Resource/VertexBufferControlFileOBJ.hh> -#include <ACGL/Resource/ArrayBufferControl.hh> -#include <ACGL/Resource/ElementArrayBufferControl.hh> -#include <ACGL/Resource/VertexBufferControl.hh> -#include <ACGL/Utils/StringOperations.hh> -#include <ACGL/Utils/Log.hh> -#include <ACGL/Types.hh> - -#include <vector> -#include <iostream> -#include <fstream> - -using namespace ACGL; -using namespace ACGL::Utils; -using namespace ACGL::Resource; - -SharedVertexBuffer VertexBufferControlFileOBJ::create(void) -{ - updateFileModificationTime(); - - SharedVertexBuffer vertexBuffer = VertexBufferControl(). - index(ElementArrayBufferControl(). - mode(GL_TRIANGLES). - usage(GL_STATIC_DRAW). - create()). - data(ArrayBufferControl(). - mode(GL_TRIANGLES). - usage(GL_STATIC_DRAW). - create()). - create(); - - if(loadOBJ(vertexBuffer)) - return vertexBuffer; - - return SharedVertexBuffer(); -} - -bool VertexBufferControlFileOBJ::update(SharedVertexBuffer& vertexBuffer) -{ - if(fileIsUpToDate()) - return false; - - if(!loadOBJ(vertexBuffer)) - return false; - - updateFileModificationTime(); - return true; -} - -bool VertexBufferControlFileOBJ::loadOBJ(SharedVertexBuffer& _vertexBuffer) -{ - std::string full = getFullFilePath(); - - std::string line = ""; - - std::ifstream fileStream(full.c_str(), std::ifstream::in); - - std::vector<GLfloat> sharedVertexVector; - std::vector<GLfloat> sharedTexCoordVector; - std::vector<GLfloat> sharedNormalVector; - uint_t texCoordElements = 0; - uint_t texCoordDimension = 0; - - sharedVertexVector.resize(4); - sharedNormalVector.resize(3); - - bool elementOK = true; - bool lineOK = true; - - uint_t lineNumber = 0; - - if(fileStream.is_open()) - { - //Read the first line - if(fileStream.good()) - std::getline(fileStream, line); - - while(fileStream.good()) - { - if(StringOperations::startsWith(line, "vt")) - { - std::vector<std::string> elements = StringOperations::split(line, ' '); - //The first occurence of vt gives us the number of texture coordinates per vertex. - if(texCoordElements == 0) - { - texCoordElements = elements.size(); - texCoordDimension = texCoordElements - 1; - sharedTexCoordVector.resize(texCoordDimension); - } - if((uint_t)elements.size() == texCoordElements) - { - for(uint_t i = 1; i < texCoordElements; ++i) - sharedTexCoordVector.push_back(StringOperations::to<GLfloat>(elements[i], &elementOK)); - } - else - { - //If a later texture coordinate is defined wrong, we enter zeros, because - //otherwise the indices would be intermixed. - for(uint_t i = 1; i < texCoordElements; ++i) - sharedTexCoordVector.push_back(0.0f); - lineOK = false; - } - } - else if(StringOperations::startsWith(line, "vn")) - { - std::vector<std::string> elements = StringOperations::split(line, ' '); - if(elements.size() == 4) - { - sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[1], &elementOK)); - sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[2], &elementOK)); - sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[3], &elementOK)); - } - else - { - //If a later texture coordinate is defined wrong, we enter zeros, because - //otherwise the indices would be intermixed. - sharedNormalVector.push_back(0.0f); - sharedNormalVector.push_back(0.0f); - sharedNormalVector.push_back(0.0f); - lineOK = false; - } - } - else if(StringOperations::startsWith(line, "v")) - { - std::vector<std::string> elements = StringOperations::split(line, ' '); - if(elements.size() == 4) - { - sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[1], &elementOK)); - sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[2], &elementOK)); - sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[3], &elementOK)); - sharedVertexVector.push_back(1.0f); - } - else - { - lineOK = false; - } - } - else if(StringOperations::startsWith(line, "f")) - { - //As soon as the first f occurs, we assume that all vertex - //positions, normals and tex coords have been defined! - break; - } - - if(!lineOK) - { - warning() << "Format of line " << lineNumber << " is wrong!" << std::endl; - debug() << line << std::endl; - lineOK = true; - } - if(!elementOK) - { - warning() << "Element in line " << lineNumber << " is wrong!" << std::endl; - debug() << line << std::endl; - elementOK = true; - } - ++lineNumber; - getline(fileStream, line); - - } - - bool hasNormals = sharedNormalVector.size() != 3; - if(!hasNormals) { - message() << "The file " << full << " has no normals!" << std::endl; - } - - //By default the number of attributes is 1 (only vertices) - uint_t attributes = 1; - //Then we check for other attributes like normals... - if(hasNormals) - attributes++; - //...or tex coords - if(texCoordDimension > 0) - attributes++; - - //By default components is the size of 3, because vertices have 3 components (x,y,z) - uint_t components = 3; - //If there are normals we have to add another 3 elements - if(hasNormals) - components += 3; - //We then add the number of tex coord components. This number is 0, if there are none - components += texCoordDimension; - - std::vector<GLfloat> interleavedDataVector; - typedef GLuint IndexType; - GLenum indexType = GL_UNSIGNED_INT; - std::vector<IndexType> indexVector; - - uint_t index = 0; - while(fileStream.good()) - { - if(StringOperations::startsWith(line, "f")) - { - std::vector<std::string> elements = StringOperations::split(line, ' '); - uint_t uiPolygonSize = elements.size() - 1; - if(uiPolygonSize > 2) - { - for(size_t i = 1; i < elements.size(); ++i) - { - if(i > 3) - { - //If we have more than 3 vertices, we have to triangulate in a simple way. - //E.g. for a quad 1234 we should get 123 134. - indexVector.push_back(index - i + 1); - indexVector.push_back(index - 1); - } - indexVector.push_back(index++); - std::vector<std::string> indices = StringOperations::split(elements[i], '/', false); - if(indices.size() == 3 && hasNormals && texCoordDimension > 0) - { - uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]); - sharedIndex = StringOperations::to<IndexType>(indices[2], &elementOK); - interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex]); - interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 1]); - interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 2]); - sharedIndex = StringOperations::to<IndexType>(indices[1], &elementOK); - for(uint_t i = 0; i < texCoordDimension; ++i) - interleavedDataVector.push_back(sharedTexCoordVector[texCoordDimension * sharedIndex + i]); - } - else if(indices.size() == 3 && hasNormals) - { - uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]); - sharedIndex = StringOperations::to<IndexType>(indices[2], &elementOK); - interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex]); - interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 1]); - interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 2]); - } - else if(indices.size() == 2 && texCoordDimension > 0) - { - uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]); - sharedIndex = StringOperations::to<IndexType>(indices[1], &elementOK); - for(uint_t i = 0; i < texCoordDimension; ++i) - interleavedDataVector.push_back(sharedTexCoordVector[texCoordDimension * sharedIndex + i]); - } - else if(indices.size() == 1) - { - uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]); - interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]); - } - else - { - for(uint_t j = 0; j < components; ++j) - interleavedDataVector.push_back(0.0f); - lineOK = false; - } - } - } - else - { - lineOK = false; - } - } - - if(!lineOK) - { - warning() << "Format of line " << lineNumber << " is wrong!" << std::endl; - debug() << line << std::endl; - lineOK = true; - } - if(!elementOK) - { - warning() << "Element in line " << lineNumber << " is wrong!" << std::endl; - debug() << line << std::endl; - elementOK = true; - } - ++lineNumber; - getline(fileStream, line); - } - - fileStream.close(); - - SharedElementArrayBuffer elementArrayBuffer = _vertexBuffer->getElementArrayBuffer(); - SharedArrayBuffer arrayBuffer = _vertexBuffer->getArrayBuffers()[0]; - - arrayBuffer->removeAttributes(); - _vertexBuffer->removeAttributes(); - - arrayBuffer->attachAttribute("aPosition", GL_FLOAT, 4); - _vertexBuffer->attachAttribute("aPosition", 0, "aPosition"); - if(hasNormals) - { - arrayBuffer->attachAttribute("aNormal", GL_FLOAT, 3); - _vertexBuffer->attachAttribute("aNormal", 0, "aNormal"); - } - if(texCoordDimension > 0) - { - arrayBuffer->attachAttribute("aTexCoord", GL_FLOAT, texCoordDimension); - _vertexBuffer->attachAttribute("aTexCoord", 0, "aTexCoord"); - } - - elementArrayBuffer->setType(indexType); - elementArrayBuffer->bind(); - elementArrayBuffer->setData(&indexVector[0], indexVector.size()); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - arrayBuffer->bind(); - arrayBuffer->setData(&interleavedDataVector[0], index); - glBindBuffer(GL_ARRAY_BUFFER, 0); - _vertexBuffer->setElements(indexVector.size()); - - return true; - } - - error() << "Failed to open file: " << getFullFilePath() << std::endl; - return false; -} +#include <ACGL/Resource/VertexBufferControlFileOBJ.hh> +#include <ACGL/Resource/ArrayBufferControl.hh> +#include <ACGL/Resource/ElementArrayBufferControl.hh> +#include <ACGL/Resource/VertexBufferControl.hh> +#include <ACGL/Utils/StringOperations.hh> +#include <ACGL/Utils/Log.hh> +#include <ACGL/Types.hh> + +#include <vector> +#include <iostream> +#include <fstream> + +using namespace ACGL; +using namespace ACGL::Utils; +using namespace ACGL::Resource; + +SharedVertexBuffer VertexBufferControlFileOBJ::create(void) +{ + updateFileModificationTime(); + + SharedVertexBuffer vertexBuffer = VertexBufferControl(). + index(ElementArrayBufferControl(). + mode(GL_TRIANGLES). + usage(GL_STATIC_DRAW). + create()). + data(ArrayBufferControl(). + mode(GL_TRIANGLES). + usage(GL_STATIC_DRAW). + create()). + create(); + + if(loadOBJ(vertexBuffer)) + return vertexBuffer; + + return SharedVertexBuffer(); +} + +bool VertexBufferControlFileOBJ::update(SharedVertexBuffer& vertexBuffer) +{ + if(fileIsUpToDate()) + return false; + + if(!loadOBJ(vertexBuffer)) + return false; + + updateFileModificationTime(); + return true; +} + +bool VertexBufferControlFileOBJ::loadOBJ(SharedVertexBuffer& _vertexBuffer) +{ + std::string full = getFullFilePath(); + + std::string line = ""; + + std::ifstream fileStream(full.c_str(), std::ifstream::in); + + std::vector<GLfloat> sharedVertexVector; + std::vector<GLfloat> sharedTexCoordVector; + std::vector<GLfloat> sharedNormalVector; + uint_t texCoordElements = 0; + uint_t texCoordDimension = 0; + + sharedVertexVector.resize(4); + sharedNormalVector.resize(3); + + bool elementOK = true; + bool lineOK = true; + + uint_t lineNumber = 0; + + if(fileStream.is_open()) + { + //Read the first line + if(fileStream.good()) + std::getline(fileStream, line); + + while(fileStream.good()) + { + if(StringOperations::startsWith(line, "vt")) + { + std::vector<std::string> elements = StringOperations::split(line, ' '); + //The first occurence of vt gives us the number of texture coordinates per vertex. + if(texCoordElements == 0) + { + texCoordElements = elements.size(); + texCoordDimension = texCoordElements - 1; + sharedTexCoordVector.resize(texCoordDimension); + } + if((uint_t)elements.size() == texCoordElements) + { + for(uint_t i = 1; i < texCoordElements; ++i) + sharedTexCoordVector.push_back(StringOperations::to<GLfloat>(elements[i], &elementOK)); + } + else + { + //If a later texture coordinate is defined wrong, we enter zeros, because + //otherwise the indices would be intermixed. + for(uint_t i = 1; i < texCoordElements; ++i) + sharedTexCoordVector.push_back(0.0f); + lineOK = false; + } + } + else if(StringOperations::startsWith(line, "vn")) + { + std::vector<std::string> elements = StringOperations::split(line, ' '); + if(elements.size() == 4) + { + sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[1], &elementOK)); + sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[2], &elementOK)); + sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[3], &elementOK)); + } + else + { + //If a later texture coordinate is defined wrong, we enter zeros, because + //otherwise the indices would be intermixed. + sharedNormalVector.push_back(0.0f); + sharedNormalVector.push_back(0.0f); + sharedNormalVector.push_back(0.0f); + lineOK = false; + } + } + else if(StringOperations::startsWith(line, "v")) + { + std::vector<std::string> elements = StringOperations::split(line, ' '); + if(elements.size() == 4) + { + sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[1], &elementOK)); + sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[2], &elementOK)); + sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[3], &elementOK)); + sharedVertexVector.push_back(1.0f); + } + else + { + lineOK = false; + } + } + else if(StringOperations::startsWith(line, "f")) + { + //As soon as the first f occurs, we assume that all vertex + //positions, normals and tex coords have been defined! + break; + } + + if(!lineOK) + { + warning() << "Format of line " << lineNumber << " is wrong!" << std::endl; + debug() << line << std::endl; + lineOK = true; + } + if(!elementOK) + { + warning() << "Element in line " << lineNumber << " is wrong!" << std::endl; + debug() << line << std::endl; + elementOK = true; + } + ++lineNumber; + getline(fileStream, line); + + } + + bool hasNormals = sharedNormalVector.size() != 3; + if(!hasNormals) { + message() << "The file " << full << " has no normals!" << std::endl; + } + + //By default the number of attributes is 1 (only vertices) + uint_t attributes = 1; + //Then we check for other attributes like normals... + if(hasNormals) + attributes++; + //...or tex coords + if(texCoordDimension > 0) + attributes++; + + //By default components is the size of 3, because vertices have 3 components (x,y,z) + uint_t components = 3; + //If there are normals we have to add another 3 elements + if(hasNormals) + components += 3; + //We then add the number of tex coord components. This number is 0, if there are none + components += texCoordDimension; + + std::vector<GLfloat> interleavedDataVector; + typedef GLuint IndexType; + GLenum indexType = GL_UNSIGNED_INT; + std::vector<IndexType> indexVector; + + uint_t index = 0; + while(fileStream.good()) + { + if(StringOperations::startsWith(line, "f")) + { + std::vector<std::string> elements = StringOperations::split(line, ' '); + uint_t uiPolygonSize = elements.size() - 1; + if(uiPolygonSize > 2) + { + for(size_t i = 1; i < elements.size(); ++i) + { + if(i > 3) + { + //If we have more than 3 vertices, we have to triangulate in a simple way. + //E.g. for a quad 1234 we should get 123 134. + indexVector.push_back(index - i + 1); + indexVector.push_back(index - 1); + } + indexVector.push_back(index++); + std::vector<std::string> indices = StringOperations::split(elements[i], '/', false); + if(indices.size() == 3 && hasNormals && texCoordDimension > 0) + { + uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]); + sharedIndex = StringOperations::to<IndexType>(indices[2], &elementOK); + interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex]); + interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 1]); + interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 2]); + sharedIndex = StringOperations::to<IndexType>(indices[1], &elementOK); + for(uint_t i = 0; i < texCoordDimension; ++i) + interleavedDataVector.push_back(sharedTexCoordVector[texCoordDimension * sharedIndex + i]); + } + else if(indices.size() == 3 && hasNormals) + { + uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]); + sharedIndex = StringOperations::to<IndexType>(indices[2], &elementOK); + interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex]); + interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 1]); + interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 2]); + } + else if(indices.size() == 2 && texCoordDimension > 0) + { + uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]); + sharedIndex = StringOperations::to<IndexType>(indices[1], &elementOK); + for(uint_t i = 0; i < texCoordDimension; ++i) + interleavedDataVector.push_back(sharedTexCoordVector[texCoordDimension * sharedIndex + i]); + } + else if(indices.size() == 1) + { + uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]); + interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]); + } + else + { + for(uint_t j = 0; j < components; ++j) + interleavedDataVector.push_back(0.0f); + lineOK = false; + } + } + } + else + { + lineOK = false; + } + } + + if(!lineOK) + { + warning() << "Format of line " << lineNumber << " is wrong!" << std::endl; + debug() << line << std::endl; + lineOK = true; + } + if(!elementOK) + { + warning() << "Element in line " << lineNumber << " is wrong!" << std::endl; + debug() << line << std::endl; + elementOK = true; + } + ++lineNumber; + getline(fileStream, line); + } + + fileStream.close(); + + SharedElementArrayBuffer elementArrayBuffer = _vertexBuffer->getElementArrayBuffer(); + SharedArrayBuffer arrayBuffer = _vertexBuffer->getArrayBuffers()[0]; + + arrayBuffer->removeAttributes(); + _vertexBuffer->removeAttributes(); + + arrayBuffer->attachAttribute("aPosition", GL_FLOAT, 4); + _vertexBuffer->attachAttribute("aPosition", 0, "aPosition"); + if(hasNormals) + { + arrayBuffer->attachAttribute("aNormal", GL_FLOAT, 3); + _vertexBuffer->attachAttribute("aNormal", 0, "aNormal"); + } + if(texCoordDimension > 0) + { + arrayBuffer->attachAttribute("aTexCoord", GL_FLOAT, texCoordDimension); + _vertexBuffer->attachAttribute("aTexCoord", 0, "aTexCoord"); + } + + elementArrayBuffer->setType(indexType); + elementArrayBuffer->bind(); + elementArrayBuffer->setData(&indexVector[0], indexVector.size()); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + arrayBuffer->bind(); + arrayBuffer->setData(&interleavedDataVector[0], index); + glBindBuffer(GL_ARRAY_BUFFER, 0); + _vertexBuffer->setElements(indexVector.size()); + + return true; + } + + error() << "Failed to open file: " << getFullFilePath() << std::endl; + return false; +} diff --git a/src/ACGL/Utils/Log.cc b/src/ACGL/Utils/Log.cc index f370b3f10058daa02c0db80f3c4d004820458370..ef8d04cb3c87e3ed556dd125159137590f3e7181 100644 --- a/src/ACGL/Utils/Log.cc +++ b/src/ACGL/Utils/Log.cc @@ -3,58 +3,58 @@ // All rights reserved. // //////////////////////////////////////////////////////////////////////////////// -#include <ACGL/Utils/Log.hh> - -#include <cstdio> -#include <iostream> - -using namespace ACGL::Utils; - -CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() { - mBufferSize = 0; - mBufferMaxSize = 256; - mBuffer = new char[ mBufferMaxSize+1 ]; - mNewLineIsAboutToStart = true; -} - -CoutLikeStreamBuffer::~CoutLikeStreamBuffer() { - if ( mBufferSize > 0 ) { - overflow( base_type::traits_type::eof() ); - sync(); - } - delete[] mBuffer; -} - -void CoutLikeStreamBuffer::setPrefix( const std::string &_prefix ) { - mPrefix = _prefix; -} - -CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_type::int_type ch) { - // print buffer - if ((mBufferSize >= mBufferMaxSize) || ( base_type::traits_type::eq_int_type(ch, base_type::traits_type::eof()))) { - - if (mNewLineIsAboutToStart) { - std::cout << mPrefix; - mNewLineIsAboutToStart = false; - } - if (mBufferSize > 0) { - mBuffer[mBufferSize] = (char) 0; - std::cout << mBuffer; - mBufferSize = 0; - } - } else { - mBuffer[ mBufferSize++ ] = (char) ch; - } - return base_type::traits_type::not_eof( ch ); -} - -// for each endl: -int CoutLikeStreamBuffer::sync() { - overflow( base_type::traits_type::eof() ); - std::cout.flush(); - mNewLineIsAboutToStart = true; - return base_type::sync(); -} - - - +#include <ACGL/Utils/Log.hh> + +#include <cstdio> +#include <iostream> + +using namespace ACGL::Utils; + +CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() { + mBufferSize = 0; + mBufferMaxSize = 256; + mBuffer = new char[ mBufferMaxSize+1 ]; + mNewLineIsAboutToStart = true; +} + +CoutLikeStreamBuffer::~CoutLikeStreamBuffer() { + if ( mBufferSize > 0 ) { + overflow( base_type::traits_type::eof() ); + sync(); + } + delete[] mBuffer; +} + +void CoutLikeStreamBuffer::setPrefix( const std::string &_prefix ) { + mPrefix = _prefix; +} + +CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_type::int_type ch) { + // print buffer + if ((mBufferSize >= mBufferMaxSize) || ( base_type::traits_type::eq_int_type(ch, base_type::traits_type::eof()))) { + + if (mNewLineIsAboutToStart) { + std::cout << mPrefix; + mNewLineIsAboutToStart = false; + } + if (mBufferSize > 0) { + mBuffer[mBufferSize] = (char) 0; + std::cout << mBuffer; + mBufferSize = 0; + } + } else { + mBuffer[ mBufferSize++ ] = (char) ch; + } + return base_type::traits_type::not_eof( ch ); +} + +// for each endl: +int CoutLikeStreamBuffer::sync() { + overflow( base_type::traits_type::eof() ); + std::cout.flush(); + mNewLineIsAboutToStart = true; + return base_type::sync(); +} + + +