home *** CD-ROM | disk | FTP | other *** search
- /* objIO.c */
- /* written by : Jason R. Wilson 2/21/93 */
-
-
- #include <math.h>
- #include <stdio.h>
- #include "datastruct.h"
-
- void ReadInObject (ObjectCell *Object,char *ObjectFileName)
-
- /* Read in an object from .obj file */
-
- {
- /*vars --------------------------------------------------*/
-
- FILE *fpObject; /* This is the file pointer for reading in the object */
- FILE *fpMap;
- int loop,loop2;
- int tempvert;
- VertexCell *NewVertex;
- PolygonCell *NewPolygon;
- VertexCell **Lookup;
- PolygonListCell *NewPolygonList;
- VertexListCell *NewVertexList;
- int SurfaceType;
- char MapFileName[20];
- int mapWidth,mapHeight;
- int maxValue;
- unsigned char r,g,b;
- int maploop,maploop2;
- double AverageR,AverageG,AverageB;
- char foo[256];
- int PolygonVertices;
-
- /*code --------------------------------------------------*/
-
- fpObject = fopen(ObjectFileName,"r"); /* open object data file */
- fscanf (fpObject,"%d",&Object->NoofVertices);
- Lookup = (VertexCell **)(malloc(4*(Object->NoofVertices+1)));
- fscanf (fpObject,"%d",&Object->NoofPolygons);
-
-
- /* READ IN VERTICES */
-
- NewVertex = (VertexCell *)(malloc(sizeof(VertexCell)));
- InitVertexCell (NewVertex);
- Object->VertexHead = NewVertex;
- for (loop=1;loop <= Object->NoofVertices;loop++)
- {
- fscanf (fpObject,"%lf %lf %lf",&NewVertex->WorldPosition.x,
- &NewVertex->WorldPosition.y,
- &NewVertex->WorldPosition.z);
- NewVertex->Number = loop;
- Lookup[loop] = NewVertex; /* set up lookup table */
- if (loop == Object->NoofVertices)
- NewVertex->Next = NULL;
- else
- {
- NewVertex->Next = (VertexCell *)(malloc(sizeof(VertexCell)));
- InitVertexCell (NewVertex->Next);
- NewVertex = NewVertex->Next;
- }
- }
-
- /* READ IN POLYGONS */
-
- NewPolygon = (PolygonCell *)(malloc(sizeof(PolygonCell)));
- InitPolygonCell (NewPolygon);
- Object->PolygonHead = NewPolygon;
- for (loop=1;loop <= Object->NoofPolygons;loop++)
- {
- NewVertexList = (VertexListCell *)(malloc(sizeof(VertexListCell)));
- InitVertexListCell (NewVertexList);
- NewPolygon->Culled = false;
- NewPolygon->Vertices = NewVertexList;
- /* Give Polygon Vertices and Give Vertices polygon */
- /* Assume for now only 4 vertices */
- fscanf (fpObject,"%d",&PolygonVertices);
- for (loop2=0;loop2 < PolygonVertices;loop2++)
- {
- fscanf (fpObject,"%d",&tempvert);
- NewVertexList->Vertex = Lookup[tempvert];
- /* Make new polygonlist cell for this vertex */
- NewPolygonList = (PolygonListCell *)(malloc(sizeof(PolygonListCell)));
- InitPolygonListCell (NewPolygonList);
- NewPolygonList->Polygon = NewPolygon;
- /* Insert into polygonlist of vertex */
- if (Lookup[tempvert]->Polygons == NULL)
- {
- Lookup[tempvert]->Polygons = NewPolygonList;
- Lookup[tempvert]->Polygons->Rest = NULL;
- }
- else
- {
- NewPolygonList->Rest = Lookup[tempvert]->Polygons;
- Lookup[tempvert]->Polygons = NewPolygonList;
- }
- if (loop2 == (PolygonVertices - 1))
- NewVertexList->Rest = NULL;
- else
- {
- NewVertexList->Rest =
- (VertexListCell *)(malloc(sizeof(VertexListCell)));
- InitVertexListCell (NewVertexList->Rest);
- NewVertexList = NewVertexList->Rest;
- }
-
- }
-
- fscanf (fpObject,"%d",&SurfaceType);
- if (SurfaceType == 0)
- {
- NewPolygon->Textured = false;
- fscanf (fpObject,"%lf",&NewPolygon->E.r);
- fscanf (fpObject,"%lf",&NewPolygon->E.g);
- fscanf (fpObject,"%lf",&NewPolygon->E.b);
-
-
- fscanf (fpObject,"%lf",&NewPolygon->R.r);
- fscanf (fpObject,"%lf",&NewPolygon->R.g);
- fscanf (fpObject,"%lf",&NewPolygon->R.b);
- }
- else
- {
- fscanf (fpObject,"%s",MapFileName);
- printf ("reading %s for texture map...\n",MapFileName);
- fpMap = fopen (MapFileName,"r"); /* open texture map data file */
- fscanf (fpMap,"%s",foo);
- fscanf (fpMap,"%d %d",&mapWidth,&mapHeight);
- fscanf (fpMap,"%d",&maxValue);
- NewPolygon->Textured = true;
- /* init. the texture map */
- Object->TextureMap = (Color **)(malloc(mapWidth*sizeof(Color *)));
- for (maploop = 0;maploop < mapWidth;maploop++)
- Object->TextureMap[maploop] = (Color *)(malloc(mapHeight*
- sizeof(Color)));
- AverageR = 0.0;
- AverageG = 0.0;
- AverageB = 0.0;
- fscanf (fpMap,"%c",&r); /* temp. hack because of giftoppm */
- for (maploop = 0;maploop < mapHeight;maploop++)
- for (maploop2 = 0;maploop2 < mapWidth;maploop2++)
- {
- fscanf (fpMap,"%c%c%c",&r,&g,&b);
- Object->TextureMap[maploop2][maploop].r =
- ((double)r/(double)maxValue);
- Object->TextureMap[maploop2][maploop].g =
- ((double)g/(double)maxValue);
- Object->TextureMap[maploop2][maploop].b =
- ((double)b/(double)maxValue);
- AverageR += Object->TextureMap[maploop2][maploop].r;
- AverageG += Object->TextureMap[maploop2][maploop].g;
- AverageB += Object->TextureMap[maploop2][maploop].b;
- }
- /* store average ref. Color value in newly read in polygon */
- NewPolygon->E.r = 0.0;
- NewPolygon->E.g = 0.0;
- NewPolygon->E.b = 0.0;
- NewPolygon->R.r = AverageR/(double)(mapHeight*mapWidth);
- NewPolygon->R.g = AverageG/(double)(mapHeight*mapWidth);
- NewPolygon->R.b = AverageB/(double)(mapHeight*mapWidth);
-
- /* store texture map sizes for object */
- Object->mapHeight = mapHeight;
- Object->mapWidth = mapWidth;
- close (fpMap);
- }
-
- if (loop == Object->NoofPolygons)
- NewPolygon->Next = NULL;
- else
- {
- NewPolygon->Next = (PolygonCell *)(malloc(sizeof(PolygonCell)));
- InitPolygonCell (NewPolygon->Next);
- NewPolygon = NewPolygon->Next;
- }
- }
- close (fpObject);
- free (Lookup); /* free up the lookup array */
- }
-
-
-
-