![]() |
CUGL 3.0
Cornell University Game Library
|
#include <CUObjLoader.h>
Public Member Functions | |
ObjLoader () | |
void | dispose () override |
bool | init () override |
bool | init (const std::shared_ptr< ThreadPool > &threads) override |
![]() | |
Loader () | |
std::shared_ptr< ObjModel > | get (const std::string key) const |
std::shared_ptr< ObjModel > | operator[] (const std::string key) const |
size_t | loadCount () const override |
size_t | inFlight () const override |
void | enqueue (const std::string key) |
void | unloadAll () override |
virtual std::vector< std::string > | keys () override |
bool | purgeKey (const std::string key) override |
bool | verify (const std::string key) const override |
![]() | |
BaseLoader () | |
~BaseLoader () | |
virtual void | dispose () |
virtual bool | init () |
virtual bool | init (const std::shared_ptr< ThreadPool > &threads) |
std::shared_ptr< BaseLoader > | getHook () |
std::shared_ptr< ThreadPool > | getThreadPool () const |
void | setThreadPool (const std::shared_ptr< ThreadPool > &threads) |
void | setManager (AssetManager *manager) |
const AssetManager * | getManager () const |
void | setJsonKey (const std::string key) |
const std::string | getJsonKey () const |
void | setPriority (Uint32 priority) |
const Uint32 | getPriority () const |
bool | load (const std::string key, const std::string source) |
bool | load (const std::shared_ptr< JsonValue > &json) |
void | loadAsync (const std::string key, const std::string source, LoaderCallback callback) |
void | loadAsync (const std::shared_ptr< JsonValue > &json, LoaderCallback callback) |
bool | unload (const std::string key) |
bool | unload (const std::shared_ptr< JsonValue > &json) |
virtual void | unloadAll () |
virtual std::vector< std::string > | keys () |
bool | contains (const std::string key) const |
void | reserve (size_t amount) |
virtual size_t | loadCount () const |
size_t | waitCount () const |
virtual size_t | inFlight () const |
bool | complete () const |
float | progress () const |
Static Public Member Functions | |
static std::shared_ptr< ObjLoader > | alloc () |
static std::shared_ptr< ObjLoader > | alloc (const std::shared_ptr< ThreadPool > &threads) |
Protected Member Functions | |
void | preloadLibrary (const std::shared_ptr< MaterialLib > &lib, const std::shared_ptr< MtlLoader > &loader) |
void | materializeLibrary (const std::shared_ptr< MaterialLib > &lib, const std::shared_ptr< MtlLoader > &loader) |
std::shared_ptr< ObjModel > | preload (const std::string key, const std::string source) |
std::shared_ptr< ObjModel > | preload (const std::shared_ptr< JsonValue > &json) |
bool | materialize (const std::string key, const std::shared_ptr< ObjModel > &model, LoaderCallback callback) |
virtual bool | read (const std::string key, const std::string source, LoaderCallback callback, bool async) override |
virtual bool | read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async) override |
virtual bool | purgeJson (const std::shared_ptr< JsonValue > &json) override |
virtual bool | read (const std::string key, const std::string source, LoaderCallback callback, bool async) |
virtual bool | read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async) |
virtual bool | purgeKey (const std::string key) |
virtual bool | purgeJson (const std::shared_ptr< JsonValue > &json) |
virtual bool | verify (const std::string key) const |
Protected Attributes | |
std::shared_ptr< ObjParser > | _parser |
std::unordered_map< std::string, std::unordered_map< std::string, std::shared_ptr< Material > > > | _materials |
std::unordered_map< std::string, std::unordered_map< std::string, std::shared_ptr< MaterialInfo > > > | _matast |
std::unordered_map< std::string, SDL_Surface * > | _surfaces |
std::unordered_map< std::string, std::unordered_map< std::string, std::shared_ptr< MaterialLib > > > | _libraries |
![]() | |
std::unordered_map< std::string, std::shared_ptr< ObjModel > > | _assets |
std::unordered_set< std::string > | _queue |
![]() | |
std::string | _jsonKey |
Uint32 | _priority |
size_t | _reserved |
std::shared_ptr< ThreadPool > | _loader |
AssetManager * | _manager |
This class is a specific implementation of Loader<ObjModel>
This asset loader allows us to allocate OBJ models from the associated OBJ, MTL, and graphics::Texture
files. While most OBJ files package their MTL and texture files in the same directory as the OBJ file, that is not required. Using the JSON specification, it is possible to put these in different directories.
Note that an OBJ loader is only responsible for loading OBJ and MTL files. It is NOT responsible for loading graphics::Texture
files. If a MTL uses any textures, there should be a graphics::TextureLoader
associated with the AssetManager
for loading these textures. If there is no such loader, then textures will fail to load.
Note that this implementation uses a two phase loading system. First, it loads as much of the asset as possible without using OpenGL. This allows us to load the model in a separate thread. It then finishes off the remainder of asset loading using Application#schedule
. This is a good template for asset loaders in general.
As with all of our loaders, this loader is designed to be attached to an asset manager. Use the method getHook()
to get the appropriate pointer for attaching the loader.
|
inline |
Creates a new, uninitialized OBJ loader
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a loader on the heap, use one of the static constructors instead.
|
inlinestatic |
Returns a newly allocated OBJ loader.
This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.
This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool
.
|
inlinestatic |
Returns a newly allocated OBJ loader.
This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.
threads | The thread pool for asynchronous loading |
|
inlineoverridevirtual |
Disposes all resources and assets of this loader
Any assets loaded by this object will be immediately released by the loader. However, a texture may still be available if it is referenced by another smart pointer. OpenGL will only release a OBJ mesh once all smart pointer attached to the asset are null.
Once the loader is disposed, any attempts to load a new asset will fail. You must reinitialize the loader to begin loading assets again.
Reimplemented from cugl::BaseLoader.
|
inlineoverridevirtual |
Initializes a new OBJ loader.
This method bootstraps the loader with any initial resources that it needs to load assets. Attempts to load an asset before this method is called will fail.
This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool
.
Reimplemented from cugl::BaseLoader.
|
inlineoverridevirtual |
Initializes a new OBJ loader.
This method bootstraps the loader with any initial resources that it needs to load assets. Attempts to load an asset before this method is called will fail.
threads | The thread pool for asynchronous loading support |
Reimplemented from cugl::BaseLoader.
|
protected |
Creates an OpenGL buffer for the model, and assigns it the given key.
This method finishes the asset loading started in preload
. This step is not safe to be done in a separate thread. Instead, it takes place in the main CUGL thread via Application#schedule
.
Any associated MTL materials will be materialized at this time. This method will only work if all of the textures and MTL files associated with this OBJ file are in the same directory as it.
This method supports an optional callback function which reports whether the asset was successfully materialized.
key | The key to access the asset after loading |
model | The model to materialize |
callback | An optional callback for asynchronous loading |
|
protected |
Creates a material library from the given information.
For best performance, all MTL objects should be loaded in a previous pass. However, if that is not the case, then this method can load any additional materials that are needed. This method uses the materialize pass of scene3::MtlLoader
.
lib | The material lib to materialize |
loader | The material loader for the parent asset manager |
|
protected |
Loads the portion of this asset that is safe to load outside the main thread.
It is not safe to create an OpenGL buffer in a separate thread. However, it is safe to create a ObjModel
, so long as it does not have a graphics buffer. Hence this method does the maximum amount of work that can be done in asynchronous OBJ loading.
If the OBJ file has any associated materials, this method will create a thread-safe instance using MtlLoader#preload
. Those materials will be materialized the same time the OBJ mesh is materialized.
json | The JSON entry specifying the asset |
|
protected |
Loads the portion of this asset that is safe to load outside the main thread.
It is not safe to create an OpenGL buffer in a separate thread. However, it is safe to create a ObjModel
, so long as it does not have a graphics buffer. Hence this method does the maximum amount of work that can be done in asynchronous OBJ loading.
If the OBJ file has any associated materials, this method will create a thread-safe instance using MtlLoader#preload
. Those materials will be materialized the same time the OBJ mesh is materialized.
key | The key to access the asset after loading |
source | The pathname to the asset |
|
protected |
Loads the portion of a material library that is safe to load outside the main thread.
For best performance, all MTL objects should be loaded in a previous pass. However, if that is not the case, then this method can load any additional materials that are needed. This method uses the preload pass of scene3::MtlLoader
.
lib | The material lib to load |
loader | The material loader for the parent asset manager |
|
overrideprotectedvirtual |
Unloads the asset for the given directory entry
An asset may still be available if it is referenced by a smart pointer. See the description of the specific implementation for how assets are released.
This method clears the internal buffers of any materials or textures associated with this model.
json | The directory entry for the asset |
Reimplemented from cugl::BaseLoader.
|
overrideprotectedvirtual |
Internal method to support asset loading.
This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.
This method will split the loading across the preload
and materialize
methods. This ensures that asynchronous loading is safe.
This version of read provides support for JSON directories. An OBJ directory entry has the following values
"file": The path to the OBJ file "mtls": An object of key:value pairs defining MTL libraries
The "mtls" entry is optional. For each MTL library, the key should match the name of the MTL file referenced in the obj file. If there are any missing MTL libraries (or the "mtls" entry is missing entirely), then the loader will attempt to use the same directory as the OBJ file.
The value of MTL entries can either be a key of a previously loaded MTL library, or it can be a JSON entry of the type supported by MtlLoader
. In the case of the latter, this will cause the the material to be loaded implicitly.
json | The directory entry for the asset |
callback | An optional callback for asynchronous loading |
async | Whether the asset was loaded asynchronously |
Reimplemented from cugl::BaseLoader.
|
overrideprotectedvirtual |
Internal method to support asset loading.
This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.
This method will split the loading across the preload
and materialize
methods. This ensures that asynchronous loading is safe.
This method will only work if all of the textures and MTL files associated with this OBJ file are in the same directory as it.
key | The key to access the asset after loading |
source | The pathname to the asset |
callback | An optional callback for asynchronous loading |
async | Whether the asset was loaded asynchronously |
Reimplemented from cugl::BaseLoader.
|
protected |
The MTL libraries assocated with each model asset
|
protected |
The materials AST for each model asset
|
protected |
The materials parsed for each model asset
|
protected |
Integrated parser for finding objects and materials
|
protected |
Any textures that must be loaded on the fly