繁体   English   中英

iPhone中的背景纹理图像在OpenGL ES中扭曲绘制

[英]Background Texture Image in iPhone Drawn with Distortion in OpenGL ES

我在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);

再次,很抱歉,这很长,但是希望它能提供足够的空间,以便有人可以看到我的混乱之处!!! 非常感谢!

汤姆

看起来像色彩量化。 视图的kEAGLDrawablePropertyColorFormat属性是否设置为kEAGLColorFormatRGB565 如果是这样,请查看将其更改为kEAGLColorFormatRGBA8可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM