Path: chuka.playstation.co.uk!news From: Steve Hunt Newsgroups: scee.yaroze.programming.2d_graphics,scee.yaroze.programming.3d_graphics Subject: 3D coord to 2D screen coord Date: Sat, 23 May 1998 17:26:32 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 64 Message-ID: <3566F8B8.228F@itallnight.u-net.com> NNTP-Posting-Host: p106.nas1.is5.u-net.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 3.01Gold (Win95; I) Xref: chuka.playstation.co.uk scee.yaroze.programming.2d_graphics:363 scee.yaroze.programming.3d_graphics:582 Hi, Someone a bit ago ask how to convert a 3d x,y,z coord into a 2d screen x,y coord. Well here's some code that does just that.... Code assumes that the 2d origin is in the center of the screen. ProjectionDistance is what ever value you passed to GsSetProjection(); Call InitTransProj() once to set things up, then TransProj to get the screen coord of your 3d object. Enjoy, Steve CODE........ void InitTransProj(void); void TransProj(VECTOR *pos, short *x, short *y); static GsCOORDINATE2 trans; // ------------------------------------------------------------------------- // init trans proj coord // ------------------------------------------------------------------------- void InitTransProj() { GsInitCoordinate2(WORLD, &trans); } // ------------------------------------------------------------------------- // Trans Proj - convert 3d x,y,z to 2d screen x,y // ------------------------------------------------------------------------- void TransProj(VECTOR *pos, short *x, short *y) { MATRIX mat; VECTOR v; trans.coord.t[0] = pos->vx; trans.coord.t[1] = pos->vy; trans.coord.t[2] = pos->vz; trans.flg = 0; GsGetLs(&trans, &mat); ApplyMatrixLV(&mat, pos, &v); if(mat.t[2]) { *x = ProjectionDistance * v.vx / mat.t[2]; *y = ProjectionDistance * v.vy / mat.t[2]; } else { *x = 0; *y = 0; } }