OpenGL-ES中的CGPathRef / Bezier曲线

时间:2011-02-20 01:59:04

标签: opengl-es bezier nsbezierpath

我正在考虑将核心动画中的iPhone项目移植到OpenGL-ES。

我需要渲染一个由CGPathRef构建的按钮。

但似乎GL没有Bezier Curves的规定。

任何人都可以提供一些在GL中呈现Bezier曲线的代码吗?

1 个答案:

答案 0 :(得分:2)

这将接受一系列点来绘制圆形贝塞尔线。它必须使用点精灵。如果你发送一个三点的线,并绘制一些点精灵,它将创建一个bezeir线。代码基于我在某处找到的东西,但我不记得在哪里。

需要:

CGPoint起源 - 第一点 CGPoint控制 - 中点 CGPoint目的地 - 终点 int segments - 要渲染的点数。

要计算点数,我使用:

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;

无论如何,代码(用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);

正常渲染此渲染。

相关问题