Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone OpenGL不再显示对象_Iphone_Ios_Opengl Es 2.0_Glkit - Fatal编程技术网

Iphone OpenGL不再显示对象

Iphone OpenGL不再显示对象,iphone,ios,opengl-es-2.0,glkit,Iphone,Ios,Opengl Es 2.0,Glkit,我是OpenGL ES编程新手。我发现一个很好的教程开始,项目显示了一个正方形。然后,我尝试将大部分OpenGL代码从视图控制器移动到一个模型对象,该模型对象将存储顶点、颜色等,然后在视图控制器中,我将调用: [model update]; [model load]; 这样的话,如果我有几个模型可以展示的话,会更实用。由于我这样做了,立方体不再显示在屏幕上。我想我指出了所有模型视图矩阵计算都发生在更新方法中的错误,因为当我注释掉此方法时,将显示多维数据集,但它只填充屏幕 -(void)upda

我是OpenGL ES编程新手。我发现一个很好的教程开始,项目显示了一个正方形。然后,我尝试将大部分OpenGL代码从视图控制器移动到一个模型对象,该模型对象将存储顶点、颜色等,然后在视图控制器中,我将调用:

[model update];
[model load];
这样的话,如果我有几个模型可以展示的话,会更实用。由于我这样做了,立方体不再显示在屏幕上。我想我指出了所有模型视图矩阵计算都发生在更新方法中的错误,因为当我注释掉此方法时,将显示多维数据集,但它只填充屏幕

-(void)update: (int)width And:(int)height{
float aspect = (width/height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(
                                      GLKMathDegreesToRadians(65.0f), aspect,
                                      4.0f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;

GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 6.0f);   
rotation += 90;
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix,
                                       GLKMathDegreesToRadians(rotation), 0, 0, 1);
self.effect.transform.modelviewMatrix = modelViewMatrix;
}  
下面是加载方法:

- (void)load{
self.effect = [[GLKBaseEffect alloc] init];

NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES],
                          GLKTextureLoaderOriginBottomLeft, 
                          nil];

NSError * error;    
NSString *path = [[NSBundle mainBundle] pathForResource:@"tile_floor" ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
    NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;

glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);

glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);        
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));

glBindVertexArrayOES(0);
}
- (void)render{
[self.effect prepareToDraw];
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

glBindVertexArrayOES(vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}
下面是渲染方法:

- (void)load{
self.effect = [[GLKBaseEffect alloc] init];

NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES],
                          GLKTextureLoaderOriginBottomLeft, 
                          nil];

NSError * error;    
NSString *path = [[NSBundle mainBundle] pathForResource:@"tile_floor" ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
    NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;

glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);

glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);        
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));

glBindVertexArrayOES(0);
}
- (void)render{
[self.effect prepareToDraw];
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

glBindVertexArrayOES(vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}

任何帮助都将不胜感激。谢谢

这里没有真正的“错误”。问题是,您没有真正设置投影矩阵和模型视图矩阵,以便它很好地构建模型。没有神奇的解决办法。你应该考虑你正在绘制的模型中的顶点(它们是否上升到100?1000?仅仅是0.5?),并为GLKMatRIX4MaMax透视选择合适的设置,将它们直接放在屏幕上。

-(void)update: (int)width And:(int)height{
float aspect = (width/height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(
                                      GLKMathDegreesToRadians(65.0f), aspect,
                                      4.0f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;

GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 6.0f);   
rotation += 90;
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix,
                                       GLKMathDegreesToRadians(rotation), 0, 0, 1);
self.effect.transform.modelviewMatrix = modelViewMatrix;
}  
作为一条捷径,(因为您说过,如果没有平移和旋转,它将填充整个屏幕),您可以使用缩放命令替换这些变换,以缩小对象的大小

比如:

GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.1f, 0.1f, 0.1f);   

作为一般建议,试着单独考虑每个命令的效果。如果您想了解投影/转换概念的介绍,我找到了以下链接:跳到“投影和世界空间”部分。

您能发布[model update]和[model load]吗?另外,不要评论整个更新方法,而是找出问题的原因。是翻译吗?轮换?如果两者都没有,无论如何都要注释掉它们,以使诊断更容易。我试着分别注释平移线和旋转线,但没有成功。屏幕仍然是黑色的。但是当我把它们都注释掉时,对象就被画出来了,但是它填充了屏幕,这不是我想要的。上面的方法是update方法。我需要将视图的宽度和高度传递给它,以便它能够正确地创建透视图。谢谢。我认为你是对的。现在我可以让它出现在屏幕上,而无需注释更新方法。