public void start() { // Ask 3DzzD factory for a fresh Scene3D this.scene = DzzD.newScene3D(); // Create a Scene3D loader and link it to a 3DS file IScene3DLoader loader = DzzD.newScene3DLoader(); loader.loadScene3D(this.getCodeBase().toString() + "3D/", "CUBE.3DS"); // Add the loader to the scene this.scene.setScene3DLoader(loader); // Wait until all object & texture are loaded while (this.scene.getNbMonitoredSceneObject() != 0) { this.scene.updateMonitoredSceneObjects(); DzzD.sleep(10); } /* * Set the active camera in the 3d scene * * We use a camera that is inside the 3ds file * * 3DzzD always provide a default camera that you can set using : * this.scene.setCurrentCamera3DById(0); */ this.scene.setCurrentCamera3DByName("Camera01"); // Ask 3DzzD factory for a software 3D Render this.render = DzzD.newRender3D(this.getClass(), "SOFT", null); // Add the Render3D canvas to the Applet Panel this.setLayout(null); this.add(this.render.getCanvas()); // Set the Render3D size and enable maximum antialias this.render.setSize(this.getSize().width, this.getSize().height, 7); // Set Camera Aspect ratio to 1:1 this.scene .getCurrentCamera3D() .setZoomY(((double) this.render.getWidth()) / ((double) this.render.getHeight())); // Tell the Render3D wich camera it should use to render this.render.setCamera3D(this.scene.getCurrentCamera3D()); // Render the frame this.renderSingleFrame(); }
private void loadAndDecode() { // LOAD 3DS FILE this.setAction(IProgressListener.ACTION_FILE_DOWNLOAD); ProgressListener pl = new ProgressListener(); IOManagerAsynch aLoader = new IOManagerAsynch(); aLoader.downloadData(this.baseURL + this.fileName, getClass(), null, pl, false); while (aLoader.running || !pl.getFinished()) { this.setProgress((50 * (pl.getProgress()) / pl.getMaximumProgress())); try { Thread.sleep(5); } catch (InterruptedException ie) { pl.setFinished(true); pl.setError(true); return; } } this.data = aLoader.getData(); if (pl.getError() || this.data == null) { this.setError(true); this.setFinished(true); return; } // DECODE 3DS FILE this.input = new ByteArrayInputStream(this.data); this.setProgress(50); this.setAction(IProgressListener.ACTION_FILE_DECOMPRESS); int offset = 0; while (offset < data.length) offset += decodeChunk(0); // CREATE SCENE FROM LOADED SCENE (INIT WITH KEYFRAME IF AVAILABLE) objets3D = new Scene3DObject[nbObjet + 1]; objets3D[0] = new Mesh3D(); objets3D[0].setName(this.fileName); double zx = 1; double zy = 1; double zz = 1; for (int nbo = 0; nbo < nbObjet; nbo++) { cObjet = tObjets3D[nbo]; objets3D[nbo + 1] = cObjet; Axis3D cAxe = cObjet.axes; Point3D o = cAxe.origine; Point3D ax = cAxe.axeX; Point3D ay = cAxe.axeY; Point3D az = cAxe.axeZ; // READ ZOOM FROM AXIS zx = ax.norm(); zy = ay.norm(); zz = az.norm(); ax.add(o); ay.add(o); az.add(o); // if(cObjet instanceof Camera3D) // System.out.println(cObjet.getPosition().toString()); // NORMALIZE AXIS cAxe.normalize(); if (cObjet instanceof Mesh3D) { Point3D vertex[] = ((Mesh3D) cObjet).vertices3D; for (int np = 0; np < vertex.length; np++) vertex[np].toLocalAxe(cAxe).zoom(1.0 / zx, 1.0 / zy, 1.0 / zz); } ax.sub(o); ay.sub(o); az.sub(o); // KEEP AXZ (TO KNOW IF ZOOM NEGATIVE) Point3D axz = new Point3D(); axz.copy(ax).cross(ay); // IF ZOOM NEGATIVE INVERT ALL AXIS AND FLIP OBJECT'S FACES NORMALS if (az.dot(axz) < 0.0) { ax.mul(-1.0); ay.mul(-1.0); az.mul(-1.0); zx = -zx; zy = -zy; zz = -zz; } ax.add(o); ay.add(o); az.add(o); // SET OBJECT ROTATION AND PIVOT FROM AXIS cObjet.axes.getPosition(cObjet.position); cObjet.axes.getRotationXZY(cObjet.rotation); // SET OBJECT ZOOM cObjet.zoom.set(zx, zy, zz); } // SET OBJECT AXIS IN WORLD SPACE WITH PIVOT,POSITION,ROTATION for (int nbo = 1; nbo < objets3D.length; nbo++) { cObjet = objets3D[nbo]; cObjet.axes.init(); cObjet.axes.set(cObjet.pivot, cObjet.position, cObjet.rotation); } // UPDATE OBJECT HIERARCHY for (int nbo = 1; nbo < objets3D.length; nbo++) { cObjet = objets3D[nbo]; if (cObjet.parent == null) cObjet.parent = objets3D[0]; cObjet.setParent(cObjet.parent); } // UPDATE OBJECT FOR HIERARCHY NEW for (int nbo = 1; nbo < objets3D.length; nbo++) { cObjet = objets3D[nbo]; // cObjet.axes.push(); IAxis3D a = DzzD.newAxis3D(); a.copy(cObjet.axes); if (cObjet.parent == null) cObjet.parent = objets3D[0]; cObjet.toLocalAxe(cObjet.parent.axes); // if(cObjet.parent instanceof ICamera3D) // cObjet.axes.pop(); cObjet.axes.copy(a); } try { // UPDATE ALL OBJECT WITH FIRST KEYFRAMER KEY if (this.isKeyFrame) { for (int nbo = 1; nbo < objets3D.length; nbo++) { cObjet = objets3D[nbo]; if (cObjet.id == -1) continue; IAxis3D a = DzzD.newAxis3D(); a.copy(cObjet.axes); // cObjet.axes.push(); Point3D rot = this.key3DRotAxe[cObjet.id]; if (rot != null) { double rota = this.key3DRotAng[cObjet.id]; cObjet.axes.init(); cObjet.axes.rotate(rota, -rot.x, -rot.y, -rot.z); cObjet.axes.getRotationXZY(cObjet.rotation); // cObjet.axes.pop(); cObjet.axes.copy(a); } Point3D trans = this.key3DTrans[cObjet.id]; if (trans != null) { cObjet.position.set(trans.x, trans.y, trans.z); if (cObjet.parent != null) cObjet.position.add(cObjet.parent.pivot); } Point3D zoom = this.key3DZoom[cObjet.id]; if (zoom != null) { cObjet.zoom.set(zoom.x, zoom.y, zoom.z); } cObjet.axes.copy(a); // cObjet.axes.pop(); } } // Add animator to object for (int nbo = 1; nbo < objets3D.length; nbo++) { cObjet = objets3D[nbo]; if (cObjet.id == -1) continue; if (this.animators[cObjet.id] != null) cObjet.setScene3DObjectAnimator(this.animators[cObjet.id]); } } catch (ArrayIndexOutOfBoundsException aioobe) { Log.log(aioobe); } catch (NullPointerException npe) { Log.log(npe); } for (int nbo = 1; nbo < objets3D.length; nbo++) { cObjet = objets3D[nbo]; Point3D zoom = cObjet.zoom; cObjet.zoom(zoom.x, zoom.y, zoom.z); } this.setFinished(true); this.setProgress(100); return; }