2

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 :)

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Hoax
  • 76
  • 8
  • What framework is that? Can you put the correct tag please? It might also help if you would put some screenshots into the question to show the problem. And also explain your code instead of just dumping lots of code in there. – Zabuzard Jul 20 '21 at 08:30
  • 2
    @Zabuzard i am using the correct tags, the api is called java-3d by oracle :) – Hoax Jul 20 '21 at 08:32
  • Have you tried reversing the order of vertices for the invisible faces? Looks like back-face culling is happening here. Clockwise vs. counter clockwise determines if a face is a front face or a back face – QBrute Jul 20 '21 at 08:33
  • @QBrute so that'd just be instead of indexing 0,1,2,3 for bottom it'd be 3,2,1,0? – Hoax Jul 20 '21 at 08:35
  • Yes like that. I don't know how java-3d works, but you're creating quads so that should be it. – QBrute Jul 20 '21 at 08:38
  • @QBrute and it works :) thx so much! – Hoax Jul 20 '21 at 08:41
  • @Hoax It's not Java 3D "by Oracle", Java 3D was originally created by Sun Microsystems, Oracle abandoned it and it's now maintained by the JogAmp community, you're still using an obsolete version: https://jogamp.org/wiki/index.php?title=Java3D_Overview – gouessej Jul 22 '21 at 19:04

2 Answers2

2

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)

QBrute
  • 4,405
  • 6
  • 34
  • 40
1

I found the fix with the help of @QBrute with the problem being that I need to index the points backwards when creating some of the faces for some apparent reason, but it works so if ur quads aren't showing just attempt to index them backwards :)

Hoax
  • 76
  • 8