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 模板...

我在第二个 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); 
4

1 回答 1

0

愚蠢的我。只要我这样做就可以工作:

glBindBuffer(GL_ARRAY_BUFFER, 0);

清除缓冲区。我一直在寻找 glUnbind 的东西...忘记了 openGL 对内存引用的工作... 2D texutre 现在正在应用于立方体,但我相信这是一个简单的修复:)....大家的帮助> :o

纹理问题已修复:)

于 2012-04-21T15:40:27.757 回答