# Manually computing draw positions...

## Recommended Posts

Hi all,

I've set up world/view/projection matrixes for a game.  In another part of the program, where I don't have easy access to the information used to set up the matrixes, I want to manually transform some points to make them float over the 3D objects.

I tried to do it this way:

1. Get the world matrix from DirectX

2. Get the view matrix from DirectX

3. Get the projeciton matrix from DirectX

4. Multiply those together

5. Use those matrixes to transform a point

When I do this... well, plenty goes wrong.  For one thing, rotations seem to be flipped horizontally, and it also doesn't scale correctly to the screen's resolution.

Is there a specific way to manually transform a point to match the matrixes that have already been sent to DirectX?

Short version: I want to manually transform an xyz position as if it went through the pipeline.

Thanks!

Edited by Raptisoft

##### Share on other sites

How did your step 5 look like? Should be equivalent to:

point = point * world;
point = point * view;
point = point * proj;
point = point / point.w;


##### Share on other sites

I had everything except your operation with point.w ... my points have xyz... what's w?  Far plane?  Viewport?

##### Share on other sites

It is the fourth component of your vector. If you want to do the operations manually, you need to work with four-dimensional homogeneous vectors; 3-dimensional vectors with only X, Y and Z-components are not enough.

##### Share on other sites
6. Apply the viewport transformation e.g. (left, top, width, height defining the viewport):
  screenX = (1.0f + v.X) * 0.5f * width + left;
screenY = (1.0f - v.Y) * 0.5f * height + top;

Usually, graphics math libraries already have such a function, called Project, e.g. D3DXVec3Project or XMVector3Project Edited by unbird

##### Share on other sites
D3DVIEWPORT9 v9;
D3DXMATRIX World;
D3DXMATRIX View;
D3DXMATRIX Projection;
D3DXMatrixIdentity(&World);
g_pDevice->GetViewport(&v9);
g_pDevice->GetTransform(D3DTS_VIEW,&View);
g_pDevice->GetTransform(D3DTS_PROJECTION,&Projection);
D3DXVECTOR3 Vec3Dst2D;
D3DXVec3Project(&Vec3Dst2D,&SrcVec3,&v9,&Projection,&View,&World);

##### Share on other sites

As an aside, using the DirectX device as a mechanism to get a matrix from one portion of your program to another seems like poor design (and prone to breakage). Consider refactoring your code so that required information is explicitly given to the place where it's needed.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978708

• 21
• 14
• 12
• 10
• 12