Sign in to follow this  
Jesse Dager

Custom 3D graphics engine

Recommended Posts

Jesse Dager    619

I am constructing a hand held game system out of an atmega328p chip. I have basic functions written for this chip to write to my portable screen such as drawline, drawrect, drawcirc, fillrect, fillcirc, drawpoint, etc. I am attempting to create a basic 3D engine for this system using lines to imitate 3D wireframe graphics. I know that to do this, I need to calculate where the line endpoints would be to give perspective, however, I do not know where to start with these algorithms. How would I write a function that would create a line in 3D virtual space and convert it to a 2D image?

Something like:

void drawline3(int x, int y, int z)
{
     //calculate line position
     drawline(x, y);   //draw the line as a 2D line
}

 

 

 

 

 

P.S. I am new to the forums, so if this is posted in the completely wrong place, I apologize.

 

Share this post


Link to post
Share on other sites
larspensjo    1561

When you have the basic understanding on what math/matrix operations you need, I would recommend to use the glm library. It supports most of the math you need for projections and other matrix operations.

Share this post


Link to post
Share on other sites
Jesse Dager    619
When you have the basic understanding on what math/matrix operations you need, I would recommend to use the glm library. It supports most of the math you need for projections and other matrix operations.

See, my problem with using the GLM library, is it is not available for an avr chip simply because there is not enough memory

Share this post


Link to post
Share on other sites
alvaro    21246
The first time I did any 3D graphics, I used a camera that sits at (0,0,-FOV) looking towards the origin. In that case, the projection is simply:

projected_x = x * FOV / (FOV + z)
projected_y = y * FOV / (FOV + z)

So things with z=0 are seen at "real size" (I was measuring everything in pixels), things that have z>0 are seen smaller, and things that have z<0 are seen larger.

Perhaps that would be an easy place to start for you.

Share this post


Link to post
Share on other sites
Jesse Dager    619
The first time I did any 3D graphics, I used a camera that sits at (0,0,-FOV) looking towards the origin. In that case, the projection is simply:

projected_x = x * FOV / (FOV + z)
projected_y = y * FOV / (FOV + z)

So things with z=0 are seen at "real size" (I was measuring everything in pixels), things that have z>0 are seen smaller, and things that have z<0 are seen larger.

Perhaps that would be an easy place to start for you.

This is a perfect place for me to start! thank you! I still am looking for help on the subject, but at least now I have a basis for what to start my coding on.

Share this post


Link to post
Share on other sites
Jesse Dager    619

I wrote a basic camera movement code that goes with the calculations you posted, Alvaro. It seems to work well other than the glitches encountered when the camera edges touch the lines. This can be fixed, however, easily. I am working on that now. 

I wrote code to draw lines and cubes based on the calcs. I use an LCD header file that handles all the line drawing.

 

Here is my 3D.h file:

int FOV = 80;
int camx;
int camy;
int camz;
int screensizex = 84;
int screensizey = 48;
int campos(int camx1, int camy1, int camz1){
	camx = camx1;
	camy = camy1;
	camz = camz1;
}
int projectedx(int x, int z, int fov){
	int pro_x;
	pro_x =  (x * fov / (fov + z) + (screensizex / 2));
	return(pro_x);
}

int projectedy(int y, int z, int fov){
	int pro_y;
	pro_y = y * FOV / (FOV + z);
	return(pro_y);
}

void drawline3(int x, int y, int z, int x1, int y1, int z1){
	LcdLine( projectedx(x, z, FOV), projectedx(x1, z1, FOV), projectedy(y, z, FOV), projectedy(y1, z1, FOV), PIXEL_ON);
}

void drawcube(int x, int y, int z, int x1, int y1, int z1){
	x = x + camx;
	y = y + camy;
	z = z + camz;
	x1 = x1 + camx;
	y1 = y1 +  camy;
	z1 = z1 + camz;
	//frontface
	if(z << camz){
	drawline3(x, y, z, x1, y, z);
	drawline3(x, y, z, x, y1, z);
	drawline3(x1, y, z, x1, y1, z);
	drawline3(x1, y1, z, x, y1, z);
		//connectors
	drawline3(x, y, z, x, y, z1);
	drawline3(x1, y, z, x1, y, z1);
	drawline3(x, y1, z, x, y1, z1);
	drawline3(x1, y1, z, x1, y1, z1);
	}
	if(z1 << camz){
	//backface
	drawline3(x, y, z1, x1, y, z1);
	drawline3(x, y, z1, x, y1, z1);
	drawline3(x1, y, z1, x1, y1, z1);
	drawline3(x1, y1, z1, x, y1, z1);
	}
}

Share this post


Link to post
Share on other sites
DDoS    312

according to your first code you want "perspective", means you need a perspective transformation, which is not an affine transformation, so you have to use homogenous coordinates

you should setup a basic rendering pipeline to transform you vertices (lineendpoints) to screenspace

 

vertex position -> view transformation (if you want a moving camera) -> perspective transformation -> homogenize your coordinates -> rasterize your graphics

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