]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.c
Remove Mod_CreateCollisionMesh and reduce the exposed API of Mod_ShadowMesh, it reall...
[xonotic/darkplaces.git] / model_shared.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // models.c -- model loading and caching
21
22 // models are the only shared resource between a client and server running
23 // on the same machine.
24
25 #include "quakedef.h"
26 #include "image.h"
27 #include "r_shadow.h"
28 #include "polygon.h"
29
30 cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "0", "mipmaps model skins so they render faster in the distance and do not display noise artifacts, can cause discoloration of skins if they contain undesirable border colors"};
31 cvar_t r_mipnormalmaps = {CVAR_SAVE, "r_mipnormalmaps", "1", "mipmaps normalmaps (turning it off looks sharper but may have aliasing)"};
32 cvar_t mod_generatelightmaps_unitspersample = {CVAR_SAVE, "mod_generatelightmaps_unitspersample", "8", "lightmap resolution"};
33 cvar_t mod_generatelightmaps_borderpixels = {CVAR_SAVE, "mod_generatelightmaps_borderpixels", "2", "extra space around polygons to prevent sampling artifacts"};
34 cvar_t mod_generatelightmaps_texturesize = {CVAR_SAVE, "mod_generatelightmaps_texturesize", "1024", "size of lightmap textures"};
35 cvar_t mod_generatelightmaps_lightmapsamples = {CVAR_SAVE, "mod_generatelightmaps_lightmapsamples", "16", "number of shadow tests done per lightmap pixel"};
36 cvar_t mod_generatelightmaps_vertexsamples = {CVAR_SAVE, "mod_generatelightmaps_vertexsamples", "16", "number of shadow tests done per vertex"};
37 cvar_t mod_generatelightmaps_gridsamples = {CVAR_SAVE, "mod_generatelightmaps_gridsamples", "64", "number of shadow tests done per lightgrid cell"};
38 cvar_t mod_generatelightmaps_lightmapradius = {CVAR_SAVE, "mod_generatelightmaps_lightmapradius", "16", "sampling area around each lightmap pixel"};
39 cvar_t mod_generatelightmaps_vertexradius = {CVAR_SAVE, "mod_generatelightmaps_vertexradius", "16", "sampling area around each vertex"};
40 cvar_t mod_generatelightmaps_gridradius = {CVAR_SAVE, "mod_generatelightmaps_gridradius", "64", "sampling area around each lightgrid cell center"};
41
42 dp_model_t *loadmodel;
43
44 static mempool_t *mod_mempool;
45 static memexpandablearray_t models;
46
47 static mempool_t* q3shaders_mem;
48 typedef struct q3shader_hash_entry_s
49 {
50   q3shaderinfo_t shader;
51   struct q3shader_hash_entry_s* chain;
52 } q3shader_hash_entry_t;
53 #define Q3SHADER_HASH_SIZE  1021
54 typedef struct q3shader_data_s
55 {
56   memexpandablearray_t hash_entries;
57   q3shader_hash_entry_t hash[Q3SHADER_HASH_SIZE];
58   memexpandablearray_t char_ptrs;
59 } q3shader_data_t;
60 static q3shader_data_t* q3shader_data;
61
62 static void mod_start(void)
63 {
64         int i, count;
65         int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
66         dp_model_t *mod;
67
68         SCR_PushLoadingScreen(false, "Loading models", 1.0);
69         count = 0;
70         for (i = 0;i < nummodels;i++)
71                 if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
72                         if (mod->used)
73                                 ++count;
74         for (i = 0;i < nummodels;i++)
75                 if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
76                         if (mod->used)
77                         {
78                                 SCR_PushLoadingScreen(true, mod->name, 1.0 / count);
79                                 Mod_LoadModel(mod, true, false);
80                                 SCR_PopLoadingScreen(false);
81                         }
82         SCR_PopLoadingScreen(false);
83 }
84
85 static void mod_shutdown(void)
86 {
87         int i;
88         int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
89         dp_model_t *mod;
90
91         for (i = 0;i < nummodels;i++)
92                 if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && (mod->loaded || mod->mempool))
93                         Mod_UnloadModel(mod);
94
95         Mod_FreeQ3Shaders();
96         Mod_Skeletal_FreeBuffers();
97 }
98
99 static void mod_newmap(void)
100 {
101         msurface_t *surface;
102         int i, j, k, l, surfacenum, ssize, tsize;
103         int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
104         dp_model_t *mod;
105
106         for (i = 0;i < nummodels;i++)
107         {
108                 if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool)
109                 {
110                         for (j = 0;j < mod->num_textures && mod->data_textures;j++)
111                         {
112                                 // note that materialshaderpass and backgroundshaderpass point to shaderpasses[] and so do the pre/post shader ranges, so this catches all of them...
113                                 for (l = 0; l < Q3SHADER_MAXLAYERS; l++)
114                                         if (mod->data_textures[j].shaderpasses[l])
115                                                 for (k = 0; k < mod->data_textures[j].shaderpasses[l]->numframes; k++)
116                                                         R_SkinFrame_MarkUsed(mod->data_textures[j].shaderpasses[l]->skinframes[k]);
117                         }
118                         if (mod->brush.solidskyskinframe)
119                                 R_SkinFrame_MarkUsed(mod->brush.solidskyskinframe);
120                         if (mod->brush.alphaskyskinframe)
121                                 R_SkinFrame_MarkUsed(mod->brush.alphaskyskinframe);
122                 }
123         }
124
125         if (!cl_stainmaps_clearonload.integer)
126                 return;
127
128         for (i = 0;i < nummodels;i++)
129         {
130                 if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool && mod->data_surfaces)
131                 {
132                         for (surfacenum = 0, surface = mod->data_surfaces;surfacenum < mod->num_surfaces;surfacenum++, surface++)
133                         {
134                                 if (surface->lightmapinfo && surface->lightmapinfo->stainsamples)
135                                 {
136                                         ssize = (surface->lightmapinfo->extents[0] >> 4) + 1;
137                                         tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
138                                         memset(surface->lightmapinfo->stainsamples, 255, ssize * tsize * 3);
139                                         mod->brushq1.lightmapupdateflags[surfacenum] = true;
140                                 }
141                         }
142                 }
143         }
144 }
145
146 /*
147 ===============
148 Mod_Init
149 ===============
150 */
151 static void Mod_Print(void);
152 static void Mod_Precache (void);
153 static void Mod_Decompile_f(void);
154 static void Mod_GenerateLightmaps_f(void);
155 void Mod_Init (void)
156 {
157         mod_mempool = Mem_AllocPool("modelinfo", 0, NULL);
158         Mem_ExpandableArray_NewArray(&models, mod_mempool, sizeof(dp_model_t), 16);
159
160         Mod_BrushInit();
161         Mod_AliasInit();
162         Mod_SpriteInit();
163
164         Cvar_RegisterVariable(&r_mipskins);
165         Cvar_RegisterVariable(&r_mipnormalmaps);
166         Cvar_RegisterVariable(&mod_generatelightmaps_unitspersample);
167         Cvar_RegisterVariable(&mod_generatelightmaps_borderpixels);
168         Cvar_RegisterVariable(&mod_generatelightmaps_texturesize);
169
170         Cvar_RegisterVariable(&mod_generatelightmaps_lightmapsamples);
171         Cvar_RegisterVariable(&mod_generatelightmaps_vertexsamples);
172         Cvar_RegisterVariable(&mod_generatelightmaps_gridsamples);
173         Cvar_RegisterVariable(&mod_generatelightmaps_lightmapradius);
174         Cvar_RegisterVariable(&mod_generatelightmaps_vertexradius);
175         Cvar_RegisterVariable(&mod_generatelightmaps_gridradius);
176
177         Cmd_AddCommand ("modellist", Mod_Print, "prints a list of loaded models");
178         Cmd_AddCommand ("modelprecache", Mod_Precache, "load a model");
179         Cmd_AddCommand ("modeldecompile", Mod_Decompile_f, "exports a model in several formats for editing purposes");
180         Cmd_AddCommand ("mod_generatelightmaps", Mod_GenerateLightmaps_f, "rebuilds lighting on current worldmodel");
181 }
182
183 void Mod_RenderInit(void)
184 {
185         R_RegisterModule("Models", mod_start, mod_shutdown, mod_newmap, NULL, NULL);
186 }
187
188 void Mod_UnloadModel (dp_model_t *mod)
189 {
190         char name[MAX_QPATH];
191         qboolean used;
192         dp_model_t *parentmodel;
193
194         if (developer_loading.integer)
195                 Con_Printf("unloading model %s\n", mod->name);
196
197         strlcpy(name, mod->name, sizeof(name));
198         parentmodel = mod->brush.parentmodel;
199         used = mod->used;
200         if (mod->mempool)
201         {
202                 if (mod->surfmesh.data_element3i_indexbuffer)
203                         R_Mesh_DestroyMeshBuffer(mod->surfmesh.data_element3i_indexbuffer);
204                 mod->surfmesh.data_element3i_indexbuffer = NULL;
205                 if (mod->surfmesh.data_element3s_indexbuffer)
206                         R_Mesh_DestroyMeshBuffer(mod->surfmesh.data_element3s_indexbuffer);
207                 mod->surfmesh.data_element3s_indexbuffer = NULL;
208                 if (mod->surfmesh.vbo_vertexbuffer)
209                         R_Mesh_DestroyMeshBuffer(mod->surfmesh.vbo_vertexbuffer);
210                 mod->surfmesh.vbo_vertexbuffer = NULL;
211         }
212         // free textures/memory attached to the model
213         R_FreeTexturePool(&mod->texturepool);
214         Mem_FreePool(&mod->mempool);
215         // clear the struct to make it available
216         memset(mod, 0, sizeof(dp_model_t));
217         // restore the fields we want to preserve
218         strlcpy(mod->name, name, sizeof(mod->name));
219         mod->brush.parentmodel = parentmodel;
220         mod->used = used;
221         mod->loaded = false;
222 }
223
224 static void R_Model_Null_Draw(entity_render_t *ent)
225 {
226         return;
227 }
228
229
230 typedef void (*mod_framegroupify_parsegroups_t) (unsigned int i, int start, int len, float fps, qboolean loop, const char *name, void *pass);
231
232 static int Mod_FrameGroupify_ParseGroups(const char *buf, mod_framegroupify_parsegroups_t cb, void *pass)
233 {
234         const char *bufptr;
235         int start, len;
236         float fps;
237         unsigned int i;
238         qboolean loop;
239         char name[64];
240
241         bufptr = buf;
242         i = 0;
243         while(bufptr)
244         {
245                 // an anim scene!
246
247                 // REQUIRED: fetch start
248                 COM_ParseToken_Simple(&bufptr, true, false, true);
249                 if (!bufptr)
250                         break; // end of file
251                 if (!strcmp(com_token, "\n"))
252                         continue; // empty line
253                 start = atoi(com_token);
254
255                 // REQUIRED: fetch length
256                 COM_ParseToken_Simple(&bufptr, true, false, true);
257                 if (!bufptr || !strcmp(com_token, "\n"))
258                 {
259                         Con_Printf("framegroups file: missing number of frames\n");
260                         continue;
261                 }
262                 len = atoi(com_token);
263
264                 // OPTIONAL args start
265                 COM_ParseToken_Simple(&bufptr, true, false, true);
266
267                 // OPTIONAL: fetch fps
268                 fps = 20;
269                 if (bufptr && strcmp(com_token, "\n"))
270                 {
271                         fps = atof(com_token);
272                         COM_ParseToken_Simple(&bufptr, true, false, true);
273                 }
274
275                 // OPTIONAL: fetch loopflag
276                 loop = true;
277                 if (bufptr && strcmp(com_token, "\n"))
278                 {
279                         loop = (atoi(com_token) != 0);
280                         COM_ParseToken_Simple(&bufptr, true, false, true);
281                 }
282
283                 // OPTIONAL: fetch name
284                 name[0] = 0;
285                 if (bufptr && strcmp(com_token, "\n"))
286                 {
287                         strlcpy(name, com_token, sizeof(name));
288                         COM_ParseToken_Simple(&bufptr, true, false, true);
289                 }
290
291                 // OPTIONAL: remaining unsupported tokens (eat them)
292                 while (bufptr && strcmp(com_token, "\n"))
293                         COM_ParseToken_Simple(&bufptr, true, false, true);
294
295                 //Con_Printf("data: %d %d %d %f %d (%s)\n", i, start, len, fps, loop, name);
296
297                 if(cb)
298                         cb(i, start, len, fps, loop, (name[0] ? name : NULL), pass);
299                 ++i;
300         }
301
302         return i;
303 }
304
305 static void Mod_FrameGroupify_ParseGroups_Store (unsigned int i, int start, int len, float fps, qboolean loop, const char *name, void *pass)
306 {
307         dp_model_t *mod = (dp_model_t *) pass;
308         animscene_t *anim = &mod->animscenes[i];
309         if(name)
310                 strlcpy(anim->name, name, sizeof(anim[i].name));
311         else
312                 dpsnprintf(anim->name, sizeof(anim[i].name), "groupified_%d_anim", i);
313         anim->firstframe = bound(0, start, mod->num_poses - 1);
314         anim->framecount = bound(1, len, mod->num_poses - anim->firstframe);
315         anim->framerate = max(1, fps);
316         anim->loop = !!loop;
317         //Con_Printf("frame group %d is %d %d %f %d\n", i, start, len, fps, loop);
318 }
319
320 static void Mod_FrameGroupify(dp_model_t *mod, const char *buf)
321 {
322         unsigned int cnt;
323
324         // 0. count
325         cnt = Mod_FrameGroupify_ParseGroups(buf, NULL, NULL);
326         if(!cnt)
327         {
328                 Con_Printf("no scene found in framegroups file, aborting\n");
329                 return;
330         }
331         mod->numframes = cnt;
332
333         // 1. reallocate
334         // (we do not free the previous animscenes, but model unloading will free the pool owning them, so it's okay)
335         mod->animscenes = (animscene_t *) Mem_Alloc(mod->mempool, sizeof(animscene_t) * mod->numframes);
336
337         // 2. parse
338         Mod_FrameGroupify_ParseGroups(buf, Mod_FrameGroupify_ParseGroups_Store, mod);
339 }
340
341 static void Mod_FindPotentialDeforms(dp_model_t *mod)
342 {
343         int i, j;
344         texture_t *texture;
345         mod->wantnormals = false;
346         mod->wanttangents = false;
347         for (i = 0;i < mod->num_textures;i++)
348         {
349                 texture = mod->data_textures + i;
350                 if (texture->materialshaderpass && texture->materialshaderpass->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
351                         mod->wantnormals = true;
352                 if (texture->materialshaderpass && texture->materialshaderpass->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
353                         mod->wantnormals = true;
354                 for (j = 0;j < Q3MAXDEFORMS;j++)
355                 {
356                         if (texture->deforms[j].deform == Q3DEFORM_AUTOSPRITE)
357                         {
358                                 mod->wanttangents = true;
359                                 mod->wantnormals = true;
360                                 break;
361                         }
362                         if (texture->deforms[j].deform != Q3DEFORM_NONE)
363                                 mod->wantnormals = true;
364                 }
365         }
366 }
367
368 /*
369 ==================
370 Mod_LoadModel
371
372 Loads a model
373 ==================
374 */
375 dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk)
376 {
377         int num;
378         unsigned int crc;
379         void *buf;
380         fs_offset_t filesize = 0;
381         char vabuf[1024];
382
383         mod->used = true;
384
385         if (mod->name[0] == '*') // submodel
386                 return mod;
387         
388         if (!strcmp(mod->name, "null"))
389         {
390                 if(mod->loaded)
391                         return mod;
392
393                 if (mod->loaded || mod->mempool)
394                         Mod_UnloadModel(mod);
395
396                 if (developer_loading.integer)
397                         Con_Printf("loading model %s\n", mod->name);
398
399                 mod->used = true;
400                 mod->crc = (unsigned int)-1;
401                 mod->loaded = false;
402
403                 VectorClear(mod->normalmins);
404                 VectorClear(mod->normalmaxs);
405                 VectorClear(mod->yawmins);
406                 VectorClear(mod->yawmaxs);
407                 VectorClear(mod->rotatedmins);
408                 VectorClear(mod->rotatedmaxs);
409
410                 mod->modeldatatypestring = "null";
411                 mod->type = mod_null;
412                 mod->Draw = R_Model_Null_Draw;
413                 mod->numframes = 2;
414                 mod->numskins = 1;
415
416                 // no fatal errors occurred, so this model is ready to use.
417                 mod->loaded = true;
418
419                 return mod;
420         }
421
422         crc = 0;
423         buf = NULL;
424
425         // even if the model is loaded it still may need reloading...
426
427         // if it is not loaded or checkdisk is true we need to calculate the crc
428         if (!mod->loaded || checkdisk)
429         {
430                 if (checkdisk && mod->loaded)
431                         Con_DPrintf("checking model %s\n", mod->name);
432                 buf = FS_LoadFile (mod->name, tempmempool, false, &filesize);
433                 if (buf)
434                 {
435                         crc = CRC_Block((unsigned char *)buf, filesize);
436                         // we need to reload the model if the crc does not match
437                         if (mod->crc != crc)
438                                 mod->loaded = false;
439                 }
440         }
441
442         // if the model is already loaded and checks passed, just return
443         if (mod->loaded)
444         {
445                 if (buf)
446                         Mem_Free(buf);
447                 return mod;
448         }
449
450         if (developer_loading.integer)
451                 Con_Printf("loading model %s\n", mod->name);
452         
453         SCR_PushLoadingScreen(true, mod->name, 1);
454
455         // LordHavoc: unload the existing model in this slot (if there is one)
456         if (mod->loaded || mod->mempool)
457                 Mod_UnloadModel(mod);
458
459         // load the model
460         mod->used = true;
461         mod->crc = crc;
462         // errors can prevent the corresponding mod->loaded = true;
463         mod->loaded = false;
464
465         // default lightmap scale
466         mod->lightmapscale = 1;
467
468         // default model radius and bounding box (mainly for missing models)
469         mod->radius = 16;
470         VectorSet(mod->normalmins, -mod->radius, -mod->radius, -mod->radius);
471         VectorSet(mod->normalmaxs, mod->radius, mod->radius, mod->radius);
472         VectorSet(mod->yawmins, -mod->radius, -mod->radius, -mod->radius);
473         VectorSet(mod->yawmaxs, mod->radius, mod->radius, mod->radius);
474         VectorSet(mod->rotatedmins, -mod->radius, -mod->radius, -mod->radius);
475         VectorSet(mod->rotatedmaxs, mod->radius, mod->radius, mod->radius);
476
477         if (!q3shaders_mem)
478         {
479                 // load q3 shaders for the first time, or after a level change
480                 Mod_LoadQ3Shaders();
481         }
482
483         if (buf)
484         {
485                 char *bufend = (char *)buf + filesize;
486
487                 // all models use memory, so allocate a memory pool
488                 mod->mempool = Mem_AllocPool(mod->name, 0, NULL);
489
490                 num = LittleLong(*((int *)buf));
491                 // call the apropriate loader
492                 loadmodel = mod;
493                 if (!strcasecmp(FS_FileExtension(mod->name), "obj")) Mod_OBJ_Load(mod, buf, bufend);
494                 else if (!memcmp(buf, "IDPO", 4)) Mod_IDP0_Load(mod, buf, bufend);
495                 else if (!memcmp(buf, "IDP2", 4)) Mod_IDP2_Load(mod, buf, bufend);
496                 else if (!memcmp(buf, "IDP3", 4)) Mod_IDP3_Load(mod, buf, bufend);
497                 else if (!memcmp(buf, "IDSP", 4)) Mod_IDSP_Load(mod, buf, bufend);
498                 else if (!memcmp(buf, "IDS2", 4)) Mod_IDS2_Load(mod, buf, bufend);
499                 else if (!memcmp(buf, "IBSP", 4)) Mod_IBSP_Load(mod, buf, bufend);
500                 else if (!memcmp(buf, "ZYMOTICMODEL", 12)) Mod_ZYMOTICMODEL_Load(mod, buf, bufend);
501                 else if (!memcmp(buf, "DARKPLACESMODEL", 16)) Mod_DARKPLACESMODEL_Load(mod, buf, bufend);
502                 else if (!memcmp(buf, "ACTRHEAD", 8)) Mod_PSKMODEL_Load(mod, buf, bufend);
503                 else if (!memcmp(buf, "INTERQUAKEMODEL", 16)) Mod_INTERQUAKEMODEL_Load(mod, buf, bufend);
504                 else if (strlen(mod->name) >= 4 && !strcmp(mod->name + strlen(mod->name) - 4, ".map")) Mod_MAP_Load(mod, buf, bufend);
505                 else if (num == BSPVERSION || num == 30 || !memcmp(buf, "BSP2", 4) || !memcmp(buf, "2PSB", 4)) Mod_Q1BSP_Load(mod, buf, bufend);
506                 else Con_Printf("Mod_LoadModel: model \"%s\" is of unknown/unsupported type\n", mod->name);
507                 Mem_Free(buf);
508
509                 Mod_FindPotentialDeforms(mod);
510
511                 buf = FS_LoadFile(va(vabuf, sizeof(vabuf), "%s.framegroups", mod->name), tempmempool, false, &filesize);
512                 if(buf)
513                 {
514                         Mod_FrameGroupify(mod, (const char *)buf);
515                         Mem_Free(buf);
516                 }
517
518                 Mod_BuildVBOs();
519         }
520         else if (crash)
521         {
522                 // LordHavoc: Sys_Error was *ANNOYING*
523                 Con_Printf ("Mod_LoadModel: %s not found\n", mod->name);
524         }
525
526         // no fatal errors occurred, so this model is ready to use.
527         mod->loaded = true;
528
529         SCR_PopLoadingScreen(false);
530
531         return mod;
532 }
533
534 void Mod_ClearUsed(void)
535 {
536         int i;
537         int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
538         dp_model_t *mod;
539         for (i = 0;i < nummodels;i++)
540                 if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0])
541                         mod->used = false;
542 }
543
544 void Mod_PurgeUnused(void)
545 {
546         int i;
547         int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
548         dp_model_t *mod;
549         for (i = 0;i < nummodels;i++)
550         {
551                 if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !mod->used)
552                 {
553                         Mod_UnloadModel(mod);
554                         Mem_ExpandableArray_FreeRecord(&models, mod);
555                 }
556         }
557 }
558
559 /*
560 ==================
561 Mod_FindName
562
563 ==================
564 */
565 dp_model_t *Mod_FindName(const char *name, const char *parentname)
566 {
567         int i;
568         int nummodels;
569         dp_model_t *mod;
570
571         if (!parentname)
572                 parentname = "";
573
574         // if we're not dedicatd, the renderer calls will crash without video
575         Host_StartVideo();
576
577         nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
578
579         if (!name[0])
580                 Host_Error ("Mod_ForName: empty name");
581
582         // search the currently loaded models
583         for (i = 0;i < nummodels;i++)
584         {
585                 if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !strcmp(mod->name, name) && ((!mod->brush.parentmodel && !parentname[0]) || (mod->brush.parentmodel && parentname[0] && !strcmp(mod->brush.parentmodel->name, parentname))))
586                 {
587                         mod->used = true;
588                         return mod;
589                 }
590         }
591
592         // no match found, create a new one
593         mod = (dp_model_t *) Mem_ExpandableArray_AllocRecord(&models);
594         strlcpy(mod->name, name, sizeof(mod->name));
595         if (parentname[0])
596                 mod->brush.parentmodel = Mod_FindName(parentname, NULL);
597         else
598                 mod->brush.parentmodel = NULL;
599         mod->loaded = false;
600         mod->used = true;
601         return mod;
602 }
603
604 /*
605 ==================
606 Mod_ForName
607
608 Loads in a model for the given name
609 ==================
610 */
611 dp_model_t *Mod_ForName(const char *name, qboolean crash, qboolean checkdisk, const char *parentname)
612 {
613         dp_model_t *model;
614         model = Mod_FindName(name, parentname);
615         if (!model->loaded || checkdisk)
616                 Mod_LoadModel(model, crash, checkdisk);
617         return model;
618 }
619
620 /*
621 ==================
622 Mod_Reload
623
624 Reloads all models if they have changed
625 ==================
626 */
627 void Mod_Reload(void)
628 {
629         int i, count;
630         int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
631         dp_model_t *mod;
632
633         SCR_PushLoadingScreen(false, "Reloading models", 1.0);
634         count = 0;
635         for (i = 0;i < nummodels;i++)
636                 if ((mod = (dp_model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*' && mod->used)
637                         ++count;
638         for (i = 0;i < nummodels;i++)
639                 if ((mod = (dp_model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*' && mod->used)
640                 {
641                         SCR_PushLoadingScreen(true, mod->name, 1.0 / count);
642                         Mod_LoadModel(mod, true, true);
643                         SCR_PopLoadingScreen(false);
644                 }
645         SCR_PopLoadingScreen(false);
646 }
647
648 unsigned char *mod_base;
649
650
651 //=============================================================================
652
653 /*
654 ================
655 Mod_Print
656 ================
657 */
658 static void Mod_Print(void)
659 {
660         int i;
661         int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
662         dp_model_t *mod;
663
664         Con_Print("Loaded models:\n");
665         for (i = 0;i < nummodels;i++)
666         {
667                 if ((mod = (dp_model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
668                 {
669                         if (mod->brush.numsubmodels)
670                                 Con_Printf("%4iK %s (%i submodels)\n", mod->mempool ? (int)((mod->mempool->totalsize + 1023) / 1024) : 0, mod->name, mod->brush.numsubmodels);
671                         else
672                                 Con_Printf("%4iK %s\n", mod->mempool ? (int)((mod->mempool->totalsize + 1023) / 1024) : 0, mod->name);
673                 }
674         }
675 }
676
677 /*
678 ================
679 Mod_Precache
680 ================
681 */
682 static void Mod_Precache(void)
683 {
684         if (Cmd_Argc() == 2)
685                 Mod_ForName(Cmd_Argv(1), false, true, Cmd_Argv(1)[0] == '*' ? cl.model_name[1] : NULL);
686         else
687                 Con_Print("usage: modelprecache <filename>\n");
688 }
689
690 int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements, int numvertices, int *remapvertices)
691 {
692         int i, count;
693         unsigned char *used;
694         used = (unsigned char *)Mem_Alloc(tempmempool, numvertices);
695         memset(used, 0, numvertices);
696         for (i = 0;i < numelements;i++)
697                 used[elements[i]] = 1;
698         for (i = 0, count = 0;i < numvertices;i++)
699                 remapvertices[i] = used[i] ? count++ : -1;
700         Mem_Free(used);
701         return count;
702 }
703
704 qboolean Mod_ValidateElements(int *element3i, unsigned short *element3s, int numtriangles, int firstvertex, int numvertices, const char *filename, int fileline)
705 {
706         int first = firstvertex, last = first + numvertices - 1, numelements = numtriangles * 3;
707         int i;
708         int invalidintcount = 0, invalidintexample = 0;
709         int invalidshortcount = 0, invalidshortexample = 0;
710         int invalidmismatchcount = 0, invalidmismatchexample = 0;
711         if (element3i)
712         {
713                 for (i = 0; i < numelements; i++)
714                 {
715                         if (element3i[i] < first || element3i[i] > last)
716                         {
717                                 invalidintcount++;
718                                 invalidintexample = i;
719                         }
720                 }
721         }
722         if (element3s)
723         {
724                 for (i = 0; i < numelements; i++)
725                 {
726                         if (element3s[i] < first || element3s[i] > last)
727                         {
728                                 invalidintcount++;
729                                 invalidintexample = i;
730                         }
731                 }
732         }
733         if (element3i && element3s)
734         {
735                 for (i = 0; i < numelements; i++)
736                 {
737                         if (element3s[i] != element3i[i])
738                         {
739                                 invalidmismatchcount++;
740                                 invalidmismatchexample = i;
741                         }
742                 }
743         }
744         if (invalidintcount || invalidshortcount || invalidmismatchcount)
745         {
746                 Con_Printf("Mod_ValidateElements(%i, %i, %i, %p, %p) called at %s:%d", numelements, first, last, element3i, element3s, filename, fileline);
747                 Con_Printf(", %i elements are invalid in element3i (example: element3i[%i] == %i)", invalidintcount, invalidintexample, element3i ? element3i[invalidintexample] : 0);
748                 Con_Printf(", %i elements are invalid in element3s (example: element3s[%i] == %i)", invalidshortcount, invalidshortexample, element3s ? element3s[invalidshortexample] : 0);
749                 Con_Printf(", %i elements mismatch between element3i and element3s (example: element3s[%i] is %i and element3i[i] is %i)", invalidmismatchcount, invalidmismatchexample, element3i ? element3i[invalidmismatchexample] : 0, invalidmismatchexample, element3s ? element3s[invalidmismatchexample] : 0);
750                 Con_Print(".  Please debug the engine code - these elements have been modified to not crash, but nothing more.\n");
751
752                 // edit the elements to make them safer, as the driver will crash otherwise
753                 if (element3i)
754                         for (i = 0; i < numelements; i++)
755                                 if (element3i[i] < first || element3i[i] > last)
756                                         element3i[i] = first;
757                 if (element3s)
758                         for (i = 0; i < numelements; i++)
759                                 if (element3s[i] < first || element3s[i] > last)
760                                         element3s[i] = first;
761                 if (element3i && element3s)
762                         for (i = 0; i < numelements; i++)
763                                 if (element3s[i] != element3i[i])
764                                         element3s[i] = element3i[i];
765
766                 return false;
767         }
768         return true;
769 }
770
771 // warning: this is an expensive function!
772 void Mod_BuildNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const int *elements, float *normal3f, qboolean areaweighting)
773 {
774         int i, j;
775         const int *element;
776         float *vectorNormal;
777         float areaNormal[3];
778         // clear the vectors
779         memset(normal3f + 3 * firstvertex, 0, numvertices * sizeof(float[3]));
780         // process each vertex of each triangle and accumulate the results
781         // use area-averaging, to make triangles with a big area have a bigger
782         // weighting on the vertex normal than triangles with a small area
783         // to do so, just add the 'normals' together (the bigger the area
784         // the greater the length of the normal is
785         element = elements;
786         for (i = 0; i < numtriangles; i++, element += 3)
787         {
788                 TriangleNormal(
789                         vertex3f + element[0] * 3,
790                         vertex3f + element[1] * 3,
791                         vertex3f + element[2] * 3,
792                         areaNormal
793                         );
794
795                 if (!areaweighting)
796                         VectorNormalize(areaNormal);
797
798                 for (j = 0;j < 3;j++)
799                 {
800                         vectorNormal = normal3f + element[j] * 3;
801                         vectorNormal[0] += areaNormal[0];
802                         vectorNormal[1] += areaNormal[1];
803                         vectorNormal[2] += areaNormal[2];
804                 }
805         }
806         // and just normalize the accumulated vertex normal in the end
807         vectorNormal = normal3f + 3 * firstvertex;
808         for (i = 0; i < numvertices; i++, vectorNormal += 3)
809                 VectorNormalize(vectorNormal);
810 }
811
812 #if 0
813 static void Mod_BuildBumpVectors(const float *v0, const float *v1, const float *v2, const float *tc0, const float *tc1, const float *tc2, float *svector3f, float *tvector3f, float *normal3f)
814 {
815         float f, tangentcross[3], v10[3], v20[3], tc10[2], tc20[2];
816         // 79 add/sub/negate/multiply (1 cycle), 1 compare (3 cycle?), total cycles not counting load/store/exchange roughly 82 cycles
817         // 6 add, 28 subtract, 39 multiply, 1 compare, 50% chance of 6 negates
818
819         // 6 multiply, 9 subtract
820         VectorSubtract(v1, v0, v10);
821         VectorSubtract(v2, v0, v20);
822         normal3f[0] = v20[1] * v10[2] - v20[2] * v10[1];
823         normal3f[1] = v20[2] * v10[0] - v20[0] * v10[2];
824         normal3f[2] = v20[0] * v10[1] - v20[1] * v10[0];
825         // 12 multiply, 10 subtract
826         tc10[1] = tc1[1] - tc0[1];
827         tc20[1] = tc2[1] - tc0[1];
828         svector3f[0] = tc10[1] * v20[0] - tc20[1] * v10[0];
829         svector3f[1] = tc10[1] * v20[1] - tc20[1] * v10[1];
830         svector3f[2] = tc10[1] * v20[2] - tc20[1] * v10[2];
831         tc10[0] = tc1[0] - tc0[0];
832         tc20[0] = tc2[0] - tc0[0];
833         tvector3f[0] = tc10[0] * v20[0] - tc20[0] * v10[0];
834         tvector3f[1] = tc10[0] * v20[1] - tc20[0] * v10[1];
835         tvector3f[2] = tc10[0] * v20[2] - tc20[0] * v10[2];
836         // 12 multiply, 4 add, 6 subtract
837         f = DotProduct(svector3f, normal3f);
838         svector3f[0] -= f * normal3f[0];
839         svector3f[1] -= f * normal3f[1];
840         svector3f[2] -= f * normal3f[2];
841         f = DotProduct(tvector3f, normal3f);
842         tvector3f[0] -= f * normal3f[0];
843         tvector3f[1] -= f * normal3f[1];
844         tvector3f[2] -= f * normal3f[2];
845         // if texture is mapped the wrong way (counterclockwise), the tangents
846         // have to be flipped, this is detected by calculating a normal from the
847         // two tangents, and seeing if it is opposite the surface normal
848         // 9 multiply, 2 add, 3 subtract, 1 compare, 50% chance of: 6 negates
849         CrossProduct(tvector3f, svector3f, tangentcross);
850         if (DotProduct(tangentcross, normal3f) < 0)
851         {
852                 VectorNegate(svector3f, svector3f);
853                 VectorNegate(tvector3f, tvector3f);
854         }
855 }
856 #endif
857
858 // warning: this is a very expensive function!
859 void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const float *texcoord2f, const float *normal3f, const int *elements, float *svector3f, float *tvector3f, qboolean areaweighting)
860 {
861         int i, tnum;
862         float sdir[3], tdir[3], normal[3], *svec, *tvec;
863         const float *v0, *v1, *v2, *tc0, *tc1, *tc2, *n;
864         float f, tangentcross[3], v10[3], v20[3], tc10[2], tc20[2];
865         const int *e;
866         // clear the vectors
867         memset(svector3f + 3 * firstvertex, 0, numvertices * sizeof(float[3]));
868         memset(tvector3f + 3 * firstvertex, 0, numvertices * sizeof(float[3]));
869         // process each vertex of each triangle and accumulate the results
870         for (tnum = 0, e = elements;tnum < numtriangles;tnum++, e += 3)
871         {
872                 v0 = vertex3f + e[0] * 3;
873                 v1 = vertex3f + e[1] * 3;
874                 v2 = vertex3f + e[2] * 3;
875                 tc0 = texcoord2f + e[0] * 2;
876                 tc1 = texcoord2f + e[1] * 2;
877                 tc2 = texcoord2f + e[2] * 2;
878
879                 // 79 add/sub/negate/multiply (1 cycle), 1 compare (3 cycle?), total cycles not counting load/store/exchange roughly 82 cycles
880                 // 6 add, 28 subtract, 39 multiply, 1 compare, 50% chance of 6 negates
881
882                 // calculate the edge directions and surface normal
883                 // 6 multiply, 9 subtract
884                 VectorSubtract(v1, v0, v10);
885                 VectorSubtract(v2, v0, v20);
886                 normal[0] = v20[1] * v10[2] - v20[2] * v10[1];
887                 normal[1] = v20[2] * v10[0] - v20[0] * v10[2];
888                 normal[2] = v20[0] * v10[1] - v20[1] * v10[0];
889
890                 // calculate the tangents
891                 // 12 multiply, 10 subtract
892                 tc10[1] = tc1[1] - tc0[1];
893                 tc20[1] = tc2[1] - tc0[1];
894                 sdir[0] = tc10[1] * v20[0] - tc20[1] * v10[0];
895                 sdir[1] = tc10[1] * v20[1] - tc20[1] * v10[1];
896                 sdir[2] = tc10[1] * v20[2] - tc20[1] * v10[2];
897                 tc10[0] = tc1[0] - tc0[0];
898                 tc20[0] = tc2[0] - tc0[0];
899                 tdir[0] = tc10[0] * v20[0] - tc20[0] * v10[0];
900                 tdir[1] = tc10[0] * v20[1] - tc20[0] * v10[1];
901                 tdir[2] = tc10[0] * v20[2] - tc20[0] * v10[2];
902
903                 // if texture is mapped the wrong way (counterclockwise), the tangents
904                 // have to be flipped, this is detected by calculating a normal from the
905                 // two tangents, and seeing if it is opposite the surface normal
906                 // 9 multiply, 2 add, 3 subtract, 1 compare, 50% chance of: 6 negates
907                 CrossProduct(tdir, sdir, tangentcross);
908                 if (DotProduct(tangentcross, normal) < 0)
909                 {
910                         VectorNegate(sdir, sdir);
911                         VectorNegate(tdir, tdir);
912                 }
913
914                 if (!areaweighting)
915                 {
916                         VectorNormalize(sdir);
917                         VectorNormalize(tdir);
918                 }
919                 for (i = 0;i < 3;i++)
920                 {
921                         VectorAdd(svector3f + e[i]*3, sdir, svector3f + e[i]*3);
922                         VectorAdd(tvector3f + e[i]*3, tdir, tvector3f + e[i]*3);
923                 }
924         }
925         // make the tangents completely perpendicular to the surface normal, and
926         // then normalize them
927         // 16 assignments, 2 divide, 2 sqrt, 2 negates, 14 adds, 24 multiplies
928         for (i = 0, svec = svector3f + 3 * firstvertex, tvec = tvector3f + 3 * firstvertex, n = normal3f + 3 * firstvertex;i < numvertices;i++, svec += 3, tvec += 3, n += 3)
929         {
930                 f = -DotProduct(svec, n);
931                 VectorMA(svec, f, n, svec);
932                 VectorNormalize(svec);
933                 f = -DotProduct(tvec, n);
934                 VectorMA(tvec, f, n, tvec);
935                 VectorNormalize(tvec);
936         }
937 }
938
939 void Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qboolean lightmapoffsets, qboolean vertexcolors)
940 {
941         unsigned char *data;
942         data = (unsigned char *)Mem_Alloc(mempool, numvertices * (3 + 3 + 3 + 3 + 2 + 2 + (vertexcolors ? 4 : 0)) * sizeof(float) + numvertices * (lightmapoffsets ? 1 : 0) * sizeof(int) + numtriangles * sizeof(int[3]) + (numvertices <= 65536 ? numtriangles * sizeof(unsigned short[3]) : 0));
943         loadmodel->surfmesh.num_vertices = numvertices;
944         loadmodel->surfmesh.num_triangles = numtriangles;
945         if (loadmodel->surfmesh.num_vertices)
946         {
947                 loadmodel->surfmesh.data_vertex3f = (float *)data, data += sizeof(float[3]) * loadmodel->surfmesh.num_vertices;
948                 loadmodel->surfmesh.data_svector3f = (float *)data, data += sizeof(float[3]) * loadmodel->surfmesh.num_vertices;
949                 loadmodel->surfmesh.data_tvector3f = (float *)data, data += sizeof(float[3]) * loadmodel->surfmesh.num_vertices;
950                 loadmodel->surfmesh.data_normal3f = (float *)data, data += sizeof(float[3]) * loadmodel->surfmesh.num_vertices;
951                 loadmodel->surfmesh.data_texcoordtexture2f = (float *)data, data += sizeof(float[2]) * loadmodel->surfmesh.num_vertices;
952                 loadmodel->surfmesh.data_texcoordlightmap2f = (float *)data, data += sizeof(float[2]) * loadmodel->surfmesh.num_vertices;
953                 if (vertexcolors)
954                         loadmodel->surfmesh.data_lightmapcolor4f = (float *)data, data += sizeof(float[4]) * loadmodel->surfmesh.num_vertices;
955                 if (lightmapoffsets)
956                         loadmodel->surfmesh.data_lightmapoffsets = (int *)data, data += sizeof(int) * loadmodel->surfmesh.num_vertices;
957         }
958         if (loadmodel->surfmesh.num_triangles)
959         {
960                 loadmodel->surfmesh.data_element3i = (int *)data, data += sizeof(int[3]) * loadmodel->surfmesh.num_triangles;
961                 if (loadmodel->surfmesh.num_vertices <= 65536)
962                         loadmodel->surfmesh.data_element3s = (unsigned short *)data, data += sizeof(unsigned short[3]) * loadmodel->surfmesh.num_triangles;
963         }
964 }
965
966 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts, int maxtriangles, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, int light, int expandable)
967 {
968         shadowmesh_t *newmesh;
969         unsigned char *data;
970         int size;
971         size = sizeof(shadowmesh_t);
972         size += maxverts * sizeof(float[3]);
973         if (light)
974                 size += maxverts * sizeof(float[11]);
975         size += maxtriangles * sizeof(int[3]);
976         if (maxverts <= 65536)
977                 size += maxtriangles * sizeof(unsigned short[3]);
978         if (expandable)
979                 size += SHADOWMESHVERTEXHASH * sizeof(shadowmeshvertexhash_t *) + maxverts * sizeof(shadowmeshvertexhash_t);
980         data = (unsigned char *)Mem_Alloc(mempool, size);
981         newmesh = (shadowmesh_t *)data;data += sizeof(*newmesh);
982         newmesh->map_diffuse = map_diffuse;
983         newmesh->map_specular = map_specular;
984         newmesh->map_normal = map_normal;
985         newmesh->maxverts = maxverts;
986         newmesh->maxtriangles = maxtriangles;
987         newmesh->numverts = 0;
988         newmesh->numtriangles = 0;
989         memset(newmesh->sideoffsets, 0, sizeof(newmesh->sideoffsets));
990         memset(newmesh->sidetotals, 0, sizeof(newmesh->sidetotals));
991
992         newmesh->vertex3f = (float *)data;data += maxverts * sizeof(float[3]);
993         if (light)
994         {
995                 newmesh->svector3f = (float *)data;data += maxverts * sizeof(float[3]);
996                 newmesh->tvector3f = (float *)data;data += maxverts * sizeof(float[3]);
997                 newmesh->normal3f = (float *)data;data += maxverts * sizeof(float[3]);
998                 newmesh->texcoord2f = (float *)data;data += maxverts * sizeof(float[2]);
999         }
1000         newmesh->element3i = (int *)data;data += maxtriangles * sizeof(int[3]);
1001         if (expandable)
1002         {
1003                 newmesh->vertexhashtable = (shadowmeshvertexhash_t **)data;data += SHADOWMESHVERTEXHASH * sizeof(shadowmeshvertexhash_t *);
1004                 newmesh->vertexhashentries = (shadowmeshvertexhash_t *)data;data += maxverts * sizeof(shadowmeshvertexhash_t);
1005         }
1006         if (maxverts <= 65536)
1007                 newmesh->element3s = (unsigned short *)data;data += maxtriangles * sizeof(unsigned short[3]);
1008         return newmesh;
1009 }
1010
1011 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light)
1012 {
1013         shadowmesh_t *newmesh;
1014         newmesh = Mod_ShadowMesh_Alloc(mempool, oldmesh->numverts, oldmesh->numtriangles, oldmesh->map_diffuse, oldmesh->map_specular, oldmesh->map_normal, light, false);
1015         newmesh->numverts = oldmesh->numverts;
1016         newmesh->numtriangles = oldmesh->numtriangles;
1017         memcpy(newmesh->sideoffsets, oldmesh->sideoffsets, sizeof(oldmesh->sideoffsets));
1018         memcpy(newmesh->sidetotals, oldmesh->sidetotals, sizeof(oldmesh->sidetotals));
1019
1020         memcpy(newmesh->vertex3f, oldmesh->vertex3f, oldmesh->numverts * sizeof(float[3]));
1021         if (newmesh->svector3f && oldmesh->svector3f)
1022         {
1023                 memcpy(newmesh->svector3f, oldmesh->svector3f, oldmesh->numverts * sizeof(float[3]));
1024                 memcpy(newmesh->tvector3f, oldmesh->tvector3f, oldmesh->numverts * sizeof(float[3]));
1025                 memcpy(newmesh->normal3f, oldmesh->normal3f, oldmesh->numverts * sizeof(float[3]));
1026                 memcpy(newmesh->texcoord2f, oldmesh->texcoord2f, oldmesh->numverts * sizeof(float[2]));
1027         }
1028         memcpy(newmesh->element3i, oldmesh->element3i, oldmesh->numtriangles * sizeof(int[3]));
1029         return newmesh;
1030 }
1031
1032 static int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f)
1033 {
1034         int hashindex, vnum;
1035         shadowmeshvertexhash_t *hash;
1036         // this uses prime numbers intentionally
1037         hashindex = (unsigned int) (vertex14f[0] * 2003 + vertex14f[1] * 4001 + vertex14f[2] * 7919) % SHADOWMESHVERTEXHASH;
1038         for (hash = mesh->vertexhashtable[hashindex];hash;hash = hash->next)
1039         {
1040                 vnum = (hash - mesh->vertexhashentries);
1041                 if ((mesh->vertex3f == NULL || (mesh->vertex3f[vnum * 3 + 0] == vertex14f[0] && mesh->vertex3f[vnum * 3 + 1] == vertex14f[1] && mesh->vertex3f[vnum * 3 + 2] == vertex14f[2]))
1042                  && (mesh->svector3f == NULL || (mesh->svector3f[vnum * 3 + 0] == vertex14f[3] && mesh->svector3f[vnum * 3 + 1] == vertex14f[4] && mesh->svector3f[vnum * 3 + 2] == vertex14f[5]))
1043                  && (mesh->tvector3f == NULL || (mesh->tvector3f[vnum * 3 + 0] == vertex14f[6] && mesh->tvector3f[vnum * 3 + 1] == vertex14f[7] && mesh->tvector3f[vnum * 3 + 2] == vertex14f[8]))
1044                  && (mesh->normal3f == NULL || (mesh->normal3f[vnum * 3 + 0] == vertex14f[9] && mesh->normal3f[vnum * 3 + 1] == vertex14f[10] && mesh->normal3f[vnum * 3 + 2] == vertex14f[11]))
1045                  && (mesh->texcoord2f == NULL || (mesh->texcoord2f[vnum * 2 + 0] == vertex14f[12] && mesh->texcoord2f[vnum * 2 + 1] == vertex14f[13])))
1046                         return hash - mesh->vertexhashentries;
1047         }
1048         vnum = mesh->numverts++;
1049         hash = mesh->vertexhashentries + vnum;
1050         hash->next = mesh->vertexhashtable[hashindex];
1051         mesh->vertexhashtable[hashindex] = hash;
1052         if (mesh->vertex3f) {mesh->vertex3f[vnum * 3 + 0] = vertex14f[0];mesh->vertex3f[vnum * 3 + 1] = vertex14f[1];mesh->vertex3f[vnum * 3 + 2] = vertex14f[2];}
1053         if (mesh->svector3f) {mesh->svector3f[vnum * 3 + 0] = vertex14f[3];mesh->svector3f[vnum * 3 + 1] = vertex14f[4];mesh->svector3f[vnum * 3 + 2] = vertex14f[5];}
1054         if (mesh->tvector3f) {mesh->tvector3f[vnum * 3 + 0] = vertex14f[6];mesh->tvector3f[vnum * 3 + 1] = vertex14f[7];mesh->tvector3f[vnum * 3 + 2] = vertex14f[8];}
1055         if (mesh->normal3f) {mesh->normal3f[vnum * 3 + 0] = vertex14f[9];mesh->normal3f[vnum * 3 + 1] = vertex14f[10];mesh->normal3f[vnum * 3 + 2] = vertex14f[11];}
1056         if (mesh->texcoord2f) {mesh->texcoord2f[vnum * 2 + 0] = vertex14f[12];mesh->texcoord2f[vnum * 2 + 1] = vertex14f[13];}
1057         return vnum;
1058 }
1059
1060 static void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex14f)
1061 {
1062         if (mesh->numtriangles == 0)
1063         {
1064                 // set the properties on this empty mesh to be more favorable...
1065                 // (note: this case only occurs for the first triangle added to a new mesh chain)
1066                 mesh->map_diffuse = map_diffuse;
1067                 mesh->map_specular = map_specular;
1068                 mesh->map_normal = map_normal;
1069         }
1070         while (mesh->map_diffuse != map_diffuse || mesh->map_specular != map_specular || mesh->map_normal != map_normal || mesh->numverts + 3 > mesh->maxverts || mesh->numtriangles + 1 > mesh->maxtriangles)
1071         {
1072                 if (mesh->next == NULL)
1073                         mesh->next = Mod_ShadowMesh_Alloc(mempool, max(mesh->maxverts, 300), max(mesh->maxtriangles, 100), map_diffuse, map_specular, map_normal, mesh->svector3f != NULL, true);
1074                 mesh = mesh->next;
1075         }
1076         mesh->element3i[mesh->numtriangles * 3 + 0] = Mod_ShadowMesh_AddVertex(mesh, vertex14f + 14 * 0);
1077         mesh->element3i[mesh->numtriangles * 3 + 1] = Mod_ShadowMesh_AddVertex(mesh, vertex14f + 14 * 1);
1078         mesh->element3i[mesh->numtriangles * 3 + 2] = Mod_ShadowMesh_AddVertex(mesh, vertex14f + 14 * 2);
1079         mesh->numtriangles++;
1080 }
1081
1082 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *texcoord2f, int numtris, const int *element3i)
1083 {
1084         int i, j, e;
1085         float vbuf[3*14], *v;
1086         memset(vbuf, 0, sizeof(vbuf));
1087         for (i = 0;i < numtris;i++)
1088         {
1089                 for (j = 0, v = vbuf;j < 3;j++, v += 14)
1090                 {
1091                         e = *element3i++;
1092                         if (vertex3f)
1093                         {
1094                                 v[0] = vertex3f[e * 3 + 0];
1095                                 v[1] = vertex3f[e * 3 + 1];
1096                                 v[2] = vertex3f[e * 3 + 2];
1097                         }
1098                         if (svector3f)
1099                         {
1100                                 v[3] = svector3f[e * 3 + 0];
1101                                 v[4] = svector3f[e * 3 + 1];
1102                                 v[5] = svector3f[e * 3 + 2];
1103                         }
1104                         if (tvector3f)
1105                         {
1106                                 v[6] = tvector3f[e * 3 + 0];
1107                                 v[7] = tvector3f[e * 3 + 1];
1108                                 v[8] = tvector3f[e * 3 + 2];
1109                         }
1110                         if (normal3f)
1111                         {
1112                                 v[9] = normal3f[e * 3 + 0];
1113                                 v[10] = normal3f[e * 3 + 1];
1114                                 v[11] = normal3f[e * 3 + 2];
1115                         }
1116                         if (texcoord2f)
1117                         {
1118                                 v[12] = texcoord2f[e * 2 + 0];
1119                                 v[13] = texcoord2f[e * 2 + 1];
1120                         }
1121                 }
1122                 Mod_ShadowMesh_AddTriangle(mempool, mesh, map_diffuse, map_specular, map_normal, vbuf);
1123         }
1124
1125         // the triangle calculation can take a while, so let's do a keepalive here
1126         CL_KeepaliveMessage(false);
1127 }
1128
1129 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int maxverts, int maxtriangles, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, int light, int expandable)
1130 {
1131         // the preparation before shadow mesh initialization can take a while, so let's do a keepalive here
1132         CL_KeepaliveMessage(false);
1133
1134         return Mod_ShadowMesh_Alloc(mempool, maxverts, maxtriangles, map_diffuse, map_specular, map_normal, light, expandable);
1135 }
1136
1137 static void Mod_ShadowMesh_CreateVBOs(shadowmesh_t *mesh, mempool_t *mempool)
1138 {
1139         if (!mesh->numverts)
1140                 return;
1141
1142         // make sure we don't crash inside the driver if something went wrong, as it's annoying to debug
1143         Mod_ValidateElements(mesh->element3i, mesh->element3s, mesh->numtriangles, 0, mesh->numverts, __FILE__, __LINE__);
1144
1145         // build r_vertexmesh_t array
1146         // (compressed interleaved array for D3D)
1147         if (!mesh->vertexmesh && mesh->texcoord2f && vid.useinterleavedarrays)
1148         {
1149                 int vertexindex;
1150                 int numvertices = mesh->numverts;
1151                 r_vertexmesh_t *vertexmesh;
1152                 mesh->vertexmesh = vertexmesh = (r_vertexmesh_t*)Mem_Alloc(mempool, numvertices * sizeof(*mesh->vertexmesh));
1153                 for (vertexindex = 0;vertexindex < numvertices;vertexindex++, vertexmesh++)
1154                 {
1155                         VectorCopy(mesh->vertex3f + 3*vertexindex, vertexmesh->vertex3f);
1156                         VectorScale(mesh->svector3f + 3*vertexindex, 1.0f, vertexmesh->svector3f);
1157                         VectorScale(mesh->tvector3f + 3*vertexindex, 1.0f, vertexmesh->tvector3f);
1158                         VectorScale(mesh->normal3f + 3*vertexindex, 1.0f, vertexmesh->normal3f);
1159                         Vector2Copy(mesh->texcoord2f + 2*vertexindex, vertexmesh->texcoordtexture2f);
1160                 }
1161         }
1162
1163         // upload short indices as a buffer
1164         if (mesh->element3s && !mesh->element3s_indexbuffer)
1165                 mesh->element3s_indexbuffer = R_Mesh_CreateMeshBuffer(mesh->element3s, mesh->numtriangles * sizeof(short[3]), "shadowmesh", true, false, false, true);
1166
1167         // upload int indices as a buffer
1168         if (mesh->element3i && !mesh->element3i_indexbuffer && !mesh->element3s)
1169                 mesh->element3i_indexbuffer = R_Mesh_CreateMeshBuffer(mesh->element3i, mesh->numtriangles * sizeof(int[3]), "shadowmesh", true, false, false, false);
1170
1171         // vertex buffer is several arrays and we put them in the same buffer
1172         //
1173         // is this wise?  the texcoordtexture2f array is used with dynamic
1174         // vertex/svector/tvector/normal when rendering animated models, on the
1175         // other hand animated models don't use a lot of vertices anyway...
1176         if (!mesh->vbo_vertexbuffer && !vid.useinterleavedarrays)
1177         {
1178                 int size;
1179                 unsigned char *mem;
1180                 size = 0;
1181                 mesh->vbooffset_vertexmesh         = size;if (mesh->vertexmesh        ) size += mesh->numverts * sizeof(r_vertexmesh_t);
1182                 mesh->vbooffset_vertex3f           = size;if (mesh->vertex3f          ) size += mesh->numverts * sizeof(float[3]);
1183                 mesh->vbooffset_svector3f          = size;if (mesh->svector3f         ) size += mesh->numverts * sizeof(float[3]);
1184                 mesh->vbooffset_tvector3f          = size;if (mesh->tvector3f         ) size += mesh->numverts * sizeof(float[3]);
1185                 mesh->vbooffset_normal3f           = size;if (mesh->normal3f          ) size += mesh->numverts * sizeof(float[3]);
1186                 mesh->vbooffset_texcoord2f         = size;if (mesh->texcoord2f        ) size += mesh->numverts * sizeof(float[2]);
1187                 mem = (unsigned char *)Mem_Alloc(tempmempool, size);
1188                 if (mesh->vertexmesh        ) memcpy(mem + mesh->vbooffset_vertexmesh        , mesh->vertexmesh        , mesh->numverts * sizeof(r_vertexmesh_t));
1189                 if (mesh->vertex3f          ) memcpy(mem + mesh->vbooffset_vertex3f          , mesh->vertex3f          , mesh->numverts * sizeof(float[3]));
1190                 if (mesh->svector3f         ) memcpy(mem + mesh->vbooffset_svector3f         , mesh->svector3f         , mesh->numverts * sizeof(float[3]));
1191                 if (mesh->tvector3f         ) memcpy(mem + mesh->vbooffset_tvector3f         , mesh->tvector3f         , mesh->numverts * sizeof(float[3]));
1192                 if (mesh->normal3f          ) memcpy(mem + mesh->vbooffset_normal3f          , mesh->normal3f          , mesh->numverts * sizeof(float[3]));
1193                 if (mesh->texcoord2f        ) memcpy(mem + mesh->vbooffset_texcoord2f        , mesh->texcoord2f        , mesh->numverts * sizeof(float[2]));
1194                 mesh->vbo_vertexbuffer = R_Mesh_CreateMeshBuffer(mem, size, "shadowmesh", false, false, false, false);
1195                 Mem_Free(mem);
1196         }
1197 }
1198
1199 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, qboolean light, qboolean createvbo)
1200 {
1201         shadowmesh_t *mesh, *newmesh, *nextmesh;
1202         // reallocate meshs to conserve space
1203         for (mesh = firstmesh, firstmesh = NULL;mesh;mesh = nextmesh)
1204         {
1205                 nextmesh = mesh->next;
1206                 if (mesh->numverts >= 3 && mesh->numtriangles >= 1)
1207                 {
1208                         newmesh = Mod_ShadowMesh_ReAlloc(mempool, mesh, light);
1209                         newmesh->next = firstmesh;
1210                         firstmesh = newmesh;
1211                         if (newmesh->element3s)
1212                         {
1213                                 int i;
1214                                 for (i = 0;i < newmesh->numtriangles*3;i++)
1215                                         newmesh->element3s[i] = newmesh->element3i[i];
1216                         }
1217                         if (createvbo)
1218                                 Mod_ShadowMesh_CreateVBOs(newmesh, mempool);
1219                 }
1220                 Mem_Free(mesh);
1221         }
1222
1223         // this can take a while, so let's do a keepalive here
1224         CL_KeepaliveMessage(false);
1225
1226         return firstmesh;
1227 }
1228
1229 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius)
1230 {
1231         int i;
1232         shadowmesh_t *mesh;
1233         vec3_t nmins, nmaxs, ncenter, temp;
1234         float nradius2, dist2, *v;
1235         VectorClear(nmins);
1236         VectorClear(nmaxs);
1237         // calculate bbox
1238         for (mesh = firstmesh;mesh;mesh = mesh->next)
1239         {
1240                 if (mesh == firstmesh)
1241                 {
1242                         VectorCopy(mesh->vertex3f, nmins);
1243                         VectorCopy(mesh->vertex3f, nmaxs);
1244                 }
1245                 for (i = 0, v = mesh->vertex3f;i < mesh->numverts;i++, v += 3)
1246                 {
1247                         if (nmins[0] > v[0]) nmins[0] = v[0];if (nmaxs[0] < v[0]) nmaxs[0] = v[0];
1248                         if (nmins[1] > v[1]) nmins[1] = v[1];if (nmaxs[1] < v[1]) nmaxs[1] = v[1];
1249                         if (nmins[2] > v[2]) nmins[2] = v[2];if (nmaxs[2] < v[2]) nmaxs[2] = v[2];
1250                 }
1251         }
1252         // calculate center and radius
1253         ncenter[0] = (nmins[0] + nmaxs[0]) * 0.5f;
1254         ncenter[1] = (nmins[1] + nmaxs[1]) * 0.5f;
1255         ncenter[2] = (nmins[2] + nmaxs[2]) * 0.5f;
1256         nradius2 = 0;
1257         for (mesh = firstmesh;mesh;mesh = mesh->next)
1258         {
1259                 for (i = 0, v = mesh->vertex3f;i < mesh->numverts;i++, v += 3)
1260                 {
1261                         VectorSubtract(v, ncenter, temp);
1262                         dist2 = DotProduct(temp, temp);
1263                         if (nradius2 < dist2)
1264                                 nradius2 = dist2;
1265                 }
1266         }
1267         // return data
1268         if (mins)
1269                 VectorCopy(nmins, mins);
1270         if (maxs)
1271                 VectorCopy(nmaxs, maxs);
1272         if (center)
1273                 VectorCopy(ncenter, center);
1274         if (radius)
1275                 *radius = sqrt(nradius2);
1276 }
1277
1278 void Mod_ShadowMesh_Free(shadowmesh_t *mesh)
1279 {
1280         shadowmesh_t *nextmesh;
1281         for (;mesh;mesh = nextmesh)
1282         {
1283                 if (mesh->element3i_indexbuffer)
1284                         R_Mesh_DestroyMeshBuffer(mesh->element3i_indexbuffer);
1285                 if (mesh->element3s_indexbuffer)
1286                         R_Mesh_DestroyMeshBuffer(mesh->element3s_indexbuffer);
1287                 if (mesh->vbo_vertexbuffer)
1288                         R_Mesh_DestroyMeshBuffer(mesh->vbo_vertexbuffer);
1289                 nextmesh = mesh->next;
1290                 Mem_Free(mesh);
1291         }
1292 }
1293
1294 #if 0
1295 static void Mod_GetTerrainVertex3fTexCoord2fFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
1296 {
1297         float v[3], tc[3];
1298         v[0] = ix;
1299         v[1] = iy;
1300         if (ix >= 0 && iy >= 0 && ix < imagewidth && iy < imageheight)
1301                 v[2] = (imagepixels[((iy*imagewidth)+ix)*4+0] + imagepixels[((iy*imagewidth)+ix)*4+1] + imagepixels[((iy*imagewidth)+ix)*4+2]) * (1.0f / 765.0f);
1302         else
1303                 v[2] = 0;
1304         Matrix4x4_Transform(pixelstepmatrix, v, vertex3f);
1305         Matrix4x4_Transform(pixeltexturestepmatrix, v, tc);
1306         texcoord2f[0] = tc[0];
1307         texcoord2f[1] = tc[1];
1308 }
1309
1310 static void Mod_GetTerrainVertexFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
1311 {
1312         float vup[3], vdown[3], vleft[3], vright[3];
1313         float tcup[3], tcdown[3], tcleft[3], tcright[3];
1314         float sv[3], tv[3], nl[3];
1315         Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix, iy, vertex3f, texcoord2f, pixelstepmatrix, pixeltexturestepmatrix);
1316         Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix, iy - 1, vup, tcup, pixelstepmatrix, pixeltexturestepmatrix);
1317         Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix, iy + 1, vdown, tcdown, pixelstepmatrix, pixeltexturestepmatrix);
1318         Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix - 1, iy, vleft, tcleft, pixelstepmatrix, pixeltexturestepmatrix);
1319         Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix + 1, iy, vright, tcright, pixelstepmatrix, pixeltexturestepmatrix);
1320         Mod_BuildBumpVectors(vertex3f, vup, vright, texcoord2f, tcup, tcright, svector3f, tvector3f, normal3f);
1321         Mod_BuildBumpVectors(vertex3f, vright, vdown, texcoord2f, tcright, tcdown, sv, tv, nl);
1322         VectorAdd(svector3f, sv, svector3f);
1323         VectorAdd(tvector3f, tv, tvector3f);
1324         VectorAdd(normal3f, nl, normal3f);
1325         Mod_BuildBumpVectors(vertex3f, vdown, vleft, texcoord2f, tcdown, tcleft, sv, tv, nl);
1326         VectorAdd(svector3f, sv, svector3f);
1327         VectorAdd(tvector3f, tv, tvector3f);
1328         VectorAdd(normal3f, nl, normal3f);
1329         Mod_BuildBumpVectors(vertex3f, vleft, vup, texcoord2f, tcleft, tcup, sv, tv, nl);
1330         VectorAdd(svector3f, sv, svector3f);
1331         VectorAdd(tvector3f, tv, tvector3f);
1332         VectorAdd(normal3f, nl, normal3f);
1333 }
1334
1335 static void Mod_ConstructTerrainPatchFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int x1, int y1, int width, int height, int *element3i, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
1336 {
1337         int x, y, ix, iy, *e;
1338         e = element3i;
1339         for (y = 0;y < height;y++)
1340         {
1341                 for (x = 0;x < width;x++)
1342                 {
1343                         e[0] = (y + 1) * (width + 1) + (x + 0);
1344                         e[1] = (y + 0) * (width + 1) + (x + 0);
1345                         e[2] = (y + 1) * (width + 1) + (x + 1);
1346                         e[3] = (y + 0) * (width + 1) + (x + 0);
1347                         e[4] = (y + 0) * (width + 1) + (x + 1);
1348                         e[5] = (y + 1) * (width + 1) + (x + 1);
1349                         e += 6;
1350                 }
1351         }
1352         for (y = 0, iy = y1;y < height + 1;y++, iy++)
1353                 for (x = 0, ix = x1;x < width + 1;x++, ix++, vertex3f += 3, texcoord2f += 2, svector3f += 3, tvector3f += 3, normal3f += 3)
1354                         Mod_GetTerrainVertexFromBGRA(imagepixels, imagewidth, imageheight, ix, iy, vertex3f, texcoord2f, svector3f, tvector3f, normal3f, pixelstepmatrix, pixeltexturestepmatrix);
1355 }
1356 #endif
1357
1358 #if 0
1359 void Mod_Terrain_SurfaceRecurseChunk(dp_model_t *model, int stepsize, int x, int y)
1360 {
1361         float mins[3];
1362         float maxs[3];
1363         float chunkwidth = min(stepsize, model->terrain.width - 1 - x);
1364         float chunkheight = min(stepsize, model->terrain.height - 1 - y);
1365         float viewvector[3];
1366         unsigned int firstvertex;
1367         unsigned int *e;
1368         float *v;
1369         if (chunkwidth < 2 || chunkheight < 2)
1370                 return;
1371         VectorSet(mins, model->terrain.mins[0] +  x    * stepsize * model->terrain.scale[0], model->terrain.mins[1] +  y    * stepsize * model->terrain.scale[1], model->terrain.mins[2]);
1372         VectorSet(maxs, model->terrain.mins[0] + (x+1) * stepsize * model->terrain.scale[0], model->terrain.mins[1] + (y+1) * stepsize * model->terrain.scale[1], model->terrain.maxs[2]);
1373         viewvector[0] = bound(mins[0], localvieworigin, maxs[0]) - model->terrain.vieworigin[0];
1374         viewvector[1] = bound(mins[1], localvieworigin, maxs[1]) - model->terrain.vieworigin[1];
1375         viewvector[2] = bound(mins[2], localvieworigin, maxs[2]) - model->terrain.vieworigin[2];
1376         if (stepsize > 1 && VectorLength(viewvector) < stepsize*model->terrain.scale[0]*r_terrain_lodscale.value)
1377         {
1378                 // too close for this stepsize, emit as 4 chunks instead
1379                 stepsize /= 2;
1380                 Mod_Terrain_SurfaceRecurseChunk(model, stepsize, x, y);
1381                 Mod_Terrain_SurfaceRecurseChunk(model, stepsize, x+stepsize, y);
1382                 Mod_Terrain_SurfaceRecurseChunk(model, stepsize, x, y+stepsize);
1383                 Mod_Terrain_SurfaceRecurseChunk(model, stepsize, x+stepsize, y+stepsize);
1384                 return;
1385         }
1386         // emit the geometry at stepsize into our vertex buffer / index buffer
1387         // we add two columns and two rows for skirt
1388         outwidth = chunkwidth+2;
1389         outheight = chunkheight+2;
1390         outwidth2 = outwidth-1;
1391         outheight2 = outheight-1;
1392         outwidth3 = outwidth+1;
1393         outheight3 = outheight+1;
1394         firstvertex = numvertices;
1395         e = model->terrain.element3i + numtriangles;
1396         numtriangles += chunkwidth*chunkheight*2+chunkwidth*2*2+chunkheight*2*2;
1397         v = model->terrain.vertex3f + numvertices;
1398         numvertices += (chunkwidth+1)*(chunkheight+1)+(chunkwidth+1)*2+(chunkheight+1)*2;
1399         // emit the triangles (note: the skirt is treated as two extra rows and two extra columns)
1400         for (ty = 0;ty < outheight;ty++)
1401         {
1402                 for (tx = 0;tx < outwidth;tx++)
1403                 {
1404                         *e++ = firstvertex + (ty  )*outwidth3+(tx  );
1405                         *e++ = firstvertex + (ty  )*outwidth3+(tx+1);
1406                         *e++ = firstvertex + (ty+1)*outwidth3+(tx+1);
1407                         *e++ = firstvertex + (ty  )*outwidth3+(tx  );
1408                         *e++ = firstvertex + (ty+1)*outwidth3+(tx+1);
1409                         *e++ = firstvertex + (ty+1)*outwidth3+(tx  );
1410                 }
1411         }
1412         // TODO: emit surface vertices (x+tx*stepsize, y+ty*stepsize)
1413         for (ty = 0;ty <= outheight;ty++)
1414         {
1415                 skirtrow = ty == 0 || ty == outheight;
1416                 ry = y+bound(1, ty, outheight)*stepsize;
1417                 for (tx = 0;tx <= outwidth;tx++)
1418                 {
1419                         skirt = skirtrow || tx == 0 || tx == outwidth;
1420                         rx = x+bound(1, tx, outwidth)*stepsize;
1421                         v[0] = rx*scale[0];
1422                         v[1] = ry*scale[1];
1423                         v[2] = heightmap[ry*terrainwidth+rx]*scale[2];
1424                         v += 3;
1425                 }
1426         }
1427         // TODO: emit skirt vertices
1428 }
1429
1430 void Mod_Terrain_UpdateSurfacesForViewOrigin(dp_model_t *model)
1431 {
1432         for (y = 0;y < model->terrain.size[1];y += model->terrain.
1433         Mod_Terrain_SurfaceRecurseChunk(model, model->terrain.maxstepsize, x, y);
1434         Mod_Terrain_BuildChunk(model, 
1435 }
1436 #endif
1437
1438 static int Mod_LoadQ3Shaders_EnumerateWaveFunc(const char *s)
1439 {
1440         int offset = 0;
1441         if (!strncasecmp(s, "user", 4)) // parse stuff like "user1sin", always user<n>func
1442         {
1443                 offset = bound(0, s[4] - '0', 9);
1444                 offset = (offset + 1) << Q3WAVEFUNC_USER_SHIFT;
1445                 s += 4;
1446                 if(*s)
1447                         ++s;
1448         }
1449         if (!strcasecmp(s, "sin"))             return offset | Q3WAVEFUNC_SIN;
1450         if (!strcasecmp(s, "square"))          return offset | Q3WAVEFUNC_SQUARE;
1451         if (!strcasecmp(s, "triangle"))        return offset | Q3WAVEFUNC_TRIANGLE;
1452         if (!strcasecmp(s, "sawtooth"))        return offset | Q3WAVEFUNC_SAWTOOTH;
1453         if (!strcasecmp(s, "inversesawtooth")) return offset | Q3WAVEFUNC_INVERSESAWTOOTH;
1454         if (!strcasecmp(s, "noise"))           return offset | Q3WAVEFUNC_NOISE;
1455         if (!strcasecmp(s, "none"))            return offset | Q3WAVEFUNC_NONE;
1456         Con_DPrintf("Mod_LoadQ3Shaders: unknown wavefunc %s\n", s);
1457         return offset | Q3WAVEFUNC_NONE;
1458 }
1459
1460 void Mod_FreeQ3Shaders(void)
1461 {
1462         Mem_FreePool(&q3shaders_mem);
1463 }
1464
1465 static void Q3Shader_AddToHash (q3shaderinfo_t* shader)
1466 {
1467         unsigned short hash = CRC_Block_CaseInsensitive ((const unsigned char *)shader->name, strlen (shader->name));
1468         q3shader_hash_entry_t* entry = q3shader_data->hash + (hash % Q3SHADER_HASH_SIZE);
1469         q3shader_hash_entry_t* lastEntry = NULL;
1470         do
1471         {
1472                 if (strcasecmp (entry->shader.name, shader->name) == 0)
1473                 {
1474                         // redeclaration
1475                         if(shader->dpshaderkill)
1476                         {
1477                                 // killed shader is a redeclarion? we can safely ignore it
1478                                 return;
1479                         }
1480                         else if(entry->shader.dpshaderkill)
1481                         {
1482                                 // replace the old shader!
1483                                 // this will skip the entry allocating part
1484                                 // below and just replace the shader
1485                                 break;
1486                         }
1487                         else
1488                         {
1489                                 unsigned char *start, *end, *start2;
1490                                 start = (unsigned char *) (&shader->Q3SHADERINFO_COMPARE_START);
1491                                 end = ((unsigned char *) (&shader->Q3SHADERINFO_COMPARE_END)) + sizeof(shader->Q3SHADERINFO_COMPARE_END);
1492                                 start2 = (unsigned char *) (&entry->shader.Q3SHADERINFO_COMPARE_START);
1493                                 if(memcmp(start, start2, end - start))
1494                                         Con_DPrintf("Shader '%s' already defined, ignoring mismatching redeclaration\n", shader->name);
1495                                 else
1496                                         Con_DPrintf("Shader '%s' already defined\n", shader->name);
1497                                 return;
1498                         }
1499                 }
1500                 lastEntry = entry;
1501                 entry = entry->chain;
1502         }
1503         while (entry != NULL);
1504         if (entry == NULL)
1505         {
1506                 if (lastEntry->shader.name[0] != 0)
1507                 {
1508                         /* Add to chain */
1509                         q3shader_hash_entry_t* newEntry = (q3shader_hash_entry_t*)
1510                           Mem_ExpandableArray_AllocRecord (&q3shader_data->hash_entries);
1511
1512                         while (lastEntry->chain != NULL) lastEntry = lastEntry->chain;
1513                         lastEntry->chain = newEntry;
1514                         newEntry->chain = NULL;
1515                         lastEntry = newEntry;
1516                 }
1517                 /* else: head of chain, in hash entry array */
1518                 entry = lastEntry;
1519         }
1520         memcpy (&entry->shader, shader, sizeof (q3shaderinfo_t));
1521 }
1522
1523 extern cvar_t mod_noshader_default_offsetmapping;
1524 extern cvar_t mod_q3shader_default_offsetmapping;
1525 extern cvar_t mod_q3shader_default_offsetmapping_scale;
1526 extern cvar_t mod_q3shader_default_offsetmapping_bias;
1527 extern cvar_t mod_q3shader_default_polygonoffset;
1528 extern cvar_t mod_q3shader_default_polygonfactor;
1529 extern cvar_t mod_q3shader_force_addalpha;
1530 extern cvar_t mod_q3shader_force_terrain_alphaflag;
1531 void Mod_LoadQ3Shaders(void)
1532 {
1533         int j;
1534         int fileindex;
1535         fssearch_t *search;
1536         char *f;
1537         const char *text;
1538         q3shaderinfo_t shader;
1539         q3shaderinfo_layer_t *layer;
1540         int numparameters;
1541         char parameter[TEXTURE_MAXFRAMES + 4][Q3PATHLENGTH];
1542         char *custsurfaceparmnames[256]; // VorteX: q3map2 has 64 but well, someone will need more
1543         unsigned long custsurfaceflags[256]; 
1544         int numcustsurfaceflags;
1545         qboolean dpshaderkill;
1546
1547         Mod_FreeQ3Shaders();
1548
1549         q3shaders_mem = Mem_AllocPool("q3shaders", 0, NULL);
1550         q3shader_data = (q3shader_data_t*)Mem_Alloc (q3shaders_mem,
1551                 sizeof (q3shader_data_t));
1552         Mem_ExpandableArray_NewArray (&q3shader_data->hash_entries,
1553                 q3shaders_mem, sizeof (q3shader_hash_entry_t), 256);
1554         Mem_ExpandableArray_NewArray (&q3shader_data->char_ptrs,
1555                 q3shaders_mem, sizeof (char**), 256);
1556
1557         // parse custinfoparms.txt
1558         numcustsurfaceflags = 0;
1559         if ((text = f = (char *)FS_LoadFile("scripts/custinfoparms.txt", tempmempool, false, NULL)) != NULL)
1560         {
1561                 if (!COM_ParseToken_QuakeC(&text, false) || strcasecmp(com_token, "{"))
1562                         Con_DPrintf("scripts/custinfoparms.txt: contentflags section parsing error - expected \"{\", found \"%s\"\n", com_token);
1563                 else
1564                 {
1565                         while (COM_ParseToken_QuakeC(&text, false))
1566                                 if (!strcasecmp(com_token, "}"))
1567                                         break;
1568                         // custom surfaceflags section
1569                         if (!COM_ParseToken_QuakeC(&text, false) || strcasecmp(com_token, "{"))
1570                                 Con_DPrintf("scripts/custinfoparms.txt: surfaceflags section parsing error - expected \"{\", found \"%s\"\n", com_token);
1571                         else
1572                         {
1573                                 while(COM_ParseToken_QuakeC(&text, false))
1574                                 {
1575                                         if (!strcasecmp(com_token, "}"))
1576                                                 break;  
1577                                         // register surfaceflag
1578                                         if (numcustsurfaceflags >= 256)
1579                                         {
1580                                                 Con_Printf("scripts/custinfoparms.txt: surfaceflags section parsing error - max 256 surfaceflags exceeded\n");
1581                                                 break;
1582                                         }
1583                                         // name
1584                                         j = (int)strlen(com_token)+1;
1585                                         custsurfaceparmnames[numcustsurfaceflags] = (char *)Mem_Alloc(tempmempool, j);
1586                                         strlcpy(custsurfaceparmnames[numcustsurfaceflags], com_token, j+1);
1587                                         // value
1588                                         if (COM_ParseToken_QuakeC(&text, false))
1589                                                 custsurfaceflags[numcustsurfaceflags] = strtol(com_token, NULL, 0);
1590                                         else
1591                                                 custsurfaceflags[numcustsurfaceflags] = 0;
1592                                         numcustsurfaceflags++;
1593                                 }
1594                         }
1595                 }
1596                 Mem_Free(f);
1597         }
1598
1599         // parse shaders
1600         search = FS_Search("scripts/*.shader", true, false);
1601         if (!search)
1602                 return;
1603         for (fileindex = 0;fileindex < search->numfilenames;fileindex++)
1604         {
1605                 text = f = (char *)FS_LoadFile(search->filenames[fileindex], tempmempool, false, NULL);
1606                 if (!f)
1607                         continue;
1608                 while (COM_ParseToken_QuakeC(&text, false))
1609                 {
1610                         memset (&shader, 0, sizeof(shader));
1611                         shader.name[0] = 0;
1612                         shader.surfaceparms = 0;
1613                         shader.surfaceflags = 0;
1614                         shader.textureflags = 0;
1615                         shader.numlayers = 0;
1616                         shader.lighting = false;
1617                         shader.vertexalpha = false;
1618                         shader.textureblendalpha = false;
1619                         shader.skyboxname[0] = 0;
1620                         shader.deforms[0].deform = Q3DEFORM_NONE;
1621                         shader.dpnortlight = false;
1622                         shader.dpshadow = false;
1623                         shader.dpnoshadow = false;
1624                         shader.dpmeshcollisions = false;
1625                         shader.dpshaderkill = false;
1626                         shader.dpreflectcube[0] = 0;
1627                         shader.reflectmin = 0;
1628                         shader.reflectmax = 1;
1629                         shader.refractfactor = 1;
1630                         Vector4Set(shader.refractcolor4f, 1, 1, 1, 1);
1631                         shader.reflectfactor = 1;
1632                         Vector4Set(shader.reflectcolor4f, 1, 1, 1, 1);
1633                         shader.r_water_wateralpha = 1;
1634                         shader.r_water_waterscroll[0] = 0;
1635                         shader.r_water_waterscroll[1] = 0;
1636                         shader.offsetmapping = (mod_q3shader_default_offsetmapping.value) ? OFFSETMAPPING_DEFAULT : OFFSETMAPPING_OFF;
1637                         shader.offsetscale = mod_q3shader_default_offsetmapping_scale.value;
1638                         shader.offsetbias = mod_q3shader_default_offsetmapping_bias.value;
1639                         shader.biaspolygonoffset = mod_q3shader_default_polygonoffset.value;
1640                         shader.biaspolygonfactor = mod_q3shader_default_polygonfactor.value;
1641                         shader.transparentsort = TRANSPARENTSORT_DISTANCE;
1642                         shader.specularscalemod = 1;
1643                         shader.specularpowermod = 1;
1644                         shader.rtlightambient = 0;
1645                         // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
1646                         // JUST GREP FOR "specularscalemod = 1".
1647
1648                         strlcpy(shader.name, com_token, sizeof(shader.name));
1649                         if (!COM_ParseToken_QuakeC(&text, false) || strcasecmp(com_token, "{"))
1650                         {
1651                                 Con_DPrintf("%s parsing error - expected \"{\", found \"%s\"\n", search->filenames[fileindex], com_token);
1652                                 break;
1653                         }
1654                         while (COM_ParseToken_QuakeC(&text, false))
1655                         {
1656                                 if (!strcasecmp(com_token, "}"))
1657                                         break;
1658                                 if (!strcasecmp(com_token, "{"))
1659                                 {
1660                                         static q3shaderinfo_layer_t dummy;
1661                                         if (shader.numlayers < Q3SHADER_MAXLAYERS)
1662                                         {
1663                                                 layer = shader.layers + shader.numlayers++;
1664                                         }
1665                                         else
1666                                         {
1667                                                 // parse and process it anyway, just don't store it (so a map $lightmap or such stuff still is found)
1668                                                 memset(&dummy, 0, sizeof(dummy));
1669                                                 layer = &dummy;
1670                                         }
1671                                         layer->rgbgen.rgbgen = Q3RGBGEN_IDENTITY;
1672                                         layer->alphagen.alphagen = Q3ALPHAGEN_IDENTITY;
1673                                         layer->tcgen.tcgen = Q3TCGEN_TEXTURE;
1674                                         layer->blendfunc[0] = GL_ONE;
1675                                         layer->blendfunc[1] = GL_ZERO;
1676                                         while (COM_ParseToken_QuakeC(&text, false))
1677                                         {
1678                                                 if (!strcasecmp(com_token, "}"))
1679                                                         break;
1680                                                 if (!strcasecmp(com_token, "\n"))
1681                                                         continue;
1682                                                 numparameters = 0;
1683                                                 for (j = 0;strcasecmp(com_token, "\n") && strcasecmp(com_token, "}");j++)
1684                                                 {
1685                                                         if (j < TEXTURE_MAXFRAMES + 4)
1686                                                         {
1687                                                                 // remap dp_water to dpwater, dp_reflect to dpreflect, etc.
1688                                                                 if(j == 0 && !strncasecmp(com_token, "dp_", 3))
1689                                                                         dpsnprintf(parameter[j], sizeof(parameter[j]), "dp%s", &com_token[3]);
1690                                                                 else
1691                                                                         strlcpy(parameter[j], com_token, sizeof(parameter[j]));
1692                                                                 numparameters = j + 1;
1693                                                         }
1694                                                         if (!COM_ParseToken_QuakeC(&text, true))
1695                                                                 break;
1696                                                 }
1697                                                 //for (j = numparameters;j < TEXTURE_MAXFRAMES + 4;j++)
1698                                                 //      parameter[j][0] = 0;
1699                                                 if (developer_insane.integer)
1700                                                 {
1701                                                         Con_DPrintf("%s %i: ", shader.name, shader.numlayers - 1);
1702                                                         for (j = 0;j < numparameters;j++)
1703                                                                 Con_DPrintf(" %s", parameter[j]);
1704                                                         Con_DPrint("\n");
1705                                                 }
1706                                                 if (numparameters >= 2 && !strcasecmp(parameter[0], "blendfunc"))
1707                                                 {
1708                                                         if (numparameters == 2)
1709                                                         {
1710                                                                 if (!strcasecmp(parameter[1], "add"))
1711                                                                 {
1712                                                                         layer->blendfunc[0] = GL_ONE;
1713                                                                         layer->blendfunc[1] = GL_ONE;
1714                                                                 }
1715                                                                 else if (!strcasecmp(parameter[1], "addalpha"))
1716                                                                 {
1717                                                                         layer->blendfunc[0] = GL_SRC_ALPHA;
1718                                                                         layer->blendfunc[1] = GL_ONE;
1719                                                                 }
1720                                                                 else if (!strcasecmp(parameter[1], "filter"))
1721                                                                 {
1722                                                                         layer->blendfunc[0] = GL_DST_COLOR;
1723                                                                         layer->blendfunc[1] = GL_ZERO;
1724                                                                 }
1725                                                                 else if (!strcasecmp(parameter[1], "blend"))
1726                                                                 {
1727                                                                         layer->blendfunc[0] = GL_SRC_ALPHA;
1728                                                                         layer->blendfunc[1] = GL_ONE_MINUS_SRC_ALPHA;
1729                                                                 }
1730                                                         }
1731                                                         else if (numparameters == 3)
1732                                                         {
1733                                                                 int k;
1734                                                                 for (k = 0;k < 2;k++)
1735                                                                 {
1736                                                                         if (!strcasecmp(parameter[k+1], "GL_ONE"))
1737                                                                                 layer->blendfunc[k] = GL_ONE;
1738                                                                         else if (!strcasecmp(parameter[k+1], "GL_ZERO"))
1739                                                                                 layer->blendfunc[k] = GL_ZERO;
1740                                                                         else if (!strcasecmp(parameter[k+1], "GL_SRC_COLOR"))
1741                                                                                 layer->blendfunc[k] = GL_SRC_COLOR;
1742                                                                         else if (!strcasecmp(parameter[k+1], "GL_SRC_ALPHA"))
1743                                                                                 layer->blendfunc[k] = GL_SRC_ALPHA;
1744                                                                         else if (!strcasecmp(parameter[k+1], "GL_DST_COLOR"))
1745                                                                                 layer->blendfunc[k] = GL_DST_COLOR;
1746                                                                         else if (!strcasecmp(parameter[k+1], "GL_DST_ALPHA"))
1747                                                                                 layer->blendfunc[k] = GL_DST_ALPHA;
1748                                                                         else if (!strcasecmp(parameter[k+1], "GL_ONE_MINUS_SRC_COLOR"))
1749                                                                                 layer->blendfunc[k] = GL_ONE_MINUS_SRC_COLOR;
1750                                                                         else if (!strcasecmp(parameter[k+1], "GL_ONE_MINUS_SRC_ALPHA"))
1751                                                                                 layer->blendfunc[k] = GL_ONE_MINUS_SRC_ALPHA;
1752                                                                         else if (!strcasecmp(parameter[k+1], "GL_ONE_MINUS_DST_COLOR"))
1753                                                                                 layer->blendfunc[k] = GL_ONE_MINUS_DST_COLOR;
1754                                                                         else if (!strcasecmp(parameter[k+1], "GL_ONE_MINUS_DST_ALPHA"))
1755                                                                                 layer->blendfunc[k] = GL_ONE_MINUS_DST_ALPHA;
1756                                                                         else
1757                                                                                 layer->blendfunc[k] = GL_ONE; // default in case of parsing error
1758                                                                 }
1759                                                         }
1760                                                 }
1761                                                 if (numparameters >= 2 && !strcasecmp(parameter[0], "alphafunc"))
1762                                                         layer->alphatest = true;
1763                                                 if (numparameters >= 2 && (!strcasecmp(parameter[0], "map") || !strcasecmp(parameter[0], "clampmap")))
1764                                                 {
1765                                                         if (!strcasecmp(parameter[0], "clampmap"))
1766                                                                 layer->clampmap = true;
1767                                                         layer->numframes = 1;
1768                                                         layer->framerate = 1;
1769                                                         layer->texturename = (char**)Mem_ExpandableArray_AllocRecord (
1770                                                                 &q3shader_data->char_ptrs);
1771                                                         layer->texturename[0] = Mem_strdup (q3shaders_mem, parameter[1]);
1772                                                         if (!strcasecmp(parameter[1], "$lightmap"))
1773                                                                 shader.lighting = true;
1774                                                 }
1775                                                 else if (numparameters >= 3 && (!strcasecmp(parameter[0], "animmap") || !strcasecmp(parameter[0], "animclampmap")))
1776                                                 {
1777                                                         int i;
1778                                                         layer->numframes = min(numparameters - 2, TEXTURE_MAXFRAMES);
1779                                                         layer->framerate = atof(parameter[1]);
1780                                                         layer->texturename = (char **) Mem_Alloc (q3shaders_mem, sizeof (char*) * layer->numframes);
1781                                                         for (i = 0;i < layer->numframes;i++)
1782                                                                 layer->texturename[i] = Mem_strdup (q3shaders_mem, parameter[i + 2]);
1783                                                 }
1784                                                 else if (numparameters >= 2 && !strcasecmp(parameter[0], "rgbgen"))
1785                                                 {
1786                                                         int i;
1787                                                         for (i = 0;i < numparameters - 2 && i < Q3RGBGEN_MAXPARMS;i++)
1788                                                                 layer->rgbgen.parms[i] = atof(parameter[i+2]);
1789                                                              if (!strcasecmp(parameter[1], "identity"))         layer->rgbgen.rgbgen = Q3RGBGEN_IDENTITY;
1790                                                         else if (!strcasecmp(parameter[1], "const"))            layer->rgbgen.rgbgen = Q3RGBGEN_CONST;
1791                                                         else if (!strcasecmp(parameter[1], "entity"))           layer->rgbgen.rgbgen = Q3RGBGEN_ENTITY;
1792                                                         else if (!strcasecmp(parameter[1], "exactvertex"))      layer->rgbgen.rgbgen = Q3RGBGEN_EXACTVERTEX;
1793                                                         else if (!strcasecmp(parameter[1], "identitylighting")) layer->rgbgen.rgbgen = Q3RGBGEN_IDENTITYLIGHTING;
1794                                                         else if (!strcasecmp(parameter[1], "lightingdiffuse"))  layer->rgbgen.rgbgen = Q3RGBGEN_LIGHTINGDIFFUSE;
1795                                                         else if (!strcasecmp(parameter[1], "oneminusentity"))   layer->rgbgen.rgbgen = Q3RGBGEN_ONEMINUSENTITY;
1796                                                         else if (!strcasecmp(parameter[1], "oneminusvertex"))   layer->rgbgen.rgbgen = Q3RGBGEN_ONEMINUSVERTEX;
1797                                                         else if (!strcasecmp(parameter[1], "vertex"))           layer->rgbgen.rgbgen = Q3RGBGEN_VERTEX;
1798                                                         else if (!strcasecmp(parameter[1], "wave"))
1799                                                         {
1800                                                                 layer->rgbgen.rgbgen = Q3RGBGEN_WAVE;
1801                                                                 layer->rgbgen.wavefunc = Mod_LoadQ3Shaders_EnumerateWaveFunc(parameter[2]);
1802                                                                 for (i = 0;i < numparameters - 3 && i < Q3WAVEPARMS;i++)
1803                                                                         layer->rgbgen.waveparms[i] = atof(parameter[i+3]);
1804                                                         }
1805                                                         else Con_DPrintf("%s parsing warning: unknown rgbgen %s\n", search->filenames[fileindex], parameter[1]);
1806                                                 }
1807                                                 else if (numparameters >= 2 && !strcasecmp(parameter[0], "alphagen"))
1808                                                 {
1809                                                         int i;
1810                                                         for (i = 0;i < numparameters - 2 && i < Q3ALPHAGEN_MAXPARMS;i++)
1811                                                                 layer->alphagen.parms[i] = atof(parameter[i+2]);
1812                                                              if (!strcasecmp(parameter[1], "identity"))         layer->alphagen.alphagen = Q3ALPHAGEN_IDENTITY;
1813                                                         else if (!strcasecmp(parameter[1], "const"))            layer->alphagen.alphagen = Q3ALPHAGEN_CONST;
1814                                                         else if (!strcasecmp(parameter[1], "entity"))           layer->alphagen.alphagen = Q3ALPHAGEN_ENTITY;
1815                                                         else if (!strcasecmp(parameter[1], "lightingspecular")) layer->alphagen.alphagen = Q3ALPHAGEN_LIGHTINGSPECULAR;
1816                                                         else if (!strcasecmp(parameter[1], "oneminusentity"))   layer->alphagen.alphagen = Q3ALPHAGEN_ONEMINUSENTITY;
1817                                                         else if (!strcasecmp(parameter[1], "oneminusvertex"))   layer->alphagen.alphagen = Q3ALPHAGEN_ONEMINUSVERTEX;
1818                                                         else if (!strcasecmp(parameter[1], "portal"))           layer->alphagen.alphagen = Q3ALPHAGEN_PORTAL;
1819                                                         else if (!strcasecmp(parameter[1], "vertex"))           layer->alphagen.alphagen = Q3ALPHAGEN_VERTEX;
1820                                                         else if (!strcasecmp(parameter[1], "wave"))
1821                                                         {
1822                                                                 layer->alphagen.alphagen = Q3ALPHAGEN_WAVE;
1823                                                                 layer->alphagen.wavefunc = Mod_LoadQ3Shaders_EnumerateWaveFunc(parameter[2]);
1824                                                                 for (i = 0;i < numparameters - 3 && i < Q3WAVEPARMS;i++)
1825                                                                         layer->alphagen.waveparms[i] = atof(parameter[i+3]);
1826                                                         }
1827                                                         else Con_DPrintf("%s parsing warning: unknown alphagen %s\n", search->filenames[fileindex], parameter[1]);
1828                                                 }
1829                                                 else if (numparameters >= 2 && (!strcasecmp(parameter[0], "texgen") || !strcasecmp(parameter[0], "tcgen")))
1830                                                 {
1831                                                         int i;
1832                                                         // observed values: tcgen environment
1833                                                         // no other values have been observed in real shaders
1834                                                         for (i = 0;i < numparameters - 2 && i < Q3TCGEN_MAXPARMS;i++)
1835                                                                 layer->tcgen.parms[i] = atof(parameter[i+2]);
1836                                                              if (!strcasecmp(parameter[1], "base"))        layer->tcgen.tcgen = Q3TCGEN_TEXTURE;
1837                                                         else if (!strcasecmp(parameter[1], "texture"))     layer->tcgen.tcgen = Q3TCGEN_TEXTURE;
1838                                                         else if (!strcasecmp(parameter[1], "environment")) layer->tcgen.tcgen = Q3TCGEN_ENVIRONMENT;
1839                                                         else if (!strcasecmp(parameter[1], "lightmap"))    layer->tcgen.tcgen = Q3TCGEN_LIGHTMAP;
1840                                                         else if (!strcasecmp(parameter[1], "vector"))      layer->tcgen.tcgen = Q3TCGEN_VECTOR;
1841                                                         else Con_DPrintf("%s parsing warning: unknown tcgen mode %s\n", search->filenames[fileindex], parameter[1]);
1842                                                 }
1843                                                 else if (numparameters >= 2 && !strcasecmp(parameter[0], "tcmod"))
1844                                                 {
1845                                                         int i, tcmodindex;
1846                                                         // observed values:
1847                                                         // tcmod rotate #
1848                                                         // tcmod scale # #
1849                                                         // tcmod scroll # #
1850                                                         // tcmod stretch sin # # # #
1851                                                         // tcmod stretch triangle # # # #
1852                                                         // tcmod transform # # # # # #
1853                                                         // tcmod turb # # # #
1854                                                         // tcmod turb sin # # # #  (this is bogus)
1855                                                         // no other values have been observed in real shaders
1856                                                         for (tcmodindex = 0;tcmodindex < Q3MAXTCMODS;tcmodindex++)
1857                                                                 if (!layer->tcmods[tcmodindex].tcmod)
1858                                                                         break;
1859                                                         if (tcmodindex < Q3MAXTCMODS)
1860                                                         {
1861                                                                 for (i = 0;i < numparameters - 2 && i < Q3TCMOD_MAXPARMS;i++)
1862                                                                         layer->tcmods[tcmodindex].parms[i] = atof(parameter[i+2]);
1863                                                                          if (!strcasecmp(parameter[1], "entitytranslate")) layer->tcmods[tcmodindex].tcmod = Q3TCMOD_ENTITYTRANSLATE;
1864                                                                 else if (!strcasecmp(parameter[1], "rotate"))          layer->tcmods[tcmodindex].tcmod = Q3TCMOD_ROTATE;
1865                                                                 else if (!strcasecmp(parameter[1], "scale"))           layer->tcmods[tcmodindex].tcmod = Q3TCMOD_SCALE;
1866                                                                 else if (!strcasecmp(parameter[1], "scroll"))          layer->tcmods[tcmodindex].tcmod = Q3TCMOD_SCROLL;
1867                                                                 else if (!strcasecmp(parameter[1], "page"))            layer->tcmods[tcmodindex].tcmod = Q3TCMOD_PAGE;
1868                                                                 else if (!strcasecmp(parameter[1], "stretch"))
1869                                                                 {
1870                                                                         layer->tcmods[tcmodindex].tcmod = Q3TCMOD_STRETCH;
1871                                                                         layer->tcmods[tcmodindex].wavefunc = Mod_LoadQ3Shaders_EnumerateWaveFunc(parameter[2]);
1872                                                                         for (i = 0;i < numparameters - 3 && i < Q3WAVEPARMS;i++)
1873                                                                                 layer->tcmods[tcmodindex].waveparms[i] = atof(parameter[i+3]);
1874                                                                 }
1875                                                                 else if (!strcasecmp(parameter[1], "transform"))       layer->tcmods[tcmodindex].tcmod = Q3TCMOD_TRANSFORM;
1876                                                                 else if (!strcasecmp(parameter[1], "turb"))            layer->tcmods[tcmodindex].tcmod = Q3TCMOD_TURBULENT;
1877                                                                 else Con_DPrintf("%s parsing warning: unknown tcmod mode %s\n", search->filenames[fileindex], parameter[1]);
1878                                                         }
1879                                                         else
1880                                                                 Con_DPrintf("%s parsing warning: too many tcmods on one layer\n", search->filenames[fileindex]);
1881                                                 }
1882                                                 // break out a level if it was a closing brace (not using the character here to not confuse vim)
1883                                                 if (!strcasecmp(com_token, "}"))
1884                                                         break;
1885                                         }
1886                                         if (layer->rgbgen.rgbgen == Q3RGBGEN_LIGHTINGDIFFUSE || layer->rgbgen.rgbgen == Q3RGBGEN_VERTEX)
1887                                                 shader.lighting = true;
1888                                         if (layer->alphagen.alphagen == Q3ALPHAGEN_VERTEX)
1889                                         {
1890                                                 if (layer == shader.layers + 0)
1891                                                 {
1892                                                         // vertex controlled transparency
1893                                                         shader.vertexalpha = true;
1894                                                 }
1895                                                 else
1896                                                 {
1897                                                         // multilayer terrain shader or similar
1898                                                         shader.textureblendalpha = true;
1899                                                         if (mod_q3shader_force_terrain_alphaflag.integer)
1900                                                                 shader.layers[0].dptexflags |= TEXF_ALPHA;
1901                                                 }
1902                                         }
1903
1904                                         if(mod_q3shader_force_addalpha.integer)
1905                                         {
1906                                                 // for a long while, DP treated GL_ONE GL_ONE as GL_SRC_ALPHA GL_ONE
1907                                                 // this cvar brings back this behaviour
1908                                                 if(layer->blendfunc[0] == GL_ONE && layer->blendfunc[1] == GL_ONE)
1909                                                         layer->blendfunc[0] = GL_SRC_ALPHA;
1910                                         }
1911                                         
1912                                         layer->dptexflags = 0;
1913                                         if (layer->alphatest)
1914                                                 layer->dptexflags |= TEXF_ALPHA;
1915                                         switch(layer->blendfunc[0])
1916                                         {
1917                                                 case GL_SRC_ALPHA:
1918                                                 case GL_ONE_MINUS_SRC_ALPHA:
1919                                                         layer->dptexflags |= TEXF_ALPHA;
1920                                                         break;
1921                                         }
1922                                         switch(layer->blendfunc[1])
1923                                         {
1924                                                 case GL_SRC_ALPHA:
1925                                                 case GL_ONE_MINUS_SRC_ALPHA:
1926                                                         layer->dptexflags |= TEXF_ALPHA;
1927                                                         break;
1928                                         }
1929                                         if (!(shader.surfaceparms & Q3SURFACEPARM_NOMIPMAPS))
1930                                                 layer->dptexflags |= TEXF_MIPMAP;
1931                                         if (!(shader.textureflags & Q3TEXTUREFLAG_NOPICMIP))
1932                                                 layer->dptexflags |= TEXF_PICMIP | TEXF_COMPRESS;
1933                                         if (layer->clampmap)
1934                                                 layer->dptexflags |= TEXF_CLAMP;
1935                                         continue;
1936                                 }
1937                                 numparameters = 0;
1938                                 for (j = 0;strcasecmp(com_token, "\n") && strcasecmp(com_token, "}");j++)
1939                                 {
1940                                         if (j < TEXTURE_MAXFRAMES + 4)
1941                                         {
1942                                                 // remap dp_water to dpwater, dp_reflect to dpreflect, etc.
1943                                                 if(j == 0 && !strncasecmp(com_token, "dp_", 3))
1944                                                         dpsnprintf(parameter[j], sizeof(parameter[j]), "dp%s", &com_token[3]);
1945                                                 else
1946                                                         strlcpy(parameter[j], com_token, sizeof(parameter[j]));
1947                                                 numparameters = j + 1;
1948                                         }
1949                                         if (!COM_ParseToken_QuakeC(&text, true))
1950                                                 break;
1951                                 }
1952                                 //for (j = numparameters;j < TEXTURE_MAXFRAMES + 4;j++)
1953                                 //      parameter[j][0] = 0;
1954                                 if (fileindex == 0 && !strcasecmp(com_token, "}"))
1955                                         break;
1956                                 if (developer_insane.integer)
1957                                 {
1958                                         Con_DPrintf("%s: ", shader.name);
1959                                         for (j = 0;j < numparameters;j++)
1960                                                 Con_DPrintf(" %s", parameter[j]);
1961                                         Con_DPrint("\n");
1962                                 }
1963                                 if (numparameters < 1)
1964                                         continue;
1965                                 if (!strcasecmp(parameter[0], "surfaceparm") && numparameters >= 2)
1966                                 {
1967                                         if (!strcasecmp(parameter[1], "alphashadow"))
1968                                                 shader.surfaceparms |= Q3SURFACEPARM_ALPHASHADOW;
1969                                         else if (!strcasecmp(parameter[1], "areaportal"))
1970                                                 shader.surfaceparms |= Q3SURFACEPARM_AREAPORTAL;
1971                                         else if (!strcasecmp(parameter[1], "botclip"))
1972                                                 shader.surfaceparms |= Q3SURFACEPARM_BOTCLIP;
1973                                         else if (!strcasecmp(parameter[1], "clusterportal"))
1974                                                 shader.surfaceparms |= Q3SURFACEPARM_CLUSTERPORTAL;
1975                                         else if (!strcasecmp(parameter[1], "detail"))
1976                                                 shader.surfaceparms |= Q3SURFACEPARM_DETAIL;
1977                                         else if (!strcasecmp(parameter[1], "donotenter"))
1978                                                 shader.surfaceparms |= Q3SURFACEPARM_DONOTENTER;
1979                                         else if (!strcasecmp(parameter[1], "dust"))
1980                                                 shader.surfaceparms |= Q3SURFACEPARM_DUST;
1981                                         else if (!strcasecmp(parameter[1], "hint"))
1982                                                 shader.surfaceparms |= Q3SURFACEPARM_HINT;
1983                                         else if (!strcasecmp(parameter[1], "fog"))
1984                                                 shader.surfaceparms |= Q3SURFACEPARM_FOG;
1985                                         else if (!strcasecmp(parameter[1], "lava"))
1986                                                 shader.surfaceparms |= Q3SURFACEPARM_LAVA;
1987                                         else if (!strcasecmp(parameter[1], "lightfilter"))
1988                                                 shader.surfaceparms |= Q3SURFACEPARM_LIGHTFILTER;
1989                                         else if (!strcasecmp(parameter[1], "lightgrid"))
1990                                                 shader.surfaceparms |= Q3SURFACEPARM_LIGHTGRID;
1991                                         else if (!strcasecmp(parameter[1], "metalsteps"))
1992                                                 shader.surfaceparms |= Q3SURFACEPARM_METALSTEPS;
1993                                         else if (!strcasecmp(parameter[1], "nodamage"))
1994                                                 shader.surfaceparms |= Q3SURFACEPARM_NODAMAGE;
1995                                         else if (!strcasecmp(parameter[1], "nodlight"))
1996                                                 shader.surfaceparms |= Q3SURFACEPARM_NODLIGHT;
1997                                         else if (!strcasecmp(parameter[1], "nodraw"))
1998                                                 shader.surfaceparms |= Q3SURFACEPARM_NODRAW;
1999                                         else if (!strcasecmp(parameter[1], "nodrop"))
2000                                                 shader.surfaceparms |= Q3SURFACEPARM_NODROP;
2001                                         else if (!strcasecmp(parameter[1], "noimpact"))
2002                                                 shader.surfaceparms |= Q3SURFACEPARM_NOIMPACT;
2003                                         else if (!strcasecmp(parameter[1], "nolightmap"))
2004                                                 shader.surfaceparms |= Q3SURFACEPARM_NOLIGHTMAP;
2005                                         else if (!strcasecmp(parameter[1], "nomarks"))
2006                                                 shader.surfaceparms |= Q3SURFACEPARM_NOMARKS;
2007                                         else if (!strcasecmp(parameter[1], "nomipmaps"))
2008                                                 shader.surfaceparms |= Q3SURFACEPARM_NOMIPMAPS;
2009                                         else if (!strcasecmp(parameter[1], "nonsolid"))
2010                                                 shader.surfaceparms |= Q3SURFACEPARM_NONSOLID;
2011                                         else if (!strcasecmp(parameter[1], "origin"))
2012                                                 shader.surfaceparms |= Q3SURFACEPARM_ORIGIN;
2013                                         else if (!strcasecmp(parameter[1], "playerclip"))
2014                                                 shader.surfaceparms |= Q3SURFACEPARM_PLAYERCLIP;
2015                                         else if (!strcasecmp(parameter[1], "sky"))
2016                                                 shader.surfaceparms |= Q3SURFACEPARM_SKY;
2017                                         else if (!strcasecmp(parameter[1], "slick"))
2018                                                 shader.surfaceparms |= Q3SURFACEPARM_SLICK;
2019                                         else if (!strcasecmp(parameter[1], "slime"))
2020                                                 shader.surfaceparms |= Q3SURFACEPARM_SLIME;
2021                                         else if (!strcasecmp(parameter[1], "structural"))
2022                                                 shader.surfaceparms |= Q3SURFACEPARM_STRUCTURAL;
2023                                         else if (!strcasecmp(parameter[1], "trans"))
2024                                                 shader.surfaceparms |= Q3SURFACEPARM_TRANS;
2025                                         else if (!strcasecmp(parameter[1], "water"))
2026                                                 shader.surfaceparms |= Q3SURFACEPARM_WATER;
2027                                         else if (!strcasecmp(parameter[1], "pointlight"))
2028                                                 shader.surfaceparms |= Q3SURFACEPARM_POINTLIGHT;
2029                                         else if (!strcasecmp(parameter[1], "antiportal"))
2030                                                 shader.surfaceparms |= Q3SURFACEPARM_ANTIPORTAL;
2031                                         else if (!strcasecmp(parameter[1], "skip"))
2032                                                 ; // shader.surfaceparms |= Q3SURFACEPARM_SKIP; FIXME we don't have enough #defines for this any more, and the engine doesn't need this one anyway
2033                                         else
2034                                         {
2035                                                 // try custom surfaceparms
2036                                                 for (j = 0; j < numcustsurfaceflags; j++)
2037                                                 {
2038                                                         if (!strcasecmp(custsurfaceparmnames[j], parameter[1]))
2039                                                         {
2040                                                                 shader.surfaceflags |= custsurfaceflags[j];
2041                                                                 break;
2042                                                         }
2043                                                 }
2044                                                 // failed all
2045                                                 if (j == numcustsurfaceflags)
2046                                                         Con_DPrintf("%s parsing warning: unknown surfaceparm \"%s\"\n", search->filenames[fileindex], parameter[1]);
2047                                         }
2048                                 }
2049                                 else if (!strcasecmp(parameter[0], "dpshadow"))
2050                                         shader.dpshadow = true;
2051                                 else if (!strcasecmp(parameter[0], "dpnoshadow"))
2052                                         shader.dpnoshadow = true;
2053                                 else if (!strcasecmp(parameter[0], "dpnortlight"))
2054                                         shader.dpnortlight = true;
2055                                 else if (!strcasecmp(parameter[0], "dpreflectcube"))
2056                                         strlcpy(shader.dpreflectcube, parameter[1], sizeof(shader.dpreflectcube));
2057                                 else if (!strcasecmp(parameter[0], "dpmeshcollisions"))
2058                                         shader.dpmeshcollisions = true;
2059                                 // this sets dpshaderkill to true if dpshaderkillifcvarzero was used, and to false if dpnoshaderkillifcvarzero was used
2060                                 else if (((dpshaderkill = !strcasecmp(parameter[0], "dpshaderkillifcvarzero")) || !strcasecmp(parameter[0], "dpnoshaderkillifcvarzero")) && numparameters >= 2)
2061                                 {
2062                                         if (Cvar_VariableValue(parameter[1]) == 0.0f)
2063                                                 shader.dpshaderkill = dpshaderkill;
2064                                 }
2065                                 // this sets dpshaderkill to true if dpshaderkillifcvar was used, and to false if dpnoshaderkillifcvar was used
2066                                 else if (((dpshaderkill = !strcasecmp(parameter[0], "dpshaderkillifcvar")) || !strcasecmp(parameter[0], "dpnoshaderkillifcvar")) && numparameters >= 2)
2067                                 {
2068                                         const char *op = NULL;
2069                                         if (numparameters >= 3)
2070                                                 op = parameter[2];
2071                                         if(!op)
2072                                         {
2073                                                 if (Cvar_VariableValue(parameter[1]) != 0.0f)
2074                                                         shader.dpshaderkill = dpshaderkill;
2075                                         }
2076                                         else if (numparameters >= 4 && !strcmp(op, "=="))
2077                                         {
2078                                                 if (Cvar_VariableValue(parameter[1]) == atof(parameter[3]))
2079                                                         shader.dpshaderkill = dpshaderkill;
2080                                         }
2081                                         else if (numparameters >= 4 && !strcmp(op, "!="))
2082                                         {
2083                                                 if (Cvar_VariableValue(parameter[1]) != atof(parameter[3]))
2084                                                         shader.dpshaderkill = dpshaderkill;
2085                                         }
2086                                         else if (numparameters >= 4 && !strcmp(op, ">"))
2087                                         {
2088                                                 if (Cvar_VariableValue(parameter[1]) > atof(parameter[3]))
2089                                                         shader.dpshaderkill = dpshaderkill;
2090                                         }
2091                                         else if (numparameters >= 4 && !strcmp(op, "<"))
2092                                         {
2093                                                 if (Cvar_VariableValue(parameter[1]) < atof(parameter[3]))
2094                                                         shader.dpshaderkill = dpshaderkill;
2095                                         }
2096                                         else if (numparameters >= 4 && !strcmp(op, ">="))
2097                                         {
2098                                                 if (Cvar_VariableValue(parameter[1]) >= atof(parameter[3]))
2099                                                         shader.dpshaderkill = dpshaderkill;
2100                                         }
2101                                         else if (numparameters >= 4 && !strcmp(op, "<="))
2102                                         {
2103                                                 if (Cvar_VariableValue(parameter[1]) <= atof(parameter[3]))
2104                                                         shader.dpshaderkill = dpshaderkill;
2105                                         }
2106                                         else
2107                                         {
2108                                                 Con_DPrintf("%s parsing warning: unknown dpshaderkillifcvar op \"%s\", or not enough arguments\n", search->filenames[fileindex], op);
2109                                         }
2110                                 }
2111                                 else if (!strcasecmp(parameter[0], "sky") && numparameters >= 2)
2112                                 {
2113                                         // some q3 skies don't have the sky parm set
2114                                         shader.surfaceparms |= Q3SURFACEPARM_SKY;
2115                                         strlcpy(shader.skyboxname, parameter[1], sizeof(shader.skyboxname));
2116                                 }
2117                                 else if (!strcasecmp(parameter[0], "skyparms") && numparameters >= 2)
2118                                 {
2119                                         // some q3 skies don't have the sky parm set
2120                                         shader.surfaceparms |= Q3SURFACEPARM_SKY;
2121                                         if (!atoi(parameter[1]) && strcasecmp(parameter[1], "-"))
2122                                                 strlcpy(shader.skyboxname, parameter[1], sizeof(shader.skyboxname));
2123                                 }
2124                                 else if (!strcasecmp(parameter[0], "cull") && numparameters >= 2)
2125                                 {
2126                                         if (!strcasecmp(parameter[1], "disable") || !strcasecmp(parameter[1], "none") || !strcasecmp(parameter[1], "twosided"))
2127                                                 shader.textureflags |= Q3TEXTUREFLAG_TWOSIDED;
2128                                 }
2129                                 else if (!strcasecmp(parameter[0], "nomipmaps"))
2130                                         shader.surfaceparms |= Q3SURFACEPARM_NOMIPMAPS;
2131                                 else if (!strcasecmp(parameter[0], "nopicmip"))
2132                                         shader.textureflags |= Q3TEXTUREFLAG_NOPICMIP;
2133                                 else if (!strcasecmp(parameter[0], "polygonoffset"))
2134                                         shader.textureflags |= Q3TEXTUREFLAG_POLYGONOFFSET;
2135                                 else if (!strcasecmp(parameter[0], "dppolygonoffset"))
2136                                 {
2137                                         shader.textureflags |= Q3TEXTUREFLAG_POLYGONOFFSET;
2138                                         if(numparameters >= 2)
2139                                         {
2140                                                 shader.biaspolygonfactor = atof(parameter[1]);
2141                                                 if(numparameters >= 3)
2142                                                         shader.biaspolygonoffset = atof(parameter[2]);
2143                                                 else
2144                                                         shader.biaspolygonoffset = 0;
2145                                         }
2146                                 }
2147                                 else if (!strcasecmp(parameter[0], "dptransparentsort") && numparameters >= 2)
2148                                 {
2149                                         shader.textureflags |= Q3TEXTUREFLAG_TRANSPARENTSORT;
2150                                         if (!strcasecmp(parameter[1], "sky"))
2151                                                 shader.transparentsort = TRANSPARENTSORT_SKY;
2152                                         else if (!strcasecmp(parameter[1], "distance"))
2153                                                 shader.transparentsort = TRANSPARENTSORT_DISTANCE;
2154                                         else if (!strcasecmp(parameter[1], "hud"))
2155                                                 shader.transparentsort = TRANSPARENTSORT_HUD;
2156                                         else
2157                                                 Con_DPrintf("%s parsing warning: unknown dptransparentsort category \"%s\", or not enough arguments\n", search->filenames[fileindex], parameter[1]);
2158                                 }
2159                                 else if (!strcasecmp(parameter[0], "dprefract") && numparameters >= 5)
2160                                 {
2161                                         shader.textureflags |= Q3TEXTUREFLAG_REFRACTION;
2162                                         shader.refractfactor = atof(parameter[1]);
2163                                         Vector4Set(shader.refractcolor4f, atof(parameter[2]), atof(parameter[3]), atof(parameter[4]), 1);
2164                                 }
2165                                 else if (!strcasecmp(parameter[0], "dpreflect") && numparameters >= 6)
2166                                 {
2167                                         shader.textureflags |= Q3TEXTUREFLAG_REFLECTION;
2168                                         shader.reflectfactor = atof(parameter[1]);
2169                                         Vector4Set(shader.reflectcolor4f, atof(parameter[2]), atof(parameter[3]), atof(parameter[4]), atof(parameter[5]));
2170                                 }
2171                                 else if (!strcasecmp(parameter[0], "dpcamera"))
2172                                 {
2173                                         shader.textureflags |= Q3TEXTUREFLAG_CAMERA;
2174                                 }
2175                                 else if (!strcasecmp(parameter[0], "dpwater") && numparameters >= 12)
2176                                 {
2177                                         shader.textureflags |= Q3TEXTUREFLAG_WATERSHADER;
2178                                         shader.reflectmin = atof(parameter[1]);
2179                                         shader.reflectmax = atof(parameter[2]);
2180                                         shader.refractfactor = atof(parameter[3]);
2181                                         shader.reflectfactor = atof(parameter[4]);
2182                                         Vector4Set(shader.refractcolor4f, atof(parameter[5]), atof(parameter[6]), atof(parameter[7]), 1);
2183                                         Vector4Set(shader.reflectcolor4f, atof(parameter[8]), atof(parameter[9]), atof(parameter[10]), 1);
2184                                         shader.r_water_wateralpha = atof(parameter[11]);
2185                                 }
2186                                 else if (!strcasecmp(parameter[0], "dpwaterscroll") && numparameters >= 3)
2187                                 {
2188                                         shader.r_water_waterscroll[0] = 1/atof(parameter[1]);
2189                                         shader.r_water_waterscroll[1] = 1/atof(parameter[2]);
2190                                 }
2191                                 else if (!strcasecmp(parameter[0], "dpglossintensitymod") && numparameters >= 2)
2192                                 {
2193                                         shader.specularscalemod = atof(parameter[1]);
2194                                 }
2195                                 else if (!strcasecmp(parameter[0], "dpglossexponentmod") && numparameters >= 2)
2196                                 {
2197                                         shader.specularpowermod = atof(parameter[1]);
2198                                 }
2199                                 else if (!strcasecmp(parameter[0], "dprtlightambient") && numparameters >= 2)
2200                                 {
2201                                         shader.rtlightambient = atof(parameter[1]);
2202                                 }
2203                                 else if (!strcasecmp(parameter[0], "dpoffsetmapping") && numparameters >= 2)
2204                                 {
2205                                         if (!strcasecmp(parameter[1], "disable") || !strcasecmp(parameter[1], "none") || !strcasecmp(parameter[1], "off"))
2206                                                 shader.offsetmapping = OFFSETMAPPING_OFF;
2207                                         else if (!strcasecmp(parameter[1], "default") || !strcasecmp(parameter[1], "normal"))
2208                                                 shader.offsetmapping = OFFSETMAPPING_DEFAULT;
2209                                         else if (!strcasecmp(parameter[1], "linear"))
2210                                                 shader.offsetmapping = OFFSETMAPPING_LINEAR;
2211                                         else if (!strcasecmp(parameter[1], "relief"))
2212                                                 shader.offsetmapping = OFFSETMAPPING_RELIEF;
2213                                         if (numparameters >= 3)
2214                                                 shader.offsetscale = atof(parameter[2]);
2215                                         if (numparameters >= 5)
2216                                         {
2217                                                 if(!strcasecmp(parameter[3], "bias"))
2218                                                         shader.offsetbias = atof(parameter[4]);
2219                                                 else if(!strcasecmp(parameter[3], "match"))
2220                                                         shader.offsetbias = 1.0f - atof(parameter[4]);
2221                                                 else if(!strcasecmp(parameter[3], "match8"))
2222                                                         shader.offsetbias = 1.0f - atof(parameter[4]) / 255.0f;
2223                                                 else if(!strcasecmp(parameter[3], "match16"))
2224                                                         shader.offsetbias = 1.0f - atof(parameter[4]) / 65535.0f;
2225                                         }
2226                                 }
2227                                 else if (!strcasecmp(parameter[0], "deformvertexes") && numparameters >= 2)
2228                                 {
2229                                         int i, deformindex;
2230                                         for (deformindex = 0;deformindex < Q3MAXDEFORMS;deformindex++)
2231                                                 if (!shader.deforms[deformindex].deform)
2232                                                         break;
2233                                         if (deformindex < Q3MAXDEFORMS)
2234                                         {
2235                                                 for (i = 0;i < numparameters - 2 && i < Q3DEFORM_MAXPARMS;i++)
2236                                                         shader.deforms[deformindex].parms[i] = atof(parameter[i+2]);
2237                                                      if (!strcasecmp(parameter[1], "projectionshadow")) shader.deforms[deformindex].deform = Q3DEFORM_PROJECTIONSHADOW;
2238                                                 else if (!strcasecmp(parameter[1], "autosprite"      )) shader.deforms[deformindex].deform = Q3DEFORM_AUTOSPRITE;
2239                                                 else if (!strcasecmp(parameter[1], "autosprite2"     )) shader.deforms[deformindex].deform = Q3DEFORM_AUTOSPRITE2;
2240                                                 else if (!strcasecmp(parameter[1], "text0"           )) shader.deforms[deformindex].deform = Q3DEFORM_TEXT0;
2241                                                 else if (!strcasecmp(parameter[1], "text1"           )) shader.deforms[deformindex].deform = Q3DEFORM_TEXT1;
2242                                                 else if (!strcasecmp(parameter[1], "text2"           )) shader.deforms[deformindex].deform = Q3DEFORM_TEXT2;
2243                                                 else if (!strcasecmp(parameter[1], "text3"           )) shader.deforms[deformindex].deform = Q3DEFORM_TEXT3;
2244                                                 else if (!strcasecmp(parameter[1], "text4"           )) shader.deforms[deformindex].deform = Q3DEFORM_TEXT4;
2245                                                 else if (!strcasecmp(parameter[1], "text5"           )) shader.deforms[deformindex].deform = Q3DEFORM_TEXT5;
2246                                                 else if (!strcasecmp(parameter[1], "text6"           )) shader.deforms[deformindex].deform = Q3DEFORM_TEXT6;
2247                                                 else if (!strcasecmp(parameter[1], "text7"           )) shader.deforms[deformindex].deform = Q3DEFORM_TEXT7;
2248                                                 else if (!strcasecmp(parameter[1], "bulge"           )) shader.deforms[deformindex].deform = Q3DEFORM_BULGE;
2249                                                 else if (!strcasecmp(parameter[1], "normal"          )) shader.deforms[deformindex].deform = Q3DEFORM_NORMAL;
2250                                                 else if (!strcasecmp(parameter[1], "wave"            ))
2251                                                 {
2252                                                         shader.deforms[deformindex].deform = Q3DEFORM_WAVE;
2253                                                         shader.deforms[deformindex].wavefunc = Mod_LoadQ3Shaders_EnumerateWaveFunc(parameter[3]);
2254                                                         for (i = 0;i < numparameters - 4 && i < Q3WAVEPARMS;i++)
2255                                                                 shader.deforms[deformindex].waveparms[i] = atof(parameter[i+4]);
2256                                                 }
2257                                                 else if (!strcasecmp(parameter[1], "move"            ))
2258                                                 {
2259                                                         shader.deforms[deformindex].deform = Q3DEFORM_MOVE;
2260                                                         shader.deforms[deformindex].wavefunc = Mod_LoadQ3Shaders_EnumerateWaveFunc(parameter[5]);
2261                                                         for (i = 0;i < numparameters - 6 && i < Q3WAVEPARMS;i++)
2262                                                                 shader.deforms[deformindex].waveparms[i] = atof(parameter[i+6]);
2263                                                 }
2264                                         }
2265                                 }
2266                         }
2267                         // hide this shader if a cvar said it should be killed
2268                         if (shader.dpshaderkill)
2269                                 shader.numlayers = 0;
2270                         // fix up multiple reflection types
2271                         if(shader.textureflags & Q3TEXTUREFLAG_WATERSHADER)
2272                                 shader.textureflags &= ~(Q3TEXTUREFLAG_REFRACTION | Q3TEXTUREFLAG_REFLECTION | Q3TEXTUREFLAG_CAMERA);
2273
2274                         Q3Shader_AddToHash (&shader);
2275                 }
2276                 Mem_Free(f);
2277         }
2278         FS_FreeSearch(search);
2279         // free custinfoparm values
2280         for (j = 0; j < numcustsurfaceflags; j++)
2281                 Mem_Free(custsurfaceparmnames[j]);
2282 }
2283
2284 q3shaderinfo_t *Mod_LookupQ3Shader(const char *name)
2285 {
2286         unsigned short hash;
2287         q3shader_hash_entry_t* entry;
2288         if (!q3shaders_mem)
2289                 Mod_LoadQ3Shaders();
2290         hash = CRC_Block_CaseInsensitive ((const unsigned char *)name, strlen (name));
2291         entry = q3shader_data->hash + (hash % Q3SHADER_HASH_SIZE);
2292         while (entry != NULL)
2293         {
2294                 if (strcasecmp (entry->shader.name, name) == 0)
2295                         return &entry->shader;
2296                 entry = entry->chain;
2297         }
2298         return NULL;
2299 }
2300
2301 texture_shaderpass_t *Mod_CreateShaderPass(mempool_t *mempool, skinframe_t *skinframe)
2302 {
2303         texture_shaderpass_t *shaderpass = (texture_shaderpass_t *)Mem_Alloc(mempool, sizeof(*shaderpass));
2304         shaderpass->framerate = 0.0f;
2305         shaderpass->numframes = 1;
2306         shaderpass->blendfunc[0] = GL_ONE;
2307         shaderpass->blendfunc[1] = GL_ZERO;
2308         shaderpass->rgbgen.rgbgen = Q3RGBGEN_IDENTITY;
2309         shaderpass->alphagen.alphagen = Q3ALPHAGEN_IDENTITY;
2310         shaderpass->alphatest = false;
2311         shaderpass->tcgen.tcgen = Q3TCGEN_TEXTURE;
2312         shaderpass->skinframes[0] = skinframe;
2313         return shaderpass;
2314 }
2315
2316 texture_shaderpass_t *Mod_CreateShaderPassFromQ3ShaderLayer(mempool_t *mempool, const char *modelname, q3shaderinfo_layer_t *layer, int layerindex, int texflags, const char *texturename)
2317 {
2318         int j;
2319         texture_shaderpass_t *shaderpass = (texture_shaderpass_t *)Mem_Alloc(mempool, sizeof(*shaderpass));
2320         shaderpass->alphatest = layer->alphatest != 0;
2321         shaderpass->framerate = layer->framerate;
2322         shaderpass->numframes = layer->numframes;
2323         shaderpass->blendfunc[0] = layer->blendfunc[0];
2324         shaderpass->blendfunc[1] = layer->blendfunc[1];
2325         shaderpass->rgbgen = layer->rgbgen;
2326         shaderpass->alphagen = layer->alphagen;
2327         shaderpass->tcgen = layer->tcgen;
2328         for (j = 0; j < Q3MAXTCMODS && layer->tcmods[j].tcmod != Q3TCMOD_NONE; j++)
2329                 shaderpass->tcmods[j] = layer->tcmods[j];
2330         for (j = 0; j < layer->numframes; j++)
2331                 shaderpass->skinframes[j] = R_SkinFrame_LoadExternal(layer->texturename[j], texflags, false, true);
2332         return shaderpass;
2333 }
2334
2335 qboolean Mod_LoadTextureFromQ3Shader(mempool_t *mempool, const char *modelname, texture_t *texture, const char *name, qboolean warnmissing, qboolean fallback, int defaulttexflags, int defaultmaterialflags)
2336 {
2337         int texflagsmask, texflagsor;
2338         qboolean success = true;
2339         q3shaderinfo_t *shader;
2340         if (!name)
2341                 name = "";
2342         strlcpy(texture->name, name, sizeof(texture->name));
2343         texture->basealpha = 1.0f;
2344         shader = name[0] ? Mod_LookupQ3Shader(name) : NULL;
2345
2346         // allow disabling of picmip or compression by defaulttexflags
2347         texflagsmask = ~0;
2348         if(!(defaulttexflags & TEXF_PICMIP))
2349                 texflagsmask &= ~TEXF_PICMIP;
2350         if(!(defaulttexflags & TEXF_COMPRESS))
2351                 texflagsmask &= ~TEXF_COMPRESS;
2352         texflagsor = 0;
2353         if(defaulttexflags & TEXF_ISWORLD)
2354                 texflagsor |= TEXF_ISWORLD;
2355         if(defaulttexflags & TEXF_ISSPRITE)
2356                 texflagsor |= TEXF_ISSPRITE;
2357         // unless later loaded from the shader
2358         texture->offsetmapping = (mod_noshader_default_offsetmapping.value) ? OFFSETMAPPING_DEFAULT : OFFSETMAPPING_OFF;
2359         texture->offsetscale = 1;
2360         texture->offsetbias = 0;
2361         texture->specularscalemod = 1;
2362         texture->specularpowermod = 1; 
2363         texture->rtlightambient = 0;
2364         texture->transparentsort = TRANSPARENTSORT_DISTANCE;
2365         // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
2366         // JUST GREP FOR "specularscalemod = 1".
2367
2368         if (shader)
2369         {
2370                 if (developer_loading.integer)
2371                         Con_Printf("%s: loaded shader for %s\n", modelname, name);
2372
2373                 if (shader->surfaceparms & Q3SURFACEPARM_SKY)
2374                 {
2375                         texture->basematerialflags = MATERIALFLAG_SKY;
2376                         if (shader->skyboxname[0] && loadmodel)
2377                         {
2378                                 // quake3 seems to append a _ to the skybox name, so this must do so as well
2379                                 dpsnprintf(loadmodel->brush.skybox, sizeof(loadmodel->brush.skybox), "%s_", shader->skyboxname);
2380                         }
2381                 }
2382                 else if ((texture->surfaceflags & Q3SURFACEFLAG_NODRAW) || shader->numlayers == 0)
2383                         texture->basematerialflags = MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW;
2384                 else
2385                         texture->basematerialflags = MATERIALFLAG_WALL;
2386
2387                 if (shader->layers[0].alphatest)
2388                         texture->basematerialflags |= MATERIALFLAG_ALPHATEST | MATERIALFLAG_NOSHADOW;
2389                 if (shader->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2390                         texture->basematerialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
2391                 if (shader->textureflags & Q3TEXTUREFLAG_POLYGONOFFSET)
2392                 {
2393                         texture->biaspolygonoffset += shader->biaspolygonoffset;
2394                         texture->biaspolygonfactor += shader->biaspolygonfactor;
2395                 }
2396                 if (shader->textureflags & Q3TEXTUREFLAG_REFRACTION)
2397                         texture->basematerialflags |= MATERIALFLAG_REFRACTION;
2398                 if (shader->textureflags & Q3TEXTUREFLAG_REFLECTION)
2399                         texture->basematerialflags |= MATERIALFLAG_REFLECTION;
2400                 if (shader->textureflags & Q3TEXTUREFLAG_WATERSHADER)
2401                         texture->basematerialflags |= MATERIALFLAG_WATERSHADER;
2402                 if (shader->textureflags & Q3TEXTUREFLAG_CAMERA)
2403                         texture->basematerialflags |= MATERIALFLAG_CAMERA;
2404                 texture->customblendfunc[0] = GL_ONE;
2405                 texture->customblendfunc[1] = GL_ZERO;
2406                 texture->transparentsort = shader->transparentsort;
2407                 if (shader->numlayers > 0)
2408                 {
2409                         texture->customblendfunc[0] = shader->layers[0].blendfunc[0];
2410                         texture->customblendfunc[1] = shader->layers[0].blendfunc[1];
2411 /*
2412 Q3 shader blendfuncs actually used in the game (* = supported by DP)
2413 * additive               GL_ONE GL_ONE
2414 additive weird         GL_ONE GL_SRC_ALPHA
2415 additive weird 2       GL_ONE GL_ONE_MINUS_SRC_ALPHA
2416 * alpha                  GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
2417 alpha inverse          GL_ONE_MINUS_SRC_ALPHA GL_SRC_ALPHA
2418 brighten               GL_DST_COLOR GL_ONE
2419 brighten               GL_ONE GL_SRC_COLOR
2420 brighten weird         GL_DST_COLOR GL_ONE_MINUS_DST_ALPHA
2421 brighten weird 2       GL_DST_COLOR GL_SRC_ALPHA
2422 * modulate               GL_DST_COLOR GL_ZERO
2423 * modulate               GL_ZERO GL_SRC_COLOR
2424 modulate inverse       GL_ZERO GL_ONE_MINUS_SRC_COLOR
2425 modulate inverse alpha GL_ZERO GL_SRC_ALPHA
2426 modulate weird inverse GL_ONE_MINUS_DST_COLOR GL_ZERO
2427 * modulate x2            GL_DST_COLOR GL_SRC_COLOR
2428 * no blend               GL_ONE GL_ZERO
2429 nothing                GL_ZERO GL_ONE
2430 */
2431                         // if not opaque, figure out what blendfunc to use
2432                         if (shader->layers[0].blendfunc[0] != GL_ONE || shader->layers[0].blendfunc[1] != GL_ZERO)
2433                         {
2434                                 if (shader->layers[0].blendfunc[0] == GL_ONE && shader->layers[0].blendfunc[1] == GL_ONE)
2435                                         texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
2436                                 else if (shader->layers[0].blendfunc[0] == GL_SRC_ALPHA && shader->layers[0].blendfunc[1] == GL_ONE)
2437                                         texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
2438                                 else if (shader->layers[0].blendfunc[0] == GL_SRC_ALPHA && shader->layers[0].blendfunc[1] == GL_ONE_MINUS_SRC_ALPHA)
2439                                         texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
2440                                 else
2441                                         texture->basematerialflags |= MATERIALFLAG_CUSTOMBLEND | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
2442                         }
2443                 }
2444                 if (!shader->lighting)
2445                         texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
2446
2447                 // here be dragons: convert quake3 shaders to material
2448                 if (shader->numlayers > 0)
2449                 {
2450                         int i;
2451                         int terrainbackgroundlayer = -1;
2452                         int lightmaplayer = -1;
2453                         int alphagenspecularlayer = -1;
2454                         int rgbgenvertexlayer = -1;
2455                         int rgbgendiffuselayer = -1;
2456                         int materiallayer = -1;
2457                         int endofprelayers = 0;
2458                         int firstpostlayer = 0;
2459                         int shaderpassindex = 0;
2460                         for (i = 0; i < shader->numlayers; i++)
2461                         {
2462                                 if (shader->layers[i].texturename != NULL && !strcasecmp(shader->layers[i].texturename[0], "$lightmap"))
2463                                         lightmaplayer = i;
2464                                 if (shader->layers[i].rgbgen.rgbgen == Q3RGBGEN_VERTEX)
2465                                         rgbgenvertexlayer = i;
2466                                 if (shader->layers[i].rgbgen.rgbgen == Q3RGBGEN_LIGHTINGDIFFUSE)
2467                                         rgbgendiffuselayer = i;
2468                                 if (shader->layers[i].alphagen.alphagen == Q3ALPHAGEN_LIGHTINGSPECULAR)
2469                                         alphagenspecularlayer = i;
2470                         }
2471                         if (shader->numlayers >= 2
2472                          && shader->layers[1].alphagen.alphagen == Q3ALPHAGEN_VERTEX
2473                          && (shader->layers[0].blendfunc[0] == GL_ONE && shader->layers[0].blendfunc[1] == GL_ZERO && !shader->layers[0].alphatest)
2474                          && ((shader->layers[1].blendfunc[0] == GL_SRC_ALPHA && shader->layers[1].blendfunc[1] == GL_ONE_MINUS_SRC_ALPHA)
2475                                  || (shader->layers[1].blendfunc[0] == GL_ONE && shader->layers[1].blendfunc[1] == GL_ZERO && shader->layers[1].alphatest)))
2476                         {
2477                                 // terrain blend or certain other effects involving alphatest over a regular layer
2478                                 terrainbackgroundlayer = 0;
2479                                 materiallayer = 1;
2480                                 // terrain may be vertex lit (in which case both layers are rgbGen vertex) or lightmapped (in which ase the third layer is lightmap)
2481                                 firstpostlayer = lightmaplayer >= 0 ? lightmaplayer + 1 : materiallayer + 1;
2482                         }
2483                         else if (lightmaplayer == 0)
2484                         {
2485                                 // ordinary texture but with $lightmap before diffuse
2486                                 materiallayer = 1;
2487                                 firstpostlayer = lightmaplayer + 2;
2488                         }
2489                         else if (lightmaplayer >= 1)
2490                         {
2491                                 // ordinary texture - we don't properly apply lighting to the prelayers, but oh well...
2492                                 endofprelayers = lightmaplayer - 1;
2493                                 materiallayer = lightmaplayer - 1;
2494                                 firstpostlayer = lightmaplayer + 1;
2495                         }
2496                         else if (rgbgenvertexlayer >= 0)
2497                         {
2498                                 // map models with baked lighting
2499                                 materiallayer = rgbgenvertexlayer;
2500                                 endofprelayers = rgbgenvertexlayer;
2501                                 firstpostlayer = rgbgenvertexlayer + 1;
2502                                 // special case for rgbgen vertex if MATERIALFLAG_VERTEXCOLOR is expected on this material
2503                                 if (defaultmaterialflags & MATERIALFLAG_VERTEXCOLOR)
2504                                         texture->basematerialflags |= MATERIALFLAG_VERTEXCOLOR;
2505                         }
2506                         else if (rgbgendiffuselayer >= 0)
2507                         {
2508                                 // entity models with dynamic lighting
2509                                 materiallayer = rgbgendiffuselayer;
2510                                 endofprelayers = rgbgendiffuselayer;
2511                                 firstpostlayer = rgbgendiffuselayer + 1;
2512                                 // player models often have specular as a pass after diffuse - we don't currently make use of that specular texture (would need to meld it into the skinframe)...
2513                                 if (alphagenspecularlayer >= 0)
2514                                         firstpostlayer = alphagenspecularlayer + 1;
2515                         }
2516                         else
2517                         {
2518                                 // special effects shaders - treat first as primary layer and do everything else as post
2519                                 endofprelayers = 0;
2520                                 materiallayer = 0;
2521                                 firstpostlayer = 1;
2522                         }
2523                         // convert the main material layer
2524                         // FIXME: if alphagenspecularlayer is used, we should pass a specular texture name to R_SkinFrame_LoadExternal and have it load that texture instead of the assumed name for _gloss texture
2525                         if (materiallayer >= 0)
2526                                 texture->materialshaderpass = texture->shaderpasses[shaderpassindex++] = Mod_CreateShaderPassFromQ3ShaderLayer(mempool, modelname, &shader->layers[materiallayer], materiallayer, (shader->layers[materiallayer].dptexflags & texflagsmask) | texflagsor, texture->name);
2527                         // convert the terrain background blend layer (if any)
2528                         if (terrainbackgroundlayer >= 0)
2529                                 texture->backgroundshaderpass = texture->shaderpasses[shaderpassindex++] = Mod_CreateShaderPassFromQ3ShaderLayer(mempool, modelname, &shader->layers[terrainbackgroundlayer], terrainbackgroundlayer, (shader->layers[terrainbackgroundlayer].dptexflags & texflagsmask) | texflagsor, texture->name);
2530                         // convert the prepass layers (if any)
2531                         texture->startpreshaderpass = shaderpassindex;
2532                         for (i = 0; i < endofprelayers; i++)
2533                                 texture->shaderpasses[shaderpassindex++] = Mod_CreateShaderPassFromQ3ShaderLayer(mempool, modelname, &shader->layers[i], i, (shader->layers[i].dptexflags & texflagsmask) | texflagsor, texture->name);
2534                         texture->endpreshaderpass = shaderpassindex;
2535                         texture->startpostshaderpass = shaderpassindex;
2536                         // convert the postpass layers (if any)
2537                         for (i = firstpostlayer; i < shader->numlayers; i++)
2538                                 texture->shaderpasses[shaderpassindex++] = Mod_CreateShaderPassFromQ3ShaderLayer(mempool, modelname, &shader->layers[i], i, (shader->layers[i].dptexflags & texflagsmask) | texflagsor, texture->name);
2539                         texture->startpostshaderpass = shaderpassindex;
2540                 }
2541
2542                 if (shader->dpshadow)
2543                         texture->basematerialflags &= ~MATERIALFLAG_NOSHADOW;
2544                 if (shader->dpnoshadow)
2545                         texture->basematerialflags |= MATERIALFLAG_NOSHADOW;
2546                 if (shader->dpnortlight)
2547                         texture->basematerialflags |= MATERIALFLAG_NORTLIGHT;
2548                 if (shader->vertexalpha)
2549                         texture->basematerialflags |= MATERIALFLAG_ALPHAGEN_VERTEX;
2550                 memcpy(texture->deforms, shader->deforms, sizeof(texture->deforms));
2551                 texture->reflectmin = shader->reflectmin;
2552                 texture->reflectmax = shader->reflectmax;
2553                 texture->refractfactor = shader->refractfactor;
2554                 Vector4Copy(shader->refractcolor4f, texture->refractcolor4f);
2555                 texture->reflectfactor = shader->reflectfactor;
2556                 Vector4Copy(shader->reflectcolor4f, texture->reflectcolor4f);
2557                 texture->r_water_wateralpha = shader->r_water_wateralpha;
2558                 Vector2Copy(shader->r_water_waterscroll, texture->r_water_waterscroll);
2559                 texture->offsetmapping = shader->offsetmapping;
2560                 texture->offsetscale = shader->offsetscale;
2561                 texture->offsetbias = shader->offsetbias;
2562                 texture->specularscalemod = shader->specularscalemod;
2563                 texture->specularpowermod = shader->specularpowermod;
2564                 texture->rtlightambient = shader->rtlightambient;
2565                 if (shader->dpreflectcube[0])
2566                         texture->reflectcubetexture = R_GetCubemap(shader->dpreflectcube);
2567
2568                 // set up default supercontents (on q3bsp this is overridden by the q3bsp loader)
2569                 texture->supercontents = SUPERCONTENTS_SOLID | SUPERCONTENTS_OPAQUE;
2570                 if (shader->surfaceparms & Q3SURFACEPARM_LAVA         ) texture->supercontents  = SUPERCONTENTS_LAVA         ;
2571                 if (shader->surfaceparms & Q3SURFACEPARM_SLIME        ) texture->supercontents  = SUPERCONTENTS_SLIME        ;
2572                 if (shader->surfaceparms & Q3SURFACEPARM_WATER        ) texture->supercontents  = SUPERCONTENTS_WATER        ;
2573                 if (shader->surfaceparms & Q3SURFACEPARM_NONSOLID     ) texture->supercontents  = 0                          ;
2574                 if (shader->surfaceparms & Q3SURFACEPARM_PLAYERCLIP   ) texture->supercontents  = SUPERCONTENTS_PLAYERCLIP   ;
2575                 if (shader->surfaceparms & Q3SURFACEPARM_BOTCLIP      ) texture->supercontents  = SUPERCONTENTS_MONSTERCLIP  ;
2576                 if (shader->surfaceparms & Q3SURFACEPARM_SKY          ) texture->supercontents  = SUPERCONTENTS_SKY          ;
2577
2578         //      if (shader->surfaceparms & Q3SURFACEPARM_ALPHASHADOW  ) texture->supercontents |= SUPERCONTENTS_ALPHASHADOW  ;
2579         //      if (shader->surfaceparms & Q3SURFACEPARM_AREAPORTAL   ) texture->supercontents |= SUPERCONTENTS_AREAPORTAL   ;
2580         //      if (shader->surfaceparms & Q3SURFACEPARM_CLUSTERPORTAL) texture->supercontents |= SUPERCONTENTS_CLUSTERPORTAL;
2581         //      if (shader->surfaceparms & Q3SURFACEPARM_DETAIL       ) texture->supercontents |= SUPERCONTENTS_DETAIL       ;
2582                 if (shader->surfaceparms & Q3SURFACEPARM_DONOTENTER   ) texture->supercontents |= SUPERCONTENTS_DONOTENTER   ;
2583         //      if (shader->surfaceparms & Q3SURFACEPARM_FOG          ) texture->supercontents |= SUPERCONTENTS_FOG          ;
2584                 if (shader->surfaceparms & Q3SURFACEPARM_LAVA         ) texture->supercontents |= SUPERCONTENTS_LAVA         ;
2585         //      if (shader->surfaceparms & Q3SURFACEPARM_LIGHTFILTER  ) texture->supercontents |= SUPERCONTENTS_LIGHTFILTER  ;
2586         //      if (shader->surfaceparms & Q3SURFACEPARM_METALSTEPS   ) texture->supercontents |= SUPERCONTENTS_METALSTEPS   ;
2587         //      if (shader->surfaceparms & Q3SURFACEPARM_NODAMAGE     ) texture->supercontents |= SUPERCONTENTS_NODAMAGE     ;
2588         //      if (shader->surfaceparms & Q3SURFACEPARM_NODLIGHT     ) texture->supercontents |= SUPERCONTENTS_NODLIGHT     ;
2589         //      if (shader->surfaceparms & Q3SURFACEPARM_NODRAW       ) texture->supercontents |= SUPERCONTENTS_NODRAW       ;
2590                 if (shader->surfaceparms & Q3SURFACEPARM_NODROP       ) texture->supercontents |= SUPERCONTENTS_NODROP       ;
2591         //      if (shader->surfaceparms & Q3SURFACEPARM_NOIMPACT     ) texture->supercontents |= SUPERCONTENTS_NOIMPACT     ;
2592         //      if (shader->surfaceparms & Q3SURFACEPARM_NOLIGHTMAP   ) texture->supercontents |= SUPERCONTENTS_NOLIGHTMAP   ;
2593         //      if (shader->surfaceparms & Q3SURFACEPARM_NOMARKS      ) texture->supercontents |= SUPERCONTENTS_NOMARKS      ;
2594         //      if (shader->surfaceparms & Q3SURFACEPARM_NOMIPMAPS    ) texture->supercontents |= SUPERCONTENTS_NOMIPMAPS    ;
2595                 if (shader->surfaceparms & Q3SURFACEPARM_NONSOLID     ) texture->supercontents &=~SUPERCONTENTS_SOLID        ;
2596         //      if (shader->surfaceparms & Q3SURFACEPARM_ORIGIN       ) texture->supercontents |= SUPERCONTENTS_ORIGIN       ;
2597                 if (shader->surfaceparms & Q3SURFACEPARM_PLAYERCLIP   ) texture->supercontents |= SUPERCONTENTS_PLAYERCLIP   ;
2598                 if (shader->surfaceparms & Q3SURFACEPARM_SKY          ) texture->supercontents |= SUPERCONTENTS_SKY          ;
2599         //      if (shader->surfaceparms & Q3SURFACEPARM_SLICK        ) texture->supercontents |= SUPERCONTENTS_SLICK        ;
2600                 if (shader->surfaceparms & Q3SURFACEPARM_SLIME        ) texture->supercontents |= SUPERCONTENTS_SLIME        ;
2601         //      if (shader->surfaceparms & Q3SURFACEPARM_STRUCTURAL   ) texture->supercontents |= SUPERCONTENTS_STRUCTURAL   ;
2602         //      if (shader->surfaceparms & Q3SURFACEPARM_TRANS        ) texture->supercontents |= SUPERCONTENTS_TRANS        ;
2603                 if (shader->surfaceparms & Q3SURFACEPARM_WATER        ) texture->supercontents |= SUPERCONTENTS_WATER        ;
2604         //      if (shader->surfaceparms & Q3SURFACEPARM_POINTLIGHT   ) texture->supercontents |= SUPERCONTENTS_POINTLIGHT   ;
2605         //      if (shader->surfaceparms & Q3SURFACEPARM_HINT         ) texture->supercontents |= SUPERCONTENTS_HINT         ;
2606         //      if (shader->surfaceparms & Q3SURFACEPARM_DUST         ) texture->supercontents |= SUPERCONTENTS_DUST         ;
2607                 if (shader->surfaceparms & Q3SURFACEPARM_BOTCLIP      ) texture->supercontents |= SUPERCONTENTS_BOTCLIP      | SUPERCONTENTS_MONSTERCLIP;
2608         //      if (shader->surfaceparms & Q3SURFACEPARM_LIGHTGRID    ) texture->supercontents |= SUPERCONTENTS_LIGHTGRID    ;
2609         //      if (shader->surfaceparms & Q3SURFACEPARM_ANTIPORTAL   ) texture->supercontents |= SUPERCONTENTS_ANTIPORTAL   ;
2610
2611                 texture->surfaceflags = shader->surfaceflags;
2612                 if (shader->surfaceparms & Q3SURFACEPARM_ALPHASHADOW  ) texture->surfaceflags |= Q3SURFACEFLAG_ALPHASHADOW  ;
2613         //      if (shader->surfaceparms & Q3SURFACEPARM_AREAPORTAL   ) texture->surfaceflags |= Q3SURFACEFLAG_AREAPORTAL   ;
2614         //      if (shader->surfaceparms & Q3SURFACEPARM_CLUSTERPORTAL) texture->surfaceflags |= Q3SURFACEFLAG_CLUSTERPORTAL;
2615         //      if (shader->surfaceparms & Q3SURFACEPARM_DETAIL       ) texture->surfaceflags |= Q3SURFACEFLAG_DETAIL       ;
2616         //      if (shader->surfaceparms & Q3SURFACEPARM_DONOTENTER   ) texture->surfaceflags |= Q3SURFACEFLAG_DONOTENTER   ;
2617         //      if (shader->surfaceparms & Q3SURFACEPARM_FOG          ) texture->surfaceflags |= Q3SURFACEFLAG_FOG          ;
2618         //      if (shader->surfaceparms & Q3SURFACEPARM_LAVA         ) texture->surfaceflags |= Q3SURFACEFLAG_LAVA         ;
2619                 if (shader->surfaceparms & Q3SURFACEPARM_LIGHTFILTER  ) texture->surfaceflags |= Q3SURFACEFLAG_LIGHTFILTER  ;
2620                 if (shader->surfaceparms & Q3SURFACEPARM_METALSTEPS   ) texture->surfaceflags |= Q3SURFACEFLAG_METALSTEPS   ;
2621                 if (shader->surfaceparms & Q3SURFACEPARM_NODAMAGE     ) texture->surfaceflags |= Q3SURFACEFLAG_NODAMAGE     ;
2622                 if (shader->surfaceparms & Q3SURFACEPARM_NODLIGHT     ) texture->surfaceflags |= Q3SURFACEFLAG_NODLIGHT     ;
2623                 if (shader->surfaceparms & Q3SURFACEPARM_NODRAW       ) texture->surfaceflags |= Q3SURFACEFLAG_NODRAW       ;
2624         //      if (shader->surfaceparms & Q3SURFACEPARM_NODROP       ) texture->surfaceflags |= Q3SURFACEFLAG_NODROP       ;
2625                 if (shader->surfaceparms & Q3SURFACEPARM_NOIMPACT     ) texture->surfaceflags |= Q3SURFACEFLAG_NOIMPACT     ;
2626                 if (shader->surfaceparms & Q3SURFACEPARM_NOLIGHTMAP   ) texture->surfaceflags |= Q3SURFACEFLAG_NOLIGHTMAP   ;
2627                 if (shader->surfaceparms & Q3SURFACEPARM_NOMARKS      ) texture->surfaceflags |= Q3SURFACEFLAG_NOMARKS      ;
2628         //      if (shader->surfaceparms & Q3SURFACEPARM_NOMIPMAPS    ) texture->surfaceflags |= Q3SURFACEFLAG_NOMIPMAPS    ;
2629                 if (shader->surfaceparms & Q3SURFACEPARM_NONSOLID     ) texture->surfaceflags |= Q3SURFACEFLAG_NONSOLID     ;
2630         //      if (shader->surfaceparms & Q3SURFACEPARM_ORIGIN       ) texture->surfaceflags |= Q3SURFACEFLAG_ORIGIN       ;
2631         //      if (shader->surfaceparms & Q3SURFACEPARM_PLAYERCLIP   ) texture->surfaceflags |= Q3SURFACEFLAG_PLAYERCLIP   ;
2632                 if (shader->surfaceparms & Q3SURFACEPARM_SKY          ) texture->surfaceflags |= Q3SURFACEFLAG_SKY          ;
2633                 if (shader->surfaceparms & Q3SURFACEPARM_SLICK        ) texture->surfaceflags |= Q3SURFACEFLAG_SLICK        ;
2634         //      if (shader->surfaceparms & Q3SURFACEPARM_SLIME        ) texture->surfaceflags |= Q3SURFACEFLAG_SLIME        ;
2635         //      if (shader->surfaceparms & Q3SURFACEPARM_STRUCTURAL   ) texture->surfaceflags |= Q3SURFACEFLAG_STRUCTURAL   ;
2636         //      if (shader->surfaceparms & Q3SURFACEPARM_TRANS        ) texture->surfaceflags |= Q3SURFACEFLAG_TRANS        ;
2637         //      if (shader->surfaceparms & Q3SURFACEPARM_WATER        ) texture->surfaceflags |= Q3SURFACEFLAG_WATER        ;
2638                 if (shader->surfaceparms & Q3SURFACEPARM_POINTLIGHT   ) texture->surfaceflags |= Q3SURFACEFLAG_POINTLIGHT   ;
2639                 if (shader->surfaceparms & Q3SURFACEPARM_HINT         ) texture->surfaceflags |= Q3SURFACEFLAG_HINT         ;
2640                 if (shader->surfaceparms & Q3SURFACEPARM_DUST         ) texture->surfaceflags |= Q3SURFACEFLAG_DUST         ;
2641         //      if (shader->surfaceparms & Q3SURFACEPARM_BOTCLIP      ) texture->surfaceflags |= Q3SURFACEFLAG_BOTCLIP      ;
2642         //      if (shader->surfaceparms & Q3SURFACEPARM_LIGHTGRID    ) texture->surfaceflags |= Q3SURFACEFLAG_LIGHTGRID    ;
2643         //      if (shader->surfaceparms & Q3SURFACEPARM_ANTIPORTAL   ) texture->surfaceflags |= Q3SURFACEFLAG_ANTIPORTAL   ;
2644
2645                 if (shader->dpmeshcollisions)
2646                         texture->basematerialflags |= MATERIALFLAG_MESHCOLLISIONS;
2647                 if (shader->dpshaderkill && developer_extra.integer)
2648                         Con_DPrintf("^1%s:^7 killing shader ^3\"%s\" because of cvar\n", modelname, name);
2649         }
2650         else if (!strcmp(texture->name, "noshader") || !texture->name[0])
2651         {
2652                 if (developer_extra.integer)
2653                         Con_DPrintf("^1%s:^7 using fallback noshader material for ^3\"%s\"\n", modelname, name);
2654                 texture->supercontents = SUPERCONTENTS_SOLID | SUPERCONTENTS_OPAQUE;
2655         }
2656         else if (!strcmp(texture->name, "common/nodraw") || !strcmp(texture->name, "textures/common/nodraw"))
2657         {
2658                 if (developer_extra.integer)
2659                         Con_DPrintf("^1%s:^7 using fallback nodraw material for ^3\"%s\"\n", modelname, name);
2660                 texture->basematerialflags = MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW;
2661                 texture->supercontents = SUPERCONTENTS_SOLID;
2662         }
2663         else
2664         {
2665                 if (developer_extra.integer)
2666                         Con_DPrintf("^1%s:^7 No shader found for texture ^3\"%s\"\n", modelname, texture->name);
2667                 if (texture->surfaceflags & Q3SURFACEFLAG_NODRAW)
2668                 {
2669                         texture->basematerialflags = MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW;
2670                         texture->supercontents = SUPERCONTENTS_SOLID;
2671                 }
2672                 else if (texture->surfaceflags & Q3SURFACEFLAG_SKY)
2673                 {
2674                         texture->basematerialflags = MATERIALFLAG_SKY;
2675                         texture->supercontents = SUPERCONTENTS_SKY;
2676                 }
2677                 else
2678                 {
2679                         texture->basematerialflags = defaultmaterialflags;
2680                         texture->supercontents = SUPERCONTENTS_SOLID | SUPERCONTENTS_OPAQUE;
2681                 }
2682                 if(cls.state == ca_dedicated)
2683                 {
2684                         texture->materialshaderpass = NULL;
2685                         success = false;
2686                 }
2687                 else
2688                 {
2689                         skinframe_t *skinframe = R_SkinFrame_LoadExternal(texture->name, defaulttexflags, false, fallback);
2690                         if (skinframe)
2691                         {
2692                                 texture->materialshaderpass = texture->shaderpasses[0] = Mod_CreateShaderPass(mempool, skinframe);
2693                                 if (texture->materialshaderpass->skinframes[0]->hasalpha)
2694                                         texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
2695                                 if (texture->q2contents)
2696                                         texture->supercontents = Mod_Q2BSP_SuperContentsFromNativeContents(texture->q2contents);
2697                         }
2698                         else
2699                                 success = false;
2700                         if (!success && warnmissing)
2701                                 Con_Printf("^1%s:^7 could not load texture ^3\"%s\"\n", modelname, texture->name);
2702                 }
2703         }
2704         // init the animation variables
2705         texture->currentframe = texture;
2706         texture->currentmaterialflags = texture->basematerialflags;
2707         if (!texture->materialshaderpass)
2708                 texture->materialshaderpass = texture->shaderpasses[0] = Mod_CreateShaderPass(mempool, R_SkinFrame_LoadMissing());
2709         if (!texture->materialshaderpass->skinframes[0])
2710                 texture->materialshaderpass->skinframes[0] = R_SkinFrame_LoadMissing();
2711         texture->currentskinframe = texture->materialshaderpass ? texture->materialshaderpass->skinframes[0] : NULL;
2712         texture->backgroundcurrentskinframe = texture->backgroundshaderpass ? texture->backgroundshaderpass->skinframes[0] : NULL;
2713         return success;
2714 }
2715
2716 void Mod_LoadCustomMaterial(mempool_t *mempool, texture_t *texture, const char *name, int supercontents, int materialflags, skinframe_t *skinframe)
2717 {
2718         if (!(materialflags & (MATERIALFLAG_WALL | MATERIALFLAG_SKY)))
2719                 Con_DPrintf("^1Custom texture ^3\"%s\" does not have MATERIALFLAG_WALL set\n", texture->name);
2720
2721         strlcpy(texture->name, name, sizeof(texture->name));
2722         texture->basealpha = 1.0f;
2723         texture->basematerialflags = materialflags;
2724         texture->supercontents = supercontents;
2725
2726         texture->offsetmapping = (mod_noshader_default_offsetmapping.value) ? OFFSETMAPPING_DEFAULT : OFFSETMAPPING_OFF;
2727         texture->offsetscale = 1;
2728         texture->offsetbias = 0;
2729         texture->specularscalemod = 1;
2730         texture->specularpowermod = 1;
2731         texture->rtlightambient = 0;
2732         texture->transparentsort = TRANSPARENTSORT_DISTANCE;
2733         // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
2734         // JUST GREP FOR "specularscalemod = 1".
2735
2736         if (developer_extra.integer)
2737                 Con_DPrintf("^1Custom texture ^3\"%s\"\n", texture->name);
2738         if (skinframe)
2739                 texture->materialshaderpass = texture->shaderpasses[0] = Mod_CreateShaderPass(mempool, skinframe);
2740
2741         // init the animation variables
2742         texture->currentmaterialflags = texture->basematerialflags;
2743         texture->currentframe = texture;
2744         texture->currentskinframe = skinframe;
2745         texture->backgroundcurrentskinframe = NULL;
2746 }
2747
2748 void Mod_UnloadCustomMaterial(texture_t *texture, qboolean purgeskins)
2749 {
2750         int i, j;
2751         for (i = 0; i < sizeof(texture->shaderpasses) / sizeof(texture->shaderpasses[0]); i++)
2752         {
2753                 if (texture->shaderpasses[i])
2754                 {
2755                         if (purgeskins)
2756                                 for (j = 0; j < sizeof(texture->shaderpasses[i]->skinframes) / sizeof(skinframe_t *);j++)
2757                                         if (texture->shaderpasses[i]->skinframes[j] && texture->shaderpasses[i]->skinframes[j]->base)
2758                                                 R_SkinFrame_PurgeSkinFrame(texture->shaderpasses[i]->skinframes[j]);
2759                         Mem_Free(texture->shaderpasses[i]);
2760                         texture->shaderpasses[i] = NULL;
2761                 }
2762         }
2763         texture->materialshaderpass = NULL;
2764         texture->currentskinframe = NULL;
2765         texture->backgroundcurrentskinframe = NULL;
2766 }
2767
2768 skinfile_t *Mod_LoadSkinFiles(void)
2769 {
2770         int i, words, line, wordsoverflow;
2771         char *text;
2772         const char *data;
2773         skinfile_t *skinfile = NULL, *first = NULL;
2774         skinfileitem_t *skinfileitem;
2775         char word[10][MAX_QPATH];
2776         char vabuf[1024];
2777
2778 /*
2779 sample file:
2780 U_bodyBox,models/players/Legoman/BikerA2.tga
2781 U_RArm,models/players/Legoman/BikerA1.tga
2782 U_LArm,models/players/Legoman/BikerA1.tga
2783 U_armor,common/nodraw
2784 U_sword,common/nodraw
2785 U_shield,common/nodraw
2786 U_homb,common/nodraw
2787 U_backpack,common/nodraw
2788 U_colcha,common/nodraw
2789 tag_head,
2790 tag_weapon,
2791 tag_torso,
2792 */
2793         memset(word, 0, sizeof(word));
2794         for (i = 0;i < 256 && (data = text = (char *)FS_LoadFile(va(vabuf, sizeof(vabuf), "%s_%i.skin", loadmodel->name, i), tempmempool, true, NULL));i++)
2795         {
2796                 // If it's the first file we parse
2797                 if (skinfile == NULL)
2798                 {
2799                         skinfile = (skinfile_t *)Mem_Alloc(loadmodel->mempool, sizeof(skinfile_t));
2800                         first = skinfile;
2801                 }
2802                 else
2803                 {
2804                         skinfile->next = (skinfile_t *)Mem_Alloc(loadmodel->mempool, sizeof(skinfile_t));
2805                         skinfile = skinfile->next;
2806                 }
2807                 skinfile->next = NULL;
2808
2809                 for(line = 0;;line++)
2810                 {
2811                         // parse line
2812                         if (!COM_ParseToken_QuakeC(&data, true))
2813                                 break;
2814                         if (!strcmp(com_token, "\n"))
2815                                 continue;
2816                         words = 0;
2817                         wordsoverflow = false;
2818                         do
2819                         {
2820                                 if (words < 10)
2821                                         strlcpy(word[words++], com_token, sizeof (word[0]));
2822                                 else
2823                                         wordsoverflow = true;
2824                         }
2825                         while (COM_ParseToken_QuakeC(&data, true) && strcmp(com_token, "\n"));
2826                         if (wordsoverflow)
2827                         {
2828                                 Con_Printf("Mod_LoadSkinFiles: parsing error in file \"%s_%i.skin\" on line #%i: line with too many statements, skipping\n", loadmodel->name, i, line);
2829                                 continue;
2830                         }
2831                         // words is always >= 1
2832                         if (!strcmp(word[0], "replace"))
2833                         {
2834                                 if (words == 3)
2835                                 {
2836                                         if (developer_loading.integer)
2837                                                 Con_Printf("Mod_LoadSkinFiles: parsed mesh \"%s\" shader replacement \"%s\"\n", word[1], word[2]);
2838                                         skinfileitem = (skinfileitem_t *)Mem_Alloc(loadmodel->mempool, sizeof(skinfileitem_t));
2839                                         skinfileitem->next = skinfile->items;
2840                                         skinfile->items = skinfileitem;
2841                                         strlcpy (skinfileitem->name, word[1], sizeof (skinfileitem->name));
2842                                         strlcpy (skinfileitem->replacement, word[2], sizeof (skinfileitem->replacement));
2843                                 }
2844                                 else
2845                                         Con_Printf("Mod_LoadSkinFiles: parsing error in file \"%s_%i.skin\" on line #%i: wrong number of parameters to command \"%s\", see documentation in DP_GFX_SKINFILES extension in dpextensions.qc\n", loadmodel->name, i, line, word[0]);
2846                         }
2847                         else if (words >= 2 && !strncmp(word[0], "tag_", 4))
2848                         {
2849                                 // tag name, like "tag_weapon,"
2850                                 // not used for anything (not even in Quake3)
2851                         }
2852                         else if (words >= 2 && !strcmp(word[1], ","))
2853                         {
2854                                 // mesh shader name, like "U_RArm,models/players/Legoman/BikerA1.tga"
2855                                 if (developer_loading.integer)
2856                                         Con_Printf("Mod_LoadSkinFiles: parsed mesh \"%s\" shader replacement \"%s\"\n", word[0], word[2]);
2857                                 skinfileitem = (skinfileitem_t *)Mem_Alloc(loadmodel->mempool, sizeof(skinfileitem_t));
2858                                 skinfileitem->next = skinfile->items;
2859                                 skinfile->items = skinfileitem;
2860                                 strlcpy (skinfileitem->name, word[0], sizeof (skinfileitem->name));
2861                                 strlcpy (skinfileitem->replacement, word[2], sizeof (skinfileitem->replacement));
2862                         }
2863                         else
2864                                 Con_Printf("Mod_LoadSkinFiles: parsing error in file \"%s_%i.skin\" on line #%i: does not look like tag or mesh specification, or replace command, see documentation in DP_GFX_SKINFILES extension in dpextensions.qc\n", loadmodel->name, i, line);
2865                 }
2866                 Mem_Free(text);
2867         }
2868         if (i)
2869                 loadmodel->numskins = i;
2870         return first;
2871 }
2872
2873 void Mod_FreeSkinFiles(skinfile_t *skinfile)
2874 {
2875         skinfile_t *next;
2876         skinfileitem_t *skinfileitem, *nextitem;
2877         for (;skinfile;skinfile = next)
2878         {
2879                 next = skinfile->next;
2880                 for (skinfileitem = skinfile->items;skinfileitem;skinfileitem = nextitem)
2881                 {
2882                         nextitem = skinfileitem->next;
2883                         Mem_Free(skinfileitem);
2884                 }
2885                 Mem_Free(skinfile);
2886         }
2887 }
2888
2889 int Mod_CountSkinFiles(skinfile_t *skinfile)
2890 {
2891         int i;
2892         for (i = 0;skinfile;skinfile = skinfile->next, i++);
2893         return i;
2894 }
2895
2896 void Mod_SnapVertices(int numcomponents, int numvertices, float *vertices, float snap)
2897 {
2898         int i;
2899         double isnap = 1.0 / snap;
2900         for (i = 0;i < numvertices*numcomponents;i++)
2901                 vertices[i] = floor(vertices[i]*isnap)*snap;
2902 }
2903
2904 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f)
2905 {
2906         int i, outtriangles;
2907         float edgedir1[3], edgedir2[3], temp[3];
2908         // a degenerate triangle is one with no width (thickness, surface area)
2909         // these are characterized by having all 3 points colinear (along a line)
2910         // or having two points identical
2911         // the simplest check is to calculate the triangle's area
2912         for (i = 0, outtriangles = 0;i < numtriangles;i++, inelement3i += 3)
2913         {
2914                 // calculate first edge
2915                 VectorSubtract(vertex3f + inelement3i[1] * 3, vertex3f + inelement3i[0] * 3, edgedir1);
2916                 VectorSubtract(vertex3f + inelement3i[2] * 3, vertex3f + inelement3i[0] * 3, edgedir2);
2917                 CrossProduct(edgedir1, edgedir2, temp);
2918                 if (VectorLength2(temp) < 0.001f)
2919                         continue; // degenerate triangle (no area)
2920                 // valid triangle (has area)
2921                 VectorCopy(inelement3i, outelement3i);
2922                 outelement3i += 3;
2923                 outtriangles++;
2924         }
2925         return outtriangles;
2926 }
2927
2928 void Mod_VertexRangeFromElements(int numelements, const int *elements, int *firstvertexpointer, int *lastvertexpointer)
2929 {
2930         int i, e;
2931         int firstvertex, lastvertex;
2932         if (numelements > 0 && elements)
2933         {
2934                 firstvertex = lastvertex = elements[0];
2935                 for (i = 1;i < numelements;i++)
2936                 {
2937                         e = elements[i];
2938                         firstvertex = min(firstvertex, e);
2939                         lastvertex = max(lastvertex, e);
2940                 }
2941         }
2942         else
2943                 firstvertex = lastvertex = 0;
2944         if (firstvertexpointer)
2945                 *firstvertexpointer = firstvertex;
2946         if (lastvertexpointer)
2947                 *lastvertexpointer = lastvertex;
2948 }
2949
2950 void Mod_MakeSortedSurfaces(dp_model_t *mod)
2951 {
2952         // make an optimal set of texture-sorted batches to draw...
2953         int j, t;
2954         int *firstsurfacefortexture;
2955         int *numsurfacesfortexture;
2956         if (!mod->sortedmodelsurfaces)
2957                 mod->sortedmodelsurfaces = (int *) Mem_Alloc(loadmodel->mempool, mod->nummodelsurfaces * sizeof(*mod->sortedmodelsurfaces));
2958         firstsurfacefortexture = (int *) Mem_Alloc(tempmempool, mod->num_textures * sizeof(*firstsurfacefortexture));
2959         numsurfacesfortexture = (int *) Mem_Alloc(tempmempool, mod->num_textures * sizeof(*numsurfacesfortexture));
2960         memset(numsurfacesfortexture, 0, mod->num_textures * sizeof(*numsurfacesfortexture));
2961         for (j = 0;j < mod->nummodelsurfaces;j++)
2962         {
2963                 const msurface_t *surface = mod->data_surfaces + j + mod->firstmodelsurface;
2964                 t = (int)(surface->texture - mod->data_textures);
2965                 numsurfacesfortexture[t]++;
2966         }
2967         j = 0;
2968         for (t = 0;t < mod->num_textures;t++)
2969         {
2970                 firstsurfacefortexture[t] = j;
2971                 j += numsurfacesfortexture[t];
2972         }
2973         for (j = 0;j < mod->nummodelsurfaces;j++)
2974         {
2975                 const msurface_t *surface = mod->data_surfaces + j + mod->firstmodelsurface;
2976                 t = (int)(surface->texture - mod->data_textures);
2977                 mod->sortedmodelsurfaces[firstsurfacefortexture[t]++] = j + mod->firstmodelsurface;
2978         }
2979         Mem_Free(firstsurfacefortexture);
2980         Mem_Free(numsurfacesfortexture);
2981 }
2982
2983 void Mod_BuildVBOs(void)
2984 {
2985         if (!loadmodel->surfmesh.num_vertices)
2986                 return;
2987
2988         if (gl_paranoid.integer && loadmodel->surfmesh.data_element3s && loadmodel->surfmesh.data_element3i)
2989         {
2990                 int i;
2991                 for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++)
2992                 {
2993                         if (loadmodel->surfmesh.data_element3s[i] != loadmodel->surfmesh.data_element3i[i])
2994                         {
2995                                 Con_Printf("Mod_BuildVBOs: element %u is incorrect (%u should be %u)\n", i, loadmodel->surfmesh.data_element3s[i], loadmodel->surfmesh.data_element3i[i]);
2996                                 loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i];
2997                         }
2998                 }
2999         }
3000
3001         // build r_vertexmesh_t array
3002         // (compressed interleaved array for D3D)
3003         if (!loadmodel->surfmesh.data_vertexmesh && vid.useinterleavedarrays)
3004         {
3005                 int vertexindex;
3006                 int numvertices = loadmodel->surfmesh.num_vertices;
3007                 r_vertexmesh_t *vertexmesh;
3008                 loadmodel->surfmesh.data_vertexmesh = vertexmesh = (r_vertexmesh_t*)Mem_Alloc(loadmodel->mempool, numvertices * sizeof(r_vertexmesh_t));
3009                 for (vertexindex = 0;vertexindex < numvertices;vertexindex++, vertexmesh++)
3010                 {
3011                         VectorCopy(loadmodel->surfmesh.data_vertex3f + 3*vertexindex, vertexmesh->vertex3f);
3012                         VectorScale(loadmodel->surfmesh.data_svector3f + 3*vertexindex, 1.0f, vertexmesh->svector3f);
3013                         VectorScale(loadmodel->surfmesh.data_tvector3f + 3*vertexindex, 1.0f, vertexmesh->tvector3f);
3014                         VectorScale(loadmodel->surfmesh.data_normal3f + 3*vertexindex, 1.0f, vertexmesh->normal3f);
3015                         if (loadmodel->surfmesh.data_lightmapcolor4f)
3016                                 Vector4Copy(loadmodel->surfmesh.data_lightmapcolor4f + 4*vertexindex, vertexmesh->color4f);
3017                         Vector2Copy(loadmodel->surfmesh.data_texcoordtexture2f + 2*vertexindex, vertexmesh->texcoordtexture2f);
3018                         if (loadmodel->surfmesh.data_texcoordlightmap2f)
3019                                 Vector2Scale(loadmodel->surfmesh.data_texcoordlightmap2f + 2*vertexindex, 1.0f, vertexmesh->texcoordlightmap2f);
3020                         if (loadmodel->surfmesh.data_skeletalindex4ub)
3021                                 Vector4Copy(loadmodel->surfmesh.data_skeletalindex4ub + 4*vertexindex, vertexmesh->skeletalindex4ub);
3022                         if (loadmodel->surfmesh.data_skeletalweight4ub)
3023                                 Vector4Copy(loadmodel->surfmesh.data_skeletalweight4ub + 4*vertexindex, vertexmesh->skeletalweight4ub);
3024                 }
3025         }
3026
3027         // upload short indices as a buffer
3028         if (loadmodel->surfmesh.data_element3s && !loadmodel->surfmesh.data_element3s_indexbuffer)
3029                 loadmodel->surfmesh.data_element3s_indexbuffer = R_Mesh_CreateMeshBuffer(loadmodel->surfmesh.data_element3s, loadmodel->surfmesh.num_triangles * sizeof(short[3]), loadmodel->name, true, false, false, true);
3030
3031         // upload int indices as a buffer
3032         if (loadmodel->surfmesh.data_element3i && !loadmodel->surfmesh.data_element3i_indexbuffer && !loadmodel->surfmesh.data_element3s)
3033                 loadmodel->surfmesh.data_element3i_indexbuffer = R_Mesh_CreateMeshBuffer(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles * sizeof(int[3]), loadmodel->name, true, false, false, false);
3034
3035         // only build a vbo if one has not already been created (this is important for brush models which load specially)
3036         // vertex buffer is several arrays and we put them in the same buffer
3037         //
3038         // is this wise?  the texcoordtexture2f array is used with dynamic
3039         // vertex/svector/tvector/normal when rendering animated models, on the
3040         // other hand animated models don't use a lot of vertices anyway...
3041         if (!loadmodel->surfmesh.vbo_vertexbuffer && !vid.useinterleavedarrays)
3042         {
3043                 int size;
3044                 unsigned char *mem;
3045                 size = 0;
3046                 loadmodel->surfmesh.vbooffset_vertexmesh         = size;if (loadmodel->surfmesh.data_vertexmesh        ) size += loadmodel->surfmesh.num_vertices * sizeof(r_vertexmesh_t);
3047                 loadmodel->surfmesh.vbooffset_vertex3f           = size;if (loadmodel->surfmesh.data_vertex3f          ) size += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
3048                 loadmodel->surfmesh.vbooffset_svector3f          = size;if (loadmodel->surfmesh.data_svector3f         ) size += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
3049                 loadmodel->surfmesh.vbooffset_tvector3f          = size;if (loadmodel->surfmesh.data_tvector3f         ) size += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
3050                 loadmodel->surfmesh.vbooffset_normal3f           = size;if (loadmodel->surfmesh.data_normal3f          ) size += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
3051                 loadmodel->surfmesh.vbooffset_texcoordtexture2f  = size;if (loadmodel->surfmesh.data_texcoordtexture2f ) size += loadmodel->surfmesh.num_vertices * sizeof(float[2]);
3052                 loadmodel->surfmesh.vbooffset_texcoordlightmap2f = size;if (loadmodel->surfmesh.data_texcoordlightmap2f) size += loadmodel->surfmesh.num_vertices * sizeof(float[2]);
3053                 loadmodel->surfmesh.vbooffset_lightmapcolor4f    = size;if (loadmodel->surfmesh.data_lightmapcolor4f   ) size += loadmodel->surfmesh.num_vertices * sizeof(float[4]);
3054                 loadmodel->surfmesh.vbooffset_skeletalindex4ub   = size;if (loadmodel->surfmesh.data_skeletalindex4ub  ) size += loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]);
3055                 loadmodel->surfmesh.vbooffset_skeletalweight4ub  = size;if (loadmodel->surfmesh.data_skeletalweight4ub ) size += loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]);
3056                 mem = (unsigned char *)Mem_Alloc(tempmempool, size);
3057                 if (loadmodel->surfmesh.data_vertexmesh        ) memcpy(mem + loadmodel->surfmesh.vbooffset_vertexmesh        , loadmodel->surfmesh.data_vertexmesh        , loadmodel->surfmesh.num_vertices * sizeof(r_vertexmesh_t));
3058                 if (loadmodel->surfmesh.data_vertex3f          ) memcpy(mem + loadmodel->surfmesh.vbooffset_vertex3f          , loadmodel->surfmesh.data_vertex3f          , loadmodel->surfmesh.num_vertices * sizeof(float[3]));
3059                 if (loadmodel->surfmesh.data_svector3f         ) memcpy(mem + loadmodel->surfmesh.vbooffset_svector3f         , loadmodel->surfmesh.data_svector3f         , loadmodel->surfmesh.num_vertices * sizeof(float[3]));
3060                 if (loadmodel->surfmesh.data_tvector3f         ) memcpy(mem + loadmodel->surfmesh.vbooffset_tvector3f         , loadmodel->surfmesh.data_tvector3f         , loadmodel->surfmesh.num_vertices * sizeof(float[3]));
3061                 if (loadmodel->surfmesh.data_normal3f          ) memcpy(mem + loadmodel->surfmesh.vbooffset_normal3f          , loadmodel->surfmesh.data_normal3f          , loadmodel->surfmesh.num_vertices * sizeof(float[3]));
3062                 if (loadmodel->surfmesh.data_texcoordtexture2f ) memcpy(mem + loadmodel->surfmesh.vbooffset_texcoordtexture2f , loadmodel->surfmesh.data_texcoordtexture2f , loadmodel->surfmesh.num_vertices * sizeof(float[2]));
3063                 if (loadmodel->surfmesh.data_texcoordlightmap2f) memcpy(mem + loadmodel->surfmesh.vbooffset_texcoordlightmap2f, loadmodel->surfmesh.data_texcoordlightmap2f, loadmodel->surfmesh.num_vertices * sizeof(float[2]));
3064                 if (loadmodel->surfmesh.data_lightmapcolor4f   ) memcpy(mem + loadmodel->surfmesh.vbooffset_lightmapcolor4f   , loadmodel->surfmesh.data_lightmapcolor4f   , loadmodel->surfmesh.num_vertices * sizeof(float[4]));
3065                 if (loadmodel->surfmesh.data_skeletalindex4ub  ) memcpy(mem + loadmodel->surfmesh.vbooffset_skeletalindex4ub  , loadmodel->surfmesh.data_skeletalindex4ub  , loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]));
3066                 if (loadmodel->surfmesh.data_skeletalweight4ub ) memcpy(mem + loadmodel->surfmesh.vbooffset_skeletalweight4ub , loadmodel->surfmesh.data_skeletalweight4ub , loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]));
3067                 loadmodel->surfmesh.vbo_vertexbuffer = R_Mesh_CreateMeshBuffer(mem, size, loadmodel->name, false, false, false, false);
3068                 Mem_Free(mem);
3069         }
3070 }
3071
3072 extern cvar_t mod_obj_orientation;
3073 static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const char *mtlfilename, const char *originalfilename)
3074 {
3075         int submodelindex, vertexindex, surfaceindex, triangleindex, textureindex, countvertices = 0, countsurfaces = 0, countfaces = 0, counttextures = 0;
3076         int a, b, c;
3077         const char *texname;
3078         const int *e;
3079         const float *v, *vn, *vt;
3080         size_t l;
3081         size_t outbufferpos = 0;
3082         size_t outbuffermax = 0x100000;
3083         char *outbuffer = (char *) Z_Malloc(outbuffermax), *oldbuffer;
3084         const msurface_t *surface;
3085         const int maxtextures = 256;
3086         char *texturenames = (char *) Z_Malloc(maxtextures * MAX_QPATH);
3087         dp_model_t *submodel;
3088
3089         // construct the mtllib file
3090         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "# mtllib for %s exported by darkplaces engine\n", originalfilename);
3091         if (l > 0)
3092                 outbufferpos += l;
3093         for (surfaceindex = 0, surface = model->data_surfaces;surfaceindex < model->num_surfaces;surfaceindex++, surface++)
3094         {
3095                 countsurfaces++;
3096                 countvertices += surface->num_vertices;
3097                 countfaces += surface->num_triangles;
3098                 texname = (surface->texture && surface->texture->name[0]) ? surface->texture->name : "default";
3099                 for (textureindex = 0;textureindex < counttextures;textureindex++)
3100                         if (!strcmp(texturenames + textureindex * MAX_QPATH, texname))
3101                                 break;
3102                 if (textureindex < counttextures)
3103                         continue; // already wrote this material entry
3104                 if (textureindex >= maxtextures)
3105                         continue; // just a precaution
3106                 textureindex = counttextures++;
3107                 strlcpy(texturenames + textureindex * MAX_QPATH, texname, MAX_QPATH);
3108                 if (outbufferpos >= outbuffermax >> 1)
3109                 {
3110                         outbuffermax *= 2;
3111                         oldbuffer = outbuffer;
3112                         outbuffer = (char *) Z_Malloc(outbuffermax);
3113                         memcpy(outbuffer, oldbuffer, outbufferpos);
3114                         Z_Free(oldbuffer);
3115                 }
3116                 l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "newmtl %s\nNs 96.078431\nKa 0 0 0\nKd 0.64 0.64 0.64\nKs 0.5 0.5 0.5\nNi 1\nd 1\nillum 2\nmap_Kd %s%s\n\n", texname, texname, strstr(texname, ".tga") ? "" : ".tga");
3117                 if (l > 0)
3118                         outbufferpos += l;
3119         }
3120
3121         // write the mtllib file
3122         FS_WriteFile(mtlfilename, outbuffer, outbufferpos);
3123
3124         // construct the obj file
3125         outbufferpos = 0;
3126         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "# model exported from %s by darkplaces engine\n# %i vertices, %i faces, %i surfaces\nmtllib %s\n", originalfilename, countvertices, countfaces, countsurfaces, mtlfilename);
3127         if (l > 0)
3128                 outbufferpos += l;
3129
3130         for (vertexindex = 0, v = model->surfmesh.data_vertex3f, vn = model->surfmesh.data_normal3f, vt = model->surfmesh.data_texcoordtexture2f;vertexindex < model->surfmesh.num_vertices;vertexindex++, v += 3, vn += 3, vt += 2)
3131         {
3132                 if (outbufferpos >= outbuffermax >> 1)
3133                 {
3134                         outbuffermax *= 2;
3135                         oldbuffer = outbuffer;
3136                         outbuffer = (char *) Z_Malloc(outbuffermax);
3137                         memcpy(outbuffer, oldbuffer, outbufferpos);
3138                         Z_Free(oldbuffer);
3139                 }
3140                 if(mod_obj_orientation.integer)
3141                         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "v %f %f %f\nvn %f %f %f\nvt %f %f\n", v[0], v[2], v[1], vn[0], vn[2], vn[1], vt[0], 1-vt[1]);
3142                 else
3143                         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "v %f %f %f\nvn %f %f %f\nvt %f %f\n", v[0], v[1], v[2], vn[0], vn[1], vn[2], vt[0], 1-vt[1]);
3144                 if (l > 0)
3145                         outbufferpos += l;
3146         }
3147
3148         for (submodelindex = 0;submodelindex < max(1, model->brush.numsubmodels);submodelindex++)
3149         {
3150                 l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "o %i\n", submodelindex);
3151                 if (l > 0)
3152                         outbufferpos += l;
3153                 submodel = model->brush.numsubmodels ? model->brush.submodels[submodelindex] : model;
3154                 for (surfaceindex = 0;surfaceindex < submodel->nummodelsurfaces;surfaceindex++)
3155                 {
3156                         surface = model->data_surfaces + submodel->sortedmodelsurfaces[surfaceindex];
3157                         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "usemtl %s\n", (surface->texture && surface->texture->name[0]) ? surface->texture->name : "default");
3158                         if (l > 0)
3159                                 outbufferpos += l;
3160                         for (triangleindex = 0, e = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
3161                         {
3162                                 if (outbufferpos >= outbuffermax >> 1)
3163                                 {
3164                                         outbuffermax *= 2;
3165                                         oldbuffer = outbuffer;
3166                                         outbuffer = (char *) Z_Malloc(outbuffermax);
3167                                         memcpy(outbuffer, oldbuffer, outbufferpos);
3168                                         Z_Free(oldbuffer);
3169                                 }
3170                                 a = e[0]+1;
3171                                 b = e[1]+1;
3172                                 c = e[2]+1;
3173                                 if(mod_obj_orientation.integer)
3174                                         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", a,a,a,b,b,b,c,c,c);
3175                                 else
3176                                         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", a,a,a,c,c,c,b,b,b);
3177                                 if (l > 0)
3178                                         outbufferpos += l;
3179                         }
3180                 }
3181         }
3182
3183         // write the obj file
3184         FS_WriteFile(filename, outbuffer, outbufferpos);
3185
3186         // clean up
3187         Z_Free(outbuffer);
3188         Z_Free(texturenames);
3189
3190         // print some stats
3191         Con_Printf("Wrote %s (%i bytes, %i vertices, %i faces, %i surfaces with %i distinct textures)\n", filename, (int)outbufferpos, countvertices, countfaces, countsurfaces, counttextures);
3192 }
3193
3194 static void Mod_Decompile_SMD(dp_model_t *model, const char *filename, int firstpose, int numposes, qboolean writetriangles)
3195 {
3196         int countnodes = 0, counttriangles = 0, countframes = 0;
3197         int surfaceindex;
3198         int triangleindex;
3199         int transformindex;
3200         int poseindex;
3201         int cornerindex;
3202         const int *e;
3203         size_t l;
3204         size_t outbufferpos = 0;
3205         size_t outbuffermax = 0x100000;
3206         char *outbuffer = (char *) Z_Malloc(outbuffermax), *oldbuffer;
3207         const msurface_t *surface;
3208         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "version 1\nnodes\n");
3209         if (l > 0)
3210                 outbufferpos += l;
3211         for (transformindex = 0;transformindex < model->num_bones;transformindex++)
3212         {
3213                 if (outbufferpos >= outbuffermax >> 1)
3214                 {
3215                         outbuffermax *= 2;
3216                         oldbuffer = outbuffer;
3217                         outbuffer = (char *) Z_Malloc(outbuffermax);
3218                         memcpy(outbuffer, oldbuffer, outbufferpos);
3219                         Z_Free(oldbuffer);
3220                 }
3221                 countnodes++;
3222                 l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "%3i \"%s\" %3i\n", transformindex, model->data_bones[transformindex].name, model->data_bones[transformindex].parent);
3223                 if (l > 0)
3224                         outbufferpos += l;
3225         }
3226         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "end\nskeleton\n");
3227         if (l > 0)
3228                 outbufferpos += l;
3229         for (poseindex = 0;poseindex < numposes;poseindex++)
3230         {
3231                 countframes++;
3232                 l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "time %i\n", poseindex);
3233                 if (l > 0)
3234                         outbufferpos += l;
3235                 for (transformindex = 0;transformindex < model->num_bones;transformindex++)
3236                 {
3237                         float angles[3];
3238                         float mtest[4][3];
3239                         matrix4x4_t posematrix;
3240                         if (outbufferpos >= outbuffermax >> 1)
3241                         {
3242                                 outbuffermax *= 2;
3243                                 oldbuffer = outbuffer;
3244                                 outbuffer = (char *) Z_Malloc(outbuffermax);
3245                                 memcpy(outbuffer, oldbuffer, outbufferpos);
3246                                 Z_Free(oldbuffer);
3247                         }
3248
3249                         // strangely the smd angles are for a transposed matrix, so we
3250                         // have to generate a transposed matrix, then convert that...
3251                         Matrix4x4_FromBonePose7s(&posematrix, model->num_posescale, model->data_poses7s + 7*(model->num_bones * poseindex + transformindex));
3252                         Matrix4x4_ToArray12FloatGL(&posematrix, mtest[0]);
3253                         AnglesFromVectors(angles, mtest[0], mtest[2], false);
3254                         if (angles[0] >= 180) angles[0] -= 360;
3255                         if (angles[1] >= 180) angles[1] -= 360;
3256                         if (angles[2] >= 180) angles[2] -= 360;
3257
3258 #if 0
3259 {
3260                         float a = DEG2RAD(angles[ROLL]);
3261                         float b = DEG2RAD(angles[PITCH]);
3262                         float c = DEG2RAD(angles[YAW]);
3263                         float cy, sy, cp, sp, cr, sr;
3264                         float test[4][3];
3265                         // smd matrix construction, for comparing
3266                         sy = sin(c);
3267                         cy = cos(c);
3268                         sp = sin(b);
3269                         cp = cos(b);
3270                         sr = sin(a);
3271                         cr = cos(a);
3272
3273                         test[0][0] = cp*cy;
3274                         test[0][1] = cp*sy;
3275                         test[0][2] = -sp;
3276                         test[1][0] = sr*sp*cy+cr*-sy;
3277                         test[1][1] = sr*sp*sy+cr*cy;
3278                         test[1][2] = sr*cp;
3279                         test[2][0] = (cr*sp*cy+-sr*-sy);
3280                         test[2][1] = (cr*sp*sy+-sr*cy);
3281                         test[2][2] = cr*cp;
3282                         test[3][0] = pose[9];
3283                         test[3][1] = pose[10];
3284                         test[3][2] = pose[11];
3285 }
3286 #endif
3287                         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "%3i %f %f %f %f %f %f\n", transformindex, mtest[3][0], mtest[3][1], mtest[3][2], DEG2RAD(angles[ROLL]), DEG2RAD(angles[PITCH]), DEG2RAD(angles[YAW]));
3288                         if (l > 0)
3289                                 outbufferpos += l;
3290                 }
3291         }
3292         l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "end\n");
3293         if (l > 0)
3294                 outbufferpos += l;
3295         if (writetriangles)
3296         {
3297                 l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "triangles\n");
3298                 if (l > 0)
3299                         outbufferpos += l;
3300                 for (surfaceindex = 0, surface = model->data_surfaces;surfaceindex < model->num_surfaces;surfaceindex++, surface++)
3301                 {
3302                         for (triangleindex = 0, e = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
3303                         {
3304                                 counttriangles++;
3305                                 if (outbufferpos >= outbuffermax >> 1)
3306                                 {
3307                                         outbuffermax *= 2;
3308                                         oldbuffer = outbuffer;
3309                                         outbuffer = (char *) Z_Malloc(outbuffermax);
3310                                         memcpy(outbuffer, oldbuffer, outbufferpos);
3311                                         Z_Free(oldbuffer);
3312                                 }
3313                                 l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "%s\n", surface->texture && surface->texture->name[0] ? surface->texture->name : "default.bmp");
3314                                 if (l > 0)
3315                                         outbufferpos += l;
3316                                 for (cornerindex = 0;cornerindex < 3;cornerindex++)
3317                                 {
3318                                         const int index = e[2-cornerindex];
3319                                         const float *v = model->surfmesh.data_vertex3f + index * 3;
3320                                         const float *vn = model->surfmesh.data_normal3f + index * 3;
3321                                         const float *vt = model->surfmesh.data_texcoordtexture2f + index * 2;
3322                                         const int b = model->surfmesh.blends[index];
3323                                         if (b < model->num_bones)
3324                                                 l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "%3i %f %f %f %f %f %f %f %f\n"                          , b, v[0], v[1], v[2], vn[0], vn[1], vn[2], vt[0], 1 - vt[1]);
3325                                         else
3326                                         {
3327                                                 const blendweights_t *w = model->surfmesh.data_blendweights + b - model->num_bones;
3328                                                 const unsigned char *wi = w->index;
3329                                                 const unsigned char *wf = w->influence;
3330                                             if (wf[3]) l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "%3i %f %f %f %f %f %f %f %f 4 %i %f %i %f %i %f %i %f\n", wi[0], v[0], v[1], v[2], vn[0], vn[1], vn[2], vt[0], 1 - vt[1], wi[0], wf[0]/255.0f, wi[1], wf[1]/255.0f, wi[2], wf[2]/255.0f, wi[3], wf[3]/255.0f);
3331                                                 else if (wf[2]) l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "%3i %f %f %f %f %f %f %f %f 3 %i %f %i %f %i %f\n"      , wi[0], v[0], v[1], v[2], vn[0], vn[1], vn[2], vt[0], 1 - vt[1], wi[0], wf[0]/255.0f, wi[1], wf[1]/255.0f, wi[2], wf[2]/255.0f);
3332                                                 else if (wf[1]) l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "%3i %f %f %f %f %f %f %f %f 2 %i %f %i %f\n"            , wi[0], v[0], v[1], v[2], vn[0], vn[1], vn[2], vt[0], 1 - vt[1], wi[0], wf[0]/255.0f, wi[1], wf[1]/255.0f);
3333                                                 else            l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "%3i %f %f %f %f %f %f %f %f\n"                          , wi[0], v[0], v[1], v[2], vn[0], vn[1], vn[2], vt[0], 1 - vt[1]);
3334                                         }
3335                                         if (l > 0)
3336                                                 outbufferpos += l;
3337                                 }
3338                         }
3339                 }
3340                 l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "end\n");
3341                 if (l > 0)
3342                         outbufferpos += l;
3343         }
3344
3345         FS_WriteFile(filename, outbuffer, outbufferpos);
3346         Z_Free(outbuffer);
3347
3348         Con_Printf("Wrote %s (%i bytes, %i nodes, %i frames, %i triangles)\n", filename, (int)outbufferpos, countnodes, countframes, counttriangles);
3349 }
3350
3351 /*
3352 ================
3353 Mod_Decompile_f
3354
3355 decompiles a model to editable files
3356 ================
3357 */
3358 static void Mod_Decompile_f(void)
3359 {
3360         int i, j, k, l, first, count;
3361         dp_model_t *mod;
3362         char inname[MAX_QPATH];
3363         char outname[MAX_QPATH];
3364         char mtlname[MAX_QPATH];
3365         char basename[MAX_QPATH];
3366         char animname[MAX_QPATH];
3367         char animname2[MAX_QPATH];
3368         char zymtextbuffer[16384];
3369         char dpmtextbuffer[16384];
3370         char framegroupstextbuffer[16384];
3371         int zymtextsize = 0;
3372         int dpmtextsize = 0;
3373         int framegroupstextsize = 0;
3374         char vabuf[1024];
3375
3376         if (Cmd_Argc() != 2)
3377         {
3378                 Con_Print("usage: modeldecompile <filename>\n");
3379                 return;
3380         }
3381
3382         strlcpy(inname, Cmd_Argv(1), sizeof(inname));
3383         FS_StripExtension(inname, basename, sizeof(basename));
3384
3385         mod = Mod_ForName(inname, false, true, inname[0] == '*' ? cl.model_name[1] : NULL);
3386         if (!mod)
3387         {
3388                 Con_Print("No such model\n");
3389                 return;
3390         }
3391         if (mod->brush.submodel)
3392         {
3393                 // if we're decompiling a submodel, be sure to give it a proper name based on its parent
3394                 FS_StripExtension(cl.model_name[1], outname, sizeof(outname));
3395                 dpsnprintf(basename, sizeof(basename), "%s/%s", outname, mod->name);
3396                 outname[0] = 0;
3397         }
3398         if (!mod->surfmesh.num_triangles)
3399         {
3400                 Con_Print("Empty model (or sprite)\n");
3401                 return;
3402         }
3403
3404         // export OBJ if possible (not on sprites)
3405         if (mod->surfmesh.num_triangles)
3406         {
3407                 dpsnprintf(outname, sizeof(outname), "%s_decompiled.obj", basename);
3408                 dpsnprintf(mtlname, sizeof(mtlname), "%s_decompiled.mtl", basename);
3409                 Mod_Decompile_OBJ(mod, outname, mtlname, inname);
3410         }
3411
3412         // export SMD if possible (only for skeletal models)
3413         if (mod->surfmesh.num_triangles && mod->num_bones)
3414         {
3415                 dpsnprintf(outname, sizeof(outname), "%s_decompiled/ref1.smd", basename);
3416                 Mod_Decompile_SMD(mod, outname, 0, 1, true);
3417                 l = dpsnprintf(zymtextbuffer + zymtextsize, sizeof(zymtextbuffer) - zymtextsize, "output out.zym\nscale 1\norigin 0 0 0\nmesh ref1.smd\n");
3418                 if (l > 0) zymtextsize += l;
3419                 l = dpsnprintf(dpmtextbuffer + dpmtextsize, sizeof(dpmtextbuffer) - dpmtextsize, "outputdir .\nmodel out\nscale 1\norigin 0 0 0\nscene ref1.smd\n");
3420                 if (l > 0) dpmtextsize += l;
3421                 for (i = 0;i < mod->numframes;i = j)
3422                 {
3423                         strlcpy(animname, mod->animscenes[i].name, sizeof(animname));
3424                         first = mod->animscenes[i].firstframe;
3425                         if (mod->animscenes[i].framecount > 1)
3426                         {
3427                                 // framegroup anim
3428                                 count = mod->animscenes[i].framecount;
3429                                 j = i + 1;
3430                         }
3431                         else
3432                         {
3433                                 // individual frame
3434                                 // check for additional frames with same name
3435                                 for (l = 0, k = (int)strlen(animname);animname[l];l++)
3436                                         if(animname[l] < '0' || animname[l] > '9')
3437                                                 k = l + 1;
3438                                 if(k > 0 && animname[k-1] == '_')
3439                                         --k;
3440                                 animname[k] = 0;
3441                                 count = mod->num_poses - first;
3442                                 for (j = i + 1;j < mod->numframes;j++)
3443                                 {
3444                                         strlcpy(animname2, mod->animscenes[j].name, sizeof(animname2));
3445                                         for (l = 0, k = (int)strlen(animname2);animname2[l];l++)
3446                                                 if(animname2[l] < '0' || animname2[l] > '9')
3447                                                         k = l + 1;
3448                                         if(k > 0 && animname[k-1] == '_')
3449                                                 --k;
3450                                         animname2[k] = 0;
3451                                         if (strcmp(animname2, animname) || mod->animscenes[j].framecount > 1)
3452                                         {
3453                                                 count = mod->animscenes[j].firstframe - first;
3454                                                 break;
3455                                         }
3456                                 }
3457                                 // if it's only one frame, use the original frame name
3458                                 if (j == i + 1)
3459                                         strlcpy(animname, mod->animscenes[i].name, sizeof(animname));
3460                                 
3461                         }
3462                         dpsnprintf(outname, sizeof(outname), "%s_decompiled/%s.smd", basename, animname);
3463                         Mod_Decompile_SMD(mod, outname, first, count, false);
3464                         if (zymtextsize < (int)sizeof(zymtextbuffer) - 100)
3465                         {
3466                                 l = dpsnprintf(zymtextbuffer + zymtextsize, sizeof(zymtextbuffer) - zymtextsize, "scene %s.smd fps %g %s\n", animname, mod->animscenes[i].framerate, mod->animscenes[i].loop ? "" : " noloop");
3467                                 if (l > 0) zymtextsize += l;
3468                         }
3469                         if (dpmtextsize < (int)sizeof(dpmtextbuffer) - 100)
3470                         {
3471                                 l = dpsnprintf(dpmtextbuffer + dpmtextsize, sizeof(dpmtextbuffer) - dpmtextsize, "scene %s.smd fps %g %s\n", animname, mod->animscenes[i].framerate, mod->animscenes[i].loop ? "" : " noloop");
3472                                 if (l > 0) dpmtextsize += l;
3473                         }
3474                         if (framegroupstextsize < (int)sizeof(framegroupstextbuffer) - 100)
3475                         {
3476                                 l = dpsnprintf(framegroupstextbuffer + framegroupstextsize, sizeof(framegroupstextbuffer) - framegroupstextsize, "%d %d %f %d // %s\n", first, count, mod->animscenes[i].framerate, mod->animscenes[i].loop, animname);
3477                                 if (l > 0) framegroupstextsize += l;
3478                         }
3479                 }
3480                 if (zymtextsize)
3481                         FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_decompiled/out_zym.txt", basename), zymtextbuffer, (fs_offset_t)zymtextsize);
3482                 if (dpmtextsize)
3483                         FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_decompiled/out_dpm.txt", basename), dpmtextbuffer, (fs_offset_t)dpmtextsize);
3484                 if (framegroupstextsize)
3485                         FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_decompiled.framegroups", basename), framegroupstextbuffer, (fs_offset_t)framegroupstextsize);
3486         }
3487 }
3488
3489 void Mod_AllocLightmap_Init(mod_alloclightmap_state_t *state, mempool_t *mempool, int width, int height)
3490 {
3491         int y;
3492         memset(state, 0, sizeof(*state));
3493         state->width = width;
3494         state->height = height;
3495         state->currentY = 0;
3496         state->rows = (mod_alloclightmap_row_t *)Mem_Alloc(mempool, state->height * sizeof(*state->rows));
3497         for (y = 0;y < state->height;y++)
3498         {
3499                 state->rows[y].currentX = 0;
3500                 state->rows[y].rowY = -1;
3501         }
3502 }
3503
3504 void Mod_AllocLightmap_Reset(mod_alloclightmap_state_t *state)
3505 {
3506         int y;
3507         state->currentY = 0;
3508         for (y = 0;y < state->height;y++)
3509         {
3510                 state->rows[y].currentX = 0;
3511                 state->rows[y].rowY = -1;
3512         }
3513 }
3514
3515 void Mod_AllocLightmap_Free(mod_alloclightmap_state_t *state)
3516 {
3517         if (state->rows)
3518                 Mem_Free(state->rows);
3519         memset(state, 0, sizeof(*state));
3520 }
3521
3522 qboolean Mod_AllocLightmap_Block(mod_alloclightmap_state_t *state, int blockwidth, int blockheight, int *outx, int *outy)
3523 {
3524         mod_alloclightmap_row_t *row;
3525         int y;
3526
3527         row = state->rows + blockheight;
3528         if ((row->rowY < 0) || (row->currentX + blockwidth > state->width))
3529         {
3530                 if (state->currentY + blockheight <= state->height)
3531                 {
3532                         // use the current allocation position
3533                         row->rowY = state->currentY;
3534                         row->currentX = 0;
3535                         state->currentY += blockheight;
3536                 }
3537                 else
3538                 {
3539                         // find another position
3540                         for (y = blockheight;y < state->height;y++)
3541                         {
3542                                 if ((state->rows[y].rowY >= 0) && (state->rows[y].currentX + blockwidth <= state->width))
3543                                 {
3544                                         row = state->rows + y;
3545                                         break;
3546                                 }
3547                         }
3548                         if (y == state->height)
3549                                 return false;
3550                 }
3551         }
3552         *outy = row->rowY;
3553         *outx = row->currentX;
3554         row->currentX += blockwidth;
3555
3556         return true;
3557 }
3558
3559 typedef struct lightmapsample_s
3560 {
3561         float pos[3];
3562         float sh1[4][3];
3563         float *vertex_color;
3564         unsigned char *lm_bgr;
3565         unsigned char *lm_dir;
3566 }
3567 lightmapsample_t;
3568
3569 typedef struct lightmapvertex_s
3570 {
3571         int index;
3572         float pos[3];
3573         float normal[3];
3574         float texcoordbase[2];
3575         float texcoordlightmap[2];
3576         float lightcolor[4];
3577 }
3578 lightmapvertex_t;
3579
3580 typedef struct lightmaptriangle_s
3581 {
3582         int triangleindex;
3583         int surfaceindex;
3584         int lightmapindex;
3585         int axis;
3586         int lmoffset[2];
3587         int lmsize[2];
3588         // 2D modelspace coordinates of min corner
3589         // snapped to lightmap grid but not in grid coordinates
3590         float lmbase[2];
3591         // 2D modelspace to lightmap coordinate scale
3592         float lmscale[2];
3593         float vertex[3][3];
3594         float mins[3];
3595         float maxs[3];
3596 }
3597 lightmaptriangle_t;
3598
3599 typedef struct lightmaplight_s
3600 {
3601         float origin[3];
3602         float radius;
3603         float iradius;
3604         float radius2;
3605         float color[3];
3606         svbsp_t svbsp;
3607 }
3608 lightmaplight_t;
3609
3610 lightmaptriangle_t *mod_generatelightmaps_lightmaptriangles;
3611
3612 #define MAX_LIGHTMAPSAMPLES 64
3613 static int mod_generatelightmaps_numoffsets[3];
3614 static float mod_generatelightmaps_offsets[3][MAX_LIGHTMAPSAMPLES][3];
3615
3616 static int mod_generatelightmaps_numlights;
3617 static lightmaplight_t *mod_generatelightmaps_lightinfo;
3618
3619 extern cvar_t r_shadow_lightattenuationdividebias;
3620 extern cvar_t r_shadow_lightattenuationlinearscale;
3621
3622 static void Mod_GenerateLightmaps_LightPoint(dp_model_t *model, const vec3_t pos, vec3_t ambient, vec3_t diffuse, vec3_t lightdir)
3623 {
3624         int i;
3625         int index;
3626         int result;
3627         float relativepoint[3];
3628         float color[3];
3629         float dir[3];
3630         float dist;
3631         float dist2;
3632         float intensity;
3633         float sample[5*3];
3634         float lightorigin[3];
3635         float lightradius;
3636         float lightradius2;
3637         float lightiradius;
3638         float lightcolor[3];
3639         trace_t trace;
3640         for (i = 0;i < 5*3;i++)
3641                 sample[i] = 0.0f;
3642         for (index = 0;;index++)
3643         {
3644                 result = R_Shadow_GetRTLightInfo(index, lightorigin, &lightradius, lightcolor);
3645                 if (result < 0)
3646                         break;
3647                 if (result == 0)
3648                         continue;
3649                 lightradius2 = lightradius * lightradius;
3650                 VectorSubtract(lightorigin, pos, relativepoint);
3651                 dist2 = VectorLength2(relativepoint);
3652                 if (dist2 >= lightradius2)
3653                         continue;
3654                 lightiradius = 1.0f / lightradius;
3655                 dist = sqrt(dist2) * lightiradius;
3656                 intensity = (1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
3657                 if (intensity <= 0.0f)
3658                         continue;
3659                 if (model && model->TraceLine)
3660                 {
3661                         model->TraceLine(model, NULL, NULL, &trace, pos, lightorigin, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT | MATERIALFLAG_NOSHADOW);
3662                         if (trace.fraction < 1)
3663                                 continue;
3664                 }
3665                 // scale down intensity to add to both ambient and diffuse
3666                 //intensity *= 0.5f;
3667                 VectorNormalize(relativepoint);
3668                 VectorScale(lightcolor, intensity, color);
3669                 VectorMA(sample    , 0.5f            , color, sample    );
3670                 VectorMA(sample + 3, relativepoint[0], color, sample + 3);
3671                 VectorMA(sample + 6, relativepoint[1], color, sample + 6);
3672                 VectorMA(sample + 9, relativepoint[2], color, sample + 9);
3673                 // calculate a weighted average light direction as well
3674                 intensity *= VectorLength(color);
3675                 VectorMA(sample + 12, intensity, relativepoint, sample + 12);
3676         }
3677         // calculate the direction we'll use to reduce the sample to a directional light source
3678         VectorCopy(sample + 12, dir);
3679         //VectorSet(dir, sample[3] + sample[4] + sample[5], sample[6] + sample[7] + sample[8], sample[9] + sample[10] + sample[11]);
3680         VectorNormalize(dir);
3681         // extract the diffuse color along the chosen direction and scale it
3682         diffuse[0] = (dir[0]*sample[3] + dir[1]*sample[6] + dir[2]*sample[ 9] + sample[ 0]);
3683         diffuse[1] = (dir[0]*sample[4] + dir[1]*sample[7] + dir[2]*sample[10] + sample[ 1]);
3684         diffuse[2] = (dir[0]*sample[5] + dir[1]*sample[8] + dir[2]*sample[11] + sample[ 2]);
3685         // subtract some of diffuse from ambient
3686         VectorMA(sample, -0.333f, diffuse, ambient);
3687         // store the normalized lightdir
3688         VectorCopy(dir, lightdir);
3689 }
3690
3691 static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP_InsertSurfaces(const dp_model_t *model, svbsp_t *svbsp, const float *mins, const float *maxs)
3692 {
3693         int surfaceindex;
3694         int triangleindex;
3695         const msurface_t *surface;
3696         const float *vertex3f = model->surfmesh.data_vertex3f;
3697         const int *element3i = model->surfmesh.data_element3i;
3698         const int *e;
3699         float v2[3][3];
3700         for (surfaceindex = 0, surface = model->data_surfaces;surfaceindex < model->nummodelsurfaces;surfaceindex++, surface++)
3701         {
3702                 if (!BoxesOverlap(surface->mins, surface->maxs, mins, maxs))
3703                         continue;
3704                 if (surface->texture->basematerialflags & MATERIALFLAG_NOSHADOW)
3705                         continue;
3706                 for (triangleindex = 0, e = element3i + 3*surface->num_firsttriangle;triangleindex < surface->num_triangles;triangleindex++, e += 3)
3707                 {
3708                         VectorCopy(vertex3f + 3*e[0], v2[0]);
3709                         VectorCopy(vertex3f + 3*e[1], v2[1]);
3710                         VectorCopy(vertex3f + 3*e[2], v2[2]);
3711                         SVBSP_AddPolygon(svbsp, 3, v2[0], true, NULL, NULL, 0);
3712                 }
3713         }
3714 }
3715
3716 static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP(dp_model_t *model, lightmaplight_t *lightinfo)
3717 {
3718         int maxnodes = 1<<14;
3719         svbsp_node_t *nodes;
3720         float origin[3];
3721         float mins[3];
3722         float maxs[3];
3723         svbsp_t svbsp;
3724         VectorSet(mins, lightinfo->origin[0] - lightinfo->radius, lightinfo->origin[1] - lightinfo->radius, lightinfo->origin[2] - lightinfo->radius);
3725         VectorSet(maxs, lightinfo->origin[0] + lightinfo->radius, lightinfo->origin[1] + lightinfo->radius, lightinfo->origin[2] + lightinfo->radius);
3726         VectorCopy(lightinfo->origin, origin);
3727         nodes = (svbsp_node_t *)Mem_Alloc(tempmempool, maxnodes * sizeof(*nodes));
3728         for (;;)
3729         {
3730                 SVBSP_Init(&svbsp, origin, maxnodes, nodes);
3731                 Mod_GenerateLightmaps_CreateLights_ComputeSVBSP_InsertSurfaces(model, &svbsp, mins, maxs);
3732                 if (svbsp.ranoutofnodes)
3733                 {
3734                         maxnodes *= 16;
3735                         if (maxnodes > 1<<22)
3736                         {
3737                                 Mem_Free(nodes);
3738                                 return;
3739                         }
3740                         Mem_Free(nodes);
3741                         nodes = (svbsp_node_t *)Mem_Alloc(tempmempool, maxnodes * sizeof(*nodes));
3742                 }
3743                 else
3744                         break;
3745         }
3746         if (svbsp.numnodes > 0)
3747         {
3748                 svbsp.nodes = (svbsp_node_t *)Mem_Alloc(tempmempool, svbsp.numnodes * sizeof(*nodes));
3749                 memcpy(svbsp.nodes, nodes, svbsp.numnodes * sizeof(*nodes));
3750                 lightinfo->svbsp = svbsp;
3751         }
3752         Mem_Free(nodes);
3753 }
3754
3755 static void Mod_GenerateLightmaps_CreateLights(dp_model_t *model)
3756 {
3757         int index;
3758         int result;
3759         lightmaplight_t *lightinfo;
3760         float origin[3];
3761         float radius;
3762         float color[3];
3763         mod_generatelightmaps_numlights = 0;
3764         for (index = 0;;index++)
3765         {
3766                 result = R_Shadow_GetRTLightInfo(index, origin, &radius, color);
3767                 if (result < 0)
3768                         break;
3769                 if (result > 0)
3770                         mod_generatelightmaps_numlights++;
3771         }
3772         if (mod_generatelightmaps_numlights > 0)
3773         {
3774                 mod_generatelightmaps_lightinfo = (lightmaplight_t *)Mem_Alloc(tempmempool, mod_generatelightmaps_numlights * sizeof(*mod_generatelightmaps_lightinfo));
3775                 lightinfo = mod_generatelightmaps_lightinfo;
3776                 for (index = 0;;index++)
3777                 {
3778                         result = R_Shadow_GetRTLightInfo(index, lightinfo->origin, &lightinfo->radius, lightinfo->color);
3779                         if (result < 0)
3780                                 break;
3781                         if (result > 0)
3782                                 lightinfo++;
3783                 }
3784         }
3785         for (index = 0, lightinfo = mod_generatelightmaps_lightinfo;index < mod_generatelightmaps_numlights;index++, lightinfo++)
3786         {
3787                 lightinfo->iradius = 1.0f / lightinfo->radius;
3788                 lightinfo->radius2 = lightinfo->radius * lightinfo->radius;
3789                 // TODO: compute svbsp
3790                 Mod_GenerateLightmaps_CreateLights_ComputeSVBSP(model, lightinfo);
3791         }
3792 }
3793
3794 static void Mod_GenerateLightmaps_DestroyLights(dp_model_t *model)
3795 {
3796         int i;
3797         if (mod_generatelightmaps_lightinfo)
3798         {
3799                 for (i = 0;i < mod_generatelightmaps_numlights;i++)
3800                         if (mod_generatelightmaps_lightinfo[i].svbsp.nodes)
3801                                 Mem_Free(mod_generatelightmaps_lightinfo[i].svbsp.nodes);
3802                 Mem_Free(mod_generatelightmaps_lightinfo);
3803         }
3804         mod_generatelightmaps_lightinfo = NULL;
3805         mod_generatelightmaps_numlights = 0;
3806 }
3807
3808 static qboolean Mod_GenerateLightmaps_SamplePoint_SVBSP(const svbsp_t *svbsp, const float *pos)
3809 {
3810         const svbsp_node_t *node;
3811         const svbsp_node_t *nodes = svbsp->nodes;
3812         int num = 0;
3813         while (num >= 0)
3814         {
3815                 node = nodes + num;
3816                 num = node->children[DotProduct(node->plane, pos) < node->plane[3]];
3817         }
3818         return num == -1; // true if empty, false if solid (shadowed)
3819 }
3820
3821 static void Mod_GenerateLightmaps_SamplePoint(const float *pos, const float *normal, float *sample, int numoffsets, const float *offsets)
3822 {
3823         int i;
3824         float relativepoint[3];
3825         float color[3];
3826         float offsetpos[3];
3827         float dist;
3828         float dist2;
3829         float intensity;
3830         int offsetindex;
3831         int hits;
3832         int tests;
3833         const lightmaplight_t *lightinfo;
3834         trace_t trace;
3835         for (i = 0;i < 5*3;i++)
3836                 sample[i] = 0.0f;
3837         for (i = 0, lightinfo = mod_generatelightmaps_lightinfo;i < mod_generatelightmaps_numlights;i++, lightinfo++)
3838         {
3839                 //R_SampleRTLights(pos, sample, numoffsets, offsets);
3840                 VectorSubtract(lightinfo->origin, pos, relativepoint);
3841                 // don't accept light from behind a surface, it causes bad shading
3842                 if (normal && DotProduct(relativepoint, normal) <= 0)
3843                         continue;
3844                 dist2 = VectorLength2(relativepoint);
3845                 if (dist2 >= lightinfo->radius2)
3846                         continue;
3847                 dist = sqrt(dist2) * lightinfo->iradius;
3848                 intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
3849                 if (intensity <= 0)
3850                         continue;
3851                 if (cl.worldmodel && cl.worldmodel->TraceLine && numoffsets > 0)
3852                 {
3853                         hits = 0;
3854                         tests = 1;
3855                         if (Mod_GenerateLightmaps_SamplePoint_SVBSP(&lightinfo->svbsp, pos))
3856                                 hits++;
3857                         for (offsetindex = 1;offsetindex < numoffsets;offsetindex++)
3858                         {
3859                                 VectorAdd(pos, offsets + 3*offsetindex, offsetpos);
3860                                 if (!normal)
3861                                 {
3862                                         // for light grid we'd better check visibility of the offset point
3863                                         cl.worldmodel->TraceLine(cl.worldmodel, NULL, NULL, &trace, pos, offsetpos, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT | MATERIALFLAG_NOSHADOW);
3864                                         if (trace.fraction < 1)
3865                                                 VectorLerp(pos, trace.fraction, offsetpos, offsetpos);
3866                                 }
3867                                 tests++;
3868                                 if (Mod_GenerateLightmaps_SamplePoint_SVBSP(&lightinfo->svbsp, offsetpos))
3869                                         hits++;
3870                         }
3871                         if (!hits)
3872                                 continue;
3873                         // scale intensity according to how many rays succeeded
3874                         // we know one test is valid, half of the rest will fail...
3875                         //if (normal && tests > 1)
3876                         //      intensity *= (tests - 1.0f) / tests;
3877                         intensity *= (float)hits / tests;
3878                 }
3879                 // scale down intensity to add to both ambient and diffuse
3880                 //intensity *= 0.5f;
3881                 VectorNormalize(relativepoint);
3882                 VectorScale(lightinfo->color, intensity, color);
3883                 VectorMA(sample    , 0.5f            , color, sample    );
3884                 VectorMA(sample + 3, relativepoint[0], color, sample + 3);
3885                 VectorMA(sample + 6, relativepoint[1], color, sample + 6);
3886                 VectorMA(sample + 9, relativepoint[2], color, sample + 9);
3887                 // calculate a weighted average light direction as well
3888                 intensity *= VectorLength(color);
3889                 VectorMA(sample + 12, intensity, relativepoint, sample + 12);
3890         }
3891 }
3892
3893 static void Mod_GenerateLightmaps_LightmapSample(const float *pos, const float *normal, unsigned char *lm_bgr, unsigned char *lm_dir)
3894 {
3895         float sample[5*3];
3896         float color[3];
3897         float dir[3];
3898         float f;
3899         Mod_GenerateLightmaps_SamplePoint(pos, normal, sample, mod_generatelightmaps_numoffsets[0], mod_generatelightmaps_offsets[0][0]);
3900         //VectorSet(dir, sample[3] + sample[4] + sample[5], sample[6] + sample[7] + sample[8], sample[9] + sample[10] + sample[11]);
3901         VectorCopy(sample + 12, dir);
3902         VectorNormalize(dir);
3903         //VectorAdd(dir, normal, dir);
3904         //VectorNormalize(dir);
3905         f = DotProduct(dir, normal);
3906         f = max(0, f) * 255.0f;
3907         VectorScale(sample, f, color);
3908         //VectorCopy(normal, dir);
3909         VectorSet(dir, (dir[0]+1.0f)*127.5f, (dir[1]+1.0f)*127.5f, (dir[2]+1.0f)*127.5f);
3910         lm_bgr[0] = (unsigned char)bound(0.0f, color[2], 255.0f);
3911         lm_bgr[1] = (unsigned char)bound(0.0f, color[1], 255.0f);
3912         lm_bgr[2] = (unsigned char)bound(0.0f, color[0], 255.0f);
3913         lm_bgr[3] = 255;
3914         lm_dir[0] = (unsigned char)dir[2];
3915         lm_dir[1] = (unsigned char)dir[1];
3916         lm_dir[2] = (unsigned char)dir[0];
3917         lm_dir[3] = 255;
3918 }
3919
3920 static void Mod_GenerateLightmaps_VertexSample(const float *pos, const float *normal, float *vertex_color)
3921 {
3922         float sample[5*3];
3923         Mod_GenerateLightmaps_SamplePoint(pos, normal, sample, mod_generatelightmaps_numoffsets[1], mod_generatelightmaps_offsets[1][0]);
3924         VectorCopy(sample, vertex_color);
3925 }
3926
3927 static void Mod_GenerateLightmaps_GridSample(const float *pos, q3dlightgrid_t *s)
3928 {
3929         float sample[5*3];
3930         float ambient[3];
3931         float diffuse[3];
3932         float dir[3];
3933         Mod_GenerateLightmaps_SamplePoint(pos, NULL, sample, mod_generatelightmaps_numoffsets[2], mod_generatelightmaps_offsets[2][0]);
3934         // calculate the direction we'll use to reduce the sample to a directional light source
3935         VectorCopy(sample + 12, dir);
3936         //VectorSet(dir, sample[3] + sample[4] + sample[5], sample[6] + sample[7] + sample[8], sample[9] + sample[10] + sample[11]);
3937         VectorNormalize(dir);
3938         // extract the diffuse color along the chosen direction and scale it
3939         diffuse[0] = (dir[0]*sample[3] + dir[1]*sample[6] + dir[2]*sample[ 9] + sample[ 0]) * 127.5f;
3940         diffuse[1] = (dir[0]*sample[4] + dir[1]*sample[7] + dir[2]*sample[10] + sample[ 1]) * 127.5f;
3941         diffuse[2] = (dir[0]*sample[5] + dir[1]*sample[8] + dir[2]*sample[11] + sample[ 2]) * 127.5f;
3942         // scale the ambient from 0-2 to 0-255 and subtract some of diffuse
3943         VectorScale(sample, 127.5f, ambient);
3944         VectorMA(ambient, -0.333f, diffuse, ambient);
3945         // encode to the grid format
3946         s->ambientrgb[0] = (unsigned char)bound(0.0f, ambient[0], 255.0f);
3947         s->ambientrgb[1] = (unsigned char)bound(0.0f, ambient[1], 255.0f);
3948         s->ambientrgb[2] = (unsigned char)bound(0.0f, ambient[2], 255.0f);
3949         s->diffusergb[0] = (unsigned char)bound(0.0f, diffuse[0], 255.0f);
3950         s->diffusergb[1] = (unsigned char)bound(0.0f, diffuse[1], 255.0f);
3951         s->diffusergb[2] = (unsigned char)bound(0.0f, diffuse[2], 255.0f);
3952         if (dir[2] >= 0.99f) {s->diffusepitch = 0;s->diffuseyaw = 0;}
3953         else if (dir[2] <= -0.99f) {s->diffusepitch = 128;s->diffuseyaw = 0;}
3954         else {s->diffusepitch = (unsigned char)(acos(dir[2]) * (127.5f/M_PI));s->diffuseyaw = (unsigned char)(atan2(dir[1], dir[0]) * (127.5f/M_PI));}
3955 }
3956
3957 static void Mod_GenerateLightmaps_InitSampleOffsets(dp_model_t *model)
3958 {
3959         float radius[3];
3960         float temp[3];
3961         int i, j;
3962         memset(mod_generatelightmaps_offsets, 0, sizeof(mod_generatelightmaps_offsets));
3963         mod_generatelightmaps_numoffsets[0] = min(MAX_LIGHTMAPSAMPLES, mod_generatelightmaps_lightmapsamples.integer);
3964         mod_generatelightmaps_numoffsets[1] = min(MAX_LIGHTMAPSAMPLES, mod_generatelightmaps_vertexsamples.integer);
3965         mod_generatelightmaps_numoffsets[2] = min(MAX_LIGHTMAPSAMPLES, mod_generatelightmaps_gridsamples.integer);
3966         radius[0] = mod_generatelightmaps_lightmapradius.value;
3967         radius[1] = mod_generatelightmaps_vertexradius.value;
3968         radius[2] = mod_generatelightmaps_gridradius.value;
3969         for (i = 0;i < 3;i++)
3970         {
3971                 for (j = 1;j < mod_generatelightmaps_numoffsets[i];j++)
3972                 {
3973                         VectorRandom(temp);
3974                         VectorScale(temp, radius[i], mod_generatelightmaps_offsets[i][j]);
3975                 }
3976         }
3977 }
3978
3979 static void Mod_GenerateLightmaps_DestroyLightmaps(dp_model_t *model)
3980 {
3981         msurface_t *surface;
3982         int surfaceindex;
3983         int i;
3984         for (surfaceindex = 0;surfaceindex < model->num_surfaces;surfaceindex++)
3985         {
3986                 surface = model->data_surfaces + surfaceindex;
3987                 surface->lightmaptexture = NULL;
3988                 surface->deluxemaptexture = NULL;
3989         }
3990         if (model->brushq3.data_lightmaps)
3991         {
3992                 for (i = 0;i < model->brushq3.num_mergedlightmaps;i++)
3993                         if (model->brushq3.data_lightmaps[i])
3994                                 R_FreeTexture(model->brushq3.data_lightmaps[i]);
3995                 Mem_Free(model->brushq3.data_lightmaps);
3996                 model->brushq3.data_lightmaps = NULL;
3997         }
3998         if (model->brushq3.data_deluxemaps)
3999         {
4000                 for (i = 0;i < model->brushq3.num_mergedlightmaps;i++)
4001                         if (model->brushq3.data_deluxemaps[i])
4002                                 R_FreeTexture(model->brushq3.data_deluxemaps[i]);
4003                 Mem_Free(model->brushq3.data_deluxemaps);
4004                 model->brushq3.data_deluxemaps = NULL;
4005         }
4006 }
4007
4008 static void Mod_GenerateLightmaps_UnweldTriangles(dp_model_t *model)
4009 {
4010         msurface_t *surface;
4011         int surfaceindex;
4012         int vertexindex;
4013         int outvertexindex;
4014         int i;
4015         const int *e;
4016         surfmesh_t oldsurfmesh;
4017         size_t size;
4018         unsigned char *data;
4019         oldsurfmesh = model->surfmesh;
4020         model->surfmesh.num_triangles = oldsurfmesh.num_triangles;
4021         model->surfmesh.num_vertices = oldsurfmesh.num_triangles * 3;
4022         size = 0;
4023         size += model->surfmesh.num_vertices * sizeof(float[3]);
4024         size += model->surfmesh.num_vertices * sizeof(float[3]);
4025         size += model->surfmesh.num_vertices * sizeof(float[3]);
4026         size += model->surfmesh.num_vertices * sizeof(float[3]);
4027         size += model->surfmesh.num_vertices * sizeof(float[2]);
4028         size += model->surfmesh.num_vertices * sizeof(float[2]);
4029         size += model->surfmesh.num_vertices * sizeof(float[4]);
4030         data = (unsigned char *)Mem_Alloc(model->mempool, size);
4031         model->surfmesh.data_vertex3f = (float *)data;data += model->surfmesh.num_vertices * sizeof(float[3]);
4032         model->surfmesh.data_normal3f = (float *)data;data += model->surfmesh.num_vertices * sizeof(float[3]);
4033         model->surfmesh.data_svector3f = (float *)data;data += model->surfmesh.num_vertices * sizeof(float[3]);
4034         model->surfmesh.data_tvector3f = (float *)data;data += model->surfmesh.num_vertices * sizeof(float[3]);
4035         model->surfmesh.data_texcoordtexture2f = (float *)data;data += model->surfmesh.num_vertices * sizeof(float[2]);
4036         model->surfmesh.data_texcoordlightmap2f = (float *)data;data += model->surfmesh.num_vertices * sizeof(float[2]);
4037         model->surfmesh.data_lightmapcolor4f = (float *)data;data += model->surfmesh.num_vertices * sizeof(float[4]);
4038         if (model->surfmesh.num_vertices > 65536)
4039                 model->surfmesh.data_element3s = NULL;
4040
4041         if (model->surfmesh.data_element3i_indexbuffer)
4042                 R_Mesh_DestroyMeshBuffer(model->surfmesh.data_element3i_indexbuffer);
4043         model->surfmesh.data_element3i_indexbuffer = NULL;
4044         if (model->surfmesh.data_element3s_indexbuffer)
4045                 R_Mesh_DestroyMeshBuffer(model->surfmesh.data_element3s_indexbuffer);
4046         model->surfmesh.data_element3s_indexbuffer = NULL;
4047         if (model->surfmesh.vbo_vertexbuffer)
4048                 R_Mesh_DestroyMeshBuffer(model->surfmesh.vbo_vertexbuffer);
4049         model->surfmesh.vbo_vertexbuffer = 0;
4050
4051         // convert all triangles to unique vertex data
4052         outvertexindex = 0;
4053         for (surfaceindex = 0;surfaceindex < model->num_surfaces;surfaceindex++)
4054         {
4055                 surface = model->data_surfaces + surfaceindex;
4056                 surface->num_firstvertex = outvertexindex;
4057                 surface->num_vertices = surface->num_triangles*3;
4058                 e = oldsurfmesh.data_element3i + surface->num_firsttriangle*3;
4059                 for (i = 0;i < surface->num_triangles*3;i++)
4060                 {
4061                         vertexindex = e[i];
4062                         model->surfmesh.data_vertex3f[outvertexindex*3+0] = oldsurfmesh.data_vertex3f[vertexindex*3+0];
4063                         model->surfmesh.data_vertex3f[outvertexindex*3+1] = oldsurfmesh.data_vertex3f[vertexindex*3+1];
4064                         model->surfmesh.data_vertex3f[outvertexindex*3+2] = oldsurfmesh.data_vertex3f[vertexindex*3+2];
4065                         model->surfmesh.data_normal3f[outvertexindex*3+0] = oldsurfmesh.data_normal3f[vertexindex*3+0];
4066                         model->surfmesh.data_normal3f[outvertexindex*3+1] = oldsurfmesh.data_normal3f[vertexindex*3+1];
4067                         model->surfmesh.data_normal3f[outvertexindex*3+2] = oldsurfmesh.data_normal3f[vertexindex*3+2];
4068                         model->surfmesh.data_svector3f[outvertexindex*3+0] = oldsurfmesh.data_svector3f[vertexindex*3+0];
4069                         model->surfmesh.data_svector3f[outvertexindex*3+1] = oldsurfmesh.data_svector3f[vertexindex*3+1];
4070                         model->surfmesh.data_svector3f[outvertexindex*3+2] = oldsurfmesh.data_svector3f[vertexindex*3+2];
4071                         model->surfmesh.data_tvector3f[outvertexindex*3+0] = oldsurfmesh.data_tvector3f[vertexindex*3+0];
4072                         model->surfmesh.data_tvector3f[outvertexindex*3+1] = oldsurfmesh.data_tvector3f[vertexindex*3+1];
4073                         model->surfmesh.data_tvector3f[outvertexindex*3+2] = oldsurfmesh.data_tvector3f[vertexindex*3+2];
4074                         model->surfmesh.data_texcoordtexture2f[outvertexindex*2+0] = oldsurfmesh.data_texcoordtexture2f[vertexindex*2+0];
4075                         model->surfmesh.data_texcoordtexture2f[outvertexindex*2+1] = oldsurfmesh.data_texcoordtexture2f[vertexindex*2+1];
4076                         if (oldsurfmesh.data_texcoordlightmap2f)
4077                         {
4078                                 model->surfmesh.data_texcoordlightmap2f[outvertexindex*2+0] = oldsurfmesh.data_texcoordlightmap2f[vertexindex*2+0];
4079                                 model->surfmesh.data_texcoordlightmap2f[outvertexindex*2+1] = oldsurfmesh.data_texcoordlightmap2f[vertexindex*2+1];
4080                         }
4081                         if (oldsurfmesh.data_lightmapcolor4f)
4082                         {
4083                                 model->surfmesh.data_lightmapcolor4f[outvertexindex*4+0] = oldsurfmesh.data_lightmapcolor4f[vertexindex*4+0];
4084                                 model->surfmesh.data_lightmapcolor4f[outvertexindex*4+1] = oldsurfmesh.data_lightmapcolor4f[vertexindex*4+1];
4085                                 model->surfmesh.data_lightmapcolor4f[outvertexindex*4+2] = oldsurfmesh.data_lightmapcolor4f[vertexindex*4+2];
4086                                 model->surfmesh.data_lightmapcolor4f[outvertexindex*4+3] = oldsurfmesh.data_lightmapcolor4f[vertexindex*4+3];
4087                         }
4088                         else
4089                                 Vector4Set(model->surfmesh.data_lightmapcolor4f + 4*outvertexindex, 1, 1, 1, 1);
4090                         model->surfmesh.data_element3i[surface->num_firsttriangle*3+i] = outvertexindex;
4091                         outvertexindex++;
4092                 }
4093         }
4094         if (model->surfmesh.data_element3s)
4095                 for (i = 0;i < model->surfmesh.num_triangles*3;i++)
4096                         model->surfmesh.data_element3s[i] = model->surfmesh.data_element3i[i];
4097
4098         // find and update all submodels to use this new surfmesh data
4099         for (i = 0;i < model->brush.numsubmodels;i++)
4100                 model->brush.submodels[i]->surfmesh = model->surfmesh;
4101 }
4102
4103 static void Mod_GenerateLightmaps_CreateTriangleInformation(dp_model_t *model)
4104 {
4105         msurface_t *surface;
4106         int surfaceindex;
4107         int i;
4108         int axis;
4109         float normal[3];
4110         const int *e;
4111         lightmaptriangle_t *triangle;
4112         // generate lightmap triangle structs
4113         mod_generatelightmaps_lightmaptriangles = (lightmaptriangle_t *)Mem_Alloc(model->mempool, model->surfmesh.num_triangles * sizeof(lightmaptriangle_t));
4114         for (surfaceindex = 0;surfaceindex < model->num_surfaces;surfaceindex++)
4115         {
4116                 surface = model->data_surfaces + surfaceindex;
4117                 e = model->surfmesh.data_element3i + surface->num_firsttriangle*3;
4118                 for (i = 0;i < surface->num_triangles;i++)
4119                 {
4120                         triangle = &mod_generatelightmaps_lightmaptriangles[surface->num_firsttriangle+i];
4121                         triangle->triangleindex = surface->num_firsttriangle+i;
4122                         triangle->surfaceindex = surfaceindex;
4123                         VectorCopy(model->surfmesh.data_vertex3f + 3*e[i*3+0], triangle->vertex[0]);
4124                         VectorCopy(model->surfmesh.data_vertex3f + 3*e[i*3+1], triangle->vertex[1]);
4125                         VectorCopy(model->surfmesh.data_vertex3f + 3*e[i*3+2], triangle->vertex[2]);
4126                         // calculate bounds of triangle
4127                         triangle->mins[0] = min(triangle->vertex[0][0], min(triangle->vertex[1][0], triangle->vertex[2][0]));
4128                         triangle->mins[1] = min(triangle->vertex[0][1], min(triangle->vertex[1][1], triangle->vertex[2][1]));
4129                         triangle->mins[2] = min(triangle->vertex[0][2], min(triangle->vertex[1][2], triangle->vertex[2][2]));
4130                         triangle->maxs[0] = max(triangle->vertex[0][0], max(triangle->vertex[1][0], triangle->vertex[2][0]));
4131                         triangle->maxs[1] = max(triangle->vertex[0][1], max(triangle->vertex[1][1], triangle->vertex[2][1]));
4132                         triangle->maxs[2] = max(triangle->vertex[0][2], max(triangle->vertex[1][2], triangle->vertex[2][2]));
4133                         // pick an axial projection based on the triangle normal
4134                         TriangleNormal(triangle->vertex[0], triangle->vertex[1], triangle->vertex[2], normal);
4135                         axis = 0;
4136                         if (fabs(normal[1]) > fabs(normal[axis]))
4137                                 axis = 1;
4138                         if (fabs(normal[2]) > fabs(normal[axis]))
4139                                 axis = 2;
4140                         triangle->axis = axis;
4141                 }
4142         }
4143 }
4144
4145 static void Mod_GenerateLightmaps_DestroyTriangleInformation(dp_model_t *model)
4146 {
4147         if (mod_generatelightmaps_lightmaptriangles)
4148                 Mem_Free(mod_generatelightmaps_lightmaptriangles);
4149         mod_generatelightmaps_lightmaptriangles = NULL;
4150 }
4151
4152 float lmaxis[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
4153
4154 static void Mod_GenerateLightmaps_CreateLightmaps(dp_model_t *model)
4155 {
4156         msurface_t *surface;
4157         int surfaceindex;
4158         int lightmapindex;
4159         int lightmapnumber;
4160         int i;
4161         int j;
4162         int k;
4163         int x;
4164         int y;
4165         int axis;
4166         int axis1;
4167         int axis2;
4168         int retry;
4169         int pixeloffset;
4170         float trianglenormal[3];
4171         float samplecenter[3];
4172         float samplenormal[3];
4173         float temp[3];
4174         float lmiscale[2];
4175         float slopex;
4176         float slopey;
4177         float slopebase;
4178         float lmscalepixels;
4179         float lmmins;
4180         float lmmaxs;
4181         float lm_basescalepixels;
4182         int lm_borderpixels;
4183         int lm_texturesize;
4184         //int lm_maxpixels;
4185         const int *e;
4186         lightmaptriangle_t *triangle;
4187         unsigned char *lightmappixels;
4188         unsigned char *deluxemappixels;
4189         mod_alloclightmap_state_t lmstate;
4190         char vabuf[1024];
4191
4192         // generate lightmap projection information for all triangles
4193         if (model->texturepool == NULL)
4194                 model->texturepool = R_AllocTexturePool();
4195         lm_basescalepixels = 1.0f / max(0.0001f, mod_generatelightmaps_unitspersample.value);
4196         lm_borderpixels = mod_generatelightmaps_borderpixels.integer;
4197         lm_texturesize = bound(lm_borderpixels*2+1, 64, (int)vid.maxtexturesize_2d);
4198         //lm_maxpixels = lm_texturesize-(lm_borderpixels*2+1);
4199         Mod_AllocLightmap_Init(&lmstate, loadmodel->mempool, lm_texturesize, lm_texturesize);
4200         lightmapnumber = 0;
4201         for (surfaceindex = 0;surfaceindex < model->num_surfaces;surfaceindex++)
4202         {
4203                 surface = model->data_surfaces + surfaceindex;
4204                 e = model->surfmesh.data_element3i + surface->num_firsttriangle*3;
4205                 lmscalepixels = lm_basescalepixels;
4206                 for (retry = 0;retry < 30;retry++)
4207                 {
4208                         // after a couple failed attempts, degrade quality to make it fit
4209                         if (retry > 1)
4210                                 lmscalepixels *= 0.5f;
4211                         for (i = 0;i < surface->num_triangles;i++)
4212                         {
4213                                 triangle = &mod_generatelightmaps_lightmaptriangles[surface->num_firsttriangle+i];
4214                                 triangle->lightmapindex = lightmapnumber;
4215                                 // calculate lightmap bounds in 3D pixel coordinates, limit size,
4216                                 // pick two planar axes for projection
4217                                 // lightmap coordinates here are in pixels
4218                                 // lightmap projections are snapped to pixel grid explicitly, such
4219                                 // that two neighboring triangles sharing an edge and projection
4220                                 // axis will have identical sample spacing along their shared edge
4221                                 k = 0;
4222                                 for (j = 0;j < 3;j++)
4223                                 {
4224                                         if (j == triangle->axis)
4225                                                 continue;
4226                                         lmmins = floor(triangle->mins[j]*lmscalepixels)-lm_borderpixels;
4227                                         lmmaxs = floor(triangle->maxs[j]*lmscalepixels)+lm_borderpixels;
4228                                         triangle->lmsize[k] = (int)(lmmaxs-lmmins);
4229                                         triangle->lmbase[k] = lmmins/lmscalepixels;
4230                                         triangle->lmscale[k] = lmscalepixels;
4231                                         k++;
4232                                 }
4233                                 if (!Mod_AllocLightmap_Block(&lmstate, triangle->lmsize[0], triangle->lmsize[1], &triangle->lmoffset[0], &triangle->lmoffset[1]))
4234                                         break;
4235                         }
4236                         // if all fit in this texture, we're done with this surface
4237                         if (i == surface->num_triangles)
4238                                 break;
4239                         // if we haven't maxed out the lightmap size yet, we retry the
4240                         // entire surface batch...
4241                         if (lm_texturesize * 2 <= min(mod_generatelightmaps_texturesize.integer, (int)vid.maxtexturesize_2d))
4242                         {
4243                                 lm_texturesize *= 2;
4244                                 surfaceindex = -1;
4245                                 lightmapnumber = 0;
4246                                 Mod_AllocLightmap_Free(&lmstate);
4247                                 Mod_AllocLightmap_Init(&lmstate, loadmodel->mempool, lm_texturesize, lm_texturesize);
4248                                 break;
4249                         }
4250                         // if we have maxed out the lightmap size, and this triangle does
4251                         // not fit in the same texture as the rest of the surface, we have
4252                         // to retry the entire surface in a new texture (can only use one)
4253                         // with multiple retries, the lightmap quality degrades until it
4254                         // fits (or gives up)
4255                         if (surfaceindex > 0)
4256                                 lightmapnumber++;
4257                         Mod_AllocLightmap_Reset(&lmstate);
4258                 }
4259         }
4260         lightmapnumber++;
4261         Mod_AllocLightmap_Free(&lmstate);
4262
4263         // now put triangles together into lightmap textures, and do not allow
4264         // triangles of a surface to go into different textures (as that would
4265         // require rewriting the surface list)
4266         model->brushq3.deluxemapping_modelspace = true;
4267         model->brushq3.deluxemapping = true;
4268         model->brushq3.num_mergedlightmaps = lightmapnumber;
4269         model->brushq3.data_lightmaps = (rtexture_t **)Mem_Alloc(model->mempool, model->brushq3.num_mergedlightmaps * sizeof(rtexture_t *));
4270         model->brushq3.data_deluxemaps = (rtexture_t **)Mem_Alloc(model->mempool, model->brushq3.num_mergedlightmaps * sizeof(rtexture_t *));
4271         lightmappixels = (unsigned char *)Mem_Alloc(tempmempool, model->brushq3.num_mergedlightmaps * lm_texturesize * lm_texturesize * 4);
4272         deluxemappixels = (unsigned char *)Mem_Alloc(tempmempool, model->brushq3.num_mergedlightmaps * lm_texturesize * lm_texturesize * 4);
4273         for (surfaceindex = 0;surfaceindex < model->num_surfaces;surfaceindex++)
4274         {
4275                 surface = model->data_surfaces + surfaceindex;
4276                 e = model->surfmesh.data_element3i + surface->num_firsttriangle*3;
4277                 for (i = 0;i < surface->num_triangles;i++)
4278                 {
4279                         triangle = &mod_generatelightmaps_lightmaptriangles[surface->num_firsttriangle+i];
4280                         TriangleNormal(triangle->vertex[0], triangle->vertex[1], triangle->vertex[2], trianglenormal);
4281                         VectorNormalize(trianglenormal);
4282                         VectorCopy(trianglenormal, samplenormal); // FIXME: this is supposed to be interpolated per pixel from vertices
4283                         axis = triangle->axis;
4284                         axis1 = axis == 0 ? 1 : 0;
4285                         axis2 = axis == 2 ? 1 : 2;
4286                         lmiscale[0] = 1.0f / triangle->lmscale[0];
4287                         lmiscale[1] = 1.0f / triangle->lmscale[1];
4288                         if (trianglenormal[axis] < 0)
4289                                 VectorNegate(trianglenormal, trianglenormal);
4290                         CrossProduct(lmaxis[axis2], trianglenormal, temp);slopex = temp[axis] / temp[axis1];
4291                         CrossProduct(lmaxis[axis1], trianglenormal, temp);slopey = temp[axis] / temp[axis2];
4292                         slopebase = triangle->vertex[0][axis] - triangle->vertex[0][axis1]*slopex - triangle->vertex[0][axis2]*slopey;
4293                         for (j = 0;j < 3;j++)
4294                         {
4295                                 float *t2f = model->surfmesh.data_texcoordlightmap2f + e[i*3+j]*2;
4296                                 t2f[0] = ((triangle->vertex[j][axis1] - triangle->lmbase[0]) * triangle->lmscale[0] + triangle->lmoffset[0]) / lm_texturesize;
4297                                 t2f[1] = ((triangle->vertex[j][axis2] - triangle->lmbase[1]) * triangle->lmscale[1] + triangle->lmoffset[1]) / lm_texturesize;
4298 #if 0
4299                                 samplecenter[axis1] = (t2f[0]*lm_texturesize-triangle->lmoffset[0])*lmiscale[0] + triangle->lmbase[0];
4300                                 samplecenter[axis2] = (t2f[1]*lm_texturesize-triangle->lmoffset[1])*lmiscale[1] + triangle->lmbase[1];
4301                                 samplecenter[axis] = samplecenter[axis1]*slopex + samplecenter[axis2]*slopey + slopebase;
4302                                 Con_Printf("%f:%f %f:%f %f:%f = %f %f\n", triangle->vertex[j][axis1], samplecenter[axis1], triangle->vertex[j][axis2], samplecenter[axis2], triangle->vertex[j][axis], samplecenter[axis], t2f[0], t2f[1]);
4303 #endif
4304                         }
4305
4306 #if 0
4307                         switch (axis)
4308                         {
4309                         default:
4310                         case 0:
4311                                 forward[0] = 0;
4312                                 forward[1] = 1.0f / triangle->lmscale[0];
4313                                 forward[2] = 0;
4314                                 left[0] = 0;
4315                                 left[1] = 0;
4316                                 left[2] = 1.0f / triangle->lmscale[1];
4317                                 up[0] = 1.0f;
4318                                 up[1] = 0;
4319                                 up[2] = 0;
4320                                 origin[0] = 0;
4321                                 origin[1] = triangle->lmbase[0];
4322                                 origin[2] = triangle->lmbase[1];
4323                                 break;
4324                         case 1:
4325                                 forward[0] = 1.0f / triangle->lmscale[0];
4326                                 forward[1] = 0;
4327                                 forward[2] = 0;
4328                                 left[0] = 0;
4329                                 left[1] = 0;
4330                                 left[2] = 1.0f / triangle->lmscale[1];
4331                                 up[0] = 0;
4332                                 up[1] = 1.0f;
4333                                 up[2] = 0;
4334                                 origin[0] = triangle->lmbase[0];
4335                                 origin[1] = 0;
4336                                 origin[2] = triangle->lmbase[1];
4337                                 break;
4338                         case 2:
4339                                 forward[0] = 1.0f / triangle->lmscale[0];
4340                                 forward[1] = 0;
4341                                 forward[2] = 0;
4342                                 left[0] = 0;
4343                                 left[1] = 1.0f / triangle->lmscale[1];
4344                                 left[2] = 0;
4345                                 up[0] = 0;
4346                                 up[1] = 0;
4347                                 up[2] = 1.0f;
4348                                 origin[0] = triangle->lmbase[0];
4349                                 origin[1] = triangle->lmbase[1];
4350                                 origin[2] = 0;
4351                                 break;
4352                         }
4353                         Matrix4x4_FromVectors(&backmatrix, forward, left, up, origin);
4354 #endif
4355 #define LM_DIST_EPSILON (1.0f / 32.0f)
4356                         for (y = 0;y < triangle->lmsize[1];y++)
4357                         {
4358                                 pixeloffset = ((triangle->lightmapindex * lm_texturesize + y + triangle->lmoffset[1]) * lm_texturesize + triangle->lmoffset[0]) * 4;
4359                                 for (x = 0;x < triangle->lmsize[0];x++, pixeloffset += 4)
4360                                 {
4361                                         samplecenter[axis1] = (x+0.5f)*lmiscale[0] + triangle->lmbase[0];
4362                                         samplecenter[axis2] = (y+0.5f)*lmiscale[1] + triangle->lmbase[1];
4363                                         samplecenter[axis] = samplecenter[axis1]*slopex + samplecenter[axis2]*slopey + slopebase;
4364                                         VectorMA(samplecenter, 0.125f, samplenormal, samplecenter);
4365                                         Mod_GenerateLightmaps_LightmapSample(samplecenter, samplenormal, lightmappixels + pixeloffset, deluxemappixels + pixeloffset);
4366                                 }
4367                         }
4368                 }
4369         }
4370
4371         for (lightmapindex = 0;lightmapindex < model->brushq3.num_mergedlightmaps;lightmapindex++)
4372         {
4373                 model->brushq3.data_lightmaps[lightmapindex] = R_LoadTexture2D(model->texturepool, va(vabuf, sizeof(vabuf), "lightmap%i", lightmapindex), lm_texturesize, lm_texturesize, lightmappixels + lightmapindex * lm_texturesize * lm_texturesize * 4, TEXTYPE_BGRA, TEXF_FORCELINEAR, -1, NULL);
4374                 model->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(model->texturepool, va(vabuf, sizeof(vabuf), "deluxemap%i", lightmapindex), lm_texturesize, lm_texturesize, deluxemappixels + lightmapindex * lm_texturesize * lm_texturesize * 4, TEXTYPE_BGRA, TEXF_FORCELINEAR, -1, NULL);
4375         }
4376
4377         if (lightmappixels)
4378                 Mem_Free(lightmappixels);
4379         if (deluxemappixels)
4380                 Mem_Free(deluxemappixels);
4381
4382         for (surfaceindex = 0;surfaceindex < model->num_surfaces;surfaceindex++)
4383         {
4384                 surface = model->data_surfaces + surfaceindex;
4385                 if (!surface->num_triangles)
4386                         continue;
4387                 lightmapindex = mod_generatelightmaps_lightmaptriangles[surface->num_firsttriangle].lightmapindex;
4388                 surface->lightmaptexture = model->brushq3.data_lightmaps[lightmapindex];
4389                 surface->deluxemaptexture = model->brushq3.data_deluxemaps[lightmapindex];
4390                 surface->lightmapinfo = NULL;
4391         }
4392
4393         model->brush.LightPoint = Mod_GenerateLightmaps_LightPoint;
4394         model->brushq1.lightdata = NULL;
4395         model->brushq1.lightmapupdateflags = NULL;
4396         model->brushq1.firstrender = false;
4397         model->brushq1.num_lightstyles = 0;
4398         model->brushq1.data_lightstyleinfo = NULL;
4399         for (i = 0;i < model->brush.numsubmodels;i++)
4400         {
4401                 model->brush.submodels[i]->brushq1.lightmapupdateflags = NULL;
4402                 model->brush.submodels[i]->brushq1.firstrender = false;
4403                 model->brush.submodels[i]->brushq1.num_lightstyles = 0;
4404                 model->brush.submodels[i]->brushq1.data_lightstyleinfo = NULL;
4405         }
4406 }
4407
4408 static void Mod_GenerateLightmaps_UpdateVertexColors(dp_model_t *model)
4409 {
4410         int i;
4411         for (i = 0;i < model->surfmesh.num_vertices;i++)
4412                 Mod_GenerateLightmaps_VertexSample(model->surfmesh.data_vertex3f + 3*i, model->surfmesh.data_normal3f + 3*i, model->surfmesh.data_lightmapcolor4f + 4*i);
4413 }
4414
4415 static void Mod_GenerateLightmaps_UpdateLightGrid(dp_model_t *model)
4416 {
4417         int x;
4418         int y;
4419         int z;
4420         int index = 0;
4421         float pos[3];
4422         for (z = 0;z < model->brushq3.num_lightgrid_isize[2];z++)
4423         {
4424                 pos[2] = (model->brushq3.num_lightgrid_imins[2] + z + 0.5f) * model->brushq3.num_lightgrid_cellsize[2];
4425                 for (y = 0;y < model->brushq3.num_lightgrid_isize[1];y++)
4426                 {
4427                         pos[1] = (model->brushq3.num_lightgrid_imins[1] + y + 0.5f) * model->brushq3.num_lightgrid_cellsize[1];
4428                         for (x = 0;x < model->brushq3.num_lightgrid_isize[0];x++, index++)
4429                         {
4430                                 pos[0] = (model->brushq3.num_lightgrid_imins[0] + x + 0.5f) * model->brushq3.num_lightgrid_cellsize[0];
4431                                 Mod_GenerateLightmaps_GridSample(pos, model->brushq3.data_lightgrid + index);
4432                         }
4433                 }
4434         }
4435 }
4436
4437 extern cvar_t mod_q3bsp_nolightmaps;
4438 static void Mod_GenerateLightmaps(dp_model_t *model)
4439 {
4440         //lightmaptriangle_t *lightmaptriangles = Mem_Alloc(model->mempool, model->surfmesh.num_triangles * sizeof(lightmaptriangle_t));
4441         dp_model_t *oldloadmodel = loadmodel;
4442         loadmodel = model;
4443
4444         Mod_GenerateLightmaps_InitSampleOffsets(model);
4445         Mod_GenerateLightmaps_DestroyLightmaps(model);
4446         Mod_GenerateLightmaps_UnweldTriangles(model);
4447         Mod_GenerateLightmaps_CreateTriangleInformation(model);
4448         Mod_GenerateLightmaps_CreateLights(model);
4449         if(!mod_q3bsp_nolightmaps.integer)
4450                 Mod_GenerateLightmaps_CreateLightmaps(model);
4451         Mod_GenerateLightmaps_UpdateVertexColors(model);
4452         Mod_GenerateLightmaps_UpdateLightGrid(model);
4453         Mod_GenerateLightmaps_DestroyLights(model);
4454         Mod_GenerateLightmaps_DestroyTriangleInformation(model);
4455
4456         loadmodel = oldloadmodel;
4457 }
4458
4459 static void Mod_GenerateLightmaps_f(void)
4460 {
4461         if (Cmd_Argc() != 1)
4462         {
4463                 Con_Printf("usage: mod_generatelightmaps\n");
4464                 return;
4465         }
4466         if (!cl.worldmodel)
4467         {
4468                 Con_Printf("no worldmodel loaded\n");
4469                 return;
4470         }
4471         Mod_GenerateLightmaps(cl.worldmodel);
4472 }
4473
4474 void Mod_Mesh_Create(dp_model_t *mod, const char *name)
4475 {
4476         memset(mod, 0, sizeof(*mod));
4477         strlcpy(mod->name, name, sizeof(mod->name));
4478         mod->mempool = Mem_AllocPool(name, 0, NULL);
4479         mod->texturepool = R_AllocTexturePool();
4480         mod->Draw = R_Q1BSP_Draw;
4481         mod->DrawDepth = R_Q1BSP_DrawDepth;
4482         mod->DrawDebug = R_Q1BSP_DrawDebug;
4483         mod->DrawPrepass = R_Q1BSP_DrawPrepass;
4484         mod->GetLightInfo = R_Q1BSP_GetLightInfo;
4485         mod->DrawShadowMap = R_Q1BSP_DrawShadowMap;
4486         mod->DrawLight = R_Q1BSP_DrawLight;
4487 }
4488
4489 void Mod_Mesh_Destroy(dp_model_t *mod)
4490 {
4491         Mod_UnloadModel(mod);
4492 }
4493
4494 // resets the mesh model to have no geometry to render, ready for a new frame -
4495 // the mesh will be prepared for rendering later using Mod_Mesh_Finalize
4496 void Mod_Mesh_Reset(dp_model_t *mod)
4497 {
4498         mod->num_surfaces = 0;
4499         mod->surfmesh.num_vertices = 0;
4500         mod->surfmesh.num_triangles = 0;
4501         memset(mod->surfmesh.data_vertexhash, -1, mod->surfmesh.num_vertexhashsize * sizeof(*mod->surfmesh.data_vertexhash));
4502         mod->DrawSky = NULL; // will be set if a texture needs it
4503         mod->DrawAddWaterPlanes = NULL; // will be set if a texture needs it
4504 }
4505
4506 texture_t *Mod_Mesh_GetTexture(dp_model_t *mod, const char *name, int defaultdrawflags, int defaulttexflags, int defaultmaterialflags)
4507 {
4508         int i;
4509         texture_t *t;
4510         for (i = 0; i < mod->num_textures; i++)
4511                 if (!strcmp(mod->data_textures[i].name, name))
4512                         return mod->data_textures + i;
4513         if (mod->max_textures <= mod->num_textures)
4514         {
4515                 texture_t *oldtextures = mod->data_textures;
4516                 mod->max_textures = max(mod->max_textures * 2, 1024);
4517                 mod->data_textures = (texture_t *)Mem_Realloc(mod->mempool, mod->data_textures, mod->max_textures * sizeof(*mod->data_textures));
4518                 // update the pointers
4519                 for (i = 0; i < mod->num_surfaces; i++)
4520                         mod->data_surfaces[i].texture = mod->data_textures + (mod->data_surfaces[i].texture - oldtextures);
4521         }
4522         t = &mod->data_textures[mod->num_textures++];
4523         Mod_LoadTextureFromQ3Shader(mod->mempool, mod->name, t, name, false, true, defaulttexflags, defaultmaterialflags);
4524         switch (defaultdrawflags & DRAWFLAG_MASK)
4525         {
4526         case DRAWFLAG_ADDITIVE:
4527                 t->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED;
4528                 t->currentmaterialflags = t->basematerialflags;
4529                 break;
4530         case DRAWFLAG_MODULATE:
4531                 t->basematerialflags |= MATERIALFLAG_CUSTOMBLEND | MATERIALFLAG_BLENDED;
4532                 t->currentmaterialflags = t->basematerialflags;
4533                 t->customblendfunc[0] = GL_DST_COLOR;
4534                 t->customblendfunc[1] = GL_ZERO;
4535                 break;
4536         case DRAWFLAG_2XMODULATE:
4537                 t->basematerialflags |= MATERIALFLAG_CUSTOMBLEND | MATERIALFLAG_BLENDED;
4538                 t->currentmaterialflags = t->basematerialflags;
4539                 t->customblendfunc[0] = GL_DST_COLOR;
4540                 t->customblendfunc[1] = GL_SRC_COLOR;
4541                 break;
4542         case DRAWFLAG_SCREEN:
4543                 t->basematerialflags |= MATERIALFLAG_CUSTOMBLEND | MATERIALFLAG_BLENDED;
4544                 t->currentmaterialflags = t->basematerialflags;
4545                 t->customblendfunc[0] = GL_ONE_MINUS_DST_COLOR;
4546                 t->customblendfunc[1] = GL_ONE;
4547                 break;
4548         default:
4549                 break;
4550         }
4551         return t;
4552 }
4553
4554 msurface_t *Mod_Mesh_AddSurface(dp_model_t *mod, texture_t *tex, qboolean batchwithprevioussurface)
4555 {
4556         msurface_t *surf;
4557         // batch if possible; primarily useful for UI rendering where bounding boxes don't matter
4558         if (batchwithprevioussurface && mod->num_surfaces > 0 && mod->data_surfaces[mod->num_surfaces - 1].texture == tex)
4559                 return mod->data_surfaces + mod->num_surfaces - 1;
4560         // create new surface
4561         if (mod->max_surfaces == mod->num_surfaces)
4562         {
4563                 mod->max_surfaces = 2 * max(mod->num_surfaces, 64);
4564                 mod->data_surfaces = (msurface_t *)Mem_Realloc(mod->mempool, mod->data_surfaces, mod->max_surfaces * sizeof(*mod->data_surfaces));
4565                 mod->sortedmodelsurfaces = (int *)Mem_Realloc(mod->mempool, mod->sortedmodelsurfaces, mod->max_surfaces * sizeof(*mod->sortedmodelsurfaces));
4566         }
4567         surf = mod->data_surfaces + mod->num_surfaces;
4568         mod->num_surfaces++;
4569         memset(surf, 0, sizeof(*surf));
4570         surf->texture = tex;
4571         surf->num_firsttriangle = mod->surfmesh.num_triangles;
4572         surf->num_firstvertex = mod->surfmesh.num_vertices;
4573         if (tex->basematerialflags & (MATERIALFLAG_SKY))
4574                 mod->DrawSky = R_Q1BSP_DrawSky;
4575         if (tex->basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
4576                 mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
4577         return surf;
4578 }
4579
4580 int Mod_Mesh_IndexForVertex(dp_model_t *mod, msurface_t *surf, float x, float y, float z, float nx, float ny, float nz, float s, float t, float u, float v, float r, float g, float b, float a)
4581 {
4582         int hashindex, h, vnum, mask;
4583         surfmesh_t *mesh = &mod->surfmesh;
4584         if (mesh->max_vertices == mesh->num_vertices)
4585         {
4586                 mesh->max_vertices = max(mesh->num_vertices * 2, 256);
4587                 mesh->data_vertex3f = (float *)Mem_Realloc(mod->mempool, mesh->data_vertex3f, mesh->max_vertices * sizeof(float[3]));
4588                 mesh->data_svector3f = (float *)Mem_Realloc(mod->mempool, mesh->data_svector3f, mesh->max_vertices * sizeof(float[3]));
4589                 mesh->data_tvector3f = (float *)Mem_Realloc(mod->mempool, mesh->data_tvector3f, mesh->max_vertices * sizeof(float[3]));
4590                 mesh->data_normal3f = (float *)Mem_Realloc(mod->mempool, mesh->data_normal3f, mesh->max_vertices * sizeof(float[3]));
4591                 mesh->data_texcoordtexture2f = (float *)Mem_Realloc(mod->mempool, mesh->data_texcoordtexture2f, mesh->max_vertices * sizeof(float[2]));
4592                 mesh->data_texcoordlightmap2f = (float *)Mem_Realloc(mod->mempool, mesh->data_texcoordlightmap2f, mesh->max_vertices * sizeof(float[2]));
4593                 mesh->data_lightmapcolor4f = (float *)Mem_Realloc(mod->mempool, mesh->data_lightmapcolor4f, mesh->max_vertices * sizeof(float[4]));
4594                 // rebuild the hash table
4595                 mesh->num_vertexhashsize = 4 * mesh->max_vertices;
4596                 mesh->num_vertexhashsize &= ~(mesh->num_vertexhashsize - 1); // round down to pow2
4597                 mesh->data_vertexhash = (int *)Mem_Realloc(mod->mempool, mesh->data_vertexhash, mesh->num_vertexhashsize * sizeof(*mesh->data_vertexhash));
4598                 memset(mesh->data_vertexhash, -1, mesh->num_vertexhashsize * sizeof(*mesh->data_vertexhash));
4599                 mask = mod->surfmesh.num_vertexhashsize - 1;
4600                 // no need to hash the vertices for the entire model, the latest surface will suffice.
4601                 for (vnum = surf ? surf->num_firstvertex : 0; vnum < mesh->num_vertices; vnum++)
4602                 {
4603                         // this uses prime numbers intentionally for computing the hash
4604                         hashindex = (unsigned int)(mesh->data_vertex3f[vnum * 3 + 0] * 2003 + mesh->data_vertex3f[vnum * 3 + 1] * 4001 + mesh->data_vertex3f[vnum * 3 + 2] * 7919 + mesh->data_normal3f[vnum * 3 + 0] * 4097 + mesh->data_normal3f[vnum * 3 + 1] * 257 + mesh->data_normal3f[vnum * 3 + 2] * 17) & mask;
4605                         for (h = hashindex; mesh->data_vertexhash[h] >= 0; h = (h + 1) & mask)
4606                                 ; // just iterate until we find the terminator
4607                         mesh->data_vertexhash[h] = vnum;
4608                 }
4609         }
4610         mask = mod->surfmesh.num_vertexhashsize - 1;
4611         // this uses prime numbers intentionally for computing the hash
4612         hashindex = (unsigned int)(x * 2003 + y * 4001 + z * 7919 + nx * 4097 + ny * 257 + nz * 17) & mask;
4613         // when possible find an identical vertex within the same surface and return it
4614         for(h = hashindex;(vnum = mesh->data_vertexhash[h]) >= 0;h = (h + 1) & mask)
4615         {
4616                 if (vnum >= surf->num_firstvertex
4617                  && mesh->data_vertex3f[vnum * 3 + 0] == x && mesh->data_vertex3f[vnum * 3 + 1] == y && mesh->data_vertex3f[vnum * 3 + 2] == z
4618                  && mesh->data_normal3f[vnum * 3 + 0] == nx && mesh->data_normal3f[vnum * 3 + 1] == ny && mesh->data_normal3f[vnum * 3 + 2] == nz
4619                  && mesh->data_texcoordtexture2f[vnum * 2 + 0] == s && mesh->data_texcoordtexture2f[vnum * 2 + 1] == t
4620                  && mesh->data_texcoordlightmap2f[vnum * 2 + 0] == u && mesh->data_texcoordlightmap2f[vnum * 2 + 1] == v
4621                  && mesh->data_lightmapcolor4f[vnum * 4 + 0] == r && mesh->data_lightmapcolor4f[vnum * 4 + 1] == g && mesh->data_lightmapcolor4f[vnum * 4 + 2] == b && mesh->data_lightmapcolor4f[vnum * 4 + 3] == a)
4622                         return vnum;
4623         }
4624         // add the new vertex
4625         vnum = mesh->num_vertices++;
4626         if (surf->num_vertices > 0)
4627         {
4628                 if (surf->mins[0] > x) surf->mins[0] = x;
4629                 if (surf->mins[1] > y) surf->mins[1] = y;
4630                 if (surf->mins[2] > z) surf->mins[2] = z;
4631                 if (surf->maxs[0] < x) surf->maxs[0] = x;
4632                 if (surf->maxs[1] < y) surf->maxs[1] = y;
4633                 if (surf->maxs[2] < z) surf->maxs[2] = z;
4634         }
4635         else
4636         {
4637                 VectorSet(surf->mins, x, y, z);
4638                 VectorSet(surf->maxs, x, y, z);
4639         }
4640         surf->num_vertices = mesh->num_vertices - surf->num_firstvertex;
4641         mesh->data_vertexhash[h] = vnum;
4642         mesh->data_vertex3f[vnum * 3 + 0] = x;
4643         mesh->data_vertex3f[vnum * 3 + 1] = y;
4644         mesh->data_vertex3f[vnum * 3 + 2] = z;
4645         mesh->data_normal3f[vnum * 3 + 0] = nx;
4646         mesh->data_normal3f[vnum * 3 + 1] = ny;
4647         mesh->data_normal3f[vnum * 3 + 2] = nz;
4648         mesh->data_texcoordtexture2f[vnum * 2 + 0] = s;
4649         mesh->data_texcoordtexture2f[vnum * 2 + 1] = t;
4650         mesh->data_texcoordlightmap2f[vnum * 2 + 0] = u;
4651         mesh->data_texcoordlightmap2f[vnum * 2 + 1] = v;
4652         mesh->data_lightmapcolor4f[vnum * 4 + 0] = r;
4653         mesh->data_lightmapcolor4f[vnum * 4 + 1] = g;
4654         mesh->data_lightmapcolor4f[vnum * 4 + 2] = b;
4655         mesh->data_lightmapcolor4f[vnum * 4 + 3] = a;
4656         return vnum;
4657 }
4658
4659 void Mod_Mesh_AddTriangle(dp_model_t *mod, msurface_t *surf, int e0, int e1, int e2)
4660 {
4661         surfmesh_t *mesh = &mod->surfmesh;
4662         if (mesh->max_triangles == mesh->num_triangles)
4663         {
4664                 mesh->max_triangles = 2 * max(mesh->num_triangles, 128);
4665                 mesh->data_element3s = (unsigned short *)Mem_Realloc(mod->mempool, mesh->data_element3s, mesh->max_triangles * sizeof(unsigned short[3]));
4666                 mesh->data_element3i = (int *)Mem_Realloc(mod->mempool, mesh->data_element3i, mesh->max_triangles * sizeof(int[3]));
4667         }
4668         mesh->data_element3s[mesh->num_triangles * 3 + 0] = e0;
4669         mesh->data_element3s[mesh->num_triangles * 3 + 1] = e1;
4670         mesh->data_element3s[mesh->num_triangles * 3 + 2] = e2;
4671         mesh->data_element3i[mesh->num_triangles * 3 + 0] = e0;
4672         mesh->data_element3i[mesh->num_triangles * 3 + 1] = e1;
4673         mesh->data_element3i[mesh->num_triangles * 3 + 2] = e2;
4674         mesh->num_triangles++;
4675         surf->num_triangles++;
4676 }
4677
4678 static void Mod_Mesh_MakeSortedSurfaces(dp_model_t *mod)
4679 {
4680         int i, j;
4681         texture_t *tex;
4682         msurface_t *surf, *surf2;
4683
4684         // build the sorted surfaces list properly to reduce material setup
4685         // this is easy because we're just sorting on texture and don't care about the order of textures
4686         mod->nummodelsurfaces = 0;
4687         for (i = 0; i < mod->num_surfaces; i++)
4688                 mod->data_surfaces[i].included = false;
4689         for (i = 0; i < mod->num_surfaces; i++)
4690         {
4691                 surf = mod->data_surfaces + i;
4692                 if (surf->included)
4693                         continue;
4694                 tex = surf->texture;
4695                 // j = i is intentional
4696                 for (j = i; j < mod->num_surfaces; j++)
4697                 {
4698                         surf2 = mod->data_surfaces + j;
4699                         if (surf2->included)
4700                                 continue;
4701                         if (surf2->texture == tex)
4702                         {
4703                                 surf2->included = true;
4704                                 mod->sortedmodelsurfaces[mod->nummodelsurfaces++] = j;
4705                         }
4706                 }
4707         }
4708 }
4709
4710 void Mod_Mesh_ComputeBounds(dp_model_t *mod)
4711 {
4712         int i;
4713         vec_t x2a, x2b, y2a, y2b, z2a, z2b, x2, y2, z2, yawradius, rotatedradius;
4714
4715         if (mod->surfmesh.num_vertices > 0)
4716         {
4717                 // calculate normalmins/normalmaxs
4718                 VectorCopy(mod->surfmesh.data_vertex3f, mod->normalmins);
4719                 VectorCopy(mod->surfmesh.data_vertex3f, mod->normalmaxs);
4720                 for (i = 1; i < mod->surfmesh.num_vertices; i++)
4721                 {
4722                         float x = mod->surfmesh.data_vertex3f[i * 3 + 0];
4723                         float y = mod->surfmesh.data_vertex3f[i * 3 + 1];
4724                         float z = mod->surfmesh.data_vertex3f[i * 3 + 2];
4725                         // expand bounds to include this vertex
4726                         if (mod->normalmins[0] > x) mod->normalmins[0] = x;
4727                         if (mod->normalmins[1] > y) mod->normalmins[1] = y;
4728                         if (mod->normalmins[2] > z) mod->normalmins[2] = z;
4729                         if (mod->normalmaxs[0] < x) mod->normalmaxs[0] = x;
4730                         if (mod->normalmaxs[1] < y) mod->normalmaxs[1] = y;
4731                         if (mod->normalmaxs[2] < z) mod->normalmaxs[2] = z;
4732                 }
4733                 // calculate yawmins/yawmaxs, rotatedmins/maxs from normalmins/maxs
4734                 // (fast but less accurate than doing it per vertex)
4735                 x2a = mod->normalmins[0] * mod->normalmins[0];
4736                 x2b = mod->normalmaxs[0] * mod->normalmaxs[0];
4737                 y2a = mod->normalmins[1] * mod->normalmins[1];
4738                 y2b = mod->normalmaxs[1] * mod->normalmaxs[1];
4739                 z2a = mod->normalmins[2] * mod->normalmins[2];
4740                 z2b = mod->normalmaxs[2] * mod->normalmaxs[2];
4741                 x2 = max(x2a, x2b);
4742                 y2 = max(y2a, y2b);
4743                 z2 = max(z2a, z2b);
4744                 yawradius = sqrt(x2 + y2);
4745                 rotatedradius = sqrt(x2 + y2 + z2);
4746                 VectorSet(mod->yawmins, -yawradius, -yawradius, mod->normalmins[2]);
4747                 VectorSet(mod->yawmaxs, yawradius, yawradius, mod->normalmaxs[2]);
4748                 VectorSet(mod->rotatedmins, -rotatedradius, -rotatedradius, -rotatedradius);
4749                 VectorSet(mod->rotatedmaxs, rotatedradius, rotatedradius, rotatedradius);
4750                 mod->radius = rotatedradius;
4751                 mod->radius2 = x2 + y2 + z2;
4752         }
4753         else
4754         {
4755                 VectorClear(mod->normalmins);
4756                 VectorClear(mod->normalmaxs);
4757                 VectorClear(mod->yawmins);
4758                 VectorClear(mod->yawmaxs);
4759                 VectorClear(mod->rotatedmins);
4760                 VectorClear(mod->rotatedmaxs);
4761                 mod->radius = 0;
4762                 mod->radius2 = 0;
4763         }
4764 }
4765
4766 void Mod_Mesh_Finalize(dp_model_t *mod)
4767 {
4768         Mod_Mesh_ComputeBounds(mod);
4769         Mod_Mesh_MakeSortedSurfaces(mod);
4770         Mod_BuildTextureVectorsFromNormals(0, mod->surfmesh.num_vertices, mod->surfmesh.num_triangles, mod->surfmesh.data_vertex3f, mod->surfmesh.data_texcoordtexture2f, mod->surfmesh.data_normal3f, mod->surfmesh.data_element3i, mod->surfmesh.data_svector3f, mod->surfmesh.data_tvector3f, true);
4771 }