简体   繁体   中英

Setting an Indices and Vertices array openGL ES

I am working on an .obj loader for my app, and to do so, i set an indices array and a vertices array and init them on runtime.

This is how the structs are defined:

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

typedef struct {
    GLuint  v1;
    GLuint  v2;
    GLuint  v3;
} Face3D;

and this is how the arrays are in the .h file:

Vertex* VerticesArr;
Face3D* Faces;

But when i init them, i see nothing on my screen, as opposed to using this way:

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

const GLubyte Indices[] = {
    0, 1, 2,
    2, 3, 0
};

by using these const arrays, i get the drawing on the screen.

My question is, is using the indices and vertices array as i do is even possible? I believe it is, it just seems to be wrong.

This is how i setup my VBO's on both ways presented earlier:

- (void)setupVBOs {

    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VerticesArr), VerticesArr, GL_STATIC_DRAW);

    GLuint indexBuffer;
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Faces), Faces, GL_STATIC_DRAW);

}

and the way i'm using draw elements:

glDrawElements(GL_POINTS, sizeof(Faces)/sizeof(Faces[0]), GL_UNSIGNED_INT, 0);

I do check that both arrays are being init as they should, and they do.

This is how i allocate space for them:

// Allocate space for the Vertices array
NSLog(@"Size of vertice is: %lu",sizeof(Vertex));
VerticesArr = (Vertex*)(malloc(sizeof(Vertex) * vertexCombinations.count));
// Allocate space for the Faces
NSLog(@"Size of face is: %lu",sizeof(Face3D));
Faces = (Face3D*)(malloc(sizeof(Face3D)*faceCount));

First problem I see is in this portion of the code:

- (void)setupVBOs {

    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VerticesArr), VerticesArr, GL_STATIC_DRAW);

    GLuint indexBuffer;
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Faces), Faces, GL_STATIC_DRAW);

}

When you create the constant array sizeof(Faces) will return the size of your array, but when you dynamically allocate them then sizeof(Faces) will be the size of a pointer (usually 4 bytes). The second problem I see is that during the static allocation your indecies is GLUByte but in Face3D they are GLUInt . Not sure if that's a problem, but it seems like it could be.

Potential issues:

1) C does not guarantee tight packing of structs. So eg the compiler might look at your Face3D , spot that it's 12 bytes long and decide to pad it to 16 bytes — each struct will have four unused bytes at the end because doing so is likely to make reading individual structs faster. Similarly it reserves the right to pad between individual items in the struct. You're using natively-sized units so padding isn't necessarily likely but your compiler reserves the right to add it and relying on tight packing means relying on undefined behaviour.

2) sizeof(Faces)/sizeof(Faces[0])sizeof(struct Face3D) is a very different quantity from sizeof(GLuint) so even if your structs are tightly packed this is a difference between your two pieces of code.

You could check whether sizeof(struct Face3D) == sizeof(GLuint)*3 and the equivalent test elsewhere at runtime. If so then you can pass the struct directly to glBufferData . If not then you could possibly just pass an appropriate stride with glVertexAttribPointer depending on the exact padding but more likely you're going to need explicitly to serialise to an unstructured array for OpenGL's benefit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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