-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloading.c
179 lines (157 loc) · 6.43 KB
/
loading.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
//
// loading.c
// Ray
//
// Created by Celestino Simone on 10/05/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include "structure.h"
#include "prototype.h"
int getOpcode(FILE *file)
{ char opcode[10];
fscanf(file,"%s",&opcode);
if(strcmp(opcode,"reso")==0) return RESO;
if(strcmp(opcode,"size")==0) return SIZE;
if(strcmp(opcode,"sphr")==0) return SPHR;
if(strcmp(opcode,"poly")==0) return POLY;
if(strcmp(opcode,"lght")==0) return LGHT;
if(strcmp(opcode,"matr")==0) return MATR;
if(strcmp(opcode,"plne")==0) return PLNE;
if(strcmp(opcode,"aamx")==0) return AAMX;
if(strcmp(opcode,"maxt")==0) return MAXT;
if(strcmp(opcode,"cpos")==0) return CPOS;
return UNKN;
}
int loadconfig(char *path, scene_t *scene)
{ FILE *file;
struct prim_t tempPrim[10],s,p,pl;
light_t tempLight[10],l;
struct material_t tempMat[10],m;
int countPrim = 0;
int countLight = 0;
int countMat = 0;
int i;
file = fopen(path,"rt");
if(!file) printf("Problema apertura file\n");
if(file != NULL)
{
int line = 0;
while(!feof(file))
{ line++;
switch(getOpcode(file))
{
case RESO:
fscanf(file,"%d",&scene->screenResX);
fscanf(file,"%d",&scene->screenResY);
break;
case SIZE:
fscanf(file,"%lf",&scene->screenSizeX);
fscanf(file,"%lf",&scene->screenSizeY);
break;
case SPHR:
s.type = SPHERE;
fscanf(file,"%lf",&s.sphere.center.x);
fscanf(file,"%lf",&s.sphere.center.y);
fscanf(file,"%lf",&s.sphere.center.z);
fscanf(file,"%lf",&s.sphere.r);
fscanf(file,"%d",&s.mat);
tempPrim[countPrim]=s;
countPrim++;
break;
case POLY:
p.type = POLYGON;
int k,h;
fscanf(file,"%d",&p.polygon.numfaces);
p.polygon.faces=(point_t *) malloc(sizeof(point_t) * p.polygon.numfaces);
for(k=0; k<p.polygon.numfaces ; k++)
{
fscanf(file,"%d",&p.polygon.faces[k].numvertices);
p.polygon.faces[k].vertices=(point_t *) malloc(sizeof(point_t) * p.polygon.numfaces);
for(h=0; h<p.polygon.numfaces ; h++)
{ fscanf(file,"%lf",&p.polygon.faces[k].vertices[h].x);
fscanf(file,"%lf",&p.polygon.faces[k].vertices[h].y);
fscanf(file,"%lf",&p.polygon.faces[k].vertices[h].z);
}
fscanf(file,"%lf",&p.polygon.faces[k].normal.x);
fscanf(file,"%lf",&p.polygon.faces[k].normal.y);
fscanf(file,"%lf",&p.polygon.faces[k].normal.z);
}
/*fscanf(file,"%lf",&p.polygon.ptA.x);
fscanf(file,"%lf",&p.polygon.ptA.y);
fscanf(file,"%lf",&p.polygon.ptA.z);
fscanf(file,"%lf",&p.polygon.ptB.x);
fscanf(file,"%lf",&p.polygon.ptB.y);
fscanf(file,"%lf",&p.polygon.ptB.z);
fscanf(file,"%lf",&p.polygon.ptC.x);
fscanf(file,"%lf",&p.polygon.ptC.y);
fscanf(file,"%lf",&p.polygon.ptC.z);*/
fscanf(file,"%d",&p.mat);
tempPrim[countPrim]=p;
countPrim++;
break;
case LGHT:
fscanf(file,"%lf",&l.x);
fscanf(file,"%lf",&l.y);
fscanf(file,"%lf",&l.z);
tempLight[countLight]=l;
countLight++;
break;
case MATR:
fscanf(file,"%d",&m.mId);
fscanf(file,"%lf",&m.col.r);
fscanf(file,"%lf",&m.col.g);
fscanf(file,"%lf",&m.col.b);
fscanf(file,"%lf",&m.coefDiffuse);
fscanf(file,"%lf",&m.coefReflect);
fscanf(file,"%lf",&m.coefRefract);
fscanf(file,"%lf",&m.refrIndex);
tempMat[countMat]=m;
countMat++;
break;
case PLNE:
pl.type = PLANE;
fscanf(file,"%lf",&pl.plane.n.x);
fscanf(file,"%lf",&pl.plane.n.y);
fscanf(file,"%lf",&pl.plane.n.z);
norm(&pl.plane.n);
fscanf(file,"%lf",&pl.plane.d);
fscanf(file,"%d",&pl.mat);
tempPrim[countPrim]=pl;
break;
case AAMX:
fscanf(file,"%d",&scene->maxAA);
break;
case CPOS:
fscanf(file,"%lf",&scene->cam.x);
fscanf(file,"%lf",&scene->cam.y);
fscanf(file,"%lf",&scene->cam.z);
break;
case MAXT:
fscanf(file,"%lf",&scene->maxT);
break;
case UNKN:
return line;
break;
}
}
// Ora occorre inserire gli elementi nella scena
scene->primCount = countPrim;
scene->lightCount = countLight;
scene->matCount = countMat;
// Allochiamo memoria per la scena
scene->prim = (prim_t *) malloc(sizeof(prim_t) * scene->primCount);
scene->light = (light_t *) malloc(sizeof(light_t) * scene->lightCount);
scene->material = (material_t *) malloc(sizeof(material_t) * scene->matCount);
// Copia da temp
for(i = 0; i < scene->primCount; i++)
scene->prim[i] = tempPrim[i];
for(i = 0; i < scene->lightCount; i++)
scene->light[i] = tempLight[i];
for( i = 0; i < scene->matCount; i++)
scene->material[i] = tempMat[i];
}
fclose(file);
return 0;
}