protected void initScene() {
   RajLog.systemInformation();
   setFrameRate(60);
   LoaderGCode gCodeParser =
       new LoaderGCode(
           getResources(), getTextureManager(), R.raw.calibrationcube_404020_psm_pla35);
   try {
     Object3D gCode3D = gCodeParser.parse().getParsedObject();
     if (null != gCode3D) {
       Line3D firstPos = (Line3D) gCode3D.getChildAt(0);
       Line3D lastPos = null;
       if (gCode3D.getNumChildren() < 2) {
         lastPos = firstPos;
       } else {
         lastPos = (Line3D) gCode3D.getChildAt(gCode3D.getNumChildren() - 1);
       }
       getCurrentCamera().setPosition(0, 0, lastPos.getPoint(0).z + 150);
       getCurrentCamera().setLookAt(0, 0, 0);
       float scaleFactor = 0.7f;
       gCode3D.setScale(scaleFactor);
       gCode3D.setPosition(
           -firstPos.getPoint(0).x * scaleFactor, -firstPos.getPoint(0).y * scaleFactor, 0);
       gCode3D.setRotation(360 - 35, 0, 360 - 15);
       Material mat = new Material();
       for (int i = 0; i < gCode3D.getNumChildren(); i++) {
         gCode3D.getChildAt(i).setMaterial(mat);
       }
       addChild(gCode3D);
     }
   } catch (Exception e) {
     RajLog.e(
         new StringBuilder()
             .append("error init'ing gcode GL scene:\n")
             .append(Log.getStackTraceString(e))
             .toString());
   }
 }
Пример #2
0
  private void buildMesh(Model model, Stack<ALight> lights)
      throws TextureException, ParsingException {
    Object3D o = new Object3D(model.name);
    boolean hasUVs = model.layerElementUV.uVIndex != null;

    int[] vidx = model.polygonVertexIndex.data;
    int[] uvidx = null;
    float[] modelVerts = model.vertices.data;
    float[] modelNorm = model.layerElementNormal.normals.data;
    float[] modelUv = null;

    ArrayList<Integer> indices = new ArrayList<Integer>();
    ArrayList<Float> vertices = new ArrayList<Float>();
    ArrayList<Float> normals = new ArrayList<Float>();
    ArrayList<Float> uvs = null;

    if (hasUVs) {
      uvs = new ArrayList<Float>();
      uvidx = model.layerElementUV.uVIndex.data;
      modelUv = model.layerElementUV.uV.data;
    }

    int count = 0;
    int indexCount = 0;
    int[] triIds = new int[3];
    int[] quadIds = new int[6];
    int i = 0, j = 0, k = 0;
    int vidxLen = vidx.length;

    for (i = 0; i < vidxLen; ++i) {
      count++;

      if (vidx[i] < 0) {
        if (count == 3) {
          int index1 = vidx[i - 2], index2 = vidx[i - 1], index3 = (vidx[i] * -1) - 1;

          indices.add(indexCount++);
          indices.add(indexCount++);
          indices.add(indexCount++);

          triIds[0] = index1 * 3;
          triIds[1] = index2 * 3;
          triIds[2] = index3 * 3;

          for (j = 0; j < 3; ++j) {
            int cid = triIds[j];
            for (k = 0; k < 3; ++k) {
              vertices.add(modelVerts[cid + k]);
              int dir = i == 0 ? -1 : 1;
              normals.add(modelNorm[cid + k] * dir);
            }
          }

          if (hasUVs) {
            int uvIndex3 = uvidx[i] * 2;
            int uvIndex2 = uvidx[i - 1] * 2;
            int uvIndex1 = uvidx[i - 2] * 2;

            uvs.add(modelUv[uvIndex1 + 0]);
            uvs.add(1f - modelUv[uvIndex1 + 1]);

            uvs.add(modelUv[uvIndex2 + 0]);
            uvs.add(1f - modelUv[uvIndex2 + 1]);

            uvs.add(modelUv[uvIndex3 + 0]);
            uvs.add(1f - modelUv[uvIndex3 + 1]);
          }
        } else {
          int index1 = vidx[i - 3];
          int index2 = vidx[i - 2];
          int index3 = vidx[i - 1];
          int index4 = (vidx[i] * -1) - 1;

          indices.add(indexCount++);
          indices.add(indexCount++);
          indices.add(indexCount++);
          indices.add(indexCount++);
          indices.add(indexCount++);
          indices.add(indexCount++);

          quadIds[0] = index1 * 3;
          quadIds[1] = index2 * 3;
          quadIds[2] = index3 * 3;
          quadIds[3] = index4 * 3;
          quadIds[4] = index1 * 3;
          quadIds[5] = index3 * 3;

          for (j = 0; j < 6; ++j) {
            int cid = quadIds[j];
            for (k = 0; k < 3; ++k) {
              vertices.add(modelVerts[cid + k]);
              normals.add(modelNorm[cid + k]);
            }
          }

          if (hasUVs) {
            int uvIndex1 = uvidx[i - 3] * 2;
            int uvIndex2 = uvidx[i - 2] * 2;
            int uvIndex3 = uvidx[i - 1] * 2;
            int uvIndex4 = uvidx[i] * 2;

            quadIds[0] = uvIndex1;
            quadIds[1] = uvIndex2;
            quadIds[2] = uvIndex3;
            quadIds[3] = uvIndex4;
            quadIds[4] = uvIndex1;
            quadIds[5] = uvIndex3;

            for (j = 0; j < 6; ++j) {
              int cid = quadIds[j];
              for (k = 0; k < 2; ++k) {
                if (k == 0) uvs.add(modelUv[cid + k]);
                else uvs.add(1f - modelUv[cid + k]);
              }
            }
          }
        }
        count = 0;
      }
    }

    o.setData(
        convertFloats(vertices),
        convertFloats(normals),
        hasUVs ? convertFloats(uvs) : null,
        null,
        convertIntegers(indices));

    vertices.clear();
    vertices = null;
    normals.clear();
    normals = null;
    if (hasUVs) {
      uvs.clear();
      uvs = null;
    }
    indices.clear();
    indices = null;
    o.setMaterial(getMaterialForMesh(o, model.name));
    setMeshTextures(o, model.name);

    o.setPosition(model.properties.lclTranslation);
    o.setX(o.getX() * -1);
    o.setScale(model.properties.lclScaling);
    o.setRotation(model.properties.lclRotation);
    o.setRotZ(-o.getRotZ());

    mRootObject.addChild(o);
  }
    protected void initScene() {
      DirectionalLight light = new DirectionalLight(0, -.6f, .4f);
      light.setPower(1);
      getCurrentScene().addLight(light);

      // -- create sky sphere
      mSphere = new Sphere(400, 8, 8);
      Material sphereMaterial = new Material();
      try {
        sphereMaterial.addTexture(new Texture("skySphere", R.drawable.skysphere));
      } catch (TextureException e1) {
        e1.printStackTrace();
      }
      mSphere.setMaterial(sphereMaterial);
      mSphere.setDoubleSided(true);
      addChild(mSphere);

      try {
        // -- load gzipped serialized object
        ObjectInputStream ois;
        GZIPInputStream zis =
            new GZIPInputStream(mContext.getResources().openRawResource(R.raw.raptor));
        ois = new ObjectInputStream(zis);
        mRaptor = new Object3D((SerializedObject3D) ois.readObject());
        Material raptorMaterial = new Material();
        raptorMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
        raptorMaterial.enableLighting(true);
        raptorMaterial.addTexture(new Texture("raptorTex", R.drawable.raptor_texture));
        mRaptor.setMaterial(raptorMaterial);
        mRaptor.setScale(.5f);
        addChild(mRaptor);
      } catch (Exception e) {
        e.printStackTrace();
      }

      // -- 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));
      } catch (TextureException e) {
        e.printStackTrace();
      }
      mRootCube.setMaterial(rootCubeMaterial);
      mRootCube.setY(-1f);
      // -- similar objects with the same material, optimize
      mRootCube.setRenderChildrenAsBatch(true);
      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), .1f);
      // -- tell the camera which object to chase
      chaseCamera.setObjectToChase(mRaptor);
      // -- set the far plane to 1000 so that we actually see the sky sphere
      chaseCamera.setFarPlane(1000);
      replaceAndSwitchCamera(chaseCamera, 0);
    }