GLKit,在纹理渲染中崩溃

时间:2014-03-13 09:12:59

标签: opengl-es opengl-es-2.0 glkit

我正在使用GLKit在OpenGL ES 2.0中创建一个矩形。这是有效的,我可以用纯色和渐变填充矩形。

现在我试图用图像中的纹理填充它,这对我来说不起作用。

我在glDrawElements行上收到EXC_BAD_ACCESS代码= 1错误。

这就是我现在所拥有的:

//设置GKLView

EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; self.glkView = [[GLKView alloc] initWithFrame:self.bounds context:context]; _glkView.opaque = NO; _glkView.backgroundColor = [UIColor clearColor]; _glkView.delegate = self; [self addSubview:_glkView];

//设置BaseEffect

self.effect = [[GLKBaseEffect alloc] init]; [EAGLContext setCurrentContext:_glkView.context]; GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, self.bounds.size.width, 0, self.bounds.size.height, 0.0, 1.0); self.effect.transform.projectionMatrix = projectionMatrix; GLKMatrix4 modelMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0, 0.0, -0.1); self.effect.transform.modelviewMatrix = modelMatrix;

//加载图片

UIImage* image = ... CGImageRef cgImage = image.CGImage; NSError* error; self.textureInfo = [GLKTextureLoader textureWithCGImage:cgImage options:nil error:&error]; if (error) { NSLog(@"could not load texture"); } if (_textureInfo) { self.effect.texture2d0.envMode = GLKTextureEnvModeReplace; self.effect.texture2d0.target = GLKTextureTarget2D; self.effect.texture2d0.name = _textureInfo.name; } //绑定缓冲区

glGenBuffers(1, &_vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glGenBuffers(1, &_indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer (GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position)); glEnableVertexAttribArray(GLKVertexAttribTexCoord0); glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, _currentData.texCoordinates);

//填充缓冲区

glBufferData(GL_ARRAY_BUFFER, (sizeof(Vertex) * _currentData.numberOfVertices), _currentData.vertices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, (sizeof(GLubyte) * _currentData.numberOfIndices), _currentData.indices, GL_STATIC_DRAW);

[self.effect prepareToDraw]; [self.glkView display];

// Inside - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect; glDrawElements(GL_TRIANGLE_STRIP, (sizeof(GLubyte) * _currentData.numberOfIndices), GL_UNSIGNED_BYTE, 0);

纹理坐标的数据是:

    SimpleVertex* vertices1 = (SimpleVertex *)malloc(sizeof(SimpleVertex) * 4);
    vertices1[0].Position[0] = 0.0;
    vertices1[0].Position[1] = 0.0;

    vertices1[1].Position[0] = 0.0;
    vertices1[1].Position[1] = 1.0;

    vertices1[2].Position[0] = 1.0;
    vertices1[2].Position[1] = 1.0;

    vertices1[3].Position[0] = 1.0;
    vertices1[3].Position[1] = 0.0;

typedef struct
{
    GLfloat Position[2];
}
SimpleVertex;

正如我所说,我确信正确绘制了矩形。我可以通过填充渐变来检查这一点,它看起来很好。纹理加载器也正确加载图像,我可以在调试器中检查它。

有人可以指出我在这里错过或做错了吗?

1 个答案:

答案 0 :(得分:0)

好的我明白了。错误是我将纹理坐标数据传递给openGL的方式。我正在以这种方式传递顶点和颜色的数据:

glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer (GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));

但是对于纹理坐标,我创建了一个单独的结构,并且传递了一个指向该数据的指针,而不是已经绑定的顶点数组的偏移量:

glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, _currentData.texCoordinates);

然后解决方案显然是将数据包含在Vertex数据结构中,然后像这样给出偏移量:

glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));

我仍然是OpenGL ES的新手,所以可能有更好的方法来解决这个问题,但这对我有用。