1

我正在尝试使用 OpenGL 在 Android 上的正方形上渲染 512x512 纹理。当我使用模拟器时,我的程序运行良好,但如果我使用 Nexus 7(旧的),glGetError() 返回 1282。这发生在我在渲染循环中调用 glUseProgram 之后。GLUtils.getEGLErrorString 表示错误 1282 对应于错误 0x502,它代表 GL_INVALID_OPERATION。

我已经尝试过这个答案、这个答案和这篇文章中提到的解决方案。我已经尝试将我的纹理放在 drawable-nodpi 中,并且我还确保我的纹理大小是 2 的幂。我还尝试将我的纹理放入 res/raw 并遵循加载纹理的替代方法,但这也不起作用。

有人能指出我正确的方向吗?

如果有什么我可以做的来改善我的问题,请告诉我!

我的着色器:

      private final String vertexShaderCode =
      // This matrix member variable provides a hook to manipulate
      // the coordinates of the objects that use this vertex shader
      "uniform mat4 uMVPMatrix;" +

      // Per-vertex texture coordinate info we will pass in
      "attribute vec2 a_TexCoordinate;" +
      // This will be passed into the fragment shader
      "varying vec2 v_TexCoordinate;" +

      "attribute vec4 vPosition;" +
      "void main() {" +
      // the matrix must be included as a modifier of gl_Position
      "  gl_Position = vPosition * uMVPMatrix;" +
      "  v_TexCoordinate = a_TexCoordinate;" +      
      "}";


      private final String fragmentShaderCode =
      // The input texture
      "uniform sampler2D u_Texture;" +
      // Interpolated texture coordinate per fragment
      "varying vec2 v_TexCoordinate;" +
      "precision mediump float;" +
      "uniform vec4 vColor;" +
      "void main() {" +
      "  gl_FragColor = (vColor * texture2D(u_Texture, v_TexCoordinate));" +
      "}";

我的抽奖电话:

 public void draw(float[] mvpMatrix) {
    GLES20.glUseProgram(shaderProgramHandle);

    // POSITION INFO - Get a handle to the position attribute in the shader and
    // associate the vertices with it
    int vPositionHandle = GLES20.glGetAttribLocation(shaderProgramHandle, "vPosition");
    GLES20.glEnableVertexAttribArray(vPositionHandle);
    GLES20.glVertexAttribPointer(
        vPositionHandle,
        COORDS_PER_VERTEX,
        GLES20.GL_FLOAT,
        false,
        COORDS_PER_VERTEX * BYTES_PER_COORD, 
        vertexBuffer);

    // COLOR INFO - Get a handle to the color attribute in the shader and associate
    // the color with it
    int vColorHandle = GLES20.glGetUniformLocation(shaderProgramHandle, "vColor");
    GLES20.glUniform4fv(vColorHandle, 1, vertexColor, 0);

    // TRANSFORM INFO - Get a handle to the MVP matrix uniform in the shader and
    // associate the MVP matrix with it
    int uMVPHandle = GLES20.glGetUniformLocation(shaderProgramHandle, "uMVPMatrix");
    GLES20.glUniformMatrix4fv(uMVPHandle, 1, false, mvpMatrix, 0);

    // TEXTURE INFO
    int texCoordinateHandle = GLES20.glGetAttribLocation(shaderProgramHandle, "a_TexCoordinate");
    GLES20.glEnableVertexAttribArray(texCoordinateHandle);
    GLES20.glVertexAttribPointer(
        texCoordinateHandle,
        COORDS_PER_TEX, 
        GLES20.GL_FLOAT,
        false,
        0,
        textureCoordinatesBuffer);
    int texUniformHandle = GLES20.glGetUniformLocation(shaderProgramHandle, "u_Texture");
//     Set the active texture unit to texture unit 0
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
//     Bind the texture to this unit
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureDataHandle);
//     Tell the texture uniform sampler to use this texture in the shader
//     by binding to texture unit 0
    GLES20.glUniform1i(texUniformHandle, 0);

    // Draw the square
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    // Cleanup
    GLES20.glDisableVertexAttribArray(vPositionHandle);
    GLES20.glDisableVertexAttribArray(texCoordinateHandle);
  }

我在 GLSurfaceView.Renderer 的 onSurfaceCreated 实现中加载我的纹理。这是一个片段,显示了我如何加载纹理。

   int loadTexture(final Context context, final int resourceId) {
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);
    Utils.checkGLError("glGenTextures");

    if (textureHandle[0] != 0) {
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inScaled = false; // No pre-scalings
      // Read in the resource
      final Bitmap bitmap = BitmapFactory.decodeResource(
          context.getResources(),
          resourceId,
          options);
//      InputStream is = context.getResources().openRawResource(resourceId);
//      Bitmap bitmap = null;
//      try {
//        bitmap = BitmapFactory.decodeStream(is);
//      } finally {
//        //Always clear and close
//        try {
//          is.close();
//          is = null;
//        } catch (IOException e) {
//        }
//      }
      // Bind to the texture in OpenGL
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

      // Set filtering
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
//      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
//          GLES20.GL_NEAREST);
//      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
//          GLES20.GL_NEAREST);
//      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, 
//          GLES20.GL_CLAMP_TO_EDGE);
//      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, 
//          GLES20.GL_CLAMP_TO_EDGE);
      // Load the bitmap into the bound texture.
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

      // Recycle the bitmap, since its data has been loaded into OpenGL.
      bitmap.recycle();
    }

    if (textureHandle[0] == 0) {
      throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
  }
4

0 回答 0