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 テンプレートを使用しています...
次のレンダー関数を使用して、2 番目の 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];
[/コード]
更新: glEnableClientState(GL_VERTEX_ARRAY); を追加しました。立方体の描画部分を囲んでいます...これにより、アクセスエラーが解消されましたが、立方体もスプライトも描画されていません(もちろん、コード部分を完全にコメントアウトしない限り)...
UPDATE2: したがって、問題は明らかに、glDrawArray を呼び出してキューブを描画した後です (これは正常に動作します)。次のように glDrawArray を再度呼び出します。しかし、どういうわけか、まだ前の配列をレンダリングしようとしていますか? // 1 self.effect.texture2d0.name = self.textureInfo.name; self.effect.texture2d0.enabled = はい;
// 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);