diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2017-01-24 14:15:25 -0900 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2017-01-24 14:15:25 -0900 |
commit | 7f362ff033196b4c60f2c4279fd0f357844227b3 (patch) | |
tree | a8a46f27dc1b03bb1501fe41b1d1373e20b043d7 | |
parent | 149617bab9a13c60e78009734b619f1c1238cd12 (diff) |
Fewer globals
-rw-r--r-- | inc/common/bsp.h | 4 | ||||
-rw-r--r-- | inc/shared/matrix.h | 26 | ||||
-rw-r--r-- | inc/shared/shared.h | 10 | ||||
-rw-r--r-- | src/common/bsp.c | 215 | ||||
-rw-r--r-- | src/refresh/gl/draw.c | 6 | ||||
-rw-r--r-- | src/refresh/gl/gl.h | 2 | ||||
-rw-r--r-- | src/refresh/gl/main.c | 2 | ||||
-rw-r--r-- | src/refresh/gl/mesh.c | 468 | ||||
-rw-r--r-- | src/refresh/gl/surf.c | 8 | ||||
-rw-r--r-- | src/refresh/gl/world.c | 83 | ||||
-rw-r--r-- | src/shared/matrix.c | 14 |
11 files changed, 423 insertions, 415 deletions
diff --git a/inc/common/bsp.h b/inc/common/bsp.h index 47c7797..87d0363 100644 --- a/inc/common/bsp.h +++ b/inc/common/bsp.h @@ -291,7 +291,9 @@ typedef struct { float fraction; } lightpoint_t; -void BSP_LightPoint(lightpoint_t *point, vec3_t start, vec3_t end, mnode_t *headnode); +qboolean BSP_LightPoint(lightpoint_t *point, vec3_t start, + vec3_t end, mnode_t *headnode); + void BSP_TransformedLightPoint(lightpoint_t *point, vec3_t start, vec3_t end, mnode_t *headnode, vec3_t origin, vec3_t angles); #endif diff --git a/inc/shared/matrix.h b/inc/shared/matrix.h index b106d8c..7574edf 100644 --- a/inc/shared/matrix.h +++ b/inc/shared/matrix.h @@ -64,8 +64,9 @@ typedef struct { (d)[2]=(a)[2]+(b)[2]*(c)[2]) #define VectorEmpty(v) ((v)[0]==0&&(v)[1]==0&&(v)[2]==0) #define VectorCompare(v1,v2) ((v1)[0]==(v2)[0]&&(v1)[1]==(v2)[1]&&(v1)[2]==(v2)[2]) -#define VectorLength(v) (sqrt(DotProduct((v),(v)))) -#define VectorLengthSquared(v) (DotProduct((v),(v))) +#define VectorLength(v) sqrt(DotProduct((v),(v))) +#define VectorLengthSquared(v) DotProduct((v),(v)) + #define VectorScale(in,scale,out) \ ((out)[0]=(in)[0]*(scale), \ (out)[1]=(in)[1]*(scale), \ @@ -74,6 +75,16 @@ typedef struct { ((out)[0]=(in)[0]*(scale)[0], \ (out)[1]=(in)[1]*(scale)[1], \ (out)[2]=(in)[2]*(scale)[2]) + +#define VectorScaleAcc(in,scale,out) \ + ((out)[0]+=(in)[0]*(scale), \ + (out)[1]+=(in)[1]*(scale), \ + (out)[2]+=(in)[2]*(scale)) +#define VectorVectorScaleAcc(in,scale,out) \ + ((out)[0]+=(in)[0]*(scale)[0], \ + (out)[1]+=(in)[1]*(scale)[1], \ + (out)[2]+=(in)[2]*(scale)[2]) + #define DistanceSquared(v1,v2) \ (((v1)[0]-(v2)[0])*((v1)[0]-(v2)[0])+ \ ((v1)[1]-(v2)[1])*((v1)[1]-(v2)[1])+ \ @@ -83,14 +94,17 @@ typedef struct { ((d)[0]=LerpAngle((a)[0],(b)[0],c), \ (d)[1]=LerpAngle((a)[1],(b)[1],c), \ (d)[2]=LerpAngle((a)[2],(b)[2],c)) + #define LerpVector(a,b,c,d) \ ((d)[0]=(a)[0]+(c)*((b)[0]-(a)[0]), \ (d)[1]=(a)[1]+(c)*((b)[1]-(a)[1]), \ (d)[2]=(a)[2]+(c)*((b)[2]-(a)[2])) -#define LerpVector2(a,b,c,d,e) \ - ((e)[0]=(a)[0]*(c)+(b)[0]*(d), \ - (e)[1]=(a)[1]*(c)+(b)[1]*(d), \ - (e)[2]=(a)[2]*(c)+(b)[2]*(d)) + +#define LerpVector2(a,b,c,d,e) \ + (VectorClear(e), \ + VectorScaleAcc(a, c, e), \ + VectorScaleAcc(b, d, e)) + #define PlaneDiff(v,p) (DotProduct(v,(p)->normal)-(p)->dist) #define Vector4Subtract(a,b,c) ((c)[0]=(a)[0]-(b)[0],(c)[1]=(a)[1]-(b)[1],(c)[2]=(a)[2]-(b)[2],(c)[3]=(a)[3]-(b)[3]) diff --git a/inc/shared/shared.h b/inc/shared/shared.h index d6633b2..8bbc736 100644 --- a/inc/shared/shared.h +++ b/inc/shared/shared.h @@ -427,6 +427,7 @@ static inline float FloatSwap(float f) } #if __BYTE_ORDER == __LITTLE_ENDIAN + #define BigShort ShortSwap #define BigLong LongSwap #define BigFloat FloatSwap @@ -435,7 +436,12 @@ static inline float FloatSwap(float f) #define LittleFloat(x) ((float)(x)) #define MakeRawLong(b1,b2,b3,b4) (((b4)<<24)|((b3)<<16)|((b2)<<8)|(b1)) #define MakeRawShort(b1,b2) (((b2)<<8)|(b1)) + +#define le16_to_cpu(_x) ((uint16_t)(_x)) +#define le32_to_cpu(_x) ((uint32_t)(_x)) + #elif __BYTE_ORDER == __BIG_ENDIAN + #define BigShort(x) ((uint16_t)(x)) #define BigLong(x) ((uint32_t)(x)) #define BigFloat(x) ((float)(x)) @@ -444,6 +450,10 @@ static inline float FloatSwap(float f) #define LittleFloat FloatSwap #define MakeRawLong(b1,b2,b3,b4) (((b1)<<24)|((b2)<<16)|((b3)<<8)|(b4)) #define MakeRawShort(b1,b2) (((b1)<<8)|(b2)) + +#define le16_to_cpu(_x) ShortSwap(_x) +#define le32_to_cpu(_x) LongSwap(_x) + #else #error Unknown byte order #endif diff --git a/src/common/bsp.c b/src/common/bsp.c index 804b98a..16987e3 100644 --- a/src/common/bsp.c +++ b/src/common/bsp.c @@ -70,7 +70,7 @@ LOAD(Visibility) bsp->vis = ALLOC(count); memcpy(bsp->vis, base, count); - numclusters = LittleLong(bsp->vis->numclusters); + numclusters = le32_to_cpu(bsp->vis->numclusters); if (numclusters > MAX_MAP_LEAFS) { DEBUG("bad numclusters"); return Q_ERR_TOO_MANY; @@ -86,7 +86,7 @@ LOAD(Visibility) for (i = 0; i < numclusters; i++) { for (j = 0; j < 2; j++) { - bitofs = LittleLong(bsp->vis->bitofs[i][j]); + bitofs = le32_to_cpu(bsp->vis->bitofs[i][j]); if (bitofs >= count) { DEBUG("bad bitofs"); return Q_ERR_BAD_INDEX; @@ -119,8 +119,8 @@ LOAD(Texinfo) out->c.name[sizeof(out->c.name) - 1] = 0; memcpy(out->name, in->texture, sizeof(out->name)); out->name[sizeof(out->name) - 1] = 0; - out->c.flags = LittleLong(in->flags); - out->c.value = LittleLong(in->value); + out->c.flags = le32_to_cpu(in->flags); + out->c.value = le32_to_cpu(in->value); #if USE_REF for (j = 0; j < 2; j++) { @@ -130,7 +130,7 @@ LOAD(Texinfo) out->offset[j] = LittleFloat(in->vecs[j][k]); } - next = (int32_t)LittleLong(in->nexttexinfo); + next = (int32_t)le32_to_cpu(in->nexttexinfo); if (next > 0) { if (next >= count) { DEBUG("bad anim chain"); @@ -197,13 +197,13 @@ LOAD(BrushSides) in = base; out = bsp->brushsides; for (i = 0; i < count; i++, in++, out++) { - planenum = LittleShort(in->planenum); + planenum = le16_to_cpu(in->planenum); if (planenum >= bsp->numplanes) { DEBUG("bad planenum"); return Q_ERR_BAD_INDEX; } out->plane = bsp->planes + planenum; - texinfo = LittleShort(in->texinfo); + texinfo = le16_to_cpu(in->texinfo); if (texinfo == (uint16_t)-1) { out->texinfo = &nulltexinfo; } else { @@ -231,8 +231,8 @@ LOAD(Brushes) in = base; out = bsp->brushes; for (i = 0; i < count; i++, out++, in++) { - firstside = LittleLong(in->firstside); - numsides = LittleLong(in->numsides); + firstside = le32_to_cpu(in->firstside); + numsides = le32_to_cpu(in->numsides); lastside = firstside + numsides; if (lastside < firstside || lastside > bsp->numbrushsides) { DEBUG("bad brushsides"); @@ -240,7 +240,7 @@ LOAD(Brushes) } out->firstbrushside = bsp->brushsides + firstside; out->numsides = numsides; - out->contents = LittleLong(in->contents); + out->contents = le32_to_cpu(in->contents); out->checkcount = 0; } @@ -260,7 +260,7 @@ LOAD(LeafBrushes) in = base; out = bsp->leafbrushes; for (i = 0; i < count; i++, in++, out++) { - brushnum = LittleShort(*in); + brushnum = le16_to_cpu(*in); if (brushnum >= bsp->numbrushes) { DEBUG("bad brushnum"); return Q_ERR_BAD_INDEX; @@ -321,7 +321,7 @@ LOAD(Edges) out = bsp->edges; for (i = 0; i < count; i++, out++, in++) { for (j = 0; j < 2; j++) { - vertnum = LittleShort(in->v[j]); + vertnum = le16_to_cpu(in->v[j]); if (vertnum >= bsp->numvertices) { DEBUG("bad vertnum"); return Q_ERR_BAD_INDEX; @@ -346,7 +346,7 @@ LOAD(SurfEdges) in = base; out = bsp->surfedges; for (i = 0; i < count; i++, out++, in++) { - index = (int32_t)LittleLong(*in); + index = (int32_t)le32_to_cpu(*in); vert = 0; if (index < 0) { @@ -381,8 +381,8 @@ LOAD(Faces) in = base; out = bsp->faces; for (i = 0; i < count; i++, in++, out++) { - firstedge = LittleLong(in->firstedge); - numedges = LittleShort(in->numedges); + firstedge = le32_to_cpu(in->firstedge); + numedges = le16_to_cpu(in->numedges); lastedge = firstedge + numedges; if (numedges < 3) { DEBUG("bad surfedges"); @@ -399,14 +399,14 @@ LOAD(Faces) out->firstsurfedge = bsp->surfedges + firstedge; out->numsurfedges = numedges; - planenum = LittleShort(in->planenum); + planenum = le16_to_cpu(in->planenum); if (planenum >= bsp->numplanes) { DEBUG("bad planenum"); return Q_ERR_BAD_INDEX; } out->plane = bsp->planes + planenum; - texinfo = LittleShort(in->texinfo); + texinfo = le16_to_cpu(in->texinfo); if (texinfo >= bsp->numtexinfo) { DEBUG("bad texinfo"); return Q_ERR_BAD_INDEX; @@ -421,7 +421,7 @@ LOAD(Faces) out->styles[j] = 255; } - lightofs = LittleLong(in->lightofs); + lightofs = le32_to_cpu(in->lightofs); if (lightofs == (uint32_t)-1 || bsp->numlightmapbytes == 0) { out->lightmap = NULL; } else { @@ -432,7 +432,7 @@ LOAD(Faces) out->lightmap = bsp->lightmap + lightofs; } - side = LittleShort(in->side); + side = le16_to_cpu(in->side); out->drawflags = side & DSURF_PLANEBACK; } @@ -452,7 +452,7 @@ LOAD(LeafFaces) in = base; out = bsp->leaffaces; for (i = 0; i < count; i++, in++, out++) { - facenum = LittleShort(*in); + facenum = le16_to_cpu(*in); if (facenum >= bsp->numfaces) { DEBUG("bad facenum"); return Q_ERR_BAD_INDEX; @@ -488,8 +488,8 @@ LOAD(Leafs) out = bsp->leafs; for (i = 0; i < count; i++, in++, out++) { out->plane = NULL; - out->contents = LittleLong(in->contents); - cluster = LittleShort(in->cluster); + out->contents = le32_to_cpu(in->contents); + cluster = le16_to_cpu(in->cluster); if (cluster == (uint16_t)-1) { // solid leafs use special -1 cluster out->cluster = -1; @@ -505,15 +505,15 @@ LOAD(Leafs) out->cluster = cluster; } - area = LittleShort(in->area); + area = le16_to_cpu(in->area); if (area >= bsp->numareas) { DEBUG("bad area"); return Q_ERR_BAD_INDEX; } out->area = area; - firstleafbrush = LittleShort(in->firstleafbrush); - numleafbrushes = LittleShort(in->numleafbrushes); + firstleafbrush = le16_to_cpu(in->firstleafbrush); + numleafbrushes = le16_to_cpu(in->numleafbrushes); lastleafbrush = firstleafbrush + numleafbrushes; if (lastleafbrush < firstleafbrush || lastleafbrush > bsp->numleafbrushes) { DEBUG("bad leafbrushes"); @@ -523,8 +523,8 @@ LOAD(Leafs) out->numleafbrushes = numleafbrushes; #if USE_REF - firstleafface = LittleShort(in->firstleafface); - numleaffaces = LittleShort(in->numleaffaces); + firstleafface = le16_to_cpu(in->firstleafface); + numleaffaces = le16_to_cpu(in->numleaffaces); lastleafface = firstleafface + numleaffaces; if (lastleafface < firstleafface || lastleafface > bsp->numleaffaces) { DEBUG("bad leaffaces"); @@ -534,8 +534,8 @@ LOAD(Leafs) out->numleaffaces = numleaffaces; for (j = 0; j < 3; j++) { - out->mins[j] = (int16_t)LittleShort(in->mins[j]); - out->maxs[j] = (int16_t)LittleShort(in->maxs[j]); + out->mins[j] = (int16_t)le16_to_cpu(in->mins[j]); + out->maxs[j] = (int16_t)le16_to_cpu(in->maxs[j]); } out->parent = NULL; @@ -572,7 +572,7 @@ LOAD(Nodes) in = base; out = bsp->nodes; for (i = 0; i < count; i++, out++, in++) { - planenum = LittleLong(in->planenum); + planenum = le32_to_cpu(in->planenum); if (planenum >= bsp->numplanes) { DEBUG("bad planenum"); return Q_ERR_BAD_INDEX; @@ -580,7 +580,7 @@ LOAD(Nodes) out->plane = bsp->planes + planenum; for (j = 0; j < 2; j++) { - child = LittleLong(in->children[j]); + child = le32_to_cpu(in->children[j]); if (child & 0x80000000) { child = ~child; if (child >= bsp->numleafs) { @@ -598,8 +598,8 @@ LOAD(Nodes) } #if USE_REF - firstface = LittleShort(in->firstface); - numfaces = LittleShort(in->numfaces); + firstface = le16_to_cpu(in->firstface); + numfaces = le16_to_cpu(in->numfaces); lastface = firstface + numfaces; if (lastface < firstface || lastface > bsp->numfaces) { DEBUG("bad faces"); @@ -609,8 +609,8 @@ LOAD(Nodes) out->numfaces = numfaces; for (j = 0; j < 3; j++) { - out->mins[j] = (int16_t)LittleShort(in->mins[j]); - out->maxs[j] = (int16_t)LittleShort(in->maxs[j]); + out->mins[j] = (int16_t)le16_to_cpu(in->mins[j]); + out->maxs[j] = (int16_t)le16_to_cpu(in->maxs[j]); } out->parent = NULL; @@ -648,7 +648,7 @@ LOAD(Submodels) out->maxs[j] = LittleFloat(in->maxs[j]) + 1; out->origin[j] = LittleFloat(in->origin[j]); } - headnode = LittleLong(in->headnode); + headnode = le32_to_cpu(in->headnode); if (headnode & 0x80000000) { // be careful, some models have no nodes, just a leaf headnode = ~headnode; @@ -668,8 +668,8 @@ LOAD(Submodels) if (i == 0) { continue; } - firstface = LittleLong(in->firstface); - numfaces = LittleLong(in->numfaces); + firstface = le32_to_cpu(in->firstface); + numfaces = le32_to_cpu(in->numfaces); lastface = firstface + numfaces; if (lastface < firstface || lastface > bsp->numfaces) { DEBUG("bad faces"); @@ -698,8 +698,8 @@ LOAD(AreaPortals) in = base; out = bsp->areaportals; for (i = 0; i < count; i++, in++, out++) { - out->portalnum = LittleLong(in->portalnum); - out->otherarea = LittleLong(in->otherarea); + out->portalnum = le32_to_cpu(in->portalnum); + out->otherarea = le32_to_cpu(in->otherarea); } return Q_ERR_SUCCESS; @@ -718,8 +718,8 @@ LOAD(Areas) in = base; out = bsp->areas; for (i = 0; i < count; i++, in++, out++) { - numareaportals = LittleLong(in->numareaportals); - firstareaportal = LittleLong(in->firstareaportal); + numareaportals = le32_to_cpu(in->numareaportals); + firstareaportal = le32_to_cpu(in->firstareaportal); lastareaportal = firstareaportal + numareaportals; if (lastareaportal < firstareaportal || lastareaportal > bsp->numareaportals) { DEBUG("bad areaportals"); @@ -987,11 +987,11 @@ qerror_t BSP_Load(const char *name, bsp_t **bsp_p) // byte swap and validate the header header = (dheader_t *)buf; - if (LittleLong(header->ident) != IDBSPHEADER) { + if (le32_to_cpu(header->ident) != IDBSPHEADER) { ret = Q_ERR_UNKNOWN_FORMAT; goto fail2; } - if (LittleLong(header->version) != BSPVERSION) { + if (le32_to_cpu(header->version) != BSPVERSION) { ret = Q_ERR_UNKNOWN_FORMAT; goto fail2; } @@ -999,8 +999,8 @@ qerror_t BSP_Load(const char *name, bsp_t **bsp_p) // byte swap and validate all lumps memsize = 0; for (info = bsp_lumps; info->load; info++) { - ofs = LittleLong(header->lumps[info->lump].fileofs); - len = LittleLong(header->lumps[info->lump].filelen); + ofs = le32_to_cpu(header->lumps[info->lump].fileofs); + len = le32_to_cpu(header->lumps[info->lump].filelen); end = ofs + len; if (end < ofs || end > filelen) { ret = Q_ERR_BAD_EXTENT; @@ -1032,7 +1032,7 @@ qerror_t BSP_Load(const char *name, bsp_t **bsp_p) Hunk_Begin(&bsp->hunk, memsize + 4096); // calculate the checksum - bsp->checksum = LittleLong(Com_BlockChecksum(buf, filelen)); + bsp->checksum = le32_to_cpu(Com_BlockChecksum(buf, filelen)); // load all lumps for (info = bsp_lumps; info->load; info++) { @@ -1079,77 +1079,76 @@ HELPER FUNCTIONS #if USE_REF -static lightpoint_t *light_point; - -static qboolean BSP_RecursiveLightPoint(mnode_t *node, float p1f, float p2f, vec3_t p1, vec3_t p2) +static qboolean BSP_RecursiveLightPoint(lightpoint_t *point, + mnode_t *node, + float p1f, float p2f, + vec3_t p1, vec3_t p2) { vec_t d1, d2, frac, midf; vec3_t mid; - int i, side, s, t; + int side, s, t; mface_t *surf; - mtexinfo_t *texinfo; - while (node->plane) { - // calculate distancies - d1 = PlaneDiffFast(p1, node->plane); - d2 = PlaneDiffFast(p2, node->plane); - side = (d1 < 0); - - if ((d2 < 0) == side) { - // both points are one the same side - node = node->children[side]; - continue; - } + if (!node->plane) + return qfalse; - // find crossing point - frac = d1 / (d1 - d2); - midf = p1f + (p2f - p1f) * frac; - LerpVector(p1, p2, frac, mid); - - // check near side - if (BSP_RecursiveLightPoint(node->children[side], p1f, midf, p1, mid)) - return qtrue; - - for (i = 0, surf = node->firstface; i < node->numfaces; i++, surf++) { - if (!surf->lightmap) - continue; - - texinfo = surf->texinfo; - if (texinfo->c.flags & SURF_NOLM_MASK) - continue; - - s = DotProduct(texinfo->axis[0], mid) + texinfo->offset[0]; - t = DotProduct(texinfo->axis[1], mid) + texinfo->offset[1]; - - s -= surf->texturemins[0]; - t -= surf->texturemins[1]; - if (s < 0 || s > surf->extents[0]) - continue; - if (t < 0 || t > surf->extents[1]) - continue; - - light_point->surf = surf; - light_point->plane = *surf->plane; - light_point->s = s; - light_point->t = t; - light_point->fraction = midf; - return qtrue; - } + // calculate distancies + d1 = PlaneDiffFast(p1, node->plane); + d2 = PlaneDiffFast(p2, node->plane); + side = d1 < 0; + + if ((d2 < 0) == side) { + // both points are on the same side + return BSP_RecursiveLightPoint(point, node->children[side], p1f, p2f, p1, p2); + } - // check far side - return BSP_RecursiveLightPoint(node->children[side ^ 1], midf, p2f, mid, p2); + // find crossing point + frac = d1 / (d1 - d2); + midf = p1f + (p2f - p1f) * frac; + LerpVector(p1, p2, frac, mid); + + // check near side + if (BSP_RecursiveLightPoint(point, node->children[side], p1f, midf, p1, mid)) + return qtrue; + + for (surf = node->firstface; + surf != node->firstface + node->numfaces; + surf++) { + if (!surf->lightmap) + continue; + + if (surf->texinfo->c.flags & SURF_NOLM_MASK) + continue; + + s = DotProduct(surf->texinfo->axis[0], mid) + + surf->texinfo->offset[0] - + surf->texturemins[0]; + + t = DotProduct(surf->texinfo->axis[1], mid) + + surf->texinfo->offset[1] - + surf->texturemins[1]; + + if (s >= 0 && s <= surf->extents[0] && + t >= 0 && t <= surf->extents[1]) { + point->surf = surf; + point->plane = *surf->plane; + point->s = s; + point->t = t; + point->fraction = midf; + return qtrue; + } } - return qfalse; + // check far side + return BSP_RecursiveLightPoint(point, node->children[side ^ 1], midf, p2f, mid, p2); } -void BSP_LightPoint(lightpoint_t *point, vec3_t start, vec3_t end, mnode_t *headnode) +qboolean BSP_LightPoint(lightpoint_t *point, vec3_t start, vec3_t end, mnode_t *headnode) { - light_point = point; - light_point->surf = NULL; - light_point->fraction = 1; + point->surf = NULL; + point->fraction = 1; - BSP_RecursiveLightPoint(headnode, 0, 1, start, end); + return BSP_RecursiveLightPoint(point, headnode, 0, 1, start, end); } void BSP_TransformedLightPoint(lightpoint_t *point, vec3_t start, vec3_t end, @@ -1158,10 +1157,6 @@ void BSP_TransformedLightPoint(lightpoint_t *point, vec3_t start, vec3_t end, vec3_t start_l, end_l; vec3_t axis[3]; - light_point = point; - light_point->surf = NULL; - light_point->fraction = 1; - // subtract origin offset VectorSubtract(start, origin, start_l); VectorSubtract(end, origin, end_l); @@ -1174,7 +1169,7 @@ void BSP_TransformedLightPoint(lightpoint_t *point, vec3_t start, vec3_t end, } // sweep the line through the model - if (!BSP_RecursiveLightPoint(headnode, 0, 1, start_l, end_l)) + if (!BSP_LightPoint(point, start_l, end_l, headnode)) return; // rotate plane normal into the worlds frame of reference diff --git a/src/refresh/gl/draw.c b/src/refresh/gl/draw.c index bbdea7b..12ee64f 100644 --- a/src/refresh/gl/draw.c +++ b/src/refresh/gl/draw.c @@ -341,15 +341,15 @@ void Draw_Stats(void) Draw_Stringf(x, y, "2D batches : %i", c.batchesDrawn2D); y += 10; } -void Draw_Lightmaps(void) +void Draw_Lightmaps(const lightmap_builder_t *lm) { int i, x, y; - for (i = 0; i < lm.nummaps; i++) { + for (i = 0; i < lm->nummaps; i++) { x = i & 1; y = i >> 1; _GL_StretchPic(256 * x, 256 * y, 256, 256, - 0, 0, 1, 1, U32_WHITE, lm.texnums[i], 0); + 0, 0, 1, 1, U32_WHITE, lm->texnums[i], 0); } } diff --git a/src/refresh/gl/gl.h b/src/refresh/gl/gl.h index ac04d9a..eafad5a 100644 --- a/src/refresh/gl/gl.h +++ b/src/refresh/gl/gl.h @@ -426,7 +426,7 @@ extern drawStatic_t draw; #ifdef _DEBUG void Draw_Stringf(int x, int y, const char *fmt, ...); void Draw_Stats(void); -void Draw_Lightmaps(void); +void Draw_Lightmaps(const lightmap_builder_t *lm); void Draw_Scrap(void); #endif diff --git a/src/refresh/gl/main.c b/src/refresh/gl/main.c index b6df338..3adb4b3 100644 --- a/src/refresh/gl/main.c +++ b/src/refresh/gl/main.c @@ -559,7 +559,7 @@ void R_RenderFrame(refdef_t *fd) #ifdef _DEBUG if (gl_lightmap->integer > 1) { - Draw_Lightmaps(); + Draw_Lightmaps(&lm); } #endif diff --git a/src/refresh/gl/mesh.c b/src/refresh/gl/mesh.c index 1396cc6..e4f9183 100644 --- a/src/refresh/gl/mesh.c +++ b/src/refresh/gl/mesh.c @@ -16,43 +16,43 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include <assert.h> + #include "gl.h" -typedef void (*tessfunc_t)(const maliasmesh_t *); +typedef struct { + unsigned oldframenum; + unsigned newframenum; + float frontlerp; + float backlerp; -static int oldframenum; -static int newframenum; -static float frontlerp; -static float backlerp; -static vec3_t origin; -static vec3_t oldscale; -static vec3_t newscale; -static vec3_t translate; -static vec_t shellscale; -static tessfunc_t tessfunc; -static vec4_t color; + vec3_t origin; + vec3_t oldscale; + vec3_t newscale; + vec3_t translate; + vec_t shellscale; + vec4_t color; -static const vec_t *shadelight; -static vec3_t shadedir; + const vec_t *shadelight; + vec3_t shadedir; -static float celscale; + float celscale; -static mat4_t shadowmatrix; + mat4_t shadowmatrix; +} tess_info_t; -static void setup_dotshading(void) +static void setup_dotshading(tess_info_t *t) { float cp, cy, sp, sy; vec_t yaw; - shadelight = NULL; - if (!gl_dotshading->integer) return; if (glr.ent->flags & RF_SHELL_MASK) return; - shadelight = color; + t->shadelight = t->color; // matches the anormtab.h precalculations yaw = -DEG2RAD(glr.ent->angles[YAW]); @@ -60,14 +60,16 @@ static void setup_dotshading(void) sy = sin(yaw); cp = cos(-M_PI / 4); sp = sin(-M_PI / 4); - shadedir[0] = cp * cy; - shadedir[1] = cp * sy; - shadedir[2] = -sp; + + t->shadedir[0] = cp * cy; + t->shadedir[1] = cp * sy; + t->shadedir[2] = -sp; } -static inline vec_t shadedot(const vec_t *normal) +static inline vec_t shadedot(const tess_info_t *t, + const vec_t *normal) { - vec_t d = DotProduct(normal, shadedir); + vec_t d = DotProduct(normal, t->shadedir); // matches the anormtab.h precalculations if (d < 0) { @@ -89,9 +91,26 @@ static inline vec_t *get_static_normal(vec_t *normal, const maliasvert_t *vert) return normal; } -static void tess_static_shell(const maliasmesh_t *mesh) +static inline vec_t *get_lerped_normal(const tess_info_t *t, + vec_t *normal, + const maliasvert_t *oldvert, + const maliasvert_t *newvert) +{ + vec3_t oldnorm, newnorm; + + get_static_normal(oldnorm, oldvert); + get_static_normal(newnorm, newvert); + + LerpVector2(oldnorm, newnorm, t->backlerp, t->frontlerp, normal); + VectorNormalize(normal); + + return normal; +} + +static void tess_static_shell(const tess_info_t *t, + const maliasmesh_t *mesh) { - maliasvert_t *src_vert = &mesh->verts[newframenum * mesh->numverts]; + maliasvert_t *src_vert = &mesh->verts[t->newframenum * mesh->numverts]; vec_t *dst_vert = tess.vertices; int count = mesh->numverts; vec3_t normal; @@ -99,182 +118,146 @@ static void tess_static_shell(const maliasmesh_t *mesh) while (count--) { get_static_normal(normal, src_vert); - dst_vert[0] = normal[0] * shellscale + - src_vert->pos[0] * newscale[0] + translate[0]; - dst_vert[1] = normal[1] * shellscale + - src_vert->pos[1] * newscale[1] + translate[1]; - dst_vert[2] = normal[2] * shellscale + - src_vert->pos[2] * newscale[2] + translate[2]; - dst_vert += 4; + VectorCopy(t->translate, dst_vert); + VectorScaleAcc(normal, t->shellscale, dst_vert); + VectorVectorScaleAcc(src_vert->pos, t->newscale, dst_vert); + dst_vert += 4; src_vert++; } } -static void tess_static_shade(const maliasmesh_t *mesh) +static void tess_static_shade(const tess_info_t *t, + const maliasmesh_t *mesh) { - maliasvert_t *src_vert = &mesh->verts[newframenum * mesh->numverts]; + maliasvert_t *src_vert = &mesh->verts[t->newframenum * mesh->numverts]; vec_t *dst_vert = tess.vertices; int count = mesh->numverts; vec3_t normal; vec_t d; while (count--) { - d = shadedot(get_static_normal(normal, src_vert)); - - dst_vert[0] = src_vert->pos[0] * newscale[0] + translate[0]; - dst_vert[1] = src_vert->pos[1] * newscale[1] + translate[1]; - dst_vert[2] = src_vert->pos[2] * newscale[2] + translate[2]; - dst_vert[4] = shadelight[0] * d; - dst_vert[5] = shadelight[1] * d; - dst_vert[6] = shadelight[2] * d; - dst_vert[7] = shadelight[3]; - dst_vert += VERTEX_SIZE; + get_static_normal(normal, src_vert); + d = shadedot(t, normal); + VectorCopy(t->translate, dst_vert); + VectorVectorScaleAcc(src_vert->pos, t->newscale, dst_vert); + + Vector4Copy(t->shadelight, dst_vert + 4); + VectorScale(dst_vert + 4, d, dst_vert + 4); + + dst_vert += VERTEX_SIZE; src_vert++; } } -static void tess_static_plain(const maliasmesh_t *mesh) +static void tess_static_plain(const tess_info_t *t, + const maliasmesh_t *mesh) { - maliasvert_t *src_vert = &mesh->verts[newframenum * mesh->numverts]; + maliasvert_t *src_vert = &mesh->verts[t->newframenum * mesh->numverts]; vec_t *dst_vert = tess.vertices; int count = mesh->numverts; while (count--) { - dst_vert[0] = src_vert->pos[0] * newscale[0] + translate[0]; - dst_vert[1] = src_vert->pos[1] * newscale[1] + translate[1]; - dst_vert[2] = src_vert->pos[2] * newscale[2] + translate[2]; - dst_vert += 4; + VectorCopy(t->translate, dst_vert); + VectorVectorScaleAcc(src_vert->pos, t->newscale, dst_vert); + dst_vert += 4; src_vert++; } } -static inline vec_t *get_lerped_normal(vec_t *normal, - const maliasvert_t *oldvert, - const maliasvert_t *newvert) -{ - vec3_t oldnorm, newnorm, tmp; - vec_t len; - - get_static_normal(oldnorm, oldvert); - get_static_normal(newnorm, newvert); - - LerpVector2(oldnorm, newnorm, backlerp, frontlerp, tmp); - - // normalize result - len = 1 / VectorLength(tmp); - VectorScale(tmp, len, normal); - - return normal; -} - -static void tess_lerped_shell(const maliasmesh_t *mesh) +static void tess_lerped_shell(const tess_info_t *t, + const maliasmesh_t *mesh) { - maliasvert_t *src_oldvert = &mesh->verts[oldframenum * mesh->numverts]; - maliasvert_t *src_newvert = &mesh->verts[newframenum * mesh->numverts]; + maliasvert_t *src_oldvert = &mesh->verts[t->oldframenum * mesh->numverts]; + maliasvert_t *src_newvert = &mesh->verts[t->newframenum * mesh->numverts]; vec_t *dst_vert = tess.vertices; int count = mesh->numverts; vec3_t normal; while (count--) { - get_lerped_normal(normal, src_oldvert, src_newvert); - - dst_vert[0] = normal[0] * shellscale + - src_oldvert->pos[0] * oldscale[0] + - src_newvert->pos[0] * newscale[0] + translate[0]; - dst_vert[1] = normal[1] * shellscale + - src_oldvert->pos[1] * oldscale[1] + - src_newvert->pos[1] * newscale[1] + translate[1]; - dst_vert[2] = normal[2] * shellscale + - src_oldvert->pos[2] * oldscale[2] + - src_newvert->pos[2] * newscale[2] + translate[2]; - dst_vert += 4; + get_lerped_normal(t, normal, src_oldvert, src_newvert); + VectorCopy(t->translate, dst_vert); + VectorVectorScaleAcc(src_oldvert->pos, t->oldscale, dst_vert); + VectorVectorScaleAcc(src_newvert->pos, t->newscale, dst_vert); + VectorScaleAcc(normal, t->shellscale, dst_vert); + + dst_vert += 4; src_oldvert++; src_newvert++; } } -static void tess_lerped_shade(const maliasmesh_t *mesh) +static void tess_lerped_shade(const tess_info_t *t, + const maliasmesh_t *mesh) { - maliasvert_t *src_oldvert = &mesh->verts[oldframenum * mesh->numverts]; - maliasvert_t *src_newvert = &mesh->verts[newframenum * mesh->numverts]; + maliasvert_t *src_oldvert = &mesh->verts[t->oldframenum * mesh->numverts]; + maliasvert_t *src_newvert = &mesh->verts[t->newframenum * mesh->numverts]; vec_t *dst_vert = tess.vertices; int count = mesh->numverts; vec3_t normal; vec_t d; while (count--) { - d = shadedot(get_lerped_normal(normal, src_oldvert, src_newvert)); - - dst_vert[0] = - src_oldvert->pos[0] * oldscale[0] + - src_newvert->pos[0] * newscale[0] + translate[0]; - dst_vert[1] = - src_oldvert->pos[1] * oldscale[1] + - src_newvert->pos[1] * newscale[1] + translate[1]; - dst_vert[2] = - src_oldvert->pos[2] * oldscale[2] + - src_newvert->pos[2] * newscale[2] + translate[2]; - dst_vert[4] = shadelight[0] * d; - dst_vert[5] = shadelight[1] * d; - dst_vert[6] = shadelight[2] * d; - dst_vert[7] = shadelight[3]; - dst_vert += VERTEX_SIZE; + get_lerped_normal(t, normal, src_oldvert, src_newvert); + d = shadedot(t, normal); + + VectorCopy(t->translate, dst_vert); + VectorVectorScaleAcc(src_oldvert->pos, t->oldscale, dst_vert); + VectorVectorScaleAcc(src_newvert->pos, t->newscale, dst_vert); + + Vector4Copy(t->shadelight, dst_vert + 4); + VectorScale(dst_vert + 4, d, dst_vert + 4); + dst_vert += VERTEX_SIZE; src_oldvert++; src_newvert++; } } -static void tess_lerped_plain(const maliasmesh_t *mesh) +static void tess_lerped_plain(const tess_info_t *t, + const maliasmesh_t *mesh) { - maliasvert_t *src_oldvert = &mesh->verts[oldframenum * mesh->numverts]; - maliasvert_t *src_newvert = &mesh->verts[newframenum * mesh->numverts]; + maliasvert_t *src_oldvert = &mesh->verts[t->oldframenum * mesh->numverts]; + maliasvert_t *src_newvert = &mesh->verts[t->newframenum * mesh->numverts]; vec_t *dst_vert = tess.vertices; int count = mesh->numverts; while (count--) { - dst_vert[0] = - src_oldvert->pos[0] * oldscale[0] + - src_newvert->pos[0] * newscale[0] + translate[0]; - dst_vert[1] = - src_oldvert->pos[1] * oldscale[1] + - src_newvert->pos[1] * newscale[1] + translate[1]; - dst_vert[2] = - src_oldvert->pos[2] * oldscale[2] + - src_newvert->pos[2] * newscale[2] + translate[2]; - dst_vert += 4; + VectorCopy(t->translate, dst_vert); + VectorVectorScaleAcc(src_oldvert->pos, t->oldscale, dst_vert); + VectorVectorScaleAcc(src_newvert->pos, t->newscale, dst_vert); + dst_vert += 4; src_oldvert++; src_newvert++; } } -static glCullResult_t cull_static_model(model_t *model) +static glCullResult_t cull_static_model(tess_info_t *t, const model_t *model) { - maliasframe_t *newframe = &model->frames[newframenum]; + maliasframe_t *newframe = &model->frames[t->newframenum]; vec3_t bounds[2]; glCullResult_t cull; if (glr.entrotated) { - cull = GL_CullSphere(origin, newframe->radius); + cull = GL_CullSphere(t->origin, newframe->radius); if (cull == CULL_OUT) { c.spheresCulled++; return cull; } if (cull == CULL_CLIP) { - cull = GL_CullLocalBox(origin, newframe->bounds); + cull = GL_CullLocalBox(t->origin, newframe->bounds); if (cull == CULL_OUT) { c.rotatedBoxesCulled++; return cull; } } } else { - VectorAdd(newframe->bounds[0], origin, bounds[0]); - VectorAdd(newframe->bounds[1], origin, bounds[1]); + VectorAdd(newframe->bounds[0], t->origin, bounds[0]); + VectorAdd(newframe->bounds[1], t->origin, bounds[1]); cull = GL_CullBox(bounds); if (cull == CULL_OUT) { c.boxesCulled++; @@ -282,31 +265,30 @@ static glCullResult_t cull_static_model(model_t *model) } } - VectorCopy(newframe->scale, newscale); - VectorCopy(newframe->translate, translate); + VectorCopy(newframe->scale, t->newscale); + VectorCopy(newframe->translate, t->translate); return cull; } -static glCullResult_t cull_lerped_model(model_t *model) +static glCullResult_t cull_lerped_model(tess_info_t *t, const model_t *model) { - maliasframe_t *newframe = &model->frames[newframenum]; - maliasframe_t *oldframe = &model->frames[oldframenum]; + maliasframe_t *newframe = &model->frames[t->newframenum]; + maliasframe_t *oldframe = &model->frames[t->oldframenum]; vec3_t bounds[2]; vec_t radius; glCullResult_t cull; if (glr.entrotated) { - radius = newframe->radius > oldframe->radius ? - newframe->radius : oldframe->radius; - cull = GL_CullSphere(origin, radius); + radius = max(newframe->radius, oldframe->radius); + cull = GL_CullSphere(t->origin, radius); if (cull == CULL_OUT) { c.spheresCulled++; return cull; } UnionBounds(newframe->bounds, oldframe->bounds, bounds); if (cull == CULL_CLIP) { - cull = GL_CullLocalBox(origin, bounds); + cull = GL_CullLocalBox(t->origin, bounds); if (cull == CULL_OUT) { c.rotatedBoxesCulled++; return cull; @@ -314,8 +296,8 @@ static glCullResult_t cull_lerped_model(model_t *model) } } else { UnionBounds(newframe->bounds, oldframe->bounds, bounds); - VectorAdd(bounds[0], origin, bounds[0]); - VectorAdd(bounds[1], origin, bounds[1]); + VectorAdd(bounds[0], t->origin, bounds[0]); + VectorAdd(bounds[1], t->origin, bounds[1]); cull = GL_CullBox(bounds); if (cull == CULL_OUT) { c.boxesCulled++; @@ -323,127 +305,122 @@ static glCullResult_t cull_lerped_model(model_t *model) } } - VectorScale(oldframe->scale, backlerp, oldscale); - VectorScale(newframe->scale, frontlerp, newscale); + VectorScale(oldframe->scale, t->backlerp, t->oldscale); + VectorScale(newframe->scale, t->frontlerp, t->newscale); LerpVector2(oldframe->translate, newframe->translate, - backlerp, frontlerp, translate); + t->backlerp, t->frontlerp, t->translate); return cull; } -static void setup_color(void) +/* sets t->color */ +static void setup_color(tess_info_t *t) { int flags = glr.ent->flags; float f, m; int i; - memset(&glr.lightpoint, 0, sizeof(glr.lightpoint)); - if (flags & RF_SHELL_MASK) { - VectorClear(color); - if (flags & RF_SHELL_HALF_DAM) { - color[0] = 0.56f; - color[1] = 0.59f; - color[2] = 0.45f; - } + VectorClear(t->color); + + if (flags & RF_SHELL_HALF_DAM) + VectorSet(t->color, 0.56f, 0.59f, 0.45f); + if (flags & RF_SHELL_DOUBLE) { - color[0] = 0.9f; - color[1] = 0.7f; - } - if (flags & RF_SHELL_RED) { - color[0] = 1; - } - if (flags & RF_SHELL_GREEN) { - color[1] = 1; - } - if (flags & RF_SHELL_BLUE) { - color[2] = 1; + t->color[0] = 0.9f; + t->color[1] = 0.7f; } + + if (flags & RF_SHELL_RED) + t->color[0] = 1; + + if (flags & RF_SHELL_GREEN) + t->color[1] = 1; + + if (flags & RF_SHELL_BLUE) + t->color[2] = 1; } else if (flags & RF_FULLBRIGHT) { - VectorSet(color, 1, 1, 1); + VectorSet(t->color, 1, 1, 1); } else if ((flags & RF_IR_VISIBLE) && (glr.fd.rdflags & RDF_IRGOGGLES)) { - VectorSet(color, 1, 0, 0); + VectorSet(t->color, 1, 0, 0); } else { - GL_LightPoint(origin, color); + GL_LightPoint(t->origin, t->color); if (flags & RF_MINLIGHT) { for (i = 0; i < 3; i++) { - if (color[i] > 0.1f) { + if (t->color[i] <= 0.1f) { + VectorSet(t->color, 0.1f, 0.1f, 0.1f); break; } } - if (i == 3) { - VectorSet(color, 0.1f, 0.1f, 0.1f); - } } if (flags & RF_GLOW) { f = 0.1f * sin(glr.fd.time * 7); + for (i = 0; i < 3; i++) { - m = color[i] * 0.8f; - color[i] += f; - if (color[i] < m) - color[i] = m; + m = t->color[i] * 0.8f; + t->color[i] += f; + if (t->color[i] < m) + t->color[i] = m; } } - for (i = 0; i < 3; i++) { - clamp(color[i], 0, 1); - } + for (i = 0; i < 3; i++) + clamp(t->color[i], 0, 1); } if (flags & RF_TRANSLUCENT) { - color[3] = glr.ent->alpha; + t->color[3] = glr.ent->alpha; } else { - color[3] = 1; + t->color[3] = 1; } } -static void setup_celshading(void) +/* sets t->celscale */ +static void setup_celshading(tess_info_t *t) { float value = Cvar_ClampValue(gl_celshading, 0, 10); vec3_t dir; - celscale = 0; - if (value == 0) return; if (glr.ent->flags & (RF_TRANSLUCENT | RF_SHELL_MASK)) return; - VectorSubtract(origin, glr.fd.vieworg, dir); - celscale = 1.0f - VectorLength(dir) / 700.0f; + VectorSubtract(t->origin, glr.fd.vieworg, dir); + t->celscale = 1.0f - VectorLength(dir) / 700.0f; } -static void draw_celshading(maliasmesh_t *mesh) +static void draw_celshading(const tess_info_t *t, const maliasmesh_t *mesh) { - if (celscale < 0.01f || celscale > 1) + if (t->celscale < 0.01f || t->celscale > 1) return; GL_BindTexture(0, TEXNUM_BLACK); GL_StateBits(GLS_BLEND_BLEND); GL_ArrayBits(GLA_VERTEX); - qglLineWidth(gl_celshading->value * celscale); + qglLineWidth(gl_celshading->value * t->celscale); qglPolygonMode(GL_FRONT_AND_BACK, GL_LINE); qglCullFace(GL_FRONT); - qglColor4f(0, 0, 0, color[3] * celscale); - qglDrawElements(GL_TRIANGLES, mesh->numindices, QGL_INDEX_ENUM, - mesh->indices); + qglColor4f(0, 0, 0, t->color[3] * t->celscale); + qglDrawElements(GL_TRIANGLES, mesh->numindices, + QGL_INDEX_ENUM, mesh->indices); qglCullFace(GL_BACK); qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL); qglLineWidth(1); } -static void setup_shadow(void) +static void setup_shadow(tess_info_t *t) { mat4_t m, tmp; cplane_t *plane; vec3_t dir; - shadowmatrix.i[3][3] = 0; + t->shadowmatrix.i[3][3] = 0; if (!gl_shadows->integer) return; @@ -487,17 +464,17 @@ static void setup_shadow(void) tmp = mul_mat4(&glr.viewmatrix, &m); // rotate for entity - m = AffineMatrix(glr.entaxis, origin); - shadowmatrix = mul_mat4(&tmp, &m); + m = AffineMatrix(glr.entaxis, t->origin); + t->shadowmatrix = mul_mat4(&tmp, &m); } -static void draw_shadow(maliasmesh_t *mesh) +static void draw_shadow(const tess_info_t *t, const maliasmesh_t *mesh) { - if (shadowmatrix.i[3][3] < 0.5f) + if (t->shadowmatrix.i[3][3] < 0.5f) return; // load shadow projection matrix - GL_LoadMatrix(&shadowmatrix); + GL_LoadMatrix(&t->shadowmatrix); // eliminate z-fighting by utilizing stencil buffer, if available if (gl_config.stencilbits) { @@ -512,7 +489,7 @@ static void draw_shadow(maliasmesh_t *mesh) qglEnable(GL_POLYGON_OFFSET_FILL); qglPolygonOffset(-1.0f, -2.0f); - qglColor4f(0, 0, 0, color[3] * 0.5f); + qglColor4f(0, 0, 0, t->color[3] * 0.5f); qglDrawElements(GL_TRIANGLES, mesh->numindices, QGL_INDEX_ENUM, mesh->indices); qglDisable(GL_POLYGON_OFFSET_FILL); @@ -526,7 +503,7 @@ static void draw_shadow(maliasmesh_t *mesh) } } -static int texnum_for_mesh(maliasmesh_t *mesh) +static int texnum_for_mesh(const maliasmesh_t *mesh) { entity_t *ent = glr.ent; @@ -550,14 +527,18 @@ static int texnum_for_mesh(maliasmesh_t *mesh) return mesh->skins[ent->skinnum]->texnum; } -static void draw_alias_mesh(maliasmesh_t *mesh) +typedef void (*tessfunc_t)(const tess_info_t *, const maliasmesh_t *); + +static void draw_alias_mesh(const tess_info_t *t, + tessfunc_t tessfunc, + const maliasmesh_t *mesh) { glStateBits_t state = GLS_DEFAULT; // fall back to entity matrix GL_LoadMatrix(&glr.entmatrix); - if (shadelight) + if (t->shadelight) state |= GLS_SHADE_SMOOTH; if (glr.ent->flags & RF_TRANSLUCENT) @@ -567,27 +548,27 @@ static void draw_alias_mesh(maliasmesh_t *mesh) GL_BindTexture(0, texnum_for_mesh(mesh)); - (*tessfunc)(mesh); + tessfunc(t, mesh); c.trisDrawn += mesh->numtris; - if (shadelight) { + if (t->shadelight) { GL_ArrayBits(GLA_VERTEX | GLA_TC | GLA_COLOR); GL_VertexPointer(3, VERTEX_SIZE, tess.vertices); GL_ColorFloatPointer(4, VERTEX_SIZE, tess.vertices + 4); } else { GL_ArrayBits(GLA_VERTEX | GLA_TC); GL_VertexPointer(3, 4, tess.vertices); - qglColor4fv(color); + qglColor4fv(t->color); } - GL_TexCoordPointer(2, 0, (GLfloat *)mesh->tcoords); + GL_TexCoordPointer(2, 0, (GLfloat *) mesh->tcoords); GL_LockArrays(mesh->numverts); qglDrawElements(GL_TRIANGLES, mesh->numindices, QGL_INDEX_ENUM, mesh->indices); - draw_celshading(mesh); + draw_celshading(t, mesh); if (gl_showtris->integer) { GL_EnableOutlines(); @@ -597,7 +578,7 @@ static void draw_alias_mesh(maliasmesh_t *mesh) } // FIXME: unlock arrays before changing matrix? - draw_shadow(mesh); + draw_shadow(t, mesh); GL_UnlockArrays(); } @@ -605,64 +586,77 @@ static void draw_alias_mesh(maliasmesh_t *mesh) void GL_DrawAliasModel(model_t *model) { entity_t *ent = glr.ent; + tessfunc_t tessfunc; glCullResult_t cull; - int i; - - newframenum = ent->frame; - if (newframenum < 0 || newframenum >= model->numframes) { - Com_DPrintf("%s: no such frame %d\n", __func__, newframenum); - newframenum = 0; + int i, do_lerp; + + tess_info_t t = { + .oldframenum = ent->oldframe, + .newframenum = ent->frame, + .frontlerp = 1.0f - ent->backlerp, + .backlerp = ent->backlerp, + + .shadelight = NULL, + .celscale = 0, + }; + +#if 0 + if (t.newframenum >= model->numframes) { + Com_DPrintf("%s: no such frame %d\n", __func__, t.newframenum); + t.newframenum = 0; } - oldframenum = ent->oldframe; - if (oldframenum < 0 || oldframenum >= model->numframes) { - Com_DPrintf("%s: no such oldframe %d\n", __func__, oldframenum); - oldframenum = 0; + if (t.oldframenum >= model->numframes) { + Com_DPrintf("%s: no such oldframe %d\n", __func__, t.oldframenum); + t.oldframenum = 0; } - - backlerp = ent->backlerp; - frontlerp = 1.0f - backlerp; +#else + assert(t.oldframenum <= model->numframes); + assert(t.newframenum <= model->numframes); +#endif // optimized case - if (backlerp == 0) - oldframenum = newframenum; + if (t.backlerp == 0) + t.oldframenum = t.newframenum; + + do_lerp = t.newframenum != t.oldframenum; // interpolate origin, if necessarry if (ent->flags & RF_FRAMELERP) LerpVector2(ent->oldorigin, ent->origin, - backlerp, frontlerp, origin); + t.backlerp, t.frontlerp, t.origin); else - VectorCopy(ent->origin, origin); + VectorCopy(ent->origin, t.origin); // cull the model, setup scale and translate vectors - if (newframenum == oldframenum) - cull = cull_static_model(model); - else - cull = cull_lerped_model(model); + cull = do_lerp + ? cull_lerped_model(&t, model) + : cull_static_model(&t, model); + if (cull == CULL_OUT) return; + memset(&glr.lightpoint, 0, sizeof(glr.lightpoint)); + // setup parameters common for all meshes - setup_color(); - setup_celshading(); - setup_dotshading(); - setup_shadow(); + setup_color(&t); + setup_celshading(&t); + setup_dotshading(&t); + setup_shadow(&t); // select proper tessfunc if (ent->flags & RF_SHELL_MASK) { - shellscale = (ent->flags & RF_WEAPONMODEL) ? + t.shellscale = (ent->flags & RF_WEAPONMODEL) ? WEAPONSHELL_SCALE : POWERSUIT_SCALE; - tessfunc = newframenum == oldframenum ? - tess_static_shell : tess_lerped_shell; - } else if (shadelight) { - tessfunc = newframenum == oldframenum ? - tess_static_shade : tess_lerped_shade; + + tessfunc = do_lerp ? tess_lerped_shell : tess_static_shell; + } else if (t.shadelight) { + tessfunc = do_lerp ? tess_lerped_shade : tess_static_shade; } else { - tessfunc = newframenum == oldframenum ? - tess_static_plain : tess_lerped_plain; + tessfunc = do_lerp ? tess_lerped_plain : tess_static_plain; } - GL_RotateForEntity(origin); + GL_RotateForEntity(t.origin); if ((ent->flags & (RF_WEAPONMODEL | RF_LEFTHAND)) == (RF_WEAPONMODEL | RF_LEFTHAND)) { @@ -677,7 +671,7 @@ void GL_DrawAliasModel(model_t *model) // draw all the meshes for (i = 0; i < model->nummeshes; i++) - draw_alias_mesh(&model->meshes[i]); + draw_alias_mesh(&t, tessfunc, &model->meshes[i]); if (ent->flags & RF_DEPTHHACK) qglDepthRange(0, 1); diff --git a/src/refresh/gl/surf.c b/src/refresh/gl/surf.c index f36e683..35332f0 100644 --- a/src/refresh/gl/surf.c +++ b/src/refresh/gl/surf.c @@ -585,11 +585,11 @@ static void build_surface_poly(mface_t *surf, vec_t *vbo) tc[0] = DotProduct(vbo, texinfo->axis[0]) + texinfo->offset[0]; tc[1] = DotProduct(vbo, texinfo->axis[1]) + texinfo->offset[1]; - if (mins[0] > tc[0]) mins[0] = tc[0]; - if (maxs[0] < tc[0]) maxs[0] = tc[0]; + mins[0] = min(mins[0], tc[0]); + maxs[0] = max(maxs[0], tc[0]); - if (mins[1] > tc[1]) mins[1] = tc[1]; - if (maxs[1] < tc[1]) maxs[1] = tc[1]; + mins[1] = min(mins[1], tc[1]); + maxs[1] = max(maxs[1], tc[1]); vbo[4] = tc[0] * scale[0]; vbo[5] = tc[1] * scale[1]; diff --git a/src/refresh/gl/world.c b/src/refresh/gl/world.c index 63ff4b8..786500f 100644 --- a/src/refresh/gl/world.c +++ b/src/refresh/gl/world.c @@ -146,38 +146,37 @@ static qboolean _GL_LightPoint(vec3_t start, vec3_t color) static void GL_MarkLights_r(mnode_t *node, dlight_t *light, int lightbit) { vec_t dot; - int count; mface_t *face; - while (node->plane) { - dot = PlaneDiffFast(light->transformed, node->plane); - if (dot > light->intensity - DLIGHT_CUTOFF) { - node = node->children[0]; - continue; - } - if (dot < -light->intensity + DLIGHT_CUTOFF) { - node = node->children[1]; - continue; - } - - face = node->firstface; - count = node->numfaces; - while (count--) { - if (!(face->drawflags & SURF_NOLM_MASK)) { - if (face->dlightframe != glr.dlightframe) { - face->dlightframe = glr.dlightframe; - face->dlightbits = 0; - } + if (!node->plane) + return; - face->dlightbits |= lightbit; - } + dot = PlaneDiffFast(light->transformed, node->plane); - face++; - } + if (dot > light->intensity - DLIGHT_CUTOFF) { + GL_MarkLights_r(node->children[0], light, lightbit); + return; + } - GL_MarkLights_r(node->children[0], light, lightbit); - node = node->children[1]; + if (dot < -light->intensity + DLIGHT_CUTOFF) { + GL_MarkLights_r(node->children[1], light, lightbit); + return; } + + for (face = node->firstface; + face != node->firstface + node->numfaces; + face++) + if (!(face->drawflags & SURF_NOLM_MASK)) { + if (face->dlightframe != glr.dlightframe) { + face->dlightframe = glr.dlightframe; + face->dlightbits = 0; + } + + face->dlightbits |= lightbit; + } + + GL_MarkLights_r(node->children[0], light, lightbit); + GL_MarkLights_r(node->children[1], light, lightbit); } static void GL_MarkLights(void) @@ -408,6 +407,7 @@ void GL_DrawBspModel(mmodel_t *model) last = model->firstface + model->numfaces; for (face = model->firstface; face < last; face++) { dot = PlaneDiffFast(transformed, face->plane); + if (BSP_CullFace(face, dot)) { c.facesCulled++; continue; @@ -528,26 +528,27 @@ static void GL_WorldNode_r(mnode_t *node, int clipflags) int side; vec_t dot; - while (node->visframe == glr.visframe) { - if (!GL_ClipNode(node, &clipflags)) { - c.nodesCulled++; - break; - } + if (node->visframe != glr.visframe) + return; - if (!node->plane) { - GL_DrawLeaf((mleaf_t *)node); - break; - } + if (!GL_ClipNode(node, &clipflags)) { + c.nodesCulled++; + return; + } + + if (!node->plane) { + GL_DrawLeaf((mleaf_t *)node); + return; + } - dot = PlaneDiffFast(glr.fd.vieworg, node->plane); - side = dot < 0; + dot = PlaneDiffFast(glr.fd.vieworg, node->plane); + side = dot < 0; - GL_WorldNode_r(node->children[side], clipflags); + GL_WorldNode_r(node->children[side], clipflags); - GL_DrawNode(node); + GL_DrawNode(node); - node = node->children[side ^ 1]; - } + GL_WorldNode_r(node->children[side ^ 1], clipflags); } void GL_DrawWorld(void) diff --git a/src/shared/matrix.c b/src/shared/matrix.c index 86dffbb..24682b2 100644 --- a/src/shared/matrix.c +++ b/src/shared/matrix.c @@ -37,20 +37,12 @@ void AngleVectors(vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) vec_t VectorNormalize(vec3_t v) { - float length, ilength; - - length = v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; - length = sqrt(length); // FIXME + float length = VectorLength(v); - if (length) { - ilength = 1 / length; - v[0] *= ilength; - v[1] *= ilength; - v[2] *= ilength; - } + if (length) + VectorScale(v, 1 / length, v); return length; - } vec_t VectorNormalize2(vec3_t v, vec3_t out) |