繁体   English   中英

在Obj C中初始化和修改结构的C数组

[英]initializing and modifying C arrays of structs in Obj C

我正在尝试在iOS中使用一些Open GL的东西,但是我被C东西所困扰,这是我十年没有使用的东西。

我用作基础的示例代码声明struct Vertex:

typedef struct {
    float Position[3];
    float Color[4];
} Vertex;

然后,它声明用于数学运算的C样式的顶点数组:

Vertex Vertices[] = {
    {{0.5, -0.5, 0}, {1, 1, 1, 1}},
    {{0.5, 0.5, 0}, {1, 1, 1, 1}},
    {{-0.5, 0.5, 0}, {1, 1, 1, 1}},
    {{-0.5, -0.5, 0}, {1, 1, 1, 1}}
};

我不仅要使用此固定数组,还需要调用一种方法来返回新的顶点数组,这就是我遇到的问题。 我想要的是类似的东西(我知道这是完全错误的,它比任何东西都要伪代码):

- (Vertex (*)[])getLineSegments {
    Vertex *vertices = ?? //(need [allPoints count] Vertexs)
    for (int i = 0; i < [allPoints count]; i++) {
        vertices[i] = { [allPoints[i] getPoints], [allPoints[i] getColor] }; //how do I make a new Vertex in this fashion?
        //and then, in getPoints/getColor, how would I make a float[] and return that properly
    }
    return vertices;
}

只是尝试使用malloc实例化和分配值,而我在其他地方已经读过的东西却失败了:

- (Vertex (*)[])getLineSegments {
    Vertex (*vertices)[4] = malloc(sizeof(Vertex) * 4);
    Vertex *point = malloc(sizeof(Vertex));
    float Pos[3] = {0.5, -0.5, 0}; //error: Array Type Pos[3] is not assingable
    point->Position = Pos;
    float Color[4] = {1,1,1,1};
    point->Color = Color;
    vertices[0] = point;
    vertices[1] = {{0.5, 0.5, 0} , {1, 1, 1, 1}};
    vertices[2] = {{-0.5, 0.5, 0}, {1, 1, 1, 1}};
    vertices[3] = {{-0.5, -0.5, 0},{1, 1, 1, 1}}; //errors... errors everywhere
    return vertices;
}

如何正确执行此操作?

---

编辑:从伯顿的建议更新为以下内容。 还有一些错误:

- (Vertex (*)[])getLineSegments {
Vertex (*vertices)[4] = malloc(sizeof(Vertex) * 4);
for (int i = 0; i < 4; i++) {
    vertices[i] = malloc(sizeof(*vertices[i])); //err: Array type 'Vertex[4]' is not assignable 
    vertices[i]->Position[0] = 0.5; //this one works. is this correct?
    vertices[i].Position[1] = -0.5; //Member reference type 'Vertex *' is a pointer; maybe you meant to use ->?
    vertices[i].Position[2] = 0;
    vertices[i].Color[0] = 1;
    vertices[i].Color[1] = 1;
    vertices[i].Color[2] = 1;
    vertices[i].Color[3] = 1;
}
return vertices;
}

您需要手动分配值。 您使用的表示法仅适用于自动变量。 它看起来不太好,但这几乎是初始化动态数据的唯一方法。 像这样:

vertices[1].Position[0] = 1;
vertices[1].Position[1] = 2;
vertices[1].Position[3] = 3;
vertices[1].Color[0] = 1;
vertices[1].Color[1] = 1;
vertices[1].Color[2] = 1;
vertices[1].Color[3] = 1;

对于每个顶点[2]和顶点[3],依此类推。

哦,你必须做

vertices[1] = malloc(sizeof(*vertices[1]));

首先为数据分配存储。

这就是我的建议(即使我使用的是变量而不是4)。 假设我了解您想要做什么。

Vertex *getLineSegments(int size) {
    Vertex *vertices = malloc(sizeof(Vertex) * size);
    for (int i = 0; i < size; ++i) {
        vertices[i].Position[0] = ...;
        vertices[i].Position[1] = ...;
        vertices[i].Position[2] = ...;
        vertices[i].Color[0] = ...;
        vertices[i].Color[1] = ...;
        vertices[i].Color[2] = ...;
        vertices[i].Color[3] = ...;
    }
}

然后调用此方法的方式:

Vertex *vertices = getLineSegments(4);

- 要么 -

Vertex vertices[] = getLineSegments(4);

如果我正确理解,您正在尝试创建Vertex类型的一维数组,这是正确的吗?

如果是这种情况,您的getLineSegments可能如下所示:

typedef struct {
    float Position[3];
    float Color[4];
} Vertex;

typedef Vertex * VertextPtr;

VertextPtr getLineSegments() 
{
    VertextPtr vertices = (VertextPtr)calloc(sizeof(Vertex), 4);

    for (int i = 0; i < 4; i++) 
    {
        vertices[i].Position[0] = 0.5;
        vertices[i].Position[1] = -0.5;
        vertices[i].Position[2] = 0;
        vertices[i].Color[0] = 1;
        vertices[i].Color[1] = 1;
        vertices[i].Color[2] = 1;
        vertices[i].Color[3] = 1;
    }
    return vertices;
}

int main(void)
{
    VertextPtr vertices = getLineSegments();

    printf("Position: %f\n", vertices[2].Position[0]);
    printf("Position: %f\n", vertices[2].Position[1]);
    printf("Position: %f\n", vertices[2].Position[2]);
    //
    // Do clean-up here.
    return 0;   
}

请注意,我在Vertex *上使用了typedef来使代码更简洁。 我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM