【问题标题】:Background Texture Image in iPhone Drawn with Distortion in OpenGL ESiPhone中的背景纹理图像在OpenGL ES中使用失真绘制
【发布时间】:2011-04-09 20:16:29
【问题描述】:

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

再次,对不起,这很长,但希望它提供了足够的东西,有人可以看到我搞砸了!非常感谢!

汤姆

【问题讨论】:

  • 谢谢 - 原来只有我在登录时才能看到它们。我修复了链接。

标签: iphone opengl-es textures texture2d


【解决方案1】:

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

【讨论】:

  • 谢谢,Pivot,但 kEAGLDrawablePropertyColorFormat 属性实际上是 kEAGLColorFormatRGBA8。如果有帮助 - 我在 iPhone 模拟器上看不到这个问题,只是在设备上(但我测试过的所有设备,包括 3G、3GS、iPad)
  • 你用什么把你的 PNG 解码成你交给 glTexImage2D 的数据指针?
  • 我在他们的一些示例中使用了 Apple 提供的 Texture2D 类。我在绘图部分稍微修改了它,但是,我只是通过 initWithImage:(UIImage*) 方法创建纹理。它基本上根据图像的像素格式为图像分配足够的空间,并创建一个 CGBitmapContext 并将 UIImage 绘制到它上面。
  • 通过更仔细地查看纹理发现了问题。有问题的图像没有透明度,因此 Texture 2D 代码试图将其解释为 RGB565。继续,只是将所有内容解释为 RGBA8 修复它。问题不是视图,实际上是加载数据指针 - Pivot 让我找到了正确的方向,所以你得到了答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 2014-11-29
相关资源
最近更新 更多