iphone - OpenGL ES 不再显示对象

标签 iphone ios opengl-es-2.0 glkit

我是 OpenGL ES 编程新手。我找到了一个很好的教程来开始,并且该项目显示了一个正方形。然后,我尝试将大部分 OpenGL 代码从 View Controller 移至模型对象,该对象将存储顶点、颜色等,然后​​在 View Controller 中调用:

[model update];
[model load];

这样,如果我有多个模型要显示,会更方便练习。自从我这样做以来,立方体不再显示在屏幕上。我想我指出了所有模型 View 矩阵计算发生的更新方法的错误,因为当我注释掉此方法时,立方体会显示,但它只是填满屏幕。

-(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);
}

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

这里并没有真正的“错误”。问题是您并没有真正设置投影和模型 View 矩阵,以便它很好地构建您的模型。没有什么神奇的解决方案。您应该考虑正在绘制的模型中的顶点(它们是否达到 100?1000?只有 0.5?)并为 GLKMatrix4MakePerspective 选择适当的设置,将它们直接放置在屏幕中。

作为捷径(因为您说过,如果没有平移和旋转,它会填充整个屏幕),您可以用缩放命令替换这些转换,以缩小对象的大小。

类似于:

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

作为一般建议,请尝试单独考虑每个命令的效果。如果您正在寻找投影/变换概念的介绍,我找到了此链接:http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-3:-3D-transformation-and-projection.html跳到名为投影和世界空间的部分。

关于iphone - OpenGL ES 不再显示对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8964431/

相关文章:

android - 无法创建应用程序,Phonegap 中的文件类型无效

opengl-es - ETC1 纹理的容器格式

iphone - 在图像上创建一个图层并通过触摸调整其坐标

ios - 从小部件扩展访问核心数据

ios - 如何找到 contentView 的 x 坐标等于 UIScrollView 框架的中心 x

ios - 将部分纹理( Sprite 表/纹理贴图)应用于 iOS OpenGL ES 2.0 中的点 Sprite

ios - 修改在 OpenGL ES 2.0 中使用的纹理贴图的一部分

ios - iOS 上的 SIGTRAP 错误 – AutoreleasePoolPage::busted

iphone - 是否可以在 iPhone 上播放视频并显示与其同步的字幕?

IOS/Objective-C : App crashes with core data - pressing the save button