Draw a straight line using OpenGL ES in iPhone?

Finally, I tried to draw a string using the OpenGL ES framework in XCode 4.2 for iPhone. A simple game application. I have learned something about GLKView and GLKViewController to draw a line on iPhone. Here is my example code that 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 appears on the screen only, the line is not displayed. And also the delegate - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect

calls infinite time. Please guide me where I am doing wrong. Why doesn't the line appear or is drawn? Could you help me? I've been trying this for 2 days. Thanks in advance.

+2


source to share


1 answer


I'm pretty much learning OpenGL ES 2.0 right now. I recommend starting a new project in Xcode first using the "OpenGL Game" template that Apple provides.

Among other things, Apple's template code will include the creation of a GLKBaseEffect, which provides some Shader functionality that appears to be necessary in order to be able to draw with OpenGL ES 2.0. (Without GLKBaseEffect, you would need to use GLSL. The template provides an example with or without explicit GLSL Shader code.)

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 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 do by including a few more steps. All of this involves sending data to the GPU being 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 

}

      

Btw, I went through the OpenGL ES training for iOS by Eric Buck , which you can buy in "Rough Cut" form through O'Reilly (early form of the book as it won't be fully published by the end of the year). At this point, the book has quite a few typos and no photographs, but I still found it quite useful. The book's source code seems to be very far away, and you can grab it from your blog . The author also wrote an excellent book, Cocoa Design Patterns.

+5


source







All Articles