static void BSPTreeCreateRecursive(BSPTreeNode *tree, PolyListNode *pllist, #if BSPTREE_STATS int depth, #endif struct obstack *scratch) { PolyListNode *plnode, *front, *back; EdgeIntersection edges[2]; tree->front = tree->back = NULL; ListPop(pllist, plnode); tree->polylist = plnode; check_poly(plnode->poly); PolyPlane(plnode, &tree->plane); front = back = NULL; while (pllist) { ListPop(pllist, plnode); check_poly(plnode->poly); switch (ClassifyPoly(&tree->plane, plnode->poly, edges)) { case BACKOF: check_poly(plnode->poly); ListPush(back, plnode); break; case COPLANAR: check_poly(plnode->poly); ListPush(tree->polylist, plnode); break; case INFRONTOF: check_poly(plnode->poly); ListPush(front, plnode); break; case BOTH_SIDES: check_poly(plnode->poly); SplitPolyNode(plnode, &front, &back, edges, scratch); break; } } if (front) { tree->front = obstack_alloc(scratch, sizeof(*tree->front)); BSPTreeCreateRecursive(tree->front, front, #if BSPTREE_STATS depth+1, #endif scratch); } if (back) { tree->back = obstack_alloc(scratch, sizeof(*tree->back)); BSPTreeCreateRecursive(tree->back, back, #if BSPTREE_STATS depth+1, #endif scratch); } #if BSPTREE_STATS if (depth > tree_depth) { tree_depth = depth; } #endif }
/* * OpenGL (GLUT) calls this routine to display the scene */ void display() { const double len=1.5; // Length of axes // Erase the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // Enable Z-buffering in OpenGL glEnable(GL_DEPTH_TEST); // Undo previous transformations glLoadIdentity(); // Set view angle glRotatef(ph,1,0,0); glRotatef(th,0,1,0); // Decide what to draw switch (mode) { // Draw cubes case 0: cube(0,0,0 , 0.3,0.3,0.3 , 0); cube(1,0,0 , 0.2,0.2,0.2 , 45); cube(0,1,0 , 0.4,0.4,0.2 , 90); break; // Draw spheres case 1: sphere1(0,0,0 , 0.4); sphere1(1,0,0 , 0.2); sphere2(0,1,0 , 0.2); break; // Line airplane case 2: PolyPlane(GL_LINE_LOOP , 0,0,0); break; // Polygon airplane case 3: PolyPlane(GL_POLYGON , 0,0,0); break; // Three flat airplanes case 4: FlatPlane( 0.0, 0.0, 0.0); FlatPlane(-0.5, 0.5,-0.5); FlatPlane(-0.5,-0.5,-0.5); break; // Three solid airplanes case 5: SolidPlane( 0, 0, 0 , 1,0,0 , 0, 1,0); SolidPlane(-1, 1, 0 ,-1,0,0 , 0,-1,0); SolidPlane(-1,-1, 0 ,-1,0,0 , 0, 1,0); break; // Mix of objects case 6: // Cube cube(-1,0,0 , 0.3,0.3,0.3 , 3*zh); // Ball sphere1(0,0,0 , 0.3); // Solid Airplane SolidPlane(Cos(zh),Sin(zh), 0 ,-Sin(zh),Cos(zh),0 , Cos(4*zh),0,Sin(4*zh)); // Utah Teapot glPushMatrix(); glTranslatef(0,0,-1); glRotatef(zh,0,1,0); glColor3f(Cos(zh)*Cos(zh),0,Sin(zh)*Sin(zh)); glutSolidTeapot(0.5); glPopMatrix(); break; } // White glColor3f(1,1,1); // Draw axes if (axes) { glBegin(GL_LINES); glVertex3d(0.0,0.0,0.0); glVertex3d(len,0.0,0.0); glVertex3d(0.0,0.0,0.0); glVertex3d(0.0,len,0.0); glVertex3d(0.0,0.0,0.0); glVertex3d(0.0,0.0,len); glEnd(); // Label axes glRasterPos3d(len,0.0,0.0); Print("X"); glRasterPos3d(0.0,len,0.0); Print("Y"); glRasterPos3d(0.0,0.0,len); Print("Z"); } // Five pixels from the lower left corner of the window glWindowPos2i(5,5); // Print the text string Print("Angle=%d,%d",th,ph); // Render the scene glFlush(); // Make the rendered scene visible glutSwapBuffers(); }
std::vector<float4> Planes(const std::vector<float3> &verts, const std::vector<int3> &tris) { std::vector<float4> planes; for (auto &t : tris) planes.push_back(PolyPlane({verts[t[0]],verts[t[1]],verts[t[2]] })); return planes; }