簡體   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