iphone - 在 iPhone 中使用 OpenGL ES 画直线?

标签 iphone ios line opengl-es-2.0 draw

最后我尝试在 XCode 4.2 for iPhone 简单游戏应用程序中使用 OpenGL ES 框架画一条线。我研究了一些关于 GLKView 和 GLKViewController 在 iPhone 中画线的知识。这是我在项目中尝试过的示例代码,

@synthesize context = _context;
@synthesize effect = _effect;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!self.context) {
        NSLog(@"Failed to create ES context");
    }


    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
    [EAGLContext setCurrentContext:self.context];

    //[self setupGL];
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    NSLog(@"DrawInRect Method");

    [EAGLContext setCurrentContext:self.context];

    // This method is calling multiple times....

    glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


   const GLfloat line[] = 
   {
        -0.5f, -0.5f, //point A 
        0.5f, -0.5f, //point B  
   };
   glVertexPointer(2, GL_FLOAT, 0, line);
   glEnableClientState(GL_VERTEX_ARRAY);
   glDrawArrays(GL_LINES, 0, 2);
}

当我运行该项目时,仅灰色仅出现在屏幕中,该行未显示。而且 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 委托(delegate)正在无限次调用。请指导我哪里做错了。为什么线条没有出现或绘制?你能帮忙吗?我正在尝试这两天。提前致谢。

最佳答案

我自己现在也是 OpenGL ES 2.0 的学生。我建议首先使用 Apple 提供的“OpenGL Game”模板在 Xcode 中启动一个新项目。

除此之外,Apple 模板代码将包括创建 GLKBaseEffect,它提供了一些着色器功能,这些功能似乎是使用 OpenGL ES 2.0 进行绘制所必需的。 (如果没有 GLKBaseEffect,您将需要使用 GLSL。该模板提供了带有和不带有显式 GLSL 着色器代码的示例。)

该模板创建了一个“setupGL”函数,我将其修改为如下所示:

- (void)setupGL
{
    [EAGLContext setCurrentContext:self.context];

    self.effect = [[[GLKBaseEffect alloc] init] autorelease];

    // Let's color the line
    self.effect.useConstantColor = GL_TRUE;

    // Make the line a cyan color
    self.effect.constantColor = GLKVector4Make(
        0.0f, // Red
        1.0f, // Green
        1.0f, // Blue
        1.0f);// Alpha
}

我能够通过添加更多步骤来绘制线条。这一切都涉及将数据发送到 GPU 进行处理。这是我的 glkView:drawInRect 函数:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Prepare the effect for rendering 
    [self.effect prepareToDraw];

    const GLfloat line[] = 
    {
        -1.0f, -1.5f, //point A 
        1.5f, -1.0f, //point B  
    };

    // Create an handle for a buffer object array
    GLuint bufferObjectNameArray; 

    // Have OpenGL generate a buffer name and store it in the buffer object array 
    glGenBuffers(1, &bufferObjectNameArray); 

    // Bind the buffer object array to the GL_ARRAY_BUFFER target buffer  
    glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray); 

    // Send the line data over to the target buffer in GPU RAM
    glBufferData(
        GL_ARRAY_BUFFER,   // the target buffer 
        sizeof(line),      // the number of bytes to put into the buffer
        line,              // a pointer to the data being copied 
        GL_STATIC_DRAW);   // the usage pattern of the data 

    // Enable vertex data to be fed down the graphics pipeline to be drawn
    glEnableVertexAttribArray(GLKVertexAttribPosition); 

    // Specify how the GPU looks up the data 
    glVertexAttribPointer(
        GLKVertexAttribPosition, // the currently bound buffer holds the data 
        2,                       // number of coordinates per vertex 
        GL_FLOAT,                // the data type of each component 
        GL_FALSE,                // can the data be scaled 
        2*4,                     // how many bytes per vertex (2 floats per vertex)
        NULL);                   // offset to the first coordinate, in this case 0 

    glDrawArrays(GL_LINES, 0, 2); // render 

}

顺便说一句,我一直在经历Learning OpenGL ES for iOS by Erik Buck ,您可以通过 O'Reilly 购买“粗剪”形式(这本书的早期形式,因为它要到年底才会完全出版)。这本书在这个阶段有相当多的错别字,而且没有图片,但我仍然发现它非常有用。这本书的源代码似乎很长,你可以在 his blog 获取。 。作者还写了一本优秀的书《Cocoa Design Patterns》。

关于iphone - 在 iPhone 中使用 OpenGL ES 画直线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9736887/

相关文章:

iphone - 如何在实现 UITableViewDataSource 协议(protocol)的类中重新加载数据

iphone - 从 iPhone 向外部数据库插入新行

ios - 在 imageView 数组中设置图像?

ios - Objective-C 中的高性能缓冲区

javascript - native 脚本;在命令行上安装 npm 问题

java - Java中如何获取在线音量

iphone - 有哪些很棒的 iPhone 练习题?

iphone - CoreData - 关系的一侧始终为零

ios - 我只得到字符串解析 XML 的第二部分

html - CSS HTML 行出现在 div :active 上