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

📄 dot3demo.java

📁 java 3d编程的一些例子源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $RCSfile: Dot3Demo.java,v $ * * Copyright (c) 2006 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: * * - Redistribution 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 MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS 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 THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.2 $ * $Date: 2006/03/03 22:02:01 $ * $State: Exp $ */package org.jdesktop.j3d.examples.dot3;import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;import com.sun.j3d.utils.geometry.GeometryInfo;import com.sun.j3d.utils.image.TextureLoader;import com.sun.j3d.utils.universe.SimpleUniverse;import com.sun.j3d.utils.universe.ViewingPlatform;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics2D;import java.awt.GraphicsConfiguration;import java.awt.image.BufferedImage;import java.util.Enumeration;import javax.media.j3d.AmbientLight;import javax.media.j3d.Appearance;import javax.media.j3d.Background;import javax.media.j3d.Behavior;import javax.media.j3d.BoundingSphere;import javax.media.j3d.BranchGroup;import javax.media.j3d.Canvas3D;import javax.media.j3d.DirectionalLight;import javax.media.j3d.GeometryArray;import javax.media.j3d.ImageComponent2D;import javax.media.j3d.Material;import javax.media.j3d.PolygonAttributes;import javax.media.j3d.Shape3D;import javax.media.j3d.TexCoordGeneration;import javax.media.j3d.Texture;import javax.media.j3d.Texture2D;import javax.media.j3d.TextureAttributes;import javax.media.j3d.TextureUnitState;import javax.media.j3d.WakeupOnElapsedFrames;import javax.swing.JFrame;import javax.swing.JPanel;import javax.vecmath.Color3f;import javax.vecmath.Point3d;import javax.vecmath.Vector3f;import org.jdesktop.j3d.examples.Resources;/** * This example program is contributed by Alessandro Borges *//** * <pre> *  DOT3 per-pixel lighting demo. * It uses a Normal map and a Light map, both coded as independent textures. * Each pixel color is a vector coded, where color range [0,255] is mapped * as vector in range [-1.0,+1.0]. * * A math operation called DOT3 applied to Light vector and Normal vector results * a scalar value, interpreted as light intensity. This operation is made for each * pixel on texture.   * Light Intensity = DOT3(light, normal); * * This technique allows complex lighting effects, as bumps, on low polygon count * geometries. * </pre> * */public class Dot3Demo extends JFrame {    // a external control panel  for this demo    private TextureControlPanel ctrlPanel = null;    // default bounds used in this application    private BoundingSphere bounds =  new BoundingSphere(new Point3d(0.0, 0.0, 0.0),            100.0);    // TextureUnitStates used in this application    TextureUnitState tuLightMap;    TextureUnitState tuDOT3NormalMap;    TextureUnitState tuColor;        /** Where the TUs are applied **/    TextureUnitState[] tusArr;    /** appearance will be changed at runtime **/    Appearance appearance;    /** polygonAttributes will be changed at runtime **/    PolygonAttributes polygonAttributes;        // textures used    Texture textureColor;    Texture textureDOT3NormalMap;    Texture2D textureLightMap;    // needs for runtime updates on lightMap    ImageComponent2D imageLightMap;        // default texture names used    String  textureColorName= "resources/images/wood.jpg";    String textureDOT3NormalMapName = "resources/images/Java3Ddot3.jpg";        /**     * Constructor.     */    public Dot3Demo() {        super("Java3D DOT3 demo");        try {            init();        } catch (Exception e) {            e.printStackTrace();        }    }            private void init() throws Exception {        this.setSize(new Dimension(400, 400));        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                JPanel mainPanel = new JPanel();        this.getContentPane().add(mainPanel, null);        mainPanel.setLayout(new BorderLayout());        // get default configuration for 3D        GraphicsConfiguration conf =   SimpleUniverse.getPreferredConfiguration();        Canvas3D canvas = new Canvas3D(conf);        // create simpleUniverse        SimpleUniverse su = new SimpleUniverse(canvas);        // create sceneGraph and add it to universe        BranchGroup sceneGraph = createSceneGraph();        su.addBranchGraph(sceneGraph);                // This will move the ViewPlatform back a bit so the        // objects in the scene can be viewed.        su.getViewingPlatform().setNominalViewingTransform();                // Ensure at least 5 msec per frame (i.e., < 200Hz)        su.getViewer().getView().setMinimumFrameCycleTime(5);                // add the behaviors to the ViewingPlatform        ViewingPlatform viewingPlatform = su.getViewingPlatform();        viewingPlatform.setNominalViewingTransform();                // add orbit behavior to ViewingPlatform        OrbitBehavior   orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL |                OrbitBehavior.STOP_ZOOM);        orbit.setSchedulingBounds(bounds);        viewingPlatform.setViewPlatformBehavior(orbit);                mainPanel.add(canvas, BorderLayout.CENTER);        this.setVisible(true);        //create a control panel to user interaction        ctrlPanel = new TextureControlPanel(this);        ctrlPanel.setVisible(true);        ctrlPanel.setLocation(410,10);    }        /**     * loads all needed textures, and creates light map texture     */    private void loadTextures() {        try {            //java.net.URL urlColor = new java.net.URL("file:" + textureColorName);            //java.net.URL urlDot3  = new java.net.URL("file:" + textureDOT3NormalMapName);            java.net.URL urlColor = Resources.getResource(textureColorName);            java.net.URL urlDot3  = Resources.getResource(textureDOT3NormalMapName);                        // loading textures            textureColor = new TextureLoader(urlColor,this).getTexture();            textureDOT3NormalMap = new TextureLoader(urlDot3,this) .getTexture();                        // create Image for textureLightMap            BufferedImage image = new BufferedImage(256,256,BufferedImage.TYPE_INT_RGB);            Graphics2D graphics = image.createGraphics();            graphics.setPaint(new Color(130,130,250));            graphics.fillRect(0,0,image.getWidth(),image.getHeight());            graphics.dispose();                        imageLightMap = new ImageComponent2D(ImageComponent2D.FORMAT_RGB,image,false,false);            imageLightMap.setCapability(ImageComponent2D.ALLOW_IMAGE_WRITE);            imageLightMap.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);                        //create textureLightMap with above imageLightMap            textureLightMap = new Texture2D(Texture2D.BASE_LEVEL,Texture2D.RGB,256,256);            textureLightMap.setImage(0,imageLightMap);            textureLightMap.setMagFilter(Texture2D.NICEST);            textureLightMap.setMinFilter(Texture2D.NICEST);                        // application with update textureLightMap at runtime, so lets enable some caps            textureLightMap.setCapability(Texture2D.ALLOW_ENABLE_WRITE);            textureLightMap.setCapability(Texture2D.ALLOW_ENABLE_READ);            textureLightMap.setCapability(Texture2D.ALLOW_IMAGE_WRITE);            textureLightMap.setCapability(Texture2D.ALLOW_IMAGE_READ);                    } catch(Exception e) {            System.err.println("Failed to load textures");            e.printStackTrace();        }    }        /**     * setup TextureUnitStates used in this demo.     *     * @return     */    private TextureUnitState[] setupTextureUnitState() {        //texture Attributes for DOT3 normal map        TextureAttributes textAttDot3 = new TextureAttributes();                        // lightMap uses  TextureAttributes with default REPLACE mode        TextureAttributes textAttLightMap = new TextureAttributes();                TextureAttributes texAttColor = new TextureAttributes();        texAttColor.setTextureMode(TextureAttributes.COMBINE);                //CombineRgbMode could be also COMBINE_ADD or COMBINE_ADD_SIGNED, with        //different results        texAttColor.setCombineRgbMode(TextureAttributes.COMBINE_MODULATE);        // increase light depth effect        texAttColor.setCombineRgbScale(2);                textAttDot3.setTextureMode(TextureAttributes.COMBINE);        textAttDot3.setCombineRgbMode(TextureAttributes.COMBINE_DOT3);        textAttDot3.setCombineAlphaMode(TextureAttributes.COMBINE_DOT3);        textAttDot3.setTextureBlendColor(1.f,1.0f,1.0f,0.0f);        //  increase light intesity        textAttDot3.setCombineRgbScale(2);        // setup functions        textAttDot3.setCombineRgbFunction(0,TextureAttributes.COMBINE_SRC_COLOR);        textAttDot3.setCombineRgbFunction(1,TextureAttributes.COMBINE_SRC_COLOR);        textAttDot3.setCombineRgbFunction(2,TextureAttributes.COMBINE_SRC_COLOR);        //combine with previous TUS, lightMap        textAttDot3.setCombineRgbSource(0,TextureAttributes.COMBINE_PREVIOUS_TEXTURE_UNIT_STATE);        textAttDot3.setCombineRgbSource(1,TextureAttributes.COMBINE_TEXTURE_COLOR );        textAttDot3.setCombineRgbSource(2,TextureAttributes.COMBINE_OBJECT_COLOR);                TexCoordGeneration tcg1=null;        // SphereMap tcg can add nice dynamic effects for curved surfaces, because it        // distributes texture like a light bean over geometry.        // It os not used in this demo, but you can try yourself at home

⌨️ 快捷键说明

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