View Single Post
Old 05 March 2020, 15:58   #1
TCH
Newbie Amiga programmer
 
TCH's Avatar
 
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;
}
Can anyone tell me what i am doing wrong?
TCH is offline  
 
Page generated in 0.04304 seconds with 11 queries