📄 dot3demo.java
字号:
// with *complex* lightmaps, i.e., spherical light distributions, // multi light sorces, degradee, waves,etc /* tcg1 = new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP, TexCoordGeneration.TEXTURE_COORDINATE_3); */ // create TUS tuLightMap = new TextureUnitState(textureLightMap,textAttLightMap,tcg1); tuDOT3NormalMap = new TextureUnitState(textureDOT3NormalMap,textAttDot3,null); tuColor = new TextureUnitState(textureColor,texAttColor,null); // this TUS array is used by geometry at runtime TextureUnitState[] tus = new TextureUnitState[3]; tus[0] = tuLightMap; tus[1] = tuDOT3NormalMap; tus[2] = tuColor; // enable texture units for read/write at runtime for (int i = 0; i < tus.length; i++) { tus[i].setCapability(TextureUnitState.ALLOW_STATE_WRITE); tus[i].setCapability(TextureUnitState.ALLOW_STATE_READ); } return tus; } /** * creates a single Quad geometry with 4 TextureCoordinateMaps, for multitexture use.<br> * Dimension is scale*(2m , 1m) * @param scale a scale for this quad * @return quad geometry for multitexture use */ private GeometryArray createGeometry(float scale) { // vertex coordinates float[] verts = { 2.0f, -1.0f, 0.0f, 2.0f, 1.0f, 0.0f, -2.0f, 1.0f, 0.0f, -2.0f, -1.0f, 0.0f }; // 2D texture Coords - each texture unit will use one set of this float[] texCoords = { 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f}; // all texture units will use texCoords from unit 0 int[] texCoordSetMap = {0,0,0,0}; // normals Vector3f normal = new Vector3f( 0.0f, 0.0f, 1.0f); Vector3f[] normals = { normal, normal, normal, normal} ; // resize quad dimension for(int i = 0;i<verts.length;i++) { verts[i] *= scale; } // create geometry using GeometryInfo GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY); gi.setCoordinates(verts); gi.setNormals(normals); // preparing for multitexture // To get up to 4 TUS, it needs 4 sets of 2D texture gi.setTextureCoordinateParams(4, 2); gi.setTexCoordSetMap(texCoordSetMap); // this demo needs just 3 TUS, but geometry // is prepared for up to 4 TUS stages gi.setTextureCoordinates(0,texCoords); gi.setTextureCoordinates(1,texCoords); gi.setTextureCoordinates(2,texCoords); gi.setTextureCoordinates(3,texCoords); return gi.getGeometryArray(); } /** * Creates scenegraphs * @return a BranchGroup with all needed objects in scene */ private BranchGroup createSceneGraph() { BranchGroup bgRoot = new BranchGroup(); CheckNewLightMapBehavior checkNewLightMapBehavior = new CheckNewLightMapBehavior(); bgRoot.addChild(checkNewLightMapBehavior); // a blue background Background background = new Background(0.4f,0.4f,0.8f); background.setApplicationBounds(bounds); bgRoot.addChild(background); AmbientLight alit = new AmbientLight(true,new Color3f(0.4f,0.4f,0.4f)); bgRoot.addChild(alit); // Set up some directional lights // DOT3 doesnot need light, because it is a perpixel lighting technique //but we add this lights to show // geometry when using non-DOT3 lighting, as color texture only and // light map texture mode Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f); Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); bgRoot.addChild(light1); DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction); light2.setInfluencingBounds(bounds); bgRoot.addChild(light2); //loading color and DOT3 normal map textures from disk, //and creating light map at runtime loadTextures(); //our single Quad geometry, enabled for multitexture GeometryArray geo = createGeometry(0.4f); // a appearance for our geometry appearance = new Appearance(); // polygon and texture unit will be updated at runtime // so we must enabled read/write operations for then appearance.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ); appearance.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE); appearance.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_READ); appearance.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE); //use a default material. It is necessary when running //on non per-pixel lighting mod, i.e., using non DOT3 textures appearance.setMaterial(new Material()); polygonAttributes = new PolygonAttributes(); polygonAttributes.setCapability(PolygonAttributes.ALLOW_MODE_WRITE); polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE); appearance.setPolygonAttributes(polygonAttributes); // uses a TUS dot3 enabled tusArr = setupTextureUnitState(); appearance.setTextureUnitState(tusArr); // joining geometry and appearance in a shape3D Shape3D shape3D = new Shape3D(geo,appearance); shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ); shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE); bgRoot.addChild(shape3D); bgRoot.compile(); return bgRoot; } /** * Toggles wireframe mode * @param mode true for wireframe, false for fill polygon */ public void setWireframeMode(boolean mode) { if(mode) polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE); else polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL); } /** * This method togles on/off textures and updates TextureUnitState in correct Order. * Some video drivers does not accept TextureUnitState arrays with null values among * non-null values * @param showLightMap togles LightMap texture * @param showDot3 togles DOT3 Normal texture * @param showColor togles Color texture */ public void showTextures(boolean showLightMap, boolean showDot3, boolean showColor) { int bitSet = 0; bitSet |= showLightMap ? 4 : 0; bitSet |= showDot3 ? 2 : 0; bitSet |= showColor ? 1 : 0; tusArr[0] = null; tusArr[1] = null; tusArr[2] = null; switch (bitSet) { case 7: { //all bit == all tus tusArr[0] = tuLightMap; tusArr[1] = tuDOT3NormalMap; tusArr[2] = tuColor; } break; case 6: { //no Color tusArr[0] = tuLightMap; tusArr[1] = tuDOT3NormalMap; } break; case 5: { //no Dot3 tusArr[0] = tuLightMap; tusArr[1] = tuColor; } break; case 4: { //lightMap only tusArr[0] = tuLightMap; } break; case 3: { //no LightMap tusArr[0] = tuDOT3NormalMap; tusArr[1] = tuColor; } break; case 2: { //Dot3 Only tusArr[0] = tuDOT3NormalMap; } break; case 1: { // Color Only tusArr[0] = tuColor; } break; default: { // case 0, no textures shows at all } break; } appearance.setTextureUnitState(tusArr); } /** * updates LightMap texture. * This method is called from checkNewLightMapBehavior * @param image new image to be applied */ public void updateLighMap(BufferedImage image) { imageLightMap.setSubImage(image,image.getWidth(),image.getHeight(),0,0,0,0); } private BufferedImage tempImage; private boolean lockTempImage = false; /** * main method * @param args */ public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new Dot3Demo(); } }); } /** * A internal class to check if there is a new Light Map to be applied */ class CheckNewLightMapBehavior extends Behavior { WakeupOnElapsedFrames wakeup = new WakeupOnElapsedFrames(0); public CheckNewLightMapBehavior() {// auto enable and set schedulling bounds setEnable(true); setSchedulingBounds(bounds); } public void initialize() { wakeupOn(wakeup); } public void processStimulus(Enumeration e) { // check if there are a new light map ready to use if (ctrlPanel.hasTextureImageReady()) { updateLighMap(ctrlPanel.getTextureImage()); } //wake up on next frame wakeupOn(wakeup); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -