每当我向顶点数组添加顶点时,我的应用程序就会崩溃。
这是代码:
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));
}