typedef struct Attributes { sizet size; unsigned int count; GLenum type; const void…
- User
typedef struct Attribute_s { size_t size; unsigned int count; GLenum type; const void *pData; GLuint VBO; } Attribute_t;
void createVAO(GLuint *pVAO, const unsigned int attributeCount, ...) { glGenVertexArrays(1, pVAO); glBindVertexArray(*pVAO);
codeva_list attributeList; va_start(attributeList, attributeCount); int offset = 0; for (unsigned int i = 0; i < attributeCount; ++i) { Attribute_t attribute = va_arg(attributeList, Attribute_t); GLsizei stride = attribute.count * sizeof(attribute.type); offset += stride; glGenBuffers(1, &attribute.VBO); glBindBuffer(GL_ARRAY_BUFFER, attribute.VBO); glBufferData(GL_ARRAY_BUFFER, attribute.size, attribute.pData, GL_STATIC_DRAW); glVertexAttribPointer(i, attribute.count, attribute.type, GL_FALSE, stride, (void *) &offset); glEnableVertexAttribArray(i); }} GLuint cube_vao, cube_ebo; Attribute_t attributes[] = { {sizeof(positions), 3, GL_FLOAT, positions, 0}, {sizeof(normals), 3, GL_FLOAT, normals, 0}, {sizeof(texture_coordinates), 2, GL_FLOAT, texture_coordinates, 0} }; createVAO(&cube_vao, 3, attributes[0], attributes[1], attributes[2]);
codeglGenBuffers(1, &cube_ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);Can you spot the cause of undefined behaviour?
- Assistant
The cause of undefined behavior in the provided code is the use of a local variable
attributein thecreateVAOfunction.In the
createVAOfunction, theattributevariable is declared usingAttribute_t attribute = va_arg(attributeList, Attribute_t);. The problem is that this variable is a local variable and its memory is not guaranteed to persist after the function returns.Later in the code,
attribute.VBOis used to store the generated buffer object and is passed toglVertexAttribPointer. However, once thecreateVAOfunction returns, theattributevariable goes out of scope and its memory may be reused for other purposes. This means thatattribute.VBObecomes a dangling pointer, leading to undefined behavior if it is accessed later.To fix this issue, you can allocate the
Attribute_tstruct dynamically usingmallocandfreeor allocate it on the heap usingnewanddelete. This way, the memory allocated for the struct will persist even after thecreateVAOfunction returns. - User
Yeah, I think that's it. It's just there to cache va_arg, do I HAVE to dynamically allocate it?
- Assistant
No, you don't necessarily have to dynamically allocate the
Attribute_tstruct. There are a few alternative approaches you can take to avoid the use of dynamic allocation:- Use a fixed-size array of
Attribute_tinstead of a variable number of arguments: Instead of using...andva_list, you can define a fixed-size array ofAttribute_tand pass it directly to thecreateVAOfunction. This eliminates the need for dynamic allocation and ensures that the memory is valid throughout the lifetime of the function.
cppvoid createVAO(GLuint *pVAO, unsigned int attributeCount, Attribute_t* attributes) { // ... }- Pass the structs by reference: Instead of passing the structs by value, pass them by reference using pointers. This way, the memory for the structs can be allocated outside of the function and its validity can be ensured.
- Use a fixed-size array of
- User
I'm still getting undefined behaviour even with this code: void createVAO(GLuint *pVAO, const unsigned int attributeCount, Attribute_t *pAttributes) { glGenVertexArrays(1, pVAO); glBindVertexArray(*pVAO);
codeint offset = 0; for (unsigned int i = 0; i < attributeCount; ++i) { GLsizei stride = pAttributes[i].count * sizeof(pAttributes[i].type); offset += stride; glGenBuffers(1, &pAttributes[i].VBO); glBindBuffer(GL_ARRAY_BUFFER, pAttributes[i].VBO); glBufferData(GL_ARRAY_BUFFER, pAttributes[i].size, pAttributes[i].pData, GL_STATIC_DRAW); glVertexAttribPointer(i, pAttributes[i].count, pAttributes[i].type, GL_FALSE, stride, (void *) &offset); glEnableVertexAttribArray(i); }}
- Assistant
It appears that the issue causing undefined behavior lies in the computation of the
offsetvariable within thecreateVAOfunction. Specifically, theoffsetvariable is incremented bystrideat each iteration of the loop.However, the
offsetvariable should be incremented by the sum of all previous strides to correctly calculate the offset for each attribute. Currently, the offset is being incremented by the stride of the current attribute only, resulting in incorrect offset values for subsequent attributes.To fix this issue, you should modify the calculation of the offset as follows:
cpp - User
The real solution was uintptr_t as you mentioned, but it caused an issue with hearing the second attribute (I get bad normals now). Why? The undefined bahviour went, but I get reading errors.
- Assistant
I apologize for the confusion caused by my previous response. You are correct that using
uintptr_tto cast the offset can introduce issues with certain data types, as it assumes that the offset represents a valid address.To correctly calculate the offset for each vertex attribute, you should use the
void*version of theglVertexAttribPointerfunction and pass the correct offset value as a pointer to the offset variable itself (without casting it to an address).Here's the updated code:
cpp