Source

dbsdev / sources / opengl2_samples / test7_simplerenderer / GLMesh.h

Full commit
#ifndef __GLMesh_H__
#define __GLMesh_H__
// /////////////////////////////////////////////////////////////////////// //
// Copyright (c) 2012, Davide Bacchet (davide.bacchet@gmail.com)           //
// All rights reserved.                                                    //
//                                                                         //
// The software is released under the Creative Commons Attribution         //
// NonCommercial (CC BY-NC) license. The full license text is available    //
// at http://creativecommons.org/licenses/by-nc/3.0/legalcode              //
//                                                                         //
// You are free to distribute and adapt the work under the conditions:     //
// * Redistributions of source code must retain the above copyright notice //
// * You must attribute the work in the manner specified by the author     //
// * You may not use this work for commercial purposes.                    //
//                                                                         //
// Commercial Usage:                                                       //
// To use this software as part of a commercial product, you must          //
// obtain a written agreement from the author.                             //
// /////////////////////////////////////////////////////////////////////// //

// GL mesh class. Loosely based on the mesh format in the BF2 engine

#include <vector>

#include "vertex.h"
#include "GLMaterial.h"
#include "GLTransform.h"


/// @class GLMesh
/// @brief a submesh is a graphic representation of an entity
/// @details a submesh is a graphical representation of an object, or part of it; 
/// it is associated to a single material
class GLSubMesh
{
public:
    
    // data
    
    /// standard constructor
    GLSubMesh();
    /// standard constructor
    GLSubMesh(const std::string &nm);
    /// copy constructor
    GLSubMesh(const GLSubMesh &mesh);
    /// destructor
    virtual ~GLSubMesh();
    
public:
    
    // associated material
    GLMaterial *material;
    // data buffers
    std::vector<unsigned int> indices;
    std::vector<Vertex_t> vertices;
    
protected:
    
    /// basic initialization
    void init( void );
    
};


/// @class GLMesh
/// @brief a mesh is a graphic representation of an entity
/// @details a mesh is composed by one or more submeshes, each having its own material. 
/// All the submeshes share the same reference system
class GLMesh
{
public:
    
    // data
    
    /// standard constructor
    GLMesh();
    /// copy constructor
    GLMesh(const GLMesh &mesh);
    /// destructor
    virtual ~GLMesh();
    
    /// add a submesh
    bool addSubMesh( const GLSubMesh &sm );
    
    /// remove a submesh by index
    bool removeSubMeshByIndex( unsigned int idx );
    
public:
        
    // data
    std::vector<GLSubMesh> submeshes;   ///< vector containing all the submeshes
    
protected:
    
    /// basic initialization
    void init( void );
    
};


// ///////////////// //
// utility functions //
// ///////////////// //

/// apply a transformation to the mesh nodes
inline void applyTransform(GLMesh *mesh, const GLTransformf &tr)
{
    Matrix4f M = tr.getTransformationMatrix();
    Matrix3f R = tr.getRotationMatrix();
    for(unsigned int k=0; k<mesh->submeshes.size(); k++)
    {
        for (unsigned int i=0; i<mesh->submeshes[k].vertices.size(); i++)
        {
            mesh->submeshes[k].vertices[i].pos  = M*mesh->submeshes[k].vertices[i].pos;
            mesh->submeshes[k].vertices[i].norm = R*mesh->submeshes[k].vertices[i].norm;
            mesh->submeshes[k].vertices[i].tang = R*mesh->submeshes[k].vertices[i].tang;
        }
    }
}


/// apply a transformation to the mesh nodes
inline void applyTransform(GLMesh *mesh, const Matrix4f &M)
{
    Matrix3f R;
      R(1,1) = M(1,1); R(1,2) = M(1,2); R(1,3) = M(1,3); 
      R(2,1) = M(2,1); R(2,2) = M(2,2); R(2,3) = M(2,3); 
      R(3,1) = M(3,1); R(3,2) = M(3,2); R(3,3) = M(3,3); 
    for(unsigned int k=0; k<mesh->submeshes.size(); k++)
    {
        for (unsigned int i=0; i<mesh->submeshes[k].vertices.size(); i++)
        {
            mesh->submeshes[k].vertices[i].pos  = M*mesh->submeshes[k].vertices[i].pos;
            mesh->submeshes[k].vertices[i].norm = R*mesh->submeshes[k].vertices[i].norm;
            mesh->submeshes[k].vertices[i].tang = R*mesh->submeshes[k].vertices[i].tang;
        }
    }
}


#endif //__GLMesh_H__