问题
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;
}
回答1:
I've found the solution. When I initialize my OpenGL functions, I have accidentally written glFrontFace(GL_CW)
instead of glFrontFace(GL_CCW)
.
回答2:
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.
来源:https://stackoverflow.com/questions/24732076/gldrawarrays-does-not-draw-anything-in-gl-triangles-mode