Esempio n. 1
0
  private void createShieldMesh() {
    shield = new Object3D(sphereBluePrint);
    shield.setVisibility(false);
    shield.setMesh(sphereBluePrint.getMesh());
    shield.build();
    shield.translate(0, 4.5f, 0);
    shield.setTransparency(7);
    shield.setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);

    if (Globals.compiledObjects) {
      shield.compile();
    }

    addChildToSuper(shield);
  }
Esempio n. 2
0
  static {
    try {
      SimpleStream ss = new SimpleStream("data/crate.3ds");
      bluePrint = new ClientObject(Loader.load3DS(ss.getStream(), 0.09f)[0]);
      ss.close();
      bluePrint.translate(0, -4.3f, -5);
      bluePrint.translateMesh();
      bluePrint.getTranslationMatrix().setIdentity();
      TextureInfo ti = null;
      if (!Globals.normalMapping) {
        bluePrint.setTexture("crate");
      } else {
        ti = new TextureInfo(TextureManager.getInstance().getTextureID("crate"));
        ti.add(
            TextureManager.getInstance().getTextureID("cratenormals"), TextureInfo.MODE_MODULATE);
        bluePrint.setTexture(ti);
      }

      bluePrint.getMesh().compress();
      bluePrint.build();

      Object3D child = null;
      Animation anim = new Animation(6);
      anim.setClampingMode(Animation.USE_CLAMPING);
      anim.createSubSequence("explosion");

      Loader.setVertexOptimization(false);

      for (int i = 1; i < 6; i++) {
        String name = "data/crate" + i + ".3ds";
        ss = new SimpleStream(name);
        Object3D obj = Loader.load3DS(ss.getStream(), 4.4050189f)[0];
        ss.close();
        obj.translate(0, -5f, -0.6f);
        obj.translateMesh();
        obj.getTranslationMatrix().setIdentity();
        if (!Globals.normalMapping) {
          obj.setTexture("crate");
        } else {
          obj.setTexture(ti);
        }
        obj.build();

        if (i == 1) {
          child = obj;
          child.setMesh(child.getMesh().cloneMesh(true));
        }
        obj.getMesh().compress();
        anim.addKeyFrame(obj.getMesh());
      }
      child.setAnimationSequence(anim);

      Loader.setVertexOptimization(true);

      childBluePrint = child;

      /*
       * for (int i=0; i<child.getMesh().getBoundingBox().length; i++) {
       * System.out.print(child.getMesh().getBoundingBox()[i]);
       * System.out.println("="+bluePrint.getMesh().getBoundingBox()[i]);
       * }
       */
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private void drawRoad(int program) {
    Matrix.setIdentityM(mMMatrix, 0);

    Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0); // View
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); // Proj

    // send to the shader
    GLES20.glUniformMatrix4fv(
        GLES20.glGetUniformLocation(program, "uMVPMatrix"), 1, false, mMVPMatrix, 0);

    // Create the normal modelview matrix
    // Invert + transpose of mvpmatrix
    Matrix.invertM(normalMatrix, 0, mMVPMatrix, 0);
    Matrix.transposeM(normalMatrix, 0, normalMatrix, 0);

    // send to the shader
    GLES20.glUniformMatrix4fv(
        GLES20.glGetUniformLocation(program, "normalMatrix"), 1, false, mMVPMatrix, 0);

    /** * DRAWING OBJECT * */
    // Get buffers from mesh
    Object3D ob = this._objects[this.ROAD];
    Mesh mesh = ob.getMesh();
    FloatBuffer _vb = mesh.get_vb();
    ShortBuffer _ib = mesh.get_ib();

    short[] _indices = mesh.get_indices();

    /*float[] myVertices = {
    	-20, 0, -20,
    	-20f, 0, 20,
    	20, 0, -20,
    	20, 0, 20
    };

    _vb = ByteBuffer.allocateDirect(myVertices.length
    		* FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
    _vb.put(myVertices);*/

    // Vertex buffer

    // the vertex coordinates
    _vb.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "aPosition"),
        3,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aPosition"));

    // the normal info
    _vb.position(TRIANGLE_VERTICES_DATA_NOR_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "aNormal"),
        3,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aNormal"));

    // Texture info

    // bind textures
    if (ob.hasTexture()) { // && enableTexture) {
      // number of textures
      int[] texIDs = ob.get_texID();

      for (int i = 0; i < _texIDs.length; i++) {
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
        // Log.d("TEXTURE BIND: ", i + " " + texIDs[i]);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIDs[i]);
        GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "texture" + (i + 1)), i);
      }
    }

    // enable texturing? [fix - sending float is waste]
    GLES20.glUniform1f(
        GLES20.glGetUniformLocation(program, "hasTexture") /*shader.hasTextureHandle*/,
        ob.hasTexture() && enableTexture ? 2.0f : 0.0f);

    // texture coordinates
    _vb.position(TRIANGLE_VERTICES_DATA_TEX_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "textureCoord") /*shader.maTextureHandle*/,
        2,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(
        GLES20.glGetAttribLocation(
            program, "textureCoord")); // GLES20.glEnableVertexAttribArray(shader.maTextureHandle);

    // Draw with indices
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, _indices.length, GLES20.GL_UNSIGNED_SHORT, _ib);
    checkGlError("glDrawElements");

    /** END DRAWING OBJECT ** */
  }
  private void drawCar(int program, float[] startPos) {
    Matrix.setIdentityM(mMMatrix, 0);
    Matrix.setIdentityM(mTransMatrix, 0);

    // Képernyõ Y tengely
    if (Math.abs(mDY) < 2) mDY = 0;

    if (Math.abs(mDY) <= 250) {
      accelY = -1 * mDY * 2;
    } else {
      if (mDY < 0) mDY = -250f;
      if (mDY > 0) mDY = 250f;
    }
    distanceY += accelY;
    Log.d("mDY:", String.valueOf(mDY));

    // Képernyõ X tengely
    if (Math.abs(mDX) < 2) mDX = 0;
    float[] forward = {(float) Math.cos(mDX / 50), 0.0f, (float) Math.sin(mDX / 50)};

    // lookAt
    lookAt[0] = eyePos[0] + 25 * forward[0];
    lookAt[1] = eyePos[1] + 25 * forward[1];
    lookAt[2] = eyePos[2] + 25 * forward[2];

    // eyePos
    eyePos[0] = 0.0f + distanceY / 1000 * forward[0];
    eyePos[2] = 25.0f + distanceY / 1000 * forward[2];

    Matrix.setLookAtM(
        mVMatrix, 0, eyePos[0], eyePos[1], eyePos[2], lookAt[0], lookAt[1], lookAt[2], 0.0f, 1.0f,
        0.0f);

    // Kormányzás
    startPos[0] = eyePos[0] + 5 * forward[0];
    startPos[2] = eyePos[2] + 5 * forward[2];

    Matrix.translateM(mTransMatrix, 0, startPos[0], startPos[1], startPos[2]);

    Matrix.multiplyMM(mMMatrix, 0, mMMatrix, 0, mTransMatrix, 0); // Translate
    Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0); // View
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); // Proj

    // send to the shader
    GLES20.glUniformMatrix4fv(
        GLES20.glGetUniformLocation(program, "uMVPMatrix"), 1, false, mMVPMatrix, 0);

    // Create the normal modelview matrix
    // Invert + transpose of mvpmatrix
    Matrix.invertM(normalMatrix, 0, mMVPMatrix, 0);
    Matrix.transposeM(normalMatrix, 0, normalMatrix, 0);

    // send to the shader
    GLES20.glUniformMatrix4fv(
        GLES20.glGetUniformLocation(program, "normalMatrix"), 1, false, mMVPMatrix, 0);

    /** * DRAWING OBJECT * */
    // Get buffers from mesh
    Object3D ob = this._objects[this.CUBE];
    Mesh mesh = ob.getMesh();
    FloatBuffer _vb = mesh.get_vb();
    ShortBuffer _ib = mesh.get_ib();

    short[] _indices = mesh.get_indices();

    // Vertex buffer

    // the vertex coordinates
    _vb.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "aPosition"),
        3,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aPosition"));

    // the normal info
    _vb.position(TRIANGLE_VERTICES_DATA_NOR_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "aNormal"),
        3,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aNormal"));

    // Texture info

    // bind textures
    if (ob.hasTexture()) { // && enableTexture) {
      // number of textures
      int[] texIDs = ob.get_texID();

      for (int i = 0; i < _texIDs.length; i++) {
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
        // Log.d("TEXTURE BIND: ", i + " " + texIDs[i]);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIDs[i]);
        GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "texture" + (i + 1)), i);
      }
    }

    // enable texturing? [fix - sending float is waste]
    GLES20.glUniform1f(
        GLES20.glGetUniformLocation(program, "hasTexture") /*shader.hasTextureHandle*/,
        ob.hasTexture() && enableTexture ? 2.0f : 0.0f);

    // texture coordinates
    _vb.position(TRIANGLE_VERTICES_DATA_TEX_OFFSET);
    GLES20.glVertexAttribPointer(
        GLES20.glGetAttribLocation(program, "textureCoord") /*shader.maTextureHandle*/,
        2,
        GLES20.GL_FLOAT,
        false,
        TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
        _vb);
    GLES20.glEnableVertexAttribArray(
        GLES20.glGetAttribLocation(
            program, "textureCoord")); // GLES20.glEnableVertexAttribArray(shader.maTextureHandle);

    // Draw with indices
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, _indices.length, GLES20.GL_UNSIGNED_SHORT, _ib);
    checkGlError("glDrawElements");

    /** END DRAWING OBJECT ** */
  }