我做了一个绘画程序。一切都按我的预期工作。但是在画画的时候,有时会发生一些奇怪的事情。
我运行应用程序,然后在图像上按鼠标左键。它应该从代码中得出点:
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);
在我按下的地方。mouseDown
注册 mouseDown 位置,将其转换为 NSValue,发送到数组,然后在绘制之前,我将 NSValue 提取到 CGPoint 并将其发送到 GLfloat,以便它可以由 glDrawArrays 绘制。但无论我在图像上的哪个位置单击鼠标,它都会在坐标 (0,0) 处绘制点。之后,一切正常。见图片:
这是第一个问题。第二个问题是,当我用它(拖动按下鼠标)绘画时,有时会出现未绘制的点。图片:
当我继续拖动它消失。经过一些拖动后,它再次出现并再次消失。等等。图片:
任何想法为什么会发生?我将在下面发布代码:
鼠标向下:
- (void) mouseDown:(NSEvent *)event
{
location = [self convertPoint: [event locationInWindow] fromView:self];
NSValue *locationValue = [NSValue valueWithPoint:location];
[vertices addObject:locationValue];
[self drawing];
}
鼠标拖动:
- (void) mouseDragged:(NSEvent *)event
{
location = [self convertPoint: [event locationInWindow] fromView:self];
NSValue *locationValue = [NSValue valueWithPoint:location];
[vertices addObject:locationValue];
[self 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);
}