]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/gtkradiant/singlepatches-radiant15/both-UTpicomodelase.diff
this never worked and the wiki works better anyway
[divverent/nexuiz.git] / misc / gtkradiant / singlepatches-radiant15 / both-UTpicomodelase.diff
1 Index: libs/picomodel/pm_ase.c
2 ===================================================================
3 --- libs/picomodel/pm_ase.c     (revision 191)
4 +++ libs/picomodel/pm_ase.c     (working copy)
5 @@ -32,6 +32,7 @@
6  
7  ----------------------------------------------------------------------------- */
8  
9 +void Sys_Printf (const char *format, ...);
10  
11  /* marker */
12  #define PM_ASE_C
13 @@ -253,7 +254,6 @@
14  struct aseVertex_s
15  {
16         picoVec3_t xyz;
17 -       picoVec3_t normal;
18         picoIndex_t id;
19  };
20  
21 @@ -276,6 +276,8 @@
22         picoIndex_t smoothingGroup;
23         picoIndex_t materialId;
24         picoIndex_t subMaterialId;
25 +   picoVec3_t  facenormal;
26 +   picoVec3_t  vertexnormal[3];
27  };
28  typedef aseFace_t* aseFacesIter_t;
29  
30 @@ -455,33 +457,157 @@
31  
32  #endif
33  
34 +static int VectorCompareExtn( picoVec3_t n1, picoVec3_t n2, float epsilon )
35 +{
36 +       int             i;
37 +       
38 +       
39 +       /* test */
40 +       for( i= 0; i < 3; i++ )
41 +               if( fabs( n1[ i ] - n2[ i ]) > epsilon )
42 +                       return -1;
43 +       return 1;
44 +}
45 +
46 +#define CrossProductTemp(a,b,c)                ((c)[0]=(a)[1]*(b)[2]-(a)[2]*(b)[1],(c)[1]=(a)[2]*(b)[0]-(a)[0]*(b)[2],(c)[2]=(a)[0]*(b)[1]-(a)[1]*(b)[0])
47 +
48  static void _ase_submit_triangles( picoModel_t* model , aseMaterial_t* materials , aseVertex_t* vertices, aseTexCoord_t* texcoords, aseColor_t* colors, aseFace_t* faces, int numFaces )
49  {
50 -       aseFacesIter_t i = faces, end = faces + numFaces;
51 -       for(; i != end; ++i)
52 +   
53 +   picoVec3_t accum;
54 +   int index;
55 +   int counter;
56 +   aseFacesIter_t i = faces, end = faces + numFaces;
57 +   counter=0;
58 +
59 +   //rebuild normals
60 +   for(i=faces; i != end; ++i)
61 +   {
62 +    
63 +      //&(*i).facenormal
64 +          //vec3_t v1, v2;
65 +          //VectorSubtract(va, vb, v1);
66 +          //VectorSubtract(vc, vb, v2);
67 +      //CrossProduct(v1, v2, out);
68 +      
69 +      picoVec3_t a,b,c;
70 +      picoVec3_t v1,v2,v3;
71 +      int j;
72 +      counter++;
73 +      for (j=0;j<3;j++)
74 +      {
75 +         a[j]    =  vertices[(*i).indices[0]].xyz[j];
76 +         b[j]    =  vertices[(*i).indices[1]].xyz[j];
77 +         c[j]    =  vertices[(*i).indices[2]].xyz[j];
78 +      }
79 +      for (j=0;j<3;j++)
80 +      {
81 +         v1[j]=a[j]-b[j];
82 +         v2[j]=c[j]-b[j];
83 +      }
84 +      
85 +      CrossProductTemp(v1,v2,v3);
86 +      _pico_normalize_vec(v3);
87 +      (*i).facenormal[0]=v3[0];
88 +      (*i).facenormal[1]=v3[1];
89 +      (*i).facenormal[2]=v3[2];
90 +      
91 +      
92 +   }
93 +   
94 +   
95 +   //if (counter>0) Sys_Printf( "Rebuilding %d Normals\n", counter * 3 );
96 +   for(i=faces; i != end; ++i)
97         {
98 -               /* look up the shader for the material/submaterial pair */
99 +      /* look up the shader for the material/submaterial pair */
100                 aseSubMaterial_t* subMtl = _ase_get_submaterial_or_default( materials, (*i).materialId, (*i).subMaterialId );
101 -               if( subMtl == NULL )
102 +
103 +      if( subMtl == NULL )
104                 {
105                         return;
106                 }
107  
108                 {
109                         picoVec3_t* xyz[3];
110 +         picoVec3_t *a[3];
111                         picoVec3_t* normal[3];
112                         picoVec2_t* st[3];
113                         picoColor_t* color[3];
114                         picoIndex_t smooth[3];
115 -                       int j;
116 -                       /* we pull the data from the vertex, color and texcoord arrays using the face index data */
117 -                       for ( j = 0 ; j < 3 ; j ++ )
118 +
119 +                       int j,z;
120 +
121 +         
122 +   
123 +         /* we pull the data from the vertex, color and texcoord arrays using the face index data */
124 +         for ( j = 0 ; j < 3 ; j ++ )
125                         {
126 -                               xyz[j]    = &vertices[(*i).indices[j]].xyz;
127 -                               normal[j] = &vertices[(*i).indices[j]].normal;
128 +            aseFacesIter_t q = faces;
129 +            aseFacesIter_t qend = faces + numFaces;
130 +
131 +            xyz[j]    = &vertices[(*i).indices[j]].xyz;
132 +            
133 +            // Use Face normal
134 +            normal[j] = &(*i).facenormal;
135 +             
136 +     
137 +            //Oooor we can use the smoothing group
138 +
139 +            //Slow method, but testing
140 +            //Find All faces that use this vertex, average their facenormals.
141 +            // skip where smoothgroups both equal 0, or don't have any shared bits (x & y)
142 +            index=(*i).indices[j];
143 +    
144 +//            accum[0]=0;
145 +  //          accum[1]=0;
146 +    //        accum[2]=0;
147 +            accum[0]=(*i).facenormal[0];
148 +            accum[1]=(*i).facenormal[1];
149 +            accum[2]=(*i).facenormal[2];
150 +            counter=1;
151 +           
152 +            
153 +            z=0;
154 +            for(; q != qend; ++q)
155 +            {
156 +               z++;
157 +               if (q==i)
158 +                  continue;
159 +               // if  ( (*q).indices[0]==index || (*q).indices[1]==index || (*q).indices[2]==index) 
160 +                a[0]=  &vertices[(*q).indices[0] ].xyz; 
161 +                a[1]=  &vertices[(*q).indices[1] ].xyz; 
162 +                a[2]=  &vertices[(*q).indices[2] ].xyz; 
163 +               
164 +               if ( VectorCompareExtn(*a[0],*xyz[j],0.01f)>0 ||
165 +                    VectorCompareExtn(*a[1],*xyz[j],0.01f)>0 ||
166 +                    VectorCompareExtn(*a[2],*xyz[j],0.01f)>0
167 +                  )
168 +               {
169 +                  if ( (*i).smoothingGroup==0 && (*q).smoothingGroup ==0 )
170 +                     continue;
171 +
172 +                  if ( (*i).smoothingGroup & (*q).smoothingGroup  )
173 +                  {
174 +                     accum[0]+=(*q).facenormal[0];
175 +                     accum[1]+=(*q).facenormal[1];
176 +                     accum[2]+=(*q).facenormal[2];
177 +                     
178 +                     counter++;
179 +             
180 +                  }
181 +               }
182 +            } 
183 +            _pico_normalize_vec(accum); 
184 +
185 +            (*i).vertexnormal[j][0]=accum[0];
186 +            (*i).vertexnormal[j][1]=accum[1];
187 +            (*i).vertexnormal[j][2]=accum[2];
188 +            normal[j]=&(*i).vertexnormal[j]; 
189 +                        
190 +
191                                 st[j]     = &texcoords[(*i).indices[j + 3]].texcoord;
192 -                       
193 -                               if( colors != NULL && (*i).indices[j + 6] >= 0 )
194 +                             
195 +               if( colors != NULL && (*i).indices[j + 6] >= 0 )
196                                 {
197                                         color[j] = &colors[(*i).indices[j + 6]].color;
198                                 }
199 @@ -490,30 +616,18 @@
200                                         color[j] = &white;
201                                 }
202  
203 -                               smooth[j] = (vertices[(*i).indices[j]].id * (1 << 16)) + (*i).smoothingGroup; /* don't merge vertices */
204 +                               smooth[j] = 0;//  (vertices[(*i).indices[j]].id * (1 << 16)) + (*i).smoothingGroup; /* don't merge vertices */
205                                 
206                         }
207  
208                         /* submit the triangle to the model */
209                         PicoAddTriangleToModel ( model , xyz , normal , 1 , st , 1 , color , subMtl->shader, smooth );
210                 }
211 +
212         }
213  }
214  
215 -static void shadername_convert(char* shaderName)
216 -{
217 -  /* unix-style path separators */
218 -  char* s = shaderName;
219 -  for(; *s != '\0'; ++s)
220 -  {
221 -    if(*s == '\\')
222 -    {
223 -      *s = '/';
224 -    }
225 -  }
226 -}
227  
228 -
229  /* _ase_load:
230   *  loads a 3dsmax ase model file.
231  */
232 @@ -534,6 +648,9 @@
233         int numColorVertices = 0;
234         int numColorVertexFaces = 0;
235         int vertexId = 0;
236 +   int currentVertexFace=0;
237 +   int currentVertexIndex=0;
238 +   int counter=0;
239  
240         aseMaterial_t* materials = NULL;
241  
242 @@ -610,10 +727,11 @@
243                 }
244                 else if (!_pico_stricmp(p->token,"*mesh_numvertex"))
245                 {
246 -                       if (!_pico_parse_int( p, &numVertices) )
247 +                       if (!_pico_parse_int( p, &numVertices) )
248                                 _ase_error_return("Missing MESH_NUMVERTEX value");
249  
250                         vertices = _pico_calloc(numVertices, sizeof(aseVertex_t));
251 +         currentVertexIndex=0;   
252                 }
253                 else if (!_pico_stricmp(p->token,"*mesh_numfaces"))
254                 {
255 @@ -621,6 +739,7 @@
256                                 _ase_error_return("Missing MESH_NUMFACES value");
257  
258                         faces = _pico_calloc(numFaces, sizeof(aseFace_t));
259 +
260                 }
261                 else if (!_pico_stricmp(p->token,"*mesh_numtvertex"))
262                 {
263 @@ -685,7 +804,20 @@
264  
265                         vertices[index].id = vertexId++;
266                 }
267 -               /* model mesh vertex normal */
268 +               else if (!_pico_stricmp(p->token,"*mesh_facenormal"))
269 +               {
270 +                  //Grab the faceindex for the next vertex normals.
271 +         if( numVertices == 0 )
272 +                               _ase_error_return("Vertex parse error (facenormals)");
273 +
274 +         if (!_pico_parse_int( p,&currentVertexFace ))
275 +                               _ase_error_return("Vertex parse error");
276 +
277 +                       if (!_pico_parse_vec( p,faces[currentVertexFace].facenormal ))
278 +                               _ase_error_return("Vertex parse error");
279 +
280 +      }
281 +      /* model mesh vertex normal */
282                 else if (!_pico_stricmp(p->token,"*mesh_vertexnormal"))
283                 {
284                         int                     index;
285 @@ -696,10 +828,25 @@
286                         /* get vertex data (orig: index +y -x +z) */
287                         if (!_pico_parse_int( p,&index ))
288                                 _ase_error_return("Vertex parse error");
289 -                       if (!_pico_parse_vec( p,vertices[index].normal ))
290 +
291 +         //^^ Index is 'wrong' in .ase models.  they reference the same vert index with multiple normals..
292 +         // I've tried, this is a lost cause.  Use the SG's
293 +         // 
294 +                       /*
295 +         
296 +         if (!_pico_parse_vec( p,vertices[counter].normal ))
297                                 _ase_error_return("Vertex parse error");
298 +         vertices[counter].faceid=index;
299 +         counter++;
300 +         */
301                 }
302                 /* model mesh face */
303 +               else if (!_pico_stricmp(p->token,"*mesh_normals"))
304 +      {
305 +      //   counter=0; //part of the above vertex normals fix
306 +      }
307 +         
308 +      /* model mesh face */
309                 else if (!_pico_stricmp(p->token,"*mesh_face"))
310                 {
311                         picoIndex_t indexes[3];
312 @@ -736,8 +883,35 @@
313                                 }
314                                 if (!_pico_stricmp (p->token,"*MESH_SMOOTHING" ))
315                                 {
316 -                                       _pico_parse_int ( p , &faces[index].smoothingGroup );
317 -                               }
318 +               int total=0;
319 +               char* point;
320 +               char* start;
321 +               _pico_parse(p,0);
322 +
323 +               point=p->token;
324 +               start=point;
325 +               faces[index].smoothingGroup=0;
326 +              
327 +               //Super dodgy comma delimited string parse
328 +               while (*point<'A') 
329 +               {
330 +                  if (*point<=32 || *point==',')
331 +                  {
332 +                     total=atoi(start);
333 +                     if (total!=0)
334 +                     {
335 +                        faces[index].smoothingGroup+=1<<total;
336 +                     }
337 +                     start=point+1;
338 +                  }
339 +                  
340 +                  point++;
341 +               }
342 +               
343 +               
344 +                             
345 +               
346 +            }
347                                 if (!_pico_stricmp (p->token,"*MESH_MTLID" ))
348                                 {
349                                         _pico_parse_int ( p , &faces[index].subMaterialId );
350 @@ -755,19 +929,19 @@
351                         int                     index;
352  
353                         if( numVertices == 0 )
354 -                               _ase_error_return("Texture Vertex parse error");
355 +                               _ase_error_return("Vertex parse error");
356  
357                         /* get uv vertex index */
358 -                       if (!_pico_parse_int( p,&index ) || index >= numTextureVertices)
359 -                               _ase_error_return("Texture vertex parse error");
360 +                       if (!_pico_parse_int( p,&index ))
361 +                               _ase_error_return("UV vertex parse error");
362  
363                         /* get uv vertex s */
364                         if (!_pico_parse_float( p,&texcoords[index].texcoord[0] ))
365 -                               _ase_error_return("Texture vertex parse error");
366 +                               _ase_error_return("UV vertex parse error");
367  
368                         /* get uv vertex t */
369                         if (!_pico_parse_float( p,&texcoords[index].texcoord[1] ))
370 -                               _ase_error_return("Texture vertex parse error");
371 +                               _ase_error_return("UV vertex parse error");
372                         
373                         /* ydnar: invert t */
374                         texcoords[index].texcoord[ 1 ] = 1.0f - texcoords[index].texcoord[ 1 ];
375 @@ -831,6 +1005,13 @@
376                         
377                         /* leave alpha alone since we don't get any data from the ASE format */
378                         colors[index].color[3] = 255;
379 +
380 +         /* 27 hack, red as alpha */
381 +         colors[index].color[3]=colors[index].color[0];
382 +         colors[index].color[0]=255;
383 +         colors[index].color[1]=255;
384 +         colors[index].color[2]=255;
385 +
386                 }
387                 /* model color face */
388                 else if (!_pico_stricmp(p->token,"*mesh_cface"))
389 @@ -900,7 +1081,6 @@
390                                 {
391                                         /* set material name */
392                                         _pico_first_token( materialName );
393 -          shadername_convert(materialName);
394                                         PicoSetShaderName( shader, materialName);
395  
396                                         /* set shader's transparency */
397 @@ -1085,7 +1265,6 @@
398                                 }
399  
400                                 /* set material name */
401 -        shadername_convert(materialName);
402                                 PicoSetShaderName( shader,materialName );
403  
404                                 /* set shader's transparency */
405 @@ -1115,8 +1294,18 @@
406            char* p = mapname;
407  
408            /* convert to shader-name format */
409 -          shadername_convert(mapname);
410            {
411 +            /* unix-style path separators */
412 +            char* s = mapname;
413 +            for(; *s != '\0'; ++s)
414 +            {
415 +              if(*s == '\\')
416 +              {
417 +                *s = '/';
418 +              }
419 +            }
420 +          }
421 +          {
422              /* remove extension */
423              char* last_period = strrchr(p, '.');
424              if(last_period != NULL)
425 @@ -1125,14 +1314,32 @@
426              }
427            }
428  
429 -          /* find shader path */
430 +          /* find game root */
431            for(; *p != '\0'; ++p)
432            {
433 -            if(_pico_strnicmp(p, "models/", 7) == 0 || _pico_strnicmp(p, "textures/", 9) == 0)
434 +            if(_pico_strnicmp(p, "quake", 5) == 0 || _pico_strnicmp(p, "doom", 4) == 0)
435              {
436                break;
437              }
438            }
439 +          /* root-relative */
440 +          for(; *p != '\0'; ++p)
441 +          {
442 +            if(*p == '/')
443 +            {
444 +              ++p;
445 +              break;
446 +            }
447 +          }
448 +          /* game-relative */
449 +          for(; *p != '\0'; ++p)
450 +          {
451 +            if(*p == '/')
452 +            {
453 +              ++p;
454 +              break;
455 +            }
456 +          }
457  
458            if(*p != '\0')
459            {