【问题标题】:GLKit, crash in texture renderingGLKit,纹理渲染崩溃
【发布时间】:2014-03-13 09:12:59
【问题描述】:

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

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

我在 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];

//里面 - (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;

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

有人可以指出我在这里遗漏或做错了什么吗?

【问题讨论】:

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


    【解决方案1】:

    好的,我想通了。错误在于我将纹理坐标数据传递给 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 的新手,所以可能有更好的方法来解决这个问题,但这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2013-12-27
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多