more 3d - overview sample4 adds: depth buffer (z buffer)evan.weaver/gam666a/3dgraphics2.pdf ·...

11
GAM666 – Introduction To Game Programming Sample4 adds: Depth Buffer (Z Buffer) Lighting Alpha Blending (Transparency) More 3D - Overview

Upload: others

Post on 04-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

Sample4 adds:

● Depth Buffer (Z Buffer)

● Lighting

● Alpha Blending (Transparency)

More 3D - Overview

Page 2: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

A depth buffer is memory matching the dimensions of the backbuffer

● Used to determine whether the pixel that the program wants to write to the screen is in front or behind the pixel that is already there, so the pixel can avoid being drawn if it is behind

● Keeps track of the Z coordinate of the projected data (viewable range: 0-1) for each pixel on the screen

● Works best with hardware support

Depth Buffers

Page 3: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

● Bit depth of the depth buffer may or may not be the same as that of the backbuffer (some hardware requires they be the same, some hardware can only do a 16-bit depth buffer regardless of the colour bit depth)

● Because of the nature of the projection calculation, far away points have closer z-coords than near points, and rounding error can lead to artifacts off in the distance

● Higher bit depth means more accuracy for far away objects, at the price of expensive video memory

Depth Buffers

Page 4: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

● Typically, the depth buffer is called a Z-buffer, since it caches the z coordinate of each pixel

● Some hardware has support for W-buffers, which spread the z-coordinates more evenly across the 0-1 range for fewer artifacts, but it hasn't been widely implemented

● The depth buffer should be cleared to 1.0 (the farthest away z value in projected space) when the backbuffer is cleared, using the Direct 3D device Clear() function at the start of a frame

Depth Buffers

Page 5: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

● The depth buffer is created by setting the EnableAutoDepthStencil and AutoDepthStencilFormat fields of the D3D device presentation parameters

● The Format of the depth buffer specified its bit depth

● Depth buffer functionality can be turned on and off while rendering different parts of a frame using SetRenderState() (for special effects)

● If hardware depth buffer support isn't available, all rendering must be done in software (if you want to use a depth buffer)

Depth Buffers

Page 6: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

Direct3D has a lighting engine which allows you to specify light sources and the light reflectivity of the things you render (material)

● The number of lights is hardware dependent, but you can generally count on up to 7 simultaneous lights

● The lighting engine does NOT account for things blocking the light (doesn't do shadows)

● Use Direct 3D device functions SetLight() and LightEnable() to set up a light and turn it on/off

Lighting

Page 7: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

● The D3DLIGHT9 structure lets you describe a light source

● There are 3 Types of light sources:● D3DLIGHT_POINT – a point source, which has a location but no direction

● D3DLIGHT_DIRECTIONAL – a directional source (from “infinitely” far away) which has a direction but no location

● D3DLIGHT_SPOT – a spot light which has a location and a direction, as well as angles defining the spread of the light

Lighting

Page 8: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

● Lights have three different colour components:● Diffuse – the normal colour of the light subject to the direction it hits a surface

● Ambient – background colour lighting all sides equally

● Specular – Colour of shine (if specular highlights, an expensive feature, are enabled)

● All except directional lights can have attenuation and range settings to control fade over distance

● There is also a SetRenderState setting to arrange a global ambient light independent of all other light sources

Lighting

Page 9: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

● For lighting to work, you must also specify a material with each primitive you render

● The D3DMATERIAL9 structure lets you describe a material:

● The colour for diffuse, ambient and specular reflectivity

● The sharpness (power) of the specular highlights (if enabled)

● These colours are factored with the colours of the lights themselves to determine what the user sees

Lighting

Page 10: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

● When using the lighting engine, each vertex must include a “normal vector”, which pointing the direction the surface faces at that vertex, used for lighting calculations

● This normal vector is often perpendicular to the triangle, but can be slightly off perpendicular to simulate curved surfaces (instead of facetted surfaces)

● The Flexible Vertex Format (FVF) setting must be adjusted to indicate the presence of the normal vector in the vertex data

Lighting

Page 11: More 3D - Overview Sample4 adds: Depth Buffer (Z Buffer)evan.weaver/gam666a/3dgraphics2.pdf · GAM666 – Introduction To Game Programming Typically, the depth buffer is called a

GAM666 – Introduction To Game Programming

Transparency (Alpha Blending) can be enabled, which then uses the “alpha” component of colour to specify the level of transparency, where 0 is invisible and 1 is opaque (when using the D3DXCOLOR macro that specifies RGBA values between 0 and 1)

● SetRenderState can turn alpha blending on and off – aplha blending is expensive so only turn it on to draw something transparent

● Draw transparent things last, since they'll potentially update the z-buffer (which doesn't understand transparency)

Alpha Blending