GlDrawArrays draws incorrectly

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

Launch the application and left-click on the image. He should draw a point from the 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 the point where I click. mouseDown registers the location of mouseDown, converts it to NSValue, sends it to an array, and then before drawing, I extract the NSValue to CGPoint and send it to GLfloat so that it can be drawn with glDrawArrays. But no matter where I click on the image, it draws a point in coordinates (0,0). After that, everything works fine. See Image:

mouse click

This was the first problem. The second problem is that when I draw with it (by dragging with the mouse), sometimes points appear where they are not drawn. Picture:

mouse drag

When I continue, it will disappear. After some dragging appears and disappears again. And so on. Picture:

enter image description here

Any ideas why this might happen? I will send the code below:


Mouse down:

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

The mouse is 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); } 
+7
source share
3 answers

You set the variable count (the one used by 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 that you set it to double the number (maybe you considered this the number of floats?), Which means that you just draw trash after the first count vertices.

+6
source

That the vertices do not appear in the exact place you clicked on should be a hint that the problem is that you did not define a 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 coordinates of the view (self), even if the point actually refers to the window.

To convert a point from window coordinates to a view, change this line to the following:

 location = [self convertPoint: [event locationInWindow] fromView: nil]; 
+1
source

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

The second arguments define the first index of vertex attributes used in drawing. You pass 1 as the first index, which causes your vertex coordinates to be deleted. I assume you want 0 there.

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

0
source

All Articles