The mesh class. More...
#include <VR3Mesh.h>
Classes | |
struct | Subset |
Subset data structure. More... | |
struct | UniformCache |
Uniform cache data structure. More... | |
Public Member Functions | |
Constructor | |
VR3Mesh (const char *file_name, const char *mesh_name=0, int objID=-1) | |
Constructor of the class. | |
Destroying Function | |
void | Destroy () |
Pseudo-Destructor for the VR3Mesh Class. | |
Get and Set Functions | |
void | Hide (int id) |
Hides a single subset. | |
void | HideAll () |
Hides all subsets. | |
void | Unhide (int id) |
Unhides a single subset. | |
void | UnhideAll () |
Unhides all subsets. | |
bool | IsHidden (int id) |
Checks if a subset is hidden. | |
VR3Material * | GetSubsetMaterial (int id) |
Gets the current material of a subset. | |
void | ForceSubsetMaterial (VR3Material *material, int id) |
Sets the current material of a subset. | |
VR3Material * | GetMaterial () |
Gets the mesh current material (first subset material) | |
void | ForceMaterial (VR3Material *material) |
Sets the mesh current material. | |
int | GetID () |
Gets the ID of the mesh. | |
const char * | GetName () |
Gets the name of the mesh. | |
const char * | GetFileName () |
Gets the name of the file the mesh was extracted from. | |
Private Member Functions | |
VR3Mesh (const VR3Mesh &) | |
Disable default copy constructor. | |
VR3Mesh & | operator= (const VR3Mesh &) |
Disable default assignment operator. | |
void | LoadUniformsStdMesh (const GLfloat *modelmat, const GLint *unilocs, int flags, bool lightmap) |
Loads mesh-specific uniforms. | |
void | ComputeUniformsStdMesh (const GLfloat *modelmat, int flags) |
Computes mesh-specific uniform values. | |
VAO and VBOs Preparation Functions | |
void | PrepareVerticesVBOs () |
Prepares the vertices VBOs (in the GL server memory) | |
void | PrepareNormalsVBOs () |
Prepares the normals VBOs (in the GL server memory) | |
void | PrepareDiffuseTexCoordVBOs () |
Prepares the diffuse texture coordinates VBOs (in the GL server memory) | |
void | PrepareLightMapCoordVBOs () |
Prepares the light map texture coordinates VBOs (in the GL server memory) | |
void | PrepareNormalMapCoordVBOs () |
Prepares the normal map texture coordinates VBOs (in the GL server memory) | |
void | PrepareDisplacementMapCoordVBOs () |
Prepares the displacement map texture coordinates VBOs (in the GL server memory) | |
Subset Specific Standard Uniforms Initialization | |
The following set of functions is used to load some of uniform variables of the standard rendering shader used to render the mesh. The function used depends on the four rendering flags state in the single subset: fl_difftex, fl_lightmap, fl_normmap and fl_dispmap. The effectively used function may be different between subsets of the same mesh, this happens because subsets do not usually share the same material. Even if the rendering flags have the same state among all subsets of the mesh, one of this function will have to be called each time a subset is drawn because material parameters may change. The uniform loaded in these functions are always subset-specific. Parameters:
| |
void | LoadUniformsSubsetFFFF (const Subset *sub) |
flags = (false - false - false - false) | |
void | LoadUniformsSubsetTFFF (const Subset *sub) |
flags = (true - false - false - false) | |
void | LoadUniformsSubsetFTFF (const Subset *sub) |
flags = (false - true - false - false) | |
void | LoadUniformsSubsetTTFF (const Subset *sub) |
flags = (true - true - false - false) | |
void | LoadUniformsSubsetFFTF (const Subset *sub) |
flags = (false - false - true - false) | |
void | LoadUniformsSubsetTFTF (const Subset *sub) |
flags = (true - false - true - false) | |
void | LoadUniformsSubsetFFTT (const Subset *sub) |
flags = (false - false - true - true) | |
void | LoadUniformsSubsetFTFT (const Subset *sub) |
flags = (false - true - false - true) | |
void | LoadUniformsSubsetTFTT (const Subset *sub) |
flags = (true - false - true - true) | |
void | LoadUniformsSubsetTTFT (const Subset *sub) |
flags = (true - true - false - true) | |
External Uniform Initialization Function | |
void | LoadUniformsExtMesh (const GLfloat *modelmat, const GLint *unilocs) |
Loads external shader uniforms relative to the whole mesh. | |
void | LoadUniformsExtSubset (const Subset *sub, const GLint *unilocs) |
Loads external shader uniforms relavite to a single subset. | |
Shadow map Uniform Initialization | |
void | LoadUniformsShadowMap (const GLfloat *modelmat) |
Loads shadow map uniforms. | |
Visibility Testing Function | |
bool | VisibilityTest (const GLfloat *modelmat, int flags) |
Checks if a mesh is visible. | |
Destructor | |
~VR3Mesh () | |
Destructor of the class. | |
Function Pointer Selection Function | |
void | LoadUniformLoader (Subset *sub) |
Selects the correct function pointer to use when loading uniforms. | |
Private Attributes | |
struct VR3Mesh::UniformCache | m_unicache |
Subset * | m_subsets |
Subsets for the current mesh. | |
int | m_subset_count |
Number of subsets in the mesh. | |
int | m_all_light_mapped |
Light mapped only mesh flag. | |
Geometric Data | |
GLint | m_vertex_count |
Total number of vertices. | |
GLfloat * | m_vertices |
The array of vertices (m_vertex_count*3 GLfloats) | |
GLint | m_tex_coord_count |
Total number of texture vertices. | |
GLfloat * | m_tex_coords |
The array of texture vertices (m_tex_coord_count*2 GLfloats) | |
GLint | m_tri_count |
Total number of triangles. | |
GLint * | m_ver_indices |
The array of triangle vertex indices (m_tri_count*3 GLints) | |
bool | m_smgroups_enabled |
Smoothing Groups enabled flag. | |
GLint | m_smgroup_count |
Number of different smoothing groups. | |
GLint * | m_smgroups |
Array of smoothing groups numbers (m_tri_count GLints) | |
Displacement map application parameters | |
GLfloat | m_BI |
Object bias for displacement mapping (may be unused) | |
GLfloat | m_SC |
Object scale for bump displacement (may be unused) | |
GLint | m_TF |
Object tessellation factor for displacement mapping (may be unused) | |
Bounding Box | |
GLfloat | m_bounding_box [6] |
Bounding box of the mesh (minx - maxx - miny - maxy - minz - maxz) | |
GLfloat | m_bbox_vertices [8][3] |
Bounding box vertices in object coordinates. | |
Mesh File Parameters | |
std::string | m_meshfile |
Name of the file the mesh was loaded from (absolute path) | |
std::string | m_name |
Name of the mesh object, extracted from the file. | |
int | m_ID |
ID of the mesh extracted from the file. | |
Friends | |
class | VR3MeshManager |
Friend class VR3MeshManager to access all private data members and the destructor. | |
class | VR3PhysicsSimulator |
Friend class VR3PhysicsSimulator to access the mesh file informations. | |
class | VR3Obj |
Friend class VR3Obj to access the draw method, the bounding box and the file name. | |
Drawing Functions | |
void | Draw (const GLfloat *modelmat, int flags) |
Real drawing method. | |
void | Draw (int flags=0) |
Draws the mesh. |
The mesh class.
The VR3Mesh class implements the functionalities related to the polygonal meshes which are contained in files in AAM format. The class offers a reduced set of functionalites. A bounding box is associated to every mesh. It is automatically used to perform visibility evaluations, but it can also be directly used for other specific purposes.
A mesh is composed of one or more subsets, a subset is a set of triangles which share the same material (and therefore textures). Each subset needs to be drawn using a shader capable of managing all the subset material features (a shader used for a subset may therefore differ from the shader used for another subset of the same object). A subset is identified (from the user's point of view) with an integer ID going from 0 to the number of available subsets -1. Those progressive IDs are assigned to the subsets depending on the order they are encountered in during the parsing of the mesh file (at least for AAM files).
When dealing with transparent meshes, the user must be aware that, because depth testing is enabled by default in the VR3Lib, subsets may not show correctly behind transparent subsets (depending on the subset drawing order). Subsets are always drawn in the order they are read from the AAM as if they were a unique stream of triangles (but not sharing the same materials). The user must handle manually all transparency-related issues, perhaps by using separate objects drawn in arbitrary order. Please note that use of depth-sorting may improve the behaviour in case of transparencies, but sorting the mesh subsets with reference to the distance from the center of their bounding box and the camera is not a complete solution because unexpected situations may always arise. A complete solution can be obtained only by sorting the single polygons using a depth sorting algorithm and by managing some particular situations as special cases. This is not handled automatically by the VR3Lib.
Definition at line 59 of file VR3Mesh.h.
VR3Mesh::VR3Mesh | ( | const VR3Mesh & | ) | [private] |
Disable default copy constructor.
VR3Mesh::~VR3Mesh | ( | ) | [private] |
Destructor of the class.
Deletes a VR3Mesh object. This is a private destructor because a VR3Mesh object may only be deleted by the VR3MeshManager static module, this will happen when the number of references to this mesh object drops to 0.
VR3Mesh::VR3Mesh | ( | const char * | file_name, |
const char * | mesh_name = 0 , |
||
int | objID = -1 |
||
) |
Constructor of the class.
Creates a new VR3Mesh object and initializes the object by loading the mesh file specified through file_name. Currently, only AAM files are supported. The mesh_name specifies which object to load from the AAM file (perhaps representing the whole scene). The ID may be used to identify the desired object as well. If no objID or name is specified, the first object read in the file will be loaded. To specify a null name value but explicitly specify the desired objID, a name of 0 must be passed to the function (NULL).
[in] | file_name | The name of the mesh file to load |
[in] | mesh_name | The name of the object to load from the file |
[in] | objID | Object ID of the desired object in the AAM file |
void VR3Mesh::ComputeUniformsStdMesh | ( | const GLfloat * | modelmat, |
int | flags | ||
) | [private] |
Computes mesh-specific uniform values.
Some uniform parameters does not change during rendering of a single mesh, even if they may be loaded to shaders multiple times due to shader program swap during subsets rendering. This function is used at the beginning of the mesh rendering when using built-in shader programs. Uniform values are stored in a uniform cache (m_unicache) and extracted from there and loaded when a new shader program becomes the used one. The rendering flags are used to understand if its necessary to compute shadow mapping-related uniforms or not.
[in] | modelmat | The resulting modeling matrix for the VR3Obj |
[in] | flags | Mesh rendering flags |
void VR3Mesh::Destroy | ( | ) |
Pseudo-Destructor for the VR3Mesh Class.
When a dynamic instance of the VR3Mesh class is not used anymore by the programmer or by a VR3Obj instace, we cannot delete the whole dynamic instance. Other entities may be using the object in dynamic memory and the only thing we must do is call the Destroy() functions in order to notify the VR3MeshManager that a reference to a mesh object has been deleted. If the mesh is to be deleted, the VR3MeshManager will do that.
void VR3Mesh::Draw | ( | const GLfloat * | modelmat, |
int | flags | ||
) | [private] |
Real drawing method.
This method is directly called by the VR3Obj::Draw(const GLfloat* modelmat) private method, and performs the effective rendering. Notice that the resulting modeling matrix must be passed as parameter, while the resulting projection and viewing matrix is easily obtained from the currently active scene. Both matrices refer to the last time one of the VR3Scene::Begin() functions has been called.
[in] | modelmat | The transformations final modeling matrix |
[in] | flags | Draw flags |
void VR3Mesh::Draw | ( | int | flags = 0 ) |
Draws the mesh.
This function may be called as the VR3Obj::Draw() method is, and simply draws this single mesh (obviously without any modeling transformation). It is recommended to use the VR3Obj::draw() method.
[in] | flags | Draw flags |
void VR3Mesh::ForceMaterial | ( | VR3Material * | material ) |
Sets the mesh current material.
The user may force the material of a mesh object even if different from the materials recovered from the file the mesh was loaded from. This function effectively changes the material for all subsets in the mesh. This function notifies the VR3MaterialManager static module that new references to the material are now active and also notifies the VR3MaterialManager that the references to the old materials have been lost due to the user forcing the material.
[in] | material | The material to apply |
void VR3Mesh::ForceSubsetMaterial | ( | VR3Material * | material, |
int | id | ||
) |
Sets the current material of a subset.
The user may force the material of a subset in themesh object even if different from the materials recovered from the file the mesh was loaded from. The material of a subset may be changed by specifying its particular subset id. This function notifies the VR3MaterialManager static module that a new reference to the material is active if the subset was found and also notifies the VR3MaterialManager that the reference to the old material have been lost due to the user forcing the material.
[in] | material | The material to apply |
[in] | id | The id of the subset to force (by default we force the material of subset 0) |
const char* VR3Mesh::GetFileName | ( | ) | [inline] |
Gets the name of the file the mesh was extracted from.
This function may be used to retrieve the file name of the file the mesh was extracted from. Many different meshes from the same file may be loaded. The retrieved file name is the absolute path to the file.
int VR3Mesh::GetID | ( | ) | [inline] |
VR3Material* VR3Mesh::GetMaterial | ( | ) |
Gets the mesh current material (first subset material)
This function simply returns the material of the first subset in the mesh, it is useful when dealing with plain meshes with no multimaterials because the user may not deal with subset ids. The VR3MaterialManager is notified about the creation and deletion of material references.
const char* VR3Mesh::GetName | ( | ) | [inline] |
Gets the name of the mesh.
This function may be used to retrieve the mesh name in the file it was extracted from. Mesh names are not unique and only IDs may be used to identify a mesh in a particular file (but not across files).
VR3Material* VR3Mesh::GetSubsetMaterial | ( | int | id ) |
Gets the current material of a subset.
A VR3Mesh object only may have more than one material (different for each subset) the user may obtain one of these material by using this function. This function is used to provide the user a reference to the material of the mesh. Be aware that this operation effectively means that a new reference to the material is now active. When the user has finished with this reference (even if it has used it in other meshes), the VR3Material::Destroy() function should be called.
id | The subset id of the subset whose material we want to obtain (by default we look for the material of subset 0) |
void VR3Mesh::Hide | ( | int | id ) |
Hides a single subset.
Single subsets may be hidden using this function.
[in] | id | The ID of the subset to hide |
void VR3Mesh::HideAll | ( | ) |
Hides all subsets.
Hides all subsets in the mesh.
bool VR3Mesh::IsHidden | ( | int | id ) |
Checks if a subset is hidden.
Checks if a single subset is hidden.
[in] | id | The ID of the subset to check |
void VR3Mesh::LoadUniformLoader | ( | Subset * | sub ) | [private] |
Selects the correct function pointer to use when loading uniforms.
When loading uniform values for a standard shader (not a user-defined one, one of the functions from the Standard Uniform Initialization group must be chosen depending on the rendering flag. This function takes care of choosing the correct function to use depending on the material properties of the received subset and assigning it to the sub->loadunifunc pointer.
[in,out] | sub | The reference to the subset where to write the uniform loader |
void VR3Mesh::LoadUniformsExtMesh | ( | const GLfloat * | modelmat, |
const GLint * | unilocs | ||
) | [private] |
Loads external shader uniforms relative to the whole mesh.
If an external user-defined shader is in use at rendering time, the default standard shader associated with the mesh is ignored and all rendering takes place as demanded by the user. In an external shader the user may declare some standard uniform parameters to receive data about the mesh that are being rendered. This effectively allows the user to render complex scenes with its shaders using the information loaded from the mesh files. This function is called only once at the beginning of the mesh rendering because all passed parameters won't change between subsets.
[in] | modelmat | The resulting modeling matrix for the VR3Obj |
[in] | unilocs | The program uniform locations (for standard uniforms) |
void VR3Mesh::LoadUniformsExtSubset | ( | const Subset * | sub, |
const GLint * | unilocs | ||
) | [private] |
Loads external shader uniforms relavite to a single subset.
If an external user-defined shader is in use at rendering time, the default standard shader associated with the mesh is ignored and all rendering takes place as demanded by the user. In an external shader the user may declare some standard uniform parameters to receive data about the mesh that are being rendered. This effectively allows the user to render complex scenes with its shaders using the information loaded from the mesh files. This function is called once for each subset because the data passed to the GL are in this case material-dependent (i.e. subset-dependent).
[in] | sub | The subset we are drawing with the external shader |
[in] | unilocs | The program uniform locations (for standard uniforms) |
void VR3Mesh::LoadUniformsShadowMap | ( | const GLfloat * | modelmat ) | [private] |
Loads shadow map uniforms.
When a shadow map is being rendered (the VR3DRAWFL_SHADOWMAP draw flag is active) a particular and very simple shader program is used. That program is made active before drawing all the meshes, but we have to pass some uniform here, when rendering the mesh.
[in] | modelmat | The resulting modeling matrix |
void VR3Mesh::LoadUniformsStdMesh | ( | const GLfloat * | modelmat, |
const GLint * | unilocs, | ||
int | flags, | ||
bool | lightmap | ||
) | [private] |
Loads mesh-specific uniforms.
This function is used to load mesh-specific uniforms that are constant during the whole mesh rendering. This function is called only when a new shader program is activated (therefore potentially it may be called just once per mesh). During subset rendering (if the shader program does not change) we need not to call this function because the previously loaded mesh-specific uniforms are still valid. The data loaded in the uniform locations is gathered from the uniform cache and only from there (computed at the beginning of the mesh rendering with standard shader programs). The rendering flags are used to understand if its necessary to pass shadow mapping-related uniforms or not.
[in] | modelmat | The resulting modeling matrix for the VR3Obj |
[in] | unilocs | The program uniform locations |
[in] | flags | Mesh drawing flags |
[in] | lightmap | Flag stating if the shader we just made active uses light mapping as lighting method |
void VR3Mesh::LoadUniformsSubsetFFFF | ( | const Subset * | sub ) | [private] |
flags = (false - false - false - false)
void VR3Mesh::LoadUniformsSubsetFFTF | ( | const Subset * | sub ) | [private] |
flags = (false - false - true - false)
void VR3Mesh::LoadUniformsSubsetFFTT | ( | const Subset * | sub ) | [private] |
flags = (false - false - true - true)
void VR3Mesh::LoadUniformsSubsetFTFF | ( | const Subset * | sub ) | [private] |
flags = (false - true - false - false)
void VR3Mesh::LoadUniformsSubsetFTFT | ( | const Subset * | sub ) | [private] |
flags = (false - true - false - true)
void VR3Mesh::LoadUniformsSubsetTFFF | ( | const Subset * | sub ) | [private] |
flags = (true - false - false - false)
void VR3Mesh::LoadUniformsSubsetTFTF | ( | const Subset * | sub ) | [private] |
flags = (true - false - true - false)
void VR3Mesh::LoadUniformsSubsetTFTT | ( | const Subset * | sub ) | [private] |
flags = (true - false - true - true)
void VR3Mesh::LoadUniformsSubsetTTFF | ( | const Subset * | sub ) | [private] |
flags = (true - true - false - false)
void VR3Mesh::LoadUniformsSubsetTTFT | ( | const Subset * | sub ) | [private] |
flags = (true - true - false - true)
void VR3Mesh::PrepareDiffuseTexCoordVBOs | ( | ) | [private] |
Prepares the diffuse texture coordinates VBOs (in the GL server memory)
Using the data written upon mesh loading from file, the diffuse texture coordinates VBOs (one for each subset having a diffuse texture) are filled with diffuse coordinate data. We use unindexed rendering and the resulting VBOs will be sequences of texture coordinates, we'll have data for each vertex of each triangle.
void VR3Mesh::PrepareDisplacementMapCoordVBOs | ( | ) | [private] |
Prepares the displacement map texture coordinates VBOs (in the GL server memory)
Using the data written upon mesh loading from file, the displacement map coordinates VBOs (one for each subset having a displacement map) are filled with displacement map coordinate data. We use unindexed rendering and the resulting VBOs will be sequences of texture coordinates, we'll have data for each vertex of each triangle.
void VR3Mesh::PrepareLightMapCoordVBOs | ( | ) | [private] |
Prepares the light map texture coordinates VBOs (in the GL server memory)
Using the data written upon mesh loading from file, the light map coordinates VBOs (one for each subset having a light map) are filled with light map coordinate data. We use unindexed rendering and the resulting VBOs will be sequences of texture coordinates, we'll have data for each vertex of each triangle.
void VR3Mesh::PrepareNormalMapCoordVBOs | ( | ) | [private] |
Prepares the normal map texture coordinates VBOs (in the GL server memory)
Using the data written upon mesh loading from file, the normal map coordinates VBOs (one for each subset having a normal map) are filled with normal map coordinate data. We use unindexed rendering and the resulting VBOs will be sequences of texture coordinates, we'll have data for each vertex of each triangle.
void VR3Mesh::PrepareNormalsVBOs | ( | ) | [private] |
Prepares the normals VBOs (in the GL server memory)
Using the data written upon mesh loading from file, the normals VBOs (one for each subset) are filled with vertices normal data. We use unindexed rendering and the resulting VBOs will be sequences of vertex normals, we'll have data for each vertex of each triangle.
void VR3Mesh::PrepareVerticesVBOs | ( | ) | [private] |
Prepares the vertices VBOs (in the GL server memory)
Using the data written upon mesh loading from file, the vertices VBOs (one for each subset) are filled with vertices position data. We use unindexed rendering and the resulting VBOs will be sequences of vertex positions, we'll have data for each vertex of each triangle.
void VR3Mesh::Unhide | ( | int | id ) |
Unhides a single subset.
Specifies a single subset to make visible.
[in] | id | The ID of the subset to unhide |
void VR3Mesh::UnhideAll | ( | ) |
Unhides all subsets.
Unhides all subsets in the mesh.
bool VR3Mesh::VisibilityTest | ( | const GLfloat * | modelmat, |
int | flags | ||
) | [private] |
Checks if a mesh is visible.
During rendering, a mesh visibility test takes place in order to figure out if the mesh effectively needs to be drawn. The test will always succeed if view frustum culling is disabled in the scene and the mesh will always be drawn. In this case, invisible meshes will be clipped only after vertex and geometry processing and therefore we may have a significant waste of time (limiting frame rate). We use a low-cost test based on the mesh bounding box that is completely safe but may return true even on some meshes that are not visibile. This visibility test takes place also when rendering the shadow map: we'll try to cull meshes outside the shadow source viewing volume.
[in] | flags | The draw flags received in the Draw() call |
[in] | modelmat | The resulting modeling matrix |
friend class VR3MeshManager [friend] |
Friend class VR3MeshManager to access all private data members and the destructor.
friend class VR3Obj [friend] |
friend class VR3PhysicsSimulator [friend] |
Friend class VR3PhysicsSimulator to access the mesh file informations.
int VR3Mesh::m_all_light_mapped [private] |
GLfloat VR3Mesh::m_bbox_vertices[8][3] [private] |
Bounding box vertices in object coordinates.
vfvertices[0] = minx - miny - minz vfvertices[1] = maxx - miny - minz vfvertices[2] = minx - maxy - minz vfvertices[3] = minx - miny - maxz vfvertices[4] = maxx - maxy - minz vfvertices[5] = maxx - miny - maxx vfvertices[6] = minx - maxy - maxz vfvertices[7] = maxx - maxy - maxz
GLfloat VR3Mesh::m_BI [private] |
GLfloat VR3Mesh::m_bounding_box[6] [private] |
int VR3Mesh::m_ID [private] |
std::string VR3Mesh::m_meshfile [private] |
std::string VR3Mesh::m_name [private] |
GLfloat VR3Mesh::m_SC [private] |
GLint VR3Mesh::m_smgroup_count [private] |
GLint* VR3Mesh::m_smgroups [private] |
bool VR3Mesh::m_smgroups_enabled [private] |
int VR3Mesh::m_subset_count [private] |
Subset* VR3Mesh::m_subsets [private] |
GLint VR3Mesh::m_tex_coord_count [private] |
GLfloat* VR3Mesh::m_tex_coords [private] |
GLint VR3Mesh::m_TF [private] |
GLint VR3Mesh::m_tri_count [private] |
struct VR3Mesh::UniformCache VR3Mesh::m_unicache [private] |
GLint* VR3Mesh::m_ver_indices [private] |
GLint VR3Mesh::m_vertex_count [private] |
GLfloat* VR3Mesh::m_vertices [private] |