int ZQuadTree::_IsInFrustum(CGeoMapData &pHeightMap, Point &bottomLeft, CFrustum& pFrustum) { bool b[4]; bool bInSphere; bInSphere = pFrustum.SphereInFrustum(bottomLeft.x + m_nCenter.x, bottomLeft.y + m_nCenter.y, 0, m_fRadius); if(!bInSphere) return FRUSTUM_OUT; b[0] = pFrustum.PointInFrustum(bottomLeft.x + m_nCorner[0].x, bottomLeft.y + m_nCorner[0].y, 0); b[1] = pFrustum.PointInFrustum(bottomLeft.x + m_nCorner[1].x, bottomLeft.y + m_nCorner[1].y, 0); b[2] = pFrustum.PointInFrustum(bottomLeft.x + m_nCorner[2].x, bottomLeft.y + m_nCorner[2].y, 0); b[3] = pFrustum.PointInFrustum(bottomLeft.x + m_nCorner[3].x, bottomLeft.y + m_nCorner[3].y, 0); if(b[0] + b[1] + b[2] + b[3] == 4) return FRUSTUM_COMPLETELY_IN; return FRUSTUM_PRTIALLY_IN; }
void RenderScene() { /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * int spheresRendered = 0; // This will hold how many spheres are being rendered char strText[255]= {0}; // This will hold the window title info glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The matrix // Give OpenGL our camera position g_Camera.Look(); // We don't need to calculate this every frame, only when the camera view changes. // I just did it every frame anyway. In this case it isn't a big deal. g_Frustum.CalculateFrustum(); // Calculate the frustum each frame // If you are unfamiliar with Quadrics, see the tutorial on Quadrics at www.GameTutorials.com. // They basically allow you to draw circles and cylinders fast and easily. GLUquadricObj *pObj = gluNewQuadric(); // Get a Quadric off the stack // Loop through all of our allowed spheres and render them to the screen if in the frustum. for(int i = 0; i < g_MaxSpheres; i++) // g_MaxSpheres varies. { g_Spheres[i].zPos += SPHERE_SPEED; // Increase the Z position of the sphere. // Below we check if the sphere needs to be draw or not. If g_bIgnoreFrustum is TRUE, // it draws it regardless (which is SLOOOooOoW). We just pass in the (X, Y, Z) // and the radius of the sphere to find out if it is inside of the frustum. if(g_bIgnoreFrustum || g_Frustum.SphereInFrustum(g_Spheres[i].xPos, g_Spheres[i].yPos, g_Spheres[i].zPos, g_Spheres[i].radius)) { // Set the sphere's color glColor3ub(g_Spheres[i].r, g_Spheres[i].g, g_Spheres[i].b); // Create a new scope before positiong the sphere so we don't effect the other spheres. glPushMatrix(); // Position the sphere on the screen at it's XYZ position. glTranslatef(g_Spheres[i].xPos, g_Spheres[i].yPos, g_Spheres[i].zPos); // Create a sphere with the desired radius chosen in the beginning. gluSphere(pObj, g_Spheres[i].radius, 20, 20); // Draw the sphere with a radius of 0.5 glPopMatrix(); // Close the scope of this matrix spheresRendered++; // Increase the amount of spheres rendered } // Here we check to see if the sphere went out of our range, // If so, we need to set it back again with a new random position. if(g_Spheres[i].zPos > MAX_DISTANCE) { // Give the sphere a new random position back in the beginning. g_Spheres[i].xPos = (rand() % (MAX_DISTANCE * 10)) * 0.1f; g_Spheres[i].yPos = (rand() % (MAX_DISTANCE * 10)) * 0.1f; g_Spheres[i].zPos = -MAX_DISTANCE; // Send it to the back again. // Give a 50/50 chance for the sphere to be to the left/right or above/below the XY axis. // This is because we are centered at the origin if(rand() % 2) g_Spheres[i].xPos = -g_Spheres[i].xPos; if(rand() % 2) g_Spheres[i].yPos = -g_Spheres[i].yPos; } } // Since I didn't want to add more code for a rendered font, I decided to just // render the frustum information in the title bar of the window. // The information tells you how many spheres were rendered and out of how many. // Use +/- to increase and decrease the max spheres tested. sprintf(strText, "www.GameTutorials.com - Spheres Rendered: %d / %d", spheresRendered, g_MaxSpheres); SetWindowText(g_hWnd, strText); // Change the window title bar /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * SwapBuffers(g_hDC); // Swap the backbuffers to the foreground gluDeleteQuadric(pObj); // Free the Quadric }