]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_alias.c
Add prvm_offsets.h to VS2017 projects, so it can be searched easily.
[xonotic/darkplaces.git] / model_alias.c
index 3da3b6202a0a54ee372fc1f50929af611d44b498..e319582c5e3ee77c89d238adf2e650eeeba69810 100644 (file)
@@ -94,37 +94,37 @@ void Mod_Skeletal_BuildTransforms(const dp_model_t * RESTRICT model, const frame
                for (i = 0;i < model->num_bones;i++)
                {
                        // blend by transform each quaternion/translation into a dual-quaternion first, then blending
-                       const short * RESTRICT pose7s = model->data_poses7s + 7 * (frameblend[0].subframe * model->num_bones + i);
-                       float lerp = frameblend[0].lerp,
-                               tx = pose7s[0], ty = pose7s[1], tz = pose7s[2],
-                               rx = pose7s[3] * lerp,
-                               ry = pose7s[4] * lerp,
-                               rz = pose7s[5] * lerp,
-                               rw = pose7s[6] * lerp,
-                               dx = tx*rw + ty*rz - tz*ry,
-                               dy = -tx*rz + ty*rw + tz*rx,
-                               dz = tx*ry - ty*rx + tz*rw,
-                               dw = -tx*rx - ty*ry - tz*rz,
+                       const short * RESTRICT firstpose7s = model->data_poses7s + 7 * (frameblend[0].subframe * model->num_bones + i);
+                       float firstlerp = frameblend[0].lerp,
+                               firsttx = firstpose7s[0], firstty = firstpose7s[1], firsttz = firstpose7s[2],
+                               rx = firstpose7s[3] * firstlerp,
+                               ry = firstpose7s[4] * firstlerp,
+                               rz = firstpose7s[5] * firstlerp,
+                               rw = firstpose7s[6] * firstlerp,
+                               dx = firsttx*rw + firstty*rz - firsttz*ry,
+                               dy = -firsttx*rz + firstty*rw + firsttz*rx,
+                               dz = firsttx*ry - firstty*rx + firsttz*rw,
+                               dw = -firsttx*rx - firstty*ry - firsttz*rz,
                                scale, sx, sy, sz, sw;
                        for (blends = 1;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
                        {
-                               const short * RESTRICT pose7s = model->data_poses7s + 7 * (frameblend[blends].subframe * model->num_bones + i);
-                               float lerp = frameblend[blends].lerp,
-                                       tx = pose7s[0], ty = pose7s[1], tz = pose7s[2],
-                                       qx = pose7s[3], qy = pose7s[4], qz = pose7s[5], qw = pose7s[6];
-                               if(rx*qx + ry*qy + rz*qz + rw*qw < 0) lerp = -lerp;
-                               qx *= lerp;
-                               qy *= lerp;
-                               qz *= lerp;
-                               qw *= lerp;
+                               const short * RESTRICT blendpose7s = model->data_poses7s + 7 * (frameblend[blends].subframe * model->num_bones + i);
+                               float blendlerp = frameblend[blends].lerp,
+                                       blendtx = blendpose7s[0], blendty = blendpose7s[1], blendtz = blendpose7s[2],
+                                       qx = blendpose7s[3], qy = blendpose7s[4], qz = blendpose7s[5], qw = blendpose7s[6];
+                               if(rx*qx + ry*qy + rz*qz + rw*qw < 0) blendlerp = -blendlerp;
+                               qx *= blendlerp;
+                               qy *= blendlerp;
+                               qz *= blendlerp;
+                               qw *= blendlerp;
                                rx += qx;
                                ry += qy;
                                rz += qz;
                                rw += qw;
-                               dx += tx*qw + ty*qz - tz*qy;
-                               dy += -tx*qz + ty*qw + tz*qx;
-                               dz += tx*qy - ty*qx + tz*qw;
-                               dw += -tx*qx - ty*qy - tz*qz;
+                               dx += blendtx*qw + blendty*qz - blendtz*qy;
+                               dy += -blendtx*qz + blendty*qw + blendtz*qx;
+                               dz += blendtx*qy - blendty*qx + blendtz*qw;
+                               dw += -blendtx*qx - blendty*qy - blendtz*qz;
                        }
                        // generate a matrix from the dual-quaternion, implicitly normalizing it in the process
                        scale = 1.0f / (rx*rx + ry*ry + rz*rz + rw*rw);
@@ -752,32 +752,46 @@ static void Mod_Alias_MorphMesh_CompileFrames(void)
        }
 }
 
-static void Mod_MDLMD2MD3_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)
+static void Mod_MDLMD2MD3_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, int skipsupercontentsmask, int skipmaterialflagsmask)
 {
        int i;
        float segmentmins[3], segmentmaxs[3];
        msurface_t *surface;
-       float vertex3fbuf[1024*3];
+       float vertex3fbuf[1024 * 3];
        float *vertex3f = vertex3fbuf;
+       float *freevertex3f = NULL;
+       // for static cases we can just call CollisionBIH which is much faster
+       if ((frameblend == NULL || (frameblend[0].subframe == 0 && frameblend[1].lerp == 0)) && (skeleton == NULL || skeleton->relativetransforms == NULL))
+       {
+               Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask);
+               return;
+       }
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
-       if (model->surfmesh.num_vertices > 1024)
-               vertex3f = (float *)Mem_Alloc(tempmempool, model->surfmesh.num_vertices * sizeof(float[3]));
+       trace->skipsupercontentsmask = skipsupercontentsmask;
+       trace->skipmaterialflagsmask = skipmaterialflagsmask;
        segmentmins[0] = min(start[0], end[0]) - 1;
        segmentmins[1] = min(start[1], end[1]) - 1;
        segmentmins[2] = min(start[2], end[2]) - 1;
        segmentmaxs[0] = max(start[0], end[0]) + 1;
        segmentmaxs[1] = max(start[1], end[1]) + 1;
        segmentmaxs[2] = max(start[2], end[2]) + 1;
-       model->AnimateVertices(model, frameblend, skeleton, vertex3f, NULL, NULL, NULL);
+       if (frameblend == NULL || frameblend[0].subframe != 0 || frameblend[0].lerp != 0 || skeleton != NULL)
+       {
+               if (model->surfmesh.num_vertices > 1024)
+                       vertex3f = freevertex3f = (float *)Mem_Alloc(tempmempool, model->surfmesh.num_vertices * sizeof(float[3]));
+               model->AnimateVertices(model, frameblend, skeleton, vertex3f, NULL, NULL, NULL);
+       }
+       else
+               vertex3f = model->surfmesh.data_vertex3f;
        for (i = 0, surface = model->data_surfaces;i < model->num_surfaces;i++, surface++)
                Collision_TraceLineTriangleMeshFloat(trace, start, end, model->surfmesh.num_triangles, model->surfmesh.data_element3i, vertex3f, 0, NULL, SUPERCONTENTS_SOLID | (surface->texture->basematerialflags & MATERIALFLAGMASK_TRANSLUCENT ? 0 : SUPERCONTENTS_OPAQUE), 0, surface->texture, segmentmins, segmentmaxs);
-       if (vertex3f != vertex3fbuf)
-               Mem_Free(vertex3f);
+       if (freevertex3f)
+               Mem_Free(freevertex3f);
 }
 
-static void Mod_MDLMD2MD3_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)
+static void Mod_MDLMD2MD3_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 skipsupercontentsmask, int skipmaterialflagsmask)
 {
        int i;
        vec3_t shiftstart, shiftend;
@@ -792,15 +806,24 @@ static void Mod_MDLMD2MD3_TraceBox(dp_model_t *model, const frameblend_t *frameb
        {
                VectorAdd(start, boxmins, shiftstart);
                VectorAdd(end, boxmins, shiftend);
-               Mod_MDLMD2MD3_TraceLine(model, frameblend, skeleton, trace, shiftstart, shiftend, hitsupercontentsmask);
+               Mod_MDLMD2MD3_TraceLine(model, frameblend, skeleton, trace, shiftstart, shiftend, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask);
                VectorSubtract(trace->endpos, boxmins, trace->endpos);
                return;
        }
 
+       // for static cases we can just call CollisionBIH which is much faster
+       if ((frameblend == NULL || (frameblend[0].subframe == 0 && frameblend[1].lerp == 0)) && (skeleton == NULL || skeleton->relativetransforms == NULL))
+       {
+               Mod_CollisionBIH_TraceBox(model, frameblend, skeleton, trace, start, boxmins, boxmaxs, end, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask);
+               return;
+       }
+
        // box trace, performed as brush trace
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
+       trace->skipsupercontentsmask = skipsupercontentsmask;
+       trace->skipmaterialflagsmask = skipmaterialflagsmask;
        if (model->surfmesh.num_vertices > 1024)
                vertex3f = (float *)Mem_Alloc(tempmempool, model->surfmesh.num_vertices * sizeof(float[3]));
        segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1;
@@ -909,9 +932,7 @@ static void Mod_BuildAliasSkinFromSkinFrame(texture_t *texture, skinframe_t *ski
        memset(texture, 0, sizeof(*texture));
        texture->currentframe = texture;
        //texture->animated = false;
-       texture->numskinframes = 1;
-       texture->skinframerate = 1;
-       texture->skinframes[0] = skinframe;
+       texture->materialshaderpass = texture->shaderpasses[0] = Mod_CreateShaderPass(skinframe);
        texture->currentskinframe = skinframe;
        //texture->backgroundnumskinframes = 0;
        //texture->customblendfunc[0] = 0;
@@ -922,6 +943,7 @@ static void Mod_BuildAliasSkinFromSkinFrame(texture_t *texture, skinframe_t *ski
        //texture->textureflags = 0;
 
        texture->basematerialflags = MATERIALFLAG_WALL;
+       texture->basealpha = 1.0f;
        if (texture->currentskinframe->hasalpha)
                texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
        texture->currentmaterialflags = texture->basematerialflags;
@@ -1308,6 +1330,7 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        loadmodel->skinscenes[loadmodel->numskins].loop = true;
 
                        //increase skin counts
+                       loadmodel->num_textures++;
                        loadmodel->numskins++;
                        totalskins++;
 
@@ -1328,9 +1351,10 @@ void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend)
        if(mod_alias_force_animated.string[0])
                loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
 
+       // Always make a BIH for the first frame, we can use it where possible.
+       Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
        if (!loadmodel->surfmesh.isanimated)
        {
-               Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
                loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
                loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
                loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
@@ -1599,9 +1623,10 @@ void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend)
        surface->num_firstvertex = 0;
        surface->num_vertices = loadmodel->surfmesh.num_vertices;
 
+       // Always make a BIH for the first frame, we can use it where possible.
+       Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
        if (!loadmodel->surfmesh.isanimated)
        {
-               Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
                loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
                loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
                loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
@@ -1794,9 +1819,10 @@ void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend)
        if(mod_alias_force_animated.string[0])
                loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
 
+       // Always make a BIH for the first frame, we can use it where possible.
+       Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
        if (!loadmodel->surfmesh.isanimated)
        {
-               Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
                loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
                loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
                loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
@@ -2194,9 +2220,10 @@ void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        if(mod_alias_force_animated.string[0])
                loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
 
+       // Always make a BIH for the first frame, we can use it where possible.
+       Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
        if (!loadmodel->surfmesh.isanimated)
        {
-               Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
                loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
                loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
                loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
@@ -2572,9 +2599,10 @@ void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        if(mod_alias_force_animated.string[0])
                loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
 
+       // Always make a BIH for the first frame, we can use it where possible.
+       Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
        if (!loadmodel->surfmesh.isanimated)
        {
-               Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
                loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
                loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
                loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
@@ -3250,9 +3278,10 @@ void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        if(mod_alias_force_animated.string[0])
                loadmodel->surfmesh.isanimated = mod_alias_force_animated.integer != 0;
 
+       // Always make a BIH for the first frame, we can use it where possible.
+       Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
        if (!loadmodel->surfmesh.isanimated)
        {
-               Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
                loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
                loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
                loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
@@ -4016,9 +4045,10 @@ void Mod_INTERQUAKEMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend)
        if (!header.ofs_bounds)
                Mod_Alias_CalculateBoundingBox();
 
-       if (!loadmodel->surfmesh.isanimated && loadmodel->surfmesh.num_triangles >= 1)
+       // Always make a BIH for the first frame, we can use it where possible.
+       Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
+       if (!loadmodel->surfmesh.isanimated)
        {
-               Mod_MakeCollisionBIH(loadmodel, true, &loadmodel->collision_bih);
                loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
                loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
                loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;