Lab 3 Shadow Mapping. Giuseppe Maggiore

Similar documents
DirectX Programming #4. Kang, Seongtae Computer Graphics, 2009 Spring

Bloom effect - before. Bloom effect - after. Postprocessing. Motion blur in Valve s Portal - roll

Lab 1 Sample Code. Giuseppe Maggiore

Shaders (some slides taken from David M. course)

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

Shaders in Eve Online Páll Ragnar Pálsson

IWKS 3400 LAB 11 1 JK Bennett

Shader Programming and Graphics Hardware

Sébastien Dominé, NVIDIA. CgFX

Could you make the XNA functions yourself?

Shaders Part II. Traditional Rendering Pipeline

Shader Programming CgFX, OpenGL 2.0. Michael Haller 2003

Understanding XNA Framework Performance. Shawn Hargreaves Software Development Engineer XNA Community Game Platform Microsoft

Designing a Modern GPU Interface

SDK White Paper. Rainbows and Fogbows Adding Natural Phenomena

The Effects Framework. October 31, Composed of the following components:

Bringing AAA graphics to mobile platforms. Niklas Smedberg Senior Engine Programmer, Epic Games

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Microsoft XNA 4.0 Game Development Cookbook

Blending Equation Blend Factors & Mode Transparency Creating an Alpha Channel Using DX Tex Tool Render Semi-Transparent Objects.

The Application Stage. The Game Loop, Resource Management and Renderer Design

Creating Real Shaders in FX Composer. Kevin Bjorke NVIDIA Corporation

Technical Report. Mesh Instancing

CS195V Week 9. GPU Architecture and Other Shading Languages

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

Geometry Shader - Silhouette edge rendering

Shadows. Prof. George Wolberg Dept. of Computer Science City College of New York

Double-click on CS470_Lab09.zip and extract the contents of the archive into a subdirectory called CS470_Lab09

XNA Climate Game Project

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

How to Work on Next Gen Effects Now: Bridging DX10 and DX9. Guennadi Riguer ATI Technologies

Working with Metal Overview

Shadows & Decals: D3D10 techniques from Frostbite. Johan Andersson Daniel Johansson

TentamensKod: (Ifylles av student) Tentamensdatum: Tid: 10:00 12:00. Hjälpmedel: Inga hjälpmedel

Computer Graphics Shadow Algorithms

Evolution of GPUs Chris Seitz

Real-Time Shadows. Last Time? Textures can Alias. Schedule. Questions? Quiz 1: Tuesday October 26 th, in class (1 week from today!

Volume Shadows Tutorial Nuclear / the Lab

Fur Shader GRAPHICS PROGRAMMING 2

Last Time. Why are Shadows Important? Today. Graphics Pipeline. Clipping. Rasterization. Why are Shadows Important?

Shadows in Computer Graphics. by Björn Kühl im/ve University of Hamburg, Germany

DH2323 DGI13 Lab 3 Rasterization

For example, could you make the XNA func8ons yourself?

For Intuition about Scene Lighting. Today. Limitations of Planar Shadows. Cast Shadows on Planar Surfaces. Shadow/View Duality.

6.837 Introduction to Computer Graphics Assignment 5: OpenGL and Solid Textures Due Wednesday October 22, 2003 at 11:59pm

XNA Game Studio 4.0.

Shaders. Slide credit to Prof. Zwicker

Real-Time Shadows. Last Time? Today. Why are Shadows Important? Shadows as a Depth Cue. For Intuition about Scene Lighting

Programming Graphics Hardware

Deferred Rendering Due: Wednesday November 15 at 10pm

XNA 4.0 RPG Tutorials. Part 5. The Tile Engine - Part 2

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

Next, we re going to specify some extra stuff related to our window such as its size and title. Add this code to the Initialize method:

Ch 10: Game Event Management. Quiz # 4 Discussion

Blending. To blend (combine) the pixels. To render semi-transparent objects. Currently Rasterizing. Previous on the Back Buffer.

Getting fancy with texture mapping (Part 2) CS559 Spring Apr 2017

Last Time. Reading for Today: Graphics Pipeline. Clipping. Rasterization

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

Visual C# 2010 Express

Basics of GPU-Based Programming

High-Precision Shading and Geometry. Kevin Bjorke NVIDIA Corporation

Metal for OpenGL Developers

Seminar: Assignment 1. Magnus Andersson

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

Real-Time Shadows. MIT EECS 6.837, Durand and Cutler

COMP 4801 Final Year Project. Ray Tracing for Computer Graphics. Final Project Report FYP Runjing Liu. Advised by. Dr. L.Y.

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

FX Composer 1.5. Chris Maughan

HLSL Development Cookbook

Summed-Area Tables. And Their Application to Dynamic Glossy Environment Reflections Thorsten Scheuermann 3D Application Research Group

Your logo here. October, 2017

Cg Toolkit. Cg 1.3 Release Notes. December 2004

HARDWARE ACCELERATED 3D/2D RENDERING

Computer Graphics - Treasure Hunter

Lab 9 - Metal and Glass

Real-Time Shadows. Last Time? Schedule. Questions? Today. Why are Shadows Important?

Heap Arrays and Linked Lists. Steven R. Bagley

Soft Particles. Tristan Lorach

Rendering Objects. Need to transform all geometry then

Low-Overhead Rendering with Direct3D. Evan Hart Principal Engineer - NVIDIA

Applications of Explicit Early-Z Z Culling. Jason Mitchell ATI Research

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

A Summoner's Tale MonoGame Tutorial Series. Chapter 3. Tile Engine and Game Play State

Rasterization Overview

Interactive & Cross-platform development studio

CS452/552; EE465/505. Shadow Mapping in WebGL

CPSC 436D Video Game Programming

the gamedesigninitiative at cornell university Lecture 14 2D Sprite Graphics

Optimizing DirectX Graphics. Richard Huddy European Developer Relations Manager

OPENGL RENDERING PIPELINE

Session 5.1. Writing Text

The Graphics Pipeline

!" "!#$!"%!&% ' ( )) * ) )+( ( ))+ ))(, ) ( - +,./ )) ) ) $( -( 1), , ) ( -)) ') 5 ' +) 67 4 ) ' 8, ( ) ( 9 :! ; $) ) ; 5<4,4$6 ( =) >

DECLARATIVE AR IN THE WEB WITH XML3D & XFLOW. By Felix Klein

Graphics Hardware. Instructor Stephen J. Guy

Cg 2.0. Mark Kilgard

CS 465 Program 5: Ray II

6.837 Introduction to Computer Graphics Quiz 2 Thursday November 20, :40-4pm One hand-written sheet of notes allowed

Multi-View Soft Shadows. Louis Bavoil

Transcription:

Lab 3 Shadow Giuseppe Maggiore

Adding Shadows to the Scene

First we need to declare a very useful helper object that will allow us to draw textures to the screen without creating a quad vertex buffer // A sprite batch is an XNA helper for drawing textures on the screen; it // is very useful to visualize intermediate computations and implementing // simple viewports. SpriteBatch sprite_batch;

The sprite batch is initialized by passing it the graphics device // We initialize the sprite batch sprite_batch = new SpriteBatch(GraphicsDevice);

We need a place where we can store the rays we will cast from the light; since all the rays are parallel to the light direction, we just store their length in a texture // We need a render target, that is a texture that can be used as a destination for // a rendering operation. This way we can draw the scene from the POV of the light // and store the depth of each direction starting from the light. RenderTarget2D shadow_map; // A render target needs a depth-stencil buffer of the same size. Since our render // target has a different size than the screen, we cannot use the default depth // buffer. DepthStencilBuffer shadow_depth;

We can create the shadow map and its associated depth buffer by calling their constructors and specifying the desired size and format // We create a 1024x1024 render target and depth buffer. The two must have the same bit // depth: single (a float32) is 4 bytes, and Depth24Stencil8 is 4 bytes too int shadow_size = 1024; shadow_map = new RenderTarget2D(GraphicsDevice, shadow_size, shadow_size, 1, SurfaceFormat.Single); shadow_depth = new DepthStencilBuffer(GraphicsDevice, shadow_size, shadow_size, DepthFormat.Depth24Stencil8);

When creating the shadow map, we first save a reference to the previous depth buffer, and then we set our depth buffer and the shadow map as a render target: all subsequent rendering operations will write the shadow map pixels // We store the previous depth buffer var depth = GraphicsDevice.DepthStencilBuffer; // We set our render target and our depth buffer GraphicsDevice.DepthStencilBuffer = shadow_depth; GraphicsDevice.SetRenderTarget(0, shadow_map);

We create view and projection matrices for drawing the scene from the point of view of the light the view matrix is relatively straightforward to compute: Matrix.CreateLookAt will suffice the projection matrix should be treated with more care: it should fit the scene as tightly as possible; if the projection covers too much (zmin that is too small or zmax too big) then huge precision errors will occur

To draw the scene, we start by restoring the original depth buffer and render target (otherwise nothing will appear on the screen since rendering will fill the shadow map) // We restore the initial depth buffer and the default render target. GraphicsDevice.DepthStencilBuffer = depth; GraphicsDevice.SetRenderTarget(0, null);

We can also draw the shadow map itself to see what is going in it // We can draw the shadow map on top of the screen, for debugging // purposes. sprite_batch.begin(spriteblendmode.alphablend, SpriteSortMode.Immediate, SaveStateMode.SaveState); sprite_batch.draw(shadow_map.gettexture(), new Rectangle(200, 0, 200, 200), Color.White); sprite_batch.end();

The shaders parameters are various matrices (scene seen from the camera and from the light) // Regular world, view and projection matrices float4x4 World; float4x4 CameraView; float4x4 CameraProjection; // View and projection matrices that define the // scene seen from the POV of the light. float4x4 LightView; float4x4 LightProjection; float4 DiffuseColor;

Also, the shadow map as a texture is needed for accessing the depth data // Texture that stores the minimum z-distances // of various pixels from the light. texture ShadowMapTexture; // Sample to read values from ShadowMapTexture sampler shadow_map_sampler = sampler_state { Texture = <ShadowMapTexture>; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = Clamp; AddressV = Clamp; };

We have two vertex shaders: one for outputting the depth of each pixel from the camera, and another for drawing with shadows; the two shaders outputs are: // Input vertex is just a position, so we don't need // to declare it as a struct. Output vertex for shadow // mapping contains the transformed position and the // original world position (which the pixel shader will // transform with the light view and projection). struct VS_OUTPUT { float4 Position : POSITION0; float4 vpos : TEXCOORD0; }; // Output vertex for shadow map construction: it // contains the transformed position and the depth of // the pixel. struct VS_SHADOW_OUTPUT { float4 Position : POSITION; float Depth : TEXCOORD0; };

A very useful function is the one which transforms a vertex by the light viewpoint: // Helper function that transforms a position by the // light viewpoint. float4 GetPositionFromLight(float4 position) { float4x4 wvp = mul(mul(world, LightView), LightProjection); return mul(position, wvp); }

One vertex shader simply outputs the position and the depth

The corresponding pixel shader simply outputs the depth: // When outputting the depth from the light, the // pixel shader returns a fake color which will // be ignored apart from the first component, // which will be stored in the single float // of our render target (the shadow map). float4 RenderShadowMapPS( VS_SHADOW_OUTPUT In ) : COLOR0 { return float4(in.depth.x,0,0,1); }

The vertex shader that outputs the position for computing the shadow is quite simple: // Vertex shader that outputs the transformed position // and the original position. VS_OUTPUT RenderShadowsVS(float4 position : POSITION0) { VS_OUTPUT Output; float4x4 wvp = mul(mul(world, CameraView), CameraProjection); Output.Position = mul(position, wvp); Output.vPos = position; return Output; }

The pixel shader that computes the shadow begins by transforming the original position by the light viewpoint

Then we read from the shadow map the depth of the shadow along the ray that goes from this vertex to the light

Now we compute our distance from the light and compare it with the shadow depth

Remember: two shaders require two techniques: technique ShadowRender { pass P0 { VertexShader = compile vs_2_0 RenderShadowsVS(); PixelShader= compile ps_2_0 RenderShadowsPS(); } } technique ShadowMapRender { pass P0 { VertexShader = compile vs_2_0 RenderShadowMapVS(); PixelShader= compile ps_2_0 RenderShadowMapPS(); } }

THAT S IT: GOOD LUCK!