//============================================================================ // makevrml.hpp : class used to create VRML files from various primitives. //============================================================================ #ifndef MAKEVRML #define MAKEVRML #include "obbtree.hpp" #include "aabbtree.hpp" #include "sphrtree.hpp" #include "viewfrus.hpp" #define _OPENINVENTOR_ 0 #define _VRML_ 1 class VRML { private: FILE* FilePtr; // POINTER TO VRML FILE int ObjectNum; // OBJECT COUNTER public: //-------------------------------------------------------------------------- // CONSTRUCTOR: OPENS THE FILE FOR WRITING, WRITES OUT THE HEADER //-------------------------------------------------------------------------- VRML(char* FileName, int Type=_VRML_) { // OPEN FILE AND WRITE OUT VRML HEADER FilePtr = fopen(FileName, "w"); if (Type==_VRML_) fprintf(FilePtr,"#VRML V1.0 ascii\n\n"); // TO OUTPUT VRML FILE else fprintf(FilePtr,"#Inventor V2.0 ascii\n\n"); // TO OUTPUT INVENTOR FILE fprintf(FilePtr,"Separator\n{\n"); ObjectNum=0; // INIT OBJECT COUNTER }; //-------------------------------------------------------------------------- // DESTRUCTOR: PERFORMS FINISH WORK ON VRML FILE, CLOSES THE FILE //-------------------------------------------------------------------------- ~VRML() { fprintf(FilePtr,"}\n"); // FINISH THE VRML FILE fclose(FilePtr); // CLOSE THE VRML FILE }; //-------------------------------------------------------------------------- // Writes out a triangle face. "Outside" is determined by right-hand rule. // Points should be stored in counter-clockwise fashion to denote sides. //-------------------------------------------------------------------------- void DrawTri(Vect3d A, Vect3d B, Vect3d C) { fprintf(FilePtr, " Coordinate3{"); fprintf(FilePtr, " point[%.3f %.3f %.3f, %.3f %.3f %.3f, %.3f %.3f %.3f]}\n", A.x,A.y,A.z, B.x,B.y,B.z, C.x,C.y,C.z); fprintf(FilePtr, " IndexedFaceSet { coordIndex[0,1,2,-1] }\n"); }; //-------------------------------------------------------------------------- // Draws an OBBtree to a specified level (if Level<0 draws leaf : default) //-------------------------------------------------------------------------- void DrawBVtree(OBBtree* obbt, int Level=-1) { if (Level==0 || obbt->IsLeaf()) DrawBV(obbt); else { DrawBVtree((OBBtree*)obbt->N,Level-1); DrawBVtree((OBBtree*)obbt->P,Level-1); }; }; //-------------------------------------------------------------------------- // Write out an OBB object, given the OBB axes, origin, and symmetric extents. //-------------------------------------------------------------------------- void DrawBV(OBBtree* obbt) { Vect3d X=obbt->pR.col(0), Y=obbt->pR.col(1), Z=obbt->pR.col(2), Orig=obbt->pT, Ext=obbt->Dim; // WRITE OUT HEADER FOR OBB OBJECT. NOTE: USES POST-MULT, ROW-VECTORS fprintf(FilePtr, " DEF OBB%d Separator\n", ObjectNum); fprintf(FilePtr, " {\n"); fprintf(FilePtr, " MatrixTransform\n"); fprintf(FilePtr, " { matrix\n"); fprintf(FilePtr, " %f %f %f 0\n", X.x*Ext.x, X.y*Ext.x, X.z*Ext.x); fprintf(FilePtr, " %f %f %f 0\n", Y.x*Ext.y, Y.y*Ext.y, Y.z*Ext.y); fprintf(FilePtr, " %f %f %f 0\n", Z.x*Ext.z, Z.y*Ext.z, Z.z*Ext.z); fprintf(FilePtr, " %f %f %f 1\n", Orig.x, Orig.y, Orig.z); fprintf(FilePtr, " }\n"); fprintf(FilePtr, " Cube {}\n"); fprintf(FilePtr, " }", ObjectNum++); }; //-------------------------------------------------------------------------- // Draws an AABBtree to a specified level (if Level<0 draws leaf : default) //-------------------------------------------------------------------------- void DrawBVtree(AABBtree* aabbt, int Level=-1) { if (Level==0 || aabbt->IsLeaf()) DrawBV(aabbt); else { DrawBVtree((AABBtree*)aabbt->N,Level-1); DrawBVtree((AABBtree*)aabbt->P,Level-1); }; }; //-------------------------------------------------------------------------- // Write out an AABB object //-------------------------------------------------------------------------- void DrawBV(AABBtree* aabbt) { Vect3d C = (aabbt->MinExt + aabbt->MaxExt) * 0.5; Vect3d D = (aabbt->MaxExt - aabbt->MinExt) * 0.5; // WRITE OUT HEADER FOR OBB OBJECT. NOTE: USES POST-MULT, ROW-VECTORS fprintf(FilePtr, " DEF AABB%d Separator\n", ObjectNum); fprintf(FilePtr, " {\n"); fprintf(FilePtr, " MatrixTransform\n"); fprintf(FilePtr, " { matrix\n"); fprintf(FilePtr, " %f 0 0 0\n", D.x); fprintf(FilePtr, " 0 %f 0 0\n", D.y); fprintf(FilePtr, " 0 0 %f 0\n", D.z); fprintf(FilePtr, " %f %f %f 1\n", C.x, C.y, C.z); fprintf(FilePtr, " }\n"); fprintf(FilePtr, " Cube {}\n"); fprintf(FilePtr, " }", ObjectNum++); }; //-------------------------------------------------------------------------- // Draws an SPHEREtree to a specified level (if Level<0 draws leaf : default) //-------------------------------------------------------------------------- void DrawBVtree(SPHEREtree* spheret, int Level=-1) { if (Level==0 || spheret->IsLeaf()) DrawBV(spheret); else { DrawBVtree((SPHEREtree*)spheret->N,Level-1); DrawBVtree((SPHEREtree*)spheret->P,Level-1); }; }; //-------------------------------------------------------------------------- // Write out a Sphere object //-------------------------------------------------------------------------- void DrawBV(SPHEREtree* spheret) { Vect3d C = spheret->Center; float R = spheret->Radius; // WRITE OUT HEADER FOR OBB OBJECT. NOTE: USES POST-MULT, ROW-VECTORS fprintf(FilePtr, " DEF AABB%d Separator\n", ObjectNum); fprintf(FilePtr, " {\n"); fprintf(FilePtr, " MatrixTransform\n"); fprintf(FilePtr, " { matrix\n"); fprintf(FilePtr, " %f 0 0 0\n", R); fprintf(FilePtr, " 0 %f 0 0\n", R); fprintf(FilePtr, " 0 0 %f 0\n", R); fprintf(FilePtr, " %f %f %f 1\n", C.x, C.y, C.z); fprintf(FilePtr, " }\n"); fprintf(FilePtr, " Sphere {}\n"); fprintf(FilePtr, " }", ObjectNum++); }; //-------------------------------------------------------------------------- // Writes out a view-frustum object, given the view frustum. //-------------------------------------------------------------------------- void DrawViewFrustum(ViewFrustum VF) { // WRITE OUT HEADER POLYGON-BASED VIEW FRUSTUM OBJECT fprintf(FilePtr, " DEF ViewFrustum%d Separator\n", ObjectNum); fprintf(FilePtr, " {\n"); fprintf(FilePtr, " Material\n"); fprintf(FilePtr, " {\n"); fprintf(FilePtr, " ambientColor [ 0.5 0.5 0.5 ]\n"); fprintf(FilePtr, " diffuseColor [ 0.8 0.8 0.8 ]\n"); fprintf(FilePtr, " specularColor [ 0 0 0 ]\n"); fprintf(FilePtr, " shininess 0.2\n"); fprintf(FilePtr, " transparency 0\n"); fprintf(FilePtr, " }\n"); fprintf(FilePtr, " ShapeHints\n"); fprintf(FilePtr, " {\n"); fprintf(FilePtr, " vertexOrdering COUNTERCLOCKWISE\n"); fprintf(FilePtr, " }\n"); fprintf(FilePtr, " Coordinate3\n"); fprintf(FilePtr, " {\n"); fprintf(FilePtr, " point\n"); fprintf(FilePtr, " [\n"); int i; for (i=0; i=VF.NumSides; i--) fprintf(FilePtr, "%d,",i); fprintf(FilePtr, " -1,\n"); for (i=1; i