static { // static initializer that creates a mesh blueprint and loads the // textures. try { SimpleStream ss = new SimpleStream("data/weapon.jpg"); TextureManager.getInstance().addTexture("weapon", new Texture(ss.getStream())); ss.close(); ss = new SimpleStream("data/snork.md2"); bluePrint = new ClientObject(Loader.loadMD2(ss.getStream(), 0.22f)); // 0.32f bluePrint.translate(0, 4.5f, 0); bluePrint.translateMesh(); bluePrint.getTranslationMatrix().setIdentity(); bluePrint.translate(100000, 100000, 100000); sphereBluePrint = Primitives.getSphere(9, 6); sphereBluePrint.build(); sphereBluePrint.setTexture("shield"); sphereBluePrint.calcTextureWrapSpherical(); ss.close(); } catch (Exception e) { e.printStackTrace(); } }
public Car() { /** * The car's "body" is this instance itself. That may seem a bit strange at first glance, but * it's quite convenient in this case. */ super(Primitives.getBox(8, 0.25f)); rightFront = Primitives.getSphere(5, 4); leftFront = Primitives.getSphere(5, 4); rightRear = Primitives.getSphere(5, 4); leftRear = Primitives.getSphere(5, 4); /** The wheels are parts, i.e. children of the car */ addChild(rightFront); addChild(leftFront); addChild(rightRear); addChild(leftRear); /** Initialize the car and the wheels */ setTexture("car"); rightFront.setTexture("car"); leftFront.setTexture("car"); rightRear.setTexture("car"); leftRear.setTexture("car"); setEnvmapped(Object3D.ENVMAP_ENABLED); rightFront.setEnvmapped(Object3D.ENVMAP_ENABLED); leftFront.setEnvmapped(Object3D.ENVMAP_ENABLED); rightRear.setEnvmapped(Object3D.ENVMAP_ENABLED); leftRear.setEnvmapped(Object3D.ENVMAP_ENABLED); /** We need to offset the wheels a little... */ rightFront.translate(new SimpleVector(-8, 4, 8)); rightRear.translate(new SimpleVector(-8, 4, -8)); leftFront.translate(new SimpleVector(8, 4, 8)); leftRear.translate(new SimpleVector(8, 4, -8)); rightFront.translateMesh(); rightRear.translateMesh(); leftFront.translateMesh(); leftRear.translateMesh(); rightFront.setTranslationMatrix(new Matrix()); rightRear.setTranslationMatrix(new Matrix()); leftFront.setTranslationMatrix(new Matrix()); leftRear.setTranslationMatrix(new Matrix()); /** ...the wheels are now in place. We can now build the car. */ build(); rightRear.build(); rightFront.build(); leftRear.build(); leftFront.build(); }
public void onSurfaceChanged(GL10 gl, int w, int h) { if (frameBuffer != null) frameBuffer.dispose(); frameBuffer = new FrameBuffer(gl, w, h); // 載入模型 arch = loadModel("Arch.3DS", 15); arch.compile(); box = loadModel("Box.3DS", 8); box.compile(); cylinder = loadModel("Cylinder.3DS", 7); cylinder.compile(); arch.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS); box.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS); cylinder.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS); arch.setName("Arch"); box.setName("Box"); cylinder.setName("Cylinder"); // 設定世界場景(加入3個模型) world = new World(); world.setAmbientLight(75, 75, 75); world.addObject(arch); world.addObject(box); world.addObject(cylinder); box.translate(new SimpleVector(-15, -20, 5)); arch.translate(new SimpleVector(-15, 5, -5)); // 記錄原始位置 archOriginPosition = arch.getTranslation(); boxOriginPosition = box.getTranslation(); cylinderOriginPosition = cylinder.getTranslation(); // 設定Camera Camera camera = world.getCamera(); camera.moveCamera(Camera.CAMERA_MOVEOUT, 50); // 向後移50 camera.lookAt(SimpleVector.ORIGIN); // 看向(0,0,0) // 設定Light sun = new Light(world); sun.setIntensity(175, 175, 175); sun.setPosition(camera.getPosition()); MemoryHelper.compact(); }
private void createShieldMesh() { shield = new Object3D(sphereBluePrint); shield.setVisibility(false); shield.setMesh(sphereBluePrint.getMesh()); shield.build(); shield.translate(0, 4.5f, 0); shield.setTransparency(7); shield.setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD); if (Globals.compiledObjects) { shield.compile(); } addChildToSuper(shield); }
@Override public void onSurfaceChanged(GL10 gl, int width, int height) { if (fb != null) { fb.dispose(); } fb = new FrameBuffer(gl, width, height); if (master == null) { world = new World(); world.setAmbientLight(20, 20, 20); sun = new Light(world); sun.setIntensity(250, 250, 250); Texture texture = new Texture(BitmapHelper.convert(getResources().getDrawable(R.drawable.bublebgnd))); TextureManager.getInstance().addTexture("bublebgnd", texture); // Create a texture out of the icon...:-) texture = new Texture( BitmapHelper.rescale( BitmapHelper.convert(getResources().getDrawable(R.drawable.icon)), 64, 64)); TextureManager.getInstance().addTexture("tex", texture); bubbles = new Object3D[BUBBLES]; for (byte i = 0; i < BUBBLES; i++) { Object3D bubble = Primitives.getSphere(8); bubble.calcTextureWrapSpherical(); // bubble.setCulling(true); bubble.translate(-30 + i * 20, 30.0f, 0.0f); bubble.setTransparency(70); // bubble.rotateX(-(float) Math.PI / Math.abs(r.nextInt()/3)); bubble.setTexture("tex"); bubble.strip(); bubble.build(); bubbles[i] = bubble; } background = Primitives.getPlane(1, 110f); // background.$ // background.calcTextureWrapSpherical(); background.setTexture("bublebgnd"); background.strip(); background.build(); world.addObject(background); // world. world.addObjects(bubbles); Camera cam = world.getCamera(); cam.moveCamera(Camera.CAMERA_MOVEOUT, 70); cam.lookAt(background.getTransformedCenter()); // cam.lookAt(bubbles.getTransformedCenter()); SimpleVector sv = new SimpleVector(); sv.set(background.getTransformedCenter()); sv.y -= 100; sv.z -= 100; sun.setPosition(sv); MemoryHelper.compact(); if (master == null) { Logger.log("Saving master Activity!"); master = BubblesActivity.this; } } }
static { try { SimpleStream ss = new SimpleStream("data/crate.3ds"); bluePrint = new ClientObject(Loader.load3DS(ss.getStream(), 0.09f)[0]); ss.close(); bluePrint.translate(0, -4.3f, -5); bluePrint.translateMesh(); bluePrint.getTranslationMatrix().setIdentity(); TextureInfo ti = null; if (!Globals.normalMapping) { bluePrint.setTexture("crate"); } else { ti = new TextureInfo(TextureManager.getInstance().getTextureID("crate")); ti.add( TextureManager.getInstance().getTextureID("cratenormals"), TextureInfo.MODE_MODULATE); bluePrint.setTexture(ti); } bluePrint.getMesh().compress(); bluePrint.build(); Object3D child = null; Animation anim = new Animation(6); anim.setClampingMode(Animation.USE_CLAMPING); anim.createSubSequence("explosion"); Loader.setVertexOptimization(false); for (int i = 1; i < 6; i++) { String name = "data/crate" + i + ".3ds"; ss = new SimpleStream(name); Object3D obj = Loader.load3DS(ss.getStream(), 4.4050189f)[0]; ss.close(); obj.translate(0, -5f, -0.6f); obj.translateMesh(); obj.getTranslationMatrix().setIdentity(); if (!Globals.normalMapping) { obj.setTexture("crate"); } else { obj.setTexture(ti); } obj.build(); if (i == 1) { child = obj; child.setMesh(child.getMesh().cloneMesh(true)); } obj.getMesh().compress(); anim.addKeyFrame(obj.getMesh()); } child.setAnimationSequence(anim); Loader.setVertexOptimization(true); childBluePrint = child; /* * for (int i=0; i<child.getMesh().getBoundingBox().length; i++) { * System.out.print(child.getMesh().getBoundingBox()[i]); * System.out.println("="+bluePrint.getMesh().getBoundingBox()[i]); * } */ } catch (Exception e) { e.printStackTrace(); } }
private void Handle3DControl(Object3D obj, float originZ) { switch (Activity3Page.currentActionType) { case Move: // ---處理移動(Translate Position: X,Y)---begin SimpleVector transPos = new SimpleVector( obj.getTranslation().x + deltaTranslatePositionX, obj.getTranslation().y + deltaTranslatePositionY, obj.getTranslation().z); SimpleVector projectScreenV3 = Interact2D.project3D2D( world.getCamera(), frameBuffer, transPos); // (重要!!!)這可以知道3D物件在2D畫面的投影 if (projectScreenV3.x >= 0 && projectScreenV3.y >= 0 && projectScreenV3.x <= frameBuffer.getWidth() && projectScreenV3.y <= frameBuffer.getHeight()) { obj.translate(deltaTranslatePositionX, deltaTranslatePositionY, 0); } deltaTranslatePositionX = 0; deltaTranslatePositionY = 0; // ---處理移動(Translate Position: X,Y)---end // ---處理放大縮小(Scale)---begin if (deltaScale != 0) { float currentScale = obj.getScale() + deltaScale; if (currentScale <= 0.5f) { obj.setScale(0.5f); } else if (currentScale >= 1.5f) { obj.setScale(1.5f); } else { obj.setScale(currentScale); } deltaScale = 0; } // ---處理放大縮小(Scale)---end break; case Rotate: // ---處理旋轉(Rotate)---begin if (deltaRotateAngleX != 0) { obj.rotateY(deltaRotateAngleX); deltaRotateAngleX = 0; } if (deltaRotateAngleY != 0) { obj.rotateX(deltaRotateAngleY); deltaRotateAngleY = 0; } // ---處理旋轉(Rotate)---end break; case Depth: // ---處理深度(Translate Depth: Z軸)---begin float currentPosZ = obj.getTranslation().z + deltaTranslatePositionZ; if (currentPosZ > originZ - ZaxisCanMoveDistance && currentPosZ < originZ + ZaxisCanMoveDistance) { obj.translate(0, 0, deltaTranslatePositionZ); } deltaTranslatePositionZ = 0; // ---處理深度(Translate Depth: Z軸)---end break; } }
public void onSurfaceChanged(GL10 gl, int w, int h) { if (fb != null) { fb.dispose(); } fb = new FrameBuffer(gl, w, h); Loader = new Model3D(); if (master == null) { world = new World(); world.setAmbientLight(20, 20, 20); sun = new Light(world); sun.setIntensity(250, 250, 250); // OBJECT 1 try { double startTime1 = System.currentTimeMillis(); obj1 = Loader.localloadModel("" + thingName1 + ".3ds", thingScale); double endTime1 = System.currentTimeMillis() - startTime1; Log.i("Object 1 loading time", endTime1 + ""); dataEvent .getInstance() .getDatabaseManager() .saveData("Object 1 loading time", 0, endTime1, 0, 0); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } obj1.build(); world.addObject(obj1); Camera cam = world.getCamera(); cam.moveCamera(Camera.CAMERA_MOVEOUT, 50); cam.lookAt(obj1.getTransformedCenter()); SimpleVector sv = new SimpleVector(); sv.set(obj1.getTransformedCenter()); sv.y -= 100; sv.z -= 100; sun.setPosition(sv); MemoryHelper.compact(); // OBJECT2 try { double startTime2 = System.currentTimeMillis(); obj2 = Loader.loadModel("" + thingName2 + ".3ds", thingScale); double endTime2 = System.currentTimeMillis() - startTime2; Log.i("Object 2 loading time", endTime2 + ""); dataEvent .getInstance() .getDatabaseManager() .saveData("Object 2 loading time", 0, endTime2, 0, 0); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } obj2.translate(-6, 28, 0); obj2.rotateY(-.6f); world.addObject(obj2); // OBJECT3 try { double startTime3 = System.currentTimeMillis(); obj3 = Loader.loadModel("" + thingName3 + ".3ds", thingScale); double endTime3 = System.currentTimeMillis() - startTime3; Log.i("Object 3 loading time", endTime3 + ""); dataEvent .getInstance() .getDatabaseManager() .saveData("Object 3 loading time", 0, endTime3, 0, 0); boolean manualExtraction = true; if (manualExtraction == true) { extractDatabaseFile(new DatabaseCommons()); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } obj3.translate(-15, -20, 0); obj3.rotateY(-.6f); world.addObject(obj3); /*if (master == null) { Logger.log("Saving master Activity!"); master = MainActivity.this; }*/ } }