我在我的 iPhone 应用程序中渲染背景纹理时遇到问题。下图显示了问题:
http://www.tojamgames.com/wp-content/uploads/2010/09/background_layer1.png
tojamgames.com/wp-content/uploads/2010/09/IMG_0246.png (无法将其设为链接,抱歉)
第一个是正确的图像,第二个是用 OpenGL ES 绘制的(是的,它更小,我在照片编辑器中手动裁剪它以删除一些游戏 UI 界面。你可以理解不过,失真效果很明显)。显然,渲染纹理上的扭曲线并不理想。这是我设置 opengl、加载纹理和渲染图像的代码 - 任何帮助都会很棒!抱歉,这很长,但我正在尝试提供尽可能多的信息:-P
OpenGL初始化:
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glShadeModel(GL_FLAT);
glDisable(GL_DITHER);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, screenBounds.size.width, 0, screenBounds.size.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND_SRC);
glEnableClientState(GL_VERTEX_ARRAY);
glDisable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
纹理加载:
glGenTextures(1, &_name);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &saveName);
glBindTexture(GL_TEXTURE_2D, _name);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
绘图场景
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glViewport(0, 0, screenBounds.size.width , screenBounds.size.height);
// texture images have pre-multiplied alpha, so use this blend function
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
[backgroundImage drawAtPoint:CGPointMake(0,0)];
// more game-specific stuff you don't need to see :-P
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
最后,绘制图像本身(在 drawAtPoint 方法调用中)
// grab texture coordinates - maxS & maxT are the width & height in the POT texture
// that contain our NPOT image
GLfloat coordinates[] = { 0, 0,
_maxS, 0,
0, _maxT,
_maxS, _maxT };
GLfloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
// adjust the vertices of the quad so they're contained within the bounding rect of the
// image in the game, subtracting from screenHeight to invert the OpenGL coordinate system
GLfloat vertices[] = { rect.origin.x, screenHeight - rect.origin.y, 0.0,
rect.origin.x + rect.size.width, screenHeight - rect.origin.y, 0.0,
rect.origin.x, screenHeight - (rect.origin.y + rect.size.height), 0.0,
rect.origin.x + rect.size.width, screenHeight - (rect.origin.y + rect.size.height), 0.0 };
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glEnable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
再次,对不起,这很长,但希望它提供了足够的东西,有人可以看到我搞砸了!太感谢了!
汤姆