简体   繁体   中英

glDrawArrays does not draw anything in GL_TRIANGLES mode

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;
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM