World to Screen ERROR HELP PLEEZ !!!

Started by
9 comments, last by kandemor 19 years, 4 months ago
Hola, i'm trying to convert a point from 3d coords to screen coords inside an mfc application, back converting from screen to world works perfectly but ther's something very very strange doing w2s, her's what i do: first i convert my point from model to world than i convert it from world to view and finally to proj, all this using the GetTransform method, than normalizing the result dividing by W produces bad coords and to be more explicit all corners are not inside a range of 2 (l:-1,r:1,u:-1,d:1), it seems that changing the point z will produces a bigger error her's my lame code: Skipping the model to world transform because it is an identity matrix for this test p got my point 3d coords


	D3DXMATRIXA16 view,proj;
	g_pd3dDevice->GetTransform(D3DTS_VIEW, &view);
	g_pd3dDevice->GetTransform(D3DTS_PROJECTION, &proj);

	float x0 = p.x * view(0,0) + p.y * view(1,0) + p.z * view(2,0) + view(3,0);
	float y0 = p.x * view(0,1) + p.y * view(1,1) + p.z * view(2,1) + view(3,1);
	float z0 = p.x * view(0,2) + p.y * view(1,2) + p.z * view(2,2) + view(3,2);	
	

	float x2 = x0 * proj(0,0) + y0 * proj(1,0) + z0 * proj(2,0) + proj(3,0);
	float y2 = x0 * proj(0,1) + y0 * proj(1,1) + z0 * proj(2,1) + proj(3,1);
	float w2 = x0 * proj(0,3) + y0 * proj(1,3) + z0 * proj(2,3) + proj(3,3);	

	float xs0 = x2/w2;
	float ys0 = y2/w2;
xs0 and ys0 coords in normalized screen space are wrongs and the error gets bigger zooming da view. What's wrong with it?!?!?!?????????? I've tried to divide by z transformed in proj, but it seems to change nothing :( Thanx in advance, any help appreciated :) !!
Advertisement
Transformed coordinates are not guaranteed to be within [-1,1], that's just the cube they are clipped against (well [0,1] for Z). So coordinates can be outside the cube and that means they are not visible on screen. The transformation does not perform any clipping for you.

The closer you get to an object the more "outside" the cube it will be.
ok, but my problem is, what d3d draws is not the same i draw transforming my self the point, i'd like to test if a point drawed using d3d is the same i draw in 2d converting it by using the same transforms, so d3d use world,view and proj to convert the point in screen space and i use those transforms to convert my point in screen space too, the result is not the same, my point got a strange offset vs the d3d one, so i guess ther's something less in my process or wrong too................. !!!
Try using this function

D3DXVECTOR3 x;

D3DXVec3Project(&x, &m_vPoints, &viewport, &proj, &view, &world);

VinCenT.
Incredible but even that doesn't works............ wrong coords, getting crazy !!!
Maybe other parts of your code are wrong, like draw code for example.

Actually, I'm using this function and works very well.

If you want to render lines, try to use the directx structure LPD3DXLINE.

Good luck!

VinCenT.
Currently i'm not drawing anything, just convert coords from world to screen, thanx anyway :(!!!
Are you setting things like the view/perspective matrices. The D3D function might not work with out it.
I had a very similar issue but using OpenGL, and I reckon the function I used looks very similar to the one suggested

D3D3VectorProject(..) - DirectX
gluProject(..) - OpenGL

Basically what my problem was that I was not setting the OpenGL context. Im no genius on DirectX, but could it be a similar fix, perhaps your camera or projection is set up incorrectly. You did say you have not drawn anything, perhaps you should to check out your camera/projection setup is correct.
ok so, my world matrix is at identity state, ma view matrix is generated by:
		D3DXMatrixLookAtLH( &matView, 			&D3DXVECTOR3( c_source.x, c_source.y, c_source.z ), 			&D3DXVECTOR3( c_target.x, c_target.y, c_target.z ),			&D3DXVECTOR3( c_roll.x, c_roll.y, c_roll.z ) );			g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );


and my projection matrix is generated by:

	D3DXMatrixPerspectiveFovLH( &matProj, (D3DX_PI*0.25f),  (float)Width / (float)Height , 100.0f, 100000.0f );	g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );


i think it's all setted roght, i've tried to pick up the current matrix sets(world, view, proj) and it seems to be all right, so i actually don't know what to do anymore, even da viewport, i've tried to set it as the back buffer res but nothing to do, my program is an mfc app !!!

[Edited by - giuseppeCT on November 30, 2004 12:39:38 PM]

This topic is closed to new replies.

Advertisement