Advertisement

How do I draw a line from model origin to specific position in WordlView

Started by February 02, 2018 01:00 PM
2 comments, last by perkalkyl 7 years ago

Hi!

Pretty new to 3D programming, using SharpDX wrapper to build a 3D world (for testing and learning).

I am adding several visible camera objects (very rudimentary models) in order to visualize different views.  

 

Let's say I have a "world" floor grid covering vectors {0,0,0 - 1,1,0}

I add a pretend camera "CAM2" object at {0.5, 1.5, -1.0}

I am looking at this world by setting the parameters for "CAM1" a worldView projection position at pos: {0.0, 1.5, 1.5} lookat: {0.5, 0.0, 0.5}  (looking down from upper left towards the center of the floor).

I would like to draw a line from the pretend camera "CAM2" model origin, to the center of the floor as it is projected through "CAM1" view.

Obviously a line from "CAM1" to the Lookat point would be invisible. But I can't for my life figure out how to apply the correct conversions to the vector end point for "CAM2".

As can be seen in the snashot, the line (green) from "CAM2" points to.... well.. Russia?? :-D

Can anyone help?

 

BR
Per

 

model.png

should be as simple as transforming that local point to the models world space:

p1 = localPoint * modelWorldMat;

p2 = worldPoint;

draw([p1, p2]);

in the vertex shader, you'll have to then multiply the points by the cam1 view mat and the projection mat

outPos = p * viewMat * projectionMat;

where viewMat is CAM1's view matrix, and p is p1 or p2

EDIT:

so your CAM2 position is the world point, and the "local" point is the direction CAM2 is facing? normally camera's are specified in world coordinates, so the direction should already be in world space

p1 = CAM2.position + CAM2.direction * lineLength;

P2 = CAM2.position;

the direction a camera is facing is usually a unit vector, so if you want a longer line you'll have to multiply that vector by a scalar, like 10 if you wanted the line to be 10 units long

Advertisement

Wow! It turned out to be too simple for me to even try!

Just subtract the "Camera" position from its "lookat" point, and it works as expected!

 Vertices.AddRange(new[]{
 new Vector4(0, 0, 0, 1), Color.V4,
 new Vector4(CameraLookAt.X - Position.X, CameraLookAt.Y - Position.Y, CameraLookAt.Z - Position.Z, 1), Color.V4,
});

But thanx for putting in the effort!

 

Cheers!

This topic is closed to new replies.

Advertisement