2012-04-21 208 views
0

我想在OpenGL ES「2.0」中結合2D和3D,即使在openGL es 1.0和2.0上有很多問題,但我很難解決這個問題。因此,對於2D我要脫離本教程:http://www.raywenderlich.com/9743/how-to-create-a-simple-2d-iphone-game-with-opengl-es-2-0-and-glkit-part-1和3D我正在使用現有的多維數據集旋轉xcode模板...ios opengl es 2.0 2D + 3D組合

我在第二個glDrawArray上獲取EXC_BAD_ACCESS(不知何故,它認爲原始緩衝區仍然適用?無論如何,在繪製2D紋理之前解除此綁定?)錯誤與以下渲染函數。它可以工作,如果我禁用行: glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition,3,GL_FLOAT,GL_FALSE,24,BUFFER_OFFSET(0)); glEnableVertexAttribArray(GLKVertexAttribNormal); glVertexAttribPointer(GLKVertexAttribNormal,3,GL_FLOAT,GL_FALSE,24,BUFFER_OFFSET(12));

發生了什麼事?提前致謝。

[代碼]

glEnable(GL_DEPTH_TEST); 

// glGenVertexArraysOES(1, &_vertexArrayNum); 
// glBindVertexArrayOES(_vertexArrayNum); 

glGenBuffers(1, &_vertexBufferNum); 
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferNum); 
glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW); 

glEnableVertexAttribArray(GLKVertexAttribPosition); 
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0)); 
glEnableVertexAttribArray(GLKVertexAttribNormal); 
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12)); 

glBindVertexArrayOES(0); 

float aspect = fabsf(self.view.bounds.size.width/self.view.bounds.size.height); 
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f); 

self.effect.transform.projectionMatrix = projectionMatrix; 

GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f); 
// baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f); 

// Compute the model view matrix for the object rendered with GLKit 
// GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 1.5f); 
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeRotation(_rotation, 0, 1, 0); 
//modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f); 
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix); 

self.effect.transform.modelviewMatrix = modelViewMatrix; 

glClearColor(0.65f, 0.65f, 0.65f, 1.0f); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

glBindVertexArrayOES(_vertexArrayNum); 

// Render the object with GLKit 
// [self.effect prepareToDraw]; 

// glDrawArrays(GL_TRIANGLES, 0, 36); 

modelViewMatrix = GLKMatrix4MakeScale(2.0f, 2.0f, 2.0f); 
projectionMatrix = GLKMatrix4MakeOrtho(0, 480, 0, 320, -1024, 1048); 
self.effect.transform.projectionMatrix = projectionMatrix; 
self.effect.transform.modelviewMatrix = modelViewMatrix; 

[self.effect prepareToDraw]; 


glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
glEnable(GL_BLEND); 

[self.player render]; 

[/代碼]

UPDATE: 添加glEnableClientState(GL_VERTEX_ARRAY);纏繞在立方體繪圖部分......這擺脫了訪問錯誤的不良,但立方體和精靈都沒有繪製(除非我註釋掉代碼部分)...

UPDATE2: 所以問題顯然是在我調用glDrawArray來繪製多維數據集(這可以正常工作)之後......我再次調用glDrawArray,如下所示。但不知何故,它仍然試圖渲染之前的數組? // 1 self.effect.texture2d0.name = self.textureInfo.name; self.effect.texture2d0.enabled = YES;

// 2  
[self.effect prepareToDraw]; 

// 3 
glEnableVertexAttribArray(GLKVertexAttribPosition); 
glEnableVertexAttribArray(GLKVertexAttribTexCoord0); 

// 4 
long offset = (long)&_quad;   
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex))); 
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex))); 

// 5  
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 

回答

0

愚蠢的我。儘快開始工作:

glBindBuffer(GL_ARRAY_BUFFER, 0); 

清除緩衝區。我一直在尋找glUnbind的東西......忘記了openGL的工作內存引用... 2D texutre現在正在應用於立方體,但我相信這是一個簡單的解決方法:)....你所有的幫助> :o

紋理問題修復:)