iOS OpenGLES2.0绘图点大小正确

时间:2013-01-03 17:19:46

标签: ios opengl-es

使用下面的代码我是从不同大小的三角形条带绘制线条。在最后三角形的位置,我想添加一个GLpoint原语,看起来这条线有一个圆形末端。如何根据最终三角形的大小计算GLpoint的正确直径?请参阅随附的图片,展示我此刻所拥有的东西(这一点太大了)。

enter image description here

  - (void)pan:(UIPanGestureRecognizer *)p {

        CGPoint v = [p velocityInView:self.view];
        CGPoint l = [p locationInView:self.view];

       float distance = sqrtf((l.x - previousPoint.x) * (l.x - previousPoint.x) + (l.y - previousPoint.y) * (l.y - previousPoint.y));

        float velocityMagnitude = sqrtf(v.x*v.x + v.y*v.y);
        float clampedVelocityMagnitude = clamp(VELOCITY_CLAMP_MIN, VELOCITY_CLAMP_MAX, velocityMagnitude);
        float normalizedVelocity = (clampedVelocityMagnitude - VELOCITY_CLAMP_MIN) / (VELOCITY_CLAMP_MAX - VELOCITY_CLAMP_MIN);

        float lowPassFilterAlpha = STROKE_WIDTH_SMOOTHING;
        float newThickness = (STROKE_WIDTH_MAX - STROKE_WIDTH_MIN) * normalizedVelocity + STROKE_WIDTH_MIN;
        penThickness = penThickness * lowPassFilterAlpha + newThickness * (1 - lowPassFilterAlpha);

        glBindVertexArrayOES(vertexArrayTriangles);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferTriangles);

        if ([p state] == UIGestureRecognizerStateBegan)
        {
            previousPoint = l;
            previousMidPoint = l;

            NISignaturePoint startPoint = {
                {    (l.x / self.view.bounds.size.width * 2. - 1), ((l.y / self.view.bounds.size.height) * 2.0 - 1) * -1, 0}, {0,0,0}
            };
            previousVertex = startPoint;
            previousThickness = penThickness;

            addVertexTriangles(&lengthTriangles, startPoint);
            addVertexTriangles(&lengthTriangles, previousVertex);

        } else if ([p state] == UIGestureRecognizerStateChanged) {

            CGPoint mid = CGPointMake((l.x + previousPoint.x) / 2.0, (l.y + previousPoint.y) / 2.0);

            if (distance > QUADRATIC_DISTANCE_TOLERANCE) {
                // Plot quadratic bezier instead of line
                unsigned int i;

                int segments = (int) distance / 1.5;

                float startPenThickness = previousThickness;
                float endPenThickness = penThickness;
                previousThickness = penThickness;

                for (i = 0; i < segments; i++)
                {
                    penThickness = startPenThickness + ((endPenThickness - startPenThickness) / segments) * i;
                    double t = (double)i / (double)segments;
                    double a = pow((1.0 - t), 2.0);
                    double b = 2.0 * t * (1.0 - t);
                    double c = pow(t, 2.0);
                    double x = a * previousMidPoint.x + b * previousPoint.x + c * mid.x;
                    double y = a * previousMidPoint.y + b * previousPoint.y + c * mid.y;

                    NISignaturePoint v = {
                        {
                            (x / self.view.bounds.size.width * 2. - 1),
                            ((y / self.view.bounds.size.height) * 2.0 - 1) * -1,
                            0
                        },
                        strokeColor
                    };

                    [self addTriangleStripPointsForPrevious:previousVertex next:v];

                    previousVertex = v;
                }
            }        
            previousPoint = l;
            previousMidPoint = mid;

        }
        else if (p.state == UIGestureRecognizerStateEnded | p.state == UIGestureRecognizerStateCancelled)
        {
            addVertexTriangles(&lengthTriangles, previousVertex);
            addVertexTriangles(&lengthTriangles, previousVertex);

            glBindVertexArrayOES(vertexArrayPoints);
            glBindBuffer(GL_ARRAY_BUFFER, vertexBufferPoints);


            NISignaturePoint startPoint = {
                {previousVertex.vertex.x, previousVertex.vertex.y, 0}, strokeColor, ???
            };

            addVertexPoints(&lengthPoints, startPoint);


            penThickness = STROKE_WIDTH_MIN;
            previousThickness = penThickness;
        }
    }



    - (void)addTriangleStripPointsForPrevious:(NISignaturePoint)previous next:(NISignaturePoint)next {
        float toTravel = penThickness / 2.0;

        //NSLog(@"add tri");

        for (int i = 0; i < 2; i++) {
            GLKVector3 p = perpendicular(previous, next);
            GLKVector3 p1 = next.vertex;
            GLKVector3 ref = GLKVector3Add(p1, p);

            float distance = GLKVector3Distance(p1, ref);
            float difX = p1.x - ref.x;
            float difY = p1.y - ref.y;
            float ratio = -1.0 * (toTravel / distance);

            difX = difX * ratio;
            difY = difY * ratio;

            NISignaturePoint stripPoint = {
                { p1.x + difX, p1.y + difY, 0.0 },
                strokeColor
            };
            addVertexTriangles(&lengthTriangles, stripPoint);

            toTravel *= -1;
        }
    }

1 个答案:

答案 0 :(得分:0)

enter image description here

您可以包括UIGestureRecognizerStateChangedUIGestureRecognizerStateEndedUIGestureRecognizerStateCancelled作为一种情况来绘制线条,但随后检测后两种情况以添加直径等于{{的结束点1}}

0.5+endPenThickness