Tuesday, February 9, 2010

D3D Render and Color Basics

After several demo programmes in the tutorial, I get familar with the basic D3D render procedure now.

In order to draw something on the screen, you need to do some preparation, e.g. create an IDirect3D9 instance and setup the device.
Next, you need to create the buffer to hold your content: basically they are vertices organized in a certain way. You put all your vertices and then optionally, tell D3D how they are organized with another buffer with indices. The indices indicate the order of the vertices in which they are constructed to form triangles.
Now that you have triangles representing your object, you can tell D3D how these triangles should be drawn: only a point at each vertex, a line on each side of the triagle or the triangle surface. This is done by setting the render state option D3DRS_FILLMODE with different values: D3DFILL_POINT, D3DFILL_WIREFRAME or D3DFILL_SOLID.
After setting up necessary transforms, camera, etc. you will be able to render the vertices in the buffer. At this stage, you still have an opertunity to tell D3D what the vertices in the buffer mean exactly: just some isolated point, lines with certain relationship between each other, triangles... by specifing the first parameter of the method DrawIndexedPrimitive or DrawPrimitive which is a D3DPRIMITIVETYPE.


Each vertex can be augmented with a color component. In order to use this feature, you need to define a customized vertex formate and then tell D3D the formate you are using by the method SetFVF with a good parameter.
In case you define a vertex like this:
struct Vertex
{
float x, y, z;
D3DCOLOR color;
};
you will need to use D3DFVF_XYZ | D3DFVF_DIFFUSE as the parameter when calling the SetFVF method.

No comments: