0

I'm learning OpenGL and along with it, I'm creating classes and functions to reduce the amount of repeated code. At the moment, I'm creating a function around glBufferData and I'm not understanding what I'm doing wrong. When I build my code without calling my function, my box renders perfectly, but if I try my function, it doesn't render at all, and I only get my background color.

Here would be the relevant code. The rest of my code that creates vertex buffers, reads in shaders, etc... all work fine, I have tested all of that. The issue is when I wrap this specific function.

float vertices[] = {
     0.5f,  0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
    -0.5f,  0.5f, 0.0f
};  

unsigned int VAO, EBO;
VertexBuffer vb;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO);

vb.Bind();
vb.LoadData(vertices); //this does not work
//This one below works when it's uncommented and the top is commented out
//glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

Function Definition of LoadData:

void VertexBuffer::LoadData(void* data)
{
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
}

As mentioned before, I'm slowly creating wrapper classes and such, so far I create the VertexBuffer which calls glGenBuffers in the constructor. I then bind it and then try to call LoadData which takes in a void* because that's what the gl function is expecting as a 3rd parameter. I'm just not sure what I'd doing wrong. I have tried passing in my vertices by address, my simply writing the variable, making it const after realizing that the gl function expects a const but none of this has worked so far.

Any suggestions are much appreciated

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
JesusH
  • 1
  • 7
    `sizeof(data)` is wrong. Its the size of a pointer because data is a pointer. – drescherjm Jun 17 '22 at 17:29
  • 1
    One way to fix the `sizeof` is to make your function `template void VertexBuffer::LoadData(const T (&data)[N])`. – Dietrich Epp Jun 17 '22 at 17:33
  • @DietrichEpp would you be able to explain the function parameter bit? I'm not sure I follow you on what [N] is doing. Is it accessing element N in my array? – JesusH Jun 17 '22 at 17:51
  • @JesusH: The `N` is the size of your array, like when you declare an array `float myArray[4];` that declares an array with 4 elements. – Dietrich Epp Jun 17 '22 at 18:36
  • @DietrichEpp Got it, I wasn't thinking about it that way. So then when I pass in my array, the size is worked out and automatically assigned to N correct? I'm asking because I forgot to move the function to a header file since it's now a template and I saw in the linker error that N was already defined but I never defines it myself so I guess it automatically resolved what the value of N is? – JesusH Jun 17 '22 at 21:57
  • You’d normally put it in a header file, perhaps inside the class definition itself. – Dietrich Epp Jun 18 '22 at 06:02

0 Answers0