简体   繁体   English

在iPhone中使用OpenGL ES画一条直线?

[英]Draw a straight line using OpenGL ES in iPhone?

Finally i tried to draw a line using OpenGL ES framework in XCode 4.2 for iPhone simple game app.I studied something about GLKView and GLKViewController to draw a line in iPhone. 最后,我试图在XCode 4.2 for iPhone简单游戏应用程序中使用OpenGL ES框架画一条线。我研究了有关GLKView和GLKViewController的一些知识以在iPhone上画一条线。 Here is my sample code that was i tried in my project, 这是我在项目中尝试过的示例代码,

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

When i run the project only the gray color only appearing in the screen, the line not showing. 当我运行该项目时,仅灰色出现在屏幕上,该行不显示。 And also the - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect delegate is calling infinite time. 而且- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect委托正在调用无限时间。 Please guide me where i am doing wrong. 请指导我我做错了什么。 Why the line not appearing or drawing? 为什么线条不显示或绘图? Can you please help? 你能帮忙吗? Am trying this 2 days. 我正在尝试这2天。 Thanks in advance. 提前致谢。

I'm rather a student of OpenGL ES 2.0 right now myself. 我本人现在是OpenGL ES 2.0的学生。 I recommend first starting a new project in Xcode with the "OpenGL Game" template Apple provides. 我建议首先使用Apple提供的“ OpenGL Game”模板在Xcode中启动一个新项目。

Among other things, the Apple template code will include creation of a GLKBaseEffect, which provides some Shader functionality that seems to be required in order to be able to draw with OpenGL ES 2.0. 除其他外,Apple模板代码将包括创建GLKBaseEffect,它提供了一些Shader功能,这些功能似乎是必需的,以便能够使用OpenGL ES 2.0进行绘制。 (Without the GLKBaseEffect, you would need to use GLSL. The template provides an example of both with and without explicit GLSL Shader code.) (如果没有GLKBaseEffect,则需要使用GLSL。该模板提供了带有或不带有显式GLSL Shader代码的示例。)

The template creates a "setupGL" function, which I modified to look like this: 该模板创建了一个“ 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
}

I was able to get the line to draw by including a few more steps. 我可以通过增加一些步骤来画线。 It all involves sending data over to the GPU to be processed. 所有这些都涉及将数据发送到GPU进行处理。 Here's my glkView:drawInRect function: 这是我的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 

}

Btw, I've been going through Learning OpenGL ES for iOS by Erik Buck , which you can buy in "Rough Cut" form through O'Reilly (an early form of the book since it is not going to be fully published until the end of the year). 顺便说一句,我一直在学习Erik Buck的《学习iOS的OpenGL ES》 ,您可以通过O'Reilly以“ Rough Cut”的形式购买(这是本书的早期版本,因为直到最后才完全出版)的一年)。 The book has a fair number of typos at this stage and no pictures, but I've still found it quite useful. 这本书在这个阶段有很多错别字,没有图片,但是我仍然发现它很有用。 The source code for the book seems to be very far along, which you can grab at his blog . 这本书的源代码似乎相去甚远,您可以在他的博客上获取 The author also wrote the excellent book Cocoa Design Patterns. 作者还撰写了出色的著作《可可设计模式》。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM