hockeyman
hockeyman

Reputation: 1183

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);
}

Upvotes: 8

Views: 3801

Answers (3)

Huperniketes
Huperniketes

Reputation: 970

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];

Upvotes: 1

Christian Rau
Christian Rau

Reputation: 45948

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.

Upvotes: 7

Anton
Anton

Reputation: 6033

The arguments to glDrawArrays are defined as (GLenum mode, GLint first, GLsizei count).

The second arguments defines the first index of the vertex attributes used when drawing. You're passing 1 as the first index which makes your vertex coordinates unmatch. I assume that you want 0 there.

http://www.opengl.org/sdk/docs/man/xhtml/glDrawArrays.xml

Upvotes: 0

Related Questions