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 2 of 2

Thread: glDrawArrays draws unneeded points

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

    glDrawArrays draws unneeded points (SOLVED)

    I have made little paint program witch draws points at mouse location. Mouse location is saved in vertex array and drawn by glDrawArrays.
    Everything looks to be working, but i am meeting some problems while drawing.

    Drawing is being done with code:
    Code :
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, brushTextura);
    glPointSize(100);
    glVertexPointer(2, GL_FLOAT, 0,GLVertices);
    glDrawArrays(GL_POINTS, 0, count);
    glDisableClientState(GL_VERTEX_ARRAY);

    At point where I press mouseDown registers mouseDown location, converts it to NSValue, sends to array, and then before drawing I extract NSValue to CGPoint and send it to GLfloat so that it could be drawn by glDrawArrays. But no matter where I click the mouse on the image it first draws the point at coordinates (0,0). After that every thing looks to be working. See image:


    This was first problem. The second problem is that when I paint with it (drag pressed mouse), sometimes points appear where they are not drawn. See image:


    When I continue drag it disappears. After some dragging it appears again and disappears again. And so on. See image:


    Any Ideas why it could happen? I will post code bellow:

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

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

    Drawing:
    Code :
    - (void) drawing {
    int count = [vertices count] * 2;
    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_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, brushTextura);
    glPointSize(100);
    glVertexPointer(2, GL_FLOAT, 0, GLVertices);
    glDrawArrays(GL_POINTS, 0, count);
    glDisableClientState(GL_VERTEX_ARRAY);
    }
    Last edited by hockeyman; 07-09-2012 at 03:01 AM.

  2. #2
    Intern Newbie
    Join Date
    Jul 2012
    Posts
    41
    Found solution. Drawing function was incorrect. I used
    Code :
    count
    incorrectly. It has to look like this:

    Code :
    - (void) drawing {
    int countVer = [vertices count] * 2;
    int count = [vertices count];
    NSLog(@"count: %d", count);
    int currIndex = 0;
    GLfloat *GLVertices = (GLfloat *)malloc(countVer * 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_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, brushTextura);
    glPointSize(100);
    glVertexPointer(2, GL_FLOAT, 0, GLVertices);
    glDrawArrays(GL_POINTS, 0, count);
    glDisableClientState(GL_VERTEX_ARRAY);

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
  •