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());
   }
 }
Exemple #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);
  }