The information in this article applies to:
SUMMARY
DPTri is a sample that demonstrates Direct3D Immediate Mode to render a
triangle using DrawPrimitive API. To close the application, select Esc and then Alt+F4.
D3ddptri.exeFor more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address http://www.microsoft.com/downloads/search.aspand then click How to use the Microsoft Download Center. MORE INFORMATIONWhen you run the self-extracting executable, the following files are expanded:
How the Sample WorksThe following discussion is a "walkthrough" of the code.The entry point of the sample is the same as any other Windows program, WinMain(). This function is where all initialization takes place for the sample. The first initialization registers the window class and creates the window. After the window is successfully created, the program enters an infinite message loop until it closes. DirectX programs (particularly games) should only run when the program has the activation. If it loses activation, or if the program is paused, the program should just process Window's messages instead of running the game loop and trying to render. There are no differences in registering the window class for a DirectX program. The sample registers the class with an icon. This icon is used by Windows to visibly represent the application on the task bar when the program loses activation with a task switch. No menu is used since the sample is a full-screen exclusive mode application. After the standard CreateWindow, ShowWindow, and UpdateWindow calls, the sample initializes DirectDraw and Direct3D in InitDDrawAndD3D(). The window callback procedure processes Windows messages. WM_ACTIVATEAPP must be handled for a "well-behaved" program. When the program loses activation, set a flag and do not enter the game loop. Let other programs use the CPU for a while. When the program gains activation, make sure to restore the program's internal status including DirectX. Hide the Windows cursor in the WM_SETCURSOR case as described. All DirectDraw programs should render their own cursor. Do not let Windows handle cursor rendering unless a flickering cursor is a feature of the program. Determine the cursor position with GetCursorPos then render your own software cursor. All the other messages are self-explanatory. Initializing DirectDraw and Direct3D can be complex. The sample uses a user-defined data structure to hold some global information. The keywords for initialization are "never assume anything about the hardware." Follow these steps to initialize DirectDraw and Direct3D:
Setting up the matrices requires defining the world, view, and projection matrices, associate them with the IDirect3DDevice by calling IDirect3DDevice::CreateMatrix and IDirect3DDevice::SetMatrix methods. In the SetupLightAndViewport function, a directional light is created and a viewport is setup to map the 3D world to the visible 2D window. To give the triangle and background some visual appeal, it needs to have colors to define each. D3D materials are used for this purpose. If you use 3D HW acceleration, the color of the triangle will be green. If you use SW, the color will be red. On DX5, if MMX is detected, the triangle will be yellow. The background will always be a shade of blue. The world, view, and projection transforms are specified with IDirect3DDevice2::SetTransform. Gouraud shade is turned on with IDirect3DDevice2::SetRenderState along with other D3DRENDERSTATEs. After you have created everything, you are ready to render the triangle. First, clear the viewport and Z-buffer with Clear. Then render the triangle with BeginScene, DrawPrimitive, and EndScene. Those 4 APIs usually are called together in that sequence. Make sure to not call SetRenderState with each frame. This is an expensive operation. If the render state doesn't change, then apply it once. When using DrawPrimitive or DrawIndexedPrimitive, make sure to send as many triangles in one call as possible (that is, don't call the API for each triangle). Applications can minimize vertices to optimize for speed by rearranging a list of triangles to use D3DPT_TRIANGLESTRIP or D3DPT_TRIANGLEFAN. With each frame, the triangle is animated by modifying the world matrix to apply a rotation around the Y-axis and then making the four rendering calls:
Finally, the image is made visible with a call to IDirectDrawSurface::Flip.
During execution the application may be deactivated and switched away. Make sure to pause all processing as mentioned earlier. When the application is activated, restore all surfaces and reload any artwork onto respective surfaces. The sample doesn't use any textures so IDDrawSurface::Restore is sufficient. When cleaning up, release all objects in the reverse order of creation. Notice that only the front buffer is released, and that the back and the Z buffer are not released. Releasing a surface with attached surfaces will release all attached surfaces. Additional query words:
Keywords : kbfile kbsample kbDirect3dIM KbDirectX KbDirectX500 kbSDKWin32 kbfaq |
Last Reviewed: January 21, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |