2010-09-08 36 views
0

我在iPhone應用程序中渲染背景紋理時出現問題。下圖顯示的問題:iPhone中的背景紋理圖像在OpenGL ES中產生變形

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

再一次,對不起,這很長,但希望它提供了足夠的人,可以看到我搞砸了!非常感謝!

Tom

+0

這兩個鏈接都被打破。 – genpfault 2010-09-08 17:19:51

+0

謝謝 - 原來他們只在我登錄時纔可見。我修復了這些鏈接。 – Tojo 2010-09-08 17:41:13

回答

0

看起來像色彩量化。您視圖的kEAGLDrawablePropertyColorFormat屬性是否設置爲kEAGLColorFormatRGB565?如果是這樣,請查看是否將其更改爲kEAGLColorFormatRGBA8可解決您的問題。

+0

謝謝,Pivot,但kEAGLDrawablePropertyColorFormat屬性實際上是kEAGLColorFormatRGBA8。如果它有幫助 - 我不會在iPhone模擬器上看到這個問題,只是在設備上(但我測試過的所有設備,包括3G,3GS,iPad) – Tojo 2010-09-09 15:49:37

+0

您是如何將PNG解碼爲數據指針的?你交給glTexImage2D? – Pivot 2010-09-09 16:43:07

+0

我在他們的一些例子中使用Apple提供的Texture2D類。我在繪圖部分略微修改了它,但是,我只是通過initWithImage:(UIImage *)方法創建了紋理。它基本上根據其像素格式爲圖像分配足夠的空間,並創建CGBitmapContext並將UIImage繪製到其上。 – Tojo 2010-09-09 17:55:44