8

我做了一个绘画程序。一切都按我的预期工作。但是在画画的时候,有时会发生一些奇怪的事情。

我运行应用程序,然后在图像上按鼠标左键。它应该从代码中得出点:

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

3 回答 3

7

您正在将count变量(在 中使用的变量glDrawArrays)设置为[vertices count] * 2,这看起来很奇怪。

的最后一个参数glDrawArrays是要绘制的顶点数,而在您的代码中,您似乎将其设置为数字的两倍(也许您认为它是浮点数?),这意味着您只是在第一个count顶点之后绘制垃圾。

于 2012-07-09T07:32:05.407 回答
1

顶点未在您单击的确切位置呈现的事实应该暗示问题是您没有正确确定视图中的命中点。

您的代码有:

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

它告诉视图将点从其坐标 (self) 转换为同一视图的坐标 (self),即使该点实际上是相对于窗口的。

要将点从窗口坐标转换为视图,请将该行更改为以下内容:

location = [self convertPoint: [event locationInWindow] fromView: nil];
于 2012-07-09T22:52:44.840 回答
0

glDrawArrays 的参数定义为(GLenum 模式,GLint first,GLsizei 计数)。

第二个参数定义绘制时使用的顶点属性的第一个索引。您将 1 作为第一个索引传递,这使您的顶点坐标不匹配。我假设你想要 0 那里。

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

于 2012-07-05T20:59:52.820 回答