1 #include <unistd.h>
2 #include <math.h>
3
4
5 #include "globals.h"
6 #include "model.h"
7
8
GenModelPoints(struct Model * model)9 void GenModelPoints(struct Model *model)
10 {
11 int i, j;
12 struct ModelPoint **tempPoints;
13
14 for (i=0; i<model->NumSides; i++) {
15 for (j=0; j<3; j++) {
16 tempPoints=&model->Points;
17 while (*tempPoints!=NULL) {
18 if (model->Sides[i].Vertices[j]->x==(*tempPoints)->point.x &&
19 model->Sides[i].Vertices[j]->y==(*tempPoints)->point.y &&
20 model->Sides[i].Vertices[j]->z==(*tempPoints)->point.z) {
21 free(model->Sides[i].Vertices[j]);
22 free(model->Sides[i].VertexColors[j]);
23 model->Sides[i].Vertices[j]=&((*tempPoints)->point);
24 model->Sides[i].VertexColors[j]=&((*tempPoints)->color);
25 break;
26 }
27 tempPoints=&((*tempPoints)->next);
28 }
29
30 if (*tempPoints==NULL) {
31 *tempPoints=(struct ModelPoint *)malloc(sizeof(struct ModelPoint));
32 (*tempPoints)->point.x=model->Sides[i].Vertices[j]->x;
33 (*tempPoints)->point.y=model->Sides[i].Vertices[j]->y;
34 (*tempPoints)->point.z=model->Sides[i].Vertices[j]->z;
35 free(model->Sides[i].Vertices[j]);
36 (*tempPoints)->color.red=model->Sides[i].VertexColors[j]->red;
37 (*tempPoints)->color.green=model->Sides[i].VertexColors[j]->green;
38 (*tempPoints)->color.blue=model->Sides[i].VertexColors[j]->blue;
39 free(model->Sides[i].VertexColors[j]);
40 model->Sides[i].Vertices[j]=&((*tempPoints)->point);
41 model->Sides[i].VertexColors[j]=&((*tempPoints)->color);
42 (*tempPoints)->next=NULL;
43 }
44 }
45 }
46 }
47
48
AddModelDetail(struct Model ** model)49 void AddModelDetail(struct Model **model)
50 {
51 struct Model *tempModel;
52 int i, j;
53 float color, lengths[3], length, newlength, randomness;
54 struct Point *newPoints[3];
55 struct Color *newColors[3];
56 struct ModelPoint **tempPoints;
57
58 tempModel=(struct Model *)malloc(sizeof(struct Model));
59
60 tempModel->NumSides=(*model)->NumSides*4;
61
62 tempModel->Sides=(struct Triangle *)malloc(sizeof(struct Triangle)*
63 tempModel->NumSides);
64 tempModel->Points=(*model)->Points;
65
66 for (i=0; i<(*model)->NumSides; i++) {
67 for (j=0; j<3; j++) {
68 lengths[j]=sqrt((*model)->Sides[i].Vertices[j]->x*
69 (*model)->Sides[i].Vertices[j]->x+
70 (*model)->Sides[i].Vertices[j]->y*
71 (*model)->Sides[i].Vertices[j]->y+
72 (*model)->Sides[i].Vertices[j]->z*
73 (*model)->Sides[i].Vertices[j]->z);
74 }
75
76 for (j=0; j<3; j++) {
77 newPoints[j]=(struct Point *)malloc(sizeof(struct Point));
78 newColors[j]=(struct Color *)malloc(sizeof(struct Color));
79
80 newlength=(lengths[j]+lengths[(j+1)%3])/2;
81 newPoints[j]->x=((*model)->Sides[i].Vertices[j]->x+
82 (*model)->Sides[i].Vertices[(j+1)%3]->x)/2;
83 newPoints[j]->y=((*model)->Sides[i].Vertices[j]->y+
84 (*model)->Sides[i].Vertices[(j+1)%3]->y)/2;
85 newPoints[j]->z=((*model)->Sides[i].Vertices[j]->z+
86 (*model)->Sides[i].Vertices[(j+1)%3]->z)/2;
87 length=sqrt(newPoints[j]->x*newPoints[j]->x+newPoints[j]->y*newPoints[j]->y+
88 newPoints[j]->z*newPoints[j]->z);
89 newPoints[j]->x*=newlength/length;
90 newPoints[j]->y*=newlength/length;
91 newPoints[j]->z*=newlength/length;
92
93 newColors[j]->red=((*model)->Sides[i].VertexColors[j]->red+
94 (*model)->Sides[i].VertexColors[(j+1)%3]->red)/2;
95 newColors[j]->green=((*model)->Sides[i].VertexColors[j]->green+
96 (*model)->Sides[i].VertexColors[(j+1)%3]->green)/2;
97 newColors[j]->blue=((*model)->Sides[i].VertexColors[j]->blue+
98 (*model)->Sides[i].VertexColors[(j+1)%3]->blue)/2;
99
100 tempPoints=&tempModel->Points;
101 while (*tempPoints!=NULL) {
102 if (newPoints[j]->x==(*tempPoints)->point.x &&
103 newPoints[j]->y==(*tempPoints)->point.y &&
104 newPoints[j]->z==(*tempPoints)->point.z) {
105 free(newPoints[j]);
106 free(newColors[j]);
107 newPoints[j]=&((*tempPoints)->point);
108 newColors[j]=&((*tempPoints)->color);
109 break;
110 }
111 tempPoints=&((*tempPoints)->next);
112 }
113
114 if (*tempPoints==NULL) {
115 *tempPoints=(struct ModelPoint *)malloc(sizeof(struct ModelPoint));
116 (*tempPoints)->point.x=newPoints[j]->x;
117 (*tempPoints)->point.y=newPoints[j]->y;
118 (*tempPoints)->point.z=newPoints[j]->z;
119 free(newPoints[j]);
120 (*tempPoints)->color.red=newColors[j]->red;
121 (*tempPoints)->color.green=newColors[j]->green;
122 (*tempPoints)->color.blue=newColors[j]->blue;
123 free(newColors[j]);
124 newPoints[j]=&((*tempPoints)->point);
125 newColors[j]=&((*tempPoints)->color);
126 (*tempPoints)->next=NULL;
127 }
128 }
129
130 for (j=0; j<3; j++) {
131 tempModel->Sides[i*4+j].VertexColors[0]=newColors[j];
132 tempModel->Sides[i*4+j].Vertices[0]=newPoints[j];
133
134 tempModel->Sides[i*4+j].VertexColors[1]=(*model)->Sides[i].VertexColors[j];
135 tempModel->Sides[i*4+j].Vertices[1]=(*model)->Sides[i].Vertices[j];
136
137 tempModel->Sides[i*4+j].VertexColors[2]=newColors[(2+j)%3];
138 tempModel->Sides[i*4+j].Vertices[2]=newPoints[(2+j)%3];
139 }
140
141 for (j=0; j<3; j++) {
142 tempModel->Sides[i*4+3].VertexColors[j]=newColors[j];
143 tempModel->Sides[i*4+3].Vertices[j]=newPoints[j];
144 }
145 }
146
147 free((*model));
148 (*model)=tempModel;
149 }
150
151
RandomizeModel(struct Model * model,float Randomization)152 void RandomizeModel(struct Model *model, float Randomization)
153 {
154 struct ModelPoint *tempPoints;
155 float Randomness;
156
157 tempPoints=model->Points;
158 while (tempPoints) {
159 Randomness=(((float)(rand()%256))*Randomization)/512.0-Randomization/2.0;
160 tempPoints->point.x+=Randomness;
161 Randomness=(((float)(rand()%256))*Randomization)/512.0-Randomization/2.0;
162 tempPoints->point.y+=Randomness;
163 Randomness=(((float)(rand()%256))*Randomization)/512.0-Randomization/2.0;
164 tempPoints->point.z+=Randomness;
165 tempPoints=tempPoints->next;
166 }
167 }
168
169
RandomizeModelColors(struct Model * model)170 void RandomizeModelColors(struct Model *model)
171 {
172 struct ModelPoint *tempPoints;
173 float color;
174
175 tempPoints=model->Points;
176 while (tempPoints) {
177 color=((float)(rand()%256))/1280.0+0.1;
178 tempPoints->color.red=color;
179 tempPoints->color.green=color;
180 tempPoints->color.blue=color;
181 tempPoints=tempPoints->next;
182 }
183 }
184
185
ShrinkModel(struct Model * model,float Size)186 void ShrinkModel(struct Model *model, float Size)
187 {
188 struct ModelPoint *tempPoints;
189 int i, j;
190
191 if (model->Points) {
192 tempPoints=model->Points;
193 while (tempPoints) {
194 tempPoints->point.x*=Size;
195 tempPoints->point.y*=Size;
196 tempPoints->point.z*=Size;
197 tempPoints=tempPoints->next;
198 }
199 } else {
200 for (i=0; i<model->NumSides; i++) {
201 for (j=0; j<3; j++) {
202 model->Sides[i].Vertices[j]->x*=Size;
203 model->Sides[i].Vertices[j]->y*=Size;
204 model->Sides[i].Vertices[j]->z*=Size;
205 }
206 }
207 }
208 }
209
210
InvertModel(struct Model * model,int Type)211 void InvertModel(struct Model *model, int Type)
212 {
213 struct ModelPoint *tempPoints;
214 int i, j;
215
216 if (model->Points) {
217 tempPoints=model->Points;
218 while (tempPoints) {
219 if (Type==0) {
220 tempPoints->point.x*=-1.0;
221 } else if (Type==1) {
222 tempPoints->point.y*=-1.0;
223 } else {
224 tempPoints->point.z*=-1.0;
225 }
226 tempPoints=tempPoints->next;
227 }
228 } else {
229 for (i=0; i<model->NumSides; i++) {
230 for (j=0; j<3; j++) {
231 if (Type==0) {
232 model->Sides[i].Vertices[j]->x*=-1.0;
233 } else if (Type==1) {
234 model->Sides[i].Vertices[j]->y*=-1.0;
235 } else {
236 model->Sides[i].Vertices[j]->z*=-1.0;
237 }
238 }
239 }
240 }
241 }
242
243
ReCenterModel(struct Model * model)244 void ReCenterModel(struct Model *model)
245 {
246 float x=0.0, y=0.0, z=0.0;
247 int NumPoints;
248
249 struct ModelPoint *tempPoints;
250
251 tempPoints=model->Points;
252 while (tempPoints) {
253 NumPoints++;
254 x+=tempPoints->point.x;
255 y+=tempPoints->point.y;
256 z+=tempPoints->point.z;
257 tempPoints=tempPoints->next;
258 }
259 x/=(float)NumPoints;
260 y/=(float)NumPoints;
261 z/=(float)NumPoints;
262
263 tempPoints=model->Points;
264 while (tempPoints) {
265 tempPoints->point.x-=x;
266 tempPoints->point.y-=y;
267 tempPoints->point.z-=z;
268 tempPoints=tempPoints->next;
269 }
270 }
271
272
FindFurthestPoint(struct Model * model)273 float FindFurthestPoint(struct Model *model)
274 {
275 struct ModelPoint *tempPoints;
276 float dist=0.0, newdist;
277 int i, j;
278
279 if (model->Points) {
280 tempPoints=model->Points;
281 while (tempPoints) {
282 newdist=sqrt((tempPoints->point.x*tempPoints->point.x)+
283 (tempPoints->point.y*tempPoints->point.y)+
284 (tempPoints->point.z*tempPoints->point.z));
285 if (newdist > dist) {
286 dist=newdist;
287 }
288 tempPoints=tempPoints->next;
289 }
290 } else {
291 for (i=0; i<model->NumSides; i++) {
292 for (j=0; j<3; j++) {
293 newdist=sqrt((model->Sides[i].Vertices[j]->x*
294 model->Sides[i].Vertices[j]->x)+
295 (model->Sides[i].Vertices[j]->y*
296 model->Sides[i].Vertices[j]->y)+
297 (model->Sides[i].Vertices[j]->z*
298 model->Sides[i].Vertices[j]->z));
299 if (newdist > dist) {
300 dist=newdist;
301 }
302 }
303 }
304 }
305
306 return(dist);
307 }
308