05 March 2020, 15:58 | #1 |
Newbie Amiga programmer
Join Date: Jun 2012
Location: Front of my A500+
Age: 38
Posts: 372
|
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 <stdint.h> #include <stdlib.h> #include <math.h> 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; } |
05 March 2020, 16:55 | #2 |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
You never set vc.
|
05 March 2020, 17:07 | #3 |
move.l #$c0ff33,throat
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
|
Exactly. Enable warnings in your compiler, then you can spot such mistakes easily.
|
05 March 2020, 17:39 | #4 |
Newbie Amiga programmer
Join Date: Jun 2012
Location: Front of my A500+
Age: 38
Posts: 372
|
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 21:52. |
07 March 2020, 15:20 | #5 |
Newbie Amiga programmer
Join Date: Jun 2012
Location: Front of my A500+
Age: 38
Posts: 372
|
Okay, it seems to work, but some other questions have emerged.
deimos' suggestion about setting exand eyto 0 was correct, but giving ezthe value of 500 yielded positionally correct but extraordinally overzoomed coordinates. However if ezsupposed to be the distance between the eye and the selected point and here the eye is actually the camera, then isn't ezsupposed 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)); |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Mike the Magic Dragon: in-game problem or WinUAE problem? | petran | support.WinUAE | 33 | 16 August 2023 20:29 |
[Factory]3d polygon techniques | Samurai_Crow | project.Amiga Game Factory | 0 | 12 December 2017 09:06 |
Polygon Zones, and why not? | volvo_0ne | Coders. AMOS | 24 | 30 January 2016 21:20 |
Problem with Kickstarts [rare problem with faulty scripts in early v1.1 release] | Topgun | project.MAGE | 14 | 08 June 2006 22:19 |
Polygon game speeds | Echo | support.WinUAE | 38 | 20 March 2003 20:00 |
|
|