TWiki home TWiki > Team1 > ProjectTwo > ProjectTwoFileConverter TWiki webs:
Main | TWiki
Team1 . { Changes | Index | Search | Go }
-- JonDodge - 04 Apr 2004
// header files
#include <fstream>
#include <iostream>
#include <string>
#include <cassert>
#include <vector>
#include "geometry.h"
#include "face.hpp"

using namespace std;

#include <GL/glut.h>

const string FOLDER_LOCATION = "res\\";
const string CONSTANTS_INFILE_LOCATION = "constants.txt";
const string CONSTANTS_OUTFILE_LOCATION = "constantsRENAME.txt";
const string CONSTANTS_INFILE_EMPTY = "empty";
const string DEBUG_MODE_ENABLE = "DEBUG";
const string TEMPLATE_MODE_ENABLE = "TEMPLATE";
const string NO_TEXTURE = "NOTEXTURE";

// function prototypes
void display(void);
void reshape(int width, int height);
void init();
void idle();
string peekToken(ifstream &in);
void ignoreToken(ifstream &in);
bool isAlpha(char alpha);
int FindLastSlash(string examined);

ifstream in;
ifstream inConstants;
ofstream out;
ofstream outConstants;

bool _debug = false;
bool _template = false;

main(int argc, char **argv)
{

  // initialize glut 
  glutInit(&argc, argv);

  // set window size
  glutInitWindowSize(0,0);

  // establish glut display parameters
  glutInitDisplayMode(GLUT_SINGLE   | GLUT_RGB );

  // create window
  glutCreateWindow("My First OpenGL program");
  glutHideWindow();

  // register callback functions
  glutDisplayFunc(display);
  glutReshapeFunc(reshape); 
  glutIdleFunc(idle);


  // initalize opengl parameters
  init();

  // loop until something happens
  glutMainLoop();
  return 0;           
}

void init()
{
  // initialize viewing system
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, 1.0, 1.0, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  // initialize background color to black
  glClearColor(0,0,0,0); 
}


void reshape(int width, int height)
{
  glViewport(0,0,width,height);
}

void display()
{

// clear buffer
   glClear(GL_COLOR_BUFFER_BIT );


   
// draw a red triangle
   glColor3f(1,0,0);
   glBegin(GL_TRIANGLES);
      glVertex3f(-3,-1,-8);
      glVertex3f(3,-1,-10);
      glVertex3f(0,3,-9);
   glEnd();

// flush buffer
   glFlush();   
}

void idle()
{

   string readFileName;
   string writeFileName;

   // Determine where the files to be read/written are.
   cout << "If you want to enable Debug Mode, enter " << DEBUG_MODE_ENABLE << endl
       << "If you want to create a template for surface constant data, enter : " << TEMPLATE_MODE_ENABLE << endl;
   cout << "Please input the filename of the readFile: ";
   cin >> readFileName;

   // Read input filename again, since that last input ran it in debug mode.
   if(readFileName == DEBUG_MODE_ENABLE)
   {
      cout << "Please input the filename of the readFile: ";
      cin >> readFileName;
      _debug = true;
   }
   if(readFileName == TEMPLATE_MODE_ENABLE)
   {
      cout << "Please input the filename of the readFile: ";
      cin >> readFileName;
      _template = true;
   }
   cout << "Please input the filename of the writeFile: ";
   cin >> writeFileName;

   // Open the file from which data about the surfaces is to be written.
   if(_template)
   {
   outConstants.open(CONSTANTS_OUTFILE_LOCATION.c_str());
   }
   in.open(readFileName.c_str());
   out.open(writeFileName.c_str());
   inConstants.open(CONSTANTS_INFILE_LOCATION.c_str());

   // Determine if we are using constants from a file, or the standard ones listed below.
   string inConstantsFirstString = peekToken(inConstants);
   bool isinConstantsEmpty = (inConstantsFirstString == CONSTANTS_INFILE_EMPTY);
   
   // Used as a measure to ensure that our files are talking about the same thing.
   bool meshesMatch = false;

   int numMeshes;

   string nextToken;
   in >> nextToken;

   // Used to pitch the crap at the beginning of the ascii file.
   while(nextToken != "Meshes:")
   {
      in >> nextToken; 
   }

   // Get the number of meshes
   in >> numMeshes;
   if(!isinConstantsEmpty)
   {
      int numMeshesInConstants;
      inConstants >> numMeshesInConstants;

      // Determine if the files are actually talking about the same model.
      meshesMatch = (numMeshes == numMeshesInConstants);
   }
   
   cout << endl << "Meshes found: " << numMeshes << endl;

   out << numMeshes << endl << endl;
   outConstants << numMeshes << endl << endl;

   // Variables we will need for proper file interactions
   int numVertices, numNormals, numTriangles, textureIndex;
   int vertexIndex, normalIndex;
   float vertexData, textureData, normalData;

   // Variables we will use to calculate statistics
   int triangleSum = 0;

   // Initialize surface constants to numbers that work.
   int tilingConstant = 1;
   float bounceDamp = .6;
   float rollDamp = .99;
   bool isHole = false;
   bool isLiquid = false;
   bool isFlying = false;
   
   for(int k = 0; k < numMeshes; ++k)
   {

      cout << endl << "Processing Mesh: " << k + 1 << endl;
      nextToken = peekToken(in);
      out << nextToken << endl;
      
      // Print to the outstream a file that can be filled in with constants.
      if(_template)
      {
         outConstants << nextToken << endl << endl
                      << "TilingConstant " << tilingConstant << endl
                   << "BounceDamp " << bounceDamp << endl
                   << "RollDamp " << rollDamp << endl
                   << "IsHole " << isHole << endl
                   << "IsLiquid " << isLiquid << endl
                   << "IsFlying " << isFlying << endl << endl;
      }

      // Ignore the first 2 tokens
      for(int i = 0; i < 2; ++i)
         ignoreToken(in);

      // Get the texture element
      in >> textureIndex;
      out << textureIndex << endl;

      // If everything is fine so far, grab the constant data.
      if(!isinConstantsEmpty && meshesMatch)
      {
         ignoreToken(inConstants);
         ignoreToken(inConstants);
         inConstants >> tilingConstant;
         ignoreToken(inConstants);
         inConstants >> bounceDamp;
         ignoreToken(inConstants);
         inConstants >> rollDamp;
         ignoreToken(inConstants);
         inConstants >> isHole;
         ignoreToken(inConstants);
         inConstants >> isLiquid;
         ignoreToken(inConstants);
         inConstants >> isFlying;
            
         if(_debug)
         {
            cout << "TilingConstant: " << tilingConstant << endl
                << "BounceDamp:     " << bounceDamp << endl
                << "RollDamp:       "  << rollDamp << endl
                << "IsHole:   " << isHole << endl
                << "IsLiquid: " << isLiquid << endl
                << "IsFlying: " << isFlying << endl
                << endl;
         }
      }

      out << bounceDamp << endl
         << rollDamp << endl
         << isHole << endl
         << isLiquid << endl
         << isFlying << endl;

      // Surface constant reading complete.
      out << endl;

      // Its Vertex readin' time
      in >> numVertices;
      out << numVertices << endl << endl;
      if(_debug)
         cout << "Number of Vertices " << numVertices << endl;

      vector<Tuple*> vertices;
      Tuple* addTuple;

      if(_debug)
         cout << "Processing Vertices..." << endl;

      // Read each piece of Vertex data and write it back out
      for(int j = 0; j < numVertices; ++j)
      {
         ignoreToken(in);
         addTuple = new Tuple(0,0,0);
         for( int i = 0; i < 3; ++i)
         {
            in >> vertexData;
            out << vertexData << ' ';
            (*addTuple)[i] = vertexData;
         }
         vertices.push_back(addTuple);
         for( int i = 0; i < 2; ++i)
         {
            in >> textureData;
            if(i==0)
               out << tilingConstant*textureData << ' ';
            else
               out << tilingConstant*(1-textureData) << ' ';
         }
         ignoreToken(in);
         out << endl;
      }

      // Vertex readin' complete
      out << endl;
      if(_debug)
         cout << "Vertex Processing Complete" << endl;

      // Its Normals readin' time
      in >> numNormals;
      out << numNormals << endl;
      if(_debug)
      {
         cout << "Processing Normals..." << endl;
      }

      // Read each pieve of normal data and write it back out.
      for(int j = 0; j < numNormals; ++j)
      {
         for(int i = 0; i < 3; ++i)
         {
            in >> normalData;
            out << normalData << ' ';
         }
         out << endl;
      }

      // Normals readin' complete.
      out << endl;
      if(_debug)
         cout << "Normals Processing Complete" << endl;

      // Its Triangles readin' time
      in >> numTriangles;
      out << numTriangles << endl;
      triangleSum += numTriangles;
      if(_debug)
         cout << "Number of Triangles " << numTriangles << endl;
      if(_debug)
         cout << "Processing Triangles..." << endl;
      vector<Tuple*> corners;

      // Read each piece of triangle data and write it back out, as well as make some more computations
      for(int i = 0; i < 3; ++i)
         corners.push_back(NULL);

      for(int j = 0; j < numTriangles; ++j)
      {
         if(_debug)
            cout << '1';
         ignoreToken(in);
         for(int i = 0; i < 3; ++i)
         {
            in >> vertexIndex;
            out << vertexIndex << ' ';
            corners[i] = vertices[vertexIndex];
         }
         Face addFace(corners[0], corners[1], corners[2]);

         if(_debug)
            cout << '2';
         for(int i = 0; i < 3; ++i)
         {
            in >> normalIndex;
            out << normalIndex << ' ';
         }
         out << endl;

         // I think this token might matter but im not sure what to do with it since i dont know
         // what it is
         ignoreToken(in);
            
         if(_debug)
            cout << '3';
         for(int i = 0; i < 3; ++i) 
         {
            for(int h = 0; h < 16; ++h) 
            {
               out << addFace.getForwardTransform(i)[h] << " ";
            }
            out << endl;
         }
         
         out << endl;

         if(_debug)
            cout << '4';
         for(int i = 0; i < 3; ++i) {
            for(int h = 0; h < 3; ++h)
               out << ((*(addFace.getStandard()))[i])[h] << " ";
            out << endl;
         }
            
         out << endl;

         if(_debug)
            cout << '5' << endl;
         Tuple SVerts;
         for(int i = 0; i < 3; ++i) 
         {
            SVerts = addFace.getSVert(i);
            for(int n = 0; n < 3; ++n)
                       out << SVerts[n] << ' ';   
               
            out << endl;
         }
               
         out << endl;
      }
      
      // Finished with triangles, and the surface.
      out << endl;
      if(_debug)
         cout << "Triangle Processing Complete" << endl;
   }    
      

   int numMaterials = 0;
   float colorData;
   string textureName;
   size_t len;

   nextToken = peekToken(in);
   if(nextToken == "Materials:") 
   {
      cout << endl << "Materials found: ";
      ignoreToken(in);
      in >> numMaterials;
      cout << numMaterials << endl;
      out << numMaterials << endl;
   }

   // Begin Processing Materials
   if(_debug)
      cout << "Processing Materials..." << endl;
   for(int k = 0; k < numMaterials; ++k)
   {
      cout << endl << "Processing Material: " << k + 1 << endl;
      nextToken = peekToken(in);

      out << nextToken << endl;
      ignoreToken(in);

      // Iterate over the four color styles (ambient, diffuse, specular, and emissive)
      if(_debug)
         cout << "Processing Color Data..." << endl;
      for(int j = 0; j < 4; ++j)
      {
         // Iterate through each of the 4 numbers in each color style
         for( int i = 0; i < 4; ++i)
         {
            in >> colorData;
            out << colorData << ' ';
         }
         out << endl;
      }

      // Get shininess
      in >> colorData;
      out << colorData << endl;

      // Get transparency
      in >> colorData;
      out << colorData << endl;

      // Get the textures
      if(_debug)
         cout << "Processing Texture..." << endl;
      in >> textureName;
      len = textureName.length();   
      int substrStartPos = FindLastSlash(textureName) + 1;
      if( len > 2 ) {
         if(-1 == substrStartPos)
         {
            cout << "Texture Name problem." << endl;
            return;
         }
         textureName = FOLDER_LOCATION + textureName.substr(substrStartPos, len-(substrStartPos+1));
         out << textureName << endl;
      }
      else
      {
         out << NO_TEXTURE << endl;
      }
      ignoreToken(in);
   }
   out << endl;

   // Done Processing Materials, thus done with execution
   out << endl << endl;
   cout << "Total Number of Triangles: " << triangleSum;
   cout << "Triangles/Surface:         " << double(triangleSum/numMeshes);
   exit(0);
}


string peekToken(ifstream &in)
{
   assert(in);
   long pos = in.tellg();
   string input;

   in >> input;

   in.seekg(pos);
   return input;
}

void ignoreToken(ifstream &in)
{
   assert(in);
   string ignored;
   in >> ignored;
   return;
}

bool isAlpha(char alpha)
{
   return (alpha >= 'a' && alpha <= 'z') || (alpha >= 'A' && alpha <= 'Z'); 
}

int FindLastSlash(string examined)
{
   int len = examined.length();
   int lastSlashPosition = -1;

   for(int i = len-1; i >=0; --i)
   {
      if(examined[i] == '\\')
         return i;
   }
}

Topic ProjectTwoFileConverter . { Edit | Attach | Ref-By | Printable | Diffs | r1.2 | > | r1.1 | More }
Revision r1.2 - 04 Apr 2004 - 14:15 GMT - JonDodge
Parents: WebHome > ProjectTwo
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.