Wiki

Clone wiki

sig / GsModel

GsModel

GsModel is the class used to represent objects. It uses arrays to represent indexed triangles and their properties, includes a .obj loader, and several utilities.

Representation

The internal data is represented with the arrays listed below. They can be manipulated directly by the user or they will be manipulated to represent models loaded from files or algorithmically built by provided methods.

#!c++
GsArray<GsPnt>      V; //!< List of vertex coordinates
GsArray<GsVec>      N; //!< List of normals
GsArray<Face>       F; //!< Triangular faces indices to V
GsArray<Face>      Fn; //!< Indices to the normals in N (size is 0 or F.size())
GsArray<GsPnt2>     T; //!< List of texture coordinates (indexed by Ft, or T.size() must be equal to V.size())
GsArray<Face>      Ft; //!< Indices to the texture coords in T
GsArray<GsMaterial> M; //!< List of materials
GsArrayPt<Group>    G; //!< Groups of faces with same material and texture (size is 0 or M.size())
Where the used structures are:
#!c++
struct Face 
{ int a, b, c; // indices to the respective data arrays
};

struct Group
{ int fi, fn;       //!< the initial face index and the number of faces in this group
  GsCharPt mtlname; //!< name of the material used by this group
  Texture* dmap;    //!< diffuse color map, or null pointer if none
};

After the data arrays are defined, the correct mode has to be set in order to inform GsModel how the information is to be used. See comments of method set_mode() in the header file.

Example: Textured Model

For example, the steps below can be used to create a textured object :

#!c++
// 1. Generate the geometry:
GsModel m; // create an empty model
...
Add your code here to set m.V, m.F, and m.N arrays
...

// 2. Create a material group”:
GsModel::Group& g = m.G.push();
g.fi = 0;            // The group starts at first face,
g.fn = m.F.size();       // convers all faces,
g.dmap = new GsModel::Texture;   // and will be textured,
g.dmap->fname.set("C:/img.png"); // with this image (put here correct path and name!).

// 3. Make sure the number of materials matches the number of groups (here only one):
m.M.push().init(); // One material added.
m.M.top()... = ...;   // You may access the material here to set any material parameters

// 4. Now you can add texture coordinates to be used per vertex:
int nv = m.V.size();
m.T.size ( nv );    // set same size as m.V array
for ( int i=0; i<nv; i++ )
{ m.T[i].set ( ... , ... ); // Compute your coordinates and put them here
}

// 5. Set mode parameters to enable texturing:
m.set_mode ( GsModel::Smooth, GsModel::PerGroupMtl );
m.textured = true;

Updated