English Amiga Board Polygon transformation problem
 Register Amiga FAQ Rules & Help Members List  /  Moderators List Today's Posts Mark Forums Read

 05 March 2020, 16:58 #1 TCH Newbie Amiga programmer   Join Date: Jun 2012 Location: Front of my A500+ Age: 33 Posts: 182 Polygon transformation problem I just begun experimenting with vertorgraphics and i'm trying to transform a 3D polygon into a 2D one, but i get zeros for all transformed coordinates. I implemented the matrixless version of the algorithm on Wikipedia. Here is the code: Code: ```#include #include #include typedef struct { float x, y, z, thx, thy, thz, costhx, costhy, costhz, sinthx, sinthy, sinthz; } Camera; typedef struct { float x, y; int16_t sx, sy; } Point2D; typedef struct { int16_t VertexCount; Point2D *Vertices; } Polygon2D; typedef struct { float x, y, z; } Point3D; typedef struct { int16_t VertexCount; Point3D *Vertices; } Polygon3D; void RecalcCameraAngles(Camera *C) { C->costhx = cos(C->thx); C->costhy = cos(C->thy); C->costhz = cos(C->thz); C->sinthx = sin(C->thx); C->sinthy = sin(C->thy); C->sinthz = sin(C->thz); } void TransformPolygon3DToPolygon2D(Polygon3D *P3D, Camera *C, Polygon2D *P2D) { int16_t k, vc; Point2D *p2D; Point3D *p3D; float x, y, z, costhx, costhy, costhz, sinthx, sinthy, sinthz, dx, dy, dz, tmp0, tmp1, px, py, Cx, Cy, Cz; const float ex = 0; const float ey = 0; const float ez = 500; costhx = C->costhx; costhy = C->costhy; costhz = C->costhz; sinthx = C->sinthx; sinthy = C->sinthy; sinthz = C->sinthz; Cx = C->x; Cy = C->y; Cz = C->z; p3D = &P3D->Vertices[0]; p2D = &P2D->Vertices[0]; for (k = 0; k < vc; ++k) { x = p3D->x - Cx; y = p3D->y - Cy; z = p3D->z - Cz; tmp0 = sinthz * y + costhz * x; dx = costhy * tmp0 - sinthy * z; tmp0 *= sinthy; tmp0 += costhy * z; tmp1 = costhz * y - sinthz * x; dy = sinthx * tmp0 + costhx * tmp1; dz = costhx * tmp0 - sinthx * tmp1; tmp0 = ez / dz; px = tmp0 * dx + ex; py = tmp0 * dy + ey; p2D->x = px; p2D->y = py; ++p2D; ++p3D; } } int main(int argc, char *argv[]) { Polygon2D P2D; Polygon3D P3D; Camera C; P3D.VertexCount = 5; P3D.Vertices = malloc(5 * sizeof(Point3D)); P3D.Vertices[0].x = 0.0; P3D.Vertices[0].y = 0.8; P3D.Vertices[0].z = 0.5; P3D.Vertices[1].x = 0.5; P3D.Vertices[1].y = 0.6; P3D.Vertices[1].z = 0.5; P3D.Vertices[2].x = 0.2; P3D.Vertices[2].y = 0.4; P3D.Vertices[2].z = 0.5; P3D.Vertices[3].x = 0.2; P3D.Vertices[3].y = 0.4; P3D.Vertices[3].z = 0.5; P3D.Vertices[4].x = 0.5; P3D.Vertices[4].y = 0.6; P3D.Vertices[4].z = 0.5; C.x = 0.0; C.y = 0.0; C.z = 0.0; C.thx = 0.0; C.thy = 0.0; C.thz = 0.0; RecalcCameraAngles(&C); P2D.VertexCount = 5; P2D.Vertices = malloc(5 * sizeof(Point2D)); TransformPolygon3DToPolygon2D(&P3D, &C, &P2D); printf("%f, %f\n", P2D.Vertices[0].x, P2D.Vertices[0].y); printf("%f, %f\n", P2D.Vertices[1].x, P2D.Vertices[1].y); printf("%f, %f\n", P2D.Vertices[2].x, P2D.Vertices[2].y); printf("%f, %f\n", P2D.Vertices[3].x, P2D.Vertices[3].y); printf("%f, %f\n", P2D.Vertices[4].x, P2D.Vertices[4].y); free(P2D.Vertices); free(P3D.Vertices); return 0; }``` Can anyone tell me what i am doing wrong?
 05 March 2020, 17:55 #2 deimos Registered User   Join Date: Jul 2018 Location: France Posts: 545 You never set vc.
 05 March 2020, 18:07 #3 StingRay move.l #\$c0ff33,throat   Join Date: Dec 2005 Location: Berlin/Joymoney Posts: 6,367 Exactly. Enable warnings in your compiler, then you can spot such mistakes easily.
 05 March 2020, 18:39 #4 TCH Newbie Amiga programmer   Join Date: Jun 2012 Location: Front of my A500+ Age: 33 Posts: 182 DOH! Thanks guys. In the original function which held all the parts it was set, but i missed this, when i splitted it for tests. Warnings were enabled, but not with `-Wall`, i switched to that. Last edited by TCH; 05 March 2020 at 22:52.
 07 March 2020, 16:20 #5 TCH Newbie Amiga programmer   Join Date: Jun 2012 Location: Front of my A500+ Age: 33 Posts: 182 Okay, it seems to work, but some other questions have emerged. deimos' suggestion about setting `ex` and `ey` to 0 was correct, but giving `ez` the value of 500 yielded positionally correct but extraordinally overzoomed coordinates. However if `ez` supposed to be the distance between the eye and the selected point and here the eye is actually the camera, then isn't `ez` supposed to be the distance between the camera's current point and the currently transformed point in the vectorspace? In short: Code: ```x = p3D->x - Cx; y = p3D->y - Cy; z = p3D->z - Cz; ez = sqrt((x * x) + (y * y) + (z * z));``` I've tried it and i got good results, but due to the lack of experience i am unable to tell if my guess was correct or this is just coincidence.

 Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 Similar Threads Thread Thread Starter Forum Replies Last Post Samurai_Crow project.Amiga Game Factory 0 12 December 2017 10:06 volvo_0ne Coders. AMOS 24 30 January 2016 22:20 petran support.WinUAE 14 10 December 2013 16:12 Topgun project.MAGE 14 08 June 2006 23:19 Echo support.WinUAE 38 20 March 2003 21:00

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home News Main     Amiga scene     Retrogaming General Discussion     Nostalgia & memories Support     New to Emulation or Amiga scene         Member Introductions     support.WinUAE     support.WinFellow     support.OtherUAE     support.FS-UAE         project.AmigaLive     support.Hardware         Hardware mods         Hardware pics     support.Games     support.Demos     support.Apps     support.Amiga Forever     support.Amix     support.Other Requests     request.UAE Wishlist     request.Old Rare Games     request.Demos     request.Apps     request.Modules     request.Music     request.Other     Looking for a game name ?     Games images which need to be WHDified abime.net - Hall Of Light     HOL news     HOL suggestions and feedback     HOL data problems     HOL contributions abime.net - Amiga Magazine Rack     AMR news     AMR suggestions and feedback     AMR data problems     AMR contributions abime.net - Home Projects     project.Amiga Lore     project.EAB     project.IRC     project.Mods Jukebox     project.Wiki abime.net - Hosted Projects     project.aGTW     project.APoV     project.ClassicWB     project.Jambo!     project.Green Amiga Alien GUIDES     project.Maptapper     project.Sprites     project.WinUAE - Kaillera Other Projects     project.Amiga Demo DVD     project.Amiga Game Factory     project.CARE     project.EAB File Server     project.CD32 Conversion     project.Game Cover Art         GCA.Feedback and Suggestions         GCA.Work in Progress         GCA.Cover Requests         GCA.Usefull Programs         GCA.Helpdesk     project.KGLoad     project.MAGE     project.Missing Full Shareware Games     project.SPS (was CAPS)     project.TOSEC (amiga only)     project.WHDLoad         project.Killergorilla's WHD packs Misc     Amiga websites reviews     MarketPlace         Swapshop     Kinky Amiga Stuff     Collections     EAB's competition Coders     Coders. General         Coders. Releases         Coders. Tutorials     Coders. Asm / Hardware     Coders. System         Coders. Scripting         Coders. Nextgen     Coders. Language         Coders. C/C++         Coders. AMOS         Coders. Blitz Basic     Coders. Contest         Coders. Entries Creation     Graphics         Graphics. Work In Progress         Graphics. Finished Work         Graphics. Tutorials     Music         Music. Work In Progress         Music. Finished Work         Music. Tutorials Off Topic     OT - General     OT - Entertainment     OT - Sports     OT - Technical     OT - Gaming

All times are GMT +2. The time now is 10:43.

 -- EAB3 skin ---- EAB2 skin ---- Mobile skin Archive - Top