Now the bounding boxes are used in two stages.
The first which I've just implemented is a simple and straight forward on-screen test of the bounding box in software.
At this point in time I'm taking a shortcut by applying the following rules:
- As soon as one of the 8 vertices of the bounding box is on screen I stop checking and render my model.
- If all of the 8 vertices are behind the camera, skip
- if all of the 8 vertices are above/below/to the left or to the right of the screen, skip
- Else render
bool isVisible(matrix pModelViewProj) { vec3 verts[8]; vec3 min; vec3 max; // Apply our matrix to our vertices for (int i = 0; i < 8; i++) { verts[i] = pModelViewProj * mVertices[i]; // get the minimum and maximum values.. if (i==0) { min = verts[i]; max = verts[i]; } else { if (min.x > verts[i].x) min.x = verts[i].x; if (min.y > verts[i].y) min.y = verts[i].y; if (min.z > verts[i].z) min.z = verts[i].z; if (max.x < verts[i].x) max.x = verts[i].x; if (max.y < verts[i].y) max.y = verts[i].y; if (max.z < verts[i].z) max.z = verts[i].z; } if ((verts[i].x >= -1.0) && (verts[i].x <= 1.0) && (verts[i].y >= -1.0) && (verts[i].y <= 1.0) && (verts[i].z < 1.0)) { // no need to check any further, this vertice is on screen!! return true; }; }; if ((max.z < 0.0) || (min.z > 1.0)) { // all vertices are behind the camera, no need to check any further return false; } else if ((max.x < -1.0) || (min.x > 1.0)) { // all vertices are to the left or right of the screen, no need to check any further return false; } else if ((max.y < -1.0) || (min.y > 1.0)) { // all vertices are to the top or bottom of the screen, no need to check any further return false; }; // We assume any other situation is visible, if our object is somewhere just off a corner // we have a false positive return true; };
Later on we'll also use our bounding box to do occlusion testing. Here we will actually attempt to render our bounding box to test if it is behind things already rendered to the screen. But that is for another day.
Unfortunately I won't know how much impact this change will have as I found out how badly my sample city is for the purpose. I wrongly made the assumption buildings would be separated out into separate models but they are not. The breakup is such that each group within the wavefront obj file covers the entire model and 95% of the model is still being rendered even with most off screen.
I'll either have to find a better way to split the model up or abandon this model for testing. I'll have to think some more about this.
No comments:
Post a Comment