Title: Intro to DirectX Part 2
1Intro to DirectX Part 2
- Meshes, Transformations, and Lighting
Danny Dyer
2Information about these slides
- Companion to the more detailed slides available
on the TAGD resources page. - http//www.gettagd.com/resources.php
- You can find the page numbers that reference the
other slides at the bottom right of these slides.
3Meshes Revisited
- Just a few reminders
- What was a mesh?
- Collection of vertices.
- Vertices store information such as position,
normal, and texture coordinates. - Stores other properties such as materials.
- What it looked like in code
- ID3DXMesh g_pMesh //declare our type... in
device_reset() - D3DXCreateBox( pd3dDevice, width, length, height,
g_pMesh, NULL) - g_pMesh-gtDrawSubset(0) //in render()
- g_pMesh-gtRelease() // in device_lost()
5
4Polygons
- A polygon is a closed 3-D figure defined by at
least 3 vertices. - A polygon can have any number of vertices, but
the most useful to us is the triangle. - The three vertices that make up a triangle are
guaranteed to be coplanar which makes rendering
MUCH more efficient. - You can compose any mesh from a set of triangles,
even smooth objects. - Triangles are the (current) standard way of
rendering objects, for the most part. - Each vertex stores several pieces of information.
Among the most important are - Position in our 3-dimensional world.
- Normal
- Color
- Texture Coordinates
5Normals
- Represents the outward direction of the mesh at
that point. - Used for lighting (among other things).
We want a box similar to this. Here red is the
normal to the face.
The vertices to represent this object would be
defined like this.
6Loading Meshes From a File
- DirectX uses a standard .X file format for
loading in mesh data. This is not the only way,
just the easiest. - D3DXLoadMeshFromX( boxmesh.x, D3DX_SYSTEMMEM,
pd3dDevice, - NULL, NULL, NULL, NULL, g_pMesh )
- Once we have our mesh loaded we can use it the
same as before.
7Matrix Transformations
- Transformations are used to convert geometry from
one coordinate space to another. A coordinate
space basically defines what our origin and three
axes are. - You can transform any vector (x, y, z) into
another vector (x', y', z') using a 4 by 4
matrix. - These can be multiplied together in sequence to
convert the vector through several coordinate
spaces. - This is one of the most powerful things in
computer graphics, as everything is based off
these principles. - Note ORDER MATTERS!
- What this tells us is matrix multiplication is
NOT commutative.
8World Transformation
- The positions and normals that are stored inside
of a mesh are considered to be in model space
where each vertex is defined relative to the
models local origin. - A world transformation converts coordinates from
model space to world space, where vertices are
defined relative to an origin common to all the
objects in a scene. - So basically a world transformation puts a model
into the world. - The world transformation can include any
combination of translations, rotations, and
scalings. - You can combine the matrices that produce these
effects into a single matrix to calculate several
transformations all at once. - Since the order of multiplication matters,
generally you will do - world scale rotation translation
9World Transformations
- Translation
- D3DXMatrixTranslation(D3DXMATRIX matrix, float
x, float y, float z) - Scaling
- D3DXMatrixScaling(D3DXMATRIX matrix, float sx,
float sy, float sz) - Rotation
- D3DXMatrixRotationX(D3DXMATRIX matrix, float
angle) - D3DXMatrixRotationY(D3DXMATRIX matrix, float
angle) - D3DXMatrixRotationZ(D3DXMATRIX matrix, float
angle) - D3DXMatrixRotationAxis(D3DXMATRIX matrix,
D3DXVECTOR3 vector, float angle) - An Example
- //rotate everything we render around the X axis
by Pi/2 - D3DXMATRIX rot
10Lighting
- Obviously, lighting is used to illuminate objects
in a scene. - When lighting is enabled in DirectX, the color of
each vertex is calculated for you based on a
combination of - The diffuse and specular colors at the vertex.
- The current material color and the texture, if
one is set. - The light sources color and intensity and the
scene's ambient light level. - Materials
- How the light reflects off a surface.
- Direct light and ambient light levels define the
light that is reflected. - Required if lighting is enabled.
- More advanced ways of doing it later
11Turning on Lighting in DirectX
This is just for diffuse light but would be
similar for ambient and specular.
//Define our light and enable it D3DLIGHT9
d3dLight // Initialize the structure. ZeroMemory
(d3dLight, sizeof(d3dLight)) // Set up a white
point light. d3dLight.Type D3DLIGHT_POINT d3dLi
ght.Diffuse.r 1.0f d3dLight.Diffuse.g
1.0f d3dLight.Diffuse.b 1.0f //Position the
Light d3dLight.Position.x 0.0f d3dLight.Positio
n.y 100.0f d3dLight.Position.z -100.0f //
Don't attenuate. d3dLight.Attenuation0 1.0f
d3dLight.Range 150.0f // Set the
property information for the first
light. pd3dDevice-gtSetLight(0, d3dLight) //actu
ally enable the light now pd3dDevice-gtLightEnable(
0, true)
- //Define our material
- D3DMATERIAL9 mat
- // Set diffuse to a light blue
- mat.Diffuse.r 0.0f
- mat.Diffuse.g 0.5f
- mat.Diffuse.b 1.0f
- mat.Diffuse.a 1.0f
- //set ambient and specular
- //
- //set the material to the device
- //(stays until changed)
- pd3dDevice-gtSetMaterial(mat)
12Lighting Some More
- There are also different types of lights
- Point (the one we used)
- Spot
- Directional
- This is a very simple lighting model, but it will
suffice for now. - Eventually we will want a better way to
illuminate our scenes
13Up Next
- Texturing
- Alpha Blending
- bovicide_at_gmail.com