2014-03-13 36 views
0

我正在使用GLKit在OpenGL ES 2.0中创建一个矩形。这是工作,我可以用纯色和渐变填充矩形。GLKit,在渲染时崩溃

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

我在glDrawElements行上得到EXC_BAD_ACCESS code = 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];

//内部 - (无效)glkView:(GLKView *)视图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; 

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

有人能指出我在这里失踪或做错吗?

回答

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

然后向溶液中明显以包括顶点数据结构内的数据,然后给偏移像这样:

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

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