Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: drawing GL_POINTS with mouse. Spacings? [brush stroke]

  1. #1
    Intern Newbie
    Join Date
    Jul 2012
    Posts
    41

    drawing GL_POINTS with mouse. Spacings? [brush stroke]

    So I have made paint app. And I want to fix one problem with my brush stroke. When I paint with mouse slowly - everything is ok. When I do it faster, there are some spacings. Its because not all mouse coordinates are registered then. But how can I fix it? Is there ant solution? I'm using vertex array with mouse locations. Here's code:

    Code :
        int count = [vertices count] * 2;
        int sum = [vertices count];
    //    NSLog(@"count: %d", count);
        int currIndex = 0;
        GLfloat *GLVertices = (GLfloat *)malloc(count * sizeof(GLfloat));
        for (NSValue *locationValue in vertices) {
            CGPoint loc = locationValue.pointValue;
            GLVertices[currIndex++] = loc.x;
            GLVertices[currIndex++] = loc.y;    
        }
     
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnable(GL_BLEND);
        glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, brushTexture);
        glPointSize(50);
        glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
        glEnable(GL_POINT_SMOOTH);
        glVertexPointer(2, GL_FLOAT, 0, GLVertices);
        glDrawArrays(GL_POINTS, 0, sum);
        glDisable(GL_POINT_SMOOTH);
        glDisable(GL_BLEND);
        glDisableClientState(GL_VERTEX_ARRAY);

    And mouse location registering code:


    Code :
    - (void) mouseDragged:(NSEvent *)event
    {
        location = [self convertPoint: [event locationInWindow] fromView:self];
        NSValue *locationValue = [NSValue valueWithPoint:location];
        [vertices addObject:locationValue];
     
            [self drawArray];
    }
     
     
    - (void) mouseDown:(NSEvent *)event
    {
        location = [self convertPoint: [event locationInWindow] fromView:self];
        NSValue *locationValue = [NSValue valueWithPoint:location];
        [vertices addObject:locationValue];
     
            [self drawArray];
    }


    And my app image:


  2. #2
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,290
    Easiest way: if the distance between A and B (start and end position) is 50.3 pixels , draw 50+1 points. Each being slightly offset, the last one matching the end position.

    Harder way, supporting only circular brushes: draw a quad or hexagon, with a shader. On each fragment, find the nearest point on the [A;B] line-segment (not infinite line). The transparency of that fragment is proportionate to that distance and size of the brush.

  3. #3
    Intern Newbie
    Join Date
    Jul 2012
    Posts
    41
    Thank you for answer This solution sounds nice, but I don't really get it. It would be easy if I do it manually. I mean, describe few points and draw them (static way). But how to do it dynamically? I mean that distance would be calculated at every mouse movement? I think you know what I mean. How to do that it would calculate distances from vertex array?

  4. #4
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,290
    Distance between current mouse position and previous mouse position. sqrt(dx*dx + dy*dy)

  5. #5
    Intern Newbie
    Join Date
    Jul 2012
    Posts
    41
    Thank you. I'm trying now to insert it to my code and make it working

  6. #6
    Intern Newbie
    Join Date
    Jul 2012
    Posts
    41
    I tried to implement it, but it wasn't successful. Maby you could suggest me any solution? When I count distance between points, i need to specify point's indexes. Sounds ok, but my app redraws vertex array at every new mouse location, so I need somehow to do, so that if distance between two points, who goes one after another, is bigger than x, then add points between them. I think you understood me, but i will explain it more clearly.

    So lets say I have 4 points in my GLfloat (vertex array).
    1st point: (x1,y1)
    2nd point: (x2,y2)
    3rd point: (x3,y3)
    4th point: (x4,y4)

    In my vertex array they look like this:
    Code :
    GLVertices[1] = x1
    GLVertices[2] = y1
    GLVertices[3] = x2
    GLVertices[4] = y2
    GLVertices[5] = x3
    GLVertices[6] = y3
    GLVertices[7] = x4
    GLVertices[8] = y4

    And I need to count distance between them. Normally it should look like this:
    Code :
    int distBetween1and2 = sqrt(GLVertices[1]*GLVertices[3] + GLVertices[2]*GLVertices[4]);
    int distBetween2and3 = sqrt(GLVertices[3]*GLVertices[5] + GLVertices[4]*GLVertices[6]);
    int distBetween3and4 = sqrt(GLVertices[5]*GLVertices[7] + GLVertices[6]*GLVertices[8]);

    But I cannot do this, because I don't know how many vertices will be in float and what indexes they will have.

    Lets go on.

    I should check them if distance is larger than something like this:

    Code :
    if (distBetween1and2 > 20) {
    what the hell should be here?
    }
    if (distBetween2and3 > 20) {
    what the hell should be here?
    }
    if (distBetween3and4 > 20) {
    what the hell should be here?
    }

    And what should I insert to "what the hell should be here?" place?

    Thanks in advance

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •