Rendering 3D-Coordinates into 2D

Started by
4 comments, last by d h k 18 years, 11 months ago
Hello, this is my first post here so I hope I am writing in the right area. I am currently writing my own simple 3D-Engine and everything is fine so far. But I have a little problem: In order to draw pixels on the screen with my engine I have to use a simple DrawLine function that draws line between two two-dimensional points ( with x and y coordinates ). But I use three-dimensional coordinates ( with x, y and z coordinates ) in my engine. Now I have to render or translate those three-dimensional coordinates with that additional depth coordinate z into simple two-dimensional coordinates. I have tried to figure out the right algorhytm for days, but I just don't seem to make any progress at all. I wrote two functions, that are supposed to do the whole rendering or translating process. Here are the two functions:

function TranslateCoordinatesX ( int pointx, int pointz )
// translates three-dimensional points with x, y and z coordinates to two dimensional
// ones with only x and y coordinates
{
	// define the new x-coordinate
	int pointnewx;
	
	// translate the coordinates
	if ( pointx < 160 )
		pointnewx = pointx + pointz;
	else if ( pointx > 160 )
		pointnewx = pointx - pointz;
	else if ( pointx == 160 )
		pointnewx = pointx;
	
	// return the new x-coordinate
	return pointnewx;
}

That is the function, that translates the x coordinate by adding the z coordinate to it and then returning it.

function TranslateCoordinatesY ( int pointy, int pointz )
// translates three-dimensional points with x, y and z coordinates to two dimensional
// ones with only x and y coordinates
{
	// define the new y-coordinate
	int pointnewy;
	
	// translate the coordinates
	if ( pointy < 100 )
		pointnewy = pointy + pointz;
	else if ( pointy > 100 )
		pointnewy = pointy - pointz;
	else if ( pointy == 100 )
		pointnewy = pointy;
	
	// return the new y-coordinate
	return pointnewy;
}

THis function is supposed to do the same for the y coordinate. I am aware of the fact that this algorythm I am using right now is wrong. Right now the functions take the original x ( or y ) value and check if it is left of the middle of the screen. If it is left then they add the z value, if it is right, they substract the z value if it is right in the middle of the screen, the z value does not change anything at all. Can anyone give me any hints or atleast push me in the right direction? Thanks in advance, Jan.
Advertisement
A push in the right direction would be to divide x and y by z, but i's rather look into Direct3D or OpenGL!
it appears that by engine you meant api. i did that back around '96 or '97 i think but it was in euphoria and asm. unless you have a good reason, i'd learn opengl or direct3d.
This space for rent.
Thank you for the quick replies.

I have a good reason why I am not using Direct 3D or OpenGL: I am working with the inbuilt script language of "Adventure Game Studio" or "AGS". It is known for not being able to do any 3D so I am trying to show that it is possible with just using a simple DrawLine "( int X1, int Y1, int X2, int Y2 );" function.

That little hint did help me, it seems to work now, I'll edit this post once I get a chance to really test it.

Thanks again,

Jan!
Use Thales theorum:
X = x/z , Y = y/z

Your function should look something like:
float [] project2d ( float [] coords3d ){        float [] coords2d = new float [ 2 ];                coords2d [ 0 ] = ( DistProjectionPlane * coords3d [ 0 ] )         					/ ( coords3d [ 2 ] + DistProjectionPlane );                coords2d [ 1 ] = -1 * ( DistProjectionPlane * coords3d [ 1 ] )        					/ ( coords3d [ 2 ] + DistProjectionPlane );                coords2d [ 0 ] += SCREEN_WIDTH / 2;        coords2d [ 1 ] += SCREEN_HEIGHT / 2;        return coords2d;}


Where:
DistProjectionPlane = the distance between the "eye" and the "screen" (where the points will be projected)
coords3d[0] = x
coords3d[1] = y
coords3d[2] = z

This is a good site.
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
Allright, thank you very much everybody, I got it to work.

These forums here seem to be quite awesome, on another forum it took ages for someone to reply...

This topic is closed to new replies.

Advertisement