0

This is my function to bind vertices data to VAO and VBO:

void bindVertexToVAO(unsigned int& VAO, unsigned int& VBO, float verticesData[]) {
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticesData), verticesData, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

In the main function, I tried this:

// Cube
unsigned int cubeVAO, cubeVBO;
bindVertexToVAO(cubeVAO, cubeVBO, cubeVertices);

But, when i render this cube with this lines of code, it rendered nothing in the screen.

    shader.use();
    glm::mat4 view = camera.GetViewMatrix();
    glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom /*field of view*/), (float)SCR_WIDTH / (float)SCR_HEIGHT /*scene ration*/, 0.1f /*near plane*/, 100.0f /*far plane*/);
    shader.setMat4("view", view);
    shader.setMat4("projection", projection);
    // cubes
    glBindVertexArray(cubeVAO);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, cubeTexture);
    // First cube
    glm::mat4 model = glm::mat4(1.0f);
    model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
    shader.setMat4("model", model);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    // Second cube
    model = glm::mat4(1.0f);
    model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
    shader.setMat4("model", model);
    glDrawArrays(GL_TRIANGLES, 0, 36);

But it worked fine when I initialized it like this and then the call the same render call as above.

GLuint cubeVAO, cubeVBO;
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

This is my cube vertices data:

float cubeVertices[] = {
    // back face
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left
     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, // top-right
     0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // bottom-right
     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, // top-right
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left
    -0.5f,  0.5f, -0.5f, 0.0f, 1.0f, // top-left
    // front face
    -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left
     0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-right
     0.5f,  0.5f, 0.5f, 1.0f, 1.0f, // top-right
     0.5f,  0.5f, 0.5f, 1.0f, 1.0f, // top-right
    -0.5f,  0.5f, 0.5f, 0.0f, 1.0f, // top-left
    -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left
    // left face
    -0.5f,  0.5f,  0.5f, 1.0f, 0.0f, // top-right
    -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, // top-left
    -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left
    -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left
    -0.5f, -0.5f,  0.5f, 0.0f, 0.0f, // bottom-right
    -0.5f,  0.5f,  0.5f, 1.0f, 0.0f, // top-right
    // right face
     0.5f,  0.5f,  0.5f, 1.0f, 0.0f, // top-left
     0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right
     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, // top-right
     0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right
     0.5f,  0.5f,  0.5f, 1.0f, 0.0f, // top-left
     0.5f, -0.5f,  0.5f, 0.0f, 0.0f, // bottom-left
    // bottom face
    -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right
     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, // top-left
     0.5f, -0.5f,  0.5f, 1.0f, 0.0f, // bottom-left
     0.5f, -0.5f,  0.5f, 1.0f, 0.0f, // bottom-left
    -0.5f, -0.5f,  0.5f, 0.0f, 0.0f, // bottom-right
    -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right
    // top face
    -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left
    -0.5f, 0.5f,  0.5f, 0.0f, 0.0f, // bottom-left
     0.5f, 0.5f,  0.5f, 1.0f, 0.0f, // bottom-right
     0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right
    -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left
     0.5f, 0.5f,  0.5f, 1.0f, 0.0f, // bottom-right
};
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 3
    `sizeof(verticesData)` doesn't do what you expect. See [`sizeof` operator](https://en.cppreference.com/w/cpp/language/sizeof). – Rabbid76 May 28 '22 at 07:37
  • Oh my god, thank you a lot. You saved me. Problem solved, i've been screaming at it for 3 days :)) – Quang Phạm May 28 '22 at 08:00

0 Answers0