0
votes

I am considering porting an iPhone project from core animation to OpenGL-ES.

I need to render a button that is constructed from CGPathRef s.

But it seems that GL has no provision for Bezier Curves.

Can anyone provide some code that renders a Bezier Curve in GL?

1

1 Answers

2
votes

This will accept a series of points to draw a rounded bezier line. It must use point sprites. If you send it a line of three points, and a number of point sprites to draw, it will create a bezeir line. The code is based of something I found somewhere, but I cannot remember where.

It requires:

CGPoint origin - First Point CGPoint control - Mid Point CGPoint destination - End Point int segments - Number of points to render.

To calculate the number of points, I use:

count = MAX(ceilf(sqrtf(([[currentStroke objectAtIndex:i+2] CGPointValue].x - [[currentStroke objectAtIndex:i] CGPointValue].x) 
                                    * ([[currentStroke objectAtIndex:i+2] CGPointValue].x - [[currentStroke objectAtIndex:i] CGPointValue].x) 
                                    + ((invertedYThirdCoord - invertedYBegCoord) * (invertedYThirdCoord - invertedYBegCoord))) / 2), 1)*4;

Anyway, the code (in C++):

CGPoint vertices[segments];
    CGPoint midPoint;



    float x, y;

        float t = 0.0;
        for(int i = 0; i < (segments); i++)
        {
            x = pow(1 - t, 2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x;
            y = pow(1 - t, 2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y;
            vertices[i] = CGPointMake(x, y);
            t += 1.0 / (segments);

        }
        midPoint = CGPointMake(x, 288 - y);
        glVertexPointer(2, GL_FLOAT, 0, vertices);
        glDrawArrays(GL_POINTS, 0, segments);

Following this render as normal.