![]() |
home | contact us | about us | |
| A DIRECTX CONTROL WITH C# On a recent project to develop software for a medical instrument, we were asked to provide a 3D graphical help facility. So when the user was asked to do something like plug a printer into the USB port on the rear of the unit, a 3D image of the unit would be displayed with the USB port highlighted. This functionality achieved using the CtlDirectXDisplay, a C# Control class, that manages an underlying DirectX device, and draws images onto a Form that would be difficult to achieve with the basic Windows drawing class. Since we developed CtlDirectXDisplay, DirectX now comes with some controls equivilent to the listboxes, radio boxes, and textbox controls. So it would not be difficult to implement an entire user interface entirely in DirectX using this control class. Below is a description of the CtlDirectXDisplay class, how to implement the CtlDirectXDisplay class, and a demonstration project that you can download and hopefully run. The CtlDirectXDisplay class As mentioned above the CtlDirectXDisplay class is a Control class that encapsulates some of the functionality of a DirectX Device object, so the CtlDirectXDisplay's most important member variable is the DirectX device. // the DX device object protected Device m_device = null; Note that this device is protected, and therefore freely available to a Control class that inherits from CtlDirectXDisplay. In brief, to display a DirectX image the following tasks must be implemented:
For moving images, the rendering and frame-advancement are a continuous process. Whenever the application is not doing anything else it should be advancing-frames and rendering a new image as quickly as possible, in the hope that the number of frames displayed per second reaches above 30fps. At this frame-rate the human eye starts to see a smoothly flowing image. For this reason, the advancing-frame and rendering must be called from the main applicational loop. A list of existing instances of CtlDirectXDisplay is held as a static member of the CtlDirectXDisplay class. // static list of all instances of CtlDirectXDisplay private static List<CtlDirectXDisplay> m_Displays = new List<CtlDirectXDisplay>(); This list is updated each time a control is constructed or destructed. Two static functions, AdvanceFrameAll and RenderAll, are called from the main application, and advance and render each CtlDirectXDisplay object. The looping section of the Program.Main function looks like this: // show form and loop Form1 form1 = new Form1(); form1.Show(); while (form1.Created) { // update our DirectX controls CtlDirectXDisplay.AdvanceFrameAll(); // draw controls CtlDirectXDisplay.RenderAll(); // process the event loop Application.DoEvents(); } |
![]() |
|
Implementing the Direct-X Control It is intended that the Direct-X control, CtlDirectXDisplay, will be implemented by deriving a second Control class from it, and that the derived class will override the following functions: public virtual void DrawRenderContent() Draw the image using the m_Device object. public virtual void AdvanceFrame() Calculate the positions of the image elements, world transormation, camera and light positions. The m_iLastTick member variable tracks the time between frames, and can be used to calculate new positions. public virtual void ResetDeviceObjects(Device device) Reset the graphical components. Outstanding Issues The downside of the Direct-X control is the frame rate, which you will notice is slow, less than 50 frames per second for the teapot. For faster frame rates, use simpler objects, or create a windowed DirectX application. If the control is placed on a form, and the form is minimized, an exception is thrown by the DirectX library. Sample Project The sample project displays a form with two DirectX controls, and a rotating teapot in each control. ![]() MORE INFORMATION Microsoft DirectX Developers Center |
![]() |
© 1998-2012 Broccoli Products Ltd Reg Number: 2895355 Reg Office: 27 Old Gloucester Street, London. WC1N 3AX |
Privacy Policy Copyright Notice Liability Disclaimer Contact Us |
|