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

A single OpenGL sub-mesh: geometry buffers, texture bindings, and deformers. More...

#include <model.hpp>

Collaboration diagram for mx::Mesh:
[legend]

Public Member Functions

 Mesh ()
 Default constructor — zeroes all buffer IDs.
 ~Mesh ()
 Destructor — calls cleanup().
 Mesh (const Mesh &m)=delete
Mesh & operator= (const Mesh &m)=delete
 Mesh (Mesh &&m) noexcept
 Move constructor.
Mesh & operator= (Mesh &&m) noexcept
 Move-assign operator.
void compressIndices ()
 Build an optimised index list from the flat vertex arrays.
void generateBuffers ()
 Upload vertex data to the GPU (must be called after populating arrays).
void cleanup ()
 Delete all GL objects (VAO, VBOs, textures).
void draw ()
 Issue a GL draw call using the stored VAO/index count.
void drawWithForcedTexture (gl::ShaderProgram &shader, GLuint texture, const std::string texture_name)
 Draw with a texture override without changing the mesh's stored texture.
void setShapeType (GLuint type)
 Set the GL primitive drawing type.
void bindTexture (gl::ShaderProgram &shader, const std::string texture_name)
 Bind the mesh's primary texture to a shader sampler.
void bindCubemapTexture (gl::ShaderProgram &shader, GLuint cubemap, const std::string texture_name)
 Bind a cubemap texture to a shader sampler.
void saveOriginal ()
 Take a snapshot of the current vertex and normal arrays.
void resetToOriginal ()
 Restore vertex and normal arrays from the last saveOriginal() snapshot.
void updateBuffers ()
 Synchronise CPU-side vertex data to the GPU buffers.
void recalculateNormals ()
 Recompute per-vertex normals from the current vertex positions.
void setNormalMap (GLuint normalMapTexture)
void setParallaxMap (GLuint heightMapTexture)
void setDisplacementMap (GLuint displacementMapTexture)
void setAmbientOcclusionMap (GLuint aoMapTexture)
void generateTangentBitangent ()
 Compute tangent and bitangent vectors for normal mapping.
void uvScroll (float uOffset, float vOffset)
 Scroll UV coordinates by an offset.
void uvScale (float uScale, float vScale)
 Scale UV coordinates.
void uvRotate (float angle)
 Rotate UV coordinates around the origin.
void applyChromaticAberration (float intensity)
 Apply a chromatic aberration effect to vertex colours.
void applyColorGrading (const glm::vec3 &colorShift)
 Shift vertex colours by a multiplied colour vector.
void applyHSV (float hueShift, float saturation, float value)
 Apply hue/saturation/value adjustment to vertex colours.
void setCubemapBlending (GLuint cubemap1, GLuint cubemap2, float blendFactor)
 Blend between two cubemap textures in the shader.
GLuint getNormalMap () const
GLuint getParallaxMap () const
GLuint getAmbientOcclusionMap () const
void scale (float factor)
 Uniformly scale all vertices.
void scale (float sx, float sy, float sz)
 Non-uniformly scale vertices along each axis.
void bend (DeformAxis axis, float angle, float center=0.0f, float range=1.0f)
 Bend the mesh around an axis.
void twist (DeformAxis axis, float angle, float center=0.0f)
 Twist vertices around an axis.
void wave (DeformAxis axis, float amplitude, float frequency, float phase=0.0f)
 Apply a sinusoidal wave deformation.
void ripple (float amplitude, float frequency, float phase=0.0f, float cx=0.0f, float cy=0.0f, float cz=0.0f)
 Apply a radial ripple deformation centred on a point.
void noise (float amplitude, unsigned int seed=12345)
 Add random noise to vertex positions.
void taper (DeformAxis axis, float factor, float center=0.0f)
 Taper (scale cross-section) along an axis.
void bulge (float factor, float cx=0.0f, float cy=0.0f, float cz=0.0f, float radius=1.0f)
 Bulge vertices outward from a sphere centre.
void morph (float t)
 Interpolate (morph) vertices between original and current.
void translate (float tx, float ty, float tz)
 Translate all vertices by an offset.
void rotate (DeformAxis axis, float angle)
 Rotate all vertices around an axis.

Public Attributes

std::vector< GLfloat > vert
 Vertex position data (x,y,z triples).
std::vector< GLfloat > tex
 Texture coordinate data (u,v pairs).
std::vector< GLfloat > norm
 Normal vector data (x,y,z triples).
std::vector< GLfloat > tangent
 Tangent vector data.
std::vector< GLfloat > bitangent
 Bitangent vector data.
std::vector< GLuint > indices
 Optional index list for indexed drawing.
std::vector< GLfloat > originalVert
 Saved copy of vertex positions.
std::vector< GLfloat > originalNorm
 Saved copy of normals.
bool hasOriginal
 Whether an original snapshot exists.
GLuint shape_type
 GL primitive type (e.g. GL_TRIANGLES).
GLuint texture
 Primary diffuse texture ID.
std::string materialName
 Material name from OBJ usemtl directive.
GLuint EBO
GLuint VAO
GLuint positionVBO
GLuint normalVBO
GLuint texCoordVBO
GLuint tangentVBO
GLuint bitangentVBO
GLuint normalMapTexture
 Normal map texture ID.
GLuint parallaxMapTexture
 Parallax/height map texture ID.
GLuint aoMapTexture
 Ambient occlusion map texture ID.
GLuint displacementMapTexture
 Displacement map texture ID.
size_t vertIndex
 Current write position in the vertex array.
size_t texIndex
 Current write position in the UV array.
size_t normIndex
 Current write position in the normal array.

Detailed Description

A single OpenGL sub-mesh: geometry buffers, texture bindings, and deformers.

Stores vertex, normal, UV, tangent, and bitangent arrays together with an optional index list. All geometry lives in CPU memory and must be synchronised to the GPU via generateBuffers() / updateBuffers(). A snapshot of the original geometry can be preserved with saveOriginal() and restored with resetToOriginal().

Definition at line 66 of file model.hpp.

Constructor & Destructor Documentation

◆ Mesh() [1/3]

mx::Mesh::Mesh ( )

Default constructor — zeroes all buffer IDs.

Definition at line 70 of file model.cpp.

References aoMapTexture, bitangentVBO, displacementMapTexture, EBO, hasOriginal, normalMapTexture, normalVBO, normIndex, parallaxMapTexture, positionVBO, shape_type, tangentVBO, texCoordVBO, texIndex, texture, VAO, and vertIndex.

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

◆ ~Mesh()

mx::Mesh::~Mesh ( )

Destructor — calls cleanup().

Definition at line 153 of file model.cpp.

References cleanup().

◆ Mesh() [2/3]

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

References Mesh().

◆ Mesh() [3/3]

Member Function Documentation

◆ applyChromaticAberration()

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

Apply a chromatic aberration effect to vertex colours.

Parameters
intensityAberration magnitude.

Definition at line 585 of file model.cpp.

◆ applyColorGrading()

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

Shift vertex colours by a multiplied colour vector.

Parameters
colorShiftRGB colour shift (each channel 0–1 or beyond).

Definition at line 584 of file model.cpp.

◆ applyHSV()

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

Apply hue/saturation/value adjustment to vertex colours.

Parameters
hueShiftHue rotation in degrees.
saturationSaturation scale (1.0 = unchanged).
valueValue scale (1.0 = unchanged).

Definition at line 583 of file model.cpp.

◆ bend()

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

Bend the mesh around an axis.

Parameters
axisAxis to bend around.
angleBend angle in degrees.
centerCentre point of the bend region.
rangeRadial range of influence.

Definition at line 478 of file model.cpp.

References vert, mx::X, mx::Y, and mx::Z.

◆ bindCubemapTexture()

void mx::Mesh::bindCubemapTexture ( gl::ShaderProgram & shader,
GLuint cubemap,
const std::string texture_name )

Bind a cubemap texture to a shader sampler.

Parameters
shaderBound shader program.
cubemapGL cubemap texture ID.
texture_nameUniform sampler name.

Definition at line 325 of file model.cpp.

References gl::ShaderProgram::setUniform().

◆ bindTexture()

void mx::Mesh::bindTexture ( gl::ShaderProgram & shader,
const std::string texture_name )

Bind the mesh's primary texture to a shader sampler.

Parameters
shaderBound shader program.
texture_nameUniform sampler name.

Definition at line 299 of file model.cpp.

References normalMapTexture, gl::ShaderProgram::setUniform(), and texture.

◆ bulge()

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

Bulge vertices outward from a sphere centre.

Parameters
factorBulge strength.
cx,cy,czBulge centre position.
radiusSphere radius of influence.

Definition at line 555 of file model.cpp.

References vert.

◆ cleanup()

void mx::Mesh::cleanup ( )

Delete all GL objects (VAO, VBOs, textures).

Definition at line 155 of file model.cpp.

References bitangentVBO, EBO, normalVBO, positionVBO, tangentVBO, texCoordVBO, and VAO.

Referenced by operator=(), and ~Mesh().

◆ compressIndices()

void mx::Mesh::compressIndices ( )

Build an optimised index list from the flat vertex arrays.

Definition at line 175 of file model.cpp.

References indices, norm, mx::Vertex::normal, mx::Vertex::position, tex, mx::Vertex::texCoord, and vert.

Referenced by mx::Model::openModelString(), and mx::Model::openOBJ().

◆ draw()

void mx::Mesh::draw ( )

Issue a GL draw call using the stored VAO/index count.

Definition at line 292 of file model.cpp.

References indices, shape_type, VAO, and vert.

Referenced by drawWithForcedTexture().

◆ drawWithForcedTexture()

void mx::Mesh::drawWithForcedTexture ( gl::ShaderProgram & shader,
GLuint texture,
const std::string texture_name )

Draw with a texture override without changing the mesh's stored texture.

Parameters
shaderBound shader program.
textureTemporary GL texture to use.
texture_nameUniform sampler name in the shader.

Definition at line 311 of file model.cpp.

References draw(), normalMapTexture, and gl::ShaderProgram::setUniform().

◆ generateBuffers()

void mx::Mesh::generateBuffers ( )

Upload vertex data to the GPU (must be called after populating arrays).

Definition at line 242 of file model.cpp.

References bitangent, bitangentVBO, EBO, indices, norm, normalVBO, positionVBO, tangent, tangentVBO, tex, texCoordVBO, VAO, and vert.

Referenced by mx::Model::openModelString(), and mx::Model::openOBJ().

◆ generateTangentBitangent()

void mx::Mesh::generateTangentBitangent ( )

Compute tangent and bitangent vectors for normal mapping.

Definition at line 388 of file model.cpp.

References bitangent, indices, tangent, tex, and vert.

Referenced by mx::Model::openModelString(), and mx::Model::openOBJ().

◆ getAmbientOcclusionMap()

GLuint mx::Mesh::getAmbientOcclusionMap ( ) const
inline
Returns
Stored ambient-occlusion map texture ID.

Definition at line 223 of file model.hpp.

References aoMapTexture.

◆ getNormalMap()

GLuint mx::Mesh::getNormalMap ( ) const
inline
Returns
Stored normal map texture ID.

Definition at line 219 of file model.hpp.

References normalMapTexture.

◆ getParallaxMap()

GLuint mx::Mesh::getParallaxMap ( ) const
inline
Returns
Stored parallax map texture ID.

Definition at line 221 of file model.hpp.

References parallaxMapTexture.

◆ morph()

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

Interpolate (morph) vertices between original and current.

Parameters
tInterpolation factor (0 = original, 1 = current).

◆ noise()

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

Add random noise to vertex positions.

Parameters
amplitudeNoise magnitude.
seedRNG seed for reproducibility.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

◆ recalculateNormals()

void mx::Mesh::recalculateNormals ( )

Recompute per-vertex normals from the current vertex positions.

Definition at line 345 of file model.cpp.

References indices, norm, and vert.

Referenced by mx::Model::openModelString(), and mx::Model::openOBJ().

◆ resetToOriginal()

void mx::Mesh::resetToOriginal ( )

Restore vertex and normal arrays from the last saveOriginal() snapshot.

Definition at line 337 of file model.cpp.

References hasOriginal, norm, originalNorm, originalVert, updateBuffers(), and vert.

◆ ripple()

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

Apply a radial ripple deformation centred on a point.

Parameters
amplitudeWave amplitude.
frequencySpatial frequency.
phasePhase offset.
cx,cy,czCentre of the ripple.

Definition at line 540 of file model.cpp.

References vert.

◆ rotate()

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

Rotate all vertices around an axis.

Parameters
axisRotation axis.
angleAngle in degrees.

◆ saveOriginal()

void mx::Mesh::saveOriginal ( )

Take a snapshot of the current vertex and normal arrays.

Definition at line 331 of file model.cpp.

References hasOriginal, norm, originalNorm, originalVert, and vert.

◆ scale() [1/2]

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

Uniformly scale all vertices.

Parameters
factorScale multiplier.

Definition at line 460 of file model.cpp.

References scale().

Referenced by scale().

◆ scale() [2/2]

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

Non-uniformly scale vertices along each axis.

Parameters
sxX scale.
syY scale.
szZ scale.

Definition at line 462 of file model.cpp.

References vert.

◆ setAmbientOcclusionMap()

void mx::Mesh::setAmbientOcclusionMap ( GLuint aoMapTexture)
Parameters
aoMapTextureGL texture ID to use as an ambient-occlusion map.

Definition at line 590 of file model.cpp.

References aoMapTexture.

◆ setCubemapBlending()

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

Blend between two cubemap textures in the shader.

Parameters
cubemap1First cubemap GL ID.
cubemap2Second cubemap GL ID.
blendFactorInterpolation factor (0 = fully cubemap1, 1 = fully cubemap2).

◆ setDisplacementMap()

void mx::Mesh::setDisplacementMap ( GLuint displacementMapTexture)
Parameters
displacementMapTextureGL texture ID to use as a displacement map.

Definition at line 589 of file model.cpp.

References displacementMapTexture.

◆ setNormalMap()

void mx::Mesh::setNormalMap ( GLuint normalMapTexture)
Parameters
normalMapTextureGL texture ID to use as a normal map.

Definition at line 587 of file model.cpp.

References normalMapTexture.

◆ setParallaxMap()

void mx::Mesh::setParallaxMap ( GLuint heightMapTexture)
Parameters
heightMapTextureGL texture ID to use as a parallax/height map.

Definition at line 588 of file model.cpp.

References parallaxMapTexture.

◆ setShapeType()

void mx::Mesh::setShapeType ( GLuint type)

Set the GL primitive drawing type.

Parameters
typeGL_TRIANGLES, GL_LINES, etc.

Definition at line 166 of file model.cpp.

References shape_type.

Referenced by mx::Model::openModelString(), and mx::Model::openOBJ().

◆ taper()

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

Taper (scale cross-section) along an axis.

Parameters
axisTaper axis.
factorTaper rate.
centerCentre position.

◆ translate()

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

Translate all vertices by an offset.

Parameters
tx,ty,tzTranslation vector.

Definition at line 470 of file model.cpp.

References vert.

◆ twist()

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

Twist vertices around an axis.

Parameters
axisTwist axis.
angleMaximum twist angle in degrees.
centerCentre of the twist.

Definition at line 507 of file model.cpp.

References vert, mx::X, mx::Y, and mx::Z.

◆ updateBuffers()

void mx::Mesh::updateBuffers ( )

Synchronise CPU-side vertex data to the GPU buffers.

Definition at line 270 of file model.cpp.

References norm, normalVBO, positionVBO, tex, texCoordVBO, VAO, and vert.

Referenced by resetToOriginal().

◆ uvRotate()

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

Rotate UV coordinates around the origin.

Parameters
angleRotation angle in degrees.

◆ uvScale()

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

Scale UV coordinates.

Parameters
uScaleU-axis scale factor.
vScaleV-axis scale factor.

◆ uvScroll()

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

Scroll UV coordinates by an offset.

Parameters
uOffsetU-axis scroll amount.
vOffsetV-axis scroll amount.

Definition at line 572 of file model.cpp.

References tex, and texCoordVBO.

◆ wave()

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

Apply a sinusoidal wave deformation.

Parameters
axisAxis along which the wave travels.
amplitudeWave amplitude.
frequencySpatial frequency.
phasePhase offset in radians.

Definition at line 531 of file model.cpp.

References vert, mx::X, mx::Y, and mx::Z.

Member Data Documentation

◆ aoMapTexture

GLuint mx::Mesh::aoMapTexture

Ambient occlusion map texture ID.

Definition at line 86 of file model.hpp.

Referenced by getAmbientOcclusionMap(), Mesh(), Mesh(), operator=(), operator=(), and setAmbientOcclusionMap().

◆ bitangent

std::vector<GLfloat> mx::Mesh::bitangent

◆ bitangentVBO

GLuint mx::Mesh::bitangentVBO

Definition at line 82 of file model.hpp.

Referenced by cleanup(), generateBuffers(), Mesh(), Mesh(), and operator=().

◆ displacementMapTexture

GLuint mx::Mesh::displacementMapTexture

Displacement map texture ID.

Definition at line 87 of file model.hpp.

Referenced by Mesh(), Mesh(), operator=(), operator=(), and setDisplacementMap().

◆ EBO

GLuint mx::Mesh::EBO

Definition at line 82 of file model.hpp.

Referenced by cleanup(), generateBuffers(), Mesh(), Mesh(), and operator=().

◆ hasOriginal

bool mx::Mesh::hasOriginal

Whether an original snapshot exists.

Definition at line 77 of file model.hpp.

Referenced by Mesh(), Mesh(), operator=(), resetToOriginal(), and saveOriginal().

◆ indices

std::vector<GLuint> mx::Mesh::indices

◆ materialName

std::string mx::Mesh::materialName

Material name from OBJ usemtl directive.

Definition at line 80 of file model.hpp.

Referenced by Mesh(), mx::Model::openOBJ(), and operator=().

◆ norm

◆ normalMapTexture

GLuint mx::Mesh::normalMapTexture

Normal map texture ID.

Definition at line 84 of file model.hpp.

Referenced by bindTexture(), drawWithForcedTexture(), getNormalMap(), Mesh(), Mesh(), operator=(), operator=(), and setNormalMap().

◆ normalVBO

GLuint mx::Mesh::normalVBO

Definition at line 82 of file model.hpp.

Referenced by cleanup(), generateBuffers(), Mesh(), Mesh(), operator=(), and updateBuffers().

◆ normIndex

size_t mx::Mesh::normIndex

Current write position in the normal array.

Definition at line 91 of file model.hpp.

Referenced by Mesh(), Mesh(), operator=(), and mx::Model::parseLine().

◆ originalNorm

std::vector<GLfloat> mx::Mesh::originalNorm

Saved copy of normals.

Definition at line 75 of file model.hpp.

Referenced by Mesh(), operator=(), resetToOriginal(), and saveOriginal().

◆ originalVert

std::vector<GLfloat> mx::Mesh::originalVert

Saved copy of vertex positions.

Definition at line 74 of file model.hpp.

Referenced by Mesh(), operator=(), resetToOriginal(), and saveOriginal().

◆ parallaxMapTexture

GLuint mx::Mesh::parallaxMapTexture

Parallax/height map texture ID.

Definition at line 85 of file model.hpp.

Referenced by getParallaxMap(), Mesh(), Mesh(), operator=(), and setParallaxMap().

◆ positionVBO

GLuint mx::Mesh::positionVBO

Definition at line 82 of file model.hpp.

Referenced by cleanup(), generateBuffers(), Mesh(), Mesh(), operator=(), and updateBuffers().

◆ shape_type

GLuint mx::Mesh::shape_type

GL primitive type (e.g. GL_TRIANGLES).

Definition at line 78 of file model.hpp.

Referenced by draw(), Mesh(), Mesh(), operator=(), and setShapeType().

◆ tangent

std::vector<GLfloat> mx::Mesh::tangent

◆ tangentVBO

GLuint mx::Mesh::tangentVBO

Definition at line 82 of file model.hpp.

Referenced by cleanup(), generateBuffers(), Mesh(), Mesh(), and operator=().

◆ tex

◆ texCoordVBO

GLuint mx::Mesh::texCoordVBO

Definition at line 82 of file model.hpp.

Referenced by cleanup(), generateBuffers(), Mesh(), Mesh(), operator=(), updateBuffers(), and uvScroll().

◆ texIndex

size_t mx::Mesh::texIndex

Current write position in the UV array.

Definition at line 90 of file model.hpp.

Referenced by Mesh(), Mesh(), operator=(), and mx::Model::parseLine().

◆ texture

GLuint mx::Mesh::texture

Primary diffuse texture ID.

Definition at line 79 of file model.hpp.

Referenced by bindTexture(), Mesh(), Mesh(), mx::Model::openModelString(), mx::Model::openOBJ(), operator=(), and operator=().

◆ VAO

GLuint mx::Mesh::VAO

Definition at line 82 of file model.hpp.

Referenced by cleanup(), draw(), generateBuffers(), Mesh(), Mesh(), operator=(), and updateBuffers().

◆ vert

◆ vertIndex

size_t mx::Mesh::vertIndex

Current write position in the vertex array.

Definition at line 89 of file model.hpp.

Referenced by Mesh(), Mesh(), operator=(), and mx::Model::parseLine().


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