Esempio n. 1
0
  /**
   * Draw the floor.
   *
   * <p>This feeds in data for the floor into the shader. Note that this doesn't feed in data about
   * position of the light, so if we rewrite our code to draw the floor first, the lighting might
   * look strange.
   */
  public void drawFloor(
      float[] modelViewProjection,
      float[] modelFloor,
      float[] modelView,
      float[] lightPosInEyeSpace) {
    GLES30.glUseProgram(mProgramObject);
    floorModelParam = GLES30.glGetUniformLocation(mProgramObject, "u_Model");
    floorModelViewParam = GLES30.glGetUniformLocation(mProgramObject, "u_MVMatrix");
    floorModelViewProjectionParam = GLES30.glGetUniformLocation(mProgramObject, "u_MVP");
    floorLightPosParam = GLES30.glGetUniformLocation(mProgramObject, "u_LightPos");

    floorPositionParam = GLES30.glGetAttribLocation(mProgramObject, "a_Position");
    floorNormalParam = GLES30.glGetAttribLocation(mProgramObject, "a_Normal");
    floorColorParam = GLES30.glGetAttribLocation(mProgramObject, "a_Color");

    GLES30.glEnableVertexAttribArray(floorPositionParam);
    GLES30.glEnableVertexAttribArray(floorNormalParam);
    GLES30.glEnableVertexAttribArray(floorColorParam);

    // Set ModelView, MVP, position, normals, and color.
    GLES30.glUniform3fv(floorLightPosParam, 1, lightPosInEyeSpace, 0);
    GLES30.glUniformMatrix4fv(floorModelParam, 1, false, modelFloor, 0);
    GLES30.glUniformMatrix4fv(floorModelViewParam, 1, false, modelView, 0);
    GLES30.glUniformMatrix4fv(floorModelViewProjectionParam, 1, false, modelViewProjection, 0);
    GLES30.glVertexAttribPointer(
        floorPositionParam, COORDS_PER_VERTEX, GLES30.GL_FLOAT, false, 0, floorVertices);
    GLES30.glVertexAttribPointer(floorNormalParam, 3, GLES30.GL_FLOAT, false, 0, floorNormals);
    GLES30.glVertexAttribPointer(floorColorParam, 4, GLES30.GL_FLOAT, false, 0, floorColors);

    GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, 6);
  }
  public static boolean buildProgram(final GLProgram _program) {
    _program.id[0] = GLES30.glCreateProgram();
    if (_program.id[0] < 1) {
      System.out.println("Failed to create program..");
      return false;
    }

    for (final GLShader shader : _program.shaders) {
      // Attach only successfully compiled shaders
      if (compileShader(shader) == true) {
        GLES30.glAttachShader(_program.id[0], shader.id[0]);
      }
    }

    final List<String> swivel = _program.swivel;
    final int size = swivel.size();

    for (int i = 0; i < size; i++) {
      GLES30.glBindAttribLocation(_program.id[0], _program.inAttributes[i], swivel.get(i));
    }

    GLES30.glLinkProgram(_program.id[0]);

    _program.inMVPMatrix = GLES30.glGetUniformLocation(_program.id[0], "inMVPMatrix");

    // Once all of the shaders have been compiled
    // and linked, we can then detach the shader sources
    // and delete the shaders from memory.
    for (final GLShader shader : _program.shaders) {
      GLES30.glDetachShader(_program.id[0], shader.id[0]);
      GLES30.glDeleteShader(shader.id[0]);
    }
    _program.shaders.clear();

    final int[] response = new int[] {0};
    GLES30.glGetProgramiv(_program.id[0], GLES30.GL_LINK_STATUS, response, 0);
    if (response[0] == GLES30.GL_FALSE) {
      System.out.println("Error linking program: " + GLES30.glGetProgramInfoLog(_program.id[0]));
      return false;
    }

    return true;
  }