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; Vertices.add(new Point3D(x, y, z, m)); Vertices.add(new Point3D(x, y+height, z, m)); Vertices.add(new Point3D(x+width, y+height, z+depth, m)); Vertices.add(new Point3D(x+width, y, z+depth, m)); 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); face.addPoint((int)Math.round(tmp.getX()),(int)Math.round(tmp.getY())); }
} 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); face.addPoint((int)tmp.getX(),(int)Math.round(tmp.getY())); } g2d.fillPolygon(face); }
}
I'd just like to apologize if my question is unclear, it's difficult for me to explain this.
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)? \$\endgroup\$