📄 interleavedniobuffer.java
字号:
// Shrink the object
t.setScale(0.6);
TransformGroup trans = new TransformGroup(t);
trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.addChild(trans);
trans.addChild(shape);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
// Set up the global lights
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
AmbientLight aLgt = new AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds);
DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
lgt1.setInfluencingBounds(bounds);
objRoot.addChild(aLgt);
objRoot.addChild(lgt1);
// Let Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
JPanel createGeometryByReferencePanel() {
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder("Geometry Type"));
String values[] = {"Array", "Strip", "Indexed", "IndexedStrip"};
geomType = new JComboBox(values);
geomType.setLightWeightPopupEnabled(false);
geomType.addActionListener(this);
geomType.setSelectedIndex(0);
panel.add(new JLabel("Geometry Type"));
panel.add(geomType);
transparency = new JCheckBox("EnableTransparency",
false);
transparency.addActionListener(this);
panel.add(transparency);
textureBox = new JCheckBox("EnableTexture", false);
textureBox.addActionListener(this);
panel.add(textureBox);
return panel;
}
public InterleavedNIOBuffer() {
}
public InterleavedNIOBuffer(java.net.URL texURL1, java.net.URL texURL2) {
texImage1 = texURL1;
texImage2 = texURL2;
}
public void init() {
// create textures
if (texImage1 == null) {
// the path to the image for an applet
try {
texImage1 = new java.net.URL(getCodeBase().toString() +
"../images/bg.jpg");
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
}
if (texImage2 == null) {
// the path to the image for an applet
try {
texImage2 = new java.net.URL(getCodeBase().toString() +
"../images/one.jpg");
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
}
Container contentPane = getContentPane();
Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
contentPane.add("Center", c);
BranchGroup scene = createSceneGraph();
// SimpleUniverse is a Convenience Utility class
u = new SimpleUniverse(c);
// add mouse behaviors to the viewingPlatform
ViewingPlatform viewingPlatform = u.getViewingPlatform();
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
viewingPlatform.setNominalViewingTransform();
// add Orbit behavior to the viewing platform
OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
orbit.setSchedulingBounds(bounds);
viewingPlatform.setViewPlatformBehavior(orbit);
u.addBranchGraph(scene);
// Create GUI
JPanel p = new JPanel();
BoxLayout boxlayout = new BoxLayout(p,
BoxLayout.Y_AXIS);
p.add(createGeometryByReferencePanel());
p.setLayout(boxlayout);
contentPane.add("South", p);
}
public void destroy() {
u.cleanup();
}
public void actionPerformed(ActionEvent e) {
Object target = e.getSource();
if (target == geomType) {
shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);
}
else if (target == transparency) {
if (transparency.isSelected()) {
transp.setTransparencyMode(TransparencyAttributes.BLENDED);
}
else {
transp.setTransparencyMode(TransparencyAttributes.NONE);
}
}
else if (target == textureBox) {
if (textureBox.isSelected()) {
app.setTextureUnitState(textureUnitState);
}
else {
app.setTextureUnitState(null);
}
}
}
public static void main(String[] args) {
java.net.URL texURL1 = null;
java.net.URL texURL2 = null;
// the path to the image for an application
try {
texURL1 = new java.net.URL("file:../images/bg.jpg");
texURL2 = new java.net.URL("file:../images/one.jpg");
}
catch (java.net.MalformedURLException ex) {
System.out.println(ex.getMessage());
System.exit(1);
}
Frame frame = new MainFrame(new InterleavedNIOBuffer(texURL1, texURL2),
800, 800);
}
public GeometryArray createGeometry (int type) {
GeometryArray tetra = null;
int texCoordSetMap[] = {0, 0};
if (type == 1) {
tetra =new TriangleArray(12,
TriangleArray.COORDINATES|
TriangleArray.COLOR_3|
/*
TriangleArray.NORMAL_3|
*/
TriangleArray.TEXTURE_COORDINATE_2 |
TriangleArray.INTERLEAVED|
TriangleArray.BY_REFERENCE|
TriangleArray.USE_NIO_BUFFER,
2, texCoordSetMap);
tetra.setInterleavedVertexBuffer(interleavedBuffer);
}
else if (type == 2) {
tetra = new TriangleStripArray(12,
TriangleStripArray.COORDINATES|
TriangleStripArray.COLOR_3|
/*
TriangleArray.NORMAL_3|
*/
TriangleArray.TEXTURE_COORDINATE_2 |
TriangleStripArray.INTERLEAVED|
TriangleStripArray.BY_REFERENCE|
TriangleStripArray.USE_NIO_BUFFER,
2, texCoordSetMap,
stripVertexCounts);
tetra.setInterleavedVertexBuffer(interleavedBuffer);
}
else if (type == 3) { // Indexed Geometry
tetra = new IndexedTriangleArray(4,
IndexedTriangleArray.COORDINATES|
IndexedTriangleArray.COLOR_3|
/*
IndexedTriangleArray.NORMAL_3|
*/
IndexedTriangleArray.TEXTURE_COORDINATE_2 |
IndexedTriangleArray.INTERLEAVED|
IndexedTriangleArray.BY_REFERENCE|
IndexedTriangleArray.USE_NIO_BUFFER|
IndexedTriangleArray.USE_COORD_INDEX_ONLY,
2, texCoordSetMap,
12);
tetra.setInterleavedVertexBuffer(indexedInterleavedBuffer);
((IndexedTriangleArray)tetra).setCoordinateIndices(0, indices);
((IndexedTriangleArray)tetra).setColorIndices(0, indices);
((IndexedTriangleArray)tetra).setTextureCoordinateIndices(
0, 0, indices);
((IndexedTriangleArray)tetra).setTextureCoordinateIndices(
1, 0, indices);
}
else if (type == 4) { // Indexed strip geometry
tetra = new IndexedTriangleStripArray(4,
IndexedTriangleStripArray.COORDINATES|
IndexedTriangleStripArray.COLOR_3|
/*
IndexedTriangleArray.NORMAL_3|
*/
IndexedTriangleArray.TEXTURE_COORDINATE_2 |
IndexedTriangleStripArray.INTERLEAVED|
IndexedTriangleStripArray.BY_REFERENCE|
IndexedTriangleStripArray.USE_NIO_BUFFER,
//IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
2, texCoordSetMap,
12,
stripVertexCounts);
tetra.setInterleavedVertexBuffer(indexedInterleavedBuffer);
((IndexedTriangleStripArray)tetra).setCoordinateIndices(0, indices);
((IndexedTriangleStripArray)tetra).setColorIndices(0, indices);
((IndexedTriangleStripArray)tetra).setTextureCoordinateIndices(
0, 0, indices);
((IndexedTriangleStripArray)tetra).setTextureCoordinateIndices(
1, 0, indices);
}
else if (type == 5) { // Interleaved array
}
return tetra;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -