TWiki . Team1 . ProjectTwoFileConverter

-- 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";

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

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

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");

  // destroy window
  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;

   cout << "Please input the filename of the readFile: ";
   cin >> readFileName;
   cout << "Please input the filename of the writeFile: ";
   cin >> writeFileName;
   in.open(readFileName.c_str());
   inConstants.open(CONSTANTS_INFILE_LOCATION.c_str());
   out.open(writeFileName.c_str());
   outConstants.open(CONSTANTS_OUTFILE_LOCATION.c_str());

   string inConstantsFirstString = peekToken(inConstants);
   bool isinConstantsEmpty = (inConstantsFirstString == CONSTANTS_INFILE_EMPTY);
   bool meshesMatch = false;
   int numMeshes;

   string nextToken;
   in >> nextToken;
   while(nextToken != "Meshes:")
   {
      in >> nextToken; 
   }

   // Get the number of meshes
   in >> numMeshes;
   if(!isinConstantsEmpty)
   {
      int numMeshesInConstants;
      inConstants >> numMeshesInConstants;
      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;

   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);
      // Is there actually a surface to be lookin' at

         out << nextToken << endl;


         if(!isinConstantsEmpty && meshesMatch)
         {
   cout << peekToken(inConstants) << endl;
            ignoreToken(inConstants);
   cout << peekToken(inConstants);
            ignoreToken(inConstants);
            inConstants >> tilingConstant;
   cout << ' ' << tilingConstant << endl << peekToken(inConstants);
            ignoreToken(inConstants);
            inConstants >> bounceDamp;
   cout << ' ' << bounceDamp << endl << peekToken(inConstants);
            ignoreToken(inConstants);
            inConstants >> rollDamp;
   cout << ' ' << rollDamp << endl << peekToken(inConstants);
            ignoreToken(inConstants);
            inConstants >> isHole;
   cout << ' ' << isHole << endl << peekToken(inConstants);
            ignoreToken(inConstants);
            inConstants >> isLiquid;
   cout << ' ' << isLiquid << endl << peekToken(inConstants);
            ignoreToken(inConstants);
            inConstants >> isFlying;
   cout << ' ' << isFlying << endl;
         }
         
         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;
         out << bounceDamp << endl
            << rollDamp << endl
            << isHole << endl
            << isLiquid << endl
            << isFlying << endl;

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

         vector<Tuple*> vertices;
         Tuple* addTuple;

         // 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;
         }

         out << endl;

         // Its Normals readin' time
         in >> numNormals;
         out << numNormals << endl;
         for(int j = 0; j < numNormals; ++j)
         {
            for(int i = 0; i < 3; ++i)
            {
               in >> normalData;
               out << normalData << ' ';
            }
            out << endl;
         }
         out << endl;

         // Its Triangles readin' time
         in >> numTriangles;
         out << numTriangles << endl;
         cout << "Number of Triangles " << numTriangles << endl;
         vector<Tuple*> corners;
         for(int i = 0; i < 3; ++i)
            corners.push_back(NULL);

         for(int j = 0; j < numTriangles; ++j)
         {
            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]);

            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);
            
            for(int i = 0; i < 3; ++i) 
            {
               for(int h = 0; h < 16; ++h) 
               {
                  out << addFace.getForwardTransform(i)[h] << " ";
               }
               out << endl;
            }
            
            out << endl;

            for(int i = 0; i < 3; ++i) {
               for(int h = 0; h < 3; ++h)
                  out << ((*(addFace.getStandard()))[i])[h] << " ";
               out << endl;
            }
            
            out << 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;
         }
         
         out << 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;
   }

   for(int k = 0; k < numMaterials; ++k)
   {
      cout << endl << "Processing Material: " << k + 1 << endl;
      nextToken = peekToken(in);
        // Is there actually a Material here?
      if(newMaterialFound(nextToken))
      {
         out << nextToken << endl;
         ignoreToken(in);

         // Iterate over the four color styles (ambient, diffuse, specular, and emissive)
         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
         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 << "NOTEXTURE" << endl;
         }
         ignoreToken(in);
      }
      out << endl;
   }
   out << endl << endl;
   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 newSurfaceFound(string nextToken)
{
   cout << nextToken << endl;
   if(nextToken.find("terrain") != string::npos || nextToken.find("Regroup") != string::npos || nextToken.find("Duplicate")!=string::npos || nextToken.find("Sphere")!=string::npos || nextToken.find("Cylinder")!=string::npos || nextToken.find("GeoSphere")!=string::npos || nextToken.find("Box")!=string::npos)
      return true;
   else 
      return false;
}
bool newMaterialFound(string nextToken)
{
   cout << nextToken << endl;
   if(nextToken.find("Material") != string::npos)
      return true;
   else
      return false;
}

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;
   }
}

----- Revision r1.1 - 04 Apr 2004 - 15:04 GMT - TWikiGuest
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.