// In this method, the objects for the scene are generated and added to // the SimpleUniverse. public void createSceneGraph(SimpleUniverse su) { // *** The root of the graph containing the scene (with a cube and a sphere). *** BranchGroup theScene = new BranchGroup(); // Generate an Appearance. Color3f ambientColourShaded = new Color3f(0.0f, 0.4f, 0.4f); Color3f emissiveColourShaded = new Color3f(0.0f, 0.0f, 0.0f); Color3f diffuseColourShaded = new Color3f(0.0f, 0.7f, 0.7f); Color3f specularColourShaded = new Color3f(0.0f, 0.5f, 0.5f); float shininessShaded = 20.0f; Appearance shadedApp = new Appearance(); shadedApp.setMaterial( new Material( ambientColourShaded, emissiveColourShaded, diffuseColourShaded, specularColourShaded, shininessShaded)); float r = 0.3f; // The radius of the sphere. float boxHL = 0.7f * r; // Half the vertex length of the cube. float shift = 3.0f * r; // Distance between cube and sphere. // *** The sphere and its transformation group *** Sphere s = new Sphere(r, Sphere.GENERATE_NORMALS, 100, shadedApp); Transform3D tfSphere = new Transform3D(); tfSphere.setTranslation(new Vector3f(-0.95f + r, 0.0f, 0.0f)); TransformGroup tgSphere = new TransformGroup(tfSphere); tgSphere.addChild(s); theScene.addChild(tgSphere); // *** The cube and its transformation group *** Box b2 = new Box(boxHL, boxHL, boxHL, shadedApp); Transform3D tfBox2 = new Transform3D(); tfBox2.setTranslation(new Vector3f(-0.95f + r + shift, 0.0f, 0.0f)); Transform3D rotation = new Transform3D(); rotation.rotY(Math.PI / 4); Transform3D rotationX = new Transform3D(); rotationX.rotX(Math.PI / 6); rotation.mul(rotationX); tfBox2.mul(rotation); TransformGroup tgBox2 = new TransformGroup(tfBox2); tgBox2.addChild(b2); theScene.addChild(tgBox2); // Generate a white background. Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), Double.MAX_VALUE); bg.setApplicationBounds(bounds); theScene.addChild(bg); theScene.compile(); // Add the scene to the universe. su.addBranchGraph(theScene); }
// In this method, the objects for the scene are generated and added to // the SimpleUniverse. public void createSceneGraph(SimpleUniverse su) { // Create the root of the branch group for the scene. BranchGroup theScene = new BranchGroup(); // Generate an Appearance for the sphere. Color3f ambientColourSphere = new Color3f(0.2f, 0.2f, 0.2f); Color3f emissiveColourSphere = new Color3f(0.0f, 0.0f, 0.0f); Color3f diffuseColourSphere = new Color3f(0.6f, 0.6f, 0.6f); Color3f specularColourSphere = new Color3f(0.5f, 0.5f, 0.5f); float shininessSphere = 20.0f; Appearance sphereApp = new Appearance(); sphereApp.setMaterial( new Material( ambientColourSphere, emissiveColourSphere, diffuseColourSphere, specularColourSphere, shininessSphere)); // n spheres with radius r will be shown. int n = 5; float r = 0.15f; float shift = 2 * r + 0.05f; // The distance between the centres of the spheres. // Arrays for the sphere, their transformations and their transformation groups // transformation groups (for positioning). Sphere[] spheres = new Sphere[n]; TransformGroup[] tg = new TransformGroup[n]; Transform3D[] tf = new Transform3D[n]; // Generate the sphere, their transformations and their // transformation groups. Add everyting to the scene. for (int i = 0; i < n; i++) { spheres[i] = new Sphere(r, Sphere.GENERATE_NORMALS, 4 + i * i * i, sphereApp); tf[i] = new Transform3D(); tf[i].setTranslation(new Vector3f(-0.95f + r + shift * i, 0.0f, 0.0f)); tg[i] = new TransformGroup(tf[i]); tg[i].addChild(spheres[i]); theScene.addChild(tg[i]); } // Generate a white background. Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0); bg.setApplicationBounds(bounds); theScene.addChild(bg); theScene.compile(); // Add the scene to the universe. su.addBranchGraph(theScene); }
/** Setup the basic scene which consists of a quad and a viewpoint */ private void setupSceneGraph() { // View group Viewpoint vp = new Viewpoint(); Vector3f trans = new Vector3f(0, 0, 1); Matrix4f mat = new Matrix4f(); mat.setIdentity(); mat.setTranslation(trans); TransformGroup tx = new TransformGroup(); tx.addChild(vp); tx.setTransform(mat); Group scene_root = new Group(); scene_root.addChild(tx); // Flat panel that has the viewable object as the demo float[] coord = {0, 0, -1, 0.25f, 0, -1, 0, 0.25f, -1}; float[] normal = {0, 0, 1, 0, 0, 1, 0, 0, 1}; TriangleArray geom = new TriangleArray(); geom.setValidVertexCount(3); geom.setVertices(TriangleArray.COORDINATE_3, coord); geom.setNormals(normal); Material material = new Material(); material.setDiffuseColor(new float[] {0, 0, 1}); material.setEmissiveColor(new float[] {0, 0, 1}); material.setSpecularColor(new float[] {1, 1, 1}); material.setTransparency(0.5f); Appearance app = new Appearance(); app.setMaterial(material); Shape3D shape = new Shape3D(); shape.setGeometry(geom); shape.setAppearance(app); TransformGroup tg = new TransformGroup(); Matrix4f transform = new Matrix4f(); transform.setIdentity(); transform.setTranslation(new Vector3f(0.15f, 0, -1)); tg.setTransform(transform); Shape3D backShape = new Shape3D(); Material material2 = new Material(); material2.setDiffuseColor(new float[] {1, 0, 0}); material2.setEmissiveColor(new float[] {1, 0, 0}); material2.setSpecularColor(new float[] {1, 1, 1}); Appearance app2 = new Appearance(); app2.setMaterial(material2); backShape.setGeometry(geom); backShape.setAppearance(app2); tg.addChild(backShape); scene_root.addChild(tg); scene_root.addChild(shape); SimpleScene scene = new SimpleScene(); scene.setRenderedGeometry(scene_root); scene.setActiveView(vp); // Then the basic layer and viewport at the top: SimpleViewport view = new SimpleViewport(); view.setDimensions(0, 0, 500, 500); view.setScene(scene); SimpleLayer layer = new SimpleLayer(); layer.setViewport(view); Layer[] layers = {layer}; displayManager.setLayers(layers, 1); }
public static Object3D[] load(byte[] data, int offset) { DataInputStream old = dis; dis = new DataInputStream(new ByteArrayInputStream(data)); try { while (dis.available() > 0) { int objectType = readByte(); int length = readInt(); System.out.println("objectType: " + objectType); System.out.println("length: " + length); dis.mark(Integer.MAX_VALUE); if (objectType == 0) { int versionHigh = readByte(); int versionLow = readByte(); boolean hasExternalReferences = readBoolean(); int totolFileSize = readInt(); int approximateContentSize = readInt(); String authoringField = readString(); objs.addElement(new Group()); // dummy } else if (objectType == 255) { // TODO: load external resource System.out.println("Loader: Loading external resources not implemented."); String uri = readString(); } else if (objectType == 1) { System.out.println("Loader: AnimationController not implemented."); objs.addElement(new Group()); // dummy } else if (objectType == 2) { System.out.println("Loader: AnimationTrack not implemented."); objs.addElement(new Group()); // dummy } else if (objectType == 3) { // System.out.println("Appearance"); Appearance appearance = new Appearance(); loadObject3D(appearance); appearance.setLayer(readByte()); appearance.setCompositingMode((CompositingMode) getObject(readInt())); appearance.setFog((Fog) getObject(readInt())); appearance.setPolygonMode((PolygonMode) getObject(readInt())); appearance.setMaterial((Material) getObject(readInt())); int numTextures = readInt(); for (int i = 0; i < numTextures; ++i) appearance.setTexture(i, (Texture2D) getObject(readInt())); objs.addElement(appearance); } else if (objectType == 4) { // System.out.println("Background"); Background background = new Background(); loadObject3D(background); background.setColor(readRGBA()); background.setImage((Image2D) getObject(readInt())); int modeX = readByte(); int modeY = readByte(); background.setImageMode(modeX, modeY); int cropX = readInt(); int cropY = readInt(); int cropWidth = readInt(); int cropHeight = readInt(); background.setCrop(cropX, cropY, cropWidth, cropHeight); background.setDepthClearEnable(readBoolean()); background.setColorClearEnable(readBoolean()); objs.addElement(background); // dummy } else if (objectType == 5) { // System.out.println("Camera"); Camera camera = new Camera(); loadNode(camera); int projectionType = readByte(); if (projectionType == Camera.GENERIC) { Transform t = new Transform(); t.set(readMatrix()); camera.setGeneric(t); } else { float fovy = readFloat(); float aspect = readFloat(); float near = readFloat(); float far = readFloat(); if (projectionType == Camera.PARALLEL) camera.setParallel(fovy, aspect, near, far); else camera.setPerspective(fovy, aspect, near, far); } objs.addElement(camera); } else if (objectType == 6) { // System.out.println("CompositingMode"); CompositingMode compositingMode = new CompositingMode(); loadObject3D(compositingMode); compositingMode.setDepthTestEnabled(readBoolean()); compositingMode.setDepthWriteEnabled(readBoolean()); compositingMode.setColorWriteEnabled(readBoolean()); compositingMode.setAlphaWriteEnabled(readBoolean()); compositingMode.setBlending(readByte()); compositingMode.setAlphaThreshold((float) readByte() / 255.0f); compositingMode.setDepthOffsetFactor(readFloat()); compositingMode.setDepthOffsetUnits(readFloat()); objs.addElement(compositingMode); } else if (objectType == 7) { // System.out.println("Fog"); Fog fog = new Fog(); loadObject3D(fog); fog.setColor(readRGB()); fog.setMode(readByte()); if (fog.getMode() == Fog.EXPONENTIAL) fog.setDensity(readFloat()); else { fog.setNearDistance(readFloat()); fog.setFarDistance(readFloat()); } objs.addElement(fog); } else if (objectType == 9) { // System.out.println("Group"); Group group = new Group(); loadGroup(group); objs.addElement(group); } else if (objectType == 10) { // System.out.println("Image2D"); Image2D image = null; loadObject3D(new Group()); // dummy int format = readByte(); boolean isMutable = readBoolean(); int width = readInt(); int height = readInt(); if (!isMutable) { // Read palette int paletteSize = readInt(); byte[] palette = null; if (paletteSize > 0) { palette = new byte[paletteSize]; dis.readFully(palette); } // Read pixels int pixelSize = readInt(); byte[] pixel = new byte[pixelSize]; dis.readFully(pixel); // Create image if (palette != null) image = new Image2D(format, width, height, pixel, palette); else image = new Image2D(format, width, height, pixel); } else image = new Image2D(format, width, height); dis.reset(); loadObject3D(image); objs.addElement(image); } else if (objectType == 19) { System.out.println("Loader: KeyframeSequence not implemented."); /* Byte interpolation; Byte repeatMode; Byte encoding; UInt32 duration; UInt32 validRangeFirst; UInt32 validRangeLast; UInt32 componentCount; UInt32 keyframeCount; IF encoding == 0 FOR each key frame... UInt32 time; Float32[componentCount] vectorValue; END ELSE IF encoding == 1 Float32[componentCount] vectorBias; Float32[componentCount] vectorScale; FOR each key frame... UInt32 time; Byte[componentCount] vectorValue; END ELSE IF encoding == 2 Float32[componentCount] vectorBias; Float32[componentCount] vectorScale; FOR each key frame... UInt32 time; UInt16[componentCount] vectorValue; END END */ objs.addElement(new Group()); // dummy } else if (objectType == 12) { // System.out.println("Light"); Light light = new Light(); loadNode(light); float constant = readFloat(); float linear = readFloat(); float quadratic = readFloat(); light.setAttenuation(constant, linear, quadratic); light.setColor(readRGB()); light.setMode(readByte()); light.setIntensity(readFloat()); light.setSpotAngle(readFloat()); light.setSpotExponent(readFloat()); objs.addElement(light); } else if (objectType == 13) { // System.out.println("Material"); Material material = new Material(); loadObject3D(material); material.setColor(Material.AMBIENT, readRGB()); material.setColor(Material.DIFFUSE, readRGBA()); material.setColor(Material.EMISSIVE, readRGB()); material.setColor(Material.SPECULAR, readRGB()); material.setShininess(readFloat()); material.setVertexColorTrackingEnable(readBoolean()); objs.addElement(material); } else if (objectType == 14) { // System.out.println("Mesh"); loadNode(new Group()); // dummy VertexBuffer vertices = (VertexBuffer) getObject(readInt()); int submeshCount = readInt(); IndexBuffer[] submeshes = new IndexBuffer[submeshCount]; Appearance[] appearances = new Appearance[submeshCount]; for (int i = 0; i < submeshCount; ++i) { submeshes[i] = (IndexBuffer) getObject(readInt()); appearances[i] = (Appearance) getObject(readInt()); } Mesh mesh = new Mesh(vertices, submeshes, appearances); dis.reset(); loadNode(mesh); objs.addElement(mesh); } else if (objectType == 15) { System.out.println("Loader: MorphingMesh not implemented."); /* UInt32 morphTargetCount; FOR each target buffer... ObjectIndex morphTarget; Float32 initialWeight; END */ objs.addElement(new Group()); // dummy } else if (objectType == 8) { // System.out.println("PolygonMode"); PolygonMode polygonMode = new PolygonMode(); loadObject3D(polygonMode); polygonMode.setCulling(readByte()); polygonMode.setShading(readByte()); polygonMode.setWinding(readByte()); polygonMode.setTwoSidedLightingEnable(readBoolean()); polygonMode.setLocalCameraLightingEnable(readBoolean()); polygonMode.setPerspectiveCorrectionEnable(readBoolean()); objs.addElement(polygonMode); } else if (objectType == 16) { System.out.println("Loader: SkinnedMesh not implemented."); /* ObjectIndex skeleton; UInt32 transformReferenceCount; FOR each bone reference... ObjectIndex transformNode; UInt32 firstVertex; UInt32 vertexCount; Int32 weight; END */ objs.addElement(new Group()); // dummy } else if (objectType == 18) { System.out.println("Loader: Sprite not implemented."); /* ObjectIndex image; ObjectIndex appearance; Boolean isScaled; Int32 cropX; Int32 cropY; Int32 cropWidth; Int32 cropHeight; */ objs.addElement(new Group()); // dummy } else if (objectType == 17) { // System.out.println("Texture2D"); loadTransformable(new Group()); // dummy Texture2D texture = new Texture2D((Image2D) getObject(readInt())); texture.setBlendColor(readRGB()); texture.setBlending(readByte()); int wrapS = readByte(); int wrapT = readByte(); texture.setWrapping(wrapS, wrapT); int levelFilter = readByte(); int imageFilter = readByte(); texture.setFiltering(levelFilter, imageFilter); dis.reset(); loadTransformable(texture); objs.addElement(texture); } else if (objectType == 11) { // System.out.println("TriangleStripArray"); loadObject3D(new Group()); // dummy int encoding = readByte(); int firstIndex = 0; int[] indices = null; if (encoding == 0) firstIndex = readInt(); else if (encoding == 1) firstIndex = readByte(); else if (encoding == 2) firstIndex = readShort(); else if (encoding == 128) { int numIndices = readInt(); indices = new int[numIndices]; for (int i = 0; i < numIndices; ++i) indices[i] = readInt(); } else if (encoding == 129) { int numIndices = readInt(); indices = new int[numIndices]; for (int i = 0; i < numIndices; ++i) indices[i] = readByte(); } else if (encoding == 130) { int numIndices = readInt(); indices = new int[numIndices]; for (int i = 0; i < numIndices; ++i) indices[i] = readShort(); } int numStripLengths = readInt(); int[] stripLengths = new int[numStripLengths]; for (int i = 0; i < numStripLengths; ++i) stripLengths[i] = readInt(); dis.reset(); TriangleStripArray triStrip = null; if (indices == null) triStrip = new TriangleStripArray(firstIndex, stripLengths); else triStrip = new TriangleStripArray(indices, stripLengths); loadObject3D(triStrip); objs.addElement(triStrip); } else if (objectType == 20) { // System.out.println("VertexArray"); loadObject3D(new Group()); // dummy int componentSize = readByte(); int componentCount = readByte(); int encoding = readByte(); int vertexCount = readShort(); VertexArray vertices = new VertexArray(vertexCount, componentCount, componentSize); if (componentSize == 1) { byte[] values = new byte[componentCount * vertexCount]; if (encoding == 0) dis.readFully(values); else { byte last = 0; for (int i = 0; i < vertexCount * componentCount; ++i) { last += readByte(); values[i] = last; } } vertices.set(0, vertexCount, values); } else { short last = 0; short[] values = new short[componentCount * vertexCount]; for (int i = 0; i < componentCount * vertexCount; ++i) { if (encoding == 0) values[i] = (short) readShort(); else { last += (short) readShort(); values[i] = last; } } vertices.set(0, vertexCount, values); } dis.reset(); loadObject3D(vertices); objs.addElement(vertices); } else if (objectType == 21) { // System.out.println("VertexBuffer"); VertexBuffer vertices = new VertexBuffer(); loadObject3D(vertices); vertices.setDefaultColor(readRGBA()); VertexArray positions = (VertexArray) getObject(readInt()); float[] bias = new float[3]; bias[0] = readFloat(); bias[1] = readFloat(); bias[2] = readFloat(); float scale = readFloat(); vertices.setPositions(positions, scale, bias); vertices.setNormals((VertexArray) getObject(readInt())); vertices.setColors((VertexArray) getObject(readInt())); int texCoordArrayCount = readInt(); for (int i = 0; i < texCoordArrayCount; ++i) { VertexArray texcoords = (VertexArray) getObject(readInt()); bias[0] = readFloat(); bias[1] = readFloat(); bias[2] = readFloat(); scale = readFloat(); vertices.setTexCoords(i, texcoords, scale, bias); } objs.addElement(vertices); } else if (objectType == 22) { // System.out.println("World"); World world = new World(); loadGroup(world); world.setActiveCamera((Camera) getObject(readInt())); world.setBackground((Background) getObject(readInt())); objs.addElement(world); } else { System.out.println("Loader: unsupported objectType " + objectType + "."); } dis.reset(); dis.skipBytes(length); } } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); } dis = old; return null; }
private void readNode(Node node) { EnvironmentDom dom; NamedNodeMap attrs = node.getAttributes(); try { dom = EnvironmentDom.valueOf(node.getNodeName()); } catch (java.lang.IllegalArgumentException ex) { return; } switch (dom) { case background: String type = attrs.getNamedItem("type").getTextContent(); if (type.equals("color")) mainScene.setBackgroundColor( new Color3f(Converter.stringToFloatArray(node.getTextContent()))); else if (type.equals("image")) { try { File bgFile = new File(attrs.getNamedItem("src").getTextContent()); mainScene.setBackgroundFile(bgFile); } catch (NullPointerException ex) { System.err.println(ex.getMessage()); } } break; case limits: float width = Float.parseFloat(attrs.getNamedItem("width").getTextContent()); float height = Float.parseFloat(attrs.getNamedItem("height").getTextContent()); float deepness = Float.parseFloat(attrs.getNamedItem("deepness").getTextContent()); float thickness = Float.parseFloat(attrs.getNamedItem("thickness").getTextContent()); mainScene.getEnvironmentLimits().updateLimits(width, height, deepness, thickness); // mainScene.getEnvironmentLimits().calculateUniverseBounds(); break; case light: type = attrs.getNamedItem("type").getTextContent(); sceneLight = new SceneLight(LightType.valueOf(type)); scene.addLightNode(sceneLight.getLight()); mainScene.addLight(sceneLight); break; case object: type = attrs.getNamedItem("type").getTextContent(); String src = attrs.getNamedItem("src").getTextContent(); String id = attrs.getNamedItem("id").getTextContent(); object = new MainSceneComponent( Integer.parseInt(id), ComponentType.valueOf(type), new File(src)); object.loadType(); if (attrs.getNamedItem("scale") != null) { String sca = attrs.getNamedItem("scale").getTextContent(); object.setScale(Double.valueOf(sca)); } if (attrs.getNamedItem("name") != null) { String name = attrs.getNamedItem("name").getTextContent(); object.setComponentName(name); } scene.addViewGroup(object.getTransformGroup()); mainScene.addComponent(object); break; case appearance: limitApp = new Appearance(); break; case material: material = new Material(); break; case texture: // TextureLoader textureLoad = new // TextureLoader(attrs.getNamedItem("src").getTextContent(), null); // ImageComponent2D textureIm = textureLoad.getScaledImage(128, 128); // texture = new Texture2D(Texture2D.BASE_LEVEL, Texture2D.RGB, // textureIm.getWidth(), textureIm.getHeight()); // texture.setImage(0, textureIm); // limitApp.setTexture(texture); // TextureAttributes textureAttr = new TextureAttributes(); // textureAttr.setTextureMode(TextureAttributes.REPLACE); // limitApp.setTextureAttributes(textureAttr); // TexCoordGeneration tcg = new // TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, // // TexCoordGeneration.TEXTURE_COORDINATE_2); // limitApp.setTexCoordGeneration(tcg); //// limitApp.setTransparencyAttributes(new // TransparencyAttributes(TransparencyAttributes.NICEST, 0.7f)); //// mainScene.getEnvironmentLimits().setAppearance(EnvironmentLimits.RIGHT, // limitApp); // mainScene.getEnvironmentLimits().setAppearance(limitApp); mainScene .getEnvironmentLimits() .setTexture(new File(attrs.getNamedItem("src").getTextContent())); boolean enabled = Boolean.parseBoolean(attrs.getNamedItem("enabled").getTextContent()); mainScene.getEnvironmentLimits().setTextureFlag(enabled); break; case ambient: material.setAmbientColor(new Color3f(Converter.stringToFloatArray(node.getTextContent()))); break; case emissive: material.setEmissiveColor(new Color3f(Converter.stringToFloatArray(node.getTextContent()))); break; case diffuse: material.setDiffuseColor(new Color3f(Converter.stringToFloatArray(node.getTextContent()))); break; case specular: material.setSpecularColor(new Color3f(Converter.stringToFloatArray(node.getTextContent()))); break; case shininess: material.setShininess(Float.parseFloat(node.getTextContent())); limitApp.setMaterial(material); // mainScene.getEnvironmentLimits().setAppearance(limitApp); break; case color: sceneLight.setColor(new Color3f(Converter.stringToFloatArray(node.getTextContent()))); break; case position: sceneLight.setPosition(new Point3f(Converter.stringToFloatArray(node.getTextContent()))); // if(sceneLight instanceof PointLight){ // PointLight pl = (PointLight)sceneLight; // pl.setPosition(new Point3f(Converter.stringToFloatArray // (node.getTextContent()))); // } else if(sceneLight instanceof DirectionalLight){ // DirectionalLight dl = (DirectionalLight)sceneLight; // dl.setDirection(new Vector3f(Converter.stringToFloatArray // (node.getTextContent()))); // } break; case direction: sceneLight.setDirection(new Vector3f(Converter.stringToFloatArray(node.getTextContent()))); case atenuation: // PointLight pl = (PointLight)sceneLight; // pl.setAttenuation(new Point3f(Converter.stringToFloatArray( // node.getTextContent()))); sceneLight.setAttenuation(new Point3f(Converter.stringToFloatArray(node.getTextContent()))); break; case objPos: object.setPosition(Converter.stringToDoubleArray(node.getTextContent())); break; case angles: double rotation[] = Converter.stringToDoubleArray(node.getTextContent()); object.setRotation(rotation); break; } }