# Converting 3D vertices to 2D screen coords

This topic is 4104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

/*Heres a quick algorithm to do this no matrices involved. Method Point3d will take x,y and z coordinates and output a 2D coordinate. */ #include <iostream> #include <math.h> using namespace std; const double pi = 3.141592654; int numxpixels(800),numypixels(600); /*NB Use a pointer to return the array from the function*/ double* Point3d(double x,double y, double z,double Axo = 30.0,double Txz=0.0,double Tyz =0.0) { double x1,y1,z1,xn,yn,zn; x1 = x*cos(Txz*pi/180.0) + z*sin(Txz*pi/180.0); y1 = y; z1 = z*cos(Txz*pi/180.0)-x*sin(Txz*pi/180.0); xn = x1; yn = y1*cos(Tyz*pi/180.0)-z1*sin(Tyz*pi/180.0); zn = z1*cos(Tyz*pi/180.0)+y1*sin(Tyz*pi/180.0); double xy2d [] = {numxpixels/2.0-1.0+(xn-zn)*cos(Axo*pi/180.0),numypixels/2.0-1.0-yn+(xn+zn)*sin(Axo*pi/180.0)}; return xy2d; } int main() { double* xy = Point3d(30.0,50.0,-70.0); cout<<"X="<<xy[0]<<"\n"<<"Y="<<xy[1]<<"\n"; printf("test \n"); return 0; }

##### Share on other sites
To say the least, this is really useless. Using a transformation matrix is the prefered way. This way it is also possible to have arbitrary projections.
You also use a lot of trig functions which are definitly slower than the matrix approach.
Appart from that you are returning a pointer to a stack allocated array which will result in undefined behaviour.

##### Share on other sites
What's Axo, Txz, Tyz???

Quote:
 numxpixels/2.0

Use numxpixels*0.5

Quote:
 Heres a quick algorithm to do this no matrices involved.

Matrices are used to order data. In math, matrices are used to represent an equation system. It's a way of representing everything. Not another method. Functions could be written without using matrices, but it would confuse developers after heavy usage

I'm also confused because I don't see where are you taking the viewport and the projection formula into account (maybe it has to do with Axo Txz and Tyz..)

Dark Sylinc

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015577
×