X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=model_brush.c;h=50ef572b9724819bc0535f5f5f14029196b94c21;hb=6fec031c43332c784db51efa5a98061a230ecc9b;hp=a5c0c1ce30a5db57f7a4c3f6e432cf7113d67784;hpb=a6032114473e2c84d3b5e9de914759366224e7b7;p=xonotic%2Fdarkplaces.git diff --git a/model_brush.c b/model_brush.c index a5c0c1ce..50ef572b 100644 --- a/model_brush.c +++ b/model_brush.c @@ -38,6 +38,8 @@ cvar_t r_subdivisions_collision_tolerance = {0, "r_subdivisions_collision_tolera cvar_t r_subdivisions_collision_mintess = {0, "r_subdivisions_collision_mintess", "0", "minimum number of subdivisions (values above 0 will smooth curves that don't need it)"}; cvar_t r_subdivisions_collision_maxtess = {0, "r_subdivisions_collision_maxtess", "1024", "maximum number of subdivisions (prevents curves beyond a certain detail level, limits smoothing)"}; cvar_t r_subdivisions_collision_maxvertices = {0, "r_subdivisions_collision_maxvertices", "4225", "maximum vertices allowed per subdivided curve"}; +cvar_t r_trippy = {0, "r_trippy", "0", "easter egg"}; +cvar_t mod_noshader_default_offsetmapping = {CVAR_SAVE, "mod_noshader_default_offsetmapping", "1", "use offsetmapping by default on all surfaces that are not using q3 shader files"}; cvar_t mod_q3bsp_curves_collisions = {0, "mod_q3bsp_curves_collisions", "1", "enables collisions with curves (SLOW)"}; cvar_t mod_q3bsp_curves_collisions_stride = {0, "mod_q3bsp_curves_collisions_stride", "16", "collisions against curves: optimize performance by doing a combined collision check for this triangle amount first (-1 avoids any box tests)"}; cvar_t mod_q3bsp_curves_stride = {0, "mod_q3bsp_curves_stride", "16", "particle effect collisions against curves: optimize performance by doing a combined collision check for this triangle amount first (-1 avoids any box tests)"}; @@ -46,13 +48,18 @@ cvar_t mod_q3bsp_debugtracebrush = {0, "mod_q3bsp_debugtracebrush", "0", "select cvar_t mod_q3bsp_lightmapmergepower = {CVAR_SAVE, "mod_q3bsp_lightmapmergepower", "4", "merges the quake3 128x128 lightmap textures into larger lightmap group textures to speed up rendering, 1 = 256x256, 2 = 512x512, 3 = 1024x1024, 4 = 2048x2048, 5 = 4096x4096, ..."}; cvar_t mod_q3bsp_nolightmaps = {CVAR_SAVE, "mod_q3bsp_nolightmaps", "0", "do not load lightmaps in Q3BSP maps (to save video RAM, but be warned: it looks ugly)"}; cvar_t mod_q3bsp_tracelineofsight_brushes = {0, "mod_q3bsp_tracelineofsight_brushes", "0", "enables culling of entities behind detail brushes, curves, etc"}; -cvar_t mod_q3shader_default_offsetmapping = {CVAR_SAVE, "mod_q3shader_default_offsetmapping", "1", "use offsetmapping by default on all surfaces"}; +cvar_t mod_q3bsp_sRGBlightmaps = {0, "mod_q3bsp_sRGBlightmaps", "0", "treat lightmaps from Q3 maps as sRGB when vid_sRGB is active"}; +cvar_t mod_q3shader_default_offsetmapping = {CVAR_SAVE, "mod_q3shader_default_offsetmapping", "1", "use offsetmapping by default on all surfaces that are using q3 shader files"}; +cvar_t mod_q3shader_default_offsetmapping_scale = {CVAR_SAVE, "mod_q3shader_default_offsetmapping_scale", "1", "default scale used for offsetmapping"}; +cvar_t mod_q3shader_default_offsetmapping_bias = {CVAR_SAVE, "mod_q3shader_default_offsetmapping_bias", "0", "default bias used for offsetmapping"}; cvar_t mod_q3shader_default_polygonfactor = {0, "mod_q3shader_default_polygonfactor", "0", "biases depth values of 'polygonoffset' shaders to prevent z-fighting artifacts"}; cvar_t mod_q3shader_default_polygonoffset = {0, "mod_q3shader_default_polygonoffset", "-2", "biases depth values of 'polygonoffset' shaders to prevent z-fighting artifacts"}; - +cvar_t mod_q3shader_force_addalpha = {0, "mod_q3shader_force_addalpha", "0", "treat GL_ONE GL_ONE (or add) blendfunc as GL_SRC_ALPHA GL_ONE for compatibility with older DarkPlaces releases"}; cvar_t mod_q1bsp_polygoncollisions = {0, "mod_q1bsp_polygoncollisions", "0", "disables use of precomputed cliphulls and instead collides with polygons (uses Bounding Interval Hierarchy optimizations)"}; cvar_t mod_collision_bih = {0, "mod_collision_bih", "1", "enables use of generated Bounding Interval Hierarchy tree instead of compiled bsp tree in collision code"}; cvar_t mod_recalculatenodeboxes = {0, "mod_recalculatenodeboxes", "1", "enables use of generated node bounding boxes based on BSP tree portal reconstruction, rather than the node boxes supplied by the map compiler"}; +extern cvar_t vid_sRGB; +extern cvar_t vid_sRGB_fallback; static texture_t mod_q1bsp_texture_solid; static texture_t mod_q1bsp_texture_sky; @@ -74,6 +81,8 @@ void Mod_BrushInit(void) Cvar_RegisterVariable(&r_subdivisions_collision_mintess); Cvar_RegisterVariable(&r_subdivisions_collision_maxtess); Cvar_RegisterVariable(&r_subdivisions_collision_maxvertices); + Cvar_RegisterVariable(&r_trippy); + Cvar_RegisterVariable(&mod_noshader_default_offsetmapping); Cvar_RegisterVariable(&mod_q3bsp_curves_collisions); Cvar_RegisterVariable(&mod_q3bsp_curves_collisions_stride); Cvar_RegisterVariable(&mod_q3bsp_curves_stride); @@ -81,14 +90,23 @@ void Mod_BrushInit(void) Cvar_RegisterVariable(&mod_q3bsp_debugtracebrush); Cvar_RegisterVariable(&mod_q3bsp_lightmapmergepower); Cvar_RegisterVariable(&mod_q3bsp_nolightmaps); + Cvar_RegisterVariable(&mod_q3bsp_sRGBlightmaps); Cvar_RegisterVariable(&mod_q3bsp_tracelineofsight_brushes); Cvar_RegisterVariable(&mod_q3shader_default_offsetmapping); + Cvar_RegisterVariable(&mod_q3shader_default_offsetmapping_scale); + Cvar_RegisterVariable(&mod_q3shader_default_offsetmapping_bias); Cvar_RegisterVariable(&mod_q3shader_default_polygonfactor); Cvar_RegisterVariable(&mod_q3shader_default_polygonoffset); + Cvar_RegisterVariable(&mod_q3shader_force_addalpha); Cvar_RegisterVariable(&mod_q1bsp_polygoncollisions); Cvar_RegisterVariable(&mod_collision_bih); Cvar_RegisterVariable(&mod_recalculatenodeboxes); + // these games were made for older DP engines and are no longer + // maintained; use this hack to show their textures properly + if(gamemode == GAME_NEXUIZ) + Cvar_SetQuick(&mod_q3shader_force_addalpha, "1"); + memset(&mod_q1bsp_texture_solid, 0, sizeof(mod_q1bsp_texture_solid)); strlcpy(mod_q1bsp_texture_solid.name, "solid" , sizeof(mod_q1bsp_texture_solid.name)); mod_q1bsp_texture_solid.surfaceflags = 0; @@ -1155,7 +1173,7 @@ void Collision_ClipTrace_Point(trace_t *trace, const vec3_t cmins, const vec3_t static qboolean Mod_Q1BSP_TraceLineOfSight(struct model_s *model, const vec3_t start, const vec3_t end) { trace_t trace; - model->TraceLine(model, NULL, NULL, &trace, start, end, SUPERCONTENTS_VISBLOCKERMASK); + Mod_Q1BSP_TraceLine(model, NULL, NULL, &trace, start, end, SUPERCONTENTS_VISBLOCKERMASK); return trace.fraction == 1; } @@ -1278,7 +1296,7 @@ loc0: } } -void Mod_Q1BSP_LightPoint(dp_model_t *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal) +static void Mod_Q1BSP_LightPoint(dp_model_t *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal) { // pretend lighting is coming down from above (due to lack of a lightgrid to know primary lighting direction) VectorSet(diffusenormal, 0, 0, 1); @@ -1516,7 +1534,7 @@ R_Q1BSP_LoadSplitSky A sky texture is 256*128, with the right side being a masked overlay ============== */ -void R_Q1BSP_LoadSplitSky (unsigned char *src, int width, int height, int bytesperpixel) +static void R_Q1BSP_LoadSplitSky (unsigned char *src, int width, int height, int bytesperpixel) { int x, y; int w = width/2; @@ -1576,8 +1594,8 @@ void R_Q1BSP_LoadSplitSky (unsigned char *src, int width, int height, int bytesp } } - loadmodel->brush.solidskyskinframe = R_SkinFrame_LoadInternalBGRA("sky_solidtexture", 0 , (unsigned char *) solidpixels, w, h); - loadmodel->brush.alphaskyskinframe = R_SkinFrame_LoadInternalBGRA("sky_alphatexture", TEXF_ALPHA, (unsigned char *) alphapixels, w, h); + loadmodel->brush.solidskyskinframe = R_SkinFrame_LoadInternalBGRA("sky_solidtexture", 0 , (unsigned char *) solidpixels, w, h, vid.sRGB3D); + loadmodel->brush.alphaskyskinframe = R_SkinFrame_LoadInternalBGRA("sky_alphatexture", TEXF_ALPHA, (unsigned char *) alphapixels, w, h, vid.sRGB3D); Mem_Free(solidpixels); Mem_Free(alphapixels); } @@ -1588,13 +1606,15 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) skinframe_t *skinframe; miptex_t *dmiptex; texture_t *tx, *tx2, *anims[10], *altanims[10]; + texture_t backuptex; dmiptexlump_t *m; unsigned char *data, *mtdata; const char *s; char mapname[MAX_QPATH], name[MAX_QPATH]; - unsigned char zero[4]; - - memset(zero, 0, sizeof(zero)); + unsigned char zeroopaque[4], zerotrans[4]; + char vabuf[1024]; + Vector4Set(zeroopaque, 0, 0, 0, 255); + Vector4Set(zerotrans, 0, 0, 0, 128); loadmodel->data_textures = NULL; @@ -1654,8 +1674,9 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) tx->reflectfactor = 1; Vector4Set(tx->reflectcolor4f, 1, 1, 1, 1); tx->r_water_wateralpha = 1; - tx->offsetmapping = OFFSETMAPPING_OFF; + tx->offsetmapping = OFFSETMAPPING_DEFAULT; tx->offsetscale = 1; + tx->offsetbias = 0; tx->specularscalemod = 1; tx->specularpowermod = 1; } @@ -1722,8 +1743,11 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) if (name[j] >= 'A' && name[j] <= 'Z') name[j] += 'a' - 'A'; + // LordHavoc: backup the texture_t because q3 shader loading overwrites it + backuptex = loadmodel->data_textures[i]; if (dmiptex->name[0] && Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, name, false, false, 0)) continue; + loadmodel->data_textures[i] = backuptex; tx = loadmodel->data_textures + i; strlcpy(tx->name, name, sizeof(tx->name)); @@ -1764,9 +1788,9 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) // LordHavoc: HL sky textures are entirely different than quake if (!loadmodel->brush.ishlbsp && !strncmp(tx->name, "sky", 3) && mtwidth == mtheight * 2) { - data = loadimagepixelsbgra(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s/%s", mapname, tx->name), false, false, r_texture_convertsRGB_skin.integer != 0, NULL); + data = loadimagepixelsbgra(gamemode == GAME_TENEBRAE ? tx->name : va(vabuf, sizeof(vabuf), "textures/%s/%s", mapname, tx->name), false, false, false, NULL); if (!data) - data = loadimagepixelsbgra(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s", tx->name), false, false, r_texture_convertsRGB_skin.integer != 0, NULL); + data = loadimagepixelsbgra(gamemode == GAME_TENEBRAE ? tx->name : va(vabuf, sizeof(vabuf), "textures/%s", tx->name), false, false, false, NULL); if (data && image_width == image_height * 2) { R_Q1BSP_LoadSplitSky(data, image_width, image_height, 4); @@ -1777,9 +1801,11 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) } else { - skinframe = R_SkinFrame_LoadExternal(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s/%s", mapname, tx->name), TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS, false); + skinframe = R_SkinFrame_LoadExternal(gamemode == GAME_TENEBRAE ? tx->name : va(vabuf, sizeof(vabuf), "textures/%s/%s", mapname, tx->name), TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS, false); if (!skinframe) - skinframe = R_SkinFrame_LoadExternal(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s", tx->name), TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS, false); + skinframe = R_SkinFrame_LoadExternal(gamemode == GAME_TENEBRAE ? tx->name : va(vabuf, sizeof(vabuf), "textures/%s", tx->name), TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS, false); + if (skinframe) + tx->offsetmapping = OFFSETMAPPING_DEFAULT; // allow offsetmapping on external textures without a q3 shader if (!skinframe) { // did not find external texture, load it from the bsp or wad3 @@ -1796,7 +1822,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) { tx->width = image_width; tx->height = image_height; - skinframe = R_SkinFrame_LoadInternalBGRA(tx->name, TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP, pixels, image_width, image_height); + skinframe = R_SkinFrame_LoadInternalBGRA(tx->name, TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP, pixels, image_width, image_height, true); } if (freepixels) Mem_Free(freepixels); @@ -1808,44 +1834,44 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) if (skinframe) tx->skinframes[0] = skinframe; } - - tx->basematerialflags = MATERIALFLAG_WALL; - if (tx->name[0] == '*') - { - // LordHavoc: some turbulent textures should not be affected by wateralpha - if (!strncmp(tx->name, "*glassmirror", 12)) // Tenebrae - { - // replace the texture with transparent black - Vector4Set(zero, 128, 128, 128, 128); - tx->skinframes[0] = R_SkinFrame_LoadInternalBGRA(tx->name, TEXF_MIPMAP | TEXF_ALPHA, zero, 1, 1); - tx->basematerialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_REFLECTION; - } - else if (!strncmp(tx->name,"*lava",5) - || !strncmp(tx->name,"*teleport",9) - || !strncmp(tx->name,"*rift",5)) // Scourge of Armagon texture - tx->basematerialflags |= MATERIALFLAG_WATERSCROLL | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW; - else - tx->basematerialflags |= MATERIALFLAG_WATERSCROLL | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW | MATERIALFLAG_WATERALPHA | MATERIALFLAG_WATERSHADER; - if (tx->skinframes[0] && tx->skinframes[0]->hasalpha) - tx->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; - } + // LordHavoc: some Tenebrae textures get replaced by black + if (!strncmp(tx->name, "*glassmirror", 12)) // Tenebrae + tx->skinframes[0] = R_SkinFrame_LoadInternalBGRA(tx->name, TEXF_MIPMAP | TEXF_ALPHA, zerotrans, 1, 1, false); else if (!strncmp(tx->name, "mirror", 6)) // Tenebrae - { - // replace the texture with black - tx->skinframes[0] = R_SkinFrame_LoadInternalBGRA(tx->name, 0, zero, 1, 1); - tx->basematerialflags |= MATERIALFLAG_REFLECTION; - } - else if (!strncmp(tx->name, "sky", 3)) - tx->basematerialflags = MATERIALFLAG_SKY | MATERIALFLAG_NOSHADOW; - else if (!strcmp(tx->name, "caulk")) - tx->basematerialflags = MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW; - else if (tx->skinframes[0] && tx->skinframes[0]->hasalpha) - tx->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; + tx->skinframes[0] = R_SkinFrame_LoadInternalBGRA(tx->name, 0, zeroopaque, 1, 1, false); + } - // start out with no animation - tx->currentframe = tx; - tx->currentskinframe = tx->skinframes[0]; + tx->basematerialflags = MATERIALFLAG_WALL; + if (tx->name[0] == '*') + { + // LordHavoc: some turbulent textures should not be affected by wateralpha + if (!strncmp(tx->name, "*glassmirror", 12)) // Tenebrae + tx->basematerialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_REFLECTION; + else if (!strncmp(tx->name,"*lava",5) + || !strncmp(tx->name,"*teleport",9) + || !strncmp(tx->name,"*rift",5)) // Scourge of Armagon texture + tx->basematerialflags |= MATERIALFLAG_WATERSCROLL | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW; + else + tx->basematerialflags |= MATERIALFLAG_WATERSCROLL | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_NOSHADOW | MATERIALFLAG_WATERALPHA | MATERIALFLAG_WATERSHADER; + if (tx->skinframes[0] && tx->skinframes[0]->hasalpha) + tx->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; } + else if (!strncmp(tx->name, "mirror", 6)) // Tenebrae + { + // replace the texture with black + tx->basematerialflags |= MATERIALFLAG_REFLECTION; + } + else if (!strncmp(tx->name, "sky", 3)) + tx->basematerialflags = MATERIALFLAG_SKY | MATERIALFLAG_NOSHADOW; + else if (!strcmp(tx->name, "caulk")) + tx->basematerialflags = MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW; + else if (tx->skinframes[0] && tx->skinframes[0]->hasalpha) + tx->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; + + // start out with no animation + tx->currentframe = tx; + tx->currentskinframe = tx->skinframes[0]; + tx->currentmaterialflags = tx->basematerialflags; } // sequence the animations @@ -2052,13 +2078,13 @@ static void Mod_Q1BSP_ParseWadsFromEntityLump(const char *data) int i, j, k; if (!data) return; - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) return; // error if (com_token[0] != '{') return; // error while (1) { - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) return; // error if (com_token[0] == '}') break; // end of worldspawn @@ -2068,7 +2094,7 @@ static void Mod_Q1BSP_ParseWadsFromEntityLump(const char *data) strlcpy(key, com_token, sizeof(key)); while (key[strlen(key)-1] == ' ') // remove trailing spaces key[strlen(key)-1] = 0; - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) return; // error dpsnprintf(value, sizeof(value), "%s", com_token); if (!strcmp("wad", key)) // for HalfLife maps @@ -2141,7 +2167,7 @@ static void Mod_Q1BSP_LoadVertexes(lump_t *l) // The following two functions should be removed and MSG_* or SZ_* function sets adjusted so they // can be used for this // REMOVEME -int SB_ReadInt (unsigned char **buffer) +static int SB_ReadInt (unsigned char **buffer) { int i; i = ((*buffer)[0]) + 256*((*buffer)[1]) + 65536*((*buffer)[2]) + 16777216*((*buffer)[3]); @@ -2150,7 +2176,7 @@ int SB_ReadInt (unsigned char **buffer) } // REMOVEME -float SB_ReadFloat (unsigned char **buffer) +static float SB_ReadFloat (unsigned char **buffer) { union { @@ -2437,6 +2463,7 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l) int i, j, count, surfacenum, planenum, smax, tmax, ssize, tsize, firstedge, numedges, totalverts, totaltris, lightmapnumber, lightmapsize, totallightmapsamples; float texmins[2], texmaxs[2], val; rtexture_t *lightmaptexture, *deluxemaptexture; + char vabuf[1024]; in = (dface_t *)(mod_base + l->fileofs); if (l->filelen % sizeof(*in)) @@ -2645,9 +2672,9 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l) loadmodel->brushq3.num_mergedlightmaps = lightmapnumber + 1; loadmodel->brushq3.data_lightmaps = (rtexture_t **)Mem_Realloc(loadmodel->mempool, loadmodel->brushq3.data_lightmaps, loadmodel->brushq3.num_mergedlightmaps * sizeof(loadmodel->brushq3.data_lightmaps[0])); loadmodel->brushq3.data_deluxemaps = (rtexture_t **)Mem_Realloc(loadmodel->mempool, loadmodel->brushq3.data_deluxemaps, loadmodel->brushq3.num_mergedlightmaps * sizeof(loadmodel->brushq3.data_deluxemaps[0])); - loadmodel->brushq3.data_lightmaps[lightmapnumber] = lightmaptexture = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%i", lightmapnumber), lightmapsize, lightmapsize, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_ALLOWUPDATES, -1, NULL); + loadmodel->brushq3.data_lightmaps[lightmapnumber] = lightmaptexture = R_LoadTexture2D(loadmodel->texturepool, va(vabuf, sizeof(vabuf), "lightmap%i", lightmapnumber), lightmapsize, lightmapsize, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_ALLOWUPDATES, -1, NULL); if (loadmodel->brushq1.nmaplightdata) - loadmodel->brushq3.data_deluxemaps[lightmapnumber] = deluxemaptexture = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%i", lightmapnumber), lightmapsize, lightmapsize, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_ALLOWUPDATES, -1, NULL); + loadmodel->brushq3.data_deluxemaps[lightmapnumber] = deluxemaptexture = R_LoadTexture2D(loadmodel->texturepool, va(vabuf, sizeof(vabuf), "deluxemap%i", lightmapnumber), lightmapsize, lightmapsize, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_ALLOWUPDATES, -1, NULL); lightmapnumber++; Mod_AllocLightmap_Reset(&allocState); Mod_AllocLightmap_Block(&allocState, ssize, tsize, &lightmapx, &lightmapy); @@ -2867,7 +2894,7 @@ static void Mod_Q1BSP_LoadLeafs(lump_t *l) } } -qboolean Mod_Q1BSP_CheckWaterAlphaSupport(void) +static qboolean Mod_Q1BSP_CheckWaterAlphaSupport(void) { int i, j; mleaf_t *leaf; @@ -3037,12 +3064,12 @@ static void Mod_Q1BSP_LoadMapBrushes(void) if (!maptext) return; text = maptext; - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) return; // error submodel = 0; for (;;) { - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) break; if (com_token[0] != '{') return; // error @@ -3053,7 +3080,7 @@ static void Mod_Q1BSP_LoadMapBrushes(void) brushes = Mem_Alloc(loadmodel->mempool, maxbrushes * sizeof(mbrush_t)); for (;;) { - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) return; // error if (com_token[0] == '}') break; // end of entity @@ -3077,7 +3104,7 @@ static void Mod_Q1BSP_LoadMapBrushes(void) } for (;;) { - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) return; // error if (com_token[0] == '}') break; // end of brush @@ -3086,25 +3113,25 @@ static void Mod_Q1BSP_LoadMapBrushes(void) // FIXME: support hl .map format for (pointnum = 0;pointnum < 3;pointnum++) { - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); for (componentnum = 0;componentnum < 3;componentnum++) { - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); point[pointnum][componentnum] = atof(com_token); } - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); } - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); strlcpy(facetexture, com_token, sizeof(facetexture)); - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); //scroll_s = atof(com_token); - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); //scroll_t = atof(com_token); - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); //rotate = atof(com_token); - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); //scale_s = atof(com_token); - COM_ParseToken_Simple(&data, false, false); + COM_ParseToken_Simple(&data, false, false, true); //scale_t = atof(com_token); TriangleNormal(point[0], point[1], point[2], planenormal); VectorNormalizeDouble(planenormal); @@ -3562,7 +3589,7 @@ static int Mod_Q1BSP_FatPVS(dp_model_t *model, const vec3_t org, vec_t radius, u { int bytes = model->brush.num_pvsclusterbytes; bytes = min(bytes, pvsbufferlength); - if (r_novis.integer || !model->brush.num_pvsclusters || !Mod_Q1BSP_GetPVS(model, org)) + if (r_novis.integer || r_trippy.integer || !model->brush.num_pvsclusters || !Mod_Q1BSP_GetPVS(model, org)) { memset(pvsbuffer, 0xFF, bytes); return bytes; @@ -3690,7 +3717,10 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend) mod->soundfromcenter = true; mod->TraceBox = Mod_Q1BSP_TraceBox; - mod->TraceLine = Mod_Q1BSP_TraceLineAgainstSurfaces; // LordHavoc: use the surface-hitting version of TraceLine in all cases + if (sv_gameplayfix_q1bsptracelinereportstexture.integer) + mod->TraceLine = Mod_Q1BSP_TraceLineAgainstSurfaces; // LordHavoc: use the surface-hitting version of TraceLine in all cases + else + mod->TraceLine = Mod_Q1BSP_TraceLine; mod->TracePoint = Mod_Q1BSP_TracePoint; mod->PointSuperContents = Mod_Q1BSP_PointSuperContents; mod->TraceLineAgainstSurfaces = Mod_Q1BSP_TraceLineAgainstSurfaces; @@ -4348,7 +4378,7 @@ static void Mod_Q2BSP_LoadModels(lump_t *l) */ } -void static Mod_Q2BSP_Load(dp_model_t *mod, void *buffer, void *bufferend) +static void Mod_Q2BSP_Load(dp_model_t *mod, void *buffer, void *bufferend) { int i; q2dheader_t *header; @@ -4424,11 +4454,11 @@ static void Mod_Q3BSP_LoadEntities(lump_t *l) // some Q3 maps override the lightgrid_cellsize with a worldspawn key // VorteX: q3map2 FS-R generates tangentspace deluxemaps for q3bsp and sets 'deluxeMaps' key loadmodel->brushq3.deluxemapping = false; - if (data && COM_ParseToken_Simple(&data, false, false) && com_token[0] == '{') + if (data && COM_ParseToken_Simple(&data, false, false, true) && com_token[0] == '{') { while (1) { - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) break; // error if (com_token[0] == '}') break; // end of worldspawn @@ -4438,7 +4468,7 @@ static void Mod_Q3BSP_LoadEntities(lump_t *l) strlcpy(key, com_token, sizeof(key)); while (key[strlen(key)-1] == ' ') // remove trailing spaces key[strlen(key)-1] = 0; - if (!COM_ParseToken_Simple(&data, false, false)) + if (!COM_ParseToken_Simple(&data, false, false, true)) break; // error strlcpy(value, com_token, sizeof(value)); if (!strcasecmp("gridsize", key)) // this one is case insensitive to 100% match q3map2 @@ -4699,9 +4729,43 @@ static void Mod_Q3BSP_LoadVertices(lump_t *l) loadmodel->brushq3.data_texcoordlightmap2f[i * 2 + 0] = LittleFloat(in->lightmap2f[0]); loadmodel->brushq3.data_texcoordlightmap2f[i * 2 + 1] = LittleFloat(in->lightmap2f[1]); // svector/tvector are calculated later in face loading - loadmodel->brushq3.data_color4f[i * 4 + 0] = in->color4ub[0] * (1.0f / 255.0f); - loadmodel->brushq3.data_color4f[i * 4 + 1] = in->color4ub[1] * (1.0f / 255.0f); - loadmodel->brushq3.data_color4f[i * 4 + 2] = in->color4ub[2] * (1.0f / 255.0f); + if(mod_q3bsp_sRGBlightmaps.integer) + { + // if lightmaps are sRGB, vertex colors are sRGB too, so we need to linearize them + // note: when this is in use, lightmap color 128 is no longer neutral, but "sRGB half power" is + // working like this may be odd, but matches q3map2 -gamma 2.2 + if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D) + { + // actually we do: Image_sRGBFloatFromLinear_Lightmap(Image_LinearFloatFromsRGBFloat(x)) + // neutral point is at Image_sRGBFloatFromLinearFloat(0.5) + // so we need to map Image_sRGBFloatFromLinearFloat(0.5) to 0.5 + // factor is 0.5 / Image_sRGBFloatFromLinearFloat(0.5) + loadmodel->brushq3.data_color4f[i * 4 + 0] = in->color4ub[0] * (1.0f / 255.0f) * 0.679942f; // fixes neutral level + loadmodel->brushq3.data_color4f[i * 4 + 1] = in->color4ub[1] * (1.0f / 255.0f) * 0.679942f; // fixes neutral level + loadmodel->brushq3.data_color4f[i * 4 + 2] = in->color4ub[2] * (1.0f / 255.0f) * 0.679942f; // fixes neutral level + } + else + { + loadmodel->brushq3.data_color4f[i * 4 + 0] = Image_LinearFloatFromsRGB(in->color4ub[0]); + loadmodel->brushq3.data_color4f[i * 4 + 1] = Image_LinearFloatFromsRGB(in->color4ub[1]); + loadmodel->brushq3.data_color4f[i * 4 + 2] = Image_LinearFloatFromsRGB(in->color4ub[2]); + } + } + else + { + if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D) + { + loadmodel->brushq3.data_color4f[i * 4 + 0] = Image_sRGBFloatFromLinear_Lightmap(in->color4ub[0]); + loadmodel->brushq3.data_color4f[i * 4 + 1] = Image_sRGBFloatFromLinear_Lightmap(in->color4ub[1]); + loadmodel->brushq3.data_color4f[i * 4 + 2] = Image_sRGBFloatFromLinear_Lightmap(in->color4ub[2]); + } + else + { + loadmodel->brushq3.data_color4f[i * 4 + 0] = in->color4ub[0] * (1.0f / 255.0f); + loadmodel->brushq3.data_color4f[i * 4 + 1] = in->color4ub[1] * (1.0f / 255.0f); + loadmodel->brushq3.data_color4f[i * 4 + 2] = in->color4ub[2] * (1.0f / 255.0f); + } + } loadmodel->brushq3.data_color4f[i * 4 + 3] = in->color4ub[3] * (1.0f / 255.0f); if(in->color4ub[0] != 255 || in->color4ub[1] != 255 || in->color4ub[2] != 255) loadmodel->lit = true; @@ -4773,6 +4837,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump) char mapname[MAX_QPATH]; qboolean external; unsigned char *inpixels[10000]; // max count q3map2 can output (it uses 4 digits) + char vabuf[1024]; // defaults for q3bsp size = 128; @@ -4809,7 +4874,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump) if (developer_loading.integer) Con_Printf("Using external lightmaps\n"); FS_StripExtension(loadmodel->name, mapname, sizeof(mapname)); - inpixels[0] = loadimagepixelsbgra(va("%s/lm_%04d", mapname, 0), false, false, false, NULL); + inpixels[0] = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s/lm_%04d", mapname, 0), false, false, false, NULL); if(!inpixels[0]) return; @@ -4829,7 +4894,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump) for(count = 1; ; ++count) { - inpixels[count] = loadimagepixelsbgra(va("%s/lm_%04d", mapname, count), false, false, false, NULL); + inpixels[count] = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s/lm_%04d", mapname, count), false, false, false, NULL); if(!inpixels[count]) break; // we got all of them if(image_width != size || image_height != size) @@ -4967,7 +5032,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump) mergebuf = (loadmodel->brushq3.deluxemapping && (i & 1)) ? mergeddeluxepixels : mergedpixels; mergebuf += 4 * (realindex & (mergedcolumns-1))*size + 4 * ((realindex >> powerx) & (mergedrows-1))*mergedwidth*size; if ((i & 1) == 0 || !loadmodel->brushq3.deluxemapping) - Con_Printf("copying original lightmap %i (%ix%i) to %i (at %i,%i)\n", i, size, size, lightmapindex, (realindex & (mergedcolumns-1))*size, ((realindex >> powerx) & (mergedrows-1))*size); + Con_DPrintf("copying original lightmap %i (%ix%i) to %i (at %i,%i)\n", i, size, size, lightmapindex, (realindex & (mergedcolumns-1))*size, ((realindex >> powerx) & (mergedrows-1))*size); // convert pixels from RGB or BGRA while copying them into the destination rectangle for (j = 0;j < size;j++) @@ -4983,9 +5048,40 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump) if (((realindex + 1) & (mergedrowsxcolumns - 1)) == 0 || (realindex + 1) == realcount) { if (loadmodel->brushq3.deluxemapping && (i & 1)) - loadmodel->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%04i", lightmapindex), mergedwidth, mergedheight, mergeddeluxepixels, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bspdeluxemaps.integer ? TEXF_COMPRESS : 0), -1, NULL); + loadmodel->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va(vabuf, sizeof(vabuf), "deluxemap%04i", lightmapindex), mergedwidth, mergedheight, mergeddeluxepixels, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bspdeluxemaps.integer ? TEXF_COMPRESS : 0), -1, NULL); else - loadmodel->brushq3.data_lightmaps [lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%04i", lightmapindex), mergedwidth, mergedheight, mergedpixels, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : 0), -1, NULL); + { + if(mod_q3bsp_sRGBlightmaps.integer) + { + textype_t t; + if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D) + { + // TODO (should we do this, or should we instead knowingly render brighter in sRGB fallback mode?) + int n = mergedwidth * mergedheight * 4; + int i; + for(i = 0; i < n; i += 4) + { + // actually we do: Image_sRGBFloatFromLinear_Lightmap(Image_LinearFloatFromsRGBFloat(x)) + // neutral point is at Image_sRGBFloatFromLinearFloat(0.5) + // so we need to map Image_sRGBFloatFromLinearFloat(0.5) to 0.5 + // factor is 0.5 / Image_sRGBFloatFromLinearFloat(0.5) + mergedpixels[i+0] = (mergedpixels[i+0] * (int)173 + 128) / 255; + mergedpixels[i+1] = (mergedpixels[i+1] * (int)173 + 128) / 255; + mergedpixels[i+2] = (mergedpixels[i+2] * (int)173 + 128) / 255; + } + t = TEXTYPE_BGRA; // in stupid fallback mode, we upload lightmaps in sRGB form and just fix their brightness + } + else + t = TEXTYPE_SRGB_BGRA; // normally, we upload lightmaps in sRGB form (possibly downconverted to linear) + loadmodel->brushq3.data_lightmaps [lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va(vabuf, sizeof(vabuf), "lightmap%04i", lightmapindex), mergedwidth, mergedheight, mergedpixels, t, TEXF_FORCELINEAR | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : 0), -1, NULL); + } + else + { + if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D) + Image_MakesRGBColorsFromLinear_Lightmap(mergedpixels, mergedpixels, mergedwidth * mergedheight); + loadmodel->brushq3.data_lightmaps [lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va(vabuf, sizeof(vabuf), "lightmap%04i", lightmapindex), mergedwidth, mergedheight, mergedpixels, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : 0), -1, NULL); + } + } } } @@ -5735,6 +5831,7 @@ static void Mod_Q3BSP_LoadLightGrid(lump_t *l) q3dlightgrid_t *in; q3dlightgrid_t *out; int count; + int i; in = (q3dlightgrid_t *)(mod_base + l->fileofs); if (l->filelen % sizeof(*in)) @@ -5770,6 +5867,58 @@ static void Mod_Q3BSP_LoadLightGrid(lump_t *l) loadmodel->brushq3.num_lightgrid = count; // no swapping or validation necessary memcpy(out, in, count * (int)sizeof(*out)); + + if(mod_q3bsp_sRGBlightmaps.integer) + { + if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D) + { + // TODO (should we do this, or should we instead knowingly render brighter in sRGB fallback mode?) + for(i = 0; i < count; ++i) + { + // actually we do: Image_sRGBFloatFromLinear_Lightmap(Image_LinearFloatFromsRGBFloat(x)) + // neutral point is at Image_sRGBFloatFromLinearFloat(0.5) + // so we need to map Image_sRGBFloatFromLinearFloat(0.5) to 0.5 + // factor is 0.5 / Image_sRGBFloatFromLinearFloat(0.5) + out[i].ambientrgb[0] = (out[i].ambientrgb[0] * (int)173 + 128) / 255; // fixes neutral level + out[i].ambientrgb[1] = (out[i].ambientrgb[1] * (int)173 + 128) / 255; // fixes neutral level + out[i].ambientrgb[2] = (out[i].ambientrgb[2] * (int)173 + 128) / 255; // fixes neutral level + out[i].diffusergb[0] = (out[i].diffusergb[0] * (int)173 + 128) / 255; // fixes neutral level + out[i].diffusergb[1] = (out[i].diffusergb[1] * (int)173 + 128) / 255; // fixes neutral level + out[i].diffusergb[2] = (out[i].diffusergb[2] * (int)173 + 128) / 255; // fixes neutral level + } + } + else + { + for(i = 0; i < count; ++i) + { + out[i].ambientrgb[0] = floor(Image_LinearFloatFromsRGB(out[i].ambientrgb[0]) * 255.0f + 0.5f); + out[i].ambientrgb[1] = floor(Image_LinearFloatFromsRGB(out[i].ambientrgb[1]) * 255.0f + 0.5f); + out[i].ambientrgb[2] = floor(Image_LinearFloatFromsRGB(out[i].ambientrgb[2]) * 255.0f + 0.5f); + out[i].diffusergb[0] = floor(Image_LinearFloatFromsRGB(out[i].diffusergb[0]) * 255.0f + 0.5f); + out[i].diffusergb[1] = floor(Image_LinearFloatFromsRGB(out[i].diffusergb[1]) * 255.0f + 0.5f); + out[i].diffusergb[2] = floor(Image_LinearFloatFromsRGB(out[i].diffusergb[2]) * 255.0f + 0.5f); + } + } + } + else + { + if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D) + { + for(i = 0; i < count; ++i) + { + out[i].ambientrgb[0] = floor(Image_sRGBFloatFromLinear_Lightmap(out[i].ambientrgb[0]) * 255.0f + 0.5f); + out[i].ambientrgb[1] = floor(Image_sRGBFloatFromLinear_Lightmap(out[i].ambientrgb[1]) * 255.0f + 0.5f); + out[i].ambientrgb[2] = floor(Image_sRGBFloatFromLinear_Lightmap(out[i].ambientrgb[2]) * 255.0f + 0.5f); + out[i].diffusergb[0] = floor(Image_sRGBFloatFromLinear_Lightmap(out[i].diffusergb[0]) * 255.0f + 0.5f); + out[i].diffusergb[1] = floor(Image_sRGBFloatFromLinear_Lightmap(out[i].diffusergb[1]) * 255.0f + 0.5f); + out[i].diffusergb[2] = floor(Image_sRGBFloatFromLinear_Lightmap(out[i].diffusergb[2]) * 255.0f + 0.5f); + } + } + else + { + // all is good + } + } } } @@ -5831,6 +5980,7 @@ static void Mod_Q3BSP_LightPoint(dp_model_t *model, const vec3_t p, vec3_t ambie break; case RENDERPATH_GL11: case RENDERPATH_GL13: + case RENDERPATH_GLES1: stylescale = r_refdef.scene.rtlightstylevalue[0]; break; } @@ -5969,33 +6119,58 @@ static qboolean Mod_Q3BSP_TraceLineOfSight(struct model_s *model, const vec3_t s } } -static void Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace_t *trace, dp_model_t *model, int nodenum, const vec3_t point) +void Mod_CollisionBIH_TracePoint(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, int hitsupercontentsmask) { + const bih_t *bih; const bih_leaf_t *leaf; const bih_node_t *node; const colbrushf_t *brush; int axis; - for(;;) + int nodenum; + int nodestackpos = 0; + int nodestack[1024]; + + memset(trace, 0, sizeof(*trace)); + trace->fraction = 1; + trace->realfraction = 1; + trace->hitsupercontentsmask = hitsupercontentsmask; + + bih = &model->collision_bih; + if(!bih->nodes) + return; + + nodenum = bih->rootnode; + nodestack[nodestackpos++] = nodenum; + while (nodestackpos) { - node = model->collision_bih.nodes + nodenum; + nodenum = nodestack[--nodestackpos]; + node = bih->nodes + nodenum; #if 1 - if (!BoxesOverlap(point, point, node->mins, node->maxs)) - return; + if (!BoxesOverlap(start, start, node->mins, node->maxs)) + continue; #endif - if (node->type == BIH_UNORDERED) + if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024) + { + axis = node->type - BIH_SPLITX; + if (start[axis] >= node->frontmin) + nodestack[nodestackpos++] = node->front; + if (start[axis] <= node->backmax) + nodestack[nodestackpos++] = node->back; + } + else if (node->type == BIH_UNORDERED) { for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++) { - leaf = model->collision_bih.leafs + node->children[axis]; + leaf = bih->leafs + node->children[axis]; #if 1 - if (!BoxesOverlap(point, point, leaf->mins, leaf->maxs)) + if (!BoxesOverlap(start, start, leaf->mins, leaf->maxs)) continue; #endif switch(leaf->type) { case BIH_BRUSH: brush = model->brush.data_brushes[leaf->itemindex].colbrushf; - Collision_TracePointBrushFloat(trace, point, brush); + Collision_TracePointBrushFloat(trace, start, brush); break; case BIH_COLLISIONTRIANGLE: // collision triangle - skipped because they have no volume @@ -6005,353 +6180,244 @@ static void Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace_t *trace, dp_mode break; } } - return; } - axis = node->type - BIH_SPLITX; - if (point[axis] <= node->backmax) - { - if (point[axis] >= node->frontmin) - Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, node->front, point); - nodenum = node->back; - } - else if (point[axis] >= node->frontmin) - nodenum = node->front; - else // no overlap with either child? just return - return; } } -static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model_t *model, bih_t *bih, int nodenum, const vec3_t start, const vec3_t end, const vec3_t linestart, const vec3_t lineend) +static void Mod_CollisionBIH_TraceLineShared(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask, const bih_t *bih) { const bih_leaf_t *leaf; const bih_node_t *node; const colbrushf_t *brush; const int *e; const texture_t *texture; - int axis; -#define BIHLINECLIP -#ifdef BIHLINECLIP - int sideflags; - vec_t frontdist1; - vec_t frontdist2; - vec_t frontfrac; - vec_t backdist1; - vec_t backdist2; - vec_t backfrac; - vec3_t clipped, newstart, newend; -#endif - vec3_t segmentmins; - vec3_t segmentmaxs; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); - for (;;) + vec3_t nodebigmins, nodebigmaxs, nodestart, nodeend, sweepnodemins, sweepnodemaxs; + vec_t d1, d2, d3, d4, f, nodestackline[1024][6]; + int axis, nodenum, nodestackpos = 0, nodestack[1024]; + + if(!bih->nodes) + return; + + if (VectorCompare(start, end)) { + Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask); + return; + } + + nodenum = bih->rootnode; + + memset(trace, 0, sizeof(*trace)); + trace->fraction = 1; + trace->realfraction = 1; + trace->hitsupercontentsmask = hitsupercontentsmask; + + // push first node + nodestackline[nodestackpos][0] = start[0]; + nodestackline[nodestackpos][1] = start[1]; + nodestackline[nodestackpos][2] = start[2]; + nodestackline[nodestackpos][3] = end[0]; + nodestackline[nodestackpos][4] = end[1]; + nodestackline[nodestackpos][5] = end[2]; + nodestack[nodestackpos++] = nodenum; + while (nodestackpos) + { + nodenum = nodestack[--nodestackpos]; node = bih->nodes + nodenum; -#if 1 - if (!BoxesOverlap(segmentmins, segmentmaxs, node->mins, node->maxs)) - return; -#endif - if (node->type == BIH_UNORDERED) - { + VectorCopy(nodestackline[nodestackpos], nodestart); + VectorCopy(nodestackline[nodestackpos] + 3, nodeend); + sweepnodemins[0] = min(nodestart[0], nodeend[0]); sweepnodemins[1] = min(nodestart[1], nodeend[1]); sweepnodemins[2] = min(nodestart[2], nodeend[2]); sweepnodemaxs[0] = max(nodestart[0], nodeend[0]); sweepnodemaxs[1] = max(nodestart[1], nodeend[1]); sweepnodemaxs[2] = max(nodestart[2], nodeend[2]); + if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, node->mins, node->maxs)) + continue; + if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024) + { + // recurse children of the split + axis = node->type - BIH_SPLITX; + d1 = node->backmax - nodestart[axis]; + d2 = node->backmax - nodeend[axis]; + d3 = nodestart[axis] - node->frontmin; + d4 = nodeend[axis] - node->frontmin; + switch((d1 < 0) | ((d2 < 0) << 1) | ((d3 < 0) << 2) | ((d4 < 0) << 3)) + { + case 0: /* >>>> */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 1: /* <>>> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 2: /* ><>> */ f = d1 / (d1 - d2); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 3: /* <<>> */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 4: /* >><> */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 5: /* <><> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 6: /* ><<> */ f = d1 / (d1 - d2); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 7: /* <<<> */ f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 8: /* >>>< */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 9: /* <>>< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 10: /* ><>< */ f = d1 / (d1 - d2); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 11: /* <<>< */ f = d3 / (d3 - d4); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 12: /* >><< */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; break; + case 13: /* <><< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; break; + case 14: /* ><<< */ f = d1 / (d1 - d2); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; break; + case 15: /* <<<< */ break; + } + } + else if (node->type == BIH_UNORDERED) + { + // calculate sweep bounds for this node + // copy node bounds into local variables + VectorCopy(node->mins, nodebigmins); + VectorCopy(node->maxs, nodebigmaxs); + // clip line to this node bounds + axis = 0; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } + axis = 1; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } + axis = 2; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } + // some of the line intersected the enlarged node box + // calculate sweep bounds for this node + sweepnodemins[0] = min(nodestart[0], nodeend[0]); sweepnodemins[1] = min(nodestart[1], nodeend[1]); sweepnodemins[2] = min(nodestart[2], nodeend[2]); sweepnodemaxs[0] = max(nodestart[0], nodeend[0]); sweepnodemaxs[1] = max(nodestart[1], nodeend[1]); sweepnodemaxs[2] = max(nodestart[2], nodeend[2]); for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++) { - leaf = model->collision_bih.leafs + node->children[axis]; -#if 1 - if (!BoxesOverlap(segmentmins, segmentmaxs, leaf->mins, leaf->maxs)) + leaf = bih->leafs + node->children[axis]; + if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, leaf->mins, leaf->maxs)) continue; -#endif switch(leaf->type) { case BIH_BRUSH: brush = model->brush.data_brushes[leaf->itemindex].colbrushf; - Collision_TraceLineBrushFloat(trace, linestart, lineend, brush, brush); + Collision_TraceLineBrushFloat(trace, start, end, brush, brush); break; case BIH_COLLISIONTRIANGLE: if (!mod_q3bsp_curves_collisions.integer) continue; e = model->brush.data_collisionelement3i + 3*leaf->itemindex; texture = model->data_textures + leaf->textureindex; - Collision_TraceLineTriangleFloat(trace, linestart, lineend, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture); + Collision_TraceLineTriangleFloat(trace, start, end, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture); break; case BIH_RENDERTRIANGLE: e = model->surfmesh.data_element3i + 3*leaf->itemindex; texture = model->data_textures + leaf->textureindex; - Collision_TraceLineTriangleFloat(trace, linestart, lineend, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture); + Collision_TraceLineTriangleFloat(trace, start, end, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture); break; } } - return; - } - axis = node->type - BIH_SPLITX; -#if 0 - if (segmentmins[axis] <= node->backmax) - { - if (segmentmaxs[axis] >= node->frontmin) - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); - nodenum = node->back; - } - else if (segmentmaxs[axis] >= node->frontmin) - nodenum = node->front; - else - return; // trace falls between children -#else - frontdist1 = start[axis] - node->frontmin; - frontdist2 = end[axis] - node->frontmin; - backdist1 = start[axis] - node->backmax; - backdist2 = end[axis] - node->backmax; - sideflags = 0; - if (frontdist1 < 0) - sideflags |= 1; - if (frontdist2 < 0) - sideflags |= 2; - if (backdist1 < 0) - sideflags |= 4; - if (backdist2 < 0) - sideflags |= 8; -#if 0 - if (sideflags & 12) - { - if ((sideflags & 3) != 3) - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); - nodenum = node->back; - } - else if ((sideflags & 3) != 3) - nodenum = node->front; - else - return; // trace falls between children -#else - switch(sideflags) - { - case 0: - // start end START END - nodenum = node->front; - continue; - case 1: - // START end START END -#ifdef BIHLINECLIP - frontfrac = frontdist1 / (frontdist1 - frontdist2); - VectorLerp(start, frontfrac, end, newstart); start = newstart; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#endif - nodenum = node->front; - break; - case 2: -#ifdef BIHLINECLIP - // start END START END - frontfrac = frontdist1 / (frontdist1 - frontdist2); - VectorLerp(start, frontfrac, end, newend); end = newend; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#endif - nodenum = node->front; - break; - case 3: - // START END START END - return; // line falls in gap between children - case 4: - // start end start END - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); -#ifdef BIHLINECLIP - backfrac = backdist1 / (backdist1 - backdist2); - VectorLerp(start, backfrac, end, newend); end = newend; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#endif - nodenum = node->back; - break; - case 5: - // START end start END -#ifdef BIHLINECLIP - frontfrac = frontdist1 / (frontdist1 - frontdist2); - VectorLerp(start, frontfrac, end, clipped); - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, clipped, end, linestart, lineend); - backfrac = backdist1 / (backdist1 - backdist2); - VectorLerp(start, backfrac, end, newend); end = newend; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#else - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); -#endif - nodenum = node->back; - break; - case 6: - // start END start END -#ifdef BIHLINECLIP - frontfrac = frontdist1 / (frontdist1 - frontdist2); - VectorLerp(start, frontfrac, end, clipped); - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, clipped, linestart, lineend); - backfrac = backdist1 / (backdist1 - backdist2); - VectorLerp(start, backfrac, end, newend); end = newend; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#else - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); -#endif - nodenum = node->back; - break; - case 7: - // START END start END -#ifdef BIHLINECLIP - backfrac = backdist1 / (backdist1 - backdist2); - VectorLerp(start, backfrac, end, newend); end = newend; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#endif - nodenum = node->back; - break; - case 8: - // start end START end - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); -#ifdef BIHLINECLIP - backfrac = backdist1 / (backdist1 - backdist2); - VectorLerp(start, backfrac, end, newstart); start = newstart; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#endif - nodenum = node->back; - break; - case 9: - // START end START end -#ifdef BIHLINECLIP - frontfrac = frontdist1 / (frontdist1 - frontdist2); - VectorLerp(start, frontfrac, end, clipped); - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, clipped, end, linestart, lineend); - backfrac = backdist1 / (backdist1 - backdist2); - VectorLerp(start, backfrac, end, newstart); start = newstart; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#else - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); -#endif - nodenum = node->back; - break; - case 10: - // start END START end -#ifdef BIHLINECLIP - frontfrac = frontdist1 / (frontdist1 - frontdist2); - VectorLerp(start, frontfrac, end, clipped); - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, clipped, linestart, lineend); - backfrac = backdist1 / (backdist1 - backdist2); - VectorLerp(start, backfrac, end, newstart); start = newstart; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#else - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); -#endif - nodenum = node->back; - break; - case 11: - // START END START end -#ifdef BIHLINECLIP - backfrac = backdist1 / (backdist1 - backdist2); - VectorLerp(start, backfrac, end, newstart); start = newstart; - segmentmins[0] = min(start[0], end[0]); - segmentmins[1] = min(start[1], end[1]); - segmentmins[2] = min(start[2], end[2]); - segmentmaxs[0] = max(start[0], end[0]); - segmentmaxs[1] = max(start[1], end[1]); - segmentmaxs[2] = max(start[2], end[2]); -#endif - nodenum = node->back; - break; - case 12: - // start end start end - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); - nodenum = node->back; - break; - case 13: - // START end start end -#ifdef BIHLINECLIP - frontfrac = frontdist1 / (frontdist1 - frontdist2); - VectorLerp(start, frontfrac, end, clipped); - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, clipped, end, linestart, lineend); -#else - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); -#endif - nodenum = node->back; - break; - case 14: - // start END start end -#ifdef BIHLINECLIP - frontfrac = frontdist1 / (frontdist1 - frontdist2); - VectorLerp(start, frontfrac, end, clipped); - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, clipped, linestart, lineend); -#else - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, bih, node->front, start, end, linestart, lineend); -#endif - nodenum = node->back; - break; - case 15: - // START END start end - nodenum = node->back; - continue; } -#endif -#endif } } -static void Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace_t *trace, dp_model_t *model, int nodenum, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const vec3_t segmentmins, const vec3_t segmentmaxs) +void Mod_CollisionBIH_TraceLine(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask) +{ + if (VectorCompare(start, end)) + { + Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask); + return; + } + Mod_CollisionBIH_TraceLineShared(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask, &model->collision_bih); +} + +void Mod_CollisionBIH_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *thisbrush_start, colbrushf_t *thisbrush_end, int hitsupercontentsmask) { + const bih_t *bih; const bih_leaf_t *leaf; const bih_node_t *node; const colbrushf_t *brush; const int *e; const texture_t *texture; - int axis; - for(;;) + vec3_t start, end, startmins, startmaxs, endmins, endmaxs, mins, maxs; + vec3_t nodebigmins, nodebigmaxs, nodestart, nodeend, sweepnodemins, sweepnodemaxs; + vec_t d1, d2, d3, d4, f, nodestackline[1024][6]; + int axis, nodenum, nodestackpos = 0, nodestack[1024]; + + if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(thisbrush_start->mins, thisbrush_start->maxs) && VectorCompare(thisbrush_end->mins, thisbrush_end->maxs)) { - node = model->collision_bih.nodes + nodenum; - if (node->type == BIH_UNORDERED) - { + if (VectorCompare(thisbrush_start->mins, thisbrush_end->mins)) + Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, thisbrush_start->mins, hitsupercontentsmask); + else + Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, thisbrush_start->mins, thisbrush_end->mins, hitsupercontentsmask); + return; + } + + bih = &model->collision_bih; + if(!bih->nodes) + return; + nodenum = bih->rootnode; + + // box trace, performed as brush trace + memset(trace, 0, sizeof(*trace)); + trace->fraction = 1; + trace->realfraction = 1; + trace->hitsupercontentsmask = hitsupercontentsmask; + + // calculate tracebox-like parameters for efficient culling + VectorMAM(0.5f, thisbrush_start->mins, 0.5f, thisbrush_start->maxs, start); + VectorMAM(0.5f, thisbrush_end->mins, 0.5f, thisbrush_end->maxs, end); + VectorSubtract(thisbrush_start->mins, start, startmins); + VectorSubtract(thisbrush_start->maxs, start, startmaxs); + VectorSubtract(thisbrush_end->mins, end, endmins); + VectorSubtract(thisbrush_end->maxs, end, endmaxs); + mins[0] = min(startmins[0], endmins[0]); + mins[1] = min(startmins[1], endmins[1]); + mins[2] = min(startmins[2], endmins[2]); + maxs[0] = max(startmaxs[0], endmaxs[0]); + maxs[1] = max(startmaxs[1], endmaxs[1]); + maxs[2] = max(startmaxs[2], endmaxs[2]); + + // push first node + nodestackline[nodestackpos][0] = start[0]; + nodestackline[nodestackpos][1] = start[1]; + nodestackline[nodestackpos][2] = start[2]; + nodestackline[nodestackpos][3] = end[0]; + nodestackline[nodestackpos][4] = end[1]; + nodestackline[nodestackpos][5] = end[2]; + nodestack[nodestackpos++] = nodenum; + while (nodestackpos) + { + nodenum = nodestack[--nodestackpos]; + node = bih->nodes + nodenum; + VectorCopy(nodestackline[nodestackpos], nodestart); + VectorCopy(nodestackline[nodestackpos] + 3, nodeend); + sweepnodemins[0] = min(nodestart[0], nodeend[0]) + mins[0]; sweepnodemins[1] = min(nodestart[1], nodeend[1]) + mins[1]; sweepnodemins[2] = min(nodestart[2], nodeend[2]) + mins[2]; sweepnodemaxs[0] = max(nodestart[0], nodeend[0]) + maxs[0]; sweepnodemaxs[1] = max(nodestart[1], nodeend[1]) + maxs[1]; sweepnodemaxs[2] = max(nodestart[2], nodeend[2]) + maxs[2]; + if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, node->mins, node->maxs)) + continue; + if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024) + { + // recurse children of the split + axis = node->type - BIH_SPLITX; + d1 = node->backmax - nodestart[axis] - mins[axis]; + d2 = node->backmax - nodeend[axis] - mins[axis]; + d3 = nodestart[axis] - node->frontmin + maxs[axis]; + d4 = nodeend[axis] - node->frontmin + maxs[axis]; + switch((d1 < 0) | ((d2 < 0) << 1) | ((d3 < 0) << 2) | ((d4 < 0) << 3)) + { + case 0: /* >>>> */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 1: /* <>>> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 2: /* ><>> */ f = d1 / (d1 - d2); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 3: /* <<>> */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 4: /* >><> */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 5: /* <><> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 6: /* ><<> */ f = d1 / (d1 - d2); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 7: /* <<<> */ f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 8: /* >>>< */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 9: /* <>>< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 10: /* ><>< */ f = d1 / (d1 - d2); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; f = d3 / (d3 - d4); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 11: /* <<>< */ f = d3 / (d3 - d4); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break; + case 12: /* >><< */ VectorCopy(nodestart, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; break; + case 13: /* <><< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy( nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; break; + case 14: /* ><<< */ f = d1 / (d1 - d2); VectorCopy(nodestart, nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back; break; + case 15: /* <<<< */ break; + } + } + else if (node->type == BIH_UNORDERED) + { + // calculate sweep bounds for this node + // copy node bounds into local variables and expand to get Minkowski Sum of the two shapes + VectorSubtract(node->mins, maxs, nodebigmins); + VectorSubtract(node->maxs, mins, nodebigmaxs); + // clip line to this node bounds + axis = 0; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } + axis = 1; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } + axis = 2; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } + // some of the line intersected the enlarged node box + // calculate sweep bounds for this node + sweepnodemins[0] = min(nodestart[0], nodeend[0]) + mins[0]; sweepnodemins[1] = min(nodestart[1], nodeend[1]) + mins[1]; sweepnodemins[2] = min(nodestart[2], nodeend[2]) + mins[2]; sweepnodemaxs[0] = max(nodestart[0], nodeend[0]) + maxs[0]; sweepnodemaxs[1] = max(nodestart[1], nodeend[1]) + maxs[1]; sweepnodemaxs[2] = max(nodestart[2], nodeend[2]) + maxs[2]; for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++) { - leaf = model->collision_bih.leafs + node->children[axis]; -#if 1 - if (!BoxesOverlap(segmentmins, segmentmaxs, leaf->mins, leaf->maxs)) + leaf = bih->leafs + node->children[axis]; + if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, leaf->mins, leaf->maxs)) continue; -#endif switch(leaf->type) { case BIH_BRUSH: @@ -6372,126 +6438,32 @@ static void Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace_t *trace, dp_mode break; } } - return; - } - axis = node->type - BIH_SPLITX; -#if 1 - if (!BoxesOverlap(segmentmins, segmentmaxs, node->mins, node->maxs)) - return; -#endif -#if 0 - Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, node->front, thisbrush_start, thisbrush_end, segmentmins, segmentmaxs); - nodenum = node->back; - continue; -#endif - if (segmentmins[axis] <= node->backmax) - { - if (segmentmaxs[axis] >= node->frontmin) - Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, node->front, thisbrush_start, thisbrush_end, segmentmins, segmentmaxs); - nodenum = node->back; } - else if (segmentmaxs[axis] >= node->frontmin) - nodenum = node->front; - else - return; // trace falls between children - } -} - -void Mod_CollisionBIH_TracePoint(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, int hitsupercontentsmask) -{ - memset(trace, 0, sizeof(*trace)); - trace->fraction = 1; - trace->realfraction = 1; - trace->hitsupercontentsmask = hitsupercontentsmask; - Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start); -} - -void Mod_CollisionBIH_TraceLine(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask) -{ - if (VectorCompare(start, end)) - { - Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask); - return; } - - memset(trace, 0, sizeof(*trace)); - trace->fraction = 1; - trace->realfraction = 1; - trace->hitsupercontentsmask = hitsupercontentsmask; - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, &model->collision_bih, model->collision_bih.rootnode, start, end, start, end); } void Mod_CollisionBIH_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask) { - float segmentmins[3], segmentmaxs[3]; colboxbrushf_t thisbrush_start, thisbrush_end; vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs; - if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(boxmins, boxmaxs)) - { - vec3_t shiftstart, shiftend; - VectorAdd(start, boxmins, shiftstart); - VectorAdd(end, boxmins, shiftend); - if (VectorCompare(start, end)) - Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, shiftstart, hitsupercontentsmask); - else - Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, shiftstart, shiftend, hitsupercontentsmask); - return; - } - // box trace, performed as brush trace - memset(trace, 0, sizeof(*trace)); - trace->fraction = 1; - trace->realfraction = 1; - trace->hitsupercontentsmask = hitsupercontentsmask; - segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1; - segmentmins[1] = min(start[1], end[1]) + boxmins[1] - 1; - segmentmins[2] = min(start[2], end[2]) + boxmins[2] - 1; - segmentmaxs[0] = max(start[0], end[0]) + boxmaxs[0] + 1; - segmentmaxs[1] = max(start[1], end[1]) + boxmaxs[1] + 1; - segmentmaxs[2] = max(start[2], end[2]) + boxmaxs[2] + 1; VectorAdd(start, boxmins, boxstartmins); VectorAdd(start, boxmaxs, boxstartmaxs); VectorAdd(end, boxmins, boxendmins); VectorAdd(end, boxmaxs, boxendmaxs); Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL); Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL); - Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, &thisbrush_start.brush, &thisbrush_end.brush, segmentmins, segmentmaxs); + Mod_CollisionBIH_TraceBrush(model, frameblend, skeleton, trace, &thisbrush_start.brush, &thisbrush_end.brush, hitsupercontentsmask); } -void Mod_CollisionBIH_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *start, colbrushf_t *end, int hitsupercontentsmask) + +static int Mod_CollisionBIH_PointSuperContents(struct model_s *model, int frame, const vec3_t point) { - float segmentmins[3], segmentmaxs[3]; - - if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(start->mins, start->maxs) && VectorCompare(end->mins, end->maxs)) - { - if (VectorCompare(start->mins, end->mins)) - Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start->mins, hitsupercontentsmask); - else - Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start->mins, end->mins, hitsupercontentsmask); - return; - } - - // box trace, performed as brush trace - memset(trace, 0, sizeof(*trace)); - trace->fraction = 1; - trace->realfraction = 1; - trace->hitsupercontentsmask = hitsupercontentsmask; - segmentmins[0] = min(start->mins[0], end->mins[0]); - segmentmins[1] = min(start->mins[1], end->mins[1]); - segmentmins[2] = min(start->mins[2], end->mins[2]); - segmentmaxs[0] = max(start->maxs[0], end->maxs[0]); - segmentmaxs[1] = max(start->maxs[1], end->maxs[1]); - segmentmaxs[2] = max(start->maxs[2], end->maxs[2]); - Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, segmentmins, segmentmaxs); -} - -int Mod_CollisionBIH_PointSuperContents(struct model_s *model, int frame, const vec3_t point) -{ - trace_t trace; - Mod_CollisionBIH_TracePoint(model, NULL, NULL, &trace, point, 0); - return trace.startsupercontents; -} + trace_t trace; + Mod_CollisionBIH_TracePoint(model, NULL, NULL, &trace, point, 0); + return trace.startsupercontents; +} void Mod_CollisionBIH_TracePoint_Mesh(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, int hitsupercontentsmask) { @@ -6506,7 +6478,7 @@ void Mod_CollisionBIH_TracePoint_Mesh(dp_model_t *model, const frameblend_t *fra trace->realfraction = 1; trace->hitsupercontentsmask = hitsupercontentsmask; #if 0 - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, start, end); + Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask); hitsupercontents = trace->hitsupercontents; memset(trace, 0, sizeof(*trace)); trace->fraction = 1; @@ -6527,7 +6499,7 @@ int Mod_CollisionBIH_PointSuperContents_Mesh(struct model_s *model, int frame, c trace.fraction = 1; trace.realfraction = 1; trace.hitsupercontentsmask = 0; - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(&trace, model, model->collision_bih.rootnode, start, end, start, end); + Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask); return trace.hitsupercontents; #else return 0; @@ -6758,7 +6730,7 @@ static void Mod_Q3BSP_TracePoint(dp_model_t *model, const frameblend_t *frameble trace->realfraction = 1; trace->hitsupercontentsmask = hitsupercontentsmask; if (mod_collision_bih.integer) - Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start); + Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask); else if (model->brush.submodel) { for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++) @@ -6793,7 +6765,7 @@ static void Mod_Q3BSP_TraceLine(dp_model_t *model, const frameblend_t *frameblen segmentmaxs[1] = max(start[1], end[1]) + 1; segmentmaxs[2] = max(start[2], end[2]) + 1; if (mod_collision_bih.integer) - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, &model->collision_bih, model->collision_bih.rootnode, start, end, start, end); + Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask); else if (model->brush.submodel) { for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++) @@ -6808,61 +6780,7 @@ static void Mod_Q3BSP_TraceLine(dp_model_t *model, const frameblend_t *frameblen Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, model, model->brush.data_nodes, start, end, 0, 1, start, end, ++markframe, segmentmins, segmentmaxs); } -static void Mod_Q3BSP_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask) -{ - int i; - float segmentmins[3], segmentmaxs[3]; - msurface_t *surface; - q3mbrush_t *brush; - colboxbrushf_t thisbrush_start, thisbrush_end; - vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs; - - if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(boxmins, boxmaxs)) - { - vec3_t shiftstart, shiftend; - VectorAdd(start, boxmins, shiftstart); - VectorAdd(end, boxmins, shiftend); - if (VectorCompare(start, end)) - Mod_Q3BSP_TracePoint(model, frameblend, skeleton, trace, shiftstart, hitsupercontentsmask); - else - Mod_Q3BSP_TraceLine(model, frameblend, skeleton, trace, shiftstart, shiftend, hitsupercontentsmask); - return; - } - - // box trace, performed as brush trace - memset(trace, 0, sizeof(*trace)); - trace->fraction = 1; - trace->realfraction = 1; - trace->hitsupercontentsmask = hitsupercontentsmask; - segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1; - segmentmins[1] = min(start[1], end[1]) + boxmins[1] - 1; - segmentmins[2] = min(start[2], end[2]) + boxmins[2] - 1; - segmentmaxs[0] = max(start[0], end[0]) + boxmaxs[0] + 1; - segmentmaxs[1] = max(start[1], end[1]) + boxmaxs[1] + 1; - segmentmaxs[2] = max(start[2], end[2]) + boxmaxs[2] + 1; - VectorAdd(start, boxmins, boxstartmins); - VectorAdd(start, boxmaxs, boxstartmaxs); - VectorAdd(end, boxmins, boxendmins); - VectorAdd(end, boxmaxs, boxendmaxs); - Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL); - Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL); - if (mod_collision_bih.integer) - Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, &thisbrush_start.brush, &thisbrush_end.brush, segmentmins, segmentmaxs); - else if (model->brush.submodel) - { - for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++) - if (brush->colbrushf && BoxesOverlap(segmentmins, segmentmaxs, brush->colbrushf->mins, brush->colbrushf->maxs)) - Collision_TraceBrushBrushFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, brush->colbrushf, brush->colbrushf); - if (mod_q3bsp_curves_collisions.integer) - for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++) - if (surface->num_collisiontriangles && BoxesOverlap(segmentmins, segmentmaxs, surface->mins, surface->maxs)) - Collision_TraceBrushTriangleMeshFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, surface->num_collisiontriangles, surface->deprecatedq3data_collisionelement3i, surface->deprecatedq3data_collisionvertex3f, surface->deprecatedq3num_collisionbboxstride, surface->deprecatedq3data_collisionbbox6f, surface->texture->supercontents, surface->texture->surfaceflags, surface->texture, segmentmins, segmentmaxs); - } - else - Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, model, model->brush.data_nodes, &thisbrush_start.brush, &thisbrush_end.brush, ++markframe, segmentmins, segmentmaxs); -} - -void Mod_Q3BSP_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *start, colbrushf_t *end, int hitsupercontentsmask) +static void Mod_Q3BSP_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *start, colbrushf_t *end, int hitsupercontentsmask) { float segmentmins[3], segmentmaxs[3]; int i; @@ -6890,7 +6808,7 @@ void Mod_Q3BSP_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, con segmentmaxs[1] = max(start->maxs[1], end->maxs[1]); segmentmaxs[2] = max(start->maxs[2], end->maxs[2]); if (mod_collision_bih.integer) - Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, segmentmins, segmentmaxs); + Mod_CollisionBIH_TraceBrush(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask); else if (model->brush.submodel) { for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++) @@ -6905,6 +6823,21 @@ void Mod_Q3BSP_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, con Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, model, model->brush.data_nodes, start, end, ++markframe, segmentmins, segmentmaxs); } +static void Mod_Q3BSP_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask) +{ + colboxbrushf_t thisbrush_start, thisbrush_end; + vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs; + + // box trace, performed as brush trace + VectorAdd(start, boxmins, boxstartmins); + VectorAdd(start, boxmaxs, boxstartmaxs); + VectorAdd(end, boxmins, boxendmins); + VectorAdd(end, boxmaxs, boxendmaxs); + Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL); + Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL); + Mod_Q3BSP_TraceBrush(model, frameblend, skeleton, trace, &thisbrush_start.brush, &thisbrush_end.brush, hitsupercontentsmask); +} + static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const vec3_t point) { int i; @@ -6912,9 +6845,7 @@ static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const q3mbrush_t *brush; if (mod_collision_bih.integer) { - trace_t trace; - Mod_Q3BSP_TracePoint(model, NULL, NULL, &trace, point, 0); - supercontents = trace.startsupercontents; + supercontents = Mod_CollisionBIH_PointSuperContents(model, frame, point); } // test if the point is inside each brush else if (model->brush.submodel) @@ -6945,11 +6876,7 @@ static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const void Mod_CollisionBIH_TraceLineAgainstSurfaces(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask) { - memset(trace, 0, sizeof(*trace)); - trace->fraction = 1; - trace->realfraction = 1; - trace->hitsupercontentsmask = hitsupercontentsmask; - Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, &model->render_bih, model->render_bih.rootnode, start, end, start, end); + Mod_CollisionBIH_TraceLineShared(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask, &model->render_bih); } @@ -7148,7 +7075,7 @@ static int Mod_Q3BSP_NativeContentsFromSuperContents(dp_model_t *model, int supe return nativecontents; } -void Mod_Q3BSP_RecursiveFindNumLeafs(mnode_t *node) +static void Mod_Q3BSP_RecursiveFindNumLeafs(mnode_t *node) { int numleafs; while (node->plane) @@ -7161,7 +7088,7 @@ void Mod_Q3BSP_RecursiveFindNumLeafs(mnode_t *node) loadmodel->brush.num_leafs = numleafs; } -void Mod_Q3BSP_Load(dp_model_t *mod, void *buffer, void *bufferend) +static void Mod_Q3BSP_Load(dp_model_t *mod, void *buffer, void *bufferend) { int i, j, lumps; q3dheader_t *header; @@ -7415,9 +7342,6 @@ void Mod_MAP_Load(dp_model_t *mod, void *buffer, void *bufferend) Host_Error("Mod_MAP_Load: not yet implemented"); } -#define OBJASMODEL - -#ifdef OBJASMODEL typedef struct objvertex_s { int nextindex; @@ -7530,6 +7454,9 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend) VectorClear(mins); VectorClear(maxs); + // we always have model 0, i.e. the first "submodel" + loadmodel->brush.numsubmodels = 1; + // parse the OBJ text now for(;;) { @@ -8005,976 +7932,3 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend) Con_DPrintf("Stats for obj model \"%s\": %i faces, %i nodes, %i leafs, %i clusters, %i clusterportals, mesh: %i vertices, %i triangles, %i surfaces\n", loadmodel->name, loadmodel->num_surfaces, loadmodel->brush.num_nodes, loadmodel->brush.num_leafs, mod->brush.num_pvsclusters, loadmodel->brush.num_portals, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->num_surfaces); } - - - - - - - - - - -#else // OBJASMODEL - -#ifdef OBJWORKS -typedef struct objvertex_s -{ - float v[3]; - float vt[2]; - float vn[3]; -} -objvertex_t; - -typedef struct objtriangle_s -{ - objvertex_t vertex[3]; - int textureindex; - // these fields are used only in conversion to surfaces - int axis; - int surfaceindex; - int surfacevertexindex[3]; - float edgeplane[3][4]; -} -objtriangle_t; - -typedef objnode_s -{ - struct objnode_s *children[2]; - struct objnode_s *parent; - objtriangle_t *triangles; - float normal[3]; - float dist; - float mins[3]; - float maxs[3]; - int numtriangles; -} -objnode_t; - -objnode_t *Mod_OBJ_BSPNodeForTriangles(objnode_t *parent, objtriangle_t *triangles, int numtriangles, const float *mins, const float *maxs, mem_expandablearray_t *nodesarray, int maxclippedtriangles, objtriangle_t *clippedfronttriangles, objtriangle_t *clippedbacktriangles) -{ - int i, j; - float normal[3]; - float dist; - int score; - float bestnormal[3]; - float bestdist; - int bestscore; - float mins[3]; - float maxs[3]; - int numfronttriangles; - int numbacktriangles; - int count_front; - int count_back; - int count_both; - int count_on; - float outfrontpoints[5][3]; - float outbackpoints[5][3]; - int neededfrontpoints; - int neededbackpoints; - int countonpoints; - objnode_t *node; - - node = (objnode_t *)Mem_ExpandableArray_AllocRecord(array); - node->parent = parent; - if (numtriangles) - { - VectorCopy(triangles[0].vertex[0].v, mins); - VectorCopy(triangles[0].vertex[0].v, maxs); - } - else if (parent && parent->children[0] == node) - { - VectorCopy(parent->mins, mins); - Vectorcopy(parent->maxs, maxs); - } - else if (parent && parent->children[1] == node) - { - VectorCopy(parent->mins, mins); - Vectorcopy(parent->maxs, maxs); - } - else - { - VectorClear(mins); - VectorClear(maxs); - } - for (i = 0;i < numtriangles;i++) - { - for (j = 0;j < 3;j++) - { - mins[0] = min(mins[0], triangles[i].vertex[j].v[0]); - mins[1] = min(mins[1], triangles[i].vertex[j].v[1]); - mins[2] = min(mins[2], triangles[i].vertex[j].v[2]); - maxs[0] = max(maxs[0], triangles[i].vertex[j].v[0]); - maxs[1] = max(maxs[1], triangles[i].vertex[j].v[1]); - maxs[2] = max(maxs[2], triangles[i].vertex[j].v[2]); - } - } - VectorCopy(mins, node->mins); - VectorCopy(maxs, node->maxs); - if (numtriangles <= mod_obj_leaftriangles.integer) - { - // create a leaf - loadmodel->brush.num_leafs++; - node->triangles = triangles; - node->numtriangles = numtriangles; - return node; - } - - // create a node - loadmodel->brush.num_nodes++; - // pick a splitting plane from the various choices available to us... - // early splits simply halve the interval - bestscore = 0; - VectorClear(bestnormal); - bestdist = 0; - if (numtriangles <= mod_obj_splitterlimit.integer) - limit = numtriangles; - else - limit = 0; - for (i = -3;i < limit;i++) - { - if (i < 0) - { - // first we try 3 axial splits (kdtree-like) - j = i + 3; - VectorClear(normal); - normal[j] = 1; - dist = (mins[j] + maxs[j]) * 0.5f; - } - else - { - // then we try each triangle plane - TriangleNormal(triangles[i].vertex[0].v, triangles[i].vertex[1].v, triangles[i].vertex[2].v, normal); - VectorNormalize(normal); - dist = DotProduct(normal, triangles[i].vertex[0].v); - // use positive axial values whenever possible - if (normal[0] == -1) - normal[0] = 1; - if (normal[1] == -1) - normal[1] = 1; - if (normal[2] == -1) - normal[2] = 1; - // skip planes that match the current best - if (VectorCompare(normal, bestnormal) && dist == bestdist) - continue; - } - count_on = 0; - count_front = 0; - count_back = 0; - count_both = 0; - for (j = 0;j < numtriangles;j++) - { - dists[0] = DotProduct(normal, triangles[j].vertex[0].v) - dist; - dists[1] = DotProduct(normal, triangles[j].vertex[1].v) - dist; - dists[2] = DotProduct(normal, triangles[j].vertex[2].v) - dist; - if (dists[0] < -DIST_EPSILON || dists[1] < -DIST_EPSILON || dists[2] < -DIST_EPSILON) - { - if (dists[0] > DIST_EPSILON || dists[1] > DIST_EPSILON || dists[2] > DIST_EPSILON) - count_both++; - else - count_back++; - } - else if (dists[0] > DIST_EPSILON || dists[1] > DIST_EPSILON || dists[2] > DIST_EPSILON) - count_front++; - else - count_on++; - } - // score is supposed to: - // prefer axial splits - // prefer evenly dividing the input triangles - // prefer triangles on the plane - // avoid triangles crossing the plane - score = count_on*count_on - count_both*count_both + min(count_front, count_back)*(count_front+count_back); - if (normal[0] == 1 || normal[1] == 1 || normal[2] == 1) - score *= 2; - if (i == -3 || bestscore < score) - { - VectorCopy(normal, bestnormal); - bestdist = dist; - bestscore = score; - } - } - - // now we have chosen an optimal split plane... - - // divide triangles by the splitting plane - numfronttriangles = 0; - numbacktriangles = 0; - for (i = 0;i < numtriangles;i++) - { - neededfrontpoints = 0; - neededbackpoints = 0; - countonpoints = 0; - PolygonF_Divide(3, triangles[i].vertex[0].v, bestnormal[0], bestnormal[1], bestnormal[2], bestdist, DIST_EPSILON, 5, outfrontpoints[0], &neededfrontpoints, 5, outbackpoints[0], &neededbackpoints, &countonpoints); - if (countonpoints > 1) - { - // triangle lies on plane, assign it to one child only - TriangleNormal(triangles[i].vertex[0].v, triangles[i].vertex[1].v, triangles[i].vertex[2].v, normal); - if (DotProduct(bestnormal, normal) >= 0) - { - // assign to front side child - obj_fronttriangles[numfronttriangles++] = triangles[i]; - } - else - { - // assign to back side child - obj_backtriangles[numbacktriangles++] = triangles[i]; - } - } - else - { - // convert clipped polygons to triangles - for (j = 0;j < neededfrontpoints-2;j++) - { - obj_fronttriangles[numfronttriangles] = triangles[i]; - VectorCopy(outfrontpoints[0], obj_fronttriangles[numfronttriangles].vertex[0].v); - VectorCopy(outfrontpoints[j+1], obj_fronttriangles[numfronttriangles].vertex[1].v); - VectorCopy(outfrontpoints[j+2], obj_fronttriangles[numfronttriangles].vertex[2].v); - numfronttriangles++; - } - for (j = 0;j < neededbackpoints-2;j++) - { - obj_backtriangles[numbacktriangles] = triangles[i]; - VectorCopy(outbackpoints[0], obj_backtriangles[numbacktriangles].vertex[0].v); - VectorCopy(outbackpoints[j+1], obj_backtriangles[numbacktriangles].vertex[1].v); - VectorCopy(outbackpoints[j+2], obj_backtriangles[numbacktriangles].vertex[2].v); - numbacktriangles++; - } - } - } - - // now copy the triangles out of the big buffer - if (numfronttriangles) - { - fronttriangles = Mem_Alloc(loadmodel->mempool, fronttriangles * sizeof(*fronttriangles)); - memcpy(fronttriangles, obj_fronttriangles, numfronttriangles * sizeof(*fronttriangles)); - } - else - fronttriangles = NULL; - if (numbacktriangles) - { - backtriangles = Mem_Alloc(loadmodel->mempool, backtriangles * sizeof(*backtriangles)); - memcpy(backtriangles, obj_backtriangles, numbacktriangles * sizeof(*backtriangles)); - } - else - backtriangles = NULL; - - // free the original triangles we were given - if (triangles) - Mem_Free(triangles); - triangles = NULL; - numtriangles = 0; - - // now create the children... - node->children[0] = Mod_OBJ_BSPNodeForTriangles(node, fronttriangles, numfronttriangles, frontmins, frontmaxs, nodesarray, maxclippedtriangles, clippedfronttriangles, clippedbacktriangles); - node->children[1] = Mod_OBJ_BSPNodeForTriangles(node, backtriangles, numbacktriangles, backmins, backmaxs, nodesarray, maxclippedtriangles, clippedfronttriangles, clippedbacktriangles); - return node; -} - -void Mod_OBJ_SnapVertex(float *v) -{ - int i; - float a = mod_obj_vertexprecision.value; - float b = 1.0f / a; - v[0] -= floor(v[0] * a + 0.5f) * b; - v[1] -= floor(v[1] * a + 0.5f) * b; - v[2] -= floor(v[2] * a + 0.5f) * b; -} - -void Mod_OBJ_ConvertBSPNode(objnode_t *objnode, mnode_t *mnodeparent) -{ - if (objnode->children[0]) - { - // convert to mnode_t - mnode_t *mnode = loadmodel->brush.data_nodes + loadmodel->brush.num_nodes++; - mnode->parent = mnodeparent; - mnode->plane = loadmodel->brush.data_planes + loadmodel->brush.num_planes++; - VectorCopy(objnode->normal, mnode->plane->normal); - mnode->plane->dist = objnode->dist; - PlaneClassify(mnode->plane); - VectorCopy(objnode->mins, mnode->mins); - VectorCopy(objnode->maxs, mnode->maxs); - // push combinedsupercontents up to the parent - if (mnodeparent) - mnodeparent->combinedsupercontents |= mnode->combinedsupercontents; - mnode->children[0] = Mod_OBJ_ConvertBSPNode(objnode->children[0], mnode); - mnode->children[1] = Mod_OBJ_ConvertBSPNode(objnode->children[1], mnode); - } - else - { - // convert to mleaf_t - mleaf_t *mleaf = loadmodel->brush.data_leafs + loadmodel->brush.num_leafs++; - mleaf->parent = mnodeparent; - VectorCopy(objnode->mins, mleaf->mins); - VectorCopy(objnode->maxs, mleaf->maxs); - mleaf->clusterindex = loadmodel->brush.num_leafs - 1; - if (objnode->numtriangles) - { - objtriangle_t *triangles = objnode->triangles; - int numtriangles = objnode->numtriangles; - texture_t *texture; - float edge[3][3]; - float normal[3]; - objvertex_t vertex[3]; - numsurfaces = 0; - maxsurfaces = numtriangles; - surfaces = NULL; - // calculate some more data on each triangle for surface gathering - for (i = 0;i < numtriangles;i++) - { - triangle = triangles + i; - texture = loadmodel->data_textures + triangle->textureindex; - Mod_OBJ_SnapVertex(triangle->vertex[0].v); - Mod_OBJ_SnapVertex(triangle->vertex[1].v); - Mod_OBJ_SnapVertex(triangle->vertex[2].v); - TriangleNormal(triangle->vertex[0].v, triangle->vertex[1].v, triangle->vertex[2].v, normal); - axis = 0; - if (fabs(normal[axis]) < fabs(normal[1])) - axis = 1; - if (fabs(normal[axis]) < fabs(normal[2])) - axis = 2; - VectorClear(normal); - normal[axis] = 1; - triangle->axis = axis; - VectorSubtract(triangle->vertex[1].v, triangle->vertex[0].v, edge[0]); - VectorSubtract(triangle->vertex[2].v, triangle->vertex[1].v, edge[1]); - VectorSubtract(triangle->vertex[0].v, triangle->vertex[2].v, edge[2]); - CrossProduct(edge[0], normal, triangle->edgeplane[0]); - CrossProduct(edge[1], normal, triangle->edgeplane[1]); - CrossProduct(edge[2], normal, triangle->edgeplane[2]); - VectorNormalize(triangle->edgeplane[0]); - VectorNormalize(triangle->edgeplane[1]); - VectorNormalize(triangle->edgeplane[2]); - triangle->edgeplane[0][3] = DotProduct(triangle->edgeplane[0], triangle->vertex[0].v); - triangle->edgeplane[1][3] = DotProduct(triangle->edgeplane[1], triangle->vertex[1].v); - triangle->edgeplane[2][3] = DotProduct(triangle->edgeplane[2], triangle->vertex[2].v); - triangle->surfaceindex = 0; - // add to the combined supercontents while we're here... - mleaf->combinedsupercontents |= texture->supercontents; - } - surfaceindex = 1; - for (i = 0;i < numtriangles;i++) - { - // skip already-assigned triangles - if (triangles[i].surfaceindex) - continue; - texture = loadmodel->data_textures + triangles[i].textureindex; - // assign a new surface to this triangle - triangles[i].surfaceindex = surfaceindex++; - axis = triangles[i].axis; - numvertices = 3; - // find the triangle's neighbors, this can take multiple passes - retry = true; - while (retry) - { - retry = false; - for (j = i+1;j < numtriangles;j++) - { - if (triangles[j].surfaceindex || triangles[j].axis != axis || triangles[j].texture != texture) - continue; - triangle = triangles + j; - for (k = i;k < j;k++) - { - if (triangles[k].surfaceindex != surfaceindex) - continue; - if (VectorCompare(triangles[k].vertex[0].v, triangles[j].vertex[0].v) - || VectorCompare(triangles[k].vertex[0].v, triangles[j].vertex[1].v) - || VectorCompare(triangles[k].vertex[0].v, triangles[j].vertex[2].v) - || VectorCompare(triangles[k].vertex[1].v, triangles[j].vertex[0].v) - || VectorCompare(triangles[k].vertex[1].v, triangles[j].vertex[1].v) - || VectorCompare(triangles[k].vertex[1].v, triangles[j].vertex[2].v) - || VectorCompare(triangles[k].vertex[2].v, triangles[j].vertex[0].v) - || VectorCompare(triangles[k].vertex[2].v, triangles[j].vertex[1].v) - || VectorCompare(triangles[k].vertex[2].v, triangles[j].vertex[2].v)) - { - // shares a vertex position - --- FIXME --- - } - } - for (k = 0;k < numvertices;k++) - if (!VectorCompare(vertex[k].v, triangles[j].vertex[0].v) || !VectorCompare(vertex[k].v, triangles[j].vertex[1].v) || !VectorCompare(vertex[k].v, triangles[j].vertex[2].v)) - break; - if (k == numvertices) - break; // not a neighbor - // this triangle is a neighbor and has the same axis and texture - // check now if it overlaps in lightmap projection space - triangles[j].surfaceindex; - if (triangles[j]. - } - } - //triangles[i].surfaceindex = surfaceindex++; - for (surfaceindex = 0;surfaceindex < numsurfaces;surfaceindex++) - { - if (surfaces[surfaceindex].texture != texture) - continue; - // check if any triangles already in this surface overlap in lightmap projection space - - { - } - break; - } - } - // let the collision code simply use the surfaces - mleaf->containscollisionsurfaces = mleaf->combinedsupercontents != 0; - mleaf->numleafsurfaces = ?; - mleaf->firstleafsurface = ?; - } - // push combinedsupercontents up to the parent - if (mnodeparent) - mnodeparent->combinedsupercontents |= mleaf->combinedsupercontents; - } -} -#endif - -void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend) -{ -#ifdef OBJWORKS - const char *textbase = (char *)buffer, *text = textbase; - char *s; - char *argv[512]; - char line[1024]; - char materialname[MAX_QPATH]; - int j, index1, index2, index3, first, prev, index; - int argc; - int linelen; - int numtriangles = 0; - int maxtriangles = 131072; - objtriangle_t *triangles = Mem_Alloc(tempmempool, maxtriangles * sizeof(*triangles)); - int linenumber = 0; - int maxtextures = 256, numtextures = 0, textureindex = 0; - int maxv = 1024, numv = 0; - int maxvt = 1024, numvt = 0; - int maxvn = 1024, numvn = 0; - char **texturenames; - float *v = Mem_Alloc(tempmempool, maxv * sizeof(float[3])); - float *vt = Mem_Alloc(tempmempool, maxvt * sizeof(float[2])); - float *vn = Mem_Alloc(tempmempool, maxvn * sizeof(float[3])); - objvertex_t vfirst, vprev, vcurrent; - float mins[3]; - float maxs[3]; -#if 0 - int hashindex; - int maxverthash = 65536, numverthash = 0; - int numhashindex = 65536; - struct objverthash_s - { - struct objverthash_s *next; - int s; - int v; - int vt; - int vn; - } - *hash, **verthash = Mem_Alloc(tempmempool, numhashindex * sizeof(*verthash)), *verthashdata = Mem_Alloc(tempmempool, maxverthash * sizeof(*verthashdata)), *oldverthashdata; -#endif - - dpsnprintf(materialname, sizeof(materialname), "%s", loadmodel->name); - - loadmodel->modeldatatypestring = "OBJ"; - - loadmodel->type = mod_obj; - loadmodel->soundfromcenter = true; - loadmodel->TraceBox = Mod_OBJ_TraceBox; - loadmodel->TraceLine = Mod_OBJ_TraceLine; - loadmodel->TracePoint = Mod_OBJ_TracePoint; - loadmodel->PointSuperContents = Mod_OBJ_PointSuperContents; - loadmodel->TraceLineAgainstSurfaces = Mod_OBJ_TraceLineAgainstSurfaces; - loadmodel->brush.TraceLineOfSight = Mod_OBJ_TraceLineOfSight; - loadmodel->brush.SuperContentsFromNativeContents = Mod_OBJ_SuperContentsFromNativeContents; - loadmodel->brush.NativeContentsFromSuperContents = Mod_OBJ_NativeContentsFromSuperContents; - loadmodel->brush.GetPVS = Mod_OBJ_GetPVS; - loadmodel->brush.FatPVS = Mod_OBJ_FatPVS; - loadmodel->brush.BoxTouchingPVS = Mod_OBJ_BoxTouchingPVS; - loadmodel->brush.BoxTouchingLeafPVS = Mod_OBJ_BoxTouchingLeafPVS; - loadmodel->brush.BoxTouchingVisibleLeafs = Mod_OBJ_BoxTouchingVisibleLeafs; - loadmodel->brush.FindBoxClusters = Mod_OBJ_FindBoxClusters; - loadmodel->brush.LightPoint = Mod_OBJ_LightPoint; - loadmodel->brush.FindNonSolidLocation = Mod_OBJ_FindNonSolidLocation; - loadmodel->brush.AmbientSoundLevelsForPoint = NULL; - loadmodel->brush.RoundUpToHullSize = NULL; - loadmodel->brush.PointInLeaf = Mod_OBJ_PointInLeaf; - loadmodel->Draw = R_Q1BSP_Draw; - loadmodel->DrawDepth = R_Q1BSP_DrawDepth; - loadmodel->DrawDebug = R_Q1BSP_DrawDebug; - loadmodel->DrawPrepass = R_Q1BSP_DrawPrepass; - loadmodel->GetLightInfo = R_Q1BSP_GetLightInfo; - loadmodel->CompileShadowMap = R_Q1BSP_CompileShadowMap; - loadmodel->DrawShadowMap = R_Q1BSP_DrawShadowMap; - loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume; - loadmodel->DrawShadowVolume = R_Q1BSP_DrawShadowVolume; - loadmodel->DrawLight = R_Q1BSP_DrawLight; - - VectorClear(mins); - VectorClear(maxs); - - // parse the OBJ text now - for(;;) - { - if (!*text) - break; - linenumber++; - linelen = 0; - for (linelen = 0;text[linelen] && text[linelen] != '\r' && text[linelen] != '\n';linelen++) - line[linelen] = text[linelen]; - line[linelen] = 0; - for (argc = 0;argc < (int)(sizeof(argv)/sizeof(argv[0]));argc++) - argv[argc] = ""; - argc = 0; - s = line; - while (*s == ' ' || *s == '\t') - s++; - while (*s) - { - argv[argc++] = s; - while (*s > ' ') - s++; - if (!*s) - break; - *s++ = 0; - while (*s == ' ' || *s == '\t') - s++; - } - if (!argc) - continue; - if (argv[0][0] == '#') - continue; - if (!strcmp(argv[0], "v")) - { - if (maxv <= numv) - { - float *oldv = v; - maxv *= 2; - v = Mem_Alloc(tempmempool, maxv * sizeof(float[3])); - if (oldv) - { - memcpy(v, oldv, numv * sizeof(float[3])); - Mem_Free(oldv); - } - } - v[numv*3+0] = atof(argv[1]); - v[numv*3+1] = atof(argv[2]); - v[numv*3+2] = atof(argv[3]); - numv++; - } - else if (!strcmp(argv[0], "vt")) - { - if (maxvt <= numvt) - { - float *oldvt = vt; - maxvt *= 2; - vt = Mem_Alloc(tempmempool, maxvt * sizeof(float[2])); - if (oldvt) - { - memcpy(vt, oldvt, numvt * sizeof(float[2])); - Mem_Free(oldvt); - } - } - vt[numvt*2+0] = atof(argv[1]); - vt[numvt*2+1] = atof(argv[2]); - numvt++; - } - else if (!strcmp(argv[0], "vn")) - { - if (maxvn <= numvn) - { - float *oldvn = vn; - maxvn *= 2; - vn = Mem_Alloc(tempmempool, maxvn * sizeof(float[3])); - if (oldvn) - { - memcpy(vn, oldvn, numvn * sizeof(float[3])); - Mem_Free(oldvn); - } - } - vn[numvn*3+0] = atof(argv[1]); - vn[numvn*3+1] = atof(argv[2]); - vn[numvn*3+2] = atof(argv[3]); - numvn++; - } - else if (!strcmp(argv[0], "f")) - { - for (j = 1;j < argc;j++) - { - index1 = atoi(argv[j]); - while(argv[j][0] && argv[j][0] != '/') - argv[j]++; - if (argv[j][0]) - argv[j]++; - index2 = atoi(argv[j]); - while(argv[j][0] && argv[j][0] != '/') - argv[j]++; - if (argv[j][0]) - argv[j]++; - index3 = atoi(argv[j]); - // negative refers to a recent vertex - // zero means not specified - // positive means an absolute vertex index - if (index1 < 0) - index1 = numv - index1; - if (index2 < 0) - index2 = numvt - index2; - if (index3 < 0) - index3 = numvn - index3; - VectorCopy(v + 3*index1, vcurrent.v); - Vector2Copy(vt + 2*index2, vcurrent.vt); - VectorCopy(vn + 3*index3, vcurrent.vn); - if (numtriangles == 0) - { - VectorCopy(vcurrent.v, mins); - VectorCopy(vcurrent.v, maxs); - } - else - { - mins[0] = min(mins[0], vcurrent.v[0]); - mins[1] = min(mins[1], vcurrent.v[1]); - mins[2] = min(mins[2], vcurrent.v[2]); - maxs[0] = max(maxs[0], vcurrent.v[0]); - maxs[1] = max(maxs[1], vcurrent.v[1]); - maxs[2] = max(maxs[2], vcurrent.v[2]); - } - if (j == 1) - vfirst = vcurrent; - else if (j >= 3) - { - if (maxtriangles <= numtriangles) - { - objtriangle_t *oldtriangles = triangles; - maxtriangles *= 2; - triangles = Mem_Alloc(tempmempool, maxtriangles * sizeof(*triangles)); - if (oldtriangles) - { - memcpy(triangles, oldtriangles, maxtriangles * sizeof(*triangles)); - Mem_Free(oldtriangles); - } - } - triangles[numtriangles].textureindex = textureindex; - triangles[numtriangles].vertex[0] = vfirst; - triangles[numtriangles].vertex[1] = vprev; - triangles[numtriangles].vertex[2] = vcurrent; - numtriangles++; - } - vprev = vcurrent; - prev = index; - } - } - else if (!strcmp(argv[0], "o") || !strcmp(argv[0], "g")) - ; - else if (!!strcmp(argv[0], "usemtl")) - { - for (i = 0;i < numtextures;i++) - if (!strcmp(texturenames[numtextures], argv[1])) - break; - if (i < numtextures) - texture = textures + i; - else - { - if (maxtextures <= numtextures) - { - texture_t *oldtextures = textures; - maxtextures *= 2; - textures = Mem_Alloc(tempmempool, maxtextures * sizeof(*textures)); - if (oldtextures) - { - memcpy(textures, oldtextures, numtextures * sizeof(*textures)); - Mem_Free(oldtextures); - } - } - textureindex = numtextures++; - texturenames[textureindex] = Mem_Alloc(tempmempool, strlen(argv[1]) + 1); - memcpy(texturenames[textureindex], argv[1], strlen(argv[1]) + 1); - } - } - text += linelen; - if (*text == '\r') - text++; - if (*text == '\n') - text++; - } - - // now that we have the OBJ data loaded as-is, we can convert it - - // load the textures - loadmodel->num_textures = numtextures; - loadmodel->data_textures = Mem_Alloc(loadmodel->mempool, loadmodel->num_textures * sizeof(texture_t)); - for (i = 0;i < numtextures;i++) - Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, texturenames[i], true, true, TEXF_MIPMAP | TEXF_ALPHA | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS); - - // free the texturenames array since we are now done with it - for (i = 0;i < numtextures;i++) - { - Mem_Free(texturenames[i]); - texturenames[i] = NULL; - } - Mem_Free(texturenames); - texturenames = NULL; - - // copy the model bounds, then enlarge the yaw and rotated bounds according to radius - VectorCopy(mins, loadmodel->normalmins); - VectorCopy(maxs, loadmodel->normalmaxs); - dist = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0])); - modelyawradius = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1])); - modelyawradius = dist*dist+modelyawradius*modelyawradius; - modelradius = max(fabs(loadmodel->normalmins[2]), fabs(loadmodel->normalmaxs[2])); - modelradius = modelyawradius + modelradius * modelradius; - modelyawradius = sqrt(modelyawradius); - modelradius = sqrt(modelradius); - loadmodel->yawmins[0] = loadmodel->yawmins[1] = -modelyawradius; - loadmodel->yawmins[2] = loadmodel->normalmins[2]; - loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius; - loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2]; - loadmodel->rotatedmins[0] = loadmodel->rotatedmins[1] = loadmodel->rotatedmins[2] = -modelradius; - loadmodel->rotatedmaxs[0] = loadmodel->rotatedmaxs[1] = loadmodel->rotatedmaxs[2] = modelradius; - loadmodel->radius = modelradius; - loadmodel->radius2 = modelradius * modelradius; - - // make sure the temp triangle buffer is big enough for BSP building - maxclippedtriangles = numtriangles*4; - if (numtriangles > 0) - { - clippedfronttriangles = Mem_Alloc(loadmodel->mempool, maxclippedtriangles * 2 * sizeof(objtriangle_t)); - clippedbacktriangles = clippedfronttriangles + maxclippedtriangles; - } - - // generate a rough BSP tree from triangle data, we don't have to be too careful here, it only has to define the basic areas of the map - loadmodel->brush.num_leafs = 0; - loadmodel->brush.num_nodes = 0; - Mem_ExpandableArray_NewArray(&nodesarray, loadmodel->mempool, sizeof(objnode_t), 1024); - rootnode = Mod_OBJ_BSPNodeForTriangles(triangles, numtriangles, mins, maxs, &nodesarray, maxclippedtriangles, clippedfronttriangles, clippedbacktriangles); - - // convert the BSP tree to mnode_t and mleaf_t structures and convert the triangles to msurface_t... - loadmodel->brush.data_leafs = Mem_Alloc(loadmodel->mempool, loadmodel->brush.num_leafs * sizeof(mleaf_t)); - loadmodel->brush.data_nodes = Mem_Alloc(loadmodel->mempool, loadmodel->brush.num_nodes * sizeof(mnode_t)); - loadmodel->brush.data_planes = Mem_Alloc(loadmodel->mempool, loadmodel->brush.num_nodes * sizeof(mplane_t)); - loadmodel->brush.num_leafs = 0; - loadmodel->brush.num_nodes = 0; - loadmodel->brush.num_planes = 0; - Mod_OBJ_ConvertAndFreeBSPNode(rootnode); - - if (clippedfronttriangles) - Mem_Free(clippedfronttriangles); - maxclippedtriangles = 0; - clippedfronttriangles = NULL; - clippedbacktriangles = NULL; - ---- NOTHING DONE PAST THIS POINT --- - - loadmodel->numskins = LittleLong(pinmodel->num_skins); - numxyz = LittleLong(pinmodel->num_xyz); - numst = LittleLong(pinmodel->num_st); - loadmodel->surfmesh.num_triangles = LittleLong(pinmodel->num_tris); - loadmodel->numframes = LittleLong(pinmodel->num_frames); - loadmodel->surfmesh.num_morphframes = loadmodel->numframes; - loadmodel->num_poses = loadmodel->surfmesh.num_morphframes; - skinwidth = LittleLong(pinmodel->skinwidth); - skinheight = LittleLong(pinmodel->skinheight); - iskinwidth = 1.0f / skinwidth; - iskinheight = 1.0f / skinheight; - - loadmodel->num_surfaces = 1; - loadmodel->nummodelsurfaces = loadmodel->num_surfaces; - data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->numframes * sizeof(animscene_t) + loadmodel->numframes * sizeof(float[6]) + loadmodel->surfmesh.num_triangles * sizeof(int[3]) + (r_enableshadowvolume.integer ? loadmodel->surfmesh.num_triangles * sizeof(int[3]) : 0)); - loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t); - loadmodel->sortedmodelsurfaces = (int *)data;data += loadmodel->num_surfaces * sizeof(int); - loadmodel->sortedmodelsurfaces[0] = 0; - loadmodel->animscenes = (animscene_t *)data;data += loadmodel->numframes * sizeof(animscene_t); - loadmodel->surfmesh.data_morphmd2framesize6f = (float *)data;data += loadmodel->numframes * sizeof(float[6]); - loadmodel->surfmesh.data_element3i = (int *)data;data += loadmodel->surfmesh.num_triangles * sizeof(int[3]); - if (r_enableshadowvolumes.integer) - loadmodel->surfmesh.data_neighbor3i = (int *)data;data += loadmodel->surfmesh.num_triangles * sizeof(int[3]); - - loadmodel->synctype = ST_RAND; - - // load the skins - inskin = (char *)(base + LittleLong(pinmodel->ofs_skins)); - skinfiles = Mod_LoadSkinFiles(); - if (skinfiles) - { - loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; - loadmodel->num_texturesperskin = loadmodel->num_surfaces; - loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t)); - Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures, skinfiles, "default", ""); - Mod_FreeSkinFiles(skinfiles); - } - else if (loadmodel->numskins) - { - // skins found (most likely not a player model) - loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; - loadmodel->num_texturesperskin = loadmodel->num_surfaces; - loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t)); - for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME) - Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i * loadmodel->num_surfaces, inskin, true, true, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP | TEXF_COMPRESS); - } - else - { - // no skins (most likely a player model) - loadmodel->numskins = 1; - loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; - loadmodel->num_texturesperskin = loadmodel->num_surfaces; - loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t)); - Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures, NULL); - } - - loadmodel->skinscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins); - for (i = 0;i < loadmodel->numskins;i++) - { - loadmodel->skinscenes[i].firstframe = i; - loadmodel->skinscenes[i].framecount = 1; - loadmodel->skinscenes[i].loop = true; - loadmodel->skinscenes[i].framerate = 10; - } - - // load the triangles and stvert data - inst = (unsigned short *)(base + LittleLong(pinmodel->ofs_st)); - intri = (md2triangle_t *)(base + LittleLong(pinmodel->ofs_tris)); - md2verthash = (struct md2verthash_s **)Mem_Alloc(tempmempool, 65536 * sizeof(hash)); - md2verthashdata = (struct md2verthash_s *)Mem_Alloc(tempmempool, loadmodel->surfmesh.num_triangles * 3 * sizeof(*hash)); - // swap the triangle list - loadmodel->surfmesh.num_vertices = 0; - for (i = 0;i < loadmodel->surfmesh.num_triangles;i++) - { - for (j = 0;j < 3;j++) - { - xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]); - st = (unsigned short) LittleShort (intri[i].index_st[j]); - if (xyz >= numxyz) - { - Con_Printf("%s has an invalid xyz index (%i) on triangle %i, resetting to 0\n", loadmodel->name, xyz, i); - xyz = 0; - } - if (st >= numst) - { - Con_Printf("%s has an invalid st index (%i) on triangle %i, resetting to 0\n", loadmodel->name, st, i); - st = 0; - } - hashindex = (xyz * 256 + st) & 65535; - for (hash = md2verthash[hashindex];hash;hash = hash->next) - if (hash->xyz == xyz && hash->st == st) - break; - if (hash == NULL) - { - hash = md2verthashdata + loadmodel->surfmesh.num_vertices++; - hash->xyz = xyz; - hash->st = st; - hash->next = md2verthash[hashindex]; - md2verthash[hashindex] = hash; - } - loadmodel->surfmesh.data_element3i[i*3+j] = (hash - md2verthashdata); - } - } - - vertremap = (int *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_vertices * sizeof(int)); - data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_vertices * sizeof(float[2]) + loadmodel->surfmesh.num_vertices * loadmodel->surfmesh.num_morphframes * sizeof(trivertx_t)); - loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += loadmodel->surfmesh.num_vertices * sizeof(float[2]); - loadmodel->surfmesh.data_morphmdlvertex = (trivertx_t *)data;data += loadmodel->surfmesh.num_vertices * loadmodel->surfmesh.num_morphframes * sizeof(trivertx_t); - for (i = 0;i < loadmodel->surfmesh.num_vertices;i++) - { - int sts, stt; - hash = md2verthashdata + i; - vertremap[i] = hash->xyz; - sts = LittleShort(inst[hash->st*2+0]); - stt = LittleShort(inst[hash->st*2+1]); - if (sts < 0 || sts >= skinwidth || stt < 0 || stt >= skinheight) - { - Con_Printf("%s has an invalid skin coordinate (%i %i) on vert %i, changing to 0 0\n", loadmodel->name, sts, stt, i); - sts = 0; - stt = 0; - } - loadmodel->surfmesh.data_texcoordtexture2f[i*2+0] = sts * iskinwidth; - loadmodel->surfmesh.data_texcoordtexture2f[i*2+1] = stt * iskinheight; - } - - Mem_Free(md2verthash); - Mem_Free(md2verthashdata); - - // generate ushort elements array if possible - if (loadmodel->surfmesh.num_vertices <= 65536) - loadmodel->surfmesh.data_element3s = (unsigned short *)Mem_Alloc(loadmodel->mempool, sizeof(unsigned short[3]) * loadmodel->surfmesh.num_triangles); - - // load the frames - datapointer = (base + LittleLong(pinmodel->ofs_frames)); - for (i = 0;i < loadmodel->surfmesh.num_morphframes;i++) - { - int k; - trivertx_t *v; - trivertx_t *out; - pinframe = (md2frame_t *)datapointer; - datapointer += sizeof(md2frame_t); - // store the frame scale/translate into the appropriate array - for (j = 0;j < 3;j++) - { - loadmodel->surfmesh.data_morphmd2framesize6f[i*6+j] = LittleFloat(pinframe->scale[j]); - loadmodel->surfmesh.data_morphmd2framesize6f[i*6+3+j] = LittleFloat(pinframe->translate[j]); - } - // convert the vertices - v = (trivertx_t *)datapointer; - out = loadmodel->surfmesh.data_morphmdlvertex + i * loadmodel->surfmesh.num_vertices; - for (k = 0;k < loadmodel->surfmesh.num_vertices;k++) - out[k] = v[vertremap[k]]; - datapointer += numxyz * sizeof(trivertx_t); - - strlcpy(loadmodel->animscenes[i].name, pinframe->name, sizeof(loadmodel->animscenes[i].name)); - loadmodel->animscenes[i].firstframe = i; - loadmodel->animscenes[i].framecount = 1; - loadmodel->animscenes[i].framerate = 10; - loadmodel->animscenes[i].loop = true; - } - - Mem_Free(vertremap); - - Mod_MakeSortedSurfaces(loadmodel); - if (loadmodel->surfmesh.data_neighbor3i) - Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles); - Mod_Alias_CalculateBoundingBox(); - Mod_Alias_MorphMesh_CompileFrames(); - - surface = loadmodel->data_surfaces; - surface->texture = loadmodel->data_textures; - surface->num_firsttriangle = 0; - surface->num_triangles = loadmodel->surfmesh.num_triangles; - surface->num_firstvertex = 0; - surface->num_vertices = loadmodel->surfmesh.num_vertices; - - loadmodel->surfmesh.isanimated = false; - - if (loadmodel->surfmesh.data_element3s) - for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++) - loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i]; -#endif -} -#endif // !OBJASMODEL - -qboolean Mod_CanSeeBox_Trace(int numsamples, float t, dp_model_t *model, vec3_t eye, vec3_t minsX, vec3_t maxsX) -{ - // we already have done PVS culling at this point... - // so we don't need to do it again. - - int i; - vec3_t testorigin, mins, maxs; - - testorigin[0] = (minsX[0] + maxsX[0]) * 0.5; - testorigin[1] = (minsX[1] + maxsX[1]) * 0.5; - testorigin[2] = (minsX[2] + maxsX[2]) * 0.5; - - if(model->brush.TraceLineOfSight(model, eye, testorigin)) - return 1; - - // expand the box a little - mins[0] = (t+1) * minsX[0] - t * maxsX[0]; - maxs[0] = (t+1) * maxsX[0] - t * minsX[0]; - mins[1] = (t+1) * minsX[1] - t * maxsX[1]; - maxs[1] = (t+1) * maxsX[1] - t * minsX[1]; - mins[2] = (t+1) * minsX[2] - t * maxsX[2]; - maxs[2] = (t+1) * maxsX[2] - t * minsX[2]; - - for(i = 0; i != numsamples; ++i) - { - testorigin[0] = lhrandom(mins[0], maxs[0]); - testorigin[1] = lhrandom(mins[1], maxs[1]); - testorigin[2] = lhrandom(mins[2], maxs[2]); - - if(model->brush.TraceLineOfSight(model, eye, testorigin)) - return 1; - } - - return 0; -} -