PDA

View Full Version : glDrawArrays draws unneeded points



hockeyman
07-09-2012, 12:23 AM
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:


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:
http://i.stack.imgur.com/3dCCE.png

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:
http://i.stack.imgur.com/cMKxM.png

When I continue drag it disappears. After some dragging it appears again and disappears again. And so on. See image:
http://i.stack.imgur.com/UNKaZ.png

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

hockeyman
07-09-2012, 01:51 AM
Found solution. Drawing function was incorrect. I used
count incorrectly. It has to look like this:


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