(Android)如何使用GLSurfaceView绘制背面纹理?

时间:2012-04-17 15:29:39

标签: android opengl-es textures

我正在实施PDF阅读器,我正在使用harism的页面卷曲API(https://github.com/harism/android_page_curl)来实现书籍/杂志效果。我需要并排显示页面而不是反向翻译。到目前为止,我设法绘制背面纹理。但是,它绘制得并不正确...只有一半的图像被渲染,如下面的快照中所示:

http://imageshack.us/photo/my-images/594/device20120410175255.png/

我的OpenGL知识有限,我不知道我做错了什么...我使用下面的代码绘制背面纹理(在CurlMesh类的draw()方法上):

// First allocate texture if there is not one yet.
if (DRAW_TEXTURE && mTextureIds == null) 
{
    // Generate textures.
    mTextureIds = new int[2];
    gl.glGenTextures(2, mTextureIds, 0);

    // Set textures attributes.
    for(int i = 0; i < mTextureIds.length ; i++)
    {
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[i]);
        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);
        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);
    }
}

// If mBitmap != null we have a new texture.
if (DRAW_TEXTURE && mBitmap != null) 
{
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[0]);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
    mBitmap.recycle();
}

if (DRAW_TEXTURE) 
{
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[0]);
}

// Some 'global' settings.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// TODO: Drop shadow drawing is done temporarily here to hide some
// problems with its calculation.
if (DRAW_SHADOW)
{
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, mShadowColors);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mShadowVertices);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mDropShadowCount);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDisable(GL10.GL_BLEND);
}

// Enable texture coordinates.
if (DRAW_TEXTURE) 
{
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexCoords);
}
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertices);

// Enable color array.
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColors);

// Draw blank / 'white' front facing vertices.
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVerticesCountFront);

// Draw front facing texture.
// TODO: Decide whether it's really needed to have alpha blending for
// front facing texture. If not, GL_BLEND isn't needed, possibly
// increasing performance. The heck, is it needed at all?
if (DRAW_TEXTURE) 
{
    gl.glEnable(GL10.GL_BLEND);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVerticesCountFront);
    gl.glDisable(GL10.GL_TEXTURE_2D);
    gl.glDisable(GL10.GL_BLEND);
}

if (DRAW_TEXTURE && mBitmapBack != null) 
{
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[1]);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmapBack, 0);
    mBitmapBack = null;
}

if (DRAW_TEXTURE)
{
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[1]);
}

int backStartIdx = Math.max(0, mVerticesCountFront - 2);
int backCount = mVerticesCountFront + mVerticesCountBack - backStartIdx;

// Draw blank / 'white' front facing vertices.
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, backStartIdx, backCount);
// Draw back facing texture.
if (DRAW_TEXTURE) 
{
    gl.glEnable(GL10.GL_BLEND);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, backStartIdx, backCount);
    gl.glDisable(GL10.GL_TEXTURE_2D);
    gl.glDisable(GL10.GL_BLEND);
} 

如果有人能告诉我我做错了什么我会很感激...这是我项目中唯一遗漏的东西:(

此致 标记

0 个答案:

没有答案