コード例 #1
0
    protected void initScene() {
      PointLight light = new PointLight();
      light.setZ(6);
      light.setPower(2);

      getCurrentScene().addLight(light);

      Texture jetTexture = new Texture("jetTexture", R.drawable.jettexture);
      SphereMapTexture sphereMapTexture =
          new SphereMapTexture("manilaSphereMapTex", R.drawable.manila_sphere_map);

      jetTexture.setInfluence(.8f);
      // -- important!
      sphereMapTexture.isEnvironmentTexture(true);
      sphereMapTexture.setInfluence(.2f);

      Object3D jet1 = null;
      // -- sphere map with texture

      try {
        Material material1 = new Material();
        material1.enableLighting(true);
        material1.setDiffuseMethod(new DiffuseMethod.Lambert());
        material1.addTexture(jetTexture);
        material1.addTexture(sphereMapTexture);

        ObjectInputStream ois;
        ois = new ObjectInputStream(mContext.getResources().openRawResource(R.raw.jet));
        jet1 = new Object3D((SerializedObject3D) ois.readObject());
        jet1.setMaterial(material1);
        jet1.setY(2.5f);
        addChild(jet1);
      } catch (Exception e) {
        e.printStackTrace();
      }

      Vector3 axis = new Vector3(2, -4, 1);
      axis.normalize();

      Animation3D anim1 = new RotateAnimation3D(axis, 360);
      anim1.setRepeatMode(RepeatMode.INFINITE);
      anim1.setDuration(12000);
      anim1.setTransformable3D(jet1);
      registerAnimation(anim1);
      anim1.play();

      sphereMapTexture = new SphereMapTexture("manilaSphereMapTex2", R.drawable.manila_sphere_map);
      sphereMapTexture.isEnvironmentTexture(true);
      sphereMapTexture.setInfluence(.5f);

      Material material2 = new Material();
      // -- important, indicate that we want to mix the sphere map with a color
      material2.enableLighting(true);
      material2.setDiffuseMethod(new DiffuseMethod.Lambert());
      try {
        material2.addTexture(sphereMapTexture);
      } catch (TextureException e) {
        e.printStackTrace();
      }
      material2.setColorInfluence(.5f);

      Object3D jet2 = jet1.clone(false);
      jet2.setMaterial(material2);
      // -- also specify a color
      jet2.setColor(0xff666666);
      jet2.setY(-2.5f);
      addChild(jet2);

      Animation3D anim2 = new RotateAnimation3D(axis, -360);
      anim2.setRepeatMode(RepeatMode.INFINITE);
      anim2.setDuration(12000);
      anim2.setTransformable3D(jet2);
      registerAnimation(anim2);
      anim2.play();

      getCurrentCamera().setPosition(0, 0, 14);
    }
コード例 #2
0
ファイル: LoaderOBJ.java プロジェクト: jitendraameta/Rajawali
    public void setMaterial(Object3D object, String materialName) throws TextureException {
      MaterialDef matDef = null;

      for (int i = 0; i < mMaterials.size(); ++i) {
        if (mMaterials.get(i).name.equals(materialName)) {
          matDef = mMaterials.get(i);
          break;
        }
      }

      boolean hasTexture = matDef != null && matDef.diffuseTexture != null;
      boolean hasBump = matDef != null && matDef.bumpTexture != null;
      boolean hasSpecularTexture = matDef != null && matDef.specularColorTexture != null;
      boolean hasSpecular =
          matDef != null && matDef.specularColor > 0xff000000 && matDef.specularCoefficient > 0;

      AMaterial mat = null;

      if (hasSpecular && !hasBump) mat = new PhongMaterial();
      else if (hasBump && !hasSpecularTexture) mat = new NormalMapMaterial();
      else if (hasBump && hasSpecularTexture) mat = new NormalMapPhongMaterial();
      else mat = new DiffuseMaterial();

      mat.setUseSingleColor(!hasTexture);
      object.setMaterial(mat);
      object.setColor(
          matDef != null ? matDef.diffuseColor : (0xff000000 + ((int) (Math.random() * 0xffffff))));
      if (hasSpecular || hasSpecularTexture) {
        PhongMaterial phong = (PhongMaterial) mat;
        phong.setSpecularColor(matDef.specularColor);
        phong.setShininess(matDef.specularCoefficient);
      }

      if (hasTexture) {
        RajLog.i("hastex " + object.getName() + ", " + matDef.diffuseTexture);
        if (mFile == null) {
          int identifier =
              mResources.getIdentifier(
                  getFileNameWithoutExtension(matDef.diffuseTexture), "drawable", mResourcePackage);
          mat.addTexture(new Texture(identifier));
        } else {
          String filePath =
              mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.diffuseTexture);
          mat.addTexture(
              new Texture(
                  getOnlyFileName(matDef.diffuseTexture), BitmapFactory.decodeFile(filePath)));
        }
      }
      if (hasBump) {
        if (mFile == null) {
          int identifier =
              mResources.getIdentifier(
                  getFileNameWithoutExtension(matDef.bumpTexture), "drawable", mResourcePackage);
          mat.addTexture(new NormalMapTexture(identifier));
        } else {
          String filePath =
              mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.bumpTexture);
          mat.addTexture(
              new NormalMapTexture(
                  getOnlyFileName(matDef.bumpTexture), BitmapFactory.decodeFile(filePath)));
        }
      }
      if (hasSpecularTexture) {
        if (mFile == null) {
          int identifier =
              mResources.getIdentifier(
                  getFileNameWithoutExtension(matDef.specularColorTexture),
                  "drawable",
                  mResourcePackage);
          mat.addTexture(new SpecularMapTexture(identifier));
        } else {
          String filePath =
              mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.specularColorTexture);
          mat.addTexture(
              new SpecularMapTexture(
                  getOnlyFileName(matDef.specularColorTexture),
                  BitmapFactory.decodeFile(filePath)));
        }
      }
    }