在iPhone中使用OpenGL ES绘制直线? [英] Draw a straight line using OpenGL ES in iPhone?

查看:135
本文介绍了在iPhone中使用OpenGL ES绘制直线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后,我试图在XCode 4.2 for iPhone简单游戏应用程序中使用OpenGL ES框架绘制一条线。我研究了一些关于GLKView和GLKViewController在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. 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);
}

当我运行项目时,只有灰色显示在屏幕上,线没有显示。还有 - (void)glkView:(GLKView *)视图drawInRect:(CGRect)rect 委托正在调用无限时间。请指导我在哪里做错了。为什么线条没有出现或绘图?你能帮忙吗?我试过这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. Please guide me where i am doing wrong. Why the line not appearing or drawing? Can you please help? Am trying this 2 days. Thanks in advance.

推荐答案

我现在就是OpenGL ES 2.0的学生。我建议首先使用Apple提供的OpenGL Game模板在Xcode中开始一个新项目。

I'm rather a student of OpenGL ES 2.0 right now myself. I recommend first starting a new project in Xcode with the "OpenGL Game" template Apple provides.

除其他外,Apple模板代码将包括GLKBaseEffect的创建,它提供了一些似乎需要的Shader功能才能使用OpenGL ES进行绘制2.0。 (如果没有GLKBaseEffect,则需要使用GLSL。模板提供了有和没有显式GLSL着色器代码的示例。)

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. (Without the GLKBaseEffect, you would need to use GLSL. The template provides an example of both with and without explicit GLSL Shader code.)

模板创建setupGL函数,我修改为这样:

The template creates a "setupGL" function, which I modified to look like this:

- (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函数:

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. Here's my glkView:drawInRect function:

- (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 

}

顺便说一下,我一直在经历< a href =http://my.safaribooksonline.com/book/animation-and-3d/9780132478939> Erik Buck为iOS学习OpenGL ES ,您可以通过O以粗剪形式购买'Reilly(这本书的早期版本,因为直到今年年底它才会完全发布)。这本书在这个阶段有很多错别字,没有图片,但我仍然发现它很有用。这本书的源代码似乎很远,你可以在他的博客上获取。作者还撰写了优秀的书籍Cocoa Design Patterns。

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). 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.

这篇关于在iPhone中使用OpenGL ES绘制直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆