每当我将顶点添加到顶点数组时,我的应用程序崩溃了。
以下是代码:
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));
}
答案 0 :(得分: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
。