android texture2D导致3d颜色丢失

时间:2011-04-13 10:30:27

标签: android opengl-es colors texture2d

我试图绘制一个3d对象,然后在屏幕上绘制一个2d纹理。使用此函数在onSurfaceCreated(GL10 gl)创建纹理:

public static int loadText(GL10 gl, float width, float height, String text) {
        gl.glGenTextures(1, mTextureNameWorkspace, 0);
        textureName = mTextureNameWorkspace[0];
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_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.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
        bitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_4444);
        canvas = new Canvas(bitmap);
        canvas.drawText(text, width / 2, 50, paint);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        mCropWorkspace[0] = 0;
        mCropWorkspace[1] = bitmap.getHeight();
        mCropWorkspace[2] = bitmap.getWidth();
        mCropWorkspace[3] = -bitmap.getHeight();
        bitmap.recycle();
        ((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, mCropWorkspace, 0);
        return textureName;
    }

问题是如果我在不调用它的情况下绘制框架,则3d对象是橙色但显然未绘制2d纹理。但是如果我在调用它之后绘制框架,那么3d对象变为黑色。用于绘图的代码:

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glDisable(GL10.GL_BLEND);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 500.0f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(4, 2, -10);
        gl.glRotatef(45, 0, 1, 0);
        car.draw(gl);

1 个答案:

答案 0 :(得分:3)

您没有在任何地方启用纹理单元。在进行任何其他与纹理相关的调用之前,请先设置glEnable(GL_TEXTURE_2D)