我正在开发一款2D游戏,它使用本机代码绘制成单个OpenGL 2D纹理。然后在屏幕上绘制纹理不变。除旋转显示外,代码工作正常。游戏继续正常播放,但纹理在屏幕上被截断并偏离中心。如果以任何方向开始游戏,游戏都会正确播放,但如果方向改变,则会出现绘画问题。我正在正确处理方向更改,以便不破坏GL上下文并正确计算每个方向的绘图参数。我唯一能想到的是,我可能在处理onSurfaceChanged()事件时错过了一些步骤。这是由onSurfaceChanged()调用的本机代码:
if (pTexture)
free(pTexture);
pTexture = malloc(iTexturePitch * iTextureHeight);
rect[0] = 0;
rect[1] = iHeight; // w,h fit within the texture size
rect[2] = iWidth;
rect[3] = -iHeight;
glDeleteTextures(1, &s_texture);
while (*start)
glDisable(*start++); // disable unused options
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &s_texture);
glBindTexture(GL_TEXTURE_2D, s_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glShadeModel(GL_FLAT);
glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, rect);
显示纹理的代码:
glClear(GL_COLOR_BUFFER_BIT);
glTexImage2D(GL_TEXTURE_2D, /* target */
0, /* level */
GL_RGB, /* internal format */
iTextureWidth, /* width */
iTextureHeight, /* height */
0, /* border */
GL_RGB, /* format */
GL_UNSIGNED_SHORT_5_6_5,/* type */
pTexture); /* pixels */
// border x/y for centering; stretched x/y is the destination size on the display
glDrawTexiOES(iBorderX, iBorderY, 0, iStretchedWidth, iStretchedHeight);