Sign in to follow this  
TheQuixotiC

From 3D World to Screen Space

Recommended Posts

TheQuixotiC    142
I try to get screen coordinates from 3d.This code gives me big numbers like x=29892,y=2000.Where is the error in this code ?
	//Multiply the vertex by the world matrix:
	//Vtx is of type D3DXVECTOR3
	D3DXVECTOR4 vMinWorldTmp;
	D3DXVec3Transform(&vMinWorldTmp, &vMinWorld, &WorldMatrix);

	//Multiply it by the view matrix
	D3DXVec3Transform(&vMinWorldTmp, &vMinWorld, &ViewMatrix);

	//Multiply by the projection matrix:
	D3DXVec3Transform(&vMinWorldTmp, &vMinWorld, &ProjectionMatrix);

	//Convert it:
	vMinWorldTmp.x = vMinWorldTmp.x * SCREEN_WIDTH/2 + Position.x + SCREEN_WIDTH/2;
	vMinWorldTmp.y = -vMinWorldTmp.y * SCREEN_HEIGHT/2 + Position.y + SCREEN_HEIGHT/2;

	//Multiply the vertex by the world matrix:
	//Vtx is of type D3DXVECTOR3
	D3DXVECTOR4 vMaxWorldTmp;
	D3DXVec3Transform(&vMaxWorldTmp, &vMaxWorld, &WorldMatrix);

	//Multiply it by the view matrix
	D3DXVec3Transform(&vMaxWorldTmp, &vMaxWorld, &ViewMatrix);

	//Multiply by the projection matrix:
	D3DXVec3Transform(&vMaxWorldTmp, &vMaxWorld, &ProjectionMatrix);

	//Convert it:
	vMaxWorldTmp.x = vMaxWorldTmp.x * SCREEN_WIDTH/2 + Position.x + SCREEN_WIDTH/2;
	vMaxWorldTmp.y = -vMaxWorldTmp.y * SCREEN_HEIGHT/2 + Position.y + SCREEN_HEIGHT/2;
	sprintf(buffer,"X= %f , Y= %f ,X2= %f , Y2= %f ",vMinWorldTmp.x,vMinWorldTmp.y,vMaxWorldTmp.x,vMaxWorldTmp.y);

Share this post


Link to post
Share on other sites
Armadon    1091
Hi there Quixotic,
How are you doing?

[The Spaces]
To better equip ourselves with a decision we have to see where these spaces fit in. Think of them as steps.

Object Space : this is where vertices are defined
World Space : We go to World space from object space with a world transformation matrix
Camera (View) Space : We go here from world space, through a view transformation matrix
Clip (Projection) Space : We go here from view space through a projection
matrix
Screen Space : We go here from the Clip space.

[Where does Screen Space come in?]
Screen space is represented by your display, by default it is set to the entire display. Whenever you want to move back up the steps you use inverse matrices.

D3DXMatrixInverse Function
Calculates the inverse of a matrix
You use this function on the matrix and it returns a matrix that you can use as a step back and just transform your vertices with it :)


Hope this helps.

Share this post


Link to post
Share on other sites
Sr_Guapo    876
Quote:
Original post by TheQuixotiC
so where is the error?


He is just trying to make sure that you have performed all the transformations correctly. There are quite a few steps and many places for something to go wrong.

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this