-2

每当我向顶点数组添加顶点时,我的应用程序就会崩溃。

这是代码:

const GLKVector3 Vertices[] = {


//8,9,0
LASH_BOTTOM_LEFT,
LASH_BOTTOM_RIGHT,
RECT_BOTTOM_L,


//9,1,0
LASH_BOTTOM_RIGHT,
RECT_BOTTOM_R,
RECT_BOTTOM_L,


//2,6,7
RECT_TOP_R,
LASH_TOP_RIGHT,
LASH_TOP_LEFT,

//3,2,7
RECT_TOP_L,
RECT_TOP_R,
LASH_TOP_LEFT,

//0,1,3
RECT_BOTTOM_L,
RECT_BOTTOM_R,
RECT_TOP_L,

//1,2,3
RECT_BOTTOM_R,
RECT_TOP_R,
RECT_TOP_L,

//2,5,3
RECT_TOP_R,
BACK_RIGHT,
RECT_TOP_L,

//3,5,4
RECT_TOP_L,
BACK_RIGHT,
BACK_LEFT,


//0,1,5
RECT_BOTTOM_L,
RECT_BOTTOM_R,
BACK_RIGHT,

//5,4,0
BACK_RIGHT,
BACK_LEFT,
RECT_BOTTOM_L,


};

设置它:

- (void)setupGL {


    [EAGLContext setCurrentContext:self.myContext];

    self.effect = [[GLKBaseEffect alloc] init];
    self.layer.contentsScale = 2.0;

    // Create default framebuffer object.
    glGenFramebuffers(1, &defaultFrameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFrameBuffer);


    ViewToMap *view = [[ViewToMap alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
    view.layer.shadowOffset = CGSizeMake(1, 1);
    view.layer.masksToBounds = NO;
    view.layer.shadowRadius = 1;
    view.layer.shadowOpacity = 0.02;

    GLfloat coordToPixScale = 1.0;//[UIScreen mainScreen].scale;


    /*********************
     MAPPING UIVIEW ONTO THE FACE
     ****************/

    self.effect.texture2d0.enabled = true;



    // make space for an RGBA image of the view
    GLubyte *pixelBuffer = (GLubyte *)malloc(
                                             4 *
                                             view.bounds.size.width * coordToPixScale *
                                             view.bounds.size.height * coordToPixScale);

    // create a suitable CoreGraphics context
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context =
    CGBitmapContextCreate(pixelBuffer,
                          view.bounds.size.width*coordToPixScale, view.bounds.size.height*coordToPixScale,
                          8, 4*view.bounds.size.width *coordToPixScale,
                          colourSpace,
                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colourSpace);

    // draw the view to the buffer
    [view.layer renderInContext:context];

    // upload to OpenGL
    glTexImage2D(GL_TEXTURE_2D, 0,
                 GL_RGBA,
                 view.bounds.size.width * coordToPixScale, view.bounds.size.height * coordToPixScale, 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);

    BOOL repeatX = NO;
    BOOL repeatY = NO;

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeatX ? GL_REPEAT
                    : GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeatY ? GL_REPEAT
                    : GL_CLAMP_TO_EDGE);



    glGenBuffers(1, &texArray);
    glBindBuffer(GL_ARRAY_BUFFER, texArray);
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glBufferData(GL_ARRAY_BUFFER, sizeof(TexCoords), TexCoords, GL_STATIC_DRAW);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0,0);

    /**************************
     ******************************************/

    // clean up
    CGContextRelease(context);
    free(pixelBuffer);

    glGenRenderbuffers(1, &depthBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, view.bounds.size.width * coordToPixScale, view.bounds.size.height * coordToPixScale);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

    glEnable(GL_DEPTH_TEST);

    glGenBuffers(1, &vertexArray);
    glBindBuffer(GL_ARRAY_BUFFER, vertexArray);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(GLKVertexAttribPosition,3,GL_FLOAT,GL_FALSE,0,0);



    self.effect.transform.projectionMatrix = GLKMatrix4MakePerspective(45.0f,0.9f, 0.01f, 1.0f);

    rotMatrix = GLKMatrix4Translate(self.effect.transform.modelviewMatrix,0,0,-2);
    self.effect.transform.projectionMatrix =          GLKMatrix4Translate(self.effect.transform.projectionMatrix, 0, 0, 1.2);


}

如果我再添加 1 个顶点,一切似乎都正常,但如果我添加另一个顶点,它会崩溃(不管它们的坐标如何,我认为顶点坐标完全正常)。

我得到的是一个糟糕的访问错误。

绘制调用:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {


    self.opaque = NO;

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    [self.effect prepareToDraw];

    glDrawArrays(GL_TRIANGLES, 0, sizeof(Vertices));
}
4

1 回答 1

1

好的,我对此有预感,但在你的情况下,驱动程序有错误(我可以解释崩溃,即程序突然终止)只有一个有错误的驱动程序。

不过,添加顶点位置会破坏您的代码也就不足为奇了。顶点是位置、纹理坐标和其他属性的整个元组。因此,当您添加职位时,您还必须添加其他属性。如果您使用的是客户端顶点数组,这将解释崩溃,但 VBO 应该抓住这一点。

无论如何,你有那些电话(注释它们)。

glBufferData(GL_ARRAY_BUFFER, buf_size_t = sizeof(TexCoords), TexCoords, GL_STATIC_DRAW);
glVertexAttribPointer(GLKVertexAttribTexCoord0, attrib_elements_t = 2, GL_FLOAT, GL_FALSE, 0,0);

glBufferData(GL_ARRAY_BUFFER, buf_size_p = sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glVertexAttribPointer(GLKVertexAttribPosition, attrib_elements_p = 3, GL_FLOAT, GL_FALSE, 0, 0);

您必须确保

buf_size_t / (attrib_elements_t * sizeof(GLfloat)) 

    ==

buf_size_p / (attrib_elements_p * sizeof(GLfloat))

如果不满足这种平等,事情就会以某种形式破裂。

由于问题编辑而更新

最后是问题根源的原因:

 glDrawArrays(GL_TRIANGLES, 0, sizeof(Vertices));
                               ^^^^^^
                                     \--- This

glDrawArrays 期望绘制的顶点数(而不是顶点数组缓冲区的大小)。

运算符不会告诉您数组中的sizeof顶点数,而是它的大小(以char. sizeof(Vertices) / ( sizeof(GLfloat) * attrib_elements_p )在你的情况下,你必须用,替换它attrib_elements_p = 3

于 2013-01-06T14:46:57.703 回答