1
votes

My problem is the glDrawArray command stops working. I've written a few programs which I used that command. However now, I don't have any idea why it doesn't work.

But I still can draw lines to the screen with GL_LINES instead of GL_TRIANGLES.

Here is my code:

#include "Mesh.h"
#include <iostream>

Mesh::Mesh()
{
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glGenBuffers(1, &vbo);
    size = 0;
}

void Mesh::AddVertices(const Vertex vertices[], int length)
{
    size = length;

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}

void Mesh::Draw()
{
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, Vertex::SIZE * 4, 0);

    glDrawArrays(GL_TRIANGLES, 0, size);

    glDisableVertexAttribArray(0);
}

Draw is always called by Game class. And my Vertex class:

#include "Vertex.h"

Vertex::Vertex(glm::vec3 pos)
{
    this->pos = pos;
}

Vertex::Vertex(float x, float y, float z)
{
    pos = glm::vec3(x, y, z);
}

glm::vec3 Vertex::GetPos() const
{
    return pos;
}

void Vertex::SetPos(glm::vec3 pos)
{
    this->pos = pos;
}
2
If you use multiple mesh objects, you might get into trouble with your VAOs, since you only set them in the constructor. So if you allocate 2 meshs before filling the first, you change the wrong VBO in AddVerticesuser1781290

2 Answers

1
votes

sizeof(vertices) is sizeof(void*), because in C or C++ arrays in function arguments decays to pointers. You should use length provided in second argument, if it is size in bytes.

1
votes

I've found the solution. When I initialize my OpenGL functions, I have accidentally written glFrontFace(GL_CW) instead of glFrontFace(GL_CCW).