-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrw.c
202 lines (161 loc) · 7.44 KB
/
rw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* rw.c
* RW implementation file - contains the code used to read and write
* models to and from files
*
* badpointer - Nov/2011
*/
/* "Imports" */
/* ------------------------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "dataStructs.h"
#include "consoleIO.h"
#include "globals.h"
/* ------------------------------------------------------------------ */
/* private methods */
/* ------------------------------------------------------------------ */
int writeModel( modelPointer tmpMod, FILE *outputFileStream)
{
int i,j;
if( fprintf(outputFileStream, "%d\n", tmpMod->numVertices) < 0) return -1;
if( fprintf(outputFileStream, "%d\n", tmpMod->numFaces) < 0) return -1;
/* all vertexes */
for(i=0; i<tmpMod->numVertices; i++)
{
if( fprintf(outputFileStream, "%f %f %f\n", tmpMod->vertices[i][0], tmpMod->vertices[i][1], tmpMod->vertices[i][2]) < 0) return -1;
}
/* all triangles */
for(i=0; i<tmpMod->numFaces; i++)
{
if( fprintf(outputFileStream, "%d %d %d\n", tmpMod->faces[i][0], tmpMod->faces[i][1], tmpMod->faces[i][2]) < 0) return -1;
}
/* all normal vectors */
for(i=0; i<tmpMod->numFaces; i++)
{
//if( fprintf(outputFileStream, "%f %f %f\n", tmpMod->normals[i][0], tmpMod->normals[i][1], tmpMod->normals[i][2]) < 0) return -1;
for(j=0; j<3; j++)
if( fprintf(outputFileStream, "%f %f %f\n", tmpMod->normals[indexTriangleVertex(j, i, tmpMod)][0], tmpMod->normals[indexTriangleVertex(j, i, tmpMod)][1], tmpMod->normals[indexTriangleVertex(j, i, tmpMod)][2]) < 0) return -1;
}
/* ligthing coefficients */
if( fprintf(outputFileStream, "%f %f %f %f\n", tmpMod->coefReflAmbiente[0], tmpMod->coefReflAmbiente[1], tmpMod->coefReflAmbiente[2], tmpMod->coefReflAmbiente[3]) < 0) return -1;
if( fprintf(outputFileStream, "%f %f %f %f\n", tmpMod->coefReflDifusa[0], tmpMod->coefReflDifusa[1], tmpMod->coefReflDifusa[2], tmpMod->coefReflDifusa[3]) < 0) return -1;
if( fprintf(outputFileStream, "%f %f %f %f\n", tmpMod->coefReflEspecular[0], tmpMod->coefReflEspecular[1], tmpMod->coefReflEspecular[2], tmpMod->coefReflEspecular[3]) < 0) return -1;
if( fprintf(outputFileStream, "%f\n", tmpMod->coefDePhong[0]) < 0) return -1;
/* translations, rotations, scalings and animations */
if( fprintf(outputFileStream, "%f %f %f\n", tmpMod->translX, tmpMod->translY, tmpMod->translZ) < 0) return -1;
if( fprintf(outputFileStream, "%f %f %f\n", tmpMod->rotAngXX, tmpMod->rotAngYY, tmpMod->rotAngZZ) < 0) return -1;
if( fprintf(outputFileStream, "%f %f %f\n", tmpMod->factorX, tmpMod->factorY, tmpMod->factorZ) < 0) return -1;
if( fprintf(outputFileStream, "%d %d %d\n", tmpMod->rotationOnXX, tmpMod->rotationOnYY, tmpMod->rotationOnZZ) < 0) return -1;
return 0;
}
int readModel( int modelIndex, FILE *inputFileStream )
{
int i, j;
modelPointer tmpMod = (Model_Record*) malloc(sizeof(Model_Record));
if( fscanf(inputFileStream, "%d\n", &tmpMod->numVertices) < 0) return -1;
if( fscanf(inputFileStream, "%d\n", &tmpMod->numFaces) < 0) return -1;
tmpMod->vertices = (GLfloat**) calloc(tmpMod->numVertices, sizeof(GLfloat*));
/* all vertexes */
for(i=0; i<tmpMod->numVertices; i++)
{
tmpMod->vertices[i] = (GLfloat*) calloc(3, sizeof(GLfloat));
if( fscanf(inputFileStream, "%f %f %f\n", &tmpMod->vertices[i][0], &tmpMod->vertices[i][1], &tmpMod->vertices[i][2]) < 0) return -1;
}
tmpMod->faces = (GLint**) calloc(tmpMod->numFaces, sizeof(GLint*));
/* all triangles */
for(i=0; i<tmpMod->numFaces; i++)
{
tmpMod->faces[i] = (GLint*) calloc(3, sizeof(GLint));
if( fscanf(inputFileStream, "%d %d %d\n", &tmpMod->faces[i][0], &tmpMod->faces[i][1], &tmpMod->faces[i][2]) < 0) return -1;
}
tmpMod->normals = (GLfloat**) calloc(tmpMod->numFaces*3, sizeof(GLfloat*));
/* all normal vectors */
for(i=0; i<tmpMod->numFaces; i++)
{
for(j=0; j<3; j++)
{
tmpMod->normals[indexTriangleVertex(j, i, tmpMod)] = (GLfloat*) calloc(3, sizeof(GLfloat));
if( fscanf(inputFileStream, "%f %f %f\n", &tmpMod->normals[indexTriangleVertex(j, i, tmpMod)][0], &tmpMod->normals[indexTriangleVertex(j, i, tmpMod)][1], &tmpMod->normals[indexTriangleVertex(j, i, tmpMod)][2]) < 0) return -1;
}
}
/* ligthing coefficients */
if( fscanf(inputFileStream, "%f %f %f %f\n", &tmpMod->coefReflAmbiente[0], &tmpMod->coefReflAmbiente[1], &tmpMod->coefReflAmbiente[2], &tmpMod->coefReflAmbiente[3]) < 0) return -1;
if( fscanf(inputFileStream, "%f %f %f %f\n", &tmpMod->coefReflDifusa[0], &tmpMod->coefReflDifusa[1], &tmpMod->coefReflDifusa[2], &tmpMod->coefReflDifusa[3]) < 0) return -1;
if( fscanf(inputFileStream, "%f %f %f %f\n", &tmpMod->coefReflEspecular[0], &tmpMod->coefReflEspecular[1], &tmpMod->coefReflEspecular[2], &tmpMod->coefReflEspecular[3]) < 0) return -1;
if( fscanf(inputFileStream, "%f\n", &tmpMod->coefDePhong[0]) < 0) return -1;
/* translations, rotations, scalings and animations */
if( fscanf(inputFileStream, "%f %f %f\n", &tmpMod->translX, &tmpMod->translY, &tmpMod->translZ) < 0) return -1;
if( fscanf(inputFileStream, "%f %f %f\n", &tmpMod->rotAngXX, &tmpMod->rotAngYY, &tmpMod->rotAngZZ) < 0) return -1;
if( fscanf(inputFileStream, "%f %f %f\n", &tmpMod->factorX, &tmpMod->factorY, &tmpMod->factorZ) < 0) return -1;
if( fscanf(inputFileStream, "%d %d %d\n", &tmpMod->rotationOnXX, &tmpMod->rotationOnYY, &tmpMod->rotationOnZZ) < 0) return -1;
models[modelIndex] = tmpMod;
return 0;
}
/* ------------------------------------------------------------------ */
/* public methods */
/* ------------------------------------------------------------------ */
int writeConstruction( void )
{
FILE *outputFileStream;
char * fileName = "construction.lgo";
int i;
/* open file for writing */
if((outputFileStream = fopen(fileName, "w")) == NULL)
{
debug("**********ERROR**********\n\tError while opening file for writing.\n**********/ERROR*********\n");
return -1;
}
/* write number of models into the file */
if( fprintf(outputFileStream, "%d\n", numberModels) < 0 )
{
debug("**********ERROR**********\n\tError while writing number of models in construction to the file.\n**********/ERROR*********\n");
return -2;
}
/* write all moedls into the file */
for(i = 0; i<numberModels; i++)
{
if( writeModel(models[i], outputFileStream) < 0 )
{
debug("**********ERROR**********\n\tError while writing model to the file.\n**********/ERROR*********\n");
return -3;
}
}
debug("Construction written with sucess\n");
/* close fileStream */
fclose(outputFileStream);
return 0;
}
int readConstruction( void )
{
FILE *inputFileStream;
char * fileName = "construction.lgo";
int i;
/* open file for reading */
if((inputFileStream = fopen("construction.lgo", "r")) == NULL)
{
debug("**********ERROR**********\n\tError while opening file for reading. Does the file exists?\n**********/ERROR*********\n");
return -1;
}
/* read number of models on the file */
if( fscanf(inputFileStream, "%d", &numberModels) < 0)
{
debug("**********ERROR**********\n\tError while reading number of models in construction to the file.\n**********/ERROR*********\n");
return -2;
}
/* read all models from the file */
for(i=0; i<numberModels; i++)
{
if( readModel(i, inputFileStream) < 0 )
{
debug("**********ERROR**********\n\tError while reading model from the file.\n**********/ERROR*********\n");
return -3;
}
}
debug("Construction has been read with sucess\n");
/* close fileStream */
fclose(inputFileStream);
return 0;
}
/* ------------------------------------------------------------------ */