Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screen space coordinates of specific verticies of a 3D model when visible

I'd like to render a model, then, if particular verticies (how would i mark them?) are visible, i want to render something 2D where they are.

How would i go about doing this?

like image 611
George Duckett Avatar asked May 26 '11 10:05

George Duckett


2 Answers

First of all, you need your vertex position as a Vector4 (perspective projections require the use of homogeneous coordinates; set W = 1). I'll assume you know which vertex you want and how to get its position. Its position will be in model space.

Now simply transform that point to projection space. That is - multiply it by your World-View-Projection matrix. That is:

Vector4 position = new Vector4(/* your position as a Vector3 */, 1);
IEffectMatrices e = /* a BasicEffect or whatever you are using to render with */
Matrix worldViewProjection = e.World * e.View * e.Projection;
Vector4 result = Vector4.Transform(position, worldViewProjection);
result /= result.W;

Now your result will be in projection space, which is (-1,-1) in the bottom left corner of the screen, and (1,1) in the top right corner. If you want to get your position in client space (which is what SpriteBatch uses), then simply transform it using the inverse of a matrix that matches the implicit View-Projection matrix used by SpriteBatch.

Viewport vp = GraphicsDevice.Viewport;
Matrix invClient = Matrix.Invert(Matrix.CreateOrthographicOffCenter(0, vp.Width, vp.Height, 0, -1, 1));
Vector2 clientResult = Vector2.Transform(new Vector2(result.X, result.Y), invClient);

Disclaimer: I haven't tested any of this code.

(Obviously, to check if a particular vertex is visible or not, simply check if it is in the (-1,-1) to (1,1) range in projection space.)

like image 75
Andrew Russell Avatar answered Sep 20 '22 18:09

Andrew Russell


Take a look at the occlusion culling features of your engine. For XNA, you can consult the framework guide (with samples) here.

http://roecode.wordpress.com/2008/02/18/xna-framework-gameengine-development-part-13-occlusion-culling-and-frustum-culling/

like image 38
ventaur Avatar answered Sep 20 '22 18:09

ventaur