3D Graphics Programming Problem

Hi all

Ive been experimenting with 3d graphics by using SDL only and lines to draw 3d shapes ( no OpenGL or extension libraries, just pure math )

Ive used this page mainly to guide me on perspective projection but i have no luck
http://en.wikipedia.org/wiki/3D_projection

Ive been trying to draw a cube, but instead it gives me a mirrored dual triangular pyramids

This code here rotates the object as seen from the equations in this link:
http://upload.wikimedia.org/math/1/3/b/13bade57f1707e5e7ff9252adff40eb2.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	double aX, aY, aZ;
	for ( i = 0; i < model->qLength; i++ )
	{
		for ( j = 0; j < 4; j++ )
		{
			aX = model->quadArr[i].pointArr[j].x;
			aY = model->quadArr[i].pointArr[j].y;
			aZ = model->quadArr[i].pointArr[j].z;

			virQuadArr[i].pointArr[j].x = cosd ( -angleY ) * ( sind ( -angleZ ) * ( aY - cY ) + cosd ( -angleZ ) * ( aX - cX ) ) - sind ( -angleY ) * ( aZ - cZ );
			virQuadArr[i].pointArr[j].y = sind ( -angleX ) * ( cosd ( -angleY ) * ( aZ - cZ ) + sind ( -angleY ) * ( sind ( -angleZ ) * ( aY - cY ) - cosd ( -angleZ ) * ( aX - cX ) ) ) + cosd ( -angleX ) * ( cosd ( -angleZ ) * ( aY - cY ) + sind ( -angleZ ) * ( aX - cX ) );
			virQuadArr[i].pointArr[j].z = cosd ( -angleX ) * ( cosd ( -angleY ) * ( aZ - cZ ) + sind ( -angleY ) * ( sind ( -angleZ ) * ( aY - cY ) - cosd ( -angleZ ) * ( aX - cX ) ) ) - sind ( -angleX ) * ( cosd ( -angleZ ) * ( aY - cY ) + sind ( -angleZ ) * ( aX - cX ) );
		}
	}


generally, i project it onto a 2d plane using:
http://upload.wikimedia.org/math/6/8/c/68cb8ee3a483cc4e7ee6553ce58b18ac.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
double dx, dy, dz, dX, dY, dZ;
int bx, by, bX, bY;
for ( i = 0; i < model->qLength; i++ )
{
	for ( j = 0; j < 4; j++ )
	{
		if ( j == 3 )
		{
			dx = virQuadArr[i].pointArr[3].x;
			dy = virQuadArr[i].pointArr[3].y;
			dz = virQuadArr[i].pointArr[3].z;

			bx = ( eZ / dz ) * dx - eX;
			by = ( eZ / dz ) * dy - eY;

			dX = virQuadArr[i].pointArr[0].x;
			dY = virQuadArr[i].pointArr[0].y;
			dZ = virQuadArr[i].pointArr[0].z;

			bX = ( eZ / dZ ) * dX - eX;
			bY = ( eZ / dZ ) * dY - eY;

			drawLine ( bx, by, bX, bY, surface, 0xFF0000 );
		}
		else
		{
			dx = virQuadArr[i].pointArr[j].x;
			dy = virQuadArr[i].pointArr[j].y;
			dz = virQuadArr[i].pointArr[j].z;

			bx = ( eZ / dz ) * dx - eX;
			by = ( eZ / dz ) * dy - eY;

			dX = virQuadArr[i].pointArr[j + 1].x;
			dY = virQuadArr[i].pointArr[j + 1].y;
			dZ = virQuadArr[i].pointArr[j + 1].z;

			bX = ( eZ / dZ ) * dX - eX;
			bY = ( eZ / dZ ) * dY - eY;

			drawLine ( bx, by, bX, bY, surface, 0x0000FF );
		}
	}
}


I understand that my code is very messy and unoptimized, but im just concentrating on getting the core concepts to work with my code

This is a difficult question to answer, i just need another programmer(s) to look at my code to see if its something stupid that ive missed.

I have read up on many things considering this subject and i just cant seems to grasp it easily into code

Let me know if you need any more infomation to help me out here

Many thanks,
Super_stinger
Alright, for anyone else who reads this i found one of the errors, it seems the formula at wikipedia for transferring a 3d point to a 2d plane may be wrong ( or i may of used it wrong ).

I found a better one here
http://upload.wikimedia.org/math/d/4/0/d4069770c68cb8f1aa4b5cfc57e81bc3.png

That fixed one of my problems, though my rotation matrix ( the first bit of code ) seems to be still out of whack
Last edited on
Topic archived. No new replies allowed.