How to get 3d point of 2d triangle?

324 Views Asked by At

I am writing a 3d graphics engine for a small project. Heres my problem.

I currently have a 3d triangle. I then map each vertex to 2d with the formulas

$ x = 1000*X/Z$

and

$y=1000*Y/Z$

Where x and y are the 2d co-ordinates, and X,Y,Z are the 3d co-ordinate. Now from each mapped vertex I draw the triangle in 2d. However, I am now going to implement a z-buffering system. And that means for each pixel in the 2d triangle, I will need to know it's 3d's co-ordinate counterpart.

I am a little stuck on how to implement this. I tried researching matrices and tried a little algebra myself with no luck. Any help will be greatly appreciated. Thank you for your time!

EDIT: I store the 3d co-ordinates of each triangle, sorry if that wasn't clear.

1

There are 1 best solutions below

5
On BEST ANSWER

With the projection you're using, the 2D point with coordinates $(x,y)$ corresponds to the 3D line $\left\{ \left( tx,\ ty,\ t1000\right) \mid t\in\mathbb R \right\}$. Said differently, any 3D point on that line will be projected to the 2D coordinates $(x,y)$.

Given a 3D triangle $ABC$, it defines 3D plane. If you know a particular 2D point before projection should belong to that 3D plane, just intersect it with the line that matches the point.