shithub: qk1

Download patch

ref: 0c16c09f75dd3102943fca15306550199f6940d0
parent: 7c5df82b538cfb1926274d676464f4f3854b519d
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sat Dec 16 16:53:10 EST 2023

horribly slow fog + skyfog

--- a/Makefile
+++ b/Makefile
@@ -63,6 +63,7 @@
 	r_draw.o\
 	r_edge.o\
 	r_efrag.o\
+	r_fog.o\
 	r_light.o\
 	r_main.o\
 	r_misc.o\
--- a/mkfile
+++ b/mkfile
@@ -63,6 +63,7 @@
 	r_draw.$O\
 	r_efrag.$O\
 	r_edge.$O\
+	r_fog.$O\
 	r_light.$O\
 	r_main.$O\
 	r_misc.$O\
--- /dev/null
+++ b/r_fog.c
@@ -1,0 +1,80 @@
+#include "quakedef.h"
+
+static cvar_t r_skyfog = {"r_skyfog", "0.5"};
+
+static struct {
+	float density;
+	pixel_t color;
+}r_fog;
+
+#ifdef __plan9__
+static double ln2c;
+#define exp2(x) (exp((x) * (ln2c ? ln2c : (ln2c = log(2.0)))))
+#endif
+
+static void
+fog(void)
+{
+	int i, j, n;
+	float x;
+
+	i = 1;
+	n = Cmd_Argc();
+	switch(n){
+	case 5:
+	case 2:
+		x = atof(Cmd_Argv(i++));
+		r_fog.density = max(0.0, x) * 0.016;
+		r_fog.density *= r_fog.density;
+		if(n == 2)
+			break;
+	case 4:
+		r_fog.color = 0;
+		for(j = 0; j < 3; j++, i++){
+			x = atof(Cmd_Argv(i));
+			r_fog.color = r_fog.color << 8 | (int)(0xff * clamp(x, 0.0, 1.0));
+		}
+		break;
+	}
+}
+
+void
+R_DrawFog(void)
+{
+	byte skyfogalpha, a;
+	pixel_t *pix;
+	int i, x, y;
+	uzint *z;
+	float d;
+
+	if(r_fog.density <= 0.0)
+		return;
+
+	skyfogalpha = 255 * clamp(r_skyfog.value, 0, 1.0);
+	/* FIXME(sigrid): this is super slow */
+	for(y = r_refdef.vrect.y; y < r_refdef.vrectbottom; y++){
+		i = y * vid.width + r_refdef.vrect.x;
+		pix = vid.buffer + i;
+		z = d_pzbuffer + i;
+		for(x = r_refdef.vrect.x; x < r_refdef.vrectright; x++, i++, pix++, z++){
+			if(*z >= 65536){
+				d = 65536.0 / (float)(d_pzbuffer[i] >> 16);
+				d = 1.0 - exp2(-r_fog.density * d*d);
+				a = 255 * clamp(d, 0.0, 1.0);
+			}else if(*z < 0){
+				a = skyfogalpha;
+			}else
+				continue;
+
+			if(a > 0)
+				*pix = blendalpha(r_fog.color, *pix, a);
+		}
+	}
+}
+
+void
+R_InitFog(void)
+{
+	Cmd_AddCommand("fog", fog);
+	Cvar_RegisterVariable(&r_skyfog);
+}
--- a/r_local.h
+++ b/r_local.h
@@ -101,9 +101,8 @@
 
 extern int r_drawflags;
 
-
-void R_ClearPolyList (void);
-void R_DrawPolyList (void);
+void R_DrawFog(void);
+void R_InitFog(void);
 
 //
 // current entity info
--- a/r_main.c
+++ b/r_main.c
@@ -128,12 +128,6 @@
 	}
 }
 
-static void
-fog(void)
-{
-	// FIXME - this was added early to shut up the spam
-}
-
 /*
 ===============
 R_Init
@@ -141,10 +135,10 @@
 */
 void R_Init (void)
 {
-	R_InitTurb ();
+	R_InitTurb();
+	R_InitFog();
 
 	Cmd_AddCommand("pointfile", loadpoints);
-	Cmd_AddCommand("fog", fog);
 
 	Cvar_RegisterVariable (&r_ambient);
 	Cvar_RegisterVariable (&r_clearcolor);
@@ -736,6 +730,8 @@
 			R_DrawEntity(cl_visedicts[i]);
 	}
 	r_drawflags = 0;
+
+	R_DrawFog();
 
 	if (r_dowarp)
 		D_WarpScreen ();