1
votes

I am having trouble getting Eigen::Vector3f and Opengl ES 2.0 VBO to work together. My initial attempt was glVertexAttribPointer(VERTEX, 3, GL_FLOAT , sizeof(Vertex), 0 ). While this draws nothing if I start to play with the stride values I can see a broken mesh. This is my current code which leads to crash. In my old code I was using simple vector3 class made from 3 floats which was working fine.

struct Vertex {

    Eigen::Vector3f pos ;

    Eigen::Vector3f normal;
};


std::vector<Vertex> Vertices;

std::vector<ushort16> Indices;

...


GLuint vao;
uint32 vboID, vboID2;

glGenVertexArraysOES(1, &vao);
glBindVertexArrayOES(vao);


glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, GL_STATIC_DRAW , sizeof(Vertex) * Vertices.size(), &Vertices[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);


glGenBuffers(1, &vboID2);
glBindBuffer(GL_ARRAY_BUFFER, vboID2);
glBufferData(GL_ARRAY_BUFFER, GL_STATIC_DRAW , sizeof(ushort16) * m_vIndices.size(), &Indices[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);


glBindBuffer(GL_ARRAY_BUFFER, vboID);
glEnableVertexAttribArray(VERTEX);
glVertexAttribPointer(VERTEX, 3, GL_FLOAT , sizeof(Vertex), &Vertices[0].pos );

glEnableVertexAttribArray(NORMAL);
glVertexAttribPointer(NORMAL, 3, GL_FLOAT , sizeof(Vertex), &Vertices[0].normal );

glBindBuffer(GL_ARRAY_BUFFER, vboID2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArrayOES(0);


...
1
Your question seems to indicate that you are using Vertex Buffer Objects, but the code you show is using the address of client memory. You have cut out too many important lines to know what is actually going on. - Andon M. Coleman
Most of the calls are already wrapped in my application and it will be hard to extract all the working bits. Let me know how I can make my question more accurate. - sabotage3d
Well, there is no draw call, there is no glBindBuffer call, and you are presumably uploading your indices to the wrong buffer object target (assuming you have the index buffer bound to GL_ELEMENT_ARRAY_BUFFER and not GL_ARRAY_BUFFER). You are free to upload data to a buffer object by binding it anywhere you want, but later on when you call glVertexAttribPointer (...) that requires it to be bound to GL_ARRAY_BUFFER and glDrawElements (...) requires it to be bound to GL_ELEMENT_ARRAY_BUFFER. - Andon M. Coleman
Thanks for pointing that out I have update the relevant parts of the code let me know if have made any other mistakes. - sabotage3d

1 Answers

1
votes

that solved my problem:

I removed the:

glBufferData(GL_ARRAY_BUFFER, GL_STATIC_DRAW , sizeof(Vertex) * Vertices.size(), &Vertices[0]);

And I changed these lines to

glVertexAttribPointer(VERTEX, 3, GL_FLOAT , sizeof(Vertex), Vertices[0].pos.data() );

....
glVertexAttribPointer(NORMAL, 3, GL_FLOAT , sizeof(Vertex), Vertices[0].normal.data() );