在Android中将OpenGL纹理分层到2D平面

时间:2012-11-26 17:33:25

标签: android opengl-es

所以,我一直在使用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);

}

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

0 个答案:

没有答案