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 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; } }