OpenGL-style matrix transformations for Direct3D
17th May, 2002
I've spent some time now playing around with OpenGL programming (mainly thanks to the superb tutorials from NeHe), and in general am fairly pleased with the results. However, for various reasons, I recently decided I would like to improve my skills with Direct Graphics (Direct3D for DirectX versions 8 and later).
The first thing that struck me as I started getting this to work was how convoluted the matrix manipulation appeared to be. Whereas in OpenGL I could simply say:
glTranslatef(1, 0, 0);
...in DirectX, I suddenly found that I had to generate a matrix for translation and multiply it by any existing matrix I had already generated.
Of course it didn't take long for me to realise that this is just what OpenGL is doing the whole time, but I still found the OpenGL syntax much easier to read. So I have created a set of functions mirroring a number of the OpenGL matrix transformation commands for DirectX. I have a feeling I may look back on this in a few months time and realise that I don't like this much, but in case it turns out to be useful to anyone else I thought I would make it available here.
The functions I have included within my code are as follows:
|DX function||Equivalent OpenGL function||Description|
|dxLoadIdentity||glLoadIdentity||Loads the current matrix with the identity matrix|
|dxRotatef||glRotatef||Rotates the current matrix around a specified axis by a number of degrees|
|dxScalef||glScalef||Scales the current matrix along each axis|
|dxTranslatef||glTranslatef||Translates the current matrix along each axis|
|dxPushMatrix||glPushMatrix||Pushes the current matrix on to the stack. It can be later retrieved with dxPopMatrix|
|dxPopMatrix||glPopMatrix||Pops the matrix stack, replacing the current matrix with the one below it|
|dxApplyMatrix||-||Sets the DirectX transformation matrix to be the current constructed matrix|
I have chosen this set of functions as they are the only matrix functions that I find myself using. If anyone else finds functions that they use in OpenGL regularly that might be useful additions to this code, then please do send them over to me.
The code for these functions (as a .cpp and a .h file) is included in the following .zip file.
DXGL.cpp and DXGL.h are placed into the public domain and may be modified and redistributed freely.
If you have any comments or suggestions regarding this article, please don't hesitate to contact me.
This article is copyright © Adam Dawes, 2002.