introduction and overview - computer science · 2020-01-30 · framebuffer uniform variables...

38
Graphics pipeline Computer Graphics CSE 167 Lecture 7

Upload: others

Post on 29-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Graphics pipeline

Computer Graphics

CSE 167

Lecture 7

Page 2: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

APPLICATION

COMMAND STREAM

VERTEX PROCESSING

TRANSFORMED GEOMETRY

RASTERIZATION

FRAGMENTS

FRAGMENT PROCESSING

FRAMEBUFFER IMAGE

DISPLAY

You are here

3D transformations, shading

Conversion of primitives to pixels

Blending, compositing, shading

User sees this

Pipeline overview

CSE 167, Winter 2020 2Based on slides courtesy of Steve Marschner

Page 3: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Transformations

CSE 167, Winter 2020 3

ModelTransformation M

ViewTransformation V

Object (or Model) Coordinate Frame

World Coordinate Frame

Camera (or Eye) Coordinate Frame

ProjectionTransformation P

Normalized Device Coordinate Frame

ViewportTransformation D

Window (or Screen) Coordinate Frame

Object (or Model) coordinates

World coordinates

Camera (or Eye) coordinates

Normalized device coordinates

Window (or Screen) coordinates

Page 4: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Transformations

CSE 167, Winter 2020 4

ModelTransformation M

ViewTransformation V

Object (or Model) Coordinate Frame

World Coordinate Frame

Camera (or Eye) Coordinate Frame

ProjectionTransformation P

Normalized Device Coordinate Frame

ViewportTransformation D

Window (or Screen) Coordinate Frame

World coordinate frame

Object (or model)coordinate frame

Camera (or eye)coordinate frame

View frustum in camera (or eye)

coordinate frame

Canonical view volume in normalized device coordinate frame

(x0, y0)

(x1, y1)

x

y

window

viewport

Window (or screen) coordinate frame

Page 5: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Pipeline for minimal operation

• Vertex stage (input: position per vertex; color per triangle)

– Transform position (object to screen space)

– Pass through color

• Rasterizer

– Pass through color

• Fragment stage (output: color)

– Write to color planes

5CSE 167, Winter 2020

Color per triangle. No shading.

Page 6: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Result of minimal pipeline

6CSE 167, Winter 2020

But order is incorrect; blue sphere is closer

to the eye

Page 7: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Hidden surface elimination

• We have discussed how to map primitives to image space

– Projection and perspective are depth cues

– Occlusion is another very important cue

7CSE 167, Winter 2020

Page 8: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Back face culling

• For closed shapes you will never see the inside

– Therefore only draw surfaces that face the camera

– Implement by checking n . vn

vn

v

8CSE 167, Winter 2020

Page 9: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Painter’s algorithm

• Simplest way to do hidden surfaces

• Draw from back to front, use overwriting in framebuffer

9CSE 167, Winter 2020

Page 10: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

B

A

C

E

D

F AB

C

F

D

E

Painter’s algorithm

• Amounts to a topological sort of the graph of occlusions

– That is, an edge from A to B means A sometimes occludes B

– Any sort is valid

• ABCDEF

• BADCFE

– If there are cycles there is no sort

10CSE 167, Winter 2020

[Fo

ley

et a

l.]

Page 11: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Painter’s algorithm

• Useful when a valid order is easy to come by

• Compatible with alpha blending

11CSE 167, Winter 2020

[Fo

ley

et a

l.]

Page 12: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

The z buffer

• In many (most) applications maintaining a z sort is too expensive– Changes all the time as the view changes

– Many data structures exist, but complex

• Solution: draw in any order, keep track of closest– Allocate an extra channel per pixel to keep track of

closest depth so far

– When drawing, compare object’s depth to current closest depth and discard if greater

– This works just like any other compositing operation

12CSE 167, Winter 2020

Page 13: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

The z buffer

–Another example of a memory-intensive brute force approach that works and has become the standard

[Fo

ley

et a

l.]

13CSE 167, Winter 2020

Page 14: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Precision in z buffer

• The precision is distributed between the near and far clipping planes

– This is why these planes have to exist

– Also why you cannot simply set them to very small and very large distances

14CSE 167, Winter 2020

Page 15: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Interpolating in projection

Linear interpolation in screen space ≠ Linear interpolation in eye space

15CSE 167, Winter 2020

Page 16: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Pipeline for basic z buffer

• Vertex stage (input: position per vertex; color per triangle)

– Transform position (object to screen space)

– Pass through color

• Rasterizer

– Interpolated parameter: z’ (screen z)

– Pass through color

• Fragment stage (output: color, z’)

– Write to color planes only if interpolated z’ < current z’

16CSE 167, Winter 2020

Color per triangle. No shading.

Page 17: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Result of z-buffer pipeline

17CSE 167, Winter 2020

Order is correct; blue sphere is

closer to the eye

Page 18: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Flat shading

• Shade using the real normal of the triangle

– Same result as ray tracing a bunch of triangles

• Leads to constant shading and faceted appearance

– Truest view of the mesh geometry

18CSE 167, Winter 2020

[Fo

ley

et a

l.]

Page 19: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Pipeline for flat shading

• Vertex stage (input: position per vertex; color and normal per triangle)– Transform position and normal (object to eye space)– Compute shaded color per triangle using normal– Transform position (eye to screen space)

• Rasterizer– Interpolated parameters: z’ (screen z)– Pass through color

• Fragment stage (output: color, z’)– Write to color planes only if interpolated z’ < current z’

19CSE 167, Winter 2020

Shaded color per triangle

Page 20: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Transforming normal vectors

• Tangent vector t at surface point X is orthogonal to normal vector n at X

• Transformed tangent vector and transformed normal vector must also be orthogonal

CSE 167, Winter 2020 20

Page 21: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Transforming normal vectors

• Tangent vector can be thought of as a difference of points, so it transforms the same as a surface point

• Normal vector does not transform the same as tangent vector

CSE 167, Winter 2020 21

How is M related to A?

We are only concerned about direction of vectors, so do not

add translation vector

Page 22: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Transforming normal vectors

• How is M related to A?

• Solve for M

• Transform normal vectors using

CSE 167, Winter 2020 22

Page 23: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Result of flat-shading pipeline

23CSE 167, Winter 2020

Page 24: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

[Go

ura

ud

th

esis

]

• Often we’re trying to draw smooth surfaces, so facets are an artifact– Compute colors at

vertices using vertex normals

– Interpolate colors across triangles

– “Gouraud shading”– “Smooth shading”

24CSE 167, Winter 2020

[Fo

ley

et a

l.]

Gouraud shading

Page 25: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Pipeline for Gouraud shading

• Vertex stage (input: position, color, and normal per vertex)

– Transform position and normal (object to eye space)

– Compute shaded color per vertex

– Transform position (eye to screen space)

• Rasterizer

– Interpolated parameters: z’ (screen z); r,g,b color

• Fragment stage (output: color, z’)

– Write to color planes only if interpolated z’ < current z’

25CSE 167, Winter 2020

Shaded color per vertex. Interpolate shaded colors of vertices over triangle.

Page 26: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Result of Gouraud shading pipeline

26CSE 167, Winter 2020

Page 27: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Local vs. infinite viewer, light

• Phong illumination requires geometric information:

– Light vector (function of position)

– Eye vector (function of position)

– Surface normal (from application)

• Light and eye vectors change

– Need to be computed (and normalized) for each vertex

27CSE 167, Winter 2020

Page 28: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Local vs. infinite viewer, light

• Look at case when eye or light is far away:

– Distant light source: nearly parallel illumination

– Distant eye point: nearly orthographic projection

– In both cases, eye or light vector changes very little

• Optimization: approximate eye and/or lightas infinitely far away

28CSE 167, Winter 2020

Page 29: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Directional light

• Directional (infinitely distant) light source

– Light vector always points in the same direction

– Often specified by position [x y z 0]

– Many pipelines are faster if you use directional lights

29CSE 167, Winter 2020

Page 30: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Infinite viewer

• Orthographic camera

– Projection direction is constant

• “Infinite viewer”

– Even with perspective, can approximate eye vector using the image plane normal

– Can produce weirdness for wide-angle views

– Blinn-Phong: light, eye, half vectors all constant!

30CSE 167, Winter 2020

Page 31: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Vertex normals

• Need normals at vertices to compute Gouraud shading

• Best to get vertex normalsfrom the underlying geometry– For example, spheres

• Otherwise, have to infer vertex normals from triangles– Simple scheme: average

surrounding face normals

31CSE 167, Winter 2020

[Fo

ley

et a

l.]

Page 32: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Non-diffuse Gouraud shading

• Can apply Gouraudshading to any illumination model– It is an general

interpolation method

• Results are not so good with fast-varying models like specular ones– Problems with any

highlights smaller than a triangle

32CSE 167, Winter 2020

[Fo

ley

et a

l.]

Page 33: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Per-pixel (Phong) shading

• Get higher quality by interpolating the normal

– Just as easy as interpolating the color

– But now we are evaluating the illumination model per pixel rather than per vertex (and normalizing the normal first)

– in pipeline, this means we are moving illumination from the vertex processing stage to the fragment processing stage

33CSE 167, Winter 2020

Page 34: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Per-pixel (Phong) shading

• Bottom line: produces much better highlights

34CSE 167, Winter 2020

[Fo

ley

et a

l.]

Page 35: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Pipeline for per-pixel shading

• Vertex stage (input: position, color, and normal per vertex)– Transform position and normal (object to eye space)

– Transform position (eye to screen space)

– Pass through color

• Rasterizer – Interpolated parameters: z’ (screen z); r,g,b color; x,y,z

normal

• Fragment stage (output: color, z’)– Compute shading using interpolated color and normal

– Write to color planes only if interpolated z’ < current z’

35CSE 167, Winter 2020

Shaded color per pixel from interpolated colors and normals of vertices over triangle

Page 36: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Result of per-pixel shading pipeline

36CSE 167, Winter 2020

Page 37: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

Programming hardware pipelines

• Modern hardware graphics pipelines are flexible– Programmer defines exactly what happens at each stage

– Do this by writing shader programs in domain-specific languages called shading languages

– Rasterization is fixed-function, as are some other operations (depth test, many data conversions, …)

• One example: OpenGL and GLSL (GL Shading Language)– Several types of shaders process primitives and vertices;

most basic is the vertex program

– After rasterization, fragments are processed by a fragment program

37CSE 167, Winter 2020

Page 38: Introduction and Overview - Computer Science · 2020-01-30 · Framebuffer Uniform variables Attributes Varying parameters Varying parameters Depth Color Rasterizer Vertex program

GLSL Shaders

38

Framebuffer

Uniform variables

Attributes

Varying parameters

Varying parameters

ColorDepth

Rasterizer

Vertex program

Fragment program

Triangles

Application

CSE 167, Winter 2020