UI: Mixing GDI and 3D AnimationsIf you haven't already viewed my Windowless UI sample you missed out on a really cool show. In this sample I have added a small 3D render engine which shows neat 3D animations and page transitions. While the framework itself works directly with Windows GDI, whenever a new window or event occurs, the window can request a 3D animation powered by DirectX to be played on the window canvas.About DirectXThe DirectX library is one of the most evolving COM libraries Microsoft has put out to date. Almost every version iteration has significant changes to the API. Classes and methods come and go all the time. This is a side-effect of Microsoft trying to keep up with the raging 3D rendering technology and the pursuit for delivering ever-faster 3D games.For quite some time, the DirectX library was reserved for fullscreen 3D games only. However, over time the graphics cards have gotten so clever that they are able to accelerate most operations on a windowed canvas as well. Using DirectXI'm not going to present a tutorial on creating a DirectX application. You can find plenty of good tutorials on the web. Since DirectX is so burgeoning, it is important to read samples that are written for version 9, though.While setting up DirectX in windowed mode is trivial, there are several implications. Most important, you inherit the display format of the Windows desktop. You can request a more suitable format for your surfaces, but most likely you will face trouble when trying to mix it with GDI. This generally means that you can only use the scarce 3D operations, the GDI integration - but may not be able to LockRect the surface to do pixel manipulations.
The general procedure for mixing DirectX with a GDI window in my sample is:
Creating a backdropWe create a copy of the contents of the desktop window to a DirectX surface, because while we are in the render loop we'll continuously copy the backdrop back to the render surface. A normal DirectX application would callDevice::Clear to reset the screen to black, but we'll give the impression that the desktop window is all intact while we're animating sprites on the window.With this illusion we turn a normal GDI desktop window into a 3D rendered target surface without the user even knowing. We can safely animate 3D objects on the DirectX surface and then silently restore the GDI window when we're done. Because of various limitations on the size of surfaces in graphics cards, we'll create the backdrop surface in System Memory. This allows an arbitrary size, but adds the risk of blit accelerations being unsupported on some 2nd generation cards. There are several approaches to making a copy of the GDI window. The reason that I mentioned the Preparing spritesThe idea with the render engine in the sample, is that it takes elements from the window (a header text or an icon) and animates them. Animations are displayed as short page transitions (fades or zooms in and out).The DirectX 9 requires us to treat animating sprites in strictly 3D geometry. We'll create a polygon (wireframe) and put a texture on it. We can animate the sprite simply by stretching or rotating the coordinates of the polygon. DirectX will make sure to paint the texture in the correct perspective as long as we make an effort to calculate all coordinate changes on all 3 axes (x, y and z) in 3D space. Many older generation 3D graphics cards have some nasty restrictions when it comes to creating texture bitmaps. Some cards only allow texture dimensions of the power of 2 (32, 64, 128...), some restrict the size to 256 pixels or less, and many only allow textures to be square in size. AnimatingDrawing the sprites are done using the standard DirectXDrawPrimitive call and with the use of vertex buffers. Coordinate transformations and rotation is done in code, rather than allowing the Direct3D accelerated matrix calculations. This is needed because we want to be absolutely sure that we can place a sprite exactly over the existing graphics - without tearing or pixel-compression errors.
Each animation is described in a simple from/to state diagram. A sprite will animate from a certain position or rotation back to its original position. The animation is scheduled to take a number of milliseconds. During this period each frame animated will smoothly interpolate transformations (horizontal and vertical movement) and rotations from one end-point to another. When no more animations are scheduled, the DirectX layer is shut down and normal GDI processing takes over. It is important to note a severe limitation of this model: that while we're animating, the normal The frame rate is currently controlled by Windows and its message pump. While normal DirectX animations attempt to animate frames whenever the message pump is idle, this sample simply uses an NotesIn the sample provided for download, all code related to the actual animation system is located in the file
UIAnim.cpp . The code is being called from the WM_PAINT message handler located in the UIControls.cpp file.
To run the sample you need to have DirectX 9 installed. If no animations show up, the application has deemed your graphics card too slow to provide smooth animations. Source Code DependenciesMicrosoft Visual C++ 6.0Microsoft DirectX 9 SDK Download Files
|
|
来自: fengit > 《Windowsless UI》