shithub: qk1

Download patch

ref: cb737961ade42b8d6668ff5d548b85a36fe48506
parent: 645a201fa018760137aecadb2ab4a7f779a566c8
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Thu Jan 11 10:15:53 EST 2024

calculate mip level when surface is added

--- a/d_edge.c
+++ b/d_edge.c
@@ -1,27 +1,8 @@
 #include "quakedef.h"
 
-float scale_for_mip;
-
 // FIXME: should go away
 void R_RotateBmodel(entity_t *entity, view_t *v);
 
-static int
-D_MipLevelForScale(float scale)
-{
-	int lmiplevel;
-
-	if(scale >= d_scalemip[0])
-		lmiplevel = 0;
-	else if(scale >= d_scalemip[1])
-		lmiplevel = 1;
-	else if(scale >= d_scalemip[2])
-		lmiplevel = 2;
-	else
-		lmiplevel = 3;
-
-	return max(d_minmip, lmiplevel);
-}
-
 static void
 D_DrawSolidSurface(surf_t *surf, pixel_t color)
 {
@@ -87,7 +68,6 @@
 	surfcache_t *pcurrentcache;
 	drawsurf_t ds = {0};
 	msurface_t *pface;
-	int miplevel;
 	entity_t *e;
 	texvars_t t;
 	surf_t *s;
@@ -136,14 +116,10 @@
 			D_CalcGradients(0, pface, transformed_modelorg, &v, &t);
 			D_DrawSpans(s->spans, &t, alpha, SPAN_TURB);
 		}else{
-			miplevel = D_MipLevelForScale(s->nearzi * scale_for_mip * pface->texinfo->mipadjust);
-			if(s->flags & SURF_FENCE)
-				miplevel = max(miplevel-1, 0);
-
-			pcurrentcache = D_CacheSurface(s->entity, pface, &ds, miplevel);
+			pcurrentcache = D_CacheSurface(s->entity, pface, &ds, s->miplevel);
 			t.p = pcurrentcache->pixels;
 			t.w = pcurrentcache->width;
-			D_CalcGradients(miplevel, pface, transformed_modelorg, &v, &t);
+			D_CalcGradients(s->miplevel, pface, transformed_modelorg, &v, &t);
 			blend = (s->flags & SURF_FENCE) || (r_drawflags & DRAW_BLEND);
 			D_DrawSpans(s->spans, &t, alpha,
 				(alpha == 255 && (s->flags & SURF_FENCE))
--- a/d_local.h
+++ b/d_local.h
@@ -63,8 +63,6 @@
 
 extern cvar_t	d_subdiv16;
 
-extern float	scale_for_mip;
-
 extern bool		d_roverwrapped;
 extern surfcache_t	*sc_rover;
 extern surfcache_t	*d_initial_rover;
--- a/model.h
+++ b/model.h
@@ -62,21 +62,21 @@
 } texture_t;
 
 enum {
-	SURF_PLANEBACK = 1<<1,
-	SURF_DRAWSKY = 1<<2,
-	SURF_DRAWSPRITE = 1<<3,
-	SURF_DRAWTURB = 1<<4,
-	SURF_DRAWTILED = 1<<5,
-	SURF_DRAWBACKGROUND = 1<<6,
-	SURF_TRANS = 1<<8,
-	SURF_FENCE = 1<<9,
-	SURF_LAVA = 1<<10,
-	SURF_SLIME = 1<<11,
-	SURF_TELE = 1<<12,
-	SURF_IN_SUBMODEL = 1<<31, /* makes surf->flags negative */
+	SURF_PLANEBACK = 1<<0,
+	SURF_DRAWSKY = 1<<1,
+	SURF_DRAWSPRITE = 1<<2,
+	SURF_DRAWTURB = 1<<3,
+	SURF_DRAWTILED = 1<<4,
+	SURF_DRAWBACKGROUND = 1<<5,
+	SURF_TRANS = 1<<6,
+	SURF_FENCE = 1<<7,
+	SURF_LAVA = 1<<8,
+	SURF_SLIME = 1<<9,
+	SURF_TELE = 1<<10,
+	SURF_IN_SUBMODEL = 1<<11,
 };
 
-#define insubmodel(s) ((s)->flags < 0)
+#define insubmodel(s) ((s)->flags & SURF_IN_SUBMODEL)
 
 // !!! if this is changed, it must be changed in asm_draw.h too !!!
 typedef struct
--- a/r_draw.c
+++ b/r_draw.c
@@ -8,6 +8,8 @@
 
 static unsigned int cacheoffset;
 
+float scale_for_mip;
+
 int r_drawflags;
 
 static medge_t			*r_pedge;
@@ -326,6 +328,23 @@
 
 float alphafor(int flags);
 
+static int
+R_MipLevelForScale(float scale)
+{
+	int lmiplevel;
+
+	if(scale >= d_scalemip[0])
+		lmiplevel = 0;
+	else if(scale >= d_scalemip[1])
+		lmiplevel = 1;
+	else if(scale >= d_scalemip[2])
+		lmiplevel = 2;
+	else
+		lmiplevel = 3;
+
+	return max(d_minmip, lmiplevel);
+}
+
 /*
 ================
 R_RenderFace
@@ -490,8 +509,10 @@
 	if (!r_emitted)
 		return 1;
 
+	surface_p->miplevel = R_MipLevelForScale(r_nearzi * scale_for_mip * fa->texinfo->mipadjust);
+	if(fa->flags & SURF_FENCE)
+		surface_p->miplevel = max(surface_p->miplevel-1, 0);
 	surface_p->data = (void *)fa;
-	surface_p->nearzi = r_nearzi;
 	surface_p->flags = fa->flags | (currententity == cl_entities ? 0 : SURF_IN_SUBMODEL);
 	surface_p->spanstate = 0;
 	surface_p->entity = currententity;
@@ -602,8 +623,10 @@
 	if (!r_emitted)
 		return;
 
+	surface_p->miplevel = R_MipLevelForScale(r_nearzi * scale_for_mip * psurf->texinfo->mipadjust);
+	if(psurf->flags & SURF_FENCE)
+		surface_p->miplevel = max(surface_p->miplevel-1, 0);
 	surface_p->data = (void *)psurf;
-	surface_p->nearzi = r_nearzi;
 	surface_p->flags = psurf->flags | SURF_IN_SUBMODEL;
 	surface_p->spanstate = 0;
 	surface_p->entity = currententity;
--- a/r_local.h
+++ b/r_local.h
@@ -82,6 +82,8 @@
 
 extern	int		r_visframecount;
 
+extern float	scale_for_mip;
+
 //=============================================================================
 
 extern int r_drawflags;
--- a/r_shared.h
+++ b/r_shared.h
@@ -71,8 +71,8 @@
 	struct espan_s	*spans;			// pointer to linked list of spans to draw
 	void		*data;				// associated data like msurface_t
 	entity_t	*entity;
-	int			flags;
-	float		nearzi;				// nearest 1/z on surface, for mipmapping
+	u16int		flags;
+	s16int		miplevel;
 	float		d_ziorigin;
 	float		d_zistepu;
 	float		d_zistepv;