Skip to content

Commit

Permalink
Add Tutorial_Hlms05_CustomizationPerObjArbitraryData
Browse files Browse the repository at this point in the history
  • Loading branch information
darksylinc committed Aug 14, 2024
1 parent 531afdf commit f29563a
Show file tree
Hide file tree
Showing 11 changed files with 828 additions and 0 deletions.
1 change: 1 addition & 0 deletions Samples/2.0/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ if( OGRE_BUILD_SAMPLES2 AND NOT OGRE_BUILD_SAMPLES2_SKIP )
add_subdirectory(Tutorials/Tutorial_Hlms02_CustomizationPerObj)
add_subdirectory(Tutorials/Tutorial_Hlms03_AlwaysOnTopA)
add_subdirectory(Tutorials/Tutorial_Hlms04_AlwaysOnTopB)
add_subdirectory(Tutorials/Tutorial_Hlms05_CustomizationPerObjArbitraryData)
if( OGRE_BUILD_COMPONENT_SCENE_FORMAT )
add_subdirectory(Tutorials/Tutorial_Memory)
endif()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#-------------------------------------------------------------------
# This file is part of the CMake build system for OGRE-Next
# (Object-oriented Graphics Rendering Engine)
# For the latest info, see http://www.ogre3d.org/
#
# The contents of this file are placed in the public domain. Feel
# free to make use of it in any way you like.
#-------------------------------------------------------------------

macro( add_recursive dir retVal )
file( GLOB_RECURSE ${retVal} ${dir}/*.h ${dir}/*.cpp ${dir}/*.c )
endmacro()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

include_directories(${CMAKE_SOURCE_DIR}/Components/Hlms/Common/include)
ogre_add_component_include_dir(Hlms/Pbs)

add_recursive( ./ SOURCE_FILES )

ogre_add_executable(Tutorial_Hlms05_CustomizationPerObjArbitraryData WIN32 MACOSX_BUNDLE ${SOURCE_FILES} ${SAMPLE_COMMON_RESOURCES})

target_link_libraries(Tutorial_Hlms05_CustomizationPerObjArbitraryData ${OGRE_LIBRARIES} ${OGRE_SAMPLES_LIBRARIES})
ogre_config_sample_lib(Tutorial_Hlms05_CustomizationPerObjArbitraryData)
ogre_config_sample_pkg(Tutorial_Hlms05_CustomizationPerObjArbitraryData)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

#include "Tutorial_Hlms05_CustomizationPerObjArbitraryData.h"

#include "Tutorial_Hlms05_CustomizationPerObjArbitraryDataGameState.h"

// Declares WinMain / main
#include "MainEntryPointHelper.h"
#include "System/Android/AndroidSystems.h"
#include "System/MainEntryPoints.h"

#if OGRE_PLATFORM != OGRE_PLATFORM_ANDROID
# if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMainApp( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR strCmdLine, INT nCmdShow )
# else
int mainApp( int argc, const char *argv[] )
# endif
{
return Demo::MainEntryPoints::mainAppSingleThreaded( DEMO_MAIN_ENTRY_PARAMS );
}
#endif

namespace Demo
{
void MainEntryPoints::createSystems( GameState **outGraphicsGameState,
GraphicsSystem **outGraphicsSystem,
GameState **outLogicGameState, LogicSystem **outLogicSystem )
{
Hlms05CustomizationPerObjArbitraryDataGameState *gfxGameState =
new Hlms05CustomizationPerObjArbitraryDataGameState(
"Tutorial_Hlms02_CustomizationPerObj showed how to show send a custom colour.\n"
"However such sample forces all customized objects to share the same colour\n"
"(or at most, a limited number of multiple colours).\n"
"\n"
"By design OgreNext disallows sending arbitrary per object data because such\n"
"functionality can easily get expensive, as it must live in a hot path: It happens\n"
"per object + per pass + per frame.\n"
"\n"
"However there are cases where users *want* to apply per-object custom data.\n"
"This sample shows how to do that. Performance is not really taken into consideration.\n"
"There may be ways to improve the performance hit depending on what assumptions you\n"
"can make, but this samples tries to be flexible.\n"
"\n"
"The code for this sample was sponsored by Open Robotics.\n"
"A variation of this implementation can be found at:\n"
"https://github.com/gazebosim/gz-rendering/blob/gz-rendering8/ogre2/src/"
"Ogre2GzHlmsPbsPrivate.cc \n"
"https://github.com/gazebosim/gz-rendering/blob/gz-rendering8/ogre2/src/"
"Ogre2GzHlmsPbsPrivate.h \n"
"https://github.com/gazebosim/gz-rendering/blob/gz-rendering8/ogre2/src/"
"Ogre2GzHlmsSharedPrivate.cc \n"
"https://github.com/gazebosim/gz-rendering/blob/gz-rendering8/ogre2/src/"
"Ogre2GzHlmsSharedPrivate.h \n"
"\n"
"\n"
"This sample depends on the media files:\n"
" * Samples/Media/2.0/materials/Tutorial_Hlms05_CustomizationPerObjArbitraryData\n" );

GraphicsSystem *graphicsSystem =
new Hlms05CustomizationPerObjArbitraryDataGraphicsSystem( gfxGameState );

gfxGameState->_notifyGraphicsSystem( graphicsSystem );

*outGraphicsGameState = gfxGameState;
*outGraphicsSystem = graphicsSystem;
}

void MainEntryPoints::destroySystems( GameState *graphicsGameState, GraphicsSystem *graphicsSystem,
GameState *logicGameState, LogicSystem *logicSystem )
{
delete graphicsSystem;
delete graphicsGameState;
}

const char *MainEntryPoints::getWindowTitle() { return "Tutoroial: Hlms02 Customization"; }
} // namespace Demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

#ifndef Demo_Tutorial_Hlms05_CustomizationPerObjArbitraryData_H
#define Demo_Tutorial_Hlms05_CustomizationPerObjArbitraryData_H

#include "GraphicsSystem.h"

#include "System/Android/AndroidSystems.h"
#include "Tutorial_Hlms05_MyHlmsPbs.h"

#include "Compositor/OgreCompositorManager2.h"
#include "OgreCamera.h"
#include "OgreConfigFile.h"
#include "OgreRoot.h"
#include "OgreSceneManager.h"
#include "OgreWindow.h"

#include "OgreArchiveManager.h"
#include "OgreHlmsManager.h"
#include "OgreHlmsPbs.h"

namespace Demo
{
class Hlms05CustomizationPerObjArbitraryDataGraphicsSystem final : public GraphicsSystem
{
Ogre::CompositorWorkspace *setupCompositor() override
{
Ogre::CompositorManager2 *compositorManager = mRoot->getCompositorManager2();
return compositorManager->addWorkspace( mSceneManager, mRenderWindow->getTexture(), mCamera,
"PbsMaterialsWorkspace", true );
}

void setupResources() override
{
GraphicsSystem::setupResources();

Ogre::ConfigFile cf;
cf.load( AndroidSystems::openFile( mResourcePath + "resources2.cfg" ) );

Ogre::String dataFolder = cf.getSetting( "DoNotUseAsResource", "Hlms", "" );

if( dataFolder.empty() )
dataFolder = AndroidSystems::isAndroid() ? "/" : "./";
else if( *( dataFolder.end() - 1 ) != '/' )
dataFolder += "/";

dataFolder += "2.0/scripts/materials/PbsMaterials";

addResourceLocation( dataFolder, getMediaReadArchiveType(), "General" );
}

void registerHlms() override
{
GraphicsSystem::registerHlms();

// Destroy the HlmsPbs that registerHlms(). We need to create it ourselves
// with our custom pieces.
Ogre::Root::getSingleton().getHlmsManager()->unregisterHlms( Ogre::HLMS_PBS );

Ogre::ConfigFile cf;
cf.load( AndroidSystems::openFile( mResourcePath + "resources2.cfg" ) );

#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE || OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
Ogre::String rootHlmsFolder =
Ogre::macBundlePath() + '/' + cf.getSetting( "DoNotUseAsResource", "Hlms", "" );
#else
Ogre::String rootHlmsFolder =
mResourcePath + cf.getSetting( "DoNotUseAsResource", "Hlms", "" );
#endif

if( rootHlmsFolder.empty() )
rootHlmsFolder = AndroidSystems::isAndroid() ? "/" : "./";
else if( *( rootHlmsFolder.end() - 1 ) != '/' )
rootHlmsFolder += "/";

// At this point rootHlmsFolder should be a valid path to the Hlms data folder

Ogre::MyHlmsPbs *hlmsPbs = 0;

// For retrieval of the paths to the different folders needed
Ogre::String mainFolderPath;
Ogre::StringVector libraryFoldersPaths;
Ogre::StringVector::const_iterator libraryFolderPathIt;
Ogre::StringVector::const_iterator libraryFolderPathEn;

Ogre::ArchiveManager &archiveManager = Ogre::ArchiveManager::getSingleton();

const Ogre::String &archiveType = getMediaReadArchiveType();

{
// Create & Register HlmsPbs
// Do the same for HlmsPbs:
Ogre::HlmsPbs::getDefaultPaths( mainFolderPath, libraryFoldersPaths );
Ogre::Archive *archivePbs =
archiveManager.load( rootHlmsFolder + mainFolderPath, archiveType, true );

// Get the library archive(s)
Ogre::ArchiveVec archivePbsLibraryFolders;
libraryFolderPathIt = libraryFoldersPaths.begin();
libraryFolderPathEn = libraryFoldersPaths.end();
while( libraryFolderPathIt != libraryFolderPathEn )
{
Ogre::Archive *archiveLibrary =
archiveManager.load( rootHlmsFolder + *libraryFolderPathIt, archiveType, true );
archivePbsLibraryFolders.push_back( archiveLibrary );
++libraryFolderPathIt;
}

{
// We now need to add our own customizations for this tutorial.
// The order in which we push to archivePbsLibraryFolders DOES matter
// since script order parsing matters.
Ogre::Archive *archiveLibrary = archiveManager.load(
rootHlmsFolder +
"2.0/scripts/materials/Tutorial_Hlms05_CustomizationPerObjArbitraryData",
archiveType, true );
archivePbsLibraryFolders.push_back( archiveLibrary );
}

// Create and register
hlmsPbs = OGRE_NEW Ogre::MyHlmsPbs( archivePbs, &archivePbsLibraryFolders );
Ogre::Root::getSingleton().getHlmsManager()->registerHlms( hlmsPbs );
}

Ogre::RenderSystem *renderSystem = mRoot->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 );
}
}

public:
Hlms05CustomizationPerObjArbitraryDataGraphicsSystem( GameState *gameState ) :
GraphicsSystem( gameState )
{
}
};
} // namespace Demo

#endif
Loading

0 comments on commit f29563a

Please sign in to comment.