# Drawing Projected Vertices/Faces In Order

I recently created a 3d engine, and have gotten to what is (as I've found) the hardest part so far. I need to sort the objects and then draw them. I've looked at sorting algorithms, but my case is rather specialized. I have an array of vertices stored inside the Face3D object which I've created, and need to draw faces correctly when overlapping, Firstly, am I thinking about this correctly? Draw the back vertices first? Secondly, How would I sort this? Here's my code for the Face3D file:

package me.dylan.ThirdDimension.src;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class Face3D {
Polygon face=new Polygon();
ArrayList Lines = new ArrayList();
ArrayList Vertices = new ArrayList();
Color color;
public Face3D(int x,int y, int z, int width, int height, int depth,Main m,Color c) {
color=c;
for(int i=0;i
Vertices.get(i).update();
Point2D tmp = new Point2D.Double();
tmp.setLocation((int)Vertices.get(i).x2d, (int)Vertices.get(i).y2d);
}

}

public void paint(Graphics g) {
g.setColor(color);
Graphics2D g2d = (Graphics2D)g;
face=new Polygon();
for(int i=0;i<Vertices.size();i++) {
Vertices.get(i).update();
Point2D tmp = new Point2D.Double();
tmp.setLocation((int)Vertices.get(i).x2d, (int)Vertices.get(i).y2d);
}
g2d.fillPolygon(face);

}


}

I'd just like to apologize if my question is unclear, it's difficult for me to explain this.

• Are you using Face3D to draw a 3D shape (like a cube) and are wondering which faces to draw first (so that the back faces are not above the front faces)? – 3aw5TZetdf Jan 7 '13 at 0:07
• Yes, that's exactly what I'm trying to do, thanks. – Dylan Katz Jan 7 '13 at 0:13
• I would maybe try sorting them based on the highest depth of each Face of the 3D shape. If you have 6 Faces with 4 points in each, get the deepest point in each Face and sort the Faces by that factor. This should work because it draws the deeper Faces first, then the ones closer to you (hope this makes sense). – 3aw5TZetdf Jan 7 '13 at 0:24
• No problem, I actually had a problem similar to yours so I am glad it worked! – 3aw5TZetdf Jan 7 '13 at 6:00

It appears that you are trying to write a 3D render engine, and you want the faces to be rendered from front to back so you don't get an x-ray effect. Your current method would not work with intersecting faces.

# Z-Buffering

A Z-buffer is an array that stores the z-distance of every pixel in the viewport: Image from Wikipedia: Original page.

When you are rendering, you calculate the Z-distance of the current pixel. You then compare that to the corresponding Z-distance in the buffer for that pixel. If the distance is less than that of the Z-buffer, you draw the pixel, and write the distance to the Z-buffer. If, however, the distance is greater, you skip that pixel and continue rendering.

For example:

drawPixel(int x, int y, int zDist, Color c)
{
if(zDist < zBuffer[x][y])
{
pixel[x][y] = c;
zBuffer[x][y] = zDist;
}
}


I'm sure there are optimizations you could make, but this is the general way of doing it.

• Z-buffer optimizations can probably be found in one of the GPU gems or programming gems books, I forget which. – jzx Feb 11 '15 at 1:22