Math : How to convert a 3D World to 2D Screen coordinate
I'm looking for a way to convert 3D xyz coordinates to 2D xy (pixel) c开发者_Go百科oordinates. I'm getting a list of coordinates which I need to plot on a 2d plane.
The plane will always be a top-down view width the following dimensions width:800 px, height 400px
The 3D world coordinates can contain negative values aswell ranging from -4000 to 4000. I have read a few conversion articles on Wikipedia and a couple of SO threads but they either didn't fit my needs or they were too complex for my limited math knowledge.
I hope someone can help me. Thank you for your time.
Regards, Mark
you can use something like [(x/z),(y/z)] to project 3d to 2d - I believe this is a fairly crude method and I would think that 3d to 2d Googlings would return some fairly standard algorithms
Rob is more or less correct, just that normally a scaling factor needs to be used (i.e [k*(x/z), k*(y/z)]). If you never change your point or direction of view, all the math you need to fully understand why this works are the intercept theorems.
I think the standard implementation of this uses so-called homogenous coordinates, which is a bit more complicated. But for a quick-and-dirty implementation just using 'normal' 3D coordinates works fine.
You also need to be a bit careful when dealing with coordinates that are behind your point of view. In fact, this is what I have found the most ugly part of (polygon-based) 3D graphics.
You may find this interesting: A 3D Plotting Library in C#.
Something that may help: Some code i'm working on...
// Location in 3D space (x,y,z), w = 1 used for affine matrix transformations...
public class Location3d : Vertex4d
{
// Default constructor
public Location3d()
{
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 1; // w = 1 used for affine matrix transformations...
}
// Initiated constructor(dx,dy,dz)
public Location3d(double dx, double dy, double dz)
{
this.x = dx;
this.y = dy;
this.z = dz;
this.w = 1; // w = 1 used for affine matrix transformations...
}
}
// Point in 2d space(x,y) , screen coordinate system?
public class Point2d
{
public int x { get; set; } // 2D space x,y
public int y { get; set; }
// Default constructor
public point2d()
{
this.x = 0;
this.y = 0;
}
}
// Check if a normal vertex4d of a plane is pointing away?
// z = looking toward the screen +1 to -1
public bool Checkvisible(Vertex4d v)
{
if(v.z <= 0)
{
return false; // pointing away, thus invisible
}
else
{
return true;
}
}
// Check if a vertex4d is behind you, out of view(behinde de camera?)
// z = looking toward the screen +1 to -1
public bool CheckIsInFront(Vertex4d v)
{
if(v.z < 0)
{
return false; // some distans from the camera
}
else
{
return true;
}
}
Some clipping to be done if the vertecies are outside the screen area !!!
精彩评论