Sign in to follow this  
Phynix

3D projection

Recommended Posts

OK, first of all, I know this question is relatively simple. I've decided to make a 3D graphics library as a hobby project. But now i have to start the 3d part. So, this is my question: How would I go about converting a 3D world coordinate to 2D object coordinates? i'm using pixeltoaster, so i have to manipulate by pixel. I already have rectangle/line drawing down, along with colors. My line code is as below: (the quad code isn't much use here, since most 3d objects don't translate to perfect rectangles)
void drawPixel(int x, int y){
	if(x>0&&y>0&&x<width&&y<height){
		position=(y*width)+x;
		if(position>pixels[0].size())return;
		if(ca==0){
			pixels[0][position].r=cr;
			pixels[0][position].g=cg;
			pixels[0][position].b=cb;
		}else{
			pixels[0][position].r=(pixels[0][position].r+cr)*ca;
			pixels[0][position].g=(pixels[0][position].g+cg)*ca;
			pixels[0][position].b=(pixels[0][position].r+cb)*ca;
		}
	}
}
void drawPixel(float x, float y){
	if(x-(int)x>=0.5f)x=x+1;
	if(y-(int)y>=0.5f)y=y+1;
	drawPixel((int)x,(int)y);
}
void swap(int &a, int &b){
	int newB=a;
	a=b;
	b=newB;
}
void swap(float &a, float &b){
	float newB=a;
	a=b;
	b=newB;
}
void drawLine(int x1, int y1, int x2, int y2){
	if(x1>x2)swap(x2,x1);
	if(y1>y2)swap(y2,y1);
	float diff=max(abs(x2-x1),abs(y2-y1));
	float xadd=(abs(x2-x1)/diff);
	float yadd=(abs(y2-y1)/diff);
	float cx=x1;
	float cy=y1;
	for(float i=0;i<diff;i++){
		drawPixel(cx,cy);
		cx+=xadd;
		cy+=yadd;
	}
}
Any help would be very much appreciated. Thanks.

Share this post


Link to post
Share on other sites
This article could be useful: http://msdn.microsoft.com/en-us/library/bb206260%28VS.85%29.aspx (it assumes a left hand coordinate system like DirectX, which is totally arbitrary and up to you)

Share this post


Link to post
Share on other sites
I think you should try to come up with the formula on your own. If you have too much difficulty, I can give you the answer. But you'll learn much more if you do it yourself.

Share this post


Link to post
Share on other sites
Trig isn't for converting from 3D to 2D, only for rotations in 3D etc.

If you have a 3D world where Z increases the further away an object is, the 2D x/y is just X / Z and Y / Z. That will mean that if an object is 10 feet wide, or whatever unit you use, on the x-axis, and is 10 feet away, then its 2D x will be 1. To render it appropriately you multiple it by some fitting value, usually (screenWidth / 2).
Since you also want an object in the center in front of the camera to be at the center of the screen, you add (screenWidth / 2) to it too.
Y is usually considered positive in the 'up' direction, which is the opposite of the screen where it's usually positive downwards, so you get y from (screenHeight / 2) - (screenWidth / 2) * (Y / Z).

When you rotate your camera, that's when the trig comes in. You can move your camera by simply subtracting the camera position from the vertices.

Share this post


Link to post
Share on other sites
Quote:
Original post by Phynix
well, back to the trig textbooks...


You don't need trig or textbooks. I did it when i was 11, using the math I knew at the time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this