⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interleavedniobuffer.java

📁 java 3d 3
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *	@(#)InterleavedNIOBuffer.java 1.4 02/10/21 13:42:27
 *
 * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed,licensed or intended
 * for use in the design, construction, operation or maintenance of
 * any nuclear facility.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import com.sun.j3d.utils.behaviors.vp.*;
import java.nio.*;

public class InterleavedNIOBuffer extends JApplet implements ActionListener {

    RenderingAttributes ra;
    ColoringAttributes ca;
    Material mat;			   
    Appearance app;			   
    JComboBox geomType;
    JCheckBox transparency;
    JCheckBox textureBox;
    Shape3D shape;			   
    TransparencyAttributes transp;

    GeometryArray tetraRegular, tetraStrip, tetraIndexed, tetraIndexedStrip;
    GeometryArray[] geoArrays = new GeometryArray[4];

   // Globally used colors
   Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
   Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
   Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
   Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
   Color3f[] colors = {white, red, green, blue};			   

    private static final float sqrt3 = (float) Math.sqrt(3.0);
    private static final float sqrt3_3 = sqrt3 / 3.0f;
    private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;

    private static final float ycenter = 0.5f * sqrt24_3;
    private static final float zcenter = -sqrt3_3;

    private static final Point3f p1 = 
	new Point3f(-1.0f, -ycenter, -zcenter);
    private static final Point3f p2 = 
	new Point3f(1.0f, -ycenter, -zcenter);
    private static final Point3f p3 =
	new Point3f(0.0f, -ycenter, -sqrt3 - zcenter);
    private static final Point3f p4 =
	new Point3f(0.0f, sqrt24_3 - ycenter, 0.0f);

    private static final Point2f t1 = new Point2f(0.0f, 0.0f);
    private static final Point2f t2 = new Point2f(0.5f, 1.0f);
    private static final Point2f t3 = new Point2f(1.0f, 0.0f);
    private static final Point2f t4 = new Point2f(1.0f, 1.0f);

    private static final Color3f c1 = new Color3f(1.0f, 0.0f, 0.0f);
    private static final Color3f c2 = new Color3f(0.0f, 1.0f, 0.0f);
    private static final Color3f c3 = new Color3f(0.0f, 1.0f, 1.0f);
    private static final Color3f c4 = new Color3f(1.0f, 1.0f, 0.0f);
    

    private static final float[] interleaved = {
	t1.x, t1.y,
	t1.x, t1.y,
	c1.x, c1.y, c1.z, // front face
	p1.x, p1.y, p1.z, // front face
	t2.x, t2.y,
	t2.x, t2.y,
	c2.x, c2.y, c2.z,
	p2.x, p2.y, p2.z,
	t4.x, t4.y,
	t4.x, t4.y,
	c4.x, c4.y, c4.z,
	p4.x, p4.y, p4.z,
	
	t1.x, t1.y,
	t1.x, t1.y,
	c1.x, c1.y, c1.z,// left, back face
	p1.x, p1.y, p1.z,// left, back face
	t4.x, t4.y,
	t4.x, t4.y,
	c4.x, c4.y, c4.z,
	p4.x, p4.y, p4.z,
	t3.x, t3.y,
	t3.x, t3.y,
	c3.x, c3.y, c3.z,
	p3.x, p3.y, p3.z,
	
	t2.x, t2.y,
	t2.x, t2.y,
	c2.x, c2.y, c2.z,// right, back face
	p2.x, p2.y, p2.z,// right, back face
	t3.x, t3.y,
	t3.x, t3.y,
	c3.x, c3.y, c3.z,
	p3.x, p3.y, p3.z,
	t4.x, t4.y,
	t4.x, t4.y,
	c4.x, c4.y, c4.z,
	p4.x, p4.y, p4.z,
	
	t1.x, t1.y,
	t1.x, t1.y,
	c1.x, c1.y, c1.z,// bottom face
	p1.x, p1.y, p1.z,// bottom face
	t3.x, t3.y,
	t3.x, t3.y,
	c3.x, c3.y, c3.z,
	p3.x, p3.y, p3.z,
	t2.x, t2.y,
	t2.x, t2.y,
	c2.x, c2.y, c2.z,
	p2.x, p2.y, p2.z,
    };
 
    private static final float[] indexedInterleaved = {
	t1.x,t1.y,
	t1.x,t1.y,
	c1.x,c1.y,c1.z,
	p1.x,p1.y,p1.z,
	t2.x,t2.y,
	t2.x,t2.y,
	c2.x,c2.y,c2.z,
	p2.x,p2.y,p2.z,
	t3.x,t3.y,
	t3.x,t3.y,
	c3.x,c3.y,c3.z,
	p3.x,p3.y,p3.z,
	t4.x,t4.y,
	t4.x,t4.y,
	c4.x,c4.y,c4.z,
	p4.x,p4.y,p4.z,
    };
    

    private static final int[] indices = {0,1,3,0,3,2,1,2,3,0,2,1};
    private int[] stripVertexCounts = {3,3,3,3};

    TextureUnitState textureUnitState[] = new TextureUnitState[2];
    Texture tex1;
    Texture tex2;

    private java.net.URL texImage1 = null;
    private java.net.URL texImage2 = null;

    private SimpleUniverse u;

    private J3DBuffer interleavedBuffer;
    private J3DBuffer indexedInterleavedBuffer;

    void createInterleavedBuffers() {
	int size;
	ByteOrder order =  ByteOrder.nativeOrder();

	size =  (2 + 2 + 3 + 3 ) * 3 * 4; 
	FloatBuffer vertex = ByteBuffer.allocateDirect(size * 4).order(order).asFloatBuffer();
	vertex.put(interleaved, 0, size);
	interleavedBuffer = new J3DBuffer(vertex);

	size = ( 2 + 2 + 3 + 3) * 4;
	FloatBuffer indexedVertex = ByteBuffer.allocateDirect(size * 4).order(order).asFloatBuffer();
	indexedVertex.put(indexedInterleaved, 0, size);
	indexedInterleavedBuffer = new J3DBuffer(indexedVertex);
    }
    
    BranchGroup createSceneGraph() {
	BranchGroup objRoot = new BranchGroup();

	// Set up attributes to render lines
        app = new Appearance();
	app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);
	
	transp = new TransparencyAttributes();
	transp.setTransparency(0.5f);
	transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
	transp.setTransparencyMode(TransparencyAttributes.NONE);
	app.setTransparencyAttributes(transp);

        // load textures
        TextureAttributes texAttr1 = new TextureAttributes();
        texAttr1.setTextureMode(TextureAttributes.DECAL);
        TextureAttributes texAttr2 = new TextureAttributes();
        texAttr2.setTextureMode(TextureAttributes.MODULATE);

        TextureLoader tex = new TextureLoader(texImage1, new String("RGB"), this);
        if (tex == null)
            return null;
        tex1 = tex.getTexture();

        tex = new TextureLoader(texImage2, new String("RGB"), this);
        if (tex == null)
            return null;
        tex2 = tex.getTexture();

        textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null);
        textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null);

	createInterleavedBuffers();
	
	tetraRegular = createGeometry(1);
	tetraStrip =createGeometry(2);
	tetraIndexed = createGeometry(3);
	tetraIndexedStrip = createGeometry(4);

	geoArrays[0] = tetraRegular;
	geoArrays[1] = tetraStrip;
	geoArrays[2] = tetraIndexed;
	geoArrays[3] = tetraIndexedStrip;
	
	shape = new Shape3D(tetraRegular, app);
	shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);

	Transform3D t = new Transform3D();
	// move the object upwards
	t.set(new Vector3f(0.0f, 0.3f, 0.0f));

	// rotate the shape
	Transform3D temp = new Transform3D();
        temp.rotX(Math.PI/4.0d);
	t.mul(temp);
        temp.rotY(Math.PI/4.0d);
        t.mul(temp);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -