1

所以,我一直在做一个使用 Android 的 OpenGL 库等的项目,我遇到了一个症结所在。部分原因是我缺乏 OpenGL 经验,部分原因是计划好旧的过度工作问题。问题是这样的:我有两个位图,我需要将它们作为纹理叠加到一个简单的 2D 正方形中(使用链式三角形)。

现在,我知道将两个纹理绑定到 GL10.GL_TEXTURE_2D 只会把事情弄得一团糟(IE,不起作用),所以我正在寻找一种方法来处理这个问题。现在我加载纹理如下:

 public void loadGLTexture(GL10 gl, Context context) {
    // loading texture
    Bitmap bitmap;
    Bitmap corn;
    boolean lastBlack = false;
    try{
        FileInputStream fis = context.openFileInput(TEMP_FILE);
        BufferedInputStream buffer = new BufferedInputStream(fis);

        Bitmap immutableBitmap = BitmapFactory.decodeStream(buffer);

        if(!IsPowerOfTwo(immutableBitmap.getWidth()) | !IsPowerOfTwo(immutableBitmap.getHeight())){
            int nearestPowerWidth = pow2(immutableBitmap.getWidth());
            int nearestPowerHeight = pow2(immutableBitmap.getHeight());


            bitmap = Bitmap.createScaledBitmap(immutableBitmap,nearestPowerWidth,nearestPowerHeight,false);
        } else{
            bitmap = immutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
        }

        int [] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
        Bitmap filterer = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        filterer.getPixels(pixels, 0, filterer.getWidth(), 0, 0, filterer.getWidth(), filterer.getHeight());
        //int stepper = 0;

        for( int x = 0; x < pixels.length; x++ ) {
            //Log.v("MEE",String.format("%d",pixels[x]));
            if(pixels[x] == Color.WHITE){

                pixels[x] = Color.BLACK;
                lastBlack = true;
            } else{
                /*if(lastBlack){
                    pixels[x] = Color.rgb(128,153,0);
                    lastBlack = false;
                } else{
                    pixels[x] = Color.rgb(128,153,0);
                }*/
                pixels[x] = Color.TRANSPARENT;


            }
        }
        bitmap = Bitmap.createBitmap( pixels, filterer.getWidth(), filterer.getHeight(), Bitmap.Config.ARGB_8888 );
         corn = BitmapFactory.decodeResource(context.getResources(),R.drawable.corn);
    }catch(Exception e){
        Log.e("Crop Object", e.toString());
         corn = BitmapFactory.decodeResource(context.getResources(),R.drawable.corn);
         bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.corn);
    }




        // generate one texture pointer

        gl.glGenTextures(2, textures,0);

        //Create  Texture and bind it to texture 0
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, corn, 0);



}

然后它们被绘制:

public void draw(GL10 gl) {
    // bind the previously generated texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    //gl.glBindTexture(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE1);
    // Point to our buffers
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // Set the face rotation
    gl.glFrontFace(GL10.GL_CW);

    // Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

}

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

4

0 回答 0