I am currently trying to set up a little debug view as I am planning to implement occlusion queries later. For those, I have written code to determine the "extreme coordinates" of the VAO I want to run the occlusion query for. The coordinates I then want to use to build a cube (bounding volume) that contains the shape to be tested. At the moment, I am using a simple triangle as test shape.
As far as I have tested, the extreme coordinates seem correct. Based on those coordinates, I went on to build the points needed for a cube. Of course I do not feel like fiddling around with 36 values, so instead, I'd like to draw the cube using indices with only 8 points.
So far so good, but I have ran into an issue: Only about 1/3 of the cube is being drawn.
Here my code to setup vertices:
//The coordinates for the triangle shape
verticesVector.push_back(-0.4f);
verticesVector.push_back(0.1f);
verticesVector.push_back(0.0f);
verticesVector.push_back(0.4f);
verticesVector.push_back(0.1f);
verticesVector.push_back(0.0f);
verticesVector.push_back(0.0f);
verticesVector.push_back(0.7f);
verticesVector.push_back(0.3f);
//The colors for the triangle shape
colorsVector.push_back(1.0f);
colorsVector.push_back(0.0f);
colorsVector.push_back(0.0f);
colorsVector.push_back(0.0f);
colorsVector.push_back(1.0f);
colorsVector.push_back(0.0f);
colorsVector.push_back(0.0f);
colorsVector.push_back(0.0f);
colorsVector.push_back(1.0f);
glGenVertexArrays(1, &vertexArrayObject);
glGenBuffers(1, &vertexBuffer);
glGenBuffers(1, &colorBuffer);
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), &verticesVector.front(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), &colorsVector.front(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
//Finding the extremes of the shape
float minx = std::numeric_limits<float>::max();
float miny = std::numeric_limits<float>::max();
float minz = std::numeric_limits<float>::max();
float maxx = std::numeric_limits<float>::lowest();
float maxy = std::numeric_limits<float>::lowest();
float maxz = std::numeric_limits<float>::lowest();
for (size_t i = 0; i < verticesVector.size(); i += 3)
{
if (verticesVector[i] < minx) minx = verticesVector[i];
if (verticesVector[i+1] < miny) miny = verticesVector[i+1];
if (verticesVector[i+2] < minz) minz = verticesVector[i+2];
if (verticesVector[i] > maxx) maxx = verticesVector[i];
if (verticesVector[i+1] > maxy) maxy = verticesVector[i+1];
if (verticesVector[i+2] > maxz) maxz = verticesVector[i+2];
}
//The indices (or elements) for the debug bounding volume
//front
cubeElements[0] = 0;
cubeElements[1] = 1;
cubeElements[2] = 2;
cubeElements[3] = 2;
cubeElements[4] = 3;
cubeElements[5] = 0;
//top
cubeElements[6] = 3;
cubeElements[7] = 2;
cubeElements[8] = 6;
cubeElements[9] = 6;
cubeElements[10] = 7;
cubeElements[11] = 3;
//back
cubeElements[12] = 7;
cubeElements[13] = 6;
cubeElements[14] = 5;
cubeElements[15] = 5;
cubeElements[16] = 4;
cubeElements[17] = 7;
//bottom
cubeElements[18] = 4;
cubeElements[18] = 5;
cubeElements[20] = 1;
cubeElements[21] = 1;
cubeElements[22] = 0;
cubeElements[23] = 4;
//left
cubeElements[24] = 4;
cubeElements[25] = 0;
cubeElements[26] = 3;
cubeElements[27] = 3;
cubeElements[28] = 7;
cubeElements[29] = 4;
//right
cubeElements[30] = 1;
cubeElements[31] = 5;
cubeElements[32] = 6;
cubeElements[33] = 6;
cubeElements[34] = 2;
cubeElements[35] = 1;
//The actual vertices made from extreme coordinates
//lower left here
boundingVertices.push_back(minx);
boundingVertices.push_back(miny);
boundingVertices.push_back(minz);
//lower right here
boundingVertices.push_back(maxx);
boundingVertices.push_back(miny);
boundingVertices.push_back(minz);
//upper right here
boundingVertices.push_back(maxx);
boundingVertices.push_back(maxy);
boundingVertices.push_back(minz);
//upper left here
boundingVertices.push_back(minx);
boundingVertices.push_back(maxy);
boundingVertices.push_back(minz);
//lower left there
boundingVertices.push_back(minx);
boundingVertices.push_back(miny);
boundingVertices.push_back(maxz);
//lower right there
boundingVertices.push_back(maxx);
boundingVertices.push_back(miny);
boundingVertices.push_back(maxz);
//upper right there
boundingVertices.push_back(maxx);
boundingVertices.push_back(maxy);
boundingVertices.push_back(maxz);
//upper left there
boundingVertices.push_back(minx);
boundingVertices.push_back(maxy);
boundingVertices.push_back(maxz);
//Generate VAO for debug view of bounding volume
glGenVertexArrays(1, &boundingAO);
glGenBuffers(1, &boundingVB);
glGenBuffers(1, &boundingCB);
glBindVertexArray(boundingAO);
glBindBuffer(GL_ARRAY_BUFFER, boundingVB);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), &boundingVertices.front(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glGenBuffers(1, &elementsBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementsBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(float), cubeElements, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Here is how I am drawing the shapes:
glBindVertexArray(boundingAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementsBuffer);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
I understand, of course, that this is an awful lot of code still, but that is mostly because of the vertices.
Here is an image of how the whole thing currently looks:
My question finally is:
What am I doing wrong here, how come only half is drawn?
To clarify: The coloured triangle is the shape that is supposed to be enclosed in the (white) cube.