libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::Model Class Reference

A collection of Mesh objects loaded from an OBJ file. More...

#include <model.hpp>

Collaboration diagram for mx::Model:
[legend]

Public Member Functions

 Model ()=default
 Default constructor.
 ~Model ()=default
 Destructor — calls cleanup on all meshes.
 Model (const std::string &filename)
 Load an OBJ file synchronously.
 Model (const Model &m)=delete
Model & operator= (const Model &m)=delete
 Model (Model &&m) noexcept
 Move constructor.
Model & operator= (Model &&m) noexcept
 Move-assign operator.
void dumpMeshStats (const Mesh &m, size_t meshIndex, bool didCompress, bool compressRequested)
 Print mesh statistics to a stream.
bool openCompressedModel (const std::string &filename)
 Load a pre-compressed binary model format.
bool openModelString (const std::string &filename, const std::string &text, bool compress=true)
 Parse an OBJ model from an in-memory string.
bool openModel (const std::string &filename, bool compress=true)
 Load an OBJ file from disk.
void drawArrays ()
 Draw all meshes using stored bindings.
void drawArraysWithTexture (GLuint texture, const std::string texture_name)
 Draw all meshes with a texture override.
void drawArraysWithCubemap (GLuint cubemap, const std::string texture_name)
 Draw all meshes with a cubemap.
void printData (std::ostream &out)
 Print per-mesh vertex/index statistics.
void setShaderProgram (gl::ShaderProgram *shader_program, const std::string texture_name="texture1")
 Assign a shader program to be used for all draw calls.
void setTextures (const std::vector< GLuint > &textures)
 Assign a pre-loaded list of GL texture IDs to meshes in order.
void setTextures (gl::GLWindow *win, const std::string &filename, std::string prefix)
 Load and assign textures from a directory using a file prefix.
void setTexturesFromMTL (gl::GLWindow *win, const std::string &objDir="")
 Load textures using map_Kd paths from parsed MTL materials.
const std::vector< MXMaterial > & materials () const
const std::string & mtlLibPath () const
void saveOriginal ()
 Take a snapshot of all mesh geometries.
void resetToOriginal ()
 Restore all meshes from the last saveOriginal() snapshot.
void updateBuffers ()
 Push all mesh geometry changes to the GPU.
void recalculateNormals ()
 Recompute normals for all meshes.
void scale (float factor)
 Uniformly scale all meshes.
void scale (float sx, float sy, float sz)
 Non-uniformly scale all meshes.
void bend (DeformAxis axis, float angle, float center=0.0f, float range=1.0f)
 Bend all meshes.
void twist (DeformAxis axis, float angle, float center=0.0f)
 Twist all meshes.
void wave (DeformAxis axis, float amplitude, float frequency, float phase=0.0f)
 Wave-deform all meshes.
void ripple (float amplitude, float frequency, float phase=0.0f, float cx=0.0f, float cy=0.0f, float cz=0.0f)
 Ripple-deform all meshes.
void noise (float amplitude, unsigned int seed=12345)
 Add noise to all meshes.
void taper (DeformAxis axis, float factor, float center=0.0f)
 Taper all meshes.
void bulge (float factor, float cx=0.0f, float cy=0.0f, float cz=0.0f, float radius=1.0f)
 Bulge all meshes.
void morph (float t)
 Morph all meshes.
void translate (float tx, float ty, float tz)
 Translate all meshes.
void rotate (DeformAxis axis, float angle)
 Rotate all meshes.
void setNormalMap (GLuint normalMapTexture)
 Set the normal map for all meshes.
void setParallaxMap (GLuint heightMapTexture)
 Set the parallax map for all meshes.
void setDisplacementMap (GLuint displacementMapTexture)
 Set the displacement map for all meshes.
void setAmbientOcclusionMap (GLuint aoMapTexture)
 Set the ambient-occlusion map for all meshes.
void generateTangentBitangent ()
 Generate tangent/bitangent for all meshes.
void uvScroll (float uOffset, float vOffset)
 Scroll UVs for all meshes.
void uvScale (float uScale, float vScale)
 Scale UVs for all meshes.
void uvRotate (float angle)
 Rotate UVs for all meshes.
void applyChromaticAberration (float intensity)
 Apply chromatic aberration to all meshes.
void applyColorGrading (const glm::vec3 &colorShift)
 Apply colour grading to all meshes.
void applyHSV (float hueShift, float saturation, float value)
 Apply HSV adjustment to all meshes.
void setCubemapBlending (GLuint cubemap1, GLuint cubemap2, float blendFactor)
 Set cubemap blending for all meshes.

Public Attributes

std::vector< Mesh > meshes
 All sub-meshes in load order.

Private Member Functions

void parseLine (const std::string &line, Mesh &currentMesh, int &type, size_t &count)
 Parse a single MXMOD line and accumulate into currentMesh.
bool openOBJ (const std::string &filename, bool compress)
 Load a Wavefront OBJ file into meshes.
void loadMTL (const std::string &path)
 Parse a Wavefront .mtl file and populate materials_.

Private Attributes

gl::ShaderProgram * program = nullptr
 Active shader program.
std::string tex_name
 Sampler uniform name.
std::vector< MXMaterial > materials_
 Parsed MTL materials.
std::string mtlLibPath_
 Path to .mtl file from OBJ mtllib.

Detailed Description

A collection of Mesh objects loaded from an OBJ file.

Model is the top-level scene node. It opens an OBJ file (optionally with index compression), provides shader and texture bindings, and proxies every deformation and map operation to all contained meshes.

Definition at line 325 of file model.hpp.

Constructor & Destructor Documentation

◆ Model() [1/4]

mx::Model::Model ( )
default

Default constructor.

Referenced by Model(), Model(), operator=(), and operator=().

◆ ~Model()

mx::Model::~Model ( )
default

Destructor — calls cleanup on all meshes.

◆ Model() [2/4]

mx::Model::Model ( const std::string & filename)

Load an OBJ file synchronously.

Parameters
filenamePath to the .obj file.

Definition at line 592 of file model.cpp.

References openModel().

◆ Model() [3/4]

mx::Model::Model ( const Model & m)
delete

References Model().

◆ Model() [4/4]

mx::Model::Model ( Model && m)
noexcept

Move constructor.

Definition at line 596 of file model.cpp.

References meshes, Model(), program, and tex_name.

Member Function Documentation

◆ applyChromaticAberration()

void mx::Model::applyChromaticAberration ( float intensity)

Apply chromatic aberration to all meshes.

Definition at line 1332 of file model.cpp.

References meshes.

◆ applyColorGrading()

void mx::Model::applyColorGrading ( const glm::vec3 & colorShift)

Apply colour grading to all meshes.

Definition at line 1331 of file model.cpp.

References meshes.

◆ applyHSV()

void mx::Model::applyHSV ( float hueShift,
float saturation,
float value )

Apply HSV adjustment to all meshes.

Definition at line 1330 of file model.cpp.

References meshes.

◆ bend()

void mx::Model::bend ( DeformAxis axis,
float angle,
float center = 0.0f,
float range = 1.0f )

Bend all meshes.

See also
Mesh::bend

Definition at line 1324 of file model.cpp.

References meshes.

◆ bulge()

void mx::Model::bulge ( float factor,
float cx = 0.0f,
float cy = 0.0f,
float cz = 0.0f,
float radius = 1.0f )

Bulge all meshes.

See also
Mesh::bulge

Definition at line 1328 of file model.cpp.

References meshes.

◆ drawArrays()

void mx::Model::drawArrays ( )

Draw all meshes using stored bindings.

Definition at line 1098 of file model.cpp.

References meshes, program, and tex_name.

◆ drawArraysWithCubemap()

void mx::Model::drawArraysWithCubemap ( GLuint cubemap,
const std::string texture_name )

Draw all meshes with a cubemap.

Parameters
cubemapGL cubemap ID.
texture_nameUniform sampler name.

Definition at line 1111 of file model.cpp.

References meshes, and program.

◆ drawArraysWithTexture()

void mx::Model::drawArraysWithTexture ( GLuint texture,
const std::string texture_name )

Draw all meshes with a texture override.

Parameters
textureGL texture ID.
texture_nameUniform sampler name.

Definition at line 1106 of file model.cpp.

References meshes, and program.

◆ dumpMeshStats()

void mx::Model::dumpMeshStats ( const Mesh & m,
size_t meshIndex,
bool didCompress,
bool compressRequested )

Print mesh statistics to a stream.

Parameters
mMesh to inspect.
meshIndexZero-based mesh number.
didCompressWhether compression was performed.
compressRequestedWhether compression was requested.

Definition at line 656 of file model.cpp.

References mx::Mesh::bitangent, mx::Mesh::indices, mx::Mesh::norm, mx::system_out, mx::Mesh::tangent, mx::Mesh::tex, and mx::Mesh::vert.

Referenced by openModelString(), and openOBJ().

◆ generateTangentBitangent()

void mx::Model::generateTangentBitangent ( )

Generate tangent/bitangent for all meshes.

Definition at line 1338 of file model.cpp.

References meshes.

◆ loadMTL()

void mx::Model::loadMTL ( const std::string & path)
private

Parse a Wavefront .mtl file and populate materials_.

Definition at line 1228 of file model.cpp.

References mx::MXMaterial::d, mx::MXMaterial::illum, mx::MXMaterial::ka, mx::MXMaterial::kd, mx::MXMaterial::ks, mx::MXMaterial::map_kd, materials_, mx::MXMaterial::name, mx::MXMaterial::ns, and mx::system_out.

Referenced by openOBJ().

◆ materials()

const std::vector< MXMaterial > & mx::Model::materials ( ) const
inline
Returns
Read-only reference to parsed MTL materials.

Definition at line 431 of file model.hpp.

References materials_.

◆ morph()

void mx::Model::morph ( float t)

Morph all meshes.

See also
Mesh::morph

◆ mtlLibPath()

const std::string & mx::Model::mtlLibPath ( ) const
inline
Returns
Path to the MTL library referenced by the OBJ, or empty.

Definition at line 433 of file model.hpp.

References mtlLibPath_.

◆ noise()

void mx::Model::noise ( float amplitude,
unsigned int seed = 12345 )

Add noise to all meshes.

See also
Mesh::noise

◆ openCompressedModel()

bool mx::Model::openCompressedModel ( const std::string & filename)

Load a pre-compressed binary model format.

Parameters
filenamePath to the compressed model file.
Returns
true on success.

◆ openModel()

bool mx::Model::openModel ( const std::string & filename,
bool compress = true )

Load an OBJ file from disk.

Parameters
filenamePath to the .obj file.
compressApply index compression if true.
Returns
true on success.

Definition at line 615 of file model.cpp.

References mx::decompressString(), mx::ends_with(), meshes, openModelString(), openOBJ(), mx::readFile(), and mx::Exception::text().

Referenced by Model().

◆ openModelString()

bool mx::Model::openModelString ( const std::string & filename,
const std::string & text,
bool compress = true )

Parse an OBJ model from an in-memory string.

Parameters
filenamePath (used only for error messages).
textOBJ content as a string.
compressApply index compression if true.
Returns
true on success.

Definition at line 669 of file model.cpp.

References mx::Mesh::bitangent, mx::Mesh::compressIndices(), dumpMeshStats(), mx::Mesh::generateBuffers(), mx::Mesh::generateTangentBitangent(), mx::Mesh::indices, meshes, mx::Mesh::norm, parseLine(), mx::Mesh::recalculateNormals(), mx::Mesh::setShapeType(), mx::system_out, mx::Mesh::tangent, mx::Mesh::tex, mx::Mesh::texture, and mx::Mesh::vert.

Referenced by openModel().

◆ openOBJ()

◆ operator=() [1/2]

Model & mx::Model::operator= ( const Model & m)
delete

References Model().

◆ operator=() [2/2]

Model & mx::Model::operator= ( Model && m)
noexcept

Move-assign operator.

Definition at line 601 of file model.cpp.

References meshes, Model(), program, and tex_name.

◆ parseLine()

void mx::Model::parseLine ( const std::string & line,
Mesh & currentMesh,
int & type,
size_t & count )
private

Parse a single MXMOD line and accumulate into currentMesh.

Definition at line 795 of file model.cpp.

References mx::Mesh::bitangent, mx::Mesh::indices, mx::Mesh::norm, mx::Mesh::normIndex, mx::Mesh::tangent, mx::Mesh::tex, mx::Mesh::texIndex, mx::Mesh::vert, and mx::Mesh::vertIndex.

Referenced by openModelString().

◆ printData()

void mx::Model::printData ( std::ostream & out)

Print per-mesh vertex/index statistics.

Parameters
outOutput stream.

◆ recalculateNormals()

void mx::Model::recalculateNormals ( )

Recompute normals for all meshes.

Definition at line 1319 of file model.cpp.

References meshes.

◆ resetToOriginal()

void mx::Model::resetToOriginal ( )

Restore all meshes from the last saveOriginal() snapshot.

Definition at line 1317 of file model.cpp.

References meshes.

◆ ripple()

void mx::Model::ripple ( float amplitude,
float frequency,
float phase = 0.0f,
float cx = 0.0f,
float cy = 0.0f,
float cz = 0.0f )

Ripple-deform all meshes.

See also
Mesh::ripple

Definition at line 1327 of file model.cpp.

References meshes.

◆ rotate()

void mx::Model::rotate ( DeformAxis axis,
float angle )

Rotate all meshes.

◆ saveOriginal()

void mx::Model::saveOriginal ( )

Take a snapshot of all mesh geometries.

Definition at line 1316 of file model.cpp.

References meshes.

◆ scale() [1/2]

void mx::Model::scale ( float factor)

Uniformly scale all meshes.

Parameters
factorScale multiplier.

Definition at line 1321 of file model.cpp.

References meshes.

◆ scale() [2/2]

void mx::Model::scale ( float sx,
float sy,
float sz )

Non-uniformly scale all meshes.

Definition at line 1322 of file model.cpp.

References meshes.

◆ setAmbientOcclusionMap()

void mx::Model::setAmbientOcclusionMap ( GLuint aoMapTexture)

Set the ambient-occlusion map for all meshes.

Definition at line 1337 of file model.cpp.

References meshes.

◆ setCubemapBlending()

void mx::Model::setCubemapBlending ( GLuint cubemap1,
GLuint cubemap2,
float blendFactor )

Set cubemap blending for all meshes.

◆ setDisplacementMap()

void mx::Model::setDisplacementMap ( GLuint displacementMapTexture)

Set the displacement map for all meshes.

Definition at line 1336 of file model.cpp.

References meshes.

◆ setNormalMap()

void mx::Model::setNormalMap ( GLuint normalMapTexture)

Set the normal map for all meshes.

Definition at line 1334 of file model.cpp.

References meshes.

◆ setParallaxMap()

void mx::Model::setParallaxMap ( GLuint heightMapTexture)

Set the parallax map for all meshes.

Definition at line 1335 of file model.cpp.

References meshes.

◆ setShaderProgram()

void mx::Model::setShaderProgram ( gl::ShaderProgram * shader_program,
const std::string texture_name = "texture1" )

Assign a shader program to be used for all draw calls.

Parameters
shader_programPointer to a compiled ShaderProgram.
texture_nameSampler uniform name (default "texture1").

Definition at line 1093 of file model.cpp.

References program, and tex_name.

◆ setTextures() [1/2]

void mx::Model::setTextures ( const std::vector< GLuint > & textures)

Assign a pre-loaded list of GL texture IDs to meshes in order.

Parameters
texturesVector of GL texture IDs.

Definition at line 1119 of file model.cpp.

References meshes.

Referenced by setTextures(), and setTexturesFromMTL().

◆ setTextures() [2/2]

void mx::Model::setTextures ( gl::GLWindow * win,
const std::string & filename,
std::string prefix )

Load and assign textures from a directory using a file prefix.

Parameters
winOwner window (for asset path).
filenameBase filename.
prefixDirectory prefix.

Definition at line 1128 of file model.cpp.

References gl::loadTexture(), setTextures(), mx::system_err, mx::system_out, and mx::Exception::text().

◆ setTexturesFromMTL()

void mx::Model::setTexturesFromMTL ( gl::GLWindow * win,
const std::string & objDir = "" )

Load textures using map_Kd paths from parsed MTL materials.

Parameters
winOwner window.
objDirDirectory containing the OBJ (for resolving texture paths), or empty to use mtl dir.

Definition at line 1269 of file model.cpp.

References gl::loadTexture(), materials_, mtlLibPath_, setTextures(), mx::system_err, and mx::system_out.

◆ taper()

void mx::Model::taper ( DeformAxis axis,
float factor,
float center = 0.0f )

Taper all meshes.

See also
Mesh::taper

◆ translate()

void mx::Model::translate ( float tx,
float ty,
float tz )

Translate all meshes.

Definition at line 1323 of file model.cpp.

References meshes.

◆ twist()

void mx::Model::twist ( DeformAxis axis,
float angle,
float center = 0.0f )

Twist all meshes.

See also
Mesh::twist

Definition at line 1325 of file model.cpp.

References meshes.

◆ updateBuffers()

void mx::Model::updateBuffers ( )

Push all mesh geometry changes to the GPU.

Definition at line 1318 of file model.cpp.

References meshes.

◆ uvRotate()

void mx::Model::uvRotate ( float angle)

Rotate UVs for all meshes.

◆ uvScale()

void mx::Model::uvScale ( float uScale,
float vScale )

Scale UVs for all meshes.

◆ uvScroll()

void mx::Model::uvScroll ( float uOffset,
float vOffset )

Scroll UVs for all meshes.

Definition at line 1329 of file model.cpp.

References meshes.

◆ wave()

void mx::Model::wave ( DeformAxis axis,
float amplitude,
float frequency,
float phase = 0.0f )

Wave-deform all meshes.

See also
Mesh::wave

Definition at line 1326 of file model.cpp.

References meshes.

Member Data Documentation

◆ materials_

std::vector<MXMaterial> mx::Model::materials_
private

Parsed MTL materials.

Definition at line 503 of file model.hpp.

Referenced by loadMTL(), materials(), openOBJ(), and setTexturesFromMTL().

◆ meshes

◆ mtlLibPath_

std::string mx::Model::mtlLibPath_
private

Path to .mtl file from OBJ mtllib.

Definition at line 504 of file model.hpp.

Referenced by mtlLibPath(), openOBJ(), and setTexturesFromMTL().

◆ program

gl::ShaderProgram* mx::Model::program = nullptr
private

Active shader program.

Definition at line 501 of file model.hpp.

Referenced by drawArrays(), drawArraysWithCubemap(), drawArraysWithTexture(), Model(), operator=(), and setShaderProgram().

◆ tex_name

std::string mx::Model::tex_name
private

Sampler uniform name.

Definition at line 502 of file model.hpp.

Referenced by drawArrays(), Model(), operator=(), and setShaderProgram().


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