Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
73 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Introduction
 Getting Started
 The Main Loop
 Drawing Something
 The Theory

 Drawing Something
 The Practical Part


 Printable version
 Discuss this article
 Get the source

The Series
 Part 1
 Part 2
 Part 3

Drawing Something - The Theory

Okay, so you’ve learnt how to initialize a Direct3D application in Visual Basic - wow. Hardly cutting edge visuals, and completely useless as well. So I’m pretty sure that you want to learn how to draw something.

Drawing In Direct3D is extremely simple when you get your head around it, but it requires a fair amount of work and memory before you can get to this point. The first part is understanding what the different words mean - right now we’ll stick to the simple definitions and elaborate on them as we get more advanced later on.

1. Vertices (plural of vertex)
A vertex can be thought of as a defining point - the corner of a triangle, square or other shape. Using vertices we can construct 2D and 3D shapes with various properties; a vertex will be described by a Visual Basic User Defined Type (we’ll see these later) and are usually made up of a position, colour and texture coordinates.

2. Polygon
Polygons are what you’ll have heard about by "normal" people most of the time - so and so 3D card pumps out 101 million polygons a second (or whatever). In fact, Direct3D renders all of it’s primitives using triangles. But then again, a triangle is the simplest possible polygon. Lists of triangles are stored as arrays of the vertex type that you are using.

3. Face
A face is usually 3 vertices arranged in some sort of polygon, but it also has an orientation - you can tell which way it is facing. Direct3D interpolates vertex components across a face, in particular colour - as we’ll be seeing later on. 3D models (imported from 3D modelling programs) tend to be made up of 100’s or 1000’s of faces.

4. Textures
A texture is applied to a triangle to make it look more real, Textures are just 2D bitmaps/pictures loaded from the hard drive into memory and then mapped to relevant polygons during rendering. We will cover these in more detail later on.

5. Mesh
A mesh is another word for a model - it is usually represented by one object in the program, and contains 100s or 1000s of vertices and faces, along with all relevant materials and textures. These will be covered later on.

Now that you have those words floating around we can start doing some interesting things. Don’t swear by the above definitions though - they are very lose and are only there to offer a basic introduction to the terms, more complex and meaningful definitions will be offered as and when they are necessary.

The first thing I want to cover is the 3 types of vertex that you can use. While the structure of Direct3D allows for 100’s of different combinations there are three types that will serve most situations - and all other situations will be adaptations or modification of these three.

1. Untransformed and Unlit vertex
These vertices are literally just points in 3D space with an orientation and a set of texture coordinates. Direct3D will do the lighting for you - you set up some lights and some vertices and it’ll do the rest. These tend to be the most commonly used unless people opt for using lightmaps or pre-calculated lighting (as some commercial games do).

2. Untransformed and Lit vertex
These vertices are points in 3D space with texture coordinates like the first type, but these ones have a colour value. This allows us to make proper 3D geometry without having to worry about lighting. When we start doing 3D geometry these will be the first type to use - as they’re the easiest.

3. Transformed and Lit vertex
These are vertices specified in two dimensions - screen coordinates. All Direct3D does is apply textures to them, clip them and draw them - you are expected to specify a colour value and a valid 2D position. Using these are the only way you will get Direct3D to do 2D graphics.

Yet more things to remember… But before we go through a simple demonstration of how to use these vertices there is one more thing you need to know. Flexible Vertex Formats. As I mentioned earlier, Direct3D allows for 100’s of possible vertex types - it is through this system of Flexible Vertex Formats (FVF) that we achieve this. A flexible vertex format description is a variable of type Long that is a combination of flags that Direct3D can use to work out what format the data you pass it is in. If you pass invalid, or incorrect for the data you use one of two things will happen: Nothing will be rendered, or something very strange will be rendered.

The vertex formats for the 3 main types look like this:

Const FVF_TLVERTEX = (D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR)
Const FVF_LVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR Or D3DFVF_TEX1)
Const FVF_VERTEX = (D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1)

And their UDT structures will look like this:

Private Type TLVERTEX
  X As Single
  Y As Single
  Z As Single
  rhw As Single
  color As Long
  Specular As Long
  tu As Single
  tv As Single
End Type

Private Type LITVERTEX
  X As Single
  Y As Single
  Z As Single
  color As Long
  Specular As Long
  tu As Single
  tv As Single
End Type

Private Type VERTEX
  X As Single
  Y As Single
  Z As Single
  nx As Single
  ny As Single
  nz As Single
  tu As Single
  tv As Single
End Type

Right this second you don’t really need to know all of these - I put them in mainly for reference purposes; those of you familiar with DirectX7 in Visual Basic, or DirectX in another language may well want to jump ahead slightly.

In the next section we’ll extend our sample program to render some basic 2D and 3D geometry in fullscreen mode. It may sound like a small task, but you’ve got the mountain to climb yet my friend.



Next : Drawing Something - The Practical Part