Android上的OpenGL - 纹理映射 - 屏幕上只显示左上角的像素

时间:2011-11-12 19:47:48

标签: android opengl-es textures

我试图理解纹理映射的概念。我试图将纹理映射到正方形(由2个三角形组成)。但是,只有图像的左上角像素完全呈现在正方形上。为什么会这样?我看到another post有同样的问题..但是这个解决方案对我不起作用

初始设置

    gl.glDisable(GL10.GL_DITHER);               //Disable dithering ( NEW )
    gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping
    gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    //Black Background
    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
    gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do

顶点坐标 -

    private float[] _vertices = {0.0f, 0.0f,0.0f, 1.0f,
                            60.0f, 0.0f, 0.0f, 1.0f,
                            0.0f, 60.0f, 0.0f, 1.0f,
                            60.0f, 60.0f, 0.0f, 1.0f};

纹理坐标 -

    private float _texCoords[] = {0.0f, 1.0f,
    1.0f, 1.0f,
    0.0f, 0.0f,
    1.0f, 0.0f };

加载纹理方法 -

public void loadGLTexture(GL10 gl, Context context) {
    //Get the texture from the Android resource directory
    InputStream is = context.getResources().openRawResource(com.mhoapps.bugswatter.R.drawable.crate);
    Bitmap bitmap = null;
    try {
        //BitmapFactory is an Android graphics utility for images
        bitmap = BitmapFactory.decodeStream(is);
    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    }

    // Generate one texture pointer...
    gl.glGenTextures(1, _textures, 0);
    int mTextureId = _textures[0];

    // ...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);

    // Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
            GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
            GL10.GL_LINEAR);


    // Use the Android GLUtils to specify a two-dimensional texture image
    // from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);       //Clean up


    bitmap.recycle();
}

最后绘制方法

    public void draw(GL10 gl) {
    //Bind the texture according to the set texture filter

    //gl.glLoadIdentity();
    gl.glTranslatef(0.0f, 0.0f, -3.0f);



    gl.glEnable(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, _textures[0]);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(4, GL10.GL_FLOAT, 0, _currentVertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, _bugTexBuffer1);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}

1 个答案:

答案 0 :(得分:1)

您是否指定了纹理坐标?听起来你不是,他们只是默认为(0,0)。另一个选项是纹理矩阵 - 您将其设置为全零吗?