diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..6d3bf6b --- /dev/null +++ b/.clang-format @@ -0,0 +1,22 @@ +# http://releases.llvm.org/6.0.0/tools/clang/docs/ClangFormatStyleOptions.html +BasedOnStyle: Google +AccessModifierOffset: -4 +AllowShortBlocksOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +BreakBeforeBraces: Allman +BraceWrapping: + AfterNamespace: false +BreakBeforeTernaryOperators: false +ColumnLimit: 100 +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +IndentWidth: 4 +Standard: Cpp11 +TabWidth: 4 +UseTab: ForIndentation +DerivePointerAlignment: false +PointerAlignment: Left +NamespaceIndentation: None +IndentCaseLabels: true \ No newline at end of file diff --git a/BuildDependencies_Debug.sh b/BuildDependencies_Debug.sh index 6253d02..528d4cc 100755 --- a/BuildDependencies_Debug.sh +++ b/BuildDependencies_Debug.sh @@ -1,5 +1,28 @@ #!/bin/sh echo "Building Cakelisp..." -cd Dependencies/cakelisp && jam -j4 && cd ../../ +cd Dependencies/cakelisp && jam -j4 && cd ../ echo "done!" + +# See the official script at +# https://raw.githubusercontent.com/OGRECave/ogre-next/master/Scripts/BuildScripts/output/build_ogre_linux_c%2B%2Blatest.sh +echo "Building Ogre dependencies..." +cd ogre-next-deps && mkdir -p build && cd build && cmake -G Ninja .. || exit $? +ninja || exit $? +ninja install || exit $? + +echo "Building Ogre..." +cd ../../ogre-next +if test ! -f Dependencies; then + ln -s ../ogre-next-deps/build/ogredeps Dependencies +fi +mkdir -p build/Debug +mkdir -p build/Release +cd build/Debug +echo "--- Building Ogre (Debug) ---" +cmake -D OGRE_USE_BOOST=0 -D OGRE_CONFIG_THREAD_PROVIDER=0 -D OGRE_CONFIG_THREADS=0 -D OGRE_BUILD_COMPONENT_SCENE_FORMAT=1 -D OGRE_BUILD_SAMPLES2=1 -D OGRE_BUILD_TESTS=1 -D CMAKE_BUILD_TYPE="Debug" -G Ninja ../.. || exit $? +ninja || exit $? +cd ../Release +echo "--- Building Ogre (Release) ---" +cmake -D OGRE_USE_BOOST=0 -D OGRE_CONFIG_THREAD_PROVIDER=0 -D OGRE_CONFIG_THREADS=0 -D OGRE_BUILD_COMPONENT_SCENE_FORMAT=1 -D OGRE_BUILD_SAMPLES2=1 -D OGRE_BUILD_TESTS=1 -D CMAKE_BUILD_TYPE="Release" -G Ninja ../.. || exit $? +ninja || exit $? diff --git a/Build_Debug.sh b/Build_Debug.sh index cbb956b..f156384 100755 --- a/Build_Debug.sh +++ b/Build_Debug.sh @@ -1,6 +1,6 @@ #!/bin/sh ./Dependencies/cakelisp/bin/cakelisp src/OgreCore.cake \ - && jam -j4 libGameLib.a + && jam -j4 -sDEBUG_BUILD=true libGameLib.a -./Dependencies/cakelisp/bin/cakelisp test/OgreApp.cake && cd test && jam -j4 +./Dependencies/cakelisp/bin/cakelisp test/OgreApp.cake && cd test && jam -j4 -sDEBUG_BUILD=true diff --git a/Jamrules b/Jamrules index 5817572..26834aa 100644 --- a/Jamrules +++ b/Jamrules @@ -43,6 +43,44 @@ else if $(NT) OS_DEPENDENT_DLLS = ; } +if $(DEBUG_BUILD) +{ + OGRE_BUILD_DIR = Dependencies/ogre-next/build/Debug ; + OGRE_C++FLAGS = -DOGRE_DEBUG_MODE=1 ; + OGRE_HDRS = Dependencies/ogre-next/OgreMain/include + Dependencies/ogre-next/Components/Hlms/Common/include + Dependencies/ogre-next/Components/Hlms/Pbs/include + Dependencies/ogre-next/Components/Hlms/Unlit/include + Dependencies/ogre-next/build/Debug/include + # Ogre overlay + Dependencies/ogre-next/Components/Overlay/include ; + OGRE_LINKLIBS = -lOgreHlmsPbs_d + -lOgreHlmsUnlit_d + -lOgreMain_d + # Ogre 2D overlays + -lOgreOverlay_d ; +} +else +{ + OGRE_BUILD_DIR = Dependencies/ogre-next/build/Release ; + OGRE_C++FLAGS = -DOGRE_DEBUG_MODE=0 ; + OGRE_HDRS = Dependencies/ogre-next/OgreMain/include + Dependencies/ogre-next/Components/Hlms/Common/include + Dependencies/ogre-next/Components/Hlms/Pbs/include + Dependencies/ogre-next/Components/Hlms/Unlit/include + Dependencies/ogre-next/build/Release/include + # Ogre overlay + Dependencies/ogre-next/Components/Overlay/include ; + OGRE_LINKLIBS = -lOgreHlmsPbs + -lOgreHlmsUnlit + -lOgreMain + # Ogre 2D overlays + -lOgreOverlay ; +} + +HDRS = src +$(OGRE_HDRS) ; + # Arguments used on all projects, regardless of any variables C++FLAGS = -std=c++11 -Wall -Wextra -Wno-unused-parameter # Only for profiling, i.e. not release builds @@ -51,6 +89,8 @@ C++FLAGS = -std=c++11 -Wall -Wextra -Wno-unused-parameter # Needed for dynamic linking -fPIC +$(OGRE_C++FLAGS) + $(OS_DEPENDENT_C++FLAGS) ; @@ -75,12 +115,15 @@ LINKLIBS = -lpthread # Functions for dynamically loading libraries (UNIX) $(OS_DEPENDENT_LINKLIBS) + +-L$(OGRE_BUILD_DIR)/lib +$(OGRE_LINKLIBS) ; LINKFLAGS = -g # -Wl = pass to linker # --export-dynamic = Export all symbols so dynamically loaded code can resolve their symbols to ours --Wl,-rpath,.,$(OS_DEPENDENT_LINKFLAGS) +-Wl,-rpath,.,$(OS_DEPENDENT_LINKFLAGS):$(OGRE_BUILD_DIR)/lib ; ## ## Jam stuff diff --git a/src/Jamfile b/src/Jamfile index 809d2f4..474437a 100644 --- a/src/Jamfile +++ b/src/Jamfile @@ -1,3 +1,3 @@ SubDir . src ; -Library libGameLib : OgreCore.cake.cpp ; +Library libGameLib : OgreCore.cake.cpp OgreInitialize.cpp ; diff --git a/src/OgreCore.cake b/src/OgreCore.cake index 245110b..1cdb9ee 100644 --- a/src/OgreCore.cake +++ b/src/OgreCore.cake @@ -1,5 +1,7 @@ -(c-import "") +(c-import "" + "OgreInitialize.hpp") (defun ogre-initialize (&return bool) + (OgreInitialize) (printf "Ogre initialized\n") (return true)) diff --git a/src/OgreInitialize.cpp b/src/OgreInitialize.cpp new file mode 100644 index 0000000..aab35f4 --- /dev/null +++ b/src/OgreInitialize.cpp @@ -0,0 +1,253 @@ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Woverloaded-virtual" +#include "OgreArchiveManager.h" +#include "OgreCamera.h" +#include "OgreConfigFile.h" +#include "OgreRoot.h" +#include "OgreWindow.h" + +#include "OgreHlmsManager.h" +#include "OgreHlmsPbs.h" +#include "OgreHlmsUnlit.h" + +#include "OgreItem.h" +#include "OgreMesh.h" +#include "OgreMesh2.h" +#include "OgreMeshManager.h" +#include "OgreMeshManager2.h" + +#include "Compositor/OgreCompositorManager2.h" +#pragma GCC diagnostic pop + +static Ogre::Root* root = nullptr; + +bool g_graphicsIntialized = false; + +static void registerHlms(void) +{ + using namespace Ogre; + + String resourcePath = "data/"; + + ConfigFile cf; + cf.load(resourcePath + "resources2.cfg"); + + String rootHlmsFolder = resourcePath + cf.getSetting("DoNotUseAsResource", "Hlms", ""); + + if (rootHlmsFolder.empty()) + rootHlmsFolder = "./"; + else if (*(rootHlmsFolder.end() - 1) != '/') + rootHlmsFolder += "/"; + + // At this point rootHlmsFolder should be a valid path to the Hlms data folder + + HlmsUnlit* hlmsUnlit = 0; + HlmsPbs* hlmsPbs = 0; + + // For retrieval of the paths to the different folders needed + String mainFolderPath; + StringVector libraryFoldersPaths; + StringVector::const_iterator libraryFolderPathIt; + StringVector::const_iterator libraryFolderPathEn; + + ArchiveManager& archiveManager = ArchiveManager::getSingleton(); + + { + // Create & Register HlmsUnlit + // Get the path to all the subdirectories used by HlmsUnlit + HlmsUnlit::getDefaultPaths(mainFolderPath, libraryFoldersPaths); + Archive* archiveUnlit = + archiveManager.load(rootHlmsFolder + mainFolderPath, "FileSystem", true); + ArchiveVec archiveUnlitLibraryFolders; + libraryFolderPathIt = libraryFoldersPaths.begin(); + libraryFolderPathEn = libraryFoldersPaths.end(); + while (libraryFolderPathIt != libraryFolderPathEn) + { + Archive* archiveLibrary = + archiveManager.load(rootHlmsFolder + *libraryFolderPathIt, "FileSystem", true); + archiveUnlitLibraryFolders.push_back(archiveLibrary); + ++libraryFolderPathIt; + } + + // Create and register the unlit Hlms + hlmsUnlit = OGRE_NEW HlmsUnlit(archiveUnlit, &archiveUnlitLibraryFolders); + Root::getSingleton().getHlmsManager()->registerHlms(hlmsUnlit); + } + + { + // Create & Register HlmsPbs + // Do the same for HlmsPbs: + HlmsPbs::getDefaultPaths(mainFolderPath, libraryFoldersPaths); + Archive* archivePbs = + archiveManager.load(rootHlmsFolder + mainFolderPath, "FileSystem", true); + + // Get the library archive(s) + ArchiveVec archivePbsLibraryFolders; + libraryFolderPathIt = libraryFoldersPaths.begin(); + libraryFolderPathEn = libraryFoldersPaths.end(); + while (libraryFolderPathIt != libraryFolderPathEn) + { + Archive* archiveLibrary = + archiveManager.load(rootHlmsFolder + *libraryFolderPathIt, "FileSystem", true); + archivePbsLibraryFolders.push_back(archiveLibrary); + ++libraryFolderPathIt; + } + + // Create and register + hlmsPbs = OGRE_NEW HlmsPbs(archivePbs, &archivePbsLibraryFolders); + Root::getSingleton().getHlmsManager()->registerHlms(hlmsPbs); + } + + RenderSystem* renderSystem = Root::getSingletonPtr()->getRenderSystem(); + if (renderSystem->getName() == "Direct3D11 Rendering Subsystem") + { + // Set lower limits 512kb instead of the default 4MB per Hlms in D3D 11.0 + // and below to avoid saturating AMD's discard limit (8MB) or + // saturate the PCIE bus in some low end machines. + bool supportsNoOverwriteOnTextureBuffers; + renderSystem->getCustomAttribute("MapNoOverwriteOnDynamicBufferSRV", + &supportsNoOverwriteOnTextureBuffers); + + if (!supportsNoOverwriteOnTextureBuffers) + { + hlmsPbs->setTextureBufferDefaultSize(512 * 1024); + hlmsUnlit->setTextureBufferDefaultSize(512 * 1024); + } + } +} + +void OgreInitialize() +{ + using namespace Ogre; + + const String pluginsFolder = "./data/"; + const String writeAccessFolder = "./"; + +#ifndef OGRE_STATIC_LIB +#if OGRE_DEBUG_MODE + const char* pluginsFile = "plugins_d.cfg"; +#else + const char* pluginsFile = "plugins.cfg"; +#endif +#endif + root = OGRE_NEW Root(pluginsFolder + pluginsFile, // + writeAccessFolder + "ogre.cfg", // + writeAccessFolder + "Ogre.log"); + + // TODO: Make this return false and quit the app + if (!root->showConfigDialog()) + return; // false + + // Initialize Root + root->getRenderSystem()->setConfigOption("sRGB Gamma Conversion", "Yes"); + // Window* window = root->initialise(/*autoCreateWindow=*/true, "Spargus Ogre"); + root->initialise(/*autoCreateWindow=*/true); + + Ogre::NameValuePairList windowSettings; + // unsigned long winHandle = reinterpret_cast(win.getBase()->getSystemHandle()); + // #ifdef _WIN32 + // unsigned long winGlContext = reinterpret_cast(wglGetCurrentContext()); + + // windowSettings["externalWindowHandle"] = StringConverter::toString(winHandle); + // windowSettings["externalGLContext"] = StringConverter::toString(winGlContext); + // windowSettings["externalGLControl"] = String("True"); + // #else + // // Deprecated. See ogre-next/RenderSystems/GL3Plus/src/windowing/GLX/OgreGLXWindow.cpp:237 + // // windowSettings["externalWindowHandle"] = StringConverter::toString(winHandle); + // windowSettings["parentWindowHandle"] = StringConverter::toString(winHandle); + // // sf::Context context; + // // unsigned long activeContextId = (unsigned long)context.getActiveContextId(); + // windowSettings["currentGLContext"] = String("True"); + // // windowSettings["currentGLContext"] = StringConverter::toString(activeContextId); + // // windowSettings["externalGLControl"] = String("True"); + // #endif + + int winWidth = 1024; + int winHeight = 1024; + + Window* window = + root->createRenderWindow("GameLib", winWidth, winHeight, true, &windowSettings); + + // window->_setVisible(true); + + registerHlms(); + + // Create SceneManager + const size_t numThreads = 1u; + SceneManager* sceneManager = root->createSceneManager(ST_GENERIC, numThreads, "SceneManager"); + + // Create & setup camera + Camera* camera = sceneManager->createCamera("Main Camera"); + + // Position it at 500 in Z direction + camera->setPosition(Vector3(0, 5, 15)); + // Look back along -Z + camera->lookAt(Vector3(0, 0, 0)); + camera->setNearClipDistance(0.2f); + camera->setFarClipDistance(1000.0f); + camera->setAutoAspectRatio(true); + + // Setup a basic compositor with a blue clear colour + CompositorManager2* compositorManager = root->getCompositorManager2(); + const String workspaceName("Demo Workspace"); + // const IdString definitionNameId = workspaceName; + const ColourValue backgroundColour(0.2f, 0.4f, 0.6f); + compositorManager->createBasicWorkspaceDef(workspaceName, backgroundColour, IdString()); + compositorManager->addWorkspace(sceneManager, window->getTexture(), camera, workspaceName, + true); + + // Mesh importing + { + Ogre::ResourceGroupManager::getSingleton().addResourceLocation("data/Models", "FileSystem", + "Models"); + + Ogre::v1::MeshPtr v1Mesh; + Ogre::MeshPtr v2Mesh; + // Load the v1 mesh. Notice the v1 namespace + // Also notice the HBU_STATIC flag; since the HBU_WRITE_ONLY + // bit would prohibit us from reading the data for importing. + v1Mesh = Ogre::v1::MeshManager::getSingleton().load( + "Chassis.mesh", Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME, + Ogre::v1::HardwareBuffer::HBU_STATIC, Ogre::v1::HardwareBuffer::HBU_STATIC); + + bool halfPosition = true; + bool halfUVs = true; + bool useQtangents = true; + + // Create a v2 mesh to import to, with a different name (arbitrary). + // Import the v1 mesh to v2 + v2Mesh = Ogre::MeshManager::getSingleton().createByImportingV1( + "Chassis.mesh Imported", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, + v1Mesh.get(), halfPosition, halfUVs, useQtangents); + + // We don't need the v1 mesh. Free CPU memory, get it out of the GPU. + // Leave it loaded if you want to use athene with v1 Entity. + v1Mesh->unload(); + + // Create an Item with the model we just imported. + // Notice we use the name of the imported model. We could also use the overload + // with the mesh pointer: + // item = sceneManager->createItem( v2Mesh, Ogre::SCENE_DYNAMIC ); + Ogre::Item* item = sceneManager->createItem( + "Chassis.mesh Imported", Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME, + Ogre::SCENE_DYNAMIC); + // Ogre::SceneNode* rootSceneNode = sceneManager->getRootSceneNode(Ogre::SCENE_DYNAMIC); + Ogre::SceneNode* rootSceneNode = sceneManager->getRootSceneNode(); + if (!rootSceneNode) + { + return; + } + // WTF + // Ogre::SceneNode* sceneNode = rootSceneNode->createChildSceneNode(Ogre::SCENE_DYNAMIC); + Ogre::SceneNode* sceneNode = static_cast( + rootSceneNode->createChild()); // rootSceneNode->createChildSceneNode(); + if (!sceneNode) + { + return; + } + sceneNode->attachObject(item); + // sceneNode->scale( 0.1f, 0.1f, 0.1f ); + } + + g_graphicsIntialized = true; +} diff --git a/src/OgreInitialize.hpp b/src/OgreInitialize.hpp new file mode 100644 index 0000000..efad2a2 --- /dev/null +++ b/src/OgreInitialize.hpp @@ -0,0 +1,3 @@ +#pragma once + +void OgreInitialize(); diff --git a/test/Jamrules b/test/Jamrules index 2dcd469..8904cc5 100644 --- a/test/Jamrules +++ b/test/Jamrules @@ -43,6 +43,42 @@ else if $(NT) OS_DEPENDENT_DLLS = ; } +# Ogre settings +if $(DEBUG_BUILD) +{ + OGRE_BUILD_DIR = ../Dependencies/ogre-next/build/Debug ; + OGRE_C++FLAGS = -DOGRE_DEBUG_MODE=1 ; + OGRE_HDRS = $(OGRE_BUILD_DIR)/OgreMain/include + $(OGRE_BUILD_DIR)/Components/Hlms/Common/include + $(OGRE_BUILD_DIR)/Components/Hlms/Pbs/include + $(OGRE_BUILD_DIR)/Components/Hlms/Unlit/include + $(OGRE_BUILD_DIR)/build/Debug/include + # Ogre overlay + $(OGRE_BUILD_DIR)/Components/Overlay/include ; + OGRE_LINKLIBS = -lOgreHlmsPbs_d + -lOgreHlmsUnlit_d + -lOgreMain_d + # Ogre 2D overlays + -lOgreOverlay_d ; +} +else +{ + OGRE_BUILD_DIR = ../Dependencies/ogre-next/build/Release ; + OGRE_C++FLAGS = -DOGRE_DEBUG_MODE=0 ; + OGRE_HDRS = $(OGRE_BUILD_DIR)/OgreMain/include + $(OGRE_BUILD_DIR)/Components/Hlms/Common/include + $(OGRE_BUILD_DIR)/Components/Hlms/Pbs/include + $(OGRE_BUILD_DIR)/Components/Hlms/Unlit/include + $(OGRE_BUILD_DIR)/build/Release/include + # Ogre overlay + $(OGRE_BUILD_DIR)/Components/Overlay/include ; + OGRE_LINKLIBS = -lOgreHlmsPbs + -lOgreHlmsUnlit + -lOgreMain + # Ogre 2D overlays + -lOgreOverlay ; +} + # Arguments used on all projects, regardless of any variables C++FLAGS = -std=c++11 -Wall -Wextra -Wno-unused-parameter # Only for profiling, i.e. not release builds @@ -52,6 +88,8 @@ C++FLAGS = -std=c++11 -Wall -Wextra -Wno-unused-parameter -fPIC $(OS_DEPENDENT_C++FLAGS) + +$(OGRE_C++FLAGS) ; # TODO: Make base hold all this weirdness? @@ -78,12 +116,15 @@ $(OS_DEPENDENT_LINKLIBS) -L../src -lGameLib + +-L$(OGRE_BUILD_DIR)/lib +$(OGRE_LINKLIBS) ; LINKFLAGS = -g # -Wl = pass to linker # --export-dynamic = Export all symbols so dynamically loaded code can resolve their symbols to ours --Wl,-rpath,.,$(OS_DEPENDENT_LINKFLAGS) +-Wl,$(OS_DEPENDENT_LINKFLAGS),-rpath,.:$(OGRE_BUILD_DIR)/lib ; ## ## Jam stuff