1

我正在尝试将图像作为纹理加载到 GLSurfaceView 上。我只能看到椒盐噪声和图像。像这样的东西。图像在 raw 文件夹中存储为 32X32 .bmp。着色器编译成功。文件被正确读取。无法理解我做错了什么。我正在研究 OpenGLES2.0。FloatBuffers 在这个 onDraw 方法之前被初始化。

  GLES20.glEnable(GLES20.GL_TEXTURE_2D);

  aVerPos = GLES20.glGetAttribLocation(mProgram, "aVerPos");
  if(aVerPos == -1) {
        Log.e("Shader program", "Cudn't find aVerPos");
    } else {
        Log.v("Shader program", "Found vPosition @ " + aVerPos);
    }
    GLES20.glEnableVertexAttribArray(aVerPos);

    aVerCol = GLES20.glGetAttribLocation(mProgram, "aVerCol");
    if(aVerCol == -1) {
        Log.e("Error", "Couldn't find aVColor");
    } else {
        Log.v("Success", "aVColor is at  " + aVerCol + " :-3");
    }
    GLES20.glEnableVertexAttribArray(aVerCol);

    aTexPos = GLES20.glGetAttribLocation(mProgram, "aTexPos");
    if(aTexPos == -1) {
        Log.e("Error", "Failed 2 find aTexPos");
    } else {
        Log.v("Succeed", "Succesfully located aTexPos @ " + aTexPos);
    }
    GLES20.glEnableVertexAttribArray(aTexPos);
    uSamp = GLES20.glGetUniformLocation(mProgram, "uSampler");

    if(uSamp == -1) {
        Log.e("Error", "Couldn't finda uSampler " + uSamp);
    } else {
        Log.v("Succeed", "uSampler is @ " + uSamp + " :3");
    }

    //Texture Property of the Image
    textureBitmap = BitmapFactory.decodeResource(mycontext.getResources(),  R.drawable.ic_launcher);

    File root = Environment.getExternalStorageDirectory();
    File imgfile = new File(root.getAbsolutePath()+"/abc.bmp");
  //  textureBitmap = BitmapFactory.decodeFile(imgfile.getAbsolutePath());
    Log.v("Bitmap inafo:", textureBitmap.getWidth() + ", " + textureBitmap.getHeight());
    Log.i("File path",imgfile.getAbsolutePath());


    int[] buffer = new int[1];
    GLES20.glGenTextures(1, buffer, 0);
    texture = buffer[0];
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
    Log.v("check inafo:", "1");
    GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLES20.GL_TRUE);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, textureBitmap, GLES20.GL_UNSIGNED_BYTE, 0);
    Log.v("check inafo:", "2");
    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_REPEAT);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
   /* GLES20.glUseProgram(mProgram);   */
    Log.v("check inafo:", "3");
    // The Draw part of Image               

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
    GLES20.glUniform1i(uSamp, 0);

    GLES20.glVertexAttribPointer(aVerPos, 3, GLES20.GL_FLOAT, false, 0, Img.texVertexBufferpointer);        
    GLES20.glVertexAttribPointer(aVerCol, 3, GLES20.GL_FLOAT, false, 0, Img.texColBufferPointer);       
    GLES20.glVertexAttribPointer(aTexPos, 2, GLES20.GL_FLOAT, false, 0, Img.textureBufferPointer);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    Log.v("check inafo:", "4");
    GLES20.glDisableVertexAttribArray(aVerPos);
    GLES20.glDisableVertexAttribArray(aVerCol);
    GLES20.glDisableVertexAttribArray(aTexPos);
4

1 回答 1

1

你可以尝试改变:

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, textureBitmap, GLES20.GL_UNSIGNED_BYTE, 0);

至:

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, textureBitmap, GLES20.GL_UNSIGNED_BYTE, 0);

Bitmap类将其像素存储为 int - 每个红色、绿色、蓝色和 alpha 1 个字节。所以我认为你的 textureBitmap 的外部格式是 RGBA。从http://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml

internalformat 必须匹配格式。纹理图像处理期间不支持格式之间的转换。type 可以用作指定所需精度的提示,但 GL 实现可以选择以它选择的任何内部分辨率存储纹理数组。

因此,如果你想使用 RGB,你必须要么转换它,要么不使用 BitmapFactory 和 Bitmap 类来加载你的图像。
这可能是问题所在。另外,如果那是您在此处显示的绘图代码,我希望您考虑不要在每次绘图时都加载图像 - 而是只加载一次。

希望这可以帮助!

于 2013-07-02T11:55:11.497 回答