Jump to content
  • Advertisement
Sign in to follow this  
blueshogun96

OpenGL [C, DX9] Using ID3DXMatrixStack

This topic is 2957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my first attempt to use matrix stacks to simulate OpenGL's matrix transformation functions. While the concept sounds simple enough, I can never seem to get it to work.

I got the idea from a certain tutorial at codesampler.com (here) and took it on from there. I think I have a good understanding how it works, but nonetheless, it never seems to work for me (even though mine handles things a bit differently). Here's my code (and keep in mind that I'm using C, not C++):


/*
* Direct3D Matrix stack
*/

ID3DXMatrixStack* mstack = NULL;


void dxInitMatrixStack()
{
D3DXMATRIX mtxview;
D3DXVECTOR3 lookat, eyept, up;

// Create the matrix stack and initialize it with the
// identity matrix.
D3DXCreateMatrixStack( 0, &mstack );
mstack->lpVtbl->LoadIdentity( mstack );

// Create a new view matrix that is in the center of 3D space
// with the positive Y axis as the up vector.
lookat.x = lookat.y = lookat.z = 0.0f;
eyept.x = eyept.y = eyept.z = 0.0f;
up.x = up.z = 0.0f;
up.y = 1.0f;

D3DXMatrixLookAtLH( &mtxview, &eyept, &lookat, &up );
D3DDevice_SetTransform( D3DTS_VIEW, &mtxview );
mstack->lpVtbl->LoadMatrix( mstack, &mtxview );
}

void dxLoadIdentity()
{
mstack->lpVtbl->LoadIdentity( mstack );
D3DDevice_SetTransform( D3DTS_WORLD, mstack->lpVtbl->GetTop( mstack ) );
}

void dxTranslatef( float x, float y, float z )
{
D3DXMATRIX trans = *mstack->lpVtbl->GetTop( mstack );

D3DXMatrixTranslation( &trans, x, y, z );
mstack->lpVtbl->MultMatrixLocal( mstack, &trans );
D3DDevice_SetTransform( D3DTS_WORLD, mstack->lpVtbl->GetTop( mstack ) );
}

void dxRotatef( float angle, float x_axis, float y_axis, float z_axis )
{
D3DXMATRIX rot = *mstack->lpVtbl->GetTop( mstack );

angle = D3DXToRadian(angle);

if( x_axis )
D3DXMatrixRotationX( &rot, angle );
if( y_axis )
D3DXMatrixRotationY( &rot, angle );
if( z_axis )
D3DXMatrixRotationZ( &rot, angle );

//D3DXMatrixRotationYawPitchRoll( &rot, y_axis, x_axis, z_axis );
mstack->lpVtbl->MultMatrixLocal( mstack, &rot );
D3DDevice_SetTransform( D3DTS_WORLD, mstack->lpVtbl->GetTop( mstack ) );
}

void dxScalef( float x, float y, float z )
{
D3DXMATRIX scale = *mstack->lpVtbl->GetTop( mstack );

D3DXMatrixScaling( &scale, x, y, z );
mstack->lpVtbl->MultMatrixLocal( mstack, &scale );
D3DDevice_SetTransform( D3DTS_WORLD, mstack->lpVtbl->GetTop( mstack ) );
}

void dxGetModelView( float* m )
{
D3DXMATRIX mtxModelView;
D3DXMATRIX world, view;

D3DDevice_GetTransform( D3DTS_WORLD, &world );
D3DDevice_GetTransform( D3DTS_VIEW, &view );
D3DXMatrixMultiply( &mtxModelView, &world, &view );

memcpy( m, mtxModelView.m, sizeof( float ) * 16 );
}

void dxPushMatrix()
{
mstack->lpVtbl->Push( mstack );
}

void dxPopMatrix()
{
mstack->lpVtbl->Pop( mstack );
}




So, there's everything in a nutshell. I've reviewed this code numerous times and I'm not sure what I'm doing wrong. This sucks. Any help is appreciated, thanks.

Share this post


Link to post
Share on other sites
Advertisement
You're kinda stuck I'm afraid; D3DXMATRIX is a class not a struct so it's not usable from C. I've a bunch of code I use to do the same thing, but I manage my own stack and use D3DMATRIX.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!