Example #1
0
void ShadowsDemo::onDraw() {
	App::onDraw();

	const auto device = graphicsDevice();

	device->setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	device->clear(ciri::ClearFlags::Color | ciri::ClearFlags::Depth);

	device->setRasterizerState(_rasterState);
	device->restoreDefaultBlendState();

	if( _spotlightShader->isValid() && _directionalShader->isValid() ) {
		const cc::Mat4f& cameraViewProj = _camera.getProj() * _camera.getView();

		bool firstLight = true;
		Light::Type boundLightType = Light::Type::Invalid;
		for( auto& light : _lights ) {
			// compute light matrices
			//const cc::Mat4f& lightView = light.view();
			//const cc::Mat4f& lightProj = light.proj();//cc::math::perspectiveRH(45.0f, 1.0f, 0.1f, light.range());//light.proj();
			//const cc::Mat4f lightViewProj = lightProj * lightView;
			if( light.type() == Light::Type::Directional ) {
				//light.computeViewProjFromFrustum(BoundingFrustum(cameraViewProj));
				light.computeViewProjFromFrustum(BoundingFrustum(_camera.getFov(), _camera.getAspect(), _camera.getNearPlane(), _camera.getFarPlane(), _camera.getPosition(), _camera.getFpsFront(), _camera.getUp()));
				//light.computeViewProjOrtho(_camera.getView(), _camera.getFov(), _camera.getAspect(), _camera.getNearPlane(), _camera.getFarPlane());
			}
			const cc::Mat4f lightViewProj = light.proj() * light.view();

			if( light.castShadows() ) {
				device->setDepthStencilState(device->getDefaultDepthStencilDefault());
				// set and clear render target
				ciri::IRenderTarget2D* depthTarget = _shadowTarget.get();
				device->setRenderTargets(&depthTarget, 1);
				device->setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
				device->clear(ciri::ClearFlags::Color | ciri::ClearFlags::Depth);
				// apply depth shader
				device->applyShader(_depthShader);
				// set viewport to depth size
				device->setViewport(ciri::Viewport(0, 0, _shadowTarget->getDepth()->getWidth(), _shadowTarget->getDepth()->getHeight()));
				// render all models
				for( auto& mdl : _models ) {
					_depthConstants.xform = lightViewProj * mdl->getXform().getWorld();
					_depthConstantsBuffer->setData(sizeof(DepthConstants), &_depthConstants);
					device->setVertexBuffer(mdl->getVertexBuffer());
					if( mdl->getIndexBuffer() != nullptr ) {
						device->setIndexBuffer(mdl->getIndexBuffer());
						device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount());
					} else {
						device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0);
					}
				}

				// reser viewport to screen
				device->setViewport(ciri::Viewport(0, 0, window()->getWidth(), window()->getHeight()));
				// restore default render targets
				device->restoreDefaultRenderTargets();
			}
			switch( light.type() ) {
				case Light::Type::Directional: {
					if( boundLightType != Light::Type::Directional || light.castShadows() ) {
						boundLightType = Light::Type::Directional;
						device->applyShader(_directionalShader);
						device->setTexture2D(0, _shadowTarget->getDepth(), ciri::ShaderStage::Pixel);
						device->setSamplerState(0, _shadowSampler, ciri::ShaderStage::Pixel);
					}
					_directionalConstants.LightDirection = light.direction();
					_directionalConstants.LightColor = light.diffuseColor();
					_directionalConstants.LightIntensity = light.diffuseIntensity();
					_directionalConstants.campos = _camera.getPosition();
					_directionalConstants.CastShadows = light.castShadows();
					_directionalConstants.lightViewProj = lightViewProj;
					for( auto& mdl : _models ) {
						if( !mdl->isValid() ) {
							continue;
						}
						_directionalConstants.world = mdl->getXform().getWorld();
						_directionalConstants.xform = cameraViewProj * _directionalConstants.world;
						_directionalConstantsBuffer->setData(sizeof(DirectionalConstants), &_directionalConstants);
						device->setVertexBuffer(mdl->getVertexBuffer());
						if( mdl->getIndexBuffer() != nullptr ) {
							device->setIndexBuffer(mdl->getIndexBuffer());
							device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount());
						} else {
							device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0);
						}
					}
					break;
				}
				case Light::Type::Spot: {
					if( boundLightType != Light::Type::Spot || light.castShadows() ) {
						boundLightType = Light::Type::Spot;
						device->applyShader(_spotlightShader);
						device->setTexture2D(0, _shadowTarget->getDepth(), ciri::ShaderStage::Pixel);
						device->setSamplerState(0, _shadowSampler, ciri::ShaderStage::Pixel);
					}
					_spotlightConstants.LightPosition = light.position();
					_spotlightConstants.LightDirection = light.direction();
					_spotlightConstants.LightColor = light.diffuseColor();
					_spotlightConstants.LightCosInner = light.cosConeInnerAngle(true);
					_spotlightConstants.LightCosOuter = light.cosConeOuterAngle(true);
					_spotlightConstants.LightIntensity = light.diffuseIntensity();
					_spotlightConstants.LightRange = light.range();
					_spotlightConstants.CastShadows = light.castShadows();
					_spotlightConstants.lightViewProj = lightViewProj;
					for( auto& mdl : _models ) {
						_spotlightConstants.world = mdl->getXform().getWorld();
						_spotlightConstants.xform = cameraViewProj * _spotlightConstants.world;
						_spotlightConstantsBuffer->setData(sizeof(SpotlightConstants), &_spotlightConstants);
						device->setVertexBuffer(mdl->getVertexBuffer());
						if( mdl->getIndexBuffer() != nullptr ) {
							device->setIndexBuffer(mdl->getIndexBuffer());
							device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount());
						} else {
							device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0);
						}
					}
					break;
				}
			}

			if( firstLight ) {
				firstLight = false;
				device->setBlendState(_additiveBlendState);
			}
		}
	}

	device->present();
}
	void init() {
		setTexture2D(img, "img.png");
	}