0

i am just trying to render a simple triangle with the most basic OpenGL code, here it is: (all of these code are in the main() function)

GLfloat data[] = {
    0, 0.5,
    -0.5, -0.5, 
    0.5, -0.5
};

GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);


while (!glfwWindowShouldClose(window))
{
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

if I do it this way, a triangle would appear on the screen, eveything works just fine, however the problem is that as soon as I move the buffer code(glGenBuffers, BindBuffer, BufferData etc) to another class, the triangle disappears, here is the code: (this is the main() function)

GLfloat data[] = {
    0, 0.5,
    -0.5, -0.5, 
    0.5, -0.5
};

//the buffer code is now called by this VertexBuffer constructor which 
//requires vertex data to be passed into
VertexBuffer vertexBuffer(data);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);


while (!glfwWindowShouldClose(window))
{
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

Here is the VertexBuffer class constructor code:

VertexBuffer::VertexBuffer(const void* data)
{
    //the vertexBufferID here is defined in the header
    glGenBuffers(1, &vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
}

So as I said the VertexBuffer constructor code is identical to the buffer code in the main() function before except it has been moved into another class. I am struggling to figure out what is the problem. Any help would be great, thank you.

lucasyyy
  • 63
  • 1
  • 1
  • 4
  • 6
    `sizeof(data)` gives you the size of a pointer, not the size of the passed in array – UnholySheep Mar 09 '18 at 21:10
  • 4
    Why do people have to start with using OpenGL and other quite complicated libraries before they learn the language? Wouldn't it be so much more easier to start with core C++ feature, understand how it works, and than start using all those complex beasts? – SergeyA Mar 09 '18 at 21:19
  • @SergeyA: Because they want to draw stuff. They see graphics in games and whatnot, and think, "I want to do that!" – Nicol Bolas Mar 09 '18 at 23:22

0 Answers0