Exemple #1
0
 public static float[] calcNormalMatrix(float matrix[]) {
   float[] tmp = new float[32];
   for (int i = 0; i < 15; ++i) {
     if (i % 4 != 3 && i < 12) {
       tmp[i] = matrix[i];
     } else {
       tmp[i] = 0.0f;
     }
   }
   tmp[15] = 1.0f;
   Matrix.invertM(tmp, 16, tmp, 0);
   Matrix.transposeM(tmp, 0, tmp, 0);
   float[] ret = new float[9];
   for (int i = 0; i < 9; ++i) {
     ret[i] = tmp[i + i / 3];
   }
   return ret;
 }
Exemple #2
0
  public void draw(float[] mvMatrix, float[] pMatrix) {
    float[] normalMatrix = new float[16];
    float[] tmpMatrix = new float[16];
    Matrix.invertM(tmpMatrix, 0, mvMatrix, 0);
    Matrix.transposeM(normalMatrix, 0, tmpMatrix, 0);

    // Add program to OpenGL ES environment
    GLES20.glUseProgram(shaderProgram);

    // vertex position attribute array
    int mPositionHandle = GLES20.glGetAttribLocation(shaderProgram, "vPosition");
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    GLES20.glVertexAttribPointer(
        mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

    // vertex normal attribute array
    int mNormalHandle = GLES20.glGetAttribLocation(shaderProgram, "vNormal");
    GLES20.glEnableVertexAttribArray(mNormalHandle);
    GLES20.glVertexAttribPointer(
        mNormalHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, normalBuffer);

    // vertex UV attribute array
    int mUVHandle = GLES20.glGetAttribLocation(shaderProgram, "vTexCoord");
    GLES20.glEnableVertexAttribArray(mUVHandle);
    GLES20.glVertexAttribPointer(mUVHandle, 2, GLES20.GL_FLOAT, false, 8, uvBuffer);

    // material data
    int mColorHandle = GLES20.glGetUniformLocation(shaderProgram, "Color");
    int mAmbientHandle = GLES20.glGetUniformLocation(shaderProgram, "Ambient");
    int mDiffuseHandle = GLES20.glGetUniformLocation(shaderProgram, "Diffuse");
    int mSpecularHandle = GLES20.glGetUniformLocation(shaderProgram, "Specular");
    int mShininessHandle = GLES20.glGetUniformLocation(shaderProgram, "Shininess");
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);
    GLES20.glUniform3fv(mAmbientHandle, 1, ambient, 0);
    GLES20.glUniform3fv(mDiffuseHandle, 1, diffuse, 0);
    GLES20.glUniform3fv(mSpecularHandle, 1, specular, 0);
    GLES20.glUniform1f(mShininessHandle, shininess);

    // transformation matrices
    int mMVMatrixHandle = GLES20.glGetUniformLocation(shaderProgram, "MVMatrix");
    int mPMatrixHandle = GLES20.glGetUniformLocation(shaderProgram, "PMatrix");
    int mNormalMatrixHandle = GLES20.glGetUniformLocation(shaderProgram, "NormalMatrix");
    GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mvMatrix, 0);
    GLES20.glUniformMatrix4fv(mPMatrixHandle, 1, false, pMatrix, 0);
    GLES20.glUniformMatrix4fv(mNormalMatrixHandle, 1, false, normalMatrix, 0);

    // lighting data
    int mLightPosHandle = GLES20.glGetUniformLocation(shaderProgram, "LightPos");
    int mLightColorHandle = GLES20.glGetUniformLocation(shaderProgram, "LightColor");
    GLES20.glUniform4fv(mLightPosHandle, 1, lightPosition, 0);
    GLES20.glUniform3fv(mLightColorHandle, 1, lightColor, 0);

    // texture
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
    int mTexHandle = GLES20.glGetUniformLocation(shaderProgram, "Tex");
    GLES20.glUniform1i(mTexHandle, 0);

    // Draw the object
    GLES20.glDrawElements(
        GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    // Disable arrays
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(mNormalHandle);
    GLES20.glDisableVertexAttribArray(mUVHandle);
  }
  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 ** */
  }