Reputation: 33
**//My 3D vertex data class**
class Vertex
{
public:
Vertex(float xx, float yy, float zz) : x(xx), y(yy), z(zz) { }
virtual ~Vertex() {}
private:
float x;
float y;
float z;
};
**//My quad data.**
std::vector<Vertex> quadVertexData;
quadVertexData.push_back(Vertex(-0.5, 0.5, 0.0));
quadVertexData.push_back(Vertex(-0.5, -0.5, 0.0));
quadVertexData.push_back(Vertex( 0.5, -0.5, 0.0));
quadVertexData.push_back(Vertex( 0.5, 0.5, 0.0));
//This is how I make my render call.
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);
glGenBuffers(1, &_vbo);
glGenBuffers(1, &_ebo);
void render(std::vector<Vertex>& vertices, std::vector<unsigned int> indices)
{
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(),
&vertices[0], GL_STATIC_DRAW); //tried with sizeof(vertices[0]) too.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indices.size(),
&indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
}
ISSUE : //Instead of quad, I get an unexpected triangle as show in pic.
How should I pass bufferdata
to get desired results?
Same code if I tried with glm::vec3
then its works fine.
//Snapshots :
Upvotes: 3
Views: 85
Reputation: 211239
class Vertex
{
public:
Vertex(float xx, float yy, float zz) : x(xx), y(yy), z(zz) { }
virtual ~Vertex() {}
private:
float x;
float y;
float z;
};
sizeof(Vertex)
is not 12 as you expected. It is probably 16 or 24 (depends on 32 or 64 bit system). This is because you have an virtual
destructor. This causes that somewhere in the object has to be stored the pointer to the virtual function table. Finally the vertex coordinates in std::vector<Vertex> quadVertexData
are not tightly packed.
To do what you want, you should use a struct
struct Vertex
{
float x;
float y;
float z;
};
Of course your code will work if you omit the virtual
keyword in class Vertex
:
class Vertex
{
public:
Vertex(float xx, float yy, float zz) : x(xx), y(yy), z(zz) { }
~Vertex() {}
private:
float x;
float y;
float z;
};
Upvotes: 3