@Override protected void onRender(long ellapsedRealtime, double deltaTime) { // -- no proper physics here, just a bad approximation to keep // this example as short as possible ;-) mRaptor.setZ(mRaptor.getZ() + 2.0); mRaptor.setX(Math.sin(mTime) * 20.0); // mRaptor.setRotZ(Math.sin(mTime + 8.0) * -30.0); // mRaptor.setRotY(180 + (mRaptor.getRotZ() * 0.1)); // mRaptor.setRotY(180); mRaptor.setY(Math.cos(mTime) * 10.0); mRaptor.setRotX(Math.cos(mTime + 1.0) * -20.0); mSphere.setZ(mRaptor.getZ()); mTime += 0.01; if (mRootCube.getZ() - mRaptor.getZ() <= (30 * -6)) { mRootCube.setZ(mRaptor.getZ()); } mPointLight.setPosition(getCurrentCamera().getPosition()); mPointLight.setLookAt(mRaptor.getWorldPosition()); super.onRender(ellapsedRealtime, deltaTime); }
@Override protected void initScene() { DirectionalLight light = new DirectionalLight(0, 0, 1.0); light.setPower(2.0f); getCurrentScene().addLight(light); mPointLight = new PointLight(); mPointLight.setPower(1.5f); getCurrentScene().addLight(mPointLight); // -- create sky sphere mSphere = new Sphere(400, 8, 8); Material sphereMaterial = new Material(); try { sphereMaterial.addTexture(new Texture("skySphere", R.drawable.skysphere)); sphereMaterial.setColorInfluence(0); } catch (ATexture.TextureException e1) { e1.printStackTrace(); } mSphere.setMaterial(sphereMaterial); mSphere.setDoubleSided(true); getCurrentScene().addChild(mSphere); mRaptor = new Sphere(1.0f, 24, 24); Material raptorMaterial = new Material(); SpecularMethod.Phong phongMethod = new SpecularMethod.Phong(); phongMethod.setShininess(180); sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert()); sphereMaterial.setSpecularMethod(phongMethod); sphereMaterial.enableLighting(true); mRaptor.setMaterial(raptorMaterial); mRaptor.setColor(0xffff00ff); getCurrentScene().addChild(mRaptor); // -- create a bunch of cubes that will serve as orientation helpers mCubes = new Object3D[30]; mRootCube = new Cube(1); Material rootCubeMaterial = new Material(); rootCubeMaterial.setDiffuseMethod(new DiffuseMethod.Lambert()); rootCubeMaterial.enableLighting(true); try { rootCubeMaterial.addTexture(new Texture("camouflage", R.drawable.camouflage)); rootCubeMaterial.setColorInfluence(0); } catch (ATexture.TextureException e) { e.printStackTrace(); } mRootCube.setMaterial(rootCubeMaterial); mRootCube.setY(-1f); // -- similar objects with the same material, optimize mRootCube.setRenderChildrenAsBatch(true); getCurrentScene().addChild(mRootCube); mCubes[0] = mRootCube; for (int i = 1; i < mCubes.length; ++i) { Object3D cube = mRootCube.clone(true); cube.setY(-1f); cube.setZ(i * 30); mRootCube.addChild(cube); mCubes[i] = cube; } // -- create a chase camera // the first parameter is the camera offset // the second parameter is the interpolation factor ChaseCamera chaseCamera = new ChaseCamera(new Vector3(0, 3, 16)); // -- tell the camera which object to chase chaseCamera.setLinkedObject(mRaptor); // -- set the far plane to 1000 so that we actually see the sky sphere chaseCamera.setFarPlane(1000); getCurrentScene().replaceAndSwitchCamera(chaseCamera, 0); }