A CStatic-Derived Class to Load and Render .x Files | CodeGuru

A CStatic-Derived Class to Load and Render .x Files

Overview CMeshViewer is a CStatic-derived class, which I wrote to use .x files in an application with DX 9.0. I had to develop some cool 3D shapes. Of course, this can be done by manual drawing, but the shapes can be changed from time to time, so I have to write this class which can […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 15, 2004
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Overview

CMeshViewer is a CStatic-derived class, which I wrote to use .x files in an application with DX 9.0. I had to develop some cool 3D shapes. Of course, this can be done by manual drawing, but the shapes can be changed from time to time, so I have to write this class which can load .x model and the basic transformations can be applied to them.

I learned the .x file loading & rendering from Andi Pike’s tutorials and also I was inspired by Jason Hattingh’s “A CStatic derived class using Direct3D Retained Mode.”

Combining what I learned from both of them and the DirectX SDK is the CMeshViewer & supporting classes.

How to Use the Class

Using CMeshViewer is very simple. All you need is to put a CStatic on your dialog box and create a variable of CMeshViewer and put the following lines in your OnInitDialog.

m_mvViewer.Create("" , WS_VISIBLE|SS_NOTIFY|SS_SUNKEN ,
                  CMeshViewer::GetRect(IDC_VIEWER, this),
                  reinterpret_cast<CWnd*>(this));
m_mvViewer.DefaultInit();
//
// IDC_VIEWER is considered to be the name of the static control
// & m_mvViewer is the name of the variable
//
// You can put the code for loading the model with the physical
// path here, or in this sample; this will be done through the
// Load Model button.
//
//
m_mvViewer.StartRendering();

That’s it. Trust me, that’s all you need to do. If you keep on loading multiple .x files, they’ll be placed at the center and you can just click any object; the object will be turned into the Wireframe mode. Now, you can use the navigation buttons to perform the basic transformations. Also, for the sake of this example, you can vary the values of Eye Point, Look At Position, and Up Direction and observe the difference.

Advertisement

Files

CordinateManager.cpp
CordinateManager.h
CCordinateManager: Helping class for managing the coordinates.

D3DCamera.cpp
D3DCamera.h
CD3DCamera: This class manages View, Projection, Eye Point, Look At Position, and Up Direction.

D3DLight.cpp
D3DLight.h
CD3DLight: As above, this is also a simple wrapper on D3DLIGHT9.

Mesh.cpp
Mesh.h
CMesh: This is a wrapper on the ID3DXMesh, LPDIRECT3DDEVICE9, D3DMATERIAL9, and LPDIRECT3DTEXTURE9. Individual mesh rendering is performed in this class.

MeshManager.cpp
MeshManager.h
CMeshManager: This class uses an STL map at the back for managing multiple models.

Model.cpp
Model.h
CModel: This class encapsulates CMesh. Individual model transformation and rendering are performed by this class.

D3DMeshApplication.cpp
D3DMeshApplication.h
CD3DMeshApplication: This is one of the core class. It encapsulate the functionality of LPDIRECT3D9 and LPDIRECT3DDEVICE9.

MeshViewer.cpp
MeshViewer.h
CMeshViewer: This is the CStatic-derived class. It separates the underlying details and provides a simplified interface to interact with.

WI_HeaderFile.h
This file contains the enum declarations, custom messages, and SCordinateInfo structure.

Conclusion

If you don’t like to get your hands dirty, it’s all finished. But, if you like to get deep into the stuff, then it’s for you.

CMeshViewer contains the CD3DMeshApplication. It’s as though CMeshViewer forwards the operation to be performed to the CD3DMeshApplication and displays the rendered output itself.

CD3DMeshApplication is where DX comes in. It encapsulates LPDIRECT3D9, LPDIRECT3DDEVICE9, CD3DCamera, CD3DLight, CMeshManager, CCordinateManager. So this class holds every other class, which performs their separate tasks. In case of mouse click for the object/model selection, CMeshViewer passes x,y coordinates to CD3DMeshApplication, which are then transformed into 3D point from 2D and try to find that either we have hit the object by ray casting or not. It also manages the overall rendering.Rest of the functionality is just forwarded to the other respective classes.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.