OpenGL ES不再显示对象

时间:2012-01-22 20:30:56

标签: iphone ios opengl-es-2.0 glkit

我是OpenGL ES编程的新手。我发现了一个很好的教程,项目显示了一个正方形。然后,我试图将最多的OpenGL代码从View Controller中移动到一个Model对象,该对象将存储顶点,颜色等,然后​​在View Controller中调用:

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

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

1 个答案:

答案 0 :(得分:1)

这里确实没有'错误'。问题是你并没有真正设置投影和模型视图矩阵,以便很好地构建你的模型。没有神奇的解决方案。你应该考虑你正在绘制的模型中的顶点(它们是否达到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请跳至名为Projection and world space的部分。