Back to the Vavoom Forum Archives


Forum

Experimental Code ;)

Mon, 19 Apr 2010 23:31:30

Firebrand

It blows! <!-- s:shock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt=":shock:" title="Shocked" /><!-- s:shock: --> This looks really good <!-- s:) --><img src="{SMILIES_PATH}/icon_smile.gif" alt=":)" title="Smile" /><!-- s:) -->, the question I have is, what is your idea for this? To use it with every dynamic light on the maps? Or to implement some kind of ambient light?
Sun, 25 Apr 2010 22:26:42

Firebrand

I'm getting the following errors when starting with shader support enabled:
Log: Sys_Error: Failed to compile shader glshaders/surf_simple.vs: Vertex shader failed to compile with the following errors:
ERROR: 0:16: error(#202) No matching overloaded function found dot
ERROR: 0:17: error(#202) No matching overloaded function found dot
ERROR: error(#273) 2 compilation errors.  No code generated


Log: - VOpenGLDrawer::LoadShader
Log: - VOpenGLDrawer::InitResolution
Log: - CheckResolutionChange
Log: - SCR_Update
Log: - Host_Frame
Log: Doing C_Shutdown
Log: Doing CL_Shutdown
Log: Doing SV_Shutdown
Log: Doing delete GNet
Log: Doing delete GInput
Log: Doing V_Shutdown
Log: Doing delete GAudio
Log: Doing T_Shutdown
Log: Doing Sys_Shutdown
Log: Doing delete GSoundManager
Log: Doing R_ShutdownTexture
Log: Doing R_ShutdownData
Log: Doing VCommand::Shutdown
Log: Doing VCvar::Shutdown
Log: Doing ShutdownMapInfo
Log: Doing FL_Shutdown
Log: Doing W_Shutdown
Log: Doing GLanguage.FreeData
Log: Doing ShutdownDecorate
Log: Doing VObject::StaticExit
Log: Doing VName::StaticExit
Uninitialised: Doing Z_Shutdown


ERROR: Failed to compile shader glshaders/surf_simple.vs: Vertex shader failed to compile with the following errors:
ERROR: 0:16: error(#202) No matching overloaded function found dot
ERROR: 0:17: error(#202) No matching overloaded function found dot
ERROR: error(#273) 2 compilation errors.  No code generated
I've got an ATI Radeon HD 3850 card, I don't know if that has anything to do with it.
Sun, 02 May 2010 18:03:19

Firebrand

I've got a new diff patch which solves the crashes when compiling shaders... it's not the best solution IMO but it works for now.
Index: basev/common/glshaders/surf_lightmap.vs
===================================================================
--- basev/common/glshaders/surf_lightmap.vs	(revision 4223)
+++ basev/common/glshaders/surf_lightmap.vs	(working copy)
@@ -21,8 +21,15 @@
 	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
 
 	//	Calculate texture coordinates.
-	float s = (dot(gl_Vertex, SAxis) + SOffs);
-	float t = (dot(gl_Vertex, TAxis) + TOffs);
+//	float s = (dot(gl_Vertex, SAxis) + SOffs);
+//	float t = (dot(gl_Vertex, TAxis) + TOffs);
+
+	//	Fix for ATI cards that doesn't seem to support the dot function
+	float sdp = gl_Vertex[0] * SAxis[0] + gl_Vertex[1] * SAxis[1] + gl_Vertex[2] * SAxis[2];
+	float tdp = gl_Vertex[0] * TAxis[0] + gl_Vertex[1] * TAxis[1] + gl_Vertex[2] * TAxis[2];
+	float s = (sdp + SOffs);
+	float t = (tdp + TOffs);
+
 	float lights = (s - TexMinS + CacheS * 16 + 8) / (BLOCK_WIDTH * 16);
 	float lightt = (t - TexMinT + CacheT * 16 + 8) / (BLOCK_HEIGHT * 16);
 	TextureCoordinate = vec2(s * TexIW, t * TexIH);
Index: basev/common/glshaders/surf_simple.vs
===================================================================
--- basev/common/glshaders/surf_simple.vs	(revision 4223)
+++ basev/common/glshaders/surf_simple.vs	(working copy)
@@ -13,7 +13,14 @@
 	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
 
 	//	Calculate texture coordinates.
-	float s = (dot(gl_Vertex, SAxis) + SOffs) * TexIW;
-	float t = (dot(gl_Vertex, TAxis) + TOffs) * TexIH;
+//	float s = (dot(gl_Vertex, SAxis) + SOffs) * TexIW;
+//	float t = (dot(gl_Vertex, TAxis) + TOffs) * TexIH;
+
+	//	Fix for ATI cards that doesn't seem to support the dot function
+	float sdp = gl_Vertex[0] * SAxis[0] + gl_Vertex[1] * SAxis[1] + gl_Vertex[2] * SAxis[2];
+	float tdp = gl_Vertex[0] * TAxis[0] + gl_Vertex[1] * TAxis[1] + gl_Vertex[2] * TAxis[2];
+	float s = (sdp + SOffs) * TexIW;
+	float t = (tdp + TOffs) * TexIH;
+
 	TextureCoordinate = vec2(s, t);
 }
Index: source/d3d_local.h
===================================================================
--- source/d3d_local.h	(revision 4223)
+++ source/d3d_local.h	(working copy)
@@ -128,9 +128,12 @@
 	}
 };
 
-class VDirect3DDrawer : public VHardwareDrawer
+class VDirect3DDrawer : public VAdvDrawer
 {
 public:
+	//
+	// VDrawer interface
+	//
 	VDirect3DDrawer();
 	void Init();
 	bool SetResolution(int, int, int, bool);
@@ -147,6 +150,12 @@
 	void SetupView(VRenderLevelDrawer*, const refdef_t*);
 	void SetupViewOrg();
 	void WorldDrawing();
+	void WorldDrawingShaders();
+	void DrawWorldAmbientPass();
+	void BeginLightShadowVolumes();
+	void RenderSurfaceShadowVolume(surface_t *surf, TVec& LightPos, float Radius);
+	void DrawLightShadowsPass(TVec& LightPos, float Radius, vuint32 Colour);
+	void DrawWorldTexturesPass();
 	void EndView();
 
 	//	Texture stuff
Index: source/d3d_poly.cpp
===================================================================
--- source/d3d_poly.cpp	(revision 4223)
+++ source/d3d_poly.cpp	(working copy)
@@ -415,6 +415,639 @@
 
 //==========================================================================
 //
+//	VDirect3DDrawer::WorldDrawingShaders
+//
+//==========================================================================
+
+void VDirect3DDrawer::WorldDrawingShaders()
+{
+/*	guard(VDirect3DDrawer::WorldDrawingShaders);
+	int			lb, i;
+	surfcache_t	*cache;
+	float		s, t;
+	float		lights, lightt;
+	surface_t	*surf;
+	texinfo_t	*tex;
+
+	//	First draw horizons.
+	if (HorizonPortalsHead)
+	{
+		for (surf = HorizonPortalsHead; surf; surf = surf->DrawNext)
+		{
+			DoHorizonPolygon(surf);
+		}
+	}
+
+	//	For sky areas we just write to the depth buffer to prevent drawing
+	// polygons behind the sky.
+	if (SkyPortalsHead)
+	{
+		p_glUseProgramObjectARB(SurfZBufProgram);
+		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+		for (surf = SkyPortalsHead; surf; surf = surf->DrawNext)
+		{
+			glBegin(GL_POLYGON);
+			for (i = 0; i < surf->count; i++)
+			{
+				glVertex(surf->verts);
+			}
+			glEnd();
+		}
+		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	}
+
+	//	Draw surfaces.
+	if (SimpleSurfsHead)
+	{
+		p_glUseProgramObjectARB(SurfSimpleProgram);
+		GLint SAxisLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "SAxis");
+		GLint TAxisLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "TAxis");
+		GLint SOffsLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "SOffs");
+		GLint TOffsLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "TOffs");
+		GLint TexIWLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "TexIW");
+		GLint TexIHLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "TexIH");
+		GLint TextureLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "Texture");
+		GLint LightLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "Light");
+		GLint FogEnabledLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "FogEnabled");
+		GLint FogTypeLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "FogType");
+		GLint FogColourLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "FogColour");
+		GLint FogDensityLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "FogDensity");
+		GLint FogStartLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "FogStart");
+		GLint FogEndLoc = p_glGetUniformLocationARB(SurfSimpleProgram, "FogEnd");
+		p_glUniform1iARB(TextureLoc, 0);
+		p_glUniform1iARB(FogTypeLoc, r_fog & 3);
+
+		for (surf = SimpleSurfsHead; surf; surf = surf->DrawNext)
+		{
+			texinfo_t* tex = surf->texinfo;
+			SetTexture(tex->Tex, tex->ColourMap);
+			p_glUniform3fvARB(SAxisLoc, 1, &tex->saxis.x);
+			p_glUniform1fARB(SOffsLoc, tex->soffs);
+			p_glUniform1fARB(TexIWLoc, tex_iw);
+			p_glUniform3fvARB(TAxisLoc, 1, &tex->taxis.x);
+			p_glUniform1fARB(TOffsLoc, tex->toffs);
+			p_glUniform1fARB(TexIHLoc, tex_ih);
+
+			float lev = float(surf->Light >> 24) / 255.0;
+			p_glUniform4fARB(LightLoc,
+				((surf->Light >> 16) & 255) * lev / 255.0,
+				((surf->Light >> 8) & 255) * lev / 255.0,
+				(surf->Light & 255) * lev / 255.0, 1.0);
+			if (surf->Fade)
+			{
+				p_glUniform1iARB(FogEnabledLoc, GL_TRUE);
+				p_glUniform4fARB(FogColourLoc,
+					((surf->Fade >> 16) & 255) / 255.0,
+					((surf->Fade >> 8) & 255) / 255.0,
+					(surf->Fade & 255) / 255.0, 1.0);
+				p_glUniform1fARB(FogDensityLoc, surf->Fade == FADE_LIGHT ? 0.3 : r_fog_density);
+				p_glUniform1fARB(FogStartLoc, surf->Fade == FADE_LIGHT ? 1.0 : r_fog_start);
+				p_glUniform1fARB(FogEndLoc, surf->Fade == FADE_LIGHT ? 1024.0 * r_fade_factor : r_fog_end);
+			}
+			else
+			{
+				p_glUniform1iARB(FogEnabledLoc, GL_FALSE);
+			}
+
+			glBegin(GL_POLYGON);
+			for (i = 0; i < surf->count; i++)
+			{
+				glVertex(surf->verts);
+			}
+			glEnd();
+		}
+	}
+
+	p_glUseProgramObjectARB(SurfLightmapProgram);
+	GLint SAxisLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "SAxis");
+	GLint TAxisLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "TAxis");
+	GLint SOffsLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "SOffs");
+	GLint TOffsLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "TOffs");
+	GLint TexIWLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "TexIW");
+	GLint TexIHLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "TexIH");
+	GLint TexMinSLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "TexMinS");
+	GLint TexMinTLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "TexMinT");
+	GLint CacheSLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "CacheS");
+	GLint CacheTLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "CacheT");
+	GLint TextureLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "Texture");
+	GLint LightMapLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "LightMap");
+	GLint SpecularMapLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "SpecularMap");
+	GLint FogEnabledLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "FogEnabled");
+	GLint FogTypeLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "FogType");
+	GLint FogColourLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "FogColour");
+	GLint FogDensityLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "FogDensity");
+	GLint FogStartLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "FogStart");
+	GLint FogEndLoc = p_glGetUniformLocationARB(SurfLightmapProgram, "FogEnd");
+	p_glUniform1iARB(TextureLoc, 0);
+	p_glUniform1iARB(LightMapLoc, 1);
+	p_glUniform1iARB(SpecularMapLoc, 2);
+	p_glUniform1iARB(FogTypeLoc, r_fog & 3);
+
+	for (lb = 0; lb < NUM_BLOCK_SURFS; lb++)
+	{
+		if (!light_chain[lb])
+		{
+			continue;
+		}
+
+		SelectTexture(1);
+		glBindTexture(GL_TEXTURE_2D, lmap_id[lb]);
+		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+		if (block_changed[lb])
+		{
+			block_changed[lb] = false;
+			glTexImage2D(GL_TEXTURE_2D, 0, 4, BLOCK_WIDTH, BLOCK_HEIGHT,
+				0, GL_RGBA, GL_UNSIGNED_BYTE, light_block[lb]);
+			add_changed[lb] = true;
+		}
+
+		SelectTexture(2);
+		glBindTexture(GL_TEXTURE_2D, addmap_id[lb]);
+		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+		if (add_changed[lb])
+		{
+			add_changed[lb] = false;
+			glTexImage2D(GL_TEXTURE_2D, 0, 4, BLOCK_WIDTH, BLOCK_HEIGHT,
+				0, GL_RGBA, GL_UNSIGNED_BYTE, add_block[lb]);
+		}
+
+		SelectTexture(0);
+
+		for (cache = light_chain[lb]; cache; cache = cache->chain)
+		{
+			surf = cache->surf;
+			tex = surf->texinfo;
+			SetTexture(tex->Tex, tex->ColourMap);
+
+			p_glUniform3fvARB(SAxisLoc, 1, &tex->saxis.x);
+			p_glUniform1fARB(SOffsLoc, tex->soffs);
+			p_glUniform1fARB(TexIWLoc, tex_iw);
+			p_glUniform3fvARB(TAxisLoc, 1, &tex->taxis.x);
+			p_glUniform1fARB(TOffsLoc, tex->toffs);
+			p_glUniform1fARB(TexIHLoc, tex_ih);
+			p_glUniform1fARB(TexMinSLoc, surf->texturemins[0]);
+			p_glUniform1fARB(TexMinTLoc, surf->texturemins[1]);
+			p_glUniform1fARB(CacheSLoc, cache->s);
+			p_glUniform1fARB(CacheTLoc, cache->t);
+
+			if (surf->Fade)
+			{
+				p_glUniform1iARB(FogEnabledLoc, GL_TRUE);
+				p_glUniform4fARB(FogColourLoc,
+					((surf->Fade >> 16) & 255) / 255.0,
+					((surf->Fade >> 8) & 255) / 255.0,
+					(surf->Fade & 255) / 255.0, 1.0);
+				p_glUniform1fARB(FogDensityLoc, surf->Fade == FADE_LIGHT ? 0.3 : r_fog_density);
+				p_glUniform1fARB(FogStartLoc, surf->Fade == FADE_LIGHT ? 1.0 : r_fog_start);
+				p_glUniform1fARB(FogEndLoc, surf->Fade == FADE_LIGHT ? 1024.0 * r_fade_factor : r_fog_end);
+			}
+			else
+			{
+				p_glUniform1iARB(FogEnabledLoc, GL_FALSE);
+			}
+
+			glBegin(GL_POLYGON);
+			for (i = 0; i < surf->count; i++)
+			{
+				s = DotProduct(surf->verts, tex->saxis) + tex->soffs;
+				t = DotProduct(surf->verts, tex->taxis) + tex->toffs;
+				lights = (s - surf->texturemins[0] +
+					cache->s * 16 + 8) / (BLOCK_WIDTH * 16);
+				lightt = (t - surf->texturemins[1] +
+					cache->t * 16 + 8) / (BLOCK_HEIGHT * 16);
+				MultiTexCoord(0, s * tex_iw, t * tex_ih);
+				MultiTexCoord(1, lights, lightt);
+				glVertex(surf->verts);
+			}
+			glEnd();
+		}
+	}
+
+	p_glUseProgramObjectARB(0);
+	unguard;*/
+}
+
+//==========================================================================
+//
+//	VDirect3DDrawer::DrawWorldAmbientPass
+//
+//==========================================================================
+
+void VDirect3DDrawer::DrawWorldAmbientPass()
+{
+	guard(VDirect3DDrawer::DrawWorldAmbientPass);
+	int			lb, i;
+	surfcache_t	*cache;
+	surface_t	*surf;
+	float		s, t, lights, lightt;
+	texinfo_t	*tex;
+	MyD3DVertex	out[256];
+
+//	p_glUseProgramObjectARB(0);
+	SetFade(0);
+	RenderDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
+
+	if (SkyPortalsHead)
+	{
+		RenderDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
+
+		for (surf = SkyPortalsHead; surf; surf = surf->DrawNext)
+		{
+			for (i = 0; i < surf->count; i++)
+			{
+				out = MyD3DVertex(surf->verts, 0, 0, 0);
+			}
+			RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+		}
+		
+		RenderDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0x0f);
+	}
+
+	if (SimpleSurfsHead)
+	{
+		for (surf = SimpleSurfsHead; surf; surf = surf->DrawNext)
+		{
+			texinfo_t *tex = surf->texinfo;
+			vuint32 light;
+			SetTexture(tex->Tex, tex->ColourMap);
+
+/*			if (surf->lightmap != NULL ||
+				surf->dlightframe == r_dlightframecount)
+			{
+				light = 0xffffffff;
+			}
+			else
+			{*/
+				int lev = surf->Light >> 24 / 255;
+				int lR = ((surf->Light >> 16) & 255) * lev / 255;
+				int lG = ((surf->Light >> 8) & 255) * lev / 255;
+				int lB = (surf->Light & 255) * lev / 255;
+				light = 0xff000000 | (lR << 16) | (lG << 8) | lB;
+//			}
+//			SetFade(surf->Fade);
+
+			for (i = 0; i < surf->count; i++)
+			{
+				out = MyD3DVertex(surf->verts, light,
+					(DotProduct(surf->verts, tex->saxis) + tex->soffs) * tex_iw,
+					(DotProduct(surf->verts, tex->taxis) + tex->toffs) * tex_ih);
+			}
+
+			RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+		}
+	}
+	RenderDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
+
+	for (lb = 0; lb < NUM_BLOCK_SURFS; lb++)
+	{
+		if (!light_chain[lb])
+		{
+			continue;
+		}
+
+		if (!light_surf[lb])
+		{
+			light_surf[lb] = CreateSurface(BLOCK_WIDTH, BLOCK_HEIGHT, 16, false);
+			block_changed[lb] = true;
+		}
+		if (block_changed[lb])
+		{
+			block_changed[lb] = false;
+			UploadTextureImage(light_surf[lb], 0, BLOCK_WIDTH, BLOCK_HEIGHT, light_block[lb]);
+		}
+
+		RenderDevice->SetTexture(0, light_surf[lb]);
+
+		for (cache = light_chain[lb]; cache; cache = cache->chain)
+		{
+			surf = cache->surf;
+			tex = surf->texinfo;
+//			SetFade(surf->Fade);
+			for (i = 0; i < surf->count; i++)
+			{
+				s = DotProduct(surf->verts, tex->saxis) + tex->soffs;
+				t = DotProduct(surf->verts, tex->taxis) + tex->toffs;
+				lights = (s - surf->texturemins[0]) / 16 + cache->s + 0.5;
+				lightt = (t - surf->texturemins[1]) / 16 + cache->t + 0.5;
+				out = MyD3DVertex(surf->verts, 0xffffffff,
+					lights / BLOCK_WIDTH, lightt / BLOCK_HEIGHT);
+			}
+			RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+		}
+	}
+
+	//	Set up for shadow volume rendering.
+	RenderDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
+	RenderDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
+	unguard;
+}
+
+//==========================================================================
+//
+//	VDirect3DDrawer::BeginLightShadowVolumes
+//
+//==========================================================================
+
+void VDirect3DDrawer::BeginLightShadowVolumes()
+{
+	guard(VDirect3DDrawer::BeginLightShadowVolumes);
+	RenderDevice->Clear(0, NULL, D3DCLEAR_STENCIL, 0, 1.0, 0);
+	RenderDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
+	RenderDevice->SetRenderState(D3DRS_DEPTHBIAS, 1.0);
+
+	RenderDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
+	RenderDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
+	RenderDevice->SetRenderState(D3DRS_STENCILREF, 0x0);
+	RenderDevice->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
+	RenderDevice->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
+	RenderDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
+	RenderDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
+	RenderDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_INCR);
+	RenderDevice->SetRenderState(D3DRS_CCW_STENCILZFAIL, D3DSTENCILOP_KEEP);
+	RenderDevice->SetRenderState(D3DRS_CCW_STENCILFAIL, D3DSTENCILOP_KEEP);
+	RenderDevice->SetRenderState(D3DRS_CCW_STENCILPASS, D3DSTENCILOP_DECR);
+	unguard;
+}
+
+//==========================================================================
+//
+//	VDirect3DDrawer::RenderSurfaceShadowVolume
+//
+//==========================================================================
+
+#define M_INFINITY  2048
+
+void VDirect3DDrawer::RenderSurfaceShadowVolume(surface_t *surf, TVec& LightPos, float Radius)
+{
+	guard(VDirect3DDrawer::RenderSurfaceShadowVolume);
+	MyD3DVertex	out[256];
+	float dist = DotProduct(LightPos, surf->plane->normal) - surf->plane->dist;
+
+	if (dist <= 0)
+	{
+		//	Viewer is in back side or on plane
+		return;
+	}
+
+	int i;
+	TArray<TVec>    v;
+	v.SetNum(surf->count);
+
+	for (int i = 0; i < surf->count; i++)
+	{
+		v = Normalise(surf->verts - LightPos);
+		v *= M_INFINITY;
+		v += LightPos;
+	}
+
+	for (i = 0; i < surf->count; i++)
+	{
+		out = MyD3DVertex(v, 0, 0, 0);
+	}
+	RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+
+	for (i = 0; i < surf->count; i++)
+	{
+		out = MyD3DVertex(surf->verts, 0, 0, 0);
+	}
+	RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+
+	for (i = 0; i < surf->count; i++)
+	{
+		out = MyD3DVertex(surf->verts, 0, 0, 0);
+	}
+	RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, surf->count - 2, out, sizeof(MyD3DVertex));
+
+	for (i = 0; i < surf->count; i++)
+	{
+		out = MyD3DVertex(v, 0, 0, 0);
+	}
+	RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, surf->count - 2, out, sizeof(MyD3DVertex));
+
+	out[0] = MyD3DVertex(surf->verts[0], 0, 0, 0);
+	RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, surf->count - 2, out, sizeof(MyD3DVertex));
+
+	out[0] = MyD3DVertex(v[0], 0, 0, 0);
+	RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, surf->count - 2, out, sizeof(MyD3DVertex));
+	unguard;
+}
+
+//==========================================================================
+//
+//	VDirect3DDrawer::DrawLightShadowsPass
+//
+//==========================================================================
+
+void VDirect3DDrawer::DrawLightShadowsPass(TVec& LightPos, float Radius, vuint32 Colour)
+{
+	guard(VDirect3DDrawer::DrawLightShadowsPass);
+	int			lb, i;
+	surfcache_t	*cache;
+	surface_t	*surf;
+	texinfo_t	*tex;
+	float		s, t, lights, lightt;
+	MyD3DVertex	out[256];
+
+	RenderDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0x0f);
+	RenderDevice->SetRenderState(D3DRS_DEPTHBIAS, 0.0);
+
+	RenderDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
+	RenderDevice->SetRenderState(D3DRS_STENCILREF, 0x0);
+	RenderDevice->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
+	RenderDevice->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
+	RenderDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
+	RenderDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
+	RenderDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
+
+	RenderDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
+	RenderDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
+	RenderDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
+	RenderDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
+	RenderDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
+
+	float r = ((Colour >> 16) & 255) / 255.0;
+	float g = ((Colour >> 8) & 255) / 255.0;
+	float b = (Colour & 255) / 255.0;
+
+	//	Draw surfaces.
+	if (SimpleSurfsHead)
+	{
+		for (surf = SimpleSurfsHead; surf; surf = surf->DrawNext)
+		{
+			texinfo_t *tex = surf->texinfo;
+			SetTexture(tex->Tex, tex->ColourMap);
+//			SetFade(surf->Fade);
+
+			for (i = 0; i < surf->count; i++)
+			{
+				float Col = (Radius - Length(surf->verts - LightPos)) / Radius;
+				if (Col < 0)
+					Col = 0;
+
+				int R = r * Col;
+				int G = g * Col;
+				int B = b * Col;
+				vuint32 colour = 0xff000000 | (R << 16) | (G << 8) | B;
+
+				out = MyD3DVertex(surf->verts, colour,
+					(DotProduct(surf->verts, tex->saxis) + tex->soffs) * tex_iw,
+					(DotProduct(surf->verts, tex->taxis) + tex->toffs) * tex_ih);
+			}
+
+			RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+		}
+	}
+
+	for (lb = 0; lb < NUM_BLOCK_SURFS; lb++)
+	{
+		if (!light_chain[lb])
+		{
+			continue;
+		}
+
+		for (cache = light_chain[lb]; cache; cache = cache->chain)
+		{
+			surf = cache->surf;
+			tex = surf->texinfo;
+			float Col = (Radius - Length(surf->verts - LightPos)) / Radius;
+			if (Col < 0)
+				Col = 0;
+
+			int R = r * Col;
+			int G = g * Col;
+			int B = b * Col;
+			vuint32 colour = 0xff000000 | (R << 16) | (G << 8) | B;
+
+			s = DotProduct(surf->verts, tex->saxis) + tex->soffs;
+			t = DotProduct(surf->verts, tex->taxis) + tex->toffs;
+			lights = (s - surf->texturemins[0]) / 16 + cache->s + 0.5;
+			lightt = (t - surf->texturemins[1]) / 16 + cache->t + 0.5;
+			out = MyD3DVertex(surf->verts, colour,
+				lights / BLOCK_WIDTH, lightt / BLOCK_HEIGHT);
+
+			RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+		}
+	}
+	RenderDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
+	RenderDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
+	RenderDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
+	RenderDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
+	RenderDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
+	unguard;
+}
+
+//==========================================================================
+//
+//	VDirect3DDrawer::DrawWorldTexturesPass
+//
+//==========================================================================
+
+void VDirect3DDrawer::DrawWorldTexturesPass()
+{
+	guard(VDirect3DDrawer::DrawWorldTexturesPass);
+	int			lb, i;
+	surfcache_t	*cache;
+	surface_t	*surf;
+	float		s, t, lights, lightt;
+	texinfo_t	*tex;
+//	vuint32 light;
+	MyD3DVertex	out[256];
+
+	//	Stop stenciling now.
+	RenderDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
+
+	RenderDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
+	RenderDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
+	RenderDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
+	RenderDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0x0f);
+
+	//	Draw surfaces.
+	if (SimpleSurfsHead)
+	{
+		for (surf = SimpleSurfsHead; surf; surf = surf->DrawNext)
+		{
+			texinfo_t *tex = surf->texinfo;
+			SetTexture(tex->Tex, tex->ColourMap);
+
+/*			if (surf->lightmap != NULL ||
+				surf->dlightframe == r_dlightframecount)
+			{
+				light = 0xffffffff;
+			}
+			else
+			{
+				int lev = surf->Light >> 24 / 255;
+				int lR = ((surf->Light >> 16) & 255) * lev / 255;
+				int lG = ((surf->Light >> 8) & 255) * lev / 255;
+				int lB = (surf->Light & 255) * lev / 255;
+				vuint32 light = 0xff000000 | (lR << 16) | (lG << 8) | lB;
+			}
+			SetFade(surf->Fade);*/
+
+			for (i = 0; i < surf->count; i++)
+			{
+				out = MyD3DVertex(surf->verts, 0xffffffff,
+					(DotProduct(surf->verts, tex->saxis) + tex->soffs) * tex_iw,
+					(DotProduct(surf->verts, tex->taxis) + tex->toffs) * tex_ih);
+			}
+
+			RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+		}
+	}
+
+	for (lb = 0; lb < NUM_BLOCK_SURFS; lb++)
+	{
+		if (!light_chain[lb])
+		{
+			continue;
+		}
+
+		if (!light_surf[lb])
+		{
+			light_surf[lb] = CreateSurface(BLOCK_WIDTH, BLOCK_HEIGHT, 16, false);
+			block_changed[lb] = true;
+		}
+		if (block_changed[lb])
+		{
+			block_changed[lb] = false;
+			UploadTextureImage(light_surf[lb], 0, BLOCK_WIDTH, BLOCK_HEIGHT, light_block[lb]);
+		}
+
+		RenderDevice->SetTexture(0, light_surf[lb]);
+
+		for (cache = light_chain[lb]; cache; cache = cache->chain)
+		{
+			surf = cache->surf;
+			tex = surf->texinfo;
+//			SetFade(surf->Fade);
+			for (i = 0; i < surf->count; i++)
+			{
+				s = DotProduct(surf->verts, tex->saxis) + tex->soffs;
+				t = DotProduct(surf->verts, tex->taxis) + tex->toffs;
+				lights = (s - surf->texturemins[0]) / 16 + cache->s + 0.5;
+				lightt = (t - surf->texturemins[1]) / 16 + cache->t + 0.5;
+				out = MyD3DVertex(surf->verts, 0xffffffff,
+					lights / BLOCK_WIDTH, lightt / BLOCK_HEIGHT);
+			}
+			RenderDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, surf->count - 2, out, sizeof(MyD3DVertex));
+		}
+	}
+
+    RenderDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
+    RenderDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
+	RenderDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
+
+
+	//	Back to normal z-buffering.
+	RenderDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
+	RenderDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
+	unguard;
+}
+
+//==========================================================================
+//
 //	VDirect3DDrawer::DoHorizonPolygon
 //
 //==========================================================================
I've also began implementing the shadowed lights in Direct3d, I'll try to get the shaders coded for it, but I don't know if I will succeed on the task.
Sat, 08 May 2010 09:19:21

Janis Legzdinsh

I'm getting the following errors when starting with shader support enabled:
I think I know what the problem is. I commited changes, check them out and let me know if it fixes the problem. Direct3D code looks good. if it works it's OK to commit.
It blows! <!-- s:shock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt=":shock:" title="Shocked" /><!-- s:shock: --> This looks really good <!-- s:) --><img src="{SMILIES_PATH}/icon_smile.gif" alt=":)" title="Smile" /><!-- s:) -->, the question I have is, what is your idea for this? To use it with every dynamic light on the maps? Or to implement some kind of ambient light?
It will be used by advanced level renderer class which I now added for both static and dynamic lights. Models will cast shadows as well.
Sat, 08 May 2010 16:04:47

Firebrand

Yes, your fixes for the shader errors were successful, I've got another error in shader files, here's the patch to fix that one:
Index: shadows_light.vs
===================================================================
--- shadows_light.vs	(revision 4230)
+++ shadows_light.vs	(working copy)
@@ -15,7 +15,7 @@
 
 	Normal = SurfNormal; 
 	Dist = dot(LightPos, SurfNormal) - SurfDist;
-	VertToLight = LightPos - gl_Vertex;
+	VertToLight.xyz = LightPos.xyz - gl_Vertex.xyz;
 
 	AlphaVal = (LightRadius - length(gl_Vertex.xyz - LightPos)) / LightRadius;
 }
I haven't had the time to properly test the Direct3d code, but I will work on it during this weekend <!-- s:) --><img src="{SMILIES_PATH}/icon_smile.gif" alt=":)" title="Smile" /><!-- s:) -->. Nice work so far with the advanced renderer! <!-- s:twisted: --><img src="{SMILIES_PATH}/icon_twisted.gif" alt=":twisted:" title="Twisted Evil" /><!-- s:twisted: -->
Wed, 02 Jun 2010 12:57:43

Firebrand

I've been thinking on the performance of the new OpenGL renderer, right now the performance is good in conservative maps and without models, I was thinking, how about drawing shadows/lights on models on odd/even frames (i.e. first do light pass on odd frames, then do shadow pass on even frames)? Would it really give a small performance boost or would it make things slower or maybe not gain anything from it?
Thu, 03 Jun 2010 06:22:05

Janis Legzdinsh

It doesn't work that way. You have to do light and shadow pass for every light source.

Back to the Vavoom Forum Archives