From 9faa503f321feb9666e78195a3feb816f913fda8 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Sun, 12 Jul 2015 22:16:55 +0200 Subject: [PATCH] Use glm for matrix math instead of the OpenGL stack Should cause less GL pipeline stalling / driver spinlocking if enabled. Disable AQUARIA_USE_GLM (default: true) in CMake to use the old GL pipeline version. Based on the implementation by Matt Bierner: https://bitbucket.org/mattbierner/ios-aquaria --- BBGE/RenderObject.cpp | 61 ++++++++++++++++++++++++++++++++++------- BBGE/RenderObject.h | 5 ++-- CMakeLists.txt | 5 ++++ win/vc90/Aquaria.vcproj | 4 +-- win/vc90/BBGE.vcproj | 4 +-- 5 files changed, 62 insertions(+), 17 deletions(-) diff --git a/BBGE/RenderObject.cpp b/BBGE/RenderObject.cpp index 5a2f8f8..f61256c 100644 --- a/BBGE/RenderObject.cpp +++ b/BBGE/RenderObject.cpp @@ -25,6 +25,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include +#ifdef BBGE_USE_GLM +#include "glm/glm.hpp" +#include "glm/gtx/transform.hpp" +#endif + bool RenderObject::renderCollisionShape = false; int RenderObject::lastTextureApplied = 0; bool RenderObject::lastTextureRepeat = false; @@ -292,24 +297,51 @@ Vector RenderObject::getInvRotPosition(const Vector &vec) #endif } -void RenderObject::matrixChain() +#ifdef BBGE_USE_GLM +static glm::mat4 matrixChain(const RenderObject *ro) { - if (parent) - parent->matrixChain(); + glm::mat4 tranformMatrix = glm::scale( + glm::translate( + glm::rotate( + glm::translate( + ro->getParent() ? matrixChain(ro->getParent()) : glm::mat4(1.0f), + glm::vec3(ro->position.x+ro->offset.x, ro->position.y+ro->offset.y, 0) + ), + ro->rotation.z + ro->rotationOffset.z, + glm::vec3(0, 0, 1) + ), + glm::vec3(ro->beforeScaleOffset.x, ro->beforeScaleOffset.y, 0.0f) + ), + glm::vec3(ro->scale.x, ro->scale.y, 0.0f) + ); + + if (ro->isfh()) + tranformMatrix *= glm::rotate(180.0f, 0.0f, 1.0f, 0.0f); + + tranformMatrix *= glm::translate(ro->internalOffset.x, ro->internalOffset.y, 0.0f); + + return tranformMatrix; +} +#else +static void matrixChain(RenderObject *ro) +{ + if (RenderObject *parent = ro->getParent()) + matrixChain(parent); #ifdef BBGE_BUILD_OPENGL - glTranslatef(position.x+offset.x, position.y+offset.y, 0); - glRotatef(rotation.z+rotationOffset.z, 0, 0, 1); - glTranslatef(beforeScaleOffset.x, beforeScaleOffset.y, 0); - glScalef(scale.x, scale.y, 0); - if (isfh()) + glTranslatef(ro->position.x+ro->offset.x, ro->position.y+ro->offset.y, 0); + glRotatef(ro->rotation.z+ro->rotationOffset.z, 0, 0, 1); + glTranslatef(ro->beforeScaleOffset.x, ro->beforeScaleOffset.y, 0); + glScalef(ro->scale.x, ro->scale.y, 0); + if (ro->isfh()) { //glDisable(GL_CULL_FACE); glRotatef(180, 0, 1, 0); } - glTranslatef(internalOffset.x, internalOffset.y, 0); + glTranslatef(ro->internalOffset.x, ro->internalOffset.y, 0); #endif } +#endif float RenderObject::getWorldRotation() { @@ -330,11 +362,19 @@ Vector RenderObject::getWorldPositionAndRotation() Vector RenderObject::getWorldCollidePosition(const Vector &vec) { +#ifdef BBGE_USE_GLM + glm::mat4 transformMatrix = glm::translate( + matrixChain(this), + glm::vec3(collidePosition.x + vec.x, collidePosition.y + vec.y, 0.0f) + ); + + return Vector(transformMatrix[3][0], transformMatrix[3][1], 0); +#else #ifdef BBGE_BUILD_OPENGL glPushMatrix(); glLoadIdentity(); - matrixChain(); + matrixChain(this); glTranslatef(collidePosition.x+vec.x, collidePosition.y+vec.y, 0); float m[16]; @@ -347,6 +387,7 @@ Vector RenderObject::getWorldCollidePosition(const Vector &vec) #elif BBGE_BUILD_DIRECTX return vec; #endif +#endif } void RenderObject::fhTo(bool fh) diff --git a/BBGE/RenderObject.h b/BBGE/RenderObject.h index 9d776bd..369b3b5 100644 --- a/BBGE/RenderObject.h +++ b/BBGE/RenderObject.h @@ -93,7 +93,6 @@ public: bool setTexture(const std::string &name); void toggleAlpha(float t = 0.2); - void matrixChain(); virtual void update(float dt); bool isDead() const {return _dead;} @@ -130,8 +129,8 @@ public: virtual void flipHorizontal(); virtual void flipVertical(); - bool isfh() { return _fh; } - bool isfv() { return _fv; } + bool isfh() const { return _fh; } + bool isfv() const { return _fv; } // recursive bool isfhr(); diff --git a/CMakeLists.txt b/CMakeLists.txt index 71f8de3..07dca08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ OPTION(AQUARIA_DEMO_BUILD "Demo Build?" FALSE) OPTION(AQUARIA_USE_VFS "Use Virtual File System? Required for some additional features." TRUE) OPTION(AQUARIA_USE_SDL2 "Use SDL2" FALSE) +OPTION(AQUARIA_USE_GLM "Use GLM for matrix math" TRUE) # No Steamworks SDK for Linux at the moment. Roll our own achievements. ADD_DEFINITIONS(-DBBGE_BUILD_ACHIEVEMENTS_INTERNAL=1) @@ -303,6 +304,10 @@ IF(AQUARIA_USE_SDL2) ADD_DEFINITIONS(-DBBGE_BUILD_SDL2=1) ENDIF(AQUARIA_USE_SDL2) +IF(AQUARIA_USE_GLM) + ADD_DEFINITIONS(-DBBGE_USE_GLM=1) +ENDIF(AQUARIA_USE_GLM) + IF(AQUARIA_DEMO_BUILD) message(STATUS "Demo build.") ADD_DEFINITIONS(-DAQUARIA_DEMO=1) diff --git a/win/vc90/Aquaria.vcproj b/win/vc90/Aquaria.vcproj index 0b704d5..93b13a6 100644 --- a/win/vc90/Aquaria.vcproj +++ b/win/vc90/Aquaria.vcproj @@ -42,7 +42,7 @@ Name="VCCLCompilerTool" Optimization="0" AdditionalIncludeDirectories=""$(SolutionDir)\..\ExternalLibs\freetype2\include\freetype\config";"$(SolutionDir)\..\ExternalLibs\gl";"$(SolutionDir)\..\ExternalLibs\glpng\zlib";"$(SolutionDir)\..\ExternalLibs\glpng\png";"$(SolutionDir)\..\ExternalLibs\libogg-1.3.0\include";"$(SolutionDir)\..\ExternalLibs\libvorbis-1.3.3\include";"$(SolutionDir)\..\ExternalLibs\freetype2\include";"$(SolutionDir)\..\ExternalLibs\FTGL\include";"$(SolutionDir)\..\ExternalLibs\SDL2\include";"$(SolutionDir)\..\ExternalLibs\AL\include";"$(SolutionDir)\..\BBGE";"$(SolutionDir)\..\ExternalLibs\lua-5.1.4\src";"$(SolutionDir)\..\ExternalLibs\lvpa\include";"$(SolutionDir)\..\ExternalLibs\lvpa";"$(SolutionDir)\..\ExternalLibs\ttvfs";"$(SolutionDir)\..\ExternalLibs\ttvfs_cfileapi";"$(SolutionDir)\..\ExternalLibs"" - PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;BBGE_BUILD_WINDOWS=1;BBGE_BUILD_VFS=1;VFS_ENABLE_C_API=1" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;BBGE_BUILD_WINDOWS=1;BBGE_BUILD_VFS=1;VFS_ENABLE_C_API=1;BBGE_USE_GLM=1" MinimalRebuild="true" BasicRuntimeChecks="0" RuntimeLibrary="3" @@ -124,7 +124,7 @@ OmitFramePointers="true" EnableFiberSafeOptimizations="true" AdditionalIncludeDirectories=""$(SolutionDir)\..\ExternalLibs\freetype2\include\freetype\config";"$(SolutionDir)\..\ExternalLibs\gl";"$(SolutionDir)\..\ExternalLibs\glpng\zlib";"$(SolutionDir)\..\ExternalLibs\glpng\png";"$(SolutionDir)\..\ExternalLibs\libogg-1.3.0\include";"$(SolutionDir)\..\ExternalLibs\libvorbis-1.3.3\include";"$(SolutionDir)\..\ExternalLibs\freetype2\include";"$(SolutionDir)\..\ExternalLibs\FTGL\include";"$(SolutionDir)\..\ExternalLibs\SDL2\include";"$(SolutionDir)\..\ExternalLibs\AL\include";"$(SolutionDir)\..\BBGE";"$(SolutionDir)\..\ExternalLibs\lua-5.1.4\src";"$(SolutionDir)\..\ExternalLibs\lvpa\include";"$(SolutionDir)\..\ExternalLibs\lvpa";"$(SolutionDir)\..\ExternalLibs\ttvfs";"$(SolutionDir)\..\ExternalLibs\ttvfs_cfileapi";"$(SolutionDir)\..\ExternalLibs"" - PreprocessorDefinitions="WIN32;NDEBUG;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;BBGE_BUILD_WINDOWS=1;_HAS_EXCEPTIONS=0;BBGE_BUILD_VFS=1;VFS_ENABLE_C_API=1" + PreprocessorDefinitions="WIN32;NDEBUG;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;BBGE_BUILD_WINDOWS=1;_HAS_EXCEPTIONS=0;BBGE_BUILD_VFS=1;VFS_ENABLE_C_API=1;BBGE_USE_GLM=1" StringPooling="true" ExceptionHandling="0" RuntimeLibrary="0" diff --git a/win/vc90/BBGE.vcproj b/win/vc90/BBGE.vcproj index c701762..266c422 100644 --- a/win/vc90/BBGE.vcproj +++ b/win/vc90/BBGE.vcproj @@ -43,7 +43,7 @@ Name="VCCLCompilerTool" Optimization="0" AdditionalIncludeDirectories=""$(SolutionDir)\..\ExternalLibs\freetype2\include\freetype\config";"$(SolutionDir)\..\ExternalLibs\gl";"$(SolutionDir)\..\ExternalLibs\glpng\zlib";"$(SolutionDir)\..\ExternalLibs\glpng\png";"$(SolutionDir)\..\ExternalLibs\libogg-1.3.0\include";"$(SolutionDir)\..\ExternalLibs\libvorbis-1.3.3\include";"$(SolutionDir)\..\ExternalLibs\freetype2\include";"$(SolutionDir)\..\ExternalLibs\FTGL\include";"$(SolutionDir)\..\ExternalLibs\SDL2\include";"$(SolutionDir)\..\ExternalLibs\AL\include";"$(SolutionDir)\..\ExternalLibs\ttvfs";"$(SolutionDir)\..\ExternalLibs\ttvfs_cfileapi";"$(SolutionDir)\..\ExternalLibs\lvpa";"$(SolutionDir)\..\ExternalLibs\lvpa\include";"$(SolutionDir)\..\ExternalLibs"" - PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;FTGL_LIBRARY_STATIC;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;BBGE_BUILD_WINDOWS=1;VFS_ENABLE_C_API=1" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;FTGL_LIBRARY_STATIC;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;BBGE_BUILD_WINDOWS=1;VFS_ENABLE_C_API=1;BBGE_USE_GLM=1" MinimalRebuild="true" BasicRuntimeChecks="0" RuntimeLibrary="3" @@ -116,7 +116,7 @@ OmitFramePointers="true" EnableFiberSafeOptimizations="true" AdditionalIncludeDirectories=""$(SolutionDir)\..\ExternalLibs\freetype2\include\freetype\config";"$(SolutionDir)\..\ExternalLibs\gl";"$(SolutionDir)\..\ExternalLibs\glpng\zlib";"$(SolutionDir)\..\ExternalLibs\glpng\png";"$(SolutionDir)\..\ExternalLibs\libogg-1.3.0\include";"$(SolutionDir)\..\ExternalLibs\libvorbis-1.3.3\include";"$(SolutionDir)\..\ExternalLibs\freetype2\include";"$(SolutionDir)\..\ExternalLibs\FTGL\include";"$(SolutionDir)\..\ExternalLibs\SDL2\include";"$(SolutionDir)\..\ExternalLibs\AL\include";"$(SolutionDir)\..\ExternalLibs\ttvfs";"$(SolutionDir)\..\ExternalLibs\ttvfs_cfileapi";"$(SolutionDir)\..\ExternalLibs\lvpa";"$(SolutionDir)\..\ExternalLibs\lvpa\include";"$(SolutionDir)\..\ExternalLibs"" - PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;FTGL_LIBRARY_STATIC;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;BBGE_BUILD_WINDOWS=1;_HAS_EXCEPTIONS=0;VFS_ENABLE_C_API=1" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;FTGL_LIBRARY_STATIC;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;BBGE_BUILD_WINDOWS=1;_HAS_EXCEPTIONS=0;VFS_ENABLE_C_API=1;BBGE_USE_GLM=1" StringPooling="true" ExceptionHandling="0" RuntimeLibrary="0"