// Persistence of Vision Ray Tracer Scene Description File // File: mesh_tool.inc // Vers: 3.5 // Desc: // Date: 04/11/2003 // Auth: Jean-Charles Marteau // #declare _Mem_Alloc_Base = 100; // -------------------------------------------------------------------------------- // Constants // Object types #declare _Vert = 0; #declare _Face = 1; #declare _Norm = 2; #declare _VrtF = 3; #declare _Trgl = 4; // Special 'object type' containing the number of objects of each type. #declare _Nb = 3; // -------------------------------------------------------------------------------- // Object creation methods // Can be implemented in a general purpose object_mgr.inc package // Creates a new mesh and returns it. #macro New_Mesh () // - Array of vertices #local _Vertices = array [_Mem_Alloc_Base]; // - Array of faces #local _Faces = array [_Mem_Alloc_Base]; // - Array of vertices normals #local _Normals = array [_Mem_Alloc_Base]; // - Array giving the Faces that use the corresponding vertice. // We use this to inverse the indexes and find which face uses which vertice // We'll define it later. // - Nb_of_elements // - Vertices // - Faces #local _Nb_Elem = array [5] { 0, 0, 0, 0, 0 }; #local _Mesh = array [4]; #local _Mesh [_Vert] = _Vertices; #local _Mesh [_Norm] = _Normals; #local _Mesh [_Face] = _Faces; #local _Mesh [_Nb] = _Nb_Elem; _Mesh #end #macro Add_Object (_Mesh, Obj, Obj_Type) // Checking if the array is big enough #if (_Mesh [_Nb][Obj_Type] >= dimension_size (_Mesh[Obj_Type],1)) // We allocate a bigger one #local _New_Objects = array [dimension_size (_Mesh[Obj_Type],1)*2]; // Copy the old one in the new one #local i = 0; #while (i < _Mesh [_Nb][Obj_Type]) #local _New_Objects [i] = _Mesh[Obj_Type][i]; #local i = i + 1; #end // Undef the old one #undef _Mesh[Obj_Type] // And affect the new one. #declare _Mesh[Obj_Type] = _New_Objects; #end // Setting the new object. #declare _Mesh[Obj_Type][_Mesh [_Nb][Obj_Type]] = Obj; #local res_index = _Mesh [_Nb][Obj_Type]; #declare _Mesh [_Nb][Obj_Type] = _Mesh [_Nb][Obj_Type] + 1; res_index #end // Add a vertice to the mesh. #macro Add_Vertice (_Mesh, P) Add_Object (_Mesh, P, _Vert) #end // Add a polygon to the mesh. #macro Add_Polygon (_Mesh, _Vert_Indexes_Array) Add_Object (_Mesh, _Vert_Indexes_Array, _Face) #end // Add a triangle to the mesh. #macro Add_Triangle (_Mesh, Pi1, Pi2, Pi3) #declare _Mesh[_Nb][_Trgl] = _Mesh[_Nb][_Trgl] + 1; Add_Polygon (_Mesh, array [3] { Pi1, Pi2, Pi3 }) #end // Add a rectangle to the mesh. #macro Add_Rectangle (_Mesh, Pi1, Pi2, Pi3, Pi4) #declare _Mesh[_Nb][_Trgl] = _Mesh[_Nb][_Trgl] + 2; Add_Polygon (_Mesh, array [4] { Pi1, Pi2, Pi3, Pi4 }) #end // -------------------------------------------------------------------------------- // Object accessors // -------------------------------------------------------------------------------- // 'High level' mesh manipulation methods #macro Generate_Mesh (_Mesh, Smooth_Max_Angle) // { // We create the array giving the faces that a vertice uses. // This will ease up normals calcultation. /* #local _Vertices_Faces = array [dimension_size (_Mesh[_Vert], 1)]; #local i = 0; #while (i < _Mesh[_Nb][_Face]) #local j = 0; #while (j < dimension_size (_Mesh[_Face][i], 1)) // Adding face i to the list of faces of vertice number _Mesh[_Face][i][j] #local vert_index = _Mesh[_Face][i][j]; #ifndef _Vertices_Faces [vert_index] _Vertices_Faces [vert_index] #end #local j = j + 1; #end #end */ mesh2 { vertex_vectors { _Mesh[_Nb][_Vert], #local i = 0; #while (i < _Mesh[_Nb][_Vert]) _Mesh[_Vert][i], #local i = i + 1; #end } face_indices { _Mesh[_Nb][_Trgl], #local i = 0; #while (i < _Mesh[_Nb][_Face]) #switch (dimension_size (_Mesh[_Face][i], 1)) #case (3) <_Mesh[_Face][i][0], _Mesh[_Face][i][1], _Mesh[_Face][i][2]>, #break #case (4) <_Mesh[_Face][i][0], _Mesh[_Face][i][1], _Mesh[_Face][i][2]>, <_Mesh[_Face][i][0], _Mesh[_Face][i][2], _Mesh[_Face][i][3]>, #break #else #error "Only Triangle and Rectangle faces supported for now !\n" #end #local i = i + 1; #end } } #end // Saves the mesh as an array for later use by this package #macro Save_Mesh_Data (_Mesh, filename) #end // Saves the mesh as a mesh #macro Save_Mesh (_Mesh, filename, Smooth_Max_Angle) #end