c++ - Having problems with my OpenGL plane -
i'm having bit of trouble creating plane (using gl_triangle) using vertex arrays.
heres code:
float halfw = (getplanewidth() / 2); float halfd = (getplanedepth() / 2); //generate vertices for(int z = halfd; z >= -halfd; z--) { for(int x = -halfw; x <= halfw; x++) { float xv = x * cellwidth; float yv = 0; float zv = z * celldepth; vertices.push_back(xv); vertices.push_back(yv); vertices.push_back(zv); float xn = xv - getx(); float yn = yv - gety(); float zn = zv - getz(); setnormals(&xn, &yn, &zn); //calculate normals normals.push_back(xn); normals.push_back(yn); normals.push_back(zn); } } //generate indices for(int y = 0; y < getplanedepth(); y++) { for(int x = 0; x < getplanewidth(); x++) { int curvertex = (x + (y * (getplanedepth() + 1))); //bottom left vertex id if(curvertex%2 == 0) { indices.push_back((x) + (y) * (getplanedepth()+1)); //bottom left indices.push_back((x+1) + (y) * (getplanedepth()+1)); //bottom right indices.push_back((x+1) + (y+1) * (getplanedepth()+1)); //top right indices.push_back((x+1) + (y+1) * (getplanedepth()+1)); //top right indices.push_back((x) + (y+1) * (getplanedepth()+1)); //top left indices.push_back((x) + (y) * (getplanedepth()+1)); //bottom left } else //reverse triangle { indices.push_back((x+1) + (y) * (getplanedepth()+1)); //bottom right indices.push_back((x) + (y) * (getplanedepth()+1)); //bottom left indices.push_back((x) + (y+1) * (getplanedepth()+1)); //top left indices.push_back((x) + (y+1) * (getplanedepth()+1)); //top left indices.push_back((x+1) + (y+1) * (getplanedepth()+1)); //top right indices.push_back((x+1) + (y) * (getplanedepth()+1)); //bottom right } } }
the code works fine if width , depth same, if different, screws up.
can see problem?
i've coded pivot point in middle of plane.
int curvertex = (x + (y * (getplanewidth() + 1))); //bottom left vertex id if(curvertex%2 == 0) { indices.push_back((x) + (y) * (getplanewidth()+1)); //bottom left indices.push_back((x+1) + (y) * (getplanewidth()+1)); //bottom right indices.push_back((x+1) + (y+1) * (getplanewidth()+1)); //top right indices.push_back((x+1) + (y+1) * (getplanewidth()+1)); //top right indices.push_back((x) + (y+1) * (getplanewidth()+1)); //top left indices.push_back((x) + (y) * (getplanewidth()+1)); //bottom left } else //reverse triangle { indices.push_back((x+1) + (y) * (getplanewidth()+1)); //bottom right indices.push_back((x) + (y) * (getplanewidth()+1)); //bottom left indices.push_back((x) + (y+1) * (getplanewidth()+1)); //top left indices.push_back((x) + (y+1) * (getplanewidth()+1)); //top left indices.push_back((x+1) + (y+1) * (getplanewidth()+1)); //top right indices.push_back((x+1) + (y) * (getplanewidth()+1)); //bottom right }
?
Comments
Post a Comment