I have a class terrain
which create a grid of Quad
s. I do it like this
for(int z=0; z<_length;z++){
for(int x=0; x<_width;x++){
vertices.push_back(vec3((float)x*250, 0.f, (float)z*250));
}
}
for(int z=0; z<(_length-1);++z){
for(int x=0; x<(_width-1);++x){
int index = z*_width+x;
Vertex _vertices[] = {
Vertex(vertices.at(index),vec3(0, 0, 0)),
Vertex(vertices.at(index+1),vec3(0, 0, 0)),
Vertex(vertices.at(index+_width),vec3(0, 0, 0)),
Vertex(vertices.at(index+1+_width),vec3(0,0,0))
};
unsigned short indices[]= {index,index + 1,index +
_width,index + 1,index + _width,index + _width + 1};
Quad quad(_vertices, 4, indices, 6);
squares.push_back(quad);
i++;
}
}
The vertices and the logic are correct, but the indices aren't, for some reason. here is the output for this code : But when I change this indices to this :
unsigned short indices[]= {0,1,2,1,2,3};
It works great :
The problem is I don't understand why this line
unsigned short indices[]= {index,index + 1,index +
_width,index + 1,index + _width,index + _width + 1};
doesn't work. And if it worked, my grid would consume a lot less ressources. If someone could explain me why it doesn't work, It would be great, thanks you. In case you need to know how I draw a Quad, here is the code :
class Quad{
public:
Quad(Vertex *_vertices, int _n, unsigned short * _indices, unsigned short _numIndices){
for(int i=0; i < _numIndices; i++){
indices.push_back(_indices[i]);
}
for(int i=0; i<_n; i++){
vec3 v = vec3(_vertices[i].position, _lengthPower);
position.push_back(v);
}
glGenVertexArrays(1, &mVertexArray);
glBindVertexArray(mVertexArray);
glGenBuffers(1, &mPositionBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vec3)*position.size(), position.data(), GL_STATIC_DRAW);
glGenBuffers(1, &mIndicesBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndicesBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*indices.size(), indices.data(), GL_STATIC_DRAW);
}
void draw(){
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndicesBuffer);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, 0);
glDisableVertexAttribArray(0);
}
~Quad(){
}
private:
std::vector<unsigned short> indices;
std::vector<vec3> position;
GLuint mVertexArray;
GLuint mPositionBuffer;
GLuint mIndicesBuffer;
};
I'm using, OpenGL, glm, glfw etc.
indices.size()
size value in your draw method? And if your indices are of length 6 (=unsigned short indices[]= {0,1,2,1,2,3};
), why is it working? Or you draw each quad in separate VBO when you use those working indices above? \$\endgroup\$glBufferData(GL_ARRAY_BUFFER, sizeof(vec3)*position.size(), position.data(), GL_STATIC_DRAW);
\$\endgroup\$