1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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)? \$\endgroup\$
    – 3aw5TZetdf
    Jan 7, 2013 at 0:07
  • \$\begingroup\$ Yes, that's exactly what I'm trying to do, thanks. \$\endgroup\$
    – Dylan Katz
    Jan 7, 2013 at 0:13
  • 1
    \$\begingroup\$ 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). \$\endgroup\$
    – 3aw5TZetdf
    Jan 7, 2013 at 0:24
  • \$\begingroup\$ No problem, I actually had a problem similar to yours so I am glad it worked! \$\endgroup\$
    – 3aw5TZetdf
    Jan 7, 2013 at 6:00

1 Answer 1

2
\$\begingroup\$

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:

Z-buffer visualization
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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .