Thursday, February 18, 2010

D3D Texture, Light, Blending

I made some progress on these points. Now I'm able to draw something with a texture and light it up. Also, I can alpha blend the texture so that it appears translucent or with an irregular shape as defined by the mask. That is quite cool.

However, what I really want to achieve is something more sophisticated. Say, ultimately, I want to have a 3D figure floating on top of the desktop, with no window boarder, nothing. It seems that I have to use a irregular shaped window so the 3D figure itself defines the window boarder.

The first idea is to get the screen covered by the my program and then render my 3D figure on top of it so that I can create an effect that the 3D figure is floating on top of the desktop. But it does not work quite well because it performs very badly, the window flickers.

Another idea is to use a transparent window and render the 3D figure on top of a certain rarely used color and make this color as the mask color so it is transparent on the client window.

Finally, maybe I can try to use the irregular window based on the SetWindowRgn technique. But a similar performance problem is how to setup the window's region efficiently. If I setup the window region pixel by pixel from the rendered 3D figure, it should be very slow, one can afford doing this normally only once when the program loads.

I'll try the second and the third.

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.