Week 2 - Wednesday. More SharpDX examples Graphics rendering pipeline Application stage Geometry...

26
CS361 Week 2 - Wednesday

Transcript of Week 2 - Wednesday. More SharpDX examples Graphics rendering pipeline Application stage Geometry...

Page 1: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

CS361Week 2 - Wednesday

Page 2: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

What did we talk about last time?

More SharpDX examples Graphics rendering pipeline

Application stage Geometry stage Rasterizer stage

Application stage Input and non-graphical output Texture animation Animation via transforms Collision detection Updating the state of the world in general Outputs geometric primitives

Page 3: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Questions?

Page 4: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Project 1

Page 5: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Graphics rendering pipeline For API design, practical top-down problem

solving, and hardware design, and efficiency, rendering is described as a pipeline

This pipeline contains three conceptual stages:

Produces

material to be

rendered

Application

Decides what, how, and

where to

render

Geometry

Renders the final image

Rasterizer

Page 6: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Student Lecture: Geometry Stage

Page 7: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Geometry Stage

Page 8: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Geometry stage

The output of the Application Stage is polygons

The Geometry Stage processes these polygons using the following pipeline:

Model and View Transform

Vertex Shading Projection Clipping Screen

Mapping

Page 9: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Model Transform

Each 3D model has its own coordinate system called model space

When combining all the models in a scene together, the models must be converted from model space to world space

After that, we still have to account for the position of the camera

Page 10: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Model and View Transform We transform the models into camera

space or eye space with a view transform Then, the camera will sit at (0,0,0), looking

into negative z The z-axis comes out of the screen in the

book's examples and in SharpDX (but not in older DirectX)

Page 11: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Vertex Shading

Figuring out the effect of light on a material is called shading

This involves computing a (sometimes complex) shading equation at different points on an object

Typically, information is computed on a per-vertex basis and may include: Location Normals Colors

Page 12: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Projection

Projection transforms the view volume into a standardized unit cube

Vertices then have a 2D location and a z-value

There are two common forms of projection: Orthographic: Parallel

lines stay parallel, objects do not get smaller in the distance

Perspective: The farther away an object is, the smaller it appears

Page 13: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Clipping

Clipping process the polygons based on their location relative to the view volume

A polygon completely inside the view volume is unchanged A polygon completely outside the view volume is ignored

(not rendered) A polygon partially inside is clipped

New vertices on the boundary of the volume are created Since everything has been transformed into a unit cube,

dedicated hardware can do the clipping in exactly the same way, every time

Page 14: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Screen mapping

Screen-mapping transforms the x and y coordinates of each polygon from the unit cube to screen coordinates

A few oddities: DirectX has weird coordinate systems for pixels where the

location is the center of the pixel DirectX conforms to the Windows standard of pixel (0,0)

being in the upper left of the screen OpenGL conforms to the Cartesian system with pixel (0,0)

in the lower left of the screen

Page 15: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

3D Rendering in SharpDX

Page 16: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Drawing a model

We're going to start by drawing a 3D model Eventually, we'll go back and create our own

primitives Like other SharpDX content, the easiest

way to manage it is to add it to your Content folder SharpDX can load (some).fbx files and a

number of other files supported by Assimp Model loading is one of the newer features of

SharpDX and seems to have a number of bugs

Page 17: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Loading a model

First, we declare a member variable to hold the model

Then we load the model in the LoadContent() method

model = Content.Load<Model>("Ship");

Model model;

Page 18: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Setting up the matrices

To draw anything in 3D, we need a world matrix, a view matrix and a projection matrix

You'll need these repeatedly, so store them as members

Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));

Matrix view = Matrix.LookAtRH(new Vector3(0, 0, 7), new Vector3(0, 0, 0), Vector3.UnitY);

Matrix projection = Matrix. PerspectiveFovRH( 0.9f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, 100.0f);

Page 19: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

What do those matrices mean?

The world matrix controls how the model is translated, scaled, and rotated with respect to the global coordinate system

This code makes a matrix that moves the model 0 units in x, 0 units in y, and 0 units in z In other words, it does nothing

Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));

Page 20: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

And the view matrix?

The view matrix sets up the orientation of the camera

The easiest way to do so is to give Camera location What the camera is pointed at Which way is up

This camera is at (0, 0, 7), looking at the origin, with positive y as up

Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 7), new Vector3(0, 0, 0), Vector3.UnitY);

Page 21: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

And projection?

The projection matrix determines how the scene is projected into 2D

It can be specified with Field of view in radians Aspect ratio of screen (width / height) Near plane location Far plane locationMatrix projection =

Matrix.CreatePerspectiveFieldOfView( .9f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, 100f);

Page 22: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Drawing

Drawing the model is done by drawing all the individual meshes that make it up

Each mesh has a series of effects Effects are used for texture mapping, visual appearance,

and other things They need to know the world, view, and projection matrices

foreach (ModelMesh mesh in model.Meshes){

foreach (BasicEffect effect in mesh.Effects){

effect.World = world;effect.View = view;effect.Projection = projection;

}

mesh.Draw();}

Page 23: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Quiz

Page 24: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Upcoming

Page 25: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Next time…

Rendering pipeline Rasterizer stage

Page 26: Week 2 - Wednesday.  More SharpDX examples  Graphics rendering pipeline  Application stage  Geometry stage  Rasterizer stage  Application stage.

Reminders

Keep reading Chapter 2