2012-03-16 96 views
2

最后我想提请使用OpenGL ES框架线的XCode 4.2的iPhone简单的游戏app.I研究一些关于GLKView和GLKViewController绘制在iPhone上线。下面是这是我在我的项目试过我的示例代码,在iPhone中使用OpenGL ES绘制直线?

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

当我运行该项目,只有灰色的颜色只出现在屏幕上,行不显示。此外,代表正在呼叫无限的时间。请指导我在哪里做错了。为什么行不出现或绘图?你能帮忙吗?我试了2天。提前致谢。

回答

5

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

除此之外,苹果模板代码将包括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,您可以在“粗切”的形式通过奥赖利(书的早期形式购买,因为它是不会直到年底才能完整发布)。这本书在这个阶段有相当数量的拼写错误,没有图片,但我仍然认为它非常有用。这本书的源代码似乎很遥远,你可以在his blog处获得。作者还撰写了优秀的可可设计模式书。

+0

Mr.Chris非常感谢您的帮助很大。这个答案我等了7天。现在我在你的帮助下画一条线。我希望你的帮助在将来。再次感谢。 – Gopinath 2012-03-23 12:22:30

+0

你好先生,我需要你的帮助。你能告诉我们如何分配点A和点B的值吗?提前致谢。 – Gopinath 2012-03-24 04:42:10

+0

我不确定我了解你的问题。你在问如何设置A点和B点的值? – 2012-03-26 07:01:53