We help IT Professionals succeed at work.

Sample code for loading .x file model into DirectX

Mohamed Abowarda
Mohamed Abowarda used Ask the Experts™
on
I am looking for sample code to load .x file model into DirectX.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Goto http://www.gtokas.com and browse to TCDX9.
Download the source code (it is for use with C++ Builder but you will find your way).
Check out the TCDXModel class and TCDX9HModel class source code.
Both have functions to load an .x file and create a 3D model.

George Tokas.
Mohamed AbowardaSenior Software Engineer

Author

Commented:
To save time, I am only looking for sample code to load .x file mesh in DirectX, this means the sample code should only include initializing Direct3D and loading the mesh from .x file.

Thanks,
TCDX9Screen class deals with initializing D3D and handle rendering to screen. The main object.

TCDX9Screen *Screen = new TCDX9Screen(AppHandle);
Screen->Create(AppHandle, Width, Height, (bool)WinOrFullScreen);

//3d model based on mesh loaded from x
TCDX9Model  *model = new RCDX9Model(Screen);
model->LoadModel(Screen->GetDevice(), "thefilename.x");

//rendering:
Screen->Clear();
Screen->BeginScene();
model->Render(Screen->GetDevice());
Screen->EndScene();
Screen->Flip();

What else do you need?

George Tokas.
Mohamed AbowardaSenior Software Engineer

Author

Commented:
@gtokas: I am trying to do that without using 3rd party class, perhaps by using D3DXLoadMeshFromX(), I also want to center the mesh position to middle of the screen and have the camera looking at model directly.
Check out the code.
IT IS FREE!!!
It will give you ideas...
THAT IS WHY I posted the link....
D3DXLoadMeshFromX() is used in TCDX9Model class...
Mohamed AbowardaSenior Software Engineer

Author

Commented:
@gtokas: I found the way to load .x file into DirectX in the last few days, however it's not centered infront of the camera and it doesn't look realistic as it was on 3Ds Max, I didn't figure out how to center the mesh and make it appear EXACTLY as it was in 3Ds Max.
The classes have a reason developed this way.
The camera is looking at a specific point.
Your 3D model from 3DSMax has a center of rotation.
Using that point 3DSMax exporter (panda) translates it to an array of vertices and indexes.
Some functions inside the TCDX9Model class are:
1. SetScale(...) for shrink/expand the model.
2. SetPosition(...) for positioning the model in world space.
3. SetRotation(...) because we need that especialy when working with 3DSMax for model creation and not only.
>>and make it appear EXACTLY as it was in 3Ds Max.
Welcome to my world.
3DSMax uses lights and more than DirectX provides.
There are ways to do that as you can see in my web page where the code is.
Depending on the version of DirectX you are using you can use specular and diffuse lights along with (maybe) a kind of cube map or whatever. The other way is the use of shaders.

George Tokas.
Mohamed AbowardaSenior Software Engineer

Author

Commented:
Thanks,