void renderCones(Storm3D_Scene &scene, bool renderShadows, float timeFactor, bool renderGlows)
	{
		// this draws spotlight cone
		frozenbyte::storm::PixelShader::disable();

		Storm3D_ShaderManager::GetSingleton()->setProjectedShaders();
		coneNvVertexShader.apply();

		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());

		glDepthMask(GL_FALSE);
		glDisable(GL_ALPHA_TEST);
		glEnable(GL_BLEND);
		glBlendFunc(GL_ONE, GL_ONE);

		StormSpotList::iterator it = spots.begin();
		for(; it != spots.end(); ++it)
		{
			Storm3D_Spotlight *spot = it->get();
			if(!spot || !spot->enabled() || !spot->featureEnabled(IStorm3D_Spotlight::ConeVisualization))
				continue;

			if(renderShadows)
				spot->renderCone(camera, timeFactor, renderGlows);
		}

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();
		frozenbyte::storm::PixelShader::disable();

		glDepthMask(GL_TRUE);
		glDisable(GL_BLEND);
	}
Esempio n. 2
0
	void renderCones(Storm3D_Scene &scene, bool renderShadows, float timeFactor, bool renderGlows)
	{
		// this draws spotlight cone
		device.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED);
		device.SetPixelShader(0);

		Storm3D_ShaderManager::GetSingleton()->setProjectedShaders();
		coneNvVertexShader.apply();

		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());

		device.SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
		device.SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
		device.SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
		device.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		device.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

		SpotList::iterator it = spots.begin();
		for(; it != spots.end(); ++it)
		{
			Storm3D_Spotlight *spot = it->get();
			if(!spot || !spot->enabled() || !spot->featureEnabled(IStorm3D_Spotlight::ConeVisualization))
				continue;

			if(renderShadows)
				spot->renderCone(camera, timeFactor, renderGlows);
		}

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();
		device.SetPixelShader(0);

		device.SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
		device.SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
		device.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
	}
Esempio n. 3
0
void Storm3D_TerrainLod::render(Storm3D_Scene &scene, int subMask, float range, float rangeX1, float rangeY1, float rangeX2, float rangeY2)
{
	GfxDevice &device = data->storm.GetD3DDevice();

	int lod = data->getLOD(range);

	bool lodX1 = data->getLOD(rangeX1) > lod;
	bool lodY1 = data->getLOD(rangeY1) > lod;
	bool lodX2 = data->getLOD(rangeX2) > lod;
	bool lodY2 = data->getLOD(rangeY2) > lod;

	int index = 0;
	if(lodX1)
		index += 1;
	if(lodX2)
		index += 2;
	if(lodY1)
		index += 4;
	if(lodY2)
		index += 8;

    IndexStorage16& indices = data->storm.getIndexStorage16();
    device.SetIndices(indices.indices);

	// Render as whole
	if(subMask == -1)
	{
		LodIndexBuffer &indexBuffer = data->lodBuffers[lod].fullBuffers[index];
        assert(indexBuffer.faceAmount);
        device.DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, data->maxVertex, indexBuffer.baseIndex, indexBuffer.faceAmount);

		scene.AddPolyCounter(indexBuffer.faceAmount);
	}
	else
	{
		LodIndexBuffer &indexBuffer = data->lodBuffers[lod].centerBuffers[subMask];
        device.DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, data->maxVertex, indexBuffer.baseIndex, indexBuffer.faceAmount);

		scene.AddPolyCounter(indexBuffer.faceAmount);

		LodIndexBuffer &linkBuffer = data->lodBuffers[lod].linkBuffers[index];
        device.DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, data->maxVertex, linkBuffer.baseIndex, linkBuffer.faceAmount);

		scene.AddPolyCounter(linkBuffer.faceAmount);
	}
}
Esempio n. 4
0
	void renderFakeSpotLights(Storm3D_Scene &scene, bool renderShadows)
	{
        GFX_TRACE_SCOPE("renderFakeSpotLights");

		// Renders fake shadows to screen?
		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());
		Storm3D_ShaderManager::GetSingleton()->setFakeShadowShaders();

		device.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
		device.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR);
		device.SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
		device.SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
		device.SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
		device.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED);
		device.SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED);
		device.SetTextureStageState(2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED);
		device.SetTextureStageState(3, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED);

		device.SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
		device.SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
		device.SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
		device.SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

		frozenbyte::storm::enableMipFiltering(device, 0, 4, false);

		FakeSpotList::iterator it = fakeSpots.begin();
		for(; it != fakeSpots.end(); ++it)
		{
			Storm3D_FakeSpotlight *spot = it->get();
			if(!spot || !spot->enabled())
				continue;

			const float *cameraView = camera.GetView4x4Matrix();
			spot->applyTextures(cameraView);

			renderer.render(IStorm3D_TerrainRendererBase::FakeSpotProjection, scene, 0, spot);
		}

		frozenbyte::storm::enableMipFiltering(device, 0, 4, true);

		device.SetPixelShader(0);
		device.SetVertexShader(0);
		device.SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
		device.SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
		device.SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
		device.SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
		device.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
		device.SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
		device.SetTextureStageState(2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
		device.SetTextureStageState(3, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();

		device.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		device.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
		device.SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
		device.SetRenderState(D3DRS_CLIPPLANEENABLE, FALSE);
	}
Esempio n. 5
0
void Storm3D_TerrainLod::render(Storm3D_Scene &scene, int subMask, float range, float rangeX1, float rangeY1, float rangeX2, float rangeY2)
{
	IDirect3DDevice9 &device = *data->storm.GetD3DDevice();

	int lod = data->getLOD(range);

	bool lodX1 = data->getLOD(rangeX1) > lod;
	bool lodY1 = data->getLOD(rangeY1) > lod;
	bool lodX2 = data->getLOD(rangeX2) > lod;
	bool lodY2 = data->getLOD(rangeY2) > lod;

	int index = 0;
	if(lodX1)
		index += 1;
	if(lodX2)
		index += 2;
	if(lodY1)
		index += 4;
	if(lodY2)
		index += 8;

	// Render as whole
	if(subMask == -1)
	{
		LodIndexBuffer &indexBuffer = data->lodBuffers[lod].fullBuffers[index];
		indexBuffer.indexBuffer.render(device, indexBuffer.faceAmount, data->maxVertex);

		scene.AddPolyCounter(indexBuffer.faceAmount);
	}
	else
	{
		LodIndexBuffer &indexBuffer = data->lodBuffers[lod].centerBuffers[subMask];
		indexBuffer.indexBuffer.render(device, indexBuffer.faceAmount, data->maxVertex);

		scene.AddPolyCounter(indexBuffer.faceAmount);

		LodIndexBuffer &linkBuffer = data->lodBuffers[lod].linkBuffers[index];
		linkBuffer.indexBuffer.render(device, linkBuffer.faceAmount, data->maxVertex);

		scene.AddPolyCounter(linkBuffer.faceAmount);
	}
}
	void renderSpotBuffers(Storm3D_Scene &scene, bool renderShadows)
	{
		bool atiShaders = false;
		int spotType = Storm3D_Spotlight::getSpotType();
		if(spotType == Storm3D_Spotlight::AtiBuffer)
			atiShaders = true;

		// FIXME: broken?
		glPolygonOffset(1, 1);
		glEnable(GL_POLYGON_OFFSET_FILL);

		if(atiShaders)
			Storm3D_ShaderManager::GetSingleton()->setAtiDepthShaders();
		else
		{
			glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
			//glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
			Storm3D_ShaderManager::GetSingleton()->setNormalShaders();
		}

		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());
		Storm3D_Spotlight::clearCache();

		if(renderShadows)
		{
			StormSpotList::iterator it = spots.begin();
			for(; it != spots.end(); ++it)
			{
				Storm3D_Spotlight *spot = it->get();
				if(!spot || !spot->enabled() || !spot->featureEnabled(IStorm3D_Spotlight::Shadows))
					continue;

				// Test spot visibility with foo scissor rect -- not really an optimal way of doing this
				if(!spot->setScissorRect(camera, VC2I(100, 100), scene))
					continue;
				glDisable(GL_SCISSOR_TEST);

				const float *cameraView = camera.GetView4x4Matrix();
				if(!spot->setAsRenderTarget(cameraView))
					continue;

				glErrors();
				renderer.render(IStorm3D_TerrainRendererBase::SpotBuffer, scene, spot);
				glErrors();
			}
		}

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();
		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
		frozenbyte::storm::PixelShader::disable();

		glDisable(GL_POLYGON_OFFSET_FILL);
		camera.Apply();
	}
Esempio n. 7
0
	void renderSpotBuffers(Storm3D_Scene &scene, bool renderShadows)
	{
        GFX_TRACE_SCOPE("renderSpotBuffers");

		device.SetRenderState(D3DRS_COLORWRITEENABLE, 0);
		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();

		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());
		Storm3D_Spotlight::clearCache();

		if(renderShadows)
		{
			SpotList::iterator it = spots.begin();
			for(; it != spots.end(); ++it)
			{
				Storm3D_Spotlight *spot = it->get();
				if(!spot || !spot->enabled() || !spot->featureEnabled(IStorm3D_Spotlight::Shadows))
					continue;

				// Test spot visibility with foo scissor rect -- not really an optimal way of doing this
				if(!spot->setScissorRect(camera, VC2I(100, 100), scene))
					continue;
				device.SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);

				const float *cameraView = camera.GetView4x4Matrix();
				if(!spot->setAsRenderTarget(cameraView))
					continue;

                /* debug
				static unsigned int count = 0;
				count++;
				device.Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, float(count % 21) / 20.0f, 0);
                */
				//glClearDepth(float(count % 11) / 10.0f);
				//glClear(GL_DEPTH_BUFFER_BIT);

				renderer.render(IStorm3D_TerrainRendererBase::SpotBuffer, scene, spot);
			}
		}

		device.SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
		device.SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();
		device.SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
		device.SetPixelShader(0);

		camera.Apply();
	}
	void renderFakeSpotBuffers(Storm3D_Scene &scene, bool renderShadows)
	{
		// Renders fake shadows to texture?
		Storm3D_ShaderManager::GetSingleton()->setFakeDepthShaders();
		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());

		// FIXME: this does not appear to do anything
		// d3d9 tinkers with these parameters here
		// but this is incorrect on opengl
		// because texture sampling state is part of the texture
		glActiveTexture(GL_TEXTURE1);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

		Storm3D_FakeSpotlight::clearCache();
		// bool rendered = false;

		FakeSpotList::iterator it = fakeSpots.begin();
		for(; it != fakeSpots.end(); ++it)
		{
			Storm3D_FakeSpotlight *spot = it->get();
			if(!spot || !spot->enabled())
				continue;

			const float *cameraView = camera.GetView4x4Matrix();
			if(!spot->setAsRenderTarget(cameraView))
				continue;

			renderer.render(IStorm3D_TerrainRendererBase::FakeSpotBuffer, scene, 0, spot);
			// rendered = true;
		}

		//if(rendered)
		//	Storm3D_FakeSpotlight::filterBuffers(storm, device);

		// FIXME: this does not appear to do anything
		// d3d9 tinkers with these parameters here
		// but this is incorrect on opengl
		// because texture sampling state is part of the texture
		glActiveTexture(GL_TEXTURE1);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

		frozenbyte::storm::PixelShader::disable();

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();
		camera.Apply();
	}
	void findDecals(Storm3D_Scene &scene)
	{
		decals.clear();

		// Find decals
		{
			IStorm3D_Camera *camera = scene.GetCamera();
			Storm3D_Camera *stormCamera = reinterpret_cast<Storm3D_Camera *> (camera);

			Frustum frustum = stormCamera->getFrustum();
			Tree::FrustumIterator itf(*tree, frustum);
			for(; !itf.end(); itf.next())
				decals.push_back(*itf);

			std::sort(decals.begin(), decals.end(), DecalSorter());
		}
	}
Esempio n. 10
0
	void renderFakeSpotBuffers(Storm3D_Scene &scene, bool renderShadows)
	{
        GFX_TRACE_SCOPE("renderFakeSpotBuffers");

		// Renders fake shadows to texture?
		Storm3D_ShaderManager::GetSingleton()->setFakeDepthShaders();
		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());

		device.SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
		device.SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
		//device.SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED);

		Storm3D_FakeSpotlight::clearCache();
		bool rendered = false;

		FakeSpotList::iterator it = fakeSpots.begin();
		for(; it != fakeSpots.end(); ++it)
		{
			Storm3D_FakeSpotlight *spot = it->get();
			if(!spot || !spot->enabled())
				continue;

			const float *cameraView = camera.GetView4x4Matrix();
			if(!spot->setAsRenderTarget(cameraView))
				continue;

			renderer.render(IStorm3D_TerrainRendererBase::FakeSpotBuffer, scene, 0, spot);
			rendered = true;
		}

		//if(rendered)
		//	Storm3D_FakeSpotlight::filterBuffers(storm, device);

		device.SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
		device.SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
		device.SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
		device.SetPixelShader(0);

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();
		camera.Apply();
	}
	void render(Storm3D_Scene &scene)
	{
		findDecals(scene);

		createIndexBuffers();
		createVertexBuffers();

		// Render
		if(!decals.empty())
		{
			glDepthMask(GL_FALSE);
			glEnable(GL_BLEND);
			glDisable(GL_ALPHA_TEST);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

			D3DXMATRIX tm;

			pixelShader.apply();
			vertexShader.apply();
			vertices.apply(0);
			// ugly HACK!
			int prevBindOffs = 0;
			Storm3D_ShaderManager::GetSingleton()->SetWorldTransform(tm);

			float outfactor[4] = { outFactor.r, outFactor.g, outFactor.b, 1.0f };
			glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, 8, outfactor);
			float infactor[4] = { inFactor.r, inFactor.g, inFactor.b, 1.0f };
			glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, 9, infactor);

			int materialIndex = 0;
			int startIndex = 0;
			int endIndex = 0;

			for(;;)
			{
				materialIndex = decals[startIndex]->materialIndex;
				materials[materialIndex].apply();

				int decalAmount = decals.size();
				for(int i = startIndex + 1; i < decalAmount; ++i)
				{
					if(decals[i]->materialIndex != materialIndex)
						break;

					endIndex = i;
				}

				int renderAmount = endIndex - startIndex + 1;

				if (prevBindOffs != startIndex * 4) {
					vertices.apply(0, (startIndex * 4) * VERTEX_SIZE);
					prevBindOffs = startIndex * 4;
				}
				indices.render(renderAmount * 2, renderAmount * 4);
				startIndex = ++endIndex;

				scene.AddPolyCounter(renderAmount * 2);
				if(startIndex >= decalAmount)
					break;
			}

			glDisable(GL_BLEND);
		}
	}
	void renderFakeSpotLights(Storm3D_Scene &scene, bool renderShadows)
	{
		// Renders fake shadows to screen?
		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());
		Storm3D_ShaderManager::GetSingleton()->setFakeShadowShaders();

		glBlendFunc(GL_ZERO, GL_SRC_COLOR);
		glEnable(GL_BLEND);
		glDisable(GL_ALPHA_TEST);
		glDepthMask(GL_FALSE);

		// D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED done in shader in OpenGL

		FakeSpotList::iterator it = fakeSpots.begin();

		unsigned int count = 0;

		for(; it != fakeSpots.end(); ++it)
		{
			Storm3D_FakeSpotlight *spot = it->get();
			if(!spot || !spot->enabled())
				continue;

			const float *cameraView = camera.GetView4x4Matrix();
			spot->applyTextures(cameraView);

			for (int i = 0; i < 4; i++)
			{
				glActiveTexture(GL_TEXTURE0 + i);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
				float temp[4] = { 0.0, 0.0f, 0.0f, 0.0f };
				glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, temp);
			}

			if (count == 0)
			{
				// this is the most horrific f****d up WTF thing ever
				// without this we get strangely colored artifacts in strange places
				// someone please explain what is going on
				// for some reason first draw of fakespotlight is messed up
				// so when drawing the first one, "fix it" by drawing it twice
				glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
				renderer.render(IStorm3D_TerrainRendererBase::FakeSpotProjection, scene, 0, spot);
				glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
			}
			renderer.render(IStorm3D_TerrainRendererBase::FakeSpotProjection, scene, 0, spot);

			++count;

		}

		frozenbyte::storm::PixelShader::disable();
		frozenbyte::storm::VertexShader::disable();

		for (int i = 0; i < 4; i++)
		{
			glActiveTexture(GL_TEXTURE0 + i);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		}

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();

		glBlendFunc(GL_ONE, GL_ONE);
		glDisable(GL_BLEND);
		glDisable(GL_CLIP_PLANE0);
		glDisable(GL_CLIP_PLANE1);
		glDisable(GL_CLIP_PLANE2);
	}
	void renderSpotLights(Storm3D_Scene &scene, bool renderShadows, LightType type)
	{
		// this renders spotlight light & shadows
		Storm3D_ShaderManager::GetSingleton()->setProjectedShaders();

		glBlendFunc(GL_ONE, GL_ONE);
		glEnable(GL_BLEND);
		glDisable(GL_ALPHA_TEST);
		glDepthMask(GL_FALSE);

		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());
		StormSpotList::iterator it = spots.begin();
		for(; it != spots.end(); ++it)
		{
			Storm3D_Spotlight *spot = it->get();
			if(!spot || !spot->enabled())
				continue;

			// Test spot visibility with foo scissor rect -- not really an optimal way of doing this
			if(!spot->setScissorRect(camera, VC2I(100, 100), scene))
				continue;
			// glDisable below makes at least some spotlights visible through movie top & bottom "black bars"
			//glDisable(GL_SCISSOR_TEST);

			spot->applyTextures(camera.GetViewMatrix(), storm, renderShadows);

			for(int i = 0; i < 4; ++i)
			{
				if(i != 2)
				{
					glActiveTexture(GL_TEXTURE0 + i);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
				}
			}
	
			if(type == RealSolid)
				renderer.render(IStorm3D_TerrainRendererBase::SpotProjectionSolid, scene, spot);
			else
			{
				renderer.render(IStorm3D_TerrainRendererBase::SpotProjectionDecal, scene, spot);
				renderer.render(IStorm3D_TerrainRendererBase::SpotProjectionAlpha, scene, spot);
			}
		}

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();

		glActiveTexture(GL_TEXTURE1);
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_TEXTURE_3D);
		glDisable(GL_TEXTURE_CUBE_MAP);
		glBindTexture(GL_TEXTURE_2D, 0);

		glActiveTexture(GL_TEXTURE2);
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_TEXTURE_3D);
		glDisable(GL_TEXTURE_CUBE_MAP);
		glBindTexture(GL_TEXTURE_2D, 0);

		glActiveTexture(GL_TEXTURE3);
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_TEXTURE_3D);
		glDisable(GL_TEXTURE_CUBE_MAP);
		glBindTexture(GL_TEXTURE_2D, 0);

		glActiveTexture(GL_TEXTURE4);
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_TEXTURE_3D);
		glDisable(GL_TEXTURE_CUBE_MAP);
		glBindTexture(GL_TEXTURE_2D, 0);

		frozenbyte::storm::PixelShader::disable();
		glDisable(GL_BLEND);
		glDisable(GL_SCISSOR_TEST);
		glDisable(GL_CLIP_PLANE0);
		glDisable(GL_CLIP_PLANE1);
		glDisable(GL_CLIP_PLANE2);
	}
Esempio n. 14
0
	void renderSpotLights(Storm3D_Scene &scene, bool renderShadows, LightType type)
	{
        GFX_TRACE_SCOPE("renderSpotLights");

		// this renders spotlight light & shadows
		//setTracing(true);

		Storm3D_ShaderManager::GetSingleton()->setProjectedShaders();

		device.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		device.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
		device.SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
		device.SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
		device.SetRenderState(D3DRS_ALPHAREF, 0x1);
		device.SetRenderState(D3DRS_ZWRITEENABLE, FALSE);

		for(int i = 0; i < 4; ++i)
		{
			if(i != 2)
			{
				device.SetSamplerState(i, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
				device.SetSamplerState(i, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
			}
		}

		Storm3D_Camera &camera = *static_cast<Storm3D_Camera *> (scene.GetCamera());
		SpotList::iterator it = spots.begin();
		for(; it != spots.end(); ++it)
		{
			Storm3D_Spotlight *spot = it->get();
			if(!spot || !spot->enabled())
				continue;

			// Test spot visibility with foo scissor rect -- not really an optimal way of doing this
			if(!spot->setScissorRect(camera, VC2I(100, 100), scene))
				continue;
			device.SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);

			if(!false)
			{
				device.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED);
				device.SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_PROJECTED);
				device.SetTextureStageState(2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
				device.SetTextureStageState(3, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
			}

			const float *cameraView = camera.GetView4x4Matrix();
			const float *cameraViewProjection = camera.GetViewProjection4x4Matrix();
			spot->applyTextures(cameraView, cameraViewProjection, storm, renderShadows);

			if(type == RealSolid)
				renderer.render(IStorm3D_TerrainRendererBase::SpotProjectionSolid, scene, spot);
			else
			{
				renderer.render(IStorm3D_TerrainRendererBase::SpotProjectionDecal, scene, spot);
				renderer.render(IStorm3D_TerrainRendererBase::SpotProjectionAlpha, scene, spot);
			}
		}

		Storm3D_ShaderManager::GetSingleton()->setNormalShaders();
		for(int i = 0; i < 4; ++i)
		{
			device.SetSamplerState(i, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
			device.SetSamplerState(i, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
		}
		//setTracing(false);

		device.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
		device.SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
		device.SetTextureStageState(2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
		device.SetTextureStageState(3, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);

		device.SetTexture(1, 0);
		device.SetTexture(2, 0);
		device.SetTexture(3, 0);
		device.SetTexture(4, 0);

		device.SetPixelShader(0);
		device.SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
		device.SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
		device.SetRenderState(D3DRS_CLIPPLANEENABLE, FALSE);
	}
	void renderShadows(Storm3D_Scene &scene)
	{
		// this is BROKEN
		// FIXME
		// just clear the list
		shadowDecals.clear();
		return;

		createIndexBuffers();

		int renderAmount = 0;
		if(!shadowDecals.empty())
		{
			IStorm3D_Camera *camera = scene.GetCamera();
			Storm3D_Camera *stormCamera = reinterpret_cast<Storm3D_Camera *> (camera);
			Frustum frustum = stormCamera->getFrustum();

			shadowVertices.create(shadowDecals.size() * 4, VERTEX_SIZE, true);

			int rawSize = shadowDecals.size() * 4 * VERTEX_SIZE;
			void *ramBuffer = malloc(rawSize);
			void *lockPointer = shadowVertices.lock();
			VXFORMAT_DECAL *buffer = reinterpret_cast<VXFORMAT_DECAL *> (ramBuffer);

			float inverseRange =1.f / fogRange;
			for(unsigned int i = 0; i < shadowDecals.size(); ++i)
			{
				const StormDecal &decal = shadowDecals[i];
				Sphere sphere(decal.position, decal.getRadius());
				if(frustum.visibility(sphere))
				{
					float factor = decal.position.y - fogEnd;
					factor *= inverseRange;
					if(factor < 0.f)
						factor = 0.f;
					if(factor > 1.f)
						factor = 1.f;
					factor = 1.f - factor;

					COL color = decal.light;
					color.r += factor * (1.f - color.r);
					color.g += factor * (1.f - color.g);
					color.b += factor * (1.f - color.b);

					DWORD vertexColor = color.GetAsD3DCompatibleARGB() & 0x00FFFFFF;

					DWORD oldColor = decal.vertexColor;
					decal.vertexColor = vertexColor;
					decal.insert(buffer);
					decal.vertexColor = oldColor;

					buffer += 4;
					++renderAmount;
				}
			}

			if(renderAmount)
			{
				if(renderAmount > STORM_MAX_DECAL_AMOUNT)
					renderAmount = STORM_MAX_DECAL_AMOUNT;
				memcpy(lockPointer, ramBuffer, renderAmount * 4 * VERTEX_SIZE);
			}

			free(ramBuffer);
			shadowVertices.unlock();
		}

		D3DXMATRIX tm;

		frozenbyte::storm::VertexShader::disable();
		frozenbyte::storm::PixelShader::disable();
		Storm3D_ShaderManager::GetSingleton()->SetWorldTransform(tm);

		applyFVF(DECAL_FVF, VERTEX_SIZE);
		glActiveTexture(GL_TEXTURE0);
		glClientActiveTexture(GL_TEXTURE0);
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
		glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
		glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PRIMARY_COLOR);
		glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);
		glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);
		glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_PRIMARY_COLOR);
		glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
		glActiveTexture(GL_TEXTURE1);
		glClientActiveTexture(GL_TEXTURE1);
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_TEXTURE_3D);
		glDisable(GL_TEXTURE_CUBE_MAP);

		glDepthMask(GL_FALSE);
		glEnable(GL_BLEND);
		glDisable(GL_ALPHA_TEST);
		glBlendFunc(GL_ZERO, GL_SRC_COLOR);

		if(renderAmount && shadowMaterial)
		{
			shadowVertices.apply(0);
			shadowMaterial->applyShadow();

			indices.render(renderAmount * 2, renderAmount * 4);
			scene.AddPolyCounter(renderAmount * 2);
		}

		glDepthMask(GL_TRUE);
		glDisable(GL_BLEND);
		shadowDecals.clear();
	}
	void renderProjection(Storm3D_Scene &scene, Storm3D_Spotlight *spot)
	{
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
		glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);

		if(!decals.empty())
		{
			glDepthMask(GL_FALSE);
			glEnable(GL_BLEND);
			glDisable(GL_ALPHA_TEST);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE);

			D3DXMATRIX tm;

			int materialIndex = 0;
			int startIndex = 0;
			int endIndex = 0;

			if(spot->getType() == IStorm3D_Spotlight::Point)
				pointVertexShader.apply();
			else if(spot->getType() == IStorm3D_Spotlight::Directional)
				dirVertexShader.apply();
			else if(spot->getType() == IStorm3D_Spotlight::Flat)
				flatVertexShader.apply();

			vertices.apply(0);
			int prevBindOffs = 0;

			Storm3D_ShaderManager::GetSingleton()->SetWorldTransform(tm);
			Storm3D_ShaderManager::GetSingleton()->SetTransparencyFactor(0.75f);

			for(;;)
			{
				materialIndex = decals[startIndex]->materialIndex;
				materials[materialIndex].applyProjection(spot->getColorMultiplier());

				int decalAmount = decals.size();
				for(int i = startIndex + 1; i < decalAmount; ++i)
				{
					if(decals[i]->materialIndex != materialIndex)
						break;

					endIndex = i;
				}

				int renderAmount = endIndex - startIndex + 1;

				if (prevBindOffs != startIndex * 4) {
					vertices.apply(0, (startIndex * 4) * VERTEX_SIZE);
					prevBindOffs = startIndex * 4;
				}
				indices.render(renderAmount * 2, renderAmount * 4);
				startIndex = ++endIndex;

				scene.AddPolyCounter(renderAmount * 2);
				if(startIndex >= decalAmount)
					break;
			}

			Storm3D_ShaderManager::GetSingleton()->SetTransparencyFactor(1.f);

			glBlendFunc(GL_ONE, GL_ONE);
			glEnable(GL_BLEND);
		}

		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
		glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
	}
    void renderShadows(Storm3D_Scene &scene)
    {
        gfx::Renderer& renderer = storm.renderer;
        gfx::Device& device = renderer.device;
        gfx::ProgramManager& programManager = renderer.programManager;

        if (shadowDecals.empty() || !shadowMaterial)
            return;

        int renderAmount = 0;

        IStorm3D_Camera *camera = scene.GetCamera();
        Storm3D_Camera *stormCamera = reinterpret_cast<Storm3D_Camera *> (camera);
        Frustum frustum = stormCamera->getFrustum();

        uint32_t baseVertex = 0;
        Vertex_P3DUV *buffer = 0;;
        renderer.lockDynVtx<Vertex_P3DUV>(shadowDecals.size() * 4, &buffer, &baseVertex);

        float inverseRange = 1.f / fogRange;
        for (unsigned int i = 0; i < shadowDecals.size() && renderAmount < MAX_DECAL_AMOUNT; ++i)
        {
            const Decal &decal = shadowDecals[i];
            Sphere sphere(decal.position, decal.getRadius());
            if (frustum.visibility(sphere))
            {
                float factor = decal.position.y - fogEnd;
                factor *= inverseRange;
                factor = std::max(0.0f, factor);
                factor = std::min(1.0f, factor);
                factor = 1.f - factor;

                COL color = decal.light;
                color.r += factor * (1.f - color.r);
                color.g += factor * (1.f - color.g);
                color.b += factor * (1.f - color.b);

                uint32_t vertexColor = color.as_u32_D3D_ARGB(decal.alpha);
                decal.insert(buffer, vertexColor);

                buffer += 4;
                ++renderAmount;
            }
        }

        renderer.unlockDynVtx();

        if (renderAmount == 0)
            return;

        D3DXMATRIX tm;
        D3DXMatrixIdentity(&tm);

        programManager.setProgram( gfx::ProgramManager::DECAL_SHADOW);
        programManager.setWorldMatrix(tm);
        programManager.applyState(device);

        device.SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
        device.SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
        device.SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
        device.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
        device.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR);

        if (shadowMaterial->baseTexture)
            shadowMaterial->baseTexture->Apply(0);

        renderer.setFVF(FVF_P3DUV);
        renderer.setDynVtxBuffer<Vertex_P3DUV>();
        renderer.drawQuads(baseVertex, renderAmount);

        device.SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
        device.SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);

        shadowDecals.clear();
    }