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

#include <CUObjParser.h>

Public Member Functions

 ObjParser ()
 
 ~ObjParser ()
 
void dispose ()
 
std::shared_ptr< ModelInfoparseObj (const std::string source, bool recurse)
 
std::shared_ptr< ModelInfoparseJson (const std::shared_ptr< JsonValue > &json)
 
std::shared_ptr< ModelInfoparseObj (const std::string key, const std::string source, bool recurse)
 
std::shared_ptr< ModelInfogetObj (const std::string key) const
 
std::shared_ptr< MaterialLibparseMtl (const std::string source)
 
std::shared_ptr< MaterialLibparseMtl (const std::string key, const std::string source)
 
std::shared_ptr< MaterialLibgetMtl (const std::string key) const
 
void clear ()
 

Public Attributes

bool debug
 
std::unordered_map< std::string, std::shared_ptr< TextureInfo > > textures
 
std::unordered_map< std::string, std::shared_ptr< MaterialLib > > materials
 
std::unordered_map< std::string, std::shared_ptr< ModelInfo > > models
 

Detailed Description

This class generates an AST for an OBJ file (and its associated MTL files).

This parse only provides limited support for OBJ and MTL files. We only support polygonal objects, with no freeform drawing. We also only support basic illumination (illum values 0-2) with bump mapping.

This class does not actually produce render data, as that is potentially time consuming. That step should be left up to an asset loader. Instead, this parser simply collates the information about the OBJ model into a single AST, as its data can potentially be spread over multiple files (including MTL and texture files).

Because OBJ data is spread over mutliple files, this parser is stateful. That means it can expand the current ModelInfo data by reading other files.

Constructor & Destructor Documentation

◆ ObjParser()

cugl::scene3::ObjParser::ObjParser ( )
inline

Creates a new OBJ parser.

This is a fairly lightweight object. Therefore it is safe to use this constructor with new (though std::make_shared is prefered).

◆ ~ObjParser()

cugl::scene3::ObjParser::~ObjParser ( )
inline

Deletes this OBJ parser, disposing all resource.

Member Function Documentation

◆ clear()

void cugl::scene3::ObjParser::clear ( )

Clears all internal caches.

This is a stateful parser. Once an OBJ or MTL file has been parsed, its data can be retrieved at any time with getObj or getMtl, respectively. This method clears all such state so that those methods return nullptr until a new file is parsed.

◆ dispose()

void cugl::scene3::ObjParser::dispose ( )
inline

Releases the data from all previously parsed files.

This method has the same affect as clear.

◆ getMtl()

std::shared_ptr< MaterialLib > cugl::scene3::ObjParser::getMtl ( const std::string  key) const

Returns the information for a previously parsed MTL file.

This method returns nullptr if the MTL file has not been yet parsed. For models that have been parsed, the retrieval key was specified at the time of parsing.

Parameters
keyThe retrieval key
Returns
the information for a previously parsed MTL file.

◆ getObj()

std::shared_ptr< ModelInfo > cugl::scene3::ObjParser::getObj ( const std::string  key) const

Returns the information for a previously parsed OBJ file.

This method returns nullptr if the OBJ file has not been yet parsed. For models that have been parsed, the retrieval key was specified at the time of parsing.

Parameters
keyThe retrieval key
Returns
the information for a previously parsed OBJ file.

◆ parseJson()

std::shared_ptr< ModelInfo > cugl::scene3::ObjParser::parseJson ( const std::shared_ptr< JsonValue > &  json)

Returns the information for the given OBJ model.

This method allows you to specify the location of the OBJ file, its MTL file, and any associated textures as a single JSON entry. An OBJ Json 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.

An MTL entry is either a string (which is a reference to the path to the MTL file) or a JSON object. Such a JSON object would have the following values:

 "file":         The path to the MTL file
 "textures":     An object of key:value pairs defining textures

The "textures" entry is optional. For each texture, the key should match the name of the texture in the MTL file. Any missing textures will attempt to be loaded if the parsing depth is correct.

The values for the texture entries should be strings or JSONs. If they are string, they should be either be a key referencing a previously loaded texture, or a path to the texture file (the loader interprets it as a path only if there is no key with that name). If it is a JSON, then the JSON should follow the same rules as graphics::Texture.

Parameters
jsonThe JSON value specifying the OBJ model
Returns
the information for the given OBJ model.

◆ parseMtl() [1/2]

std::shared_ptr< MaterialLib > cugl::scene3::ObjParser::parseMtl ( const std::string  key,
const std::string  source 
)

Returns the information for the given MTL file.

This method blocks until the MTL file is read. However, it does not read any imported files (like the Texture files). Instead, it assigns the TextureInfo attributes according the settings in the MTL file.

This method does not link a MaterialInfo to a ModelInfo object. That is the responsibility of the user.

This is a stateful parser. Once a MTL file has been parsed, its data can be retreived at any time with getMtl. This method uses the specified key for retrieval.

Parameters
keyThe retrieval key
sourceThe path to the MTL file
Returns
the information for the given MTL file.

◆ parseMtl() [2/2]

std::shared_ptr< MaterialLib > cugl::scene3::ObjParser::parseMtl ( const std::string  source)
inline

Returns the information for the given MTL file.

This method blocks until the MTL file is read. However, it does not read any imported files (like the Texture files). Instead, it assigns the TextureInfo attributes according the settings in the MTL file.

This method does not link a MaterialInfo to a ModelInfo object. That is the responsibility of the user.

This is a stateful parser. Once a MTL file has been parsed, its data can be retreived at any time with getMtl. This method uses the path to the MTL file as the key.

Parameters
sourceThe path to the MTL file
Returns
the information for the given MTL file.

◆ parseObj() [1/2]

std::shared_ptr< ModelInfo > cugl::scene3::ObjParser::parseObj ( const std::string  key,
const std::string  source,
bool  recurse 
)

Returns the information for the given OBJ file.

This method blocks until the OBJ file is read. If recurse is true, it will also read any imported MTL files (assuming that they are in the same directory as the OBJ files). If recurse ifs false, it will create entries in the ModelInfo object for the imported libraries with no values. The user can add the information for these libraries later.

This is a stateful parser. Once an OBJ file has been parsed, its data can be retreived at any time with getObj. This method uses the specified key for retrieval.

Parameters
keyThe retrieval key
sourceThe path to the OBJ file
recurseWhether to load any files referenced by source
Returns
the information for the given OBJ file.

◆ parseObj() [2/2]

std::shared_ptr< ModelInfo > cugl::scene3::ObjParser::parseObj ( const std::string  source,
bool  recurse 
)
inline

Returns the information for the given OBJ file.

This method blocks until the OBJ file is read. If recurse is true, it will also read any imported MTL files (assuming that they are in the same directory as the OBJ files). If recurse ifs false, it will create entries in the ModelInfo object for the imported libraries with no values. The user can add the information for these libraries later.

This is a stateful parser. Once an OBJ file has been parsed, its data can be retreived at any time with getObj. This method uses the path to the OBJ file as the key.

Parameters
sourceThe path to the OBJ file
recurseWhether to load any files referenced by source
Returns
the information for the given OBJ file.

Member Data Documentation

◆ debug

bool cugl::scene3::ObjParser::debug

Whether to emit debugging information

◆ materials

std::unordered_map<std::string,std::shared_ptr<MaterialLib> > cugl::scene3::ObjParser::materials

The information for previously parsed MTL files

◆ models

std::unordered_map<std::string,std::shared_ptr<ModelInfo> > cugl::scene3::ObjParser::models

The information for previously parsed OBJ files

◆ textures

std::unordered_map<std::string,std::shared_ptr<TextureInfo> > cugl::scene3::ObjParser::textures

The information for the referenced textures


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