1 / 11

GAM666 – Introduction To Game Programming

GAM666 – Introduction To Game Programming. More 3D - Overview. Sample4 adds: Depth Buffer (Z Buffer) Lighting Alpha Blending (Transparency). GAM666 – Introduction To Game Programming. Depth Buffers. A depth buffer is memory matching the dimensions of the backbuffer

Télécharger la présentation

GAM666 – Introduction To Game Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. GAM666 – Introduction To Game Programming Depth Buffers 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

  3. GAM666 – Introduction To Game Programming Depth Buffers • 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

  4. GAM666 – Introduction To Game Programming Depth Buffers • 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

  5. GAM666 – Introduction To Game Programming Depth Buffers • 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)

  6. GAM666 – Introduction To Game Programming Lighting 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

  7. GAM666 – Introduction To Game Programming Lighting • 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

  8. GAM666 – Introduction To Game Programming Lighting • 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

  9. GAM666 – Introduction To Game Programming Lighting • 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

  10. GAM666 – Introduction To Game Programming Lighting • 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

  11. GAM666 – Introduction To Game Programming Alpha Blending 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)

More Related