Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glDrawArrays not drawing correctly

I made a painting program. Everything works as I expected. But while drawing, sometimes some strange things happen.

I run app, and press left mouse button on image. It should draw point from 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 draws the point at coordinates (0,0). After that every thing works OK. See image:

mouse click

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. Image:

mouse drag

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

enter image description here

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


Mouse down:

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

        [self drawing];
}

Mouse dragged:

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

        [self drawing];
}

Drawing:

- (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);
}
like image 300
hockeyman Avatar asked Jul 05 '12 12:07

hockeyman


2 Answers

You are setting your count variable (the one used in glDrawArrays) to [vertices count] * 2, which seems strange.

The last argument to glDrawArrays is the number of vertices to draw, whereas in your code it seems you are setting it to double the number (maybe you thought it's the number of floats?), which means you are just drawing rubbish after the first count vertices.

like image 144
Christian Rau Avatar answered Oct 19 '22 10:10

Christian Rau


The fact the vertices aren't rendered in the exact location you clicked on should be a hint the problem is you've not properly determined the hit point within the view.

Your code has:

location = [self convertPoint: [event locationInWindow] fromView: self];

which tells the view to convert the point from its coordinates (self) to the same view's coordinates (self), even though the point is actually relative to the window.

To convert the point from the window's coordinates to the view, change that line to the following:

location = [self convertPoint: [event locationInWindow] fromView: nil];
like image 35
Huperniketes Avatar answered Oct 19 '22 11:10

Huperniketes