【问题标题】:Add value to vertex array向顶点数组添加值
【发布时间】:2012-07-04 16:44:00
【问题描述】:

我想让我的顶点数组动态化。这样在每个鼠标事件之后都会添加这些值。当用户拖动鼠标时,我注册鼠标位置坐标。坐标被命名为“loc”。当用户拖动鼠标时,“loc”值会更新。所以我希望当'loc'更新时,坐标将被添加到顶点数组。我仍然只能在 'loc' 更新时这样做,它会重建顶点数组,所以我的顶点数组总是只有一个坐标(当前的 'loc' 值)。

我的顶点数组值存储在 GLfloat 中:

GLfloat vertexes[] = { loc.x, loc.y };

'loc'在- (void) mouseDragged:(NSEvent *)event注册:

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

顶点数组由- (void) drawMyShape绘制:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertexes);
glPointSize(30);
glDrawArrays(GL_POINTS, 0, vertexCount);
glFlush();
glDisableClientState(GL_VERTEX_ARRAY);

- (void) drawMyShape 在注册鼠标事件并将它们添加到- (void) mouseDragged:(NSEvent *)event 中的 GLfloat 后调用:

[self drawMyShape]

当前代码:

在 .h 文件中:

NSMutableArray *vertices;
@property (nonatomic, retain) NSMutableArray *vertices;

在.m文件的开头

@dynamic vertices;

- (id)initWithCoder:(NSCoder *)coder中的.m文件中

vertices = [[NSMutableArray alloc] init];

- (void) mouseDragged:(NSEvent *)event的.m文件中

loc = [self convertPoint: [event locationInWindow] fromView:self];
NSValue *locationValue = [NSValue valueWithPoint:loc];
[vertices addObject:locationValue];`
[self addValuesToArray];

- (void) addValuesToArray中的.m文件中

int count = [vertices count] * 2; 
NSLog(@"count: %d", count); 
int currIndex = 0; 
GLfloat GLVertices[] = {*(GLfloat *)malloc(count * sizeof(GLfloat))}; 
for (NSValue *locationValue in vertices) { 
  NSValue *locationValue = [vertices objectAtIndex:currIndex++]; 
  CGPoint curLoc = locationValue.pointValue; 
  GLVertices[currIndex++] = curLoc.x; 
  GLVertices[currIndex++] = curLoc.y; 
} 

它崩溃了

NSValue *locationValue = [vertices objectAtIndex:i];

在日志崩溃后我看到(删除了日志的某些部分,因为我认为它们并不重要):

sum: 2
*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]
sum: 3
*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]
sum: 4
*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]
sum: 5
*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]
sum: 6
(lldb) 

【问题讨论】:

    标签: objective-c xcode macos cocoa opengl


    【解决方案1】:

    使用 NSMutableArray 动态存储您的位置对象。

    在初始化阶段初始化数组:

    NSMutableArray *vertices = [[NSMutableArray alloc] init];
    

    在鼠标事件上添加顶点:

    loc = [self convertPoint: [event locationInWindow] fromView:self];
    [vertices addObject:loc]; // Assuming loc can be added as an object to the array
    

    绘制前转换为GLFloat数组:

    int count = [vertices count] * 2; // * 2 for the two coordinates of a loc object
    GLFloat *glVertices = (GLFloat *)malloc(count * sizeof(GLFloat));
    int currIndex = 0;
    for (YourLocObject *loc in vertices) {
        glVertices[currIndex++] = loc.x;
        glVertices[currIndex++] = loc.y;        
    }
    

    现在您可以在绘图时使用glVertices 数组。

    【讨论】:

    • 感谢您的回答。我认为这是一个很好的解决方案。尝试这样做时,我只有一个问题。我认为这是因为 loc 是 CGPoint。我在 [vertices addObject:loc]; 中得到 error Sending 'CGPoint' (aka 'struct CGPoint') to parameter of incompatible type 'id'; 。我应该如何更改 CGPoint 以使其正常工作?
    • 使用 NSValue 将 CGPoint 存储在数组中。
    • 您确定没有添加任何内容吗?尝试在 NSLog 中使用 %d 而不是 %lu
    • 哦,初始化数组时不要再次声明 NSMutableArray*。只需使用顶点 = [...]。在存储 y 坐标之前,您的绘图函数中的另一件事会增加 i。
    • 你的 for 循环是错误的。使用我向您展示的语法。 count 是数组大小的两倍,因为每个元素都是一个位置对象,而不是一个坐标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2010-09-17
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多