在iPhone中使用OpenGL ES绘制直线?

最后,我试图在XCode 4.2中使用OpenGL ES框架为iPhone简单的游戏app.I研究了一些关于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正在调用无限时间。 请指导我在哪里做错了。 为什么行不出现或绘图? 你能帮忙吗? 我正在尝试这2天。 提前致谢。

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

除此之外,Apple模板代码将包括创build一个GLKBaseEffect,它提供了一些Shaderfunction,这些function似乎是为了能够使用OpenGL ES 2.0进行绘制而需要的。 (如果没有GLKBaseEffect,则需要使用GLSL,该模板提供了具有和不具有显式GLSL着色器代码的示例。)

该模板创build了一个“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 } 

顺便说一句,我已经通过了Erik Buck的iOS学习OpenGL ES ,你可以通过O'Reilly购买“Rough Cut”表单(这是本书的早期版本,因为它不会完全公布到最后的年份)。 这本书在这个阶段有相当多的错别字,没有图片,但是我仍然觉得它很有用。 这本书的源代码似乎很远,你可以在他的博客上抓取。 作者还写了优秀的书cocoadevise模式。