使用多边形创建形状不能正常工作 Java3d
Shape Creation using polygons doesnt work properly Java3d
总结一下这个问题,我有使用四边形创建立方体的代码,但没有这样做,一些面无缘无故地丢失了,希望有人能帮助我 :)
同样出于某种原因它不会产生控制台错误
这是用来创建立方体的代码:
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;
}
}
(大小是一个浮点数,在本例中等于 1)
多边形 1 是保存所有四边形的变量,其点与形状的点相同。
但我没有完全创建立方体,而是缺少底面和背面,我尝试了多个订单,但似乎没有任何效果。
提前致谢:)
我在@QBrute 的帮助下找到了修复方法,问题是出于某些明显的原因,我需要在创建一些面时向后索引点,但如果你的四边形不只显示,它就可以工作尝试向后索引它们:)
您看到的是 Back-face culling。根据创建三边形/四边形的顺序(顺时针或逆时针添加顶点),您可以告诉您的框架该面是正面还是背面。
取决于框架如何处理 face culling,面部是否可见(剔除)。
看起来 java-3d 默认启用了背面剔除。因此,对于您的示例,您只需要颠倒添加顶点的顺序即可。 (例如,对于底面,它的索引是 3,2,1,0
而不是 0,1,2,3
)
总结一下这个问题,我有使用四边形创建立方体的代码,但没有这样做,一些面无缘无故地丢失了,希望有人能帮助我 :)
同样出于某种原因它不会产生控制台错误
这是用来创建立方体的代码:
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;
}
}
(大小是一个浮点数,在本例中等于 1)
多边形 1 是保存所有四边形的变量,其点与形状的点相同。
但我没有完全创建立方体,而是缺少底面和背面,我尝试了多个订单,但似乎没有任何效果。
提前致谢:)
我在@QBrute 的帮助下找到了修复方法,问题是出于某些明显的原因,我需要在创建一些面时向后索引点,但如果你的四边形不只显示,它就可以工作尝试向后索引它们:)
您看到的是 Back-face culling。根据创建三边形/四边形的顺序(顺时针或逆时针添加顶点),您可以告诉您的框架该面是正面还是背面。
取决于框架如何处理 face culling,面部是否可见(剔除)。
看起来 java-3d 默认启用了背面剔除。因此,对于您的示例,您只需要颠倒添加顶点的顺序即可。 (例如,对于底面,它的索引是 3,2,1,0
而不是 0,1,2,3
)