0

I have this method

void RectangleRenderer::renderRectangle()
{
    glGenBuffers(1, &VBO);
    unsigned int verticesSize = 48;
    float* vertices = this->createVertices(centerPosition, 0.5);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, verticesSize, vertices, GL_STATIC_DRAW);
}

createVertices method currently looks like this

float * RectangleRenderer::createVertices(const glm::vec3 centerPosition, const float rectangleSize) const
{
    float verticesArray[12] = {
     0.5f,  0.5f, 0.0f,  
     0.5f, -0.5f, 0.0f, 
    -0.5f, -0.5f, 0.0f,  
    -0.5f,  0.5f, 0.0f 
    };
    return &verticesArray[0];
}

This code renders nothing and I have no idea why. When testing, I realized that if renderRectangle method is changed to the following

void RectangleRenderer::renderRectangle()
{
    glGenBuffers(1, &VBO);
    unsigned int verticesSize = 48;
    float verticesArray[12] = {
     0.5f,  0.5f, 0.0f, 
     0.5f, -0.5f, 0.0f,  
    -0.5f, -0.5f, 0.0f,  
    -0.5f,  0.5f, 0.0f  
    };
    float* vertices = &verticesArray[0];

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, verticesSize, vertices, GL_STATIC_DRAW);
}

Then the code works perfectly and has the desired outcome, which in this case is rendering a rectangle.

Am I missing something? Why does the second version of renderRectangle work, but not the first one?

P_Andre
  • 730
  • 6
  • 17

0 Answers0