CUGL 3.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::scene3::ObjLoader Class Reference

#include <CUObjLoader.h>

Inheritance diagram for cugl::scene3::ObjLoader:
cugl::Loader< ObjModel > cugl::BaseLoader

Public Member Functions

 ObjLoader ()
 
void dispose () override
 
bool init () override
 
bool init (const std::shared_ptr< ThreadPool > &threads) override
 
- Public Member Functions inherited from cugl::Loader< ObjModel >
 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
 
- Public Member Functions inherited from cugl::BaseLoader
 BaseLoader ()
 
 ~BaseLoader ()
 
virtual void dispose ()
 
virtual bool init ()
 
virtual bool init (const std::shared_ptr< ThreadPool > &threads)
 
std::shared_ptr< BaseLoadergetHook ()
 
std::shared_ptr< ThreadPoolgetThreadPool () const
 
void setThreadPool (const std::shared_ptr< ThreadPool > &threads)
 
void setManager (AssetManager *manager)
 
const AssetManagergetManager () 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< ObjLoaderalloc ()
 
static std::shared_ptr< ObjLoaderalloc (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< ObjModelpreload (const std::string key, const std::string source)
 
std::shared_ptr< ObjModelpreload (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
 
- Protected Attributes inherited from cugl::Loader< ObjModel >
std::unordered_map< std::string, std::shared_ptr< ObjModel > > _assets
 
std::unordered_set< std::string > _queue
 
- Protected Attributes inherited from cugl::BaseLoader
std::string _jsonKey
 
Uint32 _priority
 
size_t _reserved
 
std::shared_ptr< ThreadPool_loader
 
AssetManager_manager
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ ObjLoader()

cugl::scene3::ObjLoader::ObjLoader ( )
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.

Member Function Documentation

◆ alloc() [1/2]

static std::shared_ptr< ObjLoader > cugl::scene3::ObjLoader::alloc ( )
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.

Returns
a newly allocated OBJ loader.

◆ alloc() [2/2]

static std::shared_ptr< ObjLoader > cugl::scene3::ObjLoader::alloc ( const std::shared_ptr< ThreadPool > &  threads)
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.

Parameters
threadsThe thread pool for asynchronous loading
Returns
a newly allocated OBJ loader.

◆ dispose()

void cugl::scene3::ObjLoader::dispose ( )
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.

◆ init() [1/2]

bool cugl::scene3::ObjLoader::init ( )
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.

Returns
true if the asset loader was initialized successfully

Reimplemented from cugl::BaseLoader.

◆ init() [2/2]

bool cugl::scene3::ObjLoader::init ( const std::shared_ptr< ThreadPool > &  threads)
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.

Parameters
threadsThe thread pool for asynchronous loading support
Returns
true if the asset loader was initialized successfully

Reimplemented from cugl::BaseLoader.

◆ materialize()

bool cugl::scene3::ObjLoader::materialize ( const std::string  key,
const std::shared_ptr< ObjModel > &  model,
LoaderCallback  callback 
)
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.

Parameters
keyThe key to access the asset after loading
modelThe model to materialize
callbackAn optional callback for asynchronous loading
Returns
true if materialization was successful

◆ materializeLibrary()

void cugl::scene3::ObjLoader::materializeLibrary ( const std::shared_ptr< MaterialLib > &  lib,
const std::shared_ptr< MtlLoader > &  loader 
)
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.

Parameters
libThe material lib to materialize
loaderThe material loader for the parent asset manager

◆ preload() [1/2]

std::shared_ptr< ObjModel > cugl::scene3::ObjLoader::preload ( const std::shared_ptr< JsonValue > &  json)
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.

Parameters
jsonThe JSON entry specifying the asset
Returns
the incomplete OBJ model

◆ preload() [2/2]

std::shared_ptr< ObjModel > cugl::scene3::ObjLoader::preload ( const std::string  key,
const std::string  source 
)
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.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset
Returns
the incomplete OBJ model

◆ preloadLibrary()

void cugl::scene3::ObjLoader::preloadLibrary ( const std::shared_ptr< MaterialLib > &  lib,
const std::shared_ptr< MtlLoader > &  loader 
)
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.

Parameters
libThe material lib to load
loaderThe material loader for the parent asset manager

◆ purgeJson()

virtual bool cugl::scene3::ObjLoader::purgeJson ( const std::shared_ptr< JsonValue > &  json)
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.

Parameters
jsonThe directory entry for the asset
Returns
true if the asset was successfully unloaded

Reimplemented from cugl::BaseLoader.

◆ read() [1/2]

virtual bool cugl::scene3::ObjLoader::read ( const std::shared_ptr< JsonValue > &  json,
LoaderCallback  callback,
bool  async 
)
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.

Parameters
jsonThe directory entry for the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

◆ read() [2/2]

virtual bool cugl::scene3::ObjLoader::read ( const std::string  key,
const std::string  source,
LoaderCallback  callback,
bool  async 
)
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.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

Member Data Documentation

◆ _libraries

std::unordered_map<std::string, std::unordered_map<std::string,std::shared_ptr<MaterialLib> > > cugl::scene3::ObjLoader::_libraries
protected

The MTL libraries assocated with each model asset

◆ _matast

std::unordered_map<std::string, std::unordered_map<std::string,std::shared_ptr<MaterialInfo> > > cugl::scene3::ObjLoader::_matast
protected

The materials AST for each model asset

◆ _materials

std::unordered_map<std::string, std::unordered_map<std::string,std::shared_ptr<Material> > > cugl::scene3::ObjLoader::_materials
protected

The materials parsed for each model asset

◆ _parser

std::shared_ptr<ObjParser> cugl::scene3::ObjLoader::_parser
protected

Integrated parser for finding objects and materials

◆ _surfaces

std::unordered_map<std::string,SDL_Surface*> cugl::scene3::ObjLoader::_surfaces
protected

Any textures that must be loaded on the fly


The documentation for this class was generated from the following file: