5 extern float multiplier;
6 extern float viewdistance;
8 extern float fadestart;
9 extern float texdetail;
11 extern int loadscreencolor;
15 extern bool visibleloading;
17 void *allocate_aligned(size_t pointer_size, size_t byte_alignment)
19 uintptr_t pointer = (uintptr_t)malloc(pointer_size + byte_alignment + 1);
20 uintptr_t aligned_pointer = (pointer + byte_alignment + 1);
21 aligned_pointer -= (aligned_pointer % byte_alignment);
22 *(uint8_t *)(aligned_pointer - 1) = (aligned_pointer - pointer);
23 return (void *)aligned_pointer;
26 void free_aligned(void *aligned_pointer)
28 free((uint8_t *)(aligned_pointer) - *((uint8_t *)(aligned_pointer) - 1));
31 void dealloc(void* param){
36 int Model::LineCheck(XYZ *p1,XYZ *p2, XYZ *p, XYZ *move, float *rotate)
39 static float distance;
40 static float olddistance;
41 static int intersecting;
42 static int firstintersecting;
47 if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0);
48 if(*rotate)*p2=DoRotation(*p2,0,-*rotate,0);
49 if(!sphere_line_intersection(p1,p2,&boundingspherecenter,
50 &boundingsphereradius))return -1;
53 for (j=0;j<TriangleNum;j++){
54 intersecting=LineFacetd(p1,p2,&vertex[Triangles[j].vertex[0]],&vertex[Triangles[j].vertex[1]],&vertex[Triangles[j].vertex[2]],&facenormals[j],&point);
55 distance=(point.x-p1->x)*(point.x-p1->x)+(point.y-p1->y)*(point.y-p1->y)+(point.z-p1->z)*(point.z-p1->z);
56 if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
59 if(*rotate)*p=DoRotation(*p,0,*rotate,0);
61 return firstintersecting;
64 int Model::LineCheckSlide(XYZ *p1,XYZ *p2, XYZ *p, XYZ *move, float *rotate)
67 static float distance;
68 static float olddistance;
69 static int intersecting;
70 static int firstintersecting;
75 if(!sphere_line_intersection(p1,p2,&boundingspherecenter,
76 &boundingsphereradius))return -1;
78 if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0);
79 if(*rotate)*p2=DoRotation(*p2,0,-*rotate,0);
81 for (j=0;j<TriangleNum;j++){
82 intersecting=LineFacetd(p1,p2,&vertex[Triangles[j].vertex[0]],&vertex[Triangles[j].vertex[1]],&vertex[Triangles[j].vertex[2]],&facenormals[j],&point);
83 distance=(point.x-p1->x)*(point.x-p1->x)+(point.y-p1->y)*(point.y-p1->y)+(point.z-p1->z)*(point.z-p1->z);
84 if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j;}
87 distance=abs((facenormals[firstintersecting].x*p2->x)+(facenormals[firstintersecting].y*p2->y)+(facenormals[firstintersecting].z*p2->z)-((facenormals[firstintersecting].x*vertex[Triangles[firstintersecting].vertex[0]].x)+(facenormals[firstintersecting].y*vertex[Triangles[firstintersecting].vertex[0]].y)+(facenormals[firstintersecting].z*vertex[Triangles[firstintersecting].vertex[0]].z)));
88 *p2-=facenormals[firstintersecting]*distance;
90 if(*rotate)*p2=DoRotation(*p2,0,*rotate,0);
92 return firstintersecting;
95 int Model::LineCheckPossible(XYZ *p1,XYZ *p2, XYZ *p, XYZ *move, float *rotate)
98 static float distance;
99 static float olddistance;
100 static int intersecting;
101 static int firstintersecting;
106 if(!sphere_line_intersection(p1,p2,&boundingspherecenter,
107 &boundingsphereradius))return -1;
108 firstintersecting=-1;
109 if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0);
110 if(*rotate)*p2=DoRotation(*p2,0,-*rotate,0);
112 if(numpossible>0&&numpossible<TriangleNum)
113 for (j=0;j<numpossible;j++){
114 if(possible[j]>=0&&possible[j]<TriangleNum){
115 intersecting=LineFacetd(p1,p2,&vertex[Triangles[possible[j]].vertex[0]],&vertex[Triangles[possible[j]].vertex[1]],&vertex[Triangles[possible[j]].vertex[2]],&facenormals[possible[j]],&point);
116 distance=(point.x-p1->x)*(point.x-p1->x)+(point.y-p1->y)*(point.y-p1->y)+(point.z-p1->z)*(point.z-p1->z);
117 if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=possible[j]; *p=point;}
121 if(*rotate)*p=DoRotation(*p,0,*rotate,0);
123 return firstintersecting;
126 int Model::LineCheckSlidePossible(XYZ *p1,XYZ *p2, XYZ *p, XYZ *move, float *rotate)
129 static float distance;
130 static float olddistance;
131 static int intersecting;
132 static int firstintersecting;
137 if(!sphere_line_intersection(p1,p2,&boundingspherecenter,
138 &boundingsphereradius))return -1;
139 firstintersecting=-1;
140 if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0);
141 if(*rotate)*p2=DoRotation(*p2,0,-*rotate,0);
144 for (j=0;j<numpossible;j++){
145 if(possible[j]>=0&&possible[j]<TriangleNum){
146 intersecting=LineFacetd(p1,p2,&vertex[Triangles[possible[j]].vertex[0]],&vertex[Triangles[possible[j]].vertex[1]],&vertex[Triangles[possible[j]].vertex[2]],&facenormals[possible[j]],&point);
147 distance=(point.x-p1->x)*(point.x-p1->x)+(point.y-p1->y)*(point.y-p1->y)+(point.z-p1->z)*(point.z-p1->z);
148 if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=possible[j];}
152 if(firstintersecting>0){
153 distance=abs((facenormals[firstintersecting].x*p2->x)+(facenormals[firstintersecting].y*p2->y)+(facenormals[firstintersecting].z*p2->z)-((facenormals[firstintersecting].x*vertex[Triangles[firstintersecting].vertex[0]].x)+(facenormals[firstintersecting].y*vertex[Triangles[firstintersecting].vertex[0]].y)+(facenormals[firstintersecting].z*vertex[Triangles[firstintersecting].vertex[0]].z)));
154 *p2-=facenormals[firstintersecting]*distance;
157 if(*rotate)*p2=DoRotation(*p2,0,*rotate,0);
159 return firstintersecting;
162 int Model::SphereCheck(XYZ *p1,float radius, XYZ *p, XYZ *move, float *rotate)
165 static float distance;
166 static float olddistance;
167 static int intersecting;
168 static int firstintersecting;
171 static XYZ start,end;
173 firstintersecting=-1;
177 if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0);
178 if(findDistancefast(p1,&boundingspherecenter)>radius*radius+boundingsphereradius*boundingsphereradius)return -1;
181 for (j=0;j<TriangleNum;j++){
183 distance=abs((facenormals[j].x*p1->x)+(facenormals[j].y*p1->y)+(facenormals[j].z*p1->z)-((facenormals[j].x*vertex[Triangles[j].vertex[0]].x)+(facenormals[j].y*vertex[Triangles[j].vertex[0]].y)+(facenormals[j].z*vertex[Triangles[j].vertex[0]].z)));
185 point=*p1-facenormals[j]*distance;
186 if(PointInTriangle( &point, facenormals[j], &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))intersecting=1;
187 if(!intersecting)intersecting=sphere_line_intersection(&vertex[Triangles[j].vertex[0]],
188 &vertex[Triangles[j].vertex[1]],
190 if(!intersecting)intersecting=sphere_line_intersection(&vertex[Triangles[j].vertex[1]],
191 &vertex[Triangles[j].vertex[2]],
193 if(!intersecting)intersecting=sphere_line_intersection(&vertex[Triangles[j].vertex[0]],
194 &vertex[Triangles[j].vertex[2]],
197 *p1+=facenormals[j]*(distance-radius);
201 if(LineFacetd(&start,&end,&vertex[Triangles[j].vertex[0]],&vertex[Triangles[j].vertex[1]],&vertex[Triangles[j].vertex[2]],&facenormals[j],&point)){
202 p1->y=point.y+radius;
206 if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
209 if(*rotate)*p=DoRotation(*p,0,*rotate,0);
211 if(*rotate)*p1=DoRotation(*p1,0,*rotate,0);
213 return firstintersecting;
216 int Model::SphereCheckPossible(XYZ *p1,float radius, XYZ *move, float *rotate)
219 static float distance;
220 static float olddistance;
221 static int intersecting;
222 static int firstintersecting;
225 static XYZ start,end;
227 firstintersecting=-1;
234 if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0);
235 if(findDistancefast(p1,&boundingspherecenter)>radius*radius+boundingsphereradius*boundingsphereradius){*p1=oldp1; return -1;}
237 for (j=0;j<TriangleNum;j++){
239 distance=abs((facenormals[j].x*p1->x)+(facenormals[j].y*p1->y)+(facenormals[j].z*p1->z)-((facenormals[j].x*vertex[Triangles[j].vertex[0]].x)+(facenormals[j].y*vertex[Triangles[j].vertex[0]].y)+(facenormals[j].z*vertex[Triangles[j].vertex[0]].z)));
241 point=*p1-facenormals[j]*distance;
242 if(PointInTriangle( &point, facenormals[j], &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))intersecting=1;
243 if(!intersecting)intersecting=sphere_line_intersection(&vertex[Triangles[j].vertex[0]],
244 &vertex[Triangles[j].vertex[1]],
246 if(!intersecting)intersecting=sphere_line_intersection(&vertex[Triangles[j].vertex[1]],
247 &vertex[Triangles[j].vertex[2]],
249 if(!intersecting)intersecting=sphere_line_intersection(&vertex[Triangles[j].vertex[0]],
250 &vertex[Triangles[j].vertex[2]],
253 //if(j>=0&&j<TriangleNum)
254 possible[numpossible]=j;
258 if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j;}
260 if(*rotate)*p1=DoRotation(*p1,0,*rotate,0);
262 return firstintersecting;
266 void Model::UpdateVertexArray(){
267 if(type!=normaltype&&type!=decalstype)return;
271 for(i=0;i<TriangleNum;i++){
273 vArray[j+0]=Triangles[i].gx[0];
274 vArray[j+1]=Triangles[i].gy[0];
275 vArray[j+2]=normals[Triangles[i].vertex[0]].x;
276 vArray[j+3]=normals[Triangles[i].vertex[0]].y;
277 vArray[j+4]=normals[Triangles[i].vertex[0]].z;
278 vArray[j+5]=vertex[Triangles[i].vertex[0]].x;
279 vArray[j+6]=vertex[Triangles[i].vertex[0]].y;
280 vArray[j+7]=vertex[Triangles[i].vertex[0]].z;
282 vArray[j+8]=Triangles[i].gx[1];
283 vArray[j+9]=Triangles[i].gy[1];
284 vArray[j+10]=normals[Triangles[i].vertex[1]].x;
285 vArray[j+11]=normals[Triangles[i].vertex[1]].y;
286 vArray[j+12]=normals[Triangles[i].vertex[1]].z;
287 vArray[j+13]=vertex[Triangles[i].vertex[1]].x;
288 vArray[j+14]=vertex[Triangles[i].vertex[1]].y;
289 vArray[j+15]=vertex[Triangles[i].vertex[1]].z;
291 vArray[j+16]=Triangles[i].gx[2];
292 vArray[j+17]=Triangles[i].gy[2];
293 vArray[j+18]=normals[Triangles[i].vertex[2]].x;
294 vArray[j+19]=normals[Triangles[i].vertex[2]].y;
295 vArray[j+20]=normals[Triangles[i].vertex[2]].z;
296 vArray[j+21]=vertex[Triangles[i].vertex[2]].x;
297 vArray[j+22]=vertex[Triangles[i].vertex[2]].y;
298 vArray[j+23]=vertex[Triangles[i].vertex[2]].z;
301 for(i=0;i<TriangleNum;i++){
303 vArray[j+0]=Triangles[i].gx[0];
304 vArray[j+1]=Triangles[i].gy[0];
305 vArray[j+2]=facenormals[i].x*-1;
306 vArray[j+3]=facenormals[i].y*-1;
307 vArray[j+4]=facenormals[i].z*-1;
308 vArray[j+5]=vertex[Triangles[i].vertex[0]].x;
309 vArray[j+6]=vertex[Triangles[i].vertex[0]].y;
310 vArray[j+7]=vertex[Triangles[i].vertex[0]].z;
312 vArray[j+8]=Triangles[i].gx[1];
313 vArray[j+9]=Triangles[i].gy[1];
314 vArray[j+10]=facenormals[i].x*-1;
315 vArray[j+11]=facenormals[i].y*-1;
316 vArray[j+12]=facenormals[i].z*-1;
317 vArray[j+13]=vertex[Triangles[i].vertex[1]].x;
318 vArray[j+14]=vertex[Triangles[i].vertex[1]].y;
319 vArray[j+15]=vertex[Triangles[i].vertex[1]].z;
321 vArray[j+16]=Triangles[i].gx[2];
322 vArray[j+17]=Triangles[i].gy[2];
323 vArray[j+18]=facenormals[i].x*-1;
324 vArray[j+19]=facenormals[i].y*-1;
325 vArray[j+20]=facenormals[i].z*-1;
326 vArray[j+21]=vertex[Triangles[i].vertex[2]].x;
327 vArray[j+22]=vertex[Triangles[i].vertex[2]].y;
328 vArray[j+23]=vertex[Triangles[i].vertex[2]].z;
333 void Model::UpdateVertexArrayNoTex(){
334 if(type!=normaltype&&type!=decalstype)return;
338 for(i=0;i<TriangleNum;i++){
340 vArray[j+2]=normals[Triangles[i].vertex[0]].x;
341 vArray[j+3]=normals[Triangles[i].vertex[0]].y;
342 vArray[j+4]=normals[Triangles[i].vertex[0]].z;
343 vArray[j+5]=vertex[Triangles[i].vertex[0]].x;
344 vArray[j+6]=vertex[Triangles[i].vertex[0]].y;
345 vArray[j+7]=vertex[Triangles[i].vertex[0]].z;
347 vArray[j+10]=normals[Triangles[i].vertex[1]].x;
348 vArray[j+11]=normals[Triangles[i].vertex[1]].y;
349 vArray[j+12]=normals[Triangles[i].vertex[1]].z;
350 vArray[j+13]=vertex[Triangles[i].vertex[1]].x;
351 vArray[j+14]=vertex[Triangles[i].vertex[1]].y;
352 vArray[j+15]=vertex[Triangles[i].vertex[1]].z;
354 vArray[j+18]=normals[Triangles[i].vertex[2]].x;
355 vArray[j+19]=normals[Triangles[i].vertex[2]].y;
356 vArray[j+20]=normals[Triangles[i].vertex[2]].z;
357 vArray[j+21]=vertex[Triangles[i].vertex[2]].x;
358 vArray[j+22]=vertex[Triangles[i].vertex[2]].y;
359 vArray[j+23]=vertex[Triangles[i].vertex[2]].z;
362 for(i=0;i<TriangleNum;i++){
364 vArray[j+2]=facenormals[i].x*-1;
365 vArray[j+3]=facenormals[i].y*-1;
366 vArray[j+4]=facenormals[i].z*-1;
367 vArray[j+5]=vertex[Triangles[i].vertex[0]].x;
368 vArray[j+6]=vertex[Triangles[i].vertex[0]].y;
369 vArray[j+7]=vertex[Triangles[i].vertex[0]].z;
371 vArray[j+10]=facenormals[i].x*-1;
372 vArray[j+11]=facenormals[i].y*-1;
373 vArray[j+12]=facenormals[i].z*-1;
374 vArray[j+13]=vertex[Triangles[i].vertex[1]].x;
375 vArray[j+14]=vertex[Triangles[i].vertex[1]].y;
376 vArray[j+15]=vertex[Triangles[i].vertex[1]].z;
378 vArray[j+18]=facenormals[i].x*-1;
379 vArray[j+19]=facenormals[i].y*-1;
380 vArray[j+20]=facenormals[i].z*-1;
381 vArray[j+21]=vertex[Triangles[i].vertex[2]].x;
382 vArray[j+22]=vertex[Triangles[i].vertex[2]].y;
383 vArray[j+23]=vertex[Triangles[i].vertex[2]].z;
387 void Model::UpdateVertexArrayNoTexNoNorm(){
388 if(type!=normaltype&&type!=decalstype)return;
391 for(i=0;i<TriangleNum;i++){
393 vArray[j+5]=vertex[Triangles[i].vertex[0]].x;
394 vArray[j+6]=vertex[Triangles[i].vertex[0]].y;
395 vArray[j+7]=vertex[Triangles[i].vertex[0]].z;
397 vArray[j+13]=vertex[Triangles[i].vertex[1]].x;
398 vArray[j+14]=vertex[Triangles[i].vertex[1]].y;
399 vArray[j+15]=vertex[Triangles[i].vertex[1]].z;
401 vArray[j+21]=vertex[Triangles[i].vertex[2]].x;
402 vArray[j+22]=vertex[Triangles[i].vertex[2]].y;
403 vArray[j+23]=vertex[Triangles[i].vertex[2]].z;
407 bool Model::loadnotex(char *filename )
412 int oldvertexNum,oldTriangleNum;
413 oldvertexNum=vertexNum;
414 oldTriangleNum=TriangleNum;
419 tfile=fopen( filename, "rb" );
420 // read model settings
422 fseek(tfile, 0, SEEK_SET);
423 funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
425 // read the model data
426 /*if(owner)dealloc(owner);
427 if(possible)dealloc(possible);
428 if(vertex)dealloc(vertex);
429 if(normals)dealloc(normals);
430 if(facenormals)dealloc(facenormals);
431 if(Triangles)dealloc(Triangles);
432 if(vArray)dealloc(vArray);*/
437 owner = (int*)malloc(sizeof(int)*vertexNum);
438 possible = (int*)malloc(sizeof(int)*TriangleNum);
439 vertex = (XYZ*)malloc(sizeof(XYZ)*vertexNum);
440 Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle)*TriangleNum);
441 vArray = (GLfloat*)malloc(sizeof(GLfloat)*TriangleNum*24);
443 for(i=0;i<vertexNum;i++){
444 funpackf(tfile, "Bf Bf Bf", &vertex[i].x,&vertex[i].y,&vertex[i].z);
447 for(i=0;i<TriangleNum;i++){
448 // funpackf(tfile, "Bi Bi Bi", &Triangles[i].vertex[0], &Triangles[i].vertex[1], &Triangles[i].vertex[2]);
450 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
451 Triangles[i].vertex[ 0] = vertex[ 0];
452 Triangles[i].vertex[ 1] = vertex[ 2];
453 Triangles[i].vertex[ 2] = vertex[ 4];
454 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
455 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
462 for(i=0;i<vertexNum;i++){
467 boundingsphereradius=0;
468 for(i=0;i<vertexNum;i++){
469 for(j=0;j<vertexNum;j++){
470 if(j!=i&&findDistancefast(&vertex[j],&vertex[i])/2>boundingsphereradius){
471 boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2;
472 boundingspherecenter=(vertex[i]+vertex[j])/2;
476 boundingsphereradius=fast_sqrt(boundingsphereradius);
482 bool Model::load(char *filename,bool texture )
489 LOG(std::string("Loading model...") + filename);
493 pgame->LoadingScreen();
496 int oldvertexNum,oldTriangleNum;
497 oldvertexNum=vertexNum;
498 oldTriangleNum=TriangleNum;
503 tfile=fopen( ConvertFileName(filename), "rb" );
504 // read model settings
507 fseek(tfile, 0, SEEK_SET);
508 funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
510 // read the model data
511 /*if(owner)dealloc(owner);
512 if(possible)dealloc(possible);
513 if(vertex)dealloc(vertex);
514 if(normals)dealloc(normals);
515 if(facenormals)dealloc(facenormals);
516 if(Triangles)dealloc(Triangles);
517 if(vArray)dealloc(vArray);*/
522 owner = (int*)malloc(sizeof(int)*vertexNum);
523 possible = (int*)malloc(sizeof(int)*TriangleNum);
524 vertex = (XYZ*)malloc(sizeof(XYZ)*vertexNum);
525 normals = (XYZ*)malloc(sizeof(XYZ)*vertexNum);
526 facenormals = (XYZ*)malloc(sizeof(XYZ)*TriangleNum);
527 Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle)*TriangleNum);
528 vArray = (GLfloat*)malloc(sizeof(GLfloat)*TriangleNum*24);
530 for(i=0;i<vertexNum;i++){
531 funpackf(tfile, "Bf Bf Bf", &vertex[i].x,&vertex[i].y,&vertex[i].z);
534 for(i=0;i<TriangleNum;i++){
535 // funpackf(tfile, "Bi Bi Bi", &Triangles[i].vertex[0], &Triangles[i].vertex[1], &Triangles[i].vertex[2]);
537 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
538 Triangles[i].vertex[ 0] = vertex[ 0];
539 Triangles[i].vertex[ 1] = vertex[ 2];
540 Triangles[i].vertex[ 2] = vertex[ 4];
541 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
542 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
551 for(i=0;i<vertexNum;i++){
556 boundingsphereradius=0;
557 for(i=0;i<vertexNum;i++){
558 for(j=0;j<vertexNum;j++){
559 if(j!=i&&findDistancefast(&vertex[j],&vertex[i])/2>boundingsphereradius){
560 boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2;
561 boundingspherecenter=(vertex[i]+vertex[j])/2;
565 boundingsphereradius=fast_sqrt(boundingsphereradius);
570 bool Model::loaddecal(char *filename,bool texture )
577 LOG(std::string("Loading decal...") + filename);
579 int oldvertexNum,oldTriangleNum;
580 oldvertexNum=vertexNum;
581 oldTriangleNum=TriangleNum;
587 tfile=fopen( filename, "rb" );
588 // read model settings
591 fseek(tfile, 0, SEEK_SET);
592 funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
594 // read the model data
596 /*if(owner)dealloc(owner);
597 if(possible)dealloc(possible);
598 if(vertex)dealloc(vertex);
599 if(normals)dealloc(normals);
600 if(facenormals)dealloc(facenormals);
601 if(Triangles)dealloc(Triangles);
602 if(vArray)dealloc(vArray);*/
607 owner = (int*)malloc(sizeof(int)*vertexNum);
608 possible = (int*)malloc(sizeof(int)*TriangleNum);
609 vertex = (XYZ*)malloc(sizeof(XYZ)*vertexNum);
610 normals = (XYZ*)malloc(sizeof(XYZ)*vertexNum);
611 facenormals = (XYZ*)malloc(sizeof(XYZ)*TriangleNum);
612 Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle)*TriangleNum);
613 vArray = (GLfloat*)malloc(sizeof(GLfloat)*TriangleNum*24);
616 for(i=0;i<vertexNum;i++){
617 funpackf(tfile, "Bf Bf Bf", &vertex[i].x,&vertex[i].y,&vertex[i].z);
620 for(i=0;i<TriangleNum;i++){
621 // funpackf(tfile, "Bi Bi Bi", &Triangles[i].vertex[0], &Triangles[i].vertex[1], &Triangles[i].vertex[2]);
623 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
624 Triangles[i].vertex[ 0] = vertex[ 0];
625 Triangles[i].vertex[ 1] = vertex[ 2];
626 Triangles[i].vertex[ 2] = vertex[ 4];
627 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
628 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
638 for(i=0;i<vertexNum;i++){
642 boundingsphereradius=0;
643 for(i=0;i<vertexNum;i++){
644 for(j=0;j<vertexNum;j++){
645 if(j!=i&&findDistancefast(&vertex[j],&vertex[i])/2>boundingsphereradius){
646 boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2;
647 boundingspherecenter=(vertex[i]+vertex[j])/2;
651 boundingsphereradius=fast_sqrt(boundingsphereradius);
655 decaltexcoords = (float***)malloc(sizeof(float**)*max_model_decals);
656 for(i=0;i<max_model_decals;i++){
657 decaltexcoords[i] = (float**)malloc(sizeof(float*)*3);
659 decaltexcoords[i][j] = (float*)malloc(sizeof(float)*2);
662 //if(decalvertex)free(decalvertex);
663 decalvertex = (XYZ**)malloc(sizeof(XYZ*)*max_model_decals);
664 for(i=0;i<max_model_decals;i++){
665 decalvertex[i] = (XYZ*)malloc(sizeof(XYZ)*3);
668 decaltype = (int*)malloc(sizeof(int)*max_model_decals);
669 decalopacity = (float*)malloc(sizeof(float)*max_model_decals);
670 decalrotation = (float*)malloc(sizeof(float)*max_model_decals);
671 decalalivetime = (float*)malloc(sizeof(float)*max_model_decals);
672 decalposition = (XYZ*)malloc(sizeof(XYZ)*max_model_decals);
678 bool Model::loadraw(char *filename )
685 LOG(std::string("Loading raw...") + filename);
687 int oldvertexNum,oldTriangleNum;
688 oldvertexNum=vertexNum;
689 oldTriangleNum=TriangleNum;
694 tfile=fopen( filename, "rb" );
695 // read model settings
698 fseek(tfile, 0, SEEK_SET);
699 funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
701 // read the model data
702 /*if(owner)dealloc(owner);
703 if(possible)dealloc(possible);
704 if(vertex)dealloc(vertex);
705 if(normals)dealloc(normals);
706 if(facenormals)dealloc(facenormals);
707 if(Triangles)dealloc(Triangles);
708 if(vArray)dealloc(vArray);*/
713 owner = (int*)malloc(sizeof(int)*vertexNum);
714 possible = (int*)malloc(sizeof(int)*TriangleNum);
715 vertex = (XYZ*)malloc(sizeof(XYZ)*vertexNum);
716 Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle)*TriangleNum);
717 vArray = (GLfloat*)malloc(sizeof(GLfloat)*TriangleNum*24);
720 for(i=0;i<vertexNum;i++){
721 funpackf(tfile, "Bf Bf Bf", &vertex[i].x,&vertex[i].y,&vertex[i].z);
724 for(i=0;i<TriangleNum;i++){
725 // funpackf(tfile, "Bi Bi Bi", &Triangles[i].vertex[0], &Triangles[i].vertex[1], &Triangles[i].vertex[2]);
727 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
728 Triangles[i].vertex[ 0] = vertex[ 0];
729 Triangles[i].vertex[ 1] = vertex[ 2];
730 Triangles[i].vertex[ 2] = vertex[ 4];
731 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
732 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
738 for(i=0;i<vertexNum;i++){
746 void Model::UniformTexCoords()
749 for(i=0; i<TriangleNum; i++){
750 Triangles[i].gy[0]=vertex[Triangles[i].vertex[0]].y;
751 Triangles[i].gy[1]=vertex[Triangles[i].vertex[1]].y;
752 Triangles[i].gy[2]=vertex[Triangles[i].vertex[2]].y;
753 Triangles[i].gx[0]=vertex[Triangles[i].vertex[0]].x;
754 Triangles[i].gx[1]=vertex[Triangles[i].vertex[1]].x;
755 Triangles[i].gx[2]=vertex[Triangles[i].vertex[2]].x;
761 void Model::FlipTexCoords()
764 for(i=0; i<TriangleNum; i++){
765 Triangles[i].gy[0]=-Triangles[i].gy[0];
766 Triangles[i].gy[1]=-Triangles[i].gy[1];
767 Triangles[i].gy[2]=-Triangles[i].gy[2];
772 void Model::ScaleTexCoords(float howmuch)
775 for(i=0; i<TriangleNum; i++){
776 Triangles[i].gx[0]*=howmuch;
777 Triangles[i].gx[1]*=howmuch;
778 Triangles[i].gx[2]*=howmuch;
779 Triangles[i].gy[0]*=howmuch;
780 Triangles[i].gy[1]*=howmuch;
781 Triangles[i].gy[2]*=howmuch;
786 void Model::Scale(float xscale,float yscale,float zscale)
789 for(i=0; i<vertexNum; i++){
798 boundingsphereradius=0;
799 for(i=0;i<vertexNum;i++){
800 for(j=0;j<vertexNum;j++){
801 if(j!=i&&findDistancefast(&vertex[j],&vertex[i])/2>boundingsphereradius){
802 boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2;
803 boundingspherecenter=(vertex[i]+vertex[j])/2;
807 boundingsphereradius=fast_sqrt(boundingsphereradius);
810 void Model::ScaleNormals(float xscale,float yscale,float zscale)
812 if(type!=normaltype&&type!=decalstype)return;
814 for(i=0; i<vertexNum; i++){
815 normals[i].x*=xscale;
816 normals[i].y*=yscale;
817 normals[i].z*=zscale;
819 for(i=0; i<TriangleNum; i++){
820 facenormals[i].x*=xscale;
821 facenormals[i].y*=yscale;
822 facenormals[i].z*=zscale;
827 void Model::Translate(float xtrans,float ytrans,float ztrans)
830 for(i=0; i<vertexNum; i++){
838 boundingsphereradius=0;
839 for(i=0;i<vertexNum;i++){
840 for(j=0;j<vertexNum;j++){
841 if(j!=i&&findDistancefast(&vertex[j],&vertex[i])/2>boundingsphereradius){
842 boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2;
843 boundingspherecenter=(vertex[i]+vertex[j])/2;
847 boundingsphereradius=fast_sqrt(boundingsphereradius);
850 void Model::Rotate(float xang,float yang,float zang)
853 for(i=0; i<vertexNum; i++){
854 vertex[i]=DoRotation(vertex[i],xang,yang,zang);
859 boundingsphereradius=0;
860 for(i=0;i<vertexNum;i++){
861 for(j=0;j<vertexNum;j++){
862 if(j!=i&&findDistancefast(&vertex[j],&vertex[i])/2>boundingsphereradius){
863 boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2;
864 boundingspherecenter=(vertex[i]+vertex[j])/2;
868 boundingsphereradius=fast_sqrt(boundingsphereradius);
872 void Model::CalculateNormals(bool facenormalise)
876 pgame->LoadingScreen();
879 if(type!=normaltype&&type!=decalstype)return;
881 for(i=0; i<vertexNum; i++){
887 for(i=0;i<TriangleNum;i++){
888 CrossProduct(vertex[Triangles[i].vertex[1]]-vertex[Triangles[i].vertex[0]],vertex[Triangles[i].vertex[2]]-vertex[Triangles[i].vertex[0]],&facenormals[i]);
890 normals[Triangles[i].vertex[0]].x+=facenormals[i].x;
891 normals[Triangles[i].vertex[0]].y+=facenormals[i].y;
892 normals[Triangles[i].vertex[0]].z+=facenormals[i].z;
894 normals[Triangles[i].vertex[1]].x+=facenormals[i].x;
895 normals[Triangles[i].vertex[1]].y+=facenormals[i].y;
896 normals[Triangles[i].vertex[1]].z+=facenormals[i].z;
898 normals[Triangles[i].vertex[2]].x+=facenormals[i].x;
899 normals[Triangles[i].vertex[2]].y+=facenormals[i].y;
900 normals[Triangles[i].vertex[2]].z+=facenormals[i].z;
901 if(facenormalise)Normalise(&facenormals[i]);
903 for(i=0; i<vertexNum; i++){
904 Normalise(&normals[i]);
907 UpdateVertexArrayNoTex();
910 void Model::drawimmediate()
912 glBindTexture(GL_TEXTURE_2D,(unsigned long)textureptr);
913 glBegin(GL_TRIANGLES);
914 for(int i=0;i<TriangleNum;i++){
915 /*if(Triangles[i].vertex[0]<vertexNum&&Triangles[i].vertex[1]<vertexNum&&Triangles[i].vertex[2]<vertexNum&&Triangles[i].vertex[0]>=0&&Triangles[i].vertex[1]>=0&&Triangles[i].vertex[2]>=0){
916 if(isnormal(vertex[Triangles[i].vertex[0]].x)&&isnormal(vertex[Triangles[i].vertex[0]].y)&&isnormal(vertex[Triangles[i].vertex[0]].z)
917 &&isnormal(vertex[Triangles[i].vertex[1]].x)&&isnormal(vertex[Triangles[i].vertex[1]].y)&&isnormal(vertex[Triangles[i].vertex[1]].z)
918 &&isnormal(vertex[Triangles[i].vertex[2]].x)&&isnormal(vertex[Triangles[i].vertex[2]].y)&&isnormal(vertex[Triangles[i].vertex[2]].z)){
920 glTexCoord2f(Triangles[i].gx[0],Triangles[i].gy[0]);
921 if(color)glColor3f(normals[Triangles[i].vertex[0]].x,normals[Triangles[i].vertex[0]].y,normals[Triangles[i].vertex[0]].z);
922 if(!color&&!flat)glNormal3f(normals[Triangles[i].vertex[0]].x,normals[Triangles[i].vertex[0]].y,normals[Triangles[i].vertex[0]].z);
923 if(!color&&flat)glNormal3f(facenormals[i].x,facenormals[i].y,facenormals[i].y);
924 glVertex3f(vertex[Triangles[i].vertex[0]].x,vertex[Triangles[i].vertex[0]].y,vertex[Triangles[i].vertex[0]].z);
926 glTexCoord2f(Triangles[i].gx[1],Triangles[i].gy[1]);
927 if(color)glColor3f(normals[Triangles[i].vertex[1]].x,normals[Triangles[i].vertex[1]].y,normals[Triangles[i].vertex[1]].z);
928 if(!color&&!flat)glNormal3f(normals[Triangles[i].vertex[1]].x,normals[Triangles[i].vertex[1]].y,normals[Triangles[i].vertex[1]].z);
929 if(!color&&flat)glNormal3f(facenormals[i].x,facenormals[i].y,facenormals[i].y);
930 glVertex3f(vertex[Triangles[i].vertex[1]].x,vertex[Triangles[i].vertex[1]].y,vertex[Triangles[i].vertex[1]].z);
932 glTexCoord2f(Triangles[i].gx[2],Triangles[i].gy[2]);
933 if(color)glColor3f(normals[Triangles[i].vertex[2]].x,normals[Triangles[i].vertex[2]].y,normals[Triangles[i].vertex[2]].z);
934 if(!color&&!flat)glNormal3f(normals[Triangles[i].vertex[2]].x,normals[Triangles[i].vertex[2]].y,normals[Triangles[i].vertex[2]].z);
935 if(!color&&flat)glNormal3f(facenormals[i].x,facenormals[i].y,facenormals[i].y);
936 glVertex3f(vertex[Triangles[i].vertex[2]].x,vertex[Triangles[i].vertex[2]].y,vertex[Triangles[i].vertex[2]].z);
945 if(type!=normaltype&&type!=decalstype)return;
947 glEnableClientState(GL_NORMAL_ARRAY);
948 glEnableClientState(GL_VERTEX_ARRAY);
949 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
951 if(!color)glInterleavedArrays( GL_T2F_N3F_V3F,8*sizeof(GLfloat),&vArray[0]);
952 if(color)glInterleavedArrays( GL_T2F_C3F_V3F,8*sizeof(GLfloat),&vArray[0]);
953 glBindTexture(GL_TEXTURE_2D,(unsigned long)textureptr);
956 glLockArraysEXT( 0, TriangleNum*3);
958 glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
964 if(!color)glDisableClientState(GL_NORMAL_ARRAY);
965 if(color)glDisableClientState(GL_COLOR_ARRAY);
966 glDisableClientState(GL_VERTEX_ARRAY);
967 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
971 void Model::drawdifftex(GLuint texture)
973 glEnableClientState(GL_NORMAL_ARRAY);
974 glEnableClientState(GL_VERTEX_ARRAY);
975 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
976 if(!color)glInterleavedArrays( GL_T2F_N3F_V3F,8*sizeof(GLfloat),&vArray[0]);
977 if(color)glInterleavedArrays( GL_T2F_C3F_V3F,8*sizeof(GLfloat),&vArray[0]);
979 glBindTexture(GL_TEXTURE_2D,(unsigned long)texture);
980 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
981 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
985 glLockArraysEXT( 0, TriangleNum*3);
987 glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
993 if(!color)glDisableClientState(GL_NORMAL_ARRAY);
994 if(color)glDisableClientState(GL_COLOR_ARRAY);
995 glDisableClientState(GL_VERTEX_ARRAY);
996 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
997 //drawdiffteximmediate(texture);
1000 void Model::drawdiffteximmediate(GLuint texture)
1002 glBindTexture(GL_TEXTURE_2D,(unsigned long)texture);
1004 glBegin(GL_TRIANGLES);
1005 for(int i=0;i<TriangleNum;i++){
1006 /*if(Triangles[i].vertex[0]<vertexNum&&Triangles[i].vertex[1]<vertexNum&&Triangles[i].vertex[2]<vertexNum&&Triangles[i].vertex[0]>=0&&Triangles[i].vertex[1]>=0&&Triangles[i].vertex[2]>=0){
1007 if(isnormal(vertex[Triangles[i].vertex[0]].x)&&isnormal(vertex[Triangles[i].vertex[0]].y)&&isnormal(vertex[Triangles[i].vertex[0]].z)
1008 &&isnormal(vertex[Triangles[i].vertex[1]].x)&&isnormal(vertex[Triangles[i].vertex[1]].y)&&isnormal(vertex[Triangles[i].vertex[1]].z)
1009 &&isnormal(vertex[Triangles[i].vertex[2]].x)&&isnormal(vertex[Triangles[i].vertex[2]].y)&&isnormal(vertex[Triangles[i].vertex[2]].z)){
1010 */glTexCoord2f(Triangles[i].gx[0],Triangles[i].gy[0]);
1011 if(color)glColor3f(normals[Triangles[i].vertex[0]].x,normals[Triangles[i].vertex[0]].y,normals[Triangles[i].vertex[0]].z);
1012 if(!color&&!flat)glNormal3f(normals[Triangles[i].vertex[0]].x,normals[Triangles[i].vertex[0]].y,normals[Triangles[i].vertex[0]].z);
1013 if(!color&&flat)glNormal3f(facenormals[i].x,facenormals[i].y,facenormals[i].y);
1014 glVertex3f(vertex[Triangles[i].vertex[0]].x,vertex[Triangles[i].vertex[0]].y,vertex[Triangles[i].vertex[0]].z);
1016 glTexCoord2f(Triangles[i].gx[1],Triangles[i].gy[1]);
1017 if(color)glColor3f(normals[Triangles[i].vertex[1]].x,normals[Triangles[i].vertex[1]].y,normals[Triangles[i].vertex[1]].z);
1018 if(!color&&!flat)glNormal3f(normals[Triangles[i].vertex[1]].x,normals[Triangles[i].vertex[1]].y,normals[Triangles[i].vertex[1]].z);
1019 if(!color&&flat)glNormal3f(facenormals[i].x,facenormals[i].y,facenormals[i].y);
1020 glVertex3f(vertex[Triangles[i].vertex[1]].x,vertex[Triangles[i].vertex[1]].y,vertex[Triangles[i].vertex[1]].z);
1022 glTexCoord2f(Triangles[i].gx[2],Triangles[i].gy[2]);
1023 if(color)glColor3f(normals[Triangles[i].vertex[2]].x,normals[Triangles[i].vertex[2]].y,normals[Triangles[i].vertex[2]].z);
1024 if(!color&&!flat)glNormal3f(normals[Triangles[i].vertex[2]].x,normals[Triangles[i].vertex[2]].y,normals[Triangles[i].vertex[2]].z);
1025 if(!color&&flat)glNormal3f(facenormals[i].x,facenormals[i].y,facenormals[i].y);
1026 glVertex3f(vertex[Triangles[i].vertex[2]].x,vertex[Triangles[i].vertex[2]].y,vertex[Triangles[i].vertex[2]].z);
1033 void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtexture2,GLuint breaktexture)
1036 if(type!=decalstype)return;
1038 static float distancemult;
1039 static int lasttype;
1040 static float viewdistsquared;
1043 viewdistsquared=viewdistance*viewdistance;
1048 glDisable(GL_LIGHTING);
1049 glDisable(GL_CULL_FACE);
1050 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1052 if(numdecals>max_model_decals)numdecals=max_model_decals;
1053 for(i=0;i<numdecals;i++){
1054 if(decaltype[i]==blooddecalfast&&decalalivetime[i]<2)decalalivetime[i]=2;
1056 if(decaltype[i]==shadowdecal&&decaltype[i]!=lasttype){
1057 glBindTexture( GL_TEXTURE_2D, shadowtexture);
1060 glAlphaFunc(GL_GREATER, 0.0001);
1061 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1064 if(decaltype[i]==breakdecal&&decaltype[i]!=lasttype){
1065 glBindTexture( GL_TEXTURE_2D, breaktexture);
1068 glAlphaFunc(GL_GREATER, 0.0001);
1069 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1072 if((decaltype[i]==blooddecal||decaltype[i]==blooddecalslow)&&decaltype[i]!=lasttype){
1073 glBindTexture( GL_TEXTURE_2D, bloodtexture);
1076 glAlphaFunc(GL_GREATER, 0.15);
1077 glBlendFunc(GL_ONE,GL_ZERO);
1080 if((decaltype[i]==blooddecalfast)&&decaltype[i]!=lasttype){
1081 glBindTexture( GL_TEXTURE_2D, bloodtexture2);
1084 glAlphaFunc(GL_GREATER, 0.15);
1085 glBlendFunc(GL_ONE,GL_ZERO);
1088 if(decaltype[i]==shadowdecal){
1089 glColor4f(1,1,1,decalopacity[i]);
1091 if(decaltype[i]==breakdecal){
1092 glColor4f(1,1,1,decalopacity[i]);
1093 if(decalalivetime[i]>58)glColor4f(1,1,1,decalopacity[i]*(60-decalalivetime[i])/2);
1095 if((decaltype[i]==blooddecal||decaltype[i]==blooddecalfast||decaltype[i]==blooddecalslow)){
1096 glColor4f(1,1,1,decalopacity[i]);
1097 if(decalalivetime[i]<4)glColor4f(1,1,1,decalopacity[i]*decalalivetime[i]*.25);
1098 if(decalalivetime[i]>58)glColor4f(1,1,1,decalopacity[i]*(60-decalalivetime[i])/2);
1100 lasttype=decaltype[i];
1101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1104 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
1106 glBegin(GL_TRIANGLES);
1107 for(int j=0;j<3;j++)
1109 glTexCoord2f(decaltexcoords[i][j][0], decaltexcoords[i][j][1]); glVertex3f(decalvertex[i][j].x,decalvertex[i][j].y,decalvertex[i][j].z);
1114 for(i=numdecals-1;i>=0;i--){
1115 decalalivetime[i]+=multiplier;
1116 if(decaltype[i]==blooddecalslow)decalalivetime[i]-=multiplier*2/3;
1117 if(decaltype[i]==blooddecalfast)decalalivetime[i]+=multiplier*4;
1118 if(decaltype[i]==shadowdecal)DeleteDecal(i);
1119 if((decaltype[i]==blooddecal||decaltype[i]==blooddecalfast||decaltype[i]==blooddecalslow)&&decalalivetime[i]>=60)DeleteDecal(i);
1121 glAlphaFunc(GL_GREATER, 0.0001);
1122 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1126 void Model::DeleteDecal(int which)
1129 if(type!=decalstype)return;
1130 decaltype[which]=decaltype[numdecals-1];
1131 decalposition[which]=decalposition[numdecals-1];
1132 for(int i=0;i<3;i++){
1133 decalvertex[which][i]=decalvertex[numdecals-1][i];
1134 decaltexcoords[which][i][0]=decaltexcoords[numdecals-1][i][0];
1135 decaltexcoords[which][i][1]=decaltexcoords[numdecals-1][i][1];
1137 decalrotation[which]=decalrotation[numdecals-1];
1138 decalalivetime[which]=decalalivetime[numdecals-1];
1139 decalopacity[which]=decalopacity[numdecals-1];
1144 void Model::MakeDecal(int atype, XYZ *where,float *size, float *opacity, float *rotation){
1146 if(type!=decalstype)return;
1148 static float placex,placez;
1150 //static XYZ point,point1,point2;
1151 static float distance;
1155 if(findDistancefast(where,&boundingspherecenter)<(boundingsphereradius+*size)*(boundingsphereradius+*size))
1156 for(i=0;i<TriangleNum;i++){
1157 if(facenormals[i].y<-.1&&(vertex[Triangles[i].vertex[0]].y<where->y||vertex[Triangles[i].vertex[1]].y<where->y||vertex[Triangles[i].vertex[2]].y<where->y)){
1158 decalposition[numdecals]=*where;
1159 decaltype[numdecals]=atype;
1160 decalrotation[numdecals]=*rotation;
1161 decalalivetime[numdecals]=0;
1162 distance=abs(((facenormals[i].x*where->x)+(facenormals[i].y*where->y)+(facenormals[i].z*where->z)-((facenormals[i].x*vertex[Triangles[i].vertex[0]].x)+(facenormals[i].y*vertex[Triangles[i].vertex[0]].y)+(facenormals[i].z*vertex[Triangles[i].vertex[0]].z)))/facenormals[i].y);
1163 decalopacity[numdecals]=*opacity-distance/10;
1165 if(decalopacity[numdecals>0]){
1166 placex=vertex[Triangles[i].vertex[0]].x;
1167 placez=vertex[Triangles[i].vertex[0]].z;
1169 decaltexcoords[numdecals][0][0]=(placex-where->x)/(*size)/2+.5;
1170 decaltexcoords[numdecals][0][1]=(placez-where->z)/(*size)/2+.5;
1172 decalvertex[numdecals][0].x=placex;
1173 decalvertex[numdecals][0].z=placez;
1174 decalvertex[numdecals][0].y=vertex[Triangles[i].vertex[0]].y;
1177 placex=vertex[Triangles[i].vertex[1]].x;
1178 placez=vertex[Triangles[i].vertex[1]].z;
1180 decaltexcoords[numdecals][1][0]=(placex-where->x)/(*size)/2+.5;
1181 decaltexcoords[numdecals][1][1]=(placez-where->z)/(*size)/2+.5;
1183 decalvertex[numdecals][1].x=placex;
1184 decalvertex[numdecals][1].z=placez;
1185 decalvertex[numdecals][1].y=vertex[Triangles[i].vertex[1]].y;
1188 placex=vertex[Triangles[i].vertex[2]].x;
1189 placez=vertex[Triangles[i].vertex[2]].z;
1191 decaltexcoords[numdecals][2][0]=(placex-where->x)/(*size)/2+.5;
1192 decaltexcoords[numdecals][2][1]=(placez-where->z)/(*size)/2+.5;
1194 decalvertex[numdecals][2].x=placex;
1195 decalvertex[numdecals][2].z=placez;
1196 decalvertex[numdecals][2].y=vertex[Triangles[i].vertex[2]].y;
1198 if(!(decaltexcoords[numdecals][0][0]<0&&decaltexcoords[numdecals][1][0]<0&&decaltexcoords[numdecals][2][0]<0))
1199 if(!(decaltexcoords[numdecals][0][1]<0&&decaltexcoords[numdecals][1][1]<0&&decaltexcoords[numdecals][2][1]<0))
1200 if(!(decaltexcoords[numdecals][0][0]>1&&decaltexcoords[numdecals][1][0]>1&&decaltexcoords[numdecals][2][0]>1))
1201 if(!(decaltexcoords[numdecals][0][1]>1&&decaltexcoords[numdecals][1][1]>1&&decaltexcoords[numdecals][2][1]>1))
1203 if(decalrotation[numdecals]){
1206 rot.x=decaltexcoords[numdecals][j][0]-.5;
1207 rot.z=decaltexcoords[numdecals][j][1]-.5;
1208 rot=DoRotation(rot,0,-decalrotation[numdecals],0);
1209 decaltexcoords[numdecals][j][0]=rot.x+.5;
1210 decaltexcoords[numdecals][j][1]=rot.z+.5;
1213 if(numdecals<max_model_decals-1)numdecals++;
1221 void Model::MakeDecal(int atype, XYZ where,float size, float opacity, float rotation){
1223 if(type!=decalstype)return;
1225 static float placex,placez;
1227 //static XYZ point,point1,point2;
1228 static float distance;
1232 if(findDistancefast(&where,&boundingspherecenter)<(boundingsphereradius+size)*(boundingsphereradius+size))
1233 for(i=0;i<TriangleNum;i++){
1234 distance=abs(((facenormals[i].x*where.x)+(facenormals[i].y*where.y)+(facenormals[i].z*where.z)-((facenormals[i].x*vertex[Triangles[i].vertex[0]].x)+(facenormals[i].y*vertex[Triangles[i].vertex[0]].y)+(facenormals[i].z*vertex[Triangles[i].vertex[0]].z))));
1235 if(distance<.02&&abs(facenormals[i].y)>abs(facenormals[i].x)&&abs(facenormals[i].y)>abs(facenormals[i].z)){
1236 decalposition[numdecals]=where;
1237 decaltype[numdecals]=atype;
1238 decalrotation[numdecals]=rotation;
1239 decalalivetime[numdecals]=0;
1240 decalopacity[numdecals]=opacity-distance/10;
1242 if(decalopacity[numdecals>0]){
1243 placex=vertex[Triangles[i].vertex[0]].x;
1244 placez=vertex[Triangles[i].vertex[0]].z;
1246 decaltexcoords[numdecals][0][0]=(placex-where.x)/(size)/2+.5;
1247 decaltexcoords[numdecals][0][1]=(placez-where.z)/(size)/2+.5;
1249 decalvertex[numdecals][0].x=placex;
1250 decalvertex[numdecals][0].z=placez;
1251 decalvertex[numdecals][0].y=vertex[Triangles[i].vertex[0]].y;
1254 placex=vertex[Triangles[i].vertex[1]].x;
1255 placez=vertex[Triangles[i].vertex[1]].z;
1257 decaltexcoords[numdecals][1][0]=(placex-where.x)/(size)/2+.5;
1258 decaltexcoords[numdecals][1][1]=(placez-where.z)/(size)/2+.5;
1260 decalvertex[numdecals][1].x=placex;
1261 decalvertex[numdecals][1].z=placez;
1262 decalvertex[numdecals][1].y=vertex[Triangles[i].vertex[1]].y;
1265 placex=vertex[Triangles[i].vertex[2]].x;
1266 placez=vertex[Triangles[i].vertex[2]].z;
1268 decaltexcoords[numdecals][2][0]=(placex-where.x)/(size)/2+.5;
1269 decaltexcoords[numdecals][2][1]=(placez-where.z)/(size)/2+.5;
1271 decalvertex[numdecals][2].x=placex;
1272 decalvertex[numdecals][2].z=placez;
1273 decalvertex[numdecals][2].y=vertex[Triangles[i].vertex[2]].y;
1275 if(!(decaltexcoords[numdecals][0][0]<0&&decaltexcoords[numdecals][1][0]<0&&decaltexcoords[numdecals][2][0]<0))
1276 if(!(decaltexcoords[numdecals][0][1]<0&&decaltexcoords[numdecals][1][1]<0&&decaltexcoords[numdecals][2][1]<0))
1277 if(!(decaltexcoords[numdecals][0][0]>1&&decaltexcoords[numdecals][1][0]>1&&decaltexcoords[numdecals][2][0]>1))
1278 if(!(decaltexcoords[numdecals][0][1]>1&&decaltexcoords[numdecals][1][1]>1&&decaltexcoords[numdecals][2][1]>1))
1280 if(decalrotation[numdecals]){
1283 rot.x=decaltexcoords[numdecals][j][0]-.5;
1284 rot.z=decaltexcoords[numdecals][j][1]-.5;
1285 rot=DoRotation(rot,0,-decalrotation[numdecals],0);
1286 decaltexcoords[numdecals][j][0]=rot.x+.5;
1287 decaltexcoords[numdecals][j][1]=rot.z+.5;
1290 if(numdecals<max_model_decals-1)numdecals++;
1294 else if(distance<.02&&abs(facenormals[i].x)>abs(facenormals[i].y)&&abs(facenormals[i].x)>abs(facenormals[i].z)){
1295 decalposition[numdecals]=where;
1296 decaltype[numdecals]=atype;
1297 decalrotation[numdecals]=rotation;
1298 decalalivetime[numdecals]=0;
1299 decalopacity[numdecals]=opacity-distance/10;
1301 if(decalopacity[numdecals>0]){
1302 placex=vertex[Triangles[i].vertex[0]].y;
1303 placez=vertex[Triangles[i].vertex[0]].z;
1305 decaltexcoords[numdecals][0][0]=(placex-where.y)/(size)/2+.5;
1306 decaltexcoords[numdecals][0][1]=(placez-where.z)/(size)/2+.5;
1308 decalvertex[numdecals][0].x=vertex[Triangles[i].vertex[0]].x;
1309 decalvertex[numdecals][0].z=placez;
1310 decalvertex[numdecals][0].y=placex;
1313 placex=vertex[Triangles[i].vertex[1]].y;
1314 placez=vertex[Triangles[i].vertex[1]].z;
1316 decaltexcoords[numdecals][1][0]=(placex-where.y)/(size)/2+.5;
1317 decaltexcoords[numdecals][1][1]=(placez-where.z)/(size)/2+.5;
1319 decalvertex[numdecals][1].x=vertex[Triangles[i].vertex[1]].x;
1320 decalvertex[numdecals][1].z=placez;
1321 decalvertex[numdecals][1].y=placex;
1324 placex=vertex[Triangles[i].vertex[2]].y;
1325 placez=vertex[Triangles[i].vertex[2]].z;
1327 decaltexcoords[numdecals][2][0]=(placex-where.y)/(size)/2+.5;
1328 decaltexcoords[numdecals][2][1]=(placez-where.z)/(size)/2+.5;
1330 decalvertex[numdecals][2].x=vertex[Triangles[i].vertex[2]].x;
1331 decalvertex[numdecals][2].z=placez;
1332 decalvertex[numdecals][2].y=placex;
1334 if(!(decaltexcoords[numdecals][0][0]<0&&decaltexcoords[numdecals][1][0]<0&&decaltexcoords[numdecals][2][0]<0))
1335 if(!(decaltexcoords[numdecals][0][1]<0&&decaltexcoords[numdecals][1][1]<0&&decaltexcoords[numdecals][2][1]<0))
1336 if(!(decaltexcoords[numdecals][0][0]>1&&decaltexcoords[numdecals][1][0]>1&&decaltexcoords[numdecals][2][0]>1))
1337 if(!(decaltexcoords[numdecals][0][1]>1&&decaltexcoords[numdecals][1][1]>1&&decaltexcoords[numdecals][2][1]>1))
1339 if(decalrotation[numdecals]){
1342 rot.x=decaltexcoords[numdecals][j][0]-.5;
1343 rot.z=decaltexcoords[numdecals][j][1]-.5;
1344 rot=DoRotation(rot,0,-decalrotation[numdecals],0);
1345 decaltexcoords[numdecals][j][0]=rot.x+.5;
1346 decaltexcoords[numdecals][j][1]=rot.z+.5;
1349 if(numdecals<max_model_decals-1)numdecals++;
1353 else if(distance<.02&&abs(facenormals[i].z)>abs(facenormals[i].y)&&abs(facenormals[i].z)>abs(facenormals[i].x)){
1354 decalposition[numdecals]=where;
1355 decaltype[numdecals]=atype;
1356 decalrotation[numdecals]=rotation;
1357 decalalivetime[numdecals]=0;
1358 decalopacity[numdecals]=opacity-distance/10;
1360 if(decalopacity[numdecals>0]){
1361 placex=vertex[Triangles[i].vertex[0]].x;
1362 placez=vertex[Triangles[i].vertex[0]].y;
1364 decaltexcoords[numdecals][0][0]=(placex-where.x)/(size)/2+.5;
1365 decaltexcoords[numdecals][0][1]=(placez-where.y)/(size)/2+.5;
1367 decalvertex[numdecals][0].x=placex;
1368 decalvertex[numdecals][0].z=vertex[Triangles[i].vertex[0]].z;
1369 decalvertex[numdecals][0].y=placez;
1372 placex=vertex[Triangles[i].vertex[1]].x;
1373 placez=vertex[Triangles[i].vertex[1]].y;
1375 decaltexcoords[numdecals][1][0]=(placex-where.x)/(size)/2+.5;
1376 decaltexcoords[numdecals][1][1]=(placez-where.y)/(size)/2+.5;
1378 decalvertex[numdecals][1].x=placex;
1379 decalvertex[numdecals][1].z=vertex[Triangles[i].vertex[1]].z;
1380 decalvertex[numdecals][1].y=placez;
1383 placex=vertex[Triangles[i].vertex[2]].x;
1384 placez=vertex[Triangles[i].vertex[2]].y;
1386 decaltexcoords[numdecals][2][0]=(placex-where.x)/(size)/2+.5;
1387 decaltexcoords[numdecals][2][1]=(placez-where.y)/(size)/2+.5;
1389 decalvertex[numdecals][2].x=placex;
1390 decalvertex[numdecals][2].z=vertex[Triangles[i].vertex[2]].z;
1391 decalvertex[numdecals][2].y=placez;
1393 if(!(decaltexcoords[numdecals][0][0]<0&&decaltexcoords[numdecals][1][0]<0&&decaltexcoords[numdecals][2][0]<0))
1394 if(!(decaltexcoords[numdecals][0][1]<0&&decaltexcoords[numdecals][1][1]<0&&decaltexcoords[numdecals][2][1]<0))
1395 if(!(decaltexcoords[numdecals][0][0]>1&&decaltexcoords[numdecals][1][0]>1&&decaltexcoords[numdecals][2][0]>1))
1396 if(!(decaltexcoords[numdecals][0][1]>1&&decaltexcoords[numdecals][1][1]>1&&decaltexcoords[numdecals][2][1]>1))
1398 if(decalrotation[numdecals]){
1401 rot.x=decaltexcoords[numdecals][j][0]-.5;
1402 rot.z=decaltexcoords[numdecals][j][1]-.5;
1403 rot=DoRotation(rot,0,-decalrotation[numdecals],0);
1404 decaltexcoords[numdecals][j][0]=rot.x+.5;
1405 decaltexcoords[numdecals][j][1]=rot.z+.5;
1408 if(numdecals<max_model_decals-1)numdecals++;
1420 if(textureptr) glDeleteTextures( 1, &textureptr );
1423 void Model::deallocate()
1427 if(owner)dealloc(owner);
1430 if(possible)dealloc(possible);
1433 if(vertex)dealloc(vertex);
1436 if(normals)dealloc(normals);
1439 if(facenormals)dealloc(facenormals);
1442 if(Triangles)dealloc(Triangles);
1445 if(vArray)dealloc(vArray);
1452 for(i=0;i<max_model_decals;i++)
1456 dealloc(decaltexcoords[i][j]);
1458 dealloc(decaltexcoords[i]);
1460 dealloc(decaltexcoords);
1467 for(i=0;i<max_model_decals;i++)
1469 dealloc(decalvertex[i]);
1471 dealloc(decalvertex);
1479 dealloc(decalopacity);
1482 dealloc(decalrotation);
1485 dealloc(decalalivetime);
1488 dealloc(decalposition);
1495 vertexNum = 0,TriangleNum = 0;
1498 type = 0,oldtype = 0;
1509 memset(&Texture, 0, sizeof(Texture));
1513 boundingspherecenter = 0;
1514 boundingsphereradius = 0;