1
$\begingroup$

It's been a long time since I last used OpenGL and I can't figure out VAOs anymore... I want to make a simple voxel engine using geometry shaders. This is what I want to do:

vbo1: array of GLfloat for the positions (3 floats per vertex)

vbo2: array of GLfloat for the size of the cube (1 float per vertex)

vbo3: array of GLbyte for the type of voxel (1 byte per vertex)

-> vao: GLfloat x, GLfloat y, GLfloat z, GLfloat size, GLbyte type

Here's my code:

    float *positions = new GLfloat[size * 3];

    positions[0] = 0.5;
    positions[1] = 0.5;
    positions[2] = 0;

    glGenBuffers(1, &positionsId);
    glBindBuffer(GL_ARRAY_BUFFER, positionsId);
    glBufferData(GL_ARRAY_BUFFER, size * 3 * sizeof(GLfloat), positions, GL_STATIC_DRAW);

    byte * voxelValues = new GLubyte[size];


    voxelValues[0] = 0;

    glGenBuffers(1, &voxelValuesId);
    glBindBuffer(GL_ARRAY_BUFFER, voxelValuesId);
    glBufferData(GL_ARRAY_BUFFER, size * sizeof(GLubyte), voxelValues, GL_STATIC_DRAW);

    float * sizes = new GLfloat[size];


    sizes[0] = 1;

    glGenBuffers(1, &sizesId);
    glBindBuffer(GL_ARRAY_BUFFER, sizesId);
    glBufferData(GL_ARRAY_BUFFER, size * sizeof(GLfloat), sizes, GL_STATIC_DRAW);

    glGenVertexArrays(1, &vao);

    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, positionsId);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glBindBuffer(GL_ARRAY_BUFFER, sizesId);
    glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, NULL);
    glBindBuffer(GL_ARRAY_BUFFER, voxelValuesId);
    glVertexAttribPointer(2, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, NULL);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

Is the code even doing what I want it to do? And is it even possible to use different datatypes in the vao?

$\endgroup$

1 Answer 1

2
$\begingroup$

Look good to me.

And yes you can use different datatypes for different attributes in a vao, that's the entire point of having the type parameter there in the first place.

$\endgroup$
2
  • $\begingroup$ what I have a problem with is the "stride" value in glVertexAttribPointer. Why would you not tightly pack an array? $\endgroup$
    – l'arbre
    Commented Feb 28, 2017 at 19:58
  • 1
    $\begingroup$ when interleaving data by using a array of structs then the stride is the sizeof the struct. $\endgroup$ Commented Feb 28, 2017 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.