简体   繁体   English

将Array作为参数传递

[英]Issue passing Array as Parameter

I am using vertex buffers and element buffers. 我正在使用顶点缓冲区和元素缓冲区。

The following function takes vertex and element data as arrays and creates buffers out of that. 以下函数将顶点和元素数据作为数组,并从中创建缓冲区。 My real implementation is more complicated and stores the ids for later use of course, but that does not relate to this question. 我真正的实现更复杂,并存储id以供以后使用,但这与此问题无关。

void Create(const float Vertices[], const int Elements[])
{
    GLuint VertexBuffer, ElementBuffer; // ids

    glGenBuffers(1, VertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glGenBuffers(1, ElementBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Elements), Elements, GL_STATIC_DRAW);
}

In another function I call Create() passing two arrays which represents a cube. 在另一个函数中,我调用Create()传递两个表示一个多维数据集的数组。 But nothing happens. 但没有任何反应。 The window opens up and I see the cornflower blue background without any cube. 窗户打开,我看到没有任何立方体的矢车菊蓝色背景。

float VERTICES[] = {-1.f,-1.f,1.f,1.f,0.f,0.f,.8f,1.f,-1.f,1.f,0.f,1.f,0.f,.8f,1.f,1.f,1.f,0.f,0.f,1.f,.8f,-1.f,1.f,1.f,1.f,1.f,1.f,.8f,-1.f,-1.f,-1.f,0.f,0.f,1.f,.8f,1.f,-1.f,-1.f,1.f,1.f,1.f,.8f,1.f,1.f,-1.f,1.f,0.f,0.f,.8f,-1.f,1.f,-1.f,0.f,1.f,0.f,.8f};
int   ELEMENTS[] = {0,1,2,2,3,0,1,5,6,6,2,1,7,6,5,5,4,7,4,0,3,3,7,4,4,5,1,1,0,4,3,2,6,6,7,3};

Create(VERTICES, ELEMENTS);

If I move the vertex and element data inside the Create() function, everything works fine and the cube is rendered correctly. 如果将顶点和元素数据移动到Create()函数中,则一切正常,并且多维数据集正确呈现。

void Create()
{
    GLuint VertexBuffer, ElementBuffer;

    float VERTICES[] = {-1.f,-1.f,1.f,1.f,0.f,0.f,.8f,1.f,-1.f,1.f,0.f,1.f,0.f,.8f,1.f,1.f,1.f,0.f,0.f,1.f,.8f,-1.f,1.f,1.f,1.f,1.f,1.f,.8f,-1.f,-1.f,-1.f,0.f,0.f,1.f,.8f,1.f,-1.f,-1.f,1.f,1.f,1.f,.8f,1.f,1.f,-1.f,1.f,0.f,0.f,.8f,-1.f,1.f,-1.f,0.f,1.f,0.f,.8f};
    int   ELEMENTS[] = {0,1,2,2,3,0,1,5,6,6,2,1,7,6,5,5,4,7,4,0,3,3,7,4,4,5,1,1,0,4,3,2,6,6,7,3};

    glGenBuffers(1, VertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);

    glGenBuffers(1, ElementBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ELEMENTS), ELEMENTS, GL_STATIC_DRAW);
}

Therefore I assume that the problem occurs when passing the array to the Create() function. 因此,我假设将数组传递给Create()函数时会出现问题。 I do not get any compiler error or warning. 我没有得到任何编译器错误或警告。 What is wrong here? 这有什么不对?

A parameter of type const float Vertices[] is actually the same as const float Vertices* . 类型const float Vertices[]参数实际上与const float Vertices*相同。 So sizeof is just returning the size of a pointer. 所以sizeof只是返回一个指针的大小。

Use a reference to array using templates instead: 使用引用代替使用模板的数组:

template<std::size_t VerticesN, std::size_t ElementsN>
void Create(const float (&Vertices)[VerticesN], const int (&Elements)[ElementsN])
{
// ...
}

// Usage is the same since template argument deduction

float VERTICES[] = {-1.f,-1.f,1.f,1.f,0.f,0.f,.8f,1.f,-1.f,1.f,0.f,1.f,0.f,.8f,1.f,1.f,1.f,0.f,0.f,1.f,.8f,-1.f,1.f,1.f,1.f,1.f,1.f,.8f,-1.f,-1.f,-1.f,0.f,0.f,1.f,.8f,1.f,-1.f,-1.f,1.f,1.f,1.f,.8f,1.f,1.f,-1.f,1.f,0.f,0.f,.8f,-1.f,1.f,-1.f,0.f,1.f,0.f,.8f};
int   ELEMENTS[] = {0,1,2,2,3,0,1,5,6,6,2,1,7,6,5,5,4,7,4,0,3,3,7,4,4,5,1,1,0,4,3,2,6,6,7,3};

Create(VERTICES, ELEMENTS);

The problem here is sizeof(VERTICES) and sizeof(ELEMENTS) . 这里的问题是sizeof(VERTICES)sizeof(ELEMENTS) When used in the Create() method the sizes of the arrays are known, but when you pass the arrays as a parameter (like in the Create(const float Vertices[], const int Elements[]) the array degrades to a pointer, and the sizeof is reduced to returning the size of the pointer. 当在Create()方法中使用时,数组的大小是已知的,但是当您将数组作为参数传递时(例如在Create(const float Vertices[], const int Elements[]) ,数组会降级为指针,并将sizeof减小为返回指针的大小。

One simple solution is to pass the size along with the arrays. 一种简单的解决方案是将大小与数组一起传递。 So the function will look like this: 所以函数看起来像这样:

void Create(const float Vertices[], size_t VertSize, const int Elements[], size_t ElemSize) {
  ...
}

but I think I would prefer a solution that uses the new std::array which has a size() function: 但我想我更喜欢使用具有size()函数的新std :: array的解决方案:

void Create(const std::array<float>& vertices, std::array<int>& elements) {
  ...
}

If you do not have the opportunity to work with c++ 11, the boost libraries will provide the boost::array which mirrors the behaviour of c++ 11. 如果您没有机会使用c ++ 11,则boost库将提供boost :: array,该镜像反映了c ++ 11的行为。

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

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