javapolygonshapesjava-3dquad

Shape Creation using polygons doesnt work properly Java3d


To sum up the problem, I have code to create a cube using quads but instead of doing so some of the faces are missing for no clear reason and hope that somebody might be able to help me :)

Also for some reason it produces no console errors

This is the code which is supposed to create a cube:

package Main.Shapes;


import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3f;

import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;

public class Square {
    Shape3D shape;
    
    public Square() {
        
    }
    
    public Shape3D Make(float Size) {
        QuadArray polygon1 = new QuadArray (24, QuadArray.COORDINATES);
        
        //Bottom
        polygon1.setCoordinate(0, new Point3f (-Size/2, -Size/2, Size/2));
        polygon1.setCoordinate(1, new Point3f (Size/2, -Size/2, Size/2));
        polygon1.setCoordinate(2, new Point3f(Size/2, -Size/2, -Size/2));
        polygon1.setCoordinate(3, new Point3f(-Size/2, -Size/2, -Size/2));
        
        //Front
        polygon1.setCoordinate(4, new Point3f (-Size/2, -Size/2, Size/2));
        polygon1.setCoordinate(5, new Point3f (Size/2, -Size/2, Size/2));
        polygon1.setCoordinate(6, new Point3f (Size/2, Size/2, Size/2));
        polygon1.setCoordinate(7, new Point3f (-Size/2, Size/2, Size/2));

        //Right
        polygon1.setCoordinate(8, new Point3f (Size/2, -Size/2, Size/2));
        polygon1.setCoordinate(9, new Point3f (Size/2, Size/2, Size/2));
        polygon1.setCoordinate(10, new Point3f(Size/2, Size/2, -Size/2));
        polygon1.setCoordinate(11, new Point3f(Size/2, -Size/2, -Size/2));
        
        //Back
        polygon1.setCoordinate(12, new Point3f (-Size/2, -Size/2, -Size/2));
        polygon1.setCoordinate(13, new Point3f (Size/2, -Size/2, -Size/2));
        polygon1.setCoordinate(14, new Point3f (Size/2, Size/2, -Size/2));
        polygon1.setCoordinate(15, new Point3f (-Size/2, Size/2, -Size/2));
        
        //Left
        polygon1.setCoordinate(16, new Point3f (-Size/2, -Size/2, Size/2));
        polygon1.setCoordinate(17, new Point3f (-Size/2, Size/2, Size/2));
        polygon1.setCoordinate(18, new Point3f(-Size/2, Size/2, -Size/2));
        polygon1.setCoordinate(19, new Point3f(-Size/2, -Size/2, -Size/2));
        
        //Top
        polygon1.setCoordinate(20, new Point3f (-Size/2, Size/2, Size/2));
        polygon1.setCoordinate(21, new Point3f (Size/2, Size/2, Size/2));
        polygon1.setCoordinate(22, new Point3f(Size/2, Size/2, -Size/2));
        polygon1.setCoordinate(23, new Point3f(-Size/2, Size/2, -Size/2));
        

        
        GeometryInfo GI = new GeometryInfo(polygon1);
        
          NormalGenerator normalGenerator = new NormalGenerator();
          normalGenerator.generateNormals(GI);

        shape = new Shape3D(GI.getIndexedGeometryArray());
        return shape;
    }
}

(Size is a float and equal to 1 in this example)

Polygon 1 is the variable that holds all the quads with the points being identical to the points of the shape.

But instead of fully creating the cube the bottom and back faces are missing, I've tried multiple orders but nothing seems to work.

enter image description here

Thanks in advance :)


Solution

  • What you're seeing is called Back-face culling. Depending on the order in which tris / quads are created (vertices are added clockwise or anti-clockwise), you tell your framework if that face is either a front-face or a back-face.

    Depending on how the framework handles face culling, the faces are visible or not (culled).

    It looks like java-3d has back-face culling enabled by default. So for your example, you just need to reverse the order of how the vertices are added. (E.g. for the bottom face it's indices 3,2,1,0 instead of 0,1,2,3)