- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Java 技术在 netbeans 平台上制作桌面应用程序。我做了一些图像处理、数据库功能、图像捕获过程;但现在我想在 3D View 中绘制图像。
所以,我想首先我必须制作我的 .tiff 16 位灰度图像的点数组,然后使用这个点数组。我尝试了一些东西,我的代码在下面,但它不起作用。
那么我应该如何使用这个点数组在 3D View 中绘制我的图像呢?
import java.awt.BorderLayout;
import com.sun.j3d.utils.universe.*;
import java.awt.image.BufferedImage;
import javax.media.j3d.*;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
public final class HelloJava3Da extends JPanel {
PlanarImage plImg3 = JAI.create("fileload", "C:\\Users\\Desktop\\myImage.tiff");
BufferedImage histImage = plImg3.getAsBufferedImage();
int s = 0, count = 0;
public HelloJava3Da() {
setLayout(new BorderLayout());
Canvas3D canvas3D = new Canvas3D(null);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup lineGroup = new BranchGroup();
Appearance app = new Appearance();
ColoringAttributes ca = new ColoringAttributes(new Color3f(204.0f, 204.0f, 204.0f), ColoringAttributes.SHADE_FLAT);
app.setColoringAttributes(ca);
Point3f[] plaPts = new Point3f[histImage.getWidth()*histImage.getHeight()];
for (int i = 0; i < histImage.getWidth(); i++) {
for (int j = 0; j < histImage.getHeight(); j++) {
s=histImage.getRaster().getSample(i, j, 0);
plaPts[count] = new Point3f(i,j,s);
count++;
}
}
PointArray pla = new PointArray(histImage.getWidth()*histImage.getHeight(), GeometryArray.COORDINATES);
pla.setCoordinates(0, plaPts);
Shape3D plShape = new Shape3D(pla, app);
TransformGroup objRotate = new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(plShape);
lineGroup.addChild(objRotate);
return lineGroup;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new JScrollPane(new HelloJava3Da()));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
最佳答案
使用 SimpleUniverse.getPreferredConfiguration()
GraphicsConfiguration 类创建一个 SimpleUniverse
和 Canvas3D
。然后创建一个 BranchGraph。添加一个 TransformGraph 作为 BranchGraph 的子级。然后将您的 Shape3D 添加为 TransformGraph 的子项。 Shape3D 必须有两件事:几何(你的点数组)和外观。从外观上看,你做了很多 Material 和照明的规范。
如果你只使用点数组,你只会看到点。如果你想填充多边形,应该使用 TriangleStripArray 或类似的东西,如果你想要线条,应该使用 LineStripArray。
使用 TransformGroup 旋转和平移。也不要忘记添加光线。
TransformGroup 需要一个 Transform3D 类作为参数来旋转和平移或缩放。
如果你的 Shape3D 是一个表面,你可以给它自定义颜色或使用 Material 并在它附近放一盏灯,否则你看不到它。不要忘记设置光源边界范围,以便光线到达物体。
另一个重要的事情:
simpleU.getViewingPlatform().getViewPlatform().setActivationRadius(300);
SimpleU.getViewer().getView().setBackClipDistance ( 300.0 );
使您的视野范围更远,因此物体不会在您移动时消失。
将鼠标交互附加到您的转换组
MouseRotate m1=new MouseRotate();
MouseZoom m2=new MouseZoom();
MouseTranslate m3=new MouseTranslate();
如果您想将 1580 x 1050 图像映射到默认的 3D View 范围内,您应该将点坐标除以 1000。(或在变换组中缩小到第 1000 个)
寻找教程:
http://www.java3d.org/tutorial.html
在这里,我将您的教程调整为 energon.class
//skipped imports(char limit in this post)
public final class energon extends JPanel {
int s = 0, count = 0;
public energon() {
setLayout(new BorderLayout());
GraphicsConfiguration gc=SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(gc);//See the added gc? this is a preferred config
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup lineGroup = new BranchGroup();
Appearance app = new Appearance();
ColoringAttributes ca = new ColoringAttributes(new Color3f(204.0f, 204.0f, 204.0f), ColoringAttributes.SHADE_FLAT);
app.setColoringAttributes(ca);
Point3f[] plaPts = new Point3f[4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j <2; j++) {
plaPts[count] = new Point3f(i/10.0f,j/10.0f,0);
//Look up line, i and j are divided by 10.0f to be able to
//see the points inside the view screen
count++;
}
}
PointArray pla = new PointArray(4, GeometryArray.COORDINATES);
pla.setCoordinates(0, plaPts);
Shape3D plShape = new Shape3D(pla, app);
TransformGroup objRotate = new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(plShape);
lineGroup.addChild(objRotate);
return lineGroup;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new JScrollPane(new energon()));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出:
好的,让我们添加一个磅值以便我们可以清楚地看到它们(将在一分钟内添加)
新代码(只添加了 2-3 行)
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.universe.*;
import java.awt.image.BufferedImage;
import javax.media.j3d.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
public final class energon extends JPanel {
int s = 0, count = 0;
public energon() {
setLayout(new BorderLayout());
GraphicsConfiguration gc=SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(gc);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup lineGroup = new BranchGroup();
Appearance app = new Appearance();
ColoringAttributes ca = new ColoringAttributes(new Color3f(204.0f, 204.0f, 204.0f), ColoringAttributes.SHADE_FLAT);
app.setColoringAttributes(ca);
Point3f[] plaPts = new Point3f[4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j <2; j++) {
plaPts[count] = new Point3f(i/10.0f,j/10.0f,0);
count++;
}
}
PointArray pla = new PointArray(4, GeometryArray.COORDINATES);
pla.setCoordinates(0, plaPts);
//between here!
PointAttributes a_point_just_bigger=new PointAttributes();
a_point_just_bigger.setPointSize(10.0f);//10 pixel-wide point
a_point_just_bigger.setPointAntialiasingEnable(true);//now points are sphere-like(not a cube)
app.setPointAttributes(a_point_just_bigger);
//and here! sets the point-attributes so it is easily seen.
Shape3D plShape = new Shape3D(pla, app);
TransformGroup objRotate = new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(plShape);
lineGroup.addChild(objRotate);
return lineGroup;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new JScrollPane(new energon()));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出:
这是另一个使用 trianglestriparray 绘制一种箭头形状,同时您可以使用鼠标按钮(其中 3 个)进行放大缩小、旋转和平移。
//skipped imports beause of char limit in this post
public final class energon extends JPanel {
int s = 0, count = 0;
public energon() {
setLayout(new BorderLayout());
GraphicsConfiguration gc=SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(gc);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup lineGroup = new BranchGroup();
Appearance app = new Appearance();
ColoringAttributes ca = new ColoringAttributes(new Color3f(204.0f, 204.0f,204.0f), ColoringAttributes.SHADE_FLAT);
app.setColoringAttributes(ca);
Point3f[] plaPts = new Point3f[5];
for (int i = 0; i < 2; i++) {
for (int j = 0; j <2; j++) {
plaPts[count] = new Point3f(i/10.0f,j/10.0f,0);
count++;
}
}
plaPts[count] = new Point3f(3.0f/10.0f,2.0f/10.0f,0);
int[]intArr=new int[5];
intArr[0]=3;intArr[1]=4;intArr[2]=4;intArr[3]=3;intArr[4]=3;
TriangleStripArray pla =new TriangleStripArray(20, GeometryArray.COLOR_3|GeometryArray.NORMALS|GeometryArray.COORDINATES,intArr);
pla.setCoordinates(0, plaPts);
PointAttributes a_point_just_bigger=new PointAttributes();
a_point_just_bigger.setPointSize(10.0f);//10 pixel-wide point
a_point_just_bigger.setPointAntialiasingEnable(true);//now points are sphere-like(not a cube)
app.setPointAttributes(a_point_just_bigger);
PolygonAttributes la=new PolygonAttributes();
la.setPolygonMode(PolygonAttributes.POLYGON_FILL);
la.setCullFace(PolygonAttributes.CULL_NONE);
app.setPolygonAttributes(la);
Material matt=new Material();
matt.setAmbientColor(new Color3f(1,1,1));
matt.setDiffuseColor(new Color3f(0.5f,0.5f,0.7f));
matt.setEmissiveColor(new Color3f(0.2f,0.2f,0.3f));
matt.setShininess(0.5f);
matt.setSpecularColor(new Color3f(0.4f,0.6f,0.9f));
matt.setLightingEnable(true);
app.setMaterial(matt);
RenderingAttributes ra=new RenderingAttributes();
ra.setIgnoreVertexColors(true);
app.setRenderingAttributes(ra);
Shape3D plShape = new Shape3D(pla, app);
TransformGroup objRotate = new TransformGroup();
MouseRotate mr=new MouseRotate();
mr.setTransformGroup(objRotate);
mr.setSchedulingBounds(new BoundingSphere());
lineGroup.addChild(mr);
MouseZoom mz=new MouseZoom();
mz.setTransformGroup(objRotate);
mz.setSchedulingBounds(new BoundingSphere());
lineGroup.addChild(mz);
MouseTranslate msl=new MouseTranslate();
msl.setTransformGroup(objRotate);
msl.setSchedulingBounds(new BoundingSphere());
lineGroup.addChild(msl);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(plShape);
AmbientLight al=new AmbientLight();
// al.addScope(objRotate);
al.setBounds(new BoundingSphere());
al.setEnable(true);
al.setColor(new Color3f(1,1,1));
lineGroup.addChild(objRotate);
lineGroup.addChild(al);
return lineGroup;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new JScrollPane(new energon()));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出:
你想改变点的颜色吗?这是新版本:
//skipping imports..
public final class energon extends JPanel {
int s = 0, count = 0;
public energon() {
setLayout(new BorderLayout());
GraphicsConfiguration gc=SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(gc);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup lineGroup = new BranchGroup();
Appearance app = new Appearance();
ColoringAttributes ca = new ColoringAttributes(new Color3f(204.0f, 204.0f, 204.0f), ColoringAttributes.SHADE_FLAT);
app.setColoringAttributes(ca);
Point3f[] plaPts = new Point3f[4];
Color3f[] colPts=new Color3f[4]; //parallel to coordinates, colors.
for (int i = 0; i < 2; i++) {
for (int j = 0; j <2; j++) {
plaPts[count] = new Point3f(i/10.0f,j/10.0f,0);
colPts[count]=new Color3f(i/3.0f,j/3.0f,(float) ((i+j)/3.0));//my arbitrary color set :)
count++;
}
}
PointArray pla = new PointArray(4, GeometryArray.COORDINATES|GeometryArray.COLOR_3);
pla.setColors(0,colPts); //this is the color-array setting
pla.setCoordinates(0, plaPts);
//between here!
PointAttributes a_point_just_bigger=new PointAttributes();
a_point_just_bigger.setPointSize(10.0f);//10 pixel-wide point
a_point_just_bigger.setPointAntialiasingEnable(true);//now points are sphere-like(not a cube)
app.setPointAttributes(a_point_just_bigger);
//and here! sets the point-attributes so it is easily seen.
Shape3D plShape = new Shape3D(pla, app);
TransformGroup objRotate = new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(plShape);
lineGroup.addChild(objRotate);
return lineGroup;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new JScrollPane(new energon()));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出:
带有自定义立方体形状和两个带有左键鼠标按钮交互的方向灯的新示例:
//skipped imports relating with char limit in this post
public final class energon extends JPanel {
int s = 0, count = 0;
public energon() {
setLayout(new BorderLayout());
GraphicsConfiguration gc=SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(gc);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup lineGroup = new BranchGroup();
Appearance app = new Appearance();
QuadArray lsa = new QuadArray(48,QuadArray.COORDINATES|QuadArray.NORMALS);
Vector3f [] normals=new Vector3f[24];
for(int i=0;i<24;i++)normals[i]=new Vector3f();
Point3f [] pts=new Point3f[24];
for(int i=0;i<24;i++)pts[i]=new Point3f();
Color3f [] clrs=new Color3f[24];
for(int i=0;i<24;i++)clrs[i]=new Color3f(0.5f,0.5f,0.5f);
//cube=6 quads
//first quad
pts[0].x=-0.5f;pts[0].y=-0.5f;pts[0].z=-0.5f;
pts[1].x=-0.5f;pts[1].y=-0.5f;pts[1].z=0.5f;
pts[2].x=-0.5f;pts[2].y=0.5f;pts[2].z=0.5f;
pts[3].x=-0.5f;pts[3].y=0.5f;pts[3].z=-0.5f;
normals[0].x=-1;normals[1].x=-1;normals[2].x=-1;normals[3].x=-1;
//second quad
pts[4].x=0.5f;pts[4].y=-0.5f;pts[4].z=-0.5f;
pts[5].x=0.5f;pts[5].y=-0.5f;pts[5].z=0.5f;
pts[6].x=0.5f;pts[6].y=0.5f;pts[6].z=0.5f;
pts[7].x=0.5f;pts[7].y=0.5f;pts[7].z=-0.5f;
normals[4].x=1;normals[5].x=1;normals[6].x=1;normals[7].x=1;
//third quad
pts[8].x=-0.5f;pts[8].y=-0.5f;pts[8].z=-0.5f;
pts[9].x=0.5f;pts[9].y=-0.5f;pts[9].z=-0.5f;
pts[10].x=0.5f;pts[10].y=0.5f;pts[10].z=-0.5f;
pts[11].x=-0.5f;pts[11].y=0.5f;pts[11].z=-0.5f;
normals[8].z=-1;normals[9].z=-1;normals[10].z=-1;normals[11].z=-1;
//fourth quad
pts[12].x=-0.5f;pts[12].y=-0.5f;pts[12].z=0.5f;
pts[13].x=0.5f;pts[13].y=-0.5f;pts[13].z=0.5f;
pts[14].x=0.5f;pts[14].y=0.5f;pts[14].z=0.5f;
pts[15].x=-0.5f;pts[15].y=0.5f;pts[15].z=0.5f;
normals[12].z=1;normals[13].z=1;normals[14].z=1;normals[15].z=1;
//fifth quad
pts[16].x=-0.5f;pts[16].y=-0.5f;pts[16].z=-0.5f;
pts[17].x=-0.5f;pts[17].y=-0.5f;pts[17].z=0.5f;
pts[18].x=0.5f;pts[18].y=-0.5f;pts[18].z=0.5f;
pts[19].x=0.5f;pts[19].y=-0.5f;pts[19].z=-0.5f;
normals[16].y=-1;normals[17].y=-1;normals[18].y=-1;normals[19].y=-1;
//sixth quad
pts[20].x=-0.5f;pts[20].y=0.5f;pts[20].z=-0.5f;
pts[21].x=-0.5f;pts[21].y=0.5f;pts[21].z=0.5f;
pts[22].x=0.5f;pts[22].y=0.5f;pts[22].z=0.5f;
pts[23].x=0.5f;pts[23].y=0.5f;pts[23].z=-0.5f;
normals[20].y=1;normals[21].y=1;normals[22].y=1;normals[23].y=1;
lsa.setNormals(0, normals);
lsa.setCoordinates(0, pts);
Shape3D sh=new Shape3D();
PolygonAttributes pa=new PolygonAttributes();
pa.setPolygonMode(PolygonAttributes.POLYGON_FILL);
pa.setCullFace(PolygonAttributes.CULL_NONE);
Material mat=new Material();
mat.setEmissiveColor(new Color3f(0.5f,0.5f,0.5f));
mat.setAmbientColor(new Color3f(0.1f,0.1f,0.1f));
mat.setDiffuseColor(new Color3f(0.2f,0.3f,0.4f));
mat.setSpecularColor(new Color3f(0.6f,0.3f,0.2f));
mat.setLightingEnable(true);
RenderingAttributes ra=new RenderingAttributes();
ra.setIgnoreVertexColors(true);
ColoringAttributes ca=new ColoringAttributes();
ca.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
ca.setColor(new Color3f(0.2f,0.5f,0.9f));
app.setColoringAttributes(ca);
app.setRenderingAttributes(ra);
app.setMaterial(mat);
app.setPolygonAttributes(pa);
sh.setGeometry(lsa);
sh.setAppearance(app);
sh.setPickable(true);
TransformGroup objRotate = new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(sh);
DirectionalLight light1=new DirectionalLight();
light1.setInfluencingBounds(new BoundingSphere(new Point3d(-5.0,0,0),10.0));
light1.setColor(new Color3f(1f,1f,1f));
light1.setDirection(new Vector3f(0,1,0));
objRotate.addChild(light1);
DirectionalLight light2=new DirectionalLight();
light2.setInfluencingBounds(new BoundingSphere(new Point3d(5.0,0,0),10.0));
light2.setColor(new Color3f(0.5f,1f,0.5f));
light2.setDirection(new Vector3f(0,-1,0));
objRotate.addChild(light2);
MouseRotate f1=new MouseRotate();
f1.setSchedulingBounds(new BoundingSphere());
f1.setTransformGroup(objRotate);
lineGroup.addChild(f1);
objRotate.addChild(new Sphere(0.60f,1,128));
lineGroup.addChild(objRotate);
return lineGroup;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new JScrollPane(new energon()));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出:
最重要的部分是,您可以使用三角测量器仅使用点坐标来获得真实的 3D 形状
GeometryInfo ginfo=new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
Triangulator tr = new Triangulator();
NormalGenerator normalGenerator = new NormalGenerator();
Stripifier st = new Stripifier();
int [] iint=new int[]{4,4,4,4,4,4};-->each face of cube has 4 points
ginfo.setStripCounts(iint);
ginfo.setCoordinates(pts);
tr.triangulate(ginfo); // ginfo contains the geometry
normalGenerator.generateNormals( ginfo );
st.stripify(ginfo);
sh.setGeometry(ginfo.getGeometryArray()); // shape is a Shape3D.
//now you can use Shape3D-type sh as a 3D-surface-containing shape
在上一个示例中,您必须使用四元组,但现在,您可以只使用点和三角剖分器来完成同样的操作:
//skipping imports since char limit is reached in this answer
public final class energon extends JPanel {
int s = 0, count = 0;
public energon() {
setLayout(new BorderLayout());
GraphicsConfiguration gc=SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(gc);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() {
BranchGroup lineGroup = new BranchGroup();
Appearance app = new Appearance();
Vector3f [] normals=new Vector3f[24];
for(int i=0;i<24;i++)normals[i]=new Vector3f();
Point3f [] pts=new Point3f[24];
for(int i=0;i<24;i++)pts[i]=new Point3f();
Color3f [] clrs=new Color3f[24];
for(int i=0;i<24;i++)clrs[i]=new Color3f(0.5f,0.5f,0.5f);
//cube=6 quads
//first quad
pts[0].x=-0.5f;pts[0].y=-0.5f;pts[0].z=-0.5f;
pts[1].x=-0.5f;pts[1].y=-0.5f;pts[1].z=0.5f;
pts[2].x=-0.5f;pts[2].y=0.5f;pts[2].z=0.5f;
pts[3].x=-0.5f;pts[3].y=0.5f;pts[3].z=-0.5f;
normals[0].x=-1;normals[1].x=-1;normals[2].x=-1;normals[3].x=-1;
//second quad
pts[4].x=0.5f;pts[4].y=-0.5f;pts[4].z=-0.5f;
pts[5].x=0.5f;pts[5].y=-0.5f;pts[5].z=0.5f;
pts[6].x=0.5f;pts[6].y=0.5f;pts[6].z=0.5f;
pts[7].x=0.5f;pts[7].y=0.5f;pts[7].z=-0.5f;
normals[4].x=1;normals[5].x=1;normals[6].x=1;normals[7].x=1;
//third quad
pts[8].x=-0.5f;pts[8].y=-0.5f;pts[8].z=-0.5f;
pts[9].x=0.5f;pts[9].y=-0.5f;pts[9].z=-0.5f;
pts[10].x=0.5f;pts[10].y=0.5f;pts[10].z=-0.5f;
pts[11].x=-0.5f;pts[11].y=0.5f;pts[11].z=-0.5f;
normals[8].z=-1;normals[9].z=-1;normals[10].z=-1;normals[11].z=-1;
//fourth quad
pts[12].x=-0.5f;pts[12].y=-0.5f;pts[12].z=0.5f;
pts[13].x=0.5f;pts[13].y=-0.5f;pts[13].z=0.5f;
pts[14].x=0.5f;pts[14].y=0.5f;pts[14].z=0.5f;
pts[15].x=-0.5f;pts[15].y=0.5f;pts[15].z=0.5f;
normals[12].z=1;normals[13].z=1;normals[14].z=1;normals[15].z=1;
//fifth quad
pts[16].x=-0.5f;pts[16].y=-0.5f;pts[16].z=-0.5f;
pts[17].x=-0.5f;pts[17].y=-0.5f;pts[17].z=0.5f;
pts[18].x=0.5f;pts[18].y=-0.5f;pts[18].z=0.5f;
pts[19].x=0.5f;pts[19].y=-0.5f;pts[19].z=-0.5f;
normals[16].y=-1;normals[17].y=-1;normals[18].y=-1;normals[19].y=-1;
//sixth quad
pts[20].x=-0.5f;pts[20].y=0.5f;pts[20].z=-0.5f;
pts[21].x=-0.5f;pts[21].y=0.5f;pts[21].z=0.5f;
pts[22].x=0.5f;pts[22].y=0.5f;pts[22].z=0.5f;
pts[23].x=0.5f;pts[23].y=0.5f;pts[23].z=-0.5f;
normals[20].y=1;normals[21].y=1;normals[22].y=1;normals[23].y=1;
Shape3D sh=new Shape3D();
PolygonAttributes pa=new PolygonAttributes();
pa.setPolygonMode(PolygonAttributes.POLYGON_FILL);
pa.setCullFace(PolygonAttributes.CULL_NONE);
Material mat=new Material();
mat.setEmissiveColor(new Color3f(0.5f,0.5f,0.5f));
mat.setAmbientColor(new Color3f(0.1f,0.1f,0.1f));
mat.setDiffuseColor(new Color3f(0.2f,0.3f,0.4f));
mat.setSpecularColor(new Color3f(0.6f,0.3f,0.2f));
mat.setLightingEnable(true);
RenderingAttributes ra=new RenderingAttributes();
ra.setIgnoreVertexColors(true);
ColoringAttributes ca=new ColoringAttributes();
ca.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
ca.setColor(new Color3f(0.2f,0.5f,0.9f));
app.setColoringAttributes(ca);
app.setRenderingAttributes(ra);
app.setMaterial(mat);
app.setPolygonAttributes(pa);
sh.setAppearance(app);
sh.setPickable(true);
GeometryArray ga=null;
GeometryInfo ginfo=new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
Triangulator tr = new Triangulator();
NormalGenerator normalGenerator = new NormalGenerator();
Stripifier st = new Stripifier();
int [] iint=new int[]{4,4,4,4,4,4};
ginfo.setStripCounts(iint);
ginfo.setCoordinates(pts);
tr.triangulate(ginfo); // ginfo contains the geometry
normalGenerator.generateNormals( ginfo );
st.stripify(ginfo);
sh.setGeometry(ginfo.getGeometryArray()); // shape is a Shape3D.
TransformGroup objRotate = new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(sh);
DirectionalLight light1=new DirectionalLight();
light1.setInfluencingBounds(new BoundingSphere(new Point3d(-5.0,0,0),10.0));
light1.setColor(new Color3f(1f,1f,1f));
light1.setDirection(new Vector3f(0,1,0));
objRotate.addChild(light1);
DirectionalLight light2=new DirectionalLight();
light2.setInfluencingBounds(new BoundingSphere(new Point3d(5.0,0,0),10.0));
light2.setColor(new Color3f(0.5f,1f,0.5f));
light2.setDirection(new Vector3f(0,-1,0));
objRotate.addChild(light2);
MouseRotate f1=new MouseRotate();
f1.setSchedulingBounds(new BoundingSphere());
f1.setTransformGroup(objRotate);
lineGroup.addChild(f1);
objRotate.addChild(new Sphere(0.60f,1,128));
lineGroup.addChild(objRotate);
return lineGroup;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new JScrollPane(new energon()));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出:相同!你只需给出点的坐标和面顶点数
关于java - 如何使用 Java3D API 在 Java 中绘制简单的 3D 点(x,y,z)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12313917/
我正在努力实现以下目标, 假设我有字符串: ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ) ) ) ) ) 我想编写一个正则
给定: 1 2 3 4 5 6
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
大家好,我卡颂。 Svelte问世很久了,一直想写一篇好懂的原理分析文章,拖了这么久终于写了。 本文会围绕一张流程图和两个Demo讲解,正确的食用方式是用电脑打开本文,跟着流程图、Demo一
身份证为15位或者18位,15位的全为数字,18位的前17位为数字,最后一位为数字或者大写字母”X“。 与之匹配的正则表达式: ?
我们先来最简单的,网页的登录窗口; 不过开始之前,大家先下载jquery的插件 本人习惯用了vs2008来做网页了,先添加一个空白页 这是最简单的的做法。。。先在body里面插入 <
1、MySQL自带的压力测试工具 Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、简单、实用的数据库文档(字典)生成工具,该工具支持CHM、Word、Excel、PDF、Html、XML、Markdown等
Go语言语法类似于C语言,因此熟悉C语言及其派生语言( C++、 C#、Objective-C 等)的人都会迅速熟悉这门语言。 C语言的有些语法会让代码可读性降低甚至发生歧义。Go语言在C语言的
我正在使用快速将 mkv 转换为 mp4 ffmpeg 命令 ffmpeg -i test.mkv -vcodec copy -acodec copy new.mp4 但不适用于任何 mkv 文件,当
我想计算我的工作簿中的工作表数量,然后从总数中减去特定的工作表。我错过了什么?这给了我一个对象错误: wsCount = ThisWorkbook.Sheets.Count - ThisWorkboo
我有一个 perl 文件,用于查看文件夹中是否存在 ini。如果是,它会从中读取,如果不是,它会根据我为它制作的模板创建一个。 我在 ini 部分使用 Config::Simple。 我的问题是,如果
尝试让一个 ViewController 通过标准 Cocoa 通知与另一个 ViewController 进行通信。 编写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDi
我正在绘制高程剖面图,显示沿路径的高程增益/损失,类似于下面的: Sample Elevation Profile with hand-placed labels http://img38.image
嗨,所以我需要做的是最终让 regStart 和 regPage 根据点击事件交替可见性,我不太担心编写 JavaScript 函数,但我根本无法让我的 regPage 首先隐藏。这是我的代码。请简单
我有一个非常简单的程序来测量一个函数花费了多少时间。 #include #include #include struct Foo { void addSample(uint64_t s)
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Simple.Data 的新手。但我很难找到如何进行“分组依据”。 我想要的是非常基本的。 表格看起来像: +________+ | cards | +________+ | id |
我现在正在开发一个 JS UDF,它看起来遵循编码。 通常情况下,由于循环计数为 2,Alert Msg 会出现两次。我想要的是即使循环计数为 3,Alert Msg 也只会出现一次。任何想法都
我是一名优秀的程序员,十分优秀!