void CBuilding::CreateTower () { int left, right, front, back, bottom; int section_height, section_width, section_depth; int remaining_height; int ledge_height; int tier_fraction; int grouping; int foundation; int narrowing_interval; int tiers; float ledge; float uv_start; bool blank_corners; bool roof_spike; bool tower; //How much ledges protrude from the building ledge = (float)RandomVal (3) * 0.25f; //How tall the ledges are, in stories ledge_height = RandomVal (4) + 1; //How the windows are grouped grouping = RandomVal (3) + 2; //if the corners of the building have no windows blank_corners = RandomVal (4) > 0; //if the roof is pointed or has infrastructure on it roof_spike = RandomVal (3) == 0; //What fraction of the remaining height should be given to each tier tier_fraction = 2 + RandomVal (4); //How often (in tiers) does the building get narrorwer? narrowing_interval = 1 + RandomVal (10); //The height of the windowsless slab at the bottom foundation = 2 + RandomVal (3); //The odds that we'll have a big fancy spikey top tower = RandomVal (5) != 0 && _height > 40; //set our initial parameters left = _x; right = _x + _width; front = _y; back = _y + _depth; bottom = 0; tiers = 0; //build the foundations. ConstructCube ((float)left - ledge, (float)right + ledge, (float)front - ledge, (float)back + ledge, (float)bottom, (float)foundation); bottom += foundation; //now add tiers until we reach the top while (1) { remaining_height = _height - bottom; section_depth = back - front; section_width = right - left; section_height = MAX (remaining_height / tier_fraction, 2); if (remaining_height < 10) section_height = remaining_height; //Build the four walls uv_start = (float)RandomVal (SEGMENTS_PER_TEXTURE) / SEGMENTS_PER_TEXTURE; uv_start = ConstructWall (left, bottom, back, SOUTH, section_depth, section_height, grouping, uv_start, blank_corners) - ONE_SEGMENT; uv_start = ConstructWall (left, bottom, front, EAST, section_width, section_height, grouping, uv_start, blank_corners) - ONE_SEGMENT; uv_start = ConstructWall (right, bottom, front, NORTH, section_depth, section_height, grouping, uv_start, blank_corners) - ONE_SEGMENT; uv_start = ConstructWall (right, bottom, back, WEST, section_width, section_height, grouping, uv_start, blank_corners) - ONE_SEGMENT; bottom += section_height; //Build the slab / ledges to cap this section. if (bottom + ledge_height > _height) break; ConstructCube ((float)left - ledge, (float)right + ledge, (float)front - ledge, (float)back + ledge, (float)bottom, (float)(bottom + ledge_height)); bottom += ledge_height; if (bottom > _height) break; tiers++; if ((tiers % narrowing_interval) == 0) { if (section_width > 7) { left+=1; right-=1; } if (section_depth > 7) { front+=1; back-=1; } } } ConstructRoof ((float)left, (float)right, (float)front, (float)back, (float)bottom); _mesh->Compile (); _mesh_flat->Compile (); }
/** * Construct the scene */ void ConstructScene() { // -------------------- Geometry -------------------- // movingSquare = new UnitSquareFlatSurface(200,32.2f*.814f,true,Vector2(-.01f,0),.03f); SceneNode* house = ConstructHouse(70,90); // Construct a unit cylinder surface WasherSurface* cylinder = new WasherSurface(.9f, 1.0f, 18); //Wheel WasherSurface* washer = new WasherSurface(10,20,14); std::vector<LightNode*> lights; lights.push_back(new LightNode(GL_LIGHT3)); lights.push_back(new LightNode(GL_LIGHT4)); wheel = new WheelNode(10,20,washer,ConstructBox(9,19.5f,1),8,4,lights); waterfall = new ParticleNode(25); //-------------------- Materials ------------------------- // // Wood PresentationNode* wood = new PresentationNode; wood->SetMaterialAmbientAndDiffuse(Color4(0.5f, 0.5f, 0.5f)); wood->SetMaterialSpecular(Color4(0.3f, 0.3f, 0.3f)); wood->SetMaterialShininess(32.0f); wood->setTexture(LoadTextureBMP("images/wood.bin",true,128)); // Stone PresentationNode* stone = new PresentationNode; stone->SetMaterialAmbient(Color4(0.19225f, 0.19225f, 0.19225f)); stone->SetMaterialDiffuse(Color4(0.50754f, 0.50754f, 0.50754f)); stone->SetMaterialSpecular(Color4(0.508273f, 0.508273f, 0.508273f)); stone->SetMaterialShininess(10.2f); stone->setTexture(LoadTextureBMP("images/stone.bin",true,128)); PresentationNode* steel = new PresentationNode; steel->SetMaterialAmbient(Color4(0.19225f, 0.19225f, 0.19225f)); steel->SetMaterialDiffuse(Color4(0.7f, 0.7f, 0.7f)); steel->SetMaterialSpecular(Color4(0.7f, 0.7f, 0.7f)); steel->SetMaterialShininess(90.2f); // Water PresentationNode* water = new PresentationNode; water->SetMaterialAmbient(Color4(0.01f, 0.01f, 0.1f)); water->SetMaterialDiffuse(Color4(0.2f, 0.2f, 0.6f)); water->SetMaterialSpecular(Color4(0.5f, 0.5f, 0.5f)); water->SetMaterialShininess(10); water->setTexture(LoadTextureBMP("images/water.bin",true,128)); PresentationNode* blue = new PresentationNode; blue->SetMaterialAmbient(Color4(0.01f, 0.01f, 0.1f)); blue->SetMaterialDiffuse(Color4(0.2f, 0.2f, 0.6f)); blue->SetMaterialSpecular(Color4(0.5f, 0.5f, 0.5f)); blue->SetMaterialShininess(10); // ------------------ Transformations ------------------- // TransformNode* wheelTransform = new TransformNode; wheelTransform->Translate(-80.0f, 35.0f, 40.0f); wheelTransform->Rotate(90.0f, 1.0f, 0.0f, 0.0f); TransformNode* houseTransform = new TransformNode; houseTransform->Translate(-100,35,0); TransformNode* house2Transform = new TransformNode; house2Transform->Rotate(180,0,0,1); house2Transform->Translate(-100,-35,0); TransformNode* wallTransform = new TransformNode; wallTransform->Translate(0,35,0); TransformNode* waterfallTransform = new TransformNode; waterfallTransform->Translate(-128.0f,35,15.0f); TransformNode* pipeTransform = new TransformNode; pipeTransform->Rotate(-90,0,1,0); pipeTransform->Translate(76.5f,35,125); pipeTransform->Scale(5,5,20); // -------------------- Lighting --------------------------/ // Light 0 - point light source in back right corner LightNode* light0 = new LightNode(GL_LIGHT0); light0->SetDiffuse(Color4(0.5f, 0.5f, 0.5f, 1.0f)); light0->SetSpecular(Color4(0.5f, 0.5f, 0.5f, 1.0f)); light0->SetPosition(HPoint3(90.0f, 90.0f, 30.f, 1.0f)); light0->Enable(); // Light1 - directional light from the ceiling LightNode* light1 = new LightNode(GL_LIGHT1); light1->SetDiffuse(Color4(0.7f, 0.7f, 0.7f, 1.0f )); light1->SetSpecular(Color4(0.7f, 0.7f, 0.7f, 1.0f)); light1->SetPosition(HPoint3(0.0f, 0.0f, 1.0f, 0.0f)); light1->Enable(); // Light2 - spotlight - we will place at the camera location // shining along -VPN LightNode* light2 = new LightNode(GL_LIGHT2); light2->SetDiffuse(Color4(0.8f, 0.8f, 0.8f, 1.0f )); light2->SetSpecular(Color4(0.8f, 0.8f, 0.8f, 1.0f)); light2->SetPosition(HPoint3(0.0f, 0.0f, 0.0f, 1.0f)); light2->SetSpotlight(Vector3(0.0f, 0.0f, -1.0f), 32.0f, 30.0f); light2->Enable(); lights[0]->SetDiffuse(Color4(0.8f, 0.8f, 0.8f, 1.0f)); lights[0]->SetSpecular(Color4(0.8f, 0.8f, 0.8f, 1.0f)); lights[0]->SetPosition(HPoint3(-100.0f,35.0f,90.0f,1.0f)); lights[0]->SetSpotlightDirection(Vector3(0,0,-1)); lights[0]->SetSpotlight(Vector3(0,0,-1),1,90); lights[0]->Disable(); lights[1]->SetDiffuse(Color4(0.8f, 0.8f, 0.8f, 1.0f)); lights[1]->SetSpecular(Color4(0.8f, 0.8f, 0.8f, 1.0f)); lights[1]->SetPosition(HPoint3(100.0f,35.0f,90.0f,1.0f)); lights[1]->SetSpotlightDirection(Vector3(0,0,-1)); lights[1]->SetSpotlight(Vector3(0,0,-1),1,90); lights[1]->Disable(); // --------------------------- Camera ----------------------- // MyCamera = new CameraNode; MyCamera->SetPosition(Point3(100.0f, -100.0f, 50.0f)); MyCamera->SetLookAtPt(Point3(0.0f, 0.0f, 50.0f)); MyCamera->SetViewUp(Vector3(0.0, 0.0, 1.0)); MyCamera->SetPerspective(50.0, 1.0, 1.0, 2400); // --------------------- Scene construction ----------------- // // Construct the scene root node SceneRoot = new SceneNode; // Create a scene node to hold all scene objects (other than camera // and lights) SceneNode* myScene = new SceneNode; // Add the spotlight as the first child of the root node. Since this is // accessed before the camera it will position the light relative to the // camera SceneRoot->AddChild(light2); // Set the camera SceneRoot->AddChild(MyCamera); MyCamera->AddChild(light0); MyCamera->AddChild(light1); MyCamera->AddChild(lights[0]); MyCamera->AddChild(lights[1]); MyCamera->AddChild(myScene); // Construct the room (walls, floor, ceiling) SceneNode* skybox = ConstructRoom(); wallTransform->AddChild(ConstructWall(stone,water,movingSquare)); myScene->AddChild(wallTransform); // Construct the wheel AddSubTree(myScene,wood,wheelTransform,wheel); // Place 2 Houses wood->AddChild(houseTransform); houseTransform->AddChild(house); wood->AddChild(house2Transform); house2Transform->AddChild(house); myScene->AddChild(skybox); AddSubTree(myScene,steel,pipeTransform,cylinder); AddSubTree(myScene,blue,waterfallTransform,waterfall); }
void CBuilding::CreateBlocky () { int min_height; int left, right, front, back; int max_left, max_right, max_front, max_back; int height; int mid_x, mid_z; int half_depth, half_width; int tiers; int max_tiers; int grouping; float lid_height; float uv_start; bool skip; bool blank_corners; //Choose if the corners of the building are to be windowless. blank_corners = COIN_FLIP; //Choose a random column on our texture; uv_start = (float)RandomVal (SEGMENTS_PER_TEXTURE) / SEGMENTS_PER_TEXTURE; //Choose how the windows are grouped grouping = 2 + RandomVal (4); //Choose how tall the lid should be on top of each section lid_height = (float)(RandomVal (3) + 1); //find the center of the building. mid_x = _x + _width / 2; mid_z = _y + _depth / 2; max_left = max_right = max_front = max_back = 1; height = _height; min_height = _height / 2; min_height = 3; half_depth = _depth / 2; half_width = _width / 2; tiers = 0; if (_height > 40) max_tiers = 15; else if (_height > 30) max_tiers = 10; else if (_height > 20) max_tiers = 5; else if (_height > 10) max_tiers = 2; else max_tiers = 1; //We begin at the top of the building, and work our way down. //Viewed from above, the sections of the building are randomly sized //rectangles that ALWAYS include the center of the building somewhere within //their area. while (1) { if (height < min_height) break; if (tiers >= max_tiers) break; //pick new locationsfor our four outer walls left = (RandomVal () % half_width) + 1; right = (RandomVal () % half_width) + 1; front = (RandomVal () % half_depth) + 1; back = (RandomVal () % half_depth) + 1; skip = false; //At least ONE of the walls must reach out beyond a previous maximum. //Otherwise, this tier would be completely hidden within a previous one. if (left <= max_left && right <= max_right && front <= max_front && back <= max_back) skip = true; //If any of the four walls is in the same position as the previous max,then //skip this tier, or else the two walls will end up z-fightng. if (left == max_left || right == max_right || front == max_front || back == max_back) skip = true; if (!skip) { //if this is the top, then put some lights up here max_left = MAX (left, max_left); max_right = MAX (right, max_right); max_front = MAX (front, max_front); max_back = MAX (back, max_back); //Now build the four walls of this part uv_start = ConstructWall (mid_x - left, 0, mid_z + back, SOUTH, front + back, height, grouping, uv_start, blank_corners) - ONE_SEGMENT; uv_start = ConstructWall (mid_x - left, 0, mid_z - front, EAST, right + left, height, grouping, uv_start, blank_corners) - ONE_SEGMENT; uv_start = ConstructWall (mid_x + right, 0, mid_z - front, NORTH, front + back, height, grouping, uv_start, blank_corners) - ONE_SEGMENT; uv_start = ConstructWall (mid_x + right, 0, mid_z + back, WEST, right + left, height, grouping, uv_start, blank_corners) - ONE_SEGMENT; if (!tiers) ConstructRoof ((float)(mid_x - left), (float)(mid_x + right), (float)(mid_z - front), (float)(mid_z + back), (float)height); else //add a flat-color lid onto this section ConstructCube ((float)(mid_x - left), (float)(mid_x + right), (float)(mid_z - front), (float)(mid_z + back), (float)height, (float)height + lid_height); height -= (RandomVal () % 10) + 1; tiers++; } height--; } ConstructCube (mid_x - half_width, mid_x + half_width, mid_z - half_depth, mid_z + half_depth, 0, 2); _mesh->Compile (); _mesh_flat->Compile (); }