HRESULT cMyHost::InitDeviceObjects() { cGameHost::InitDeviceObjects(); // generate a random height map m_pHeightMap = displayManager() .texturePool() .createResource(cString("height map")); m_pHeightMap->createTexture( 256, 256, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED); m_pHeightMap->generatePerlinNoise( 0.03f, 5, 0.6f); // create a terrain from this map m_terrainSystem.create( &rootNode(), m_pHeightMap, worldExtents(), 3); // load our render method m_pRenderMethod = TheGameHost .displayManager() .renderMethodPool() .createResource("terrain method"); m_pRenderMethod->loadEffect( 0, "media\\shaders\\simple_terrain.fx"); m_pRenderMethod->loadEffect( 1, "media\\shaders\\simple_terrain_ambient.fx"); m_pRenderMethod->loadEffect( 2, "media\\shaders\\simple_terrain_bump.fx"); m_pRenderMethod->loadEffect( 3, "media\\shaders\\simple_terrain_sunlight.fx"); // generate three elevation structures cTerrain::elevationData elevation[3]; // grass (all elevations and slopes) elevation[0].minElevation = -500; elevation[0].maxElevation = 1000; elevation[0].minNormalZ = -1.0f; elevation[0].maxNormalZ = 1.0f; elevation[0].strength = 1.0f; // rock (all elevations, steep slopes) elevation[1].minElevation = 0; elevation[1].maxElevation = 1500; elevation[1].minNormalZ = 0.0f; elevation[1].maxNormalZ = 0.85f; elevation[1].strength = 10.0f; // dirt (high elevation, flat slope) elevation[2].minElevation = 800; elevation[2].maxElevation = 1500; elevation[2].minNormalZ = 0.75f; elevation[2].maxNormalZ = 1.0f; elevation[2].strength = 20.0f; // generate the blend image cImage* pBlendImage; pBlendImage = displayManager() .imagePool() .createResource(cString("image map 3")); pBlendImage->create( 256, 256, cImage::k_32bit); m_terrainSystem.generateBlendImage( pBlendImage, elevation, 3); pBlendImage->randomChannelNoise(3, 200, 255); // upload the blend image to a texture m_pBlendMap = displayManager() .texturePool() .createResource(cString("image map")); m_pBlendMap->createTexture( 256, 256, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED); m_pBlendMap->uploadImage(pBlendImage); // now convert the blend image to // a mask for bump-mapping. First, clear // the alpha channel back to zero pBlendImage->setChannel(3,0); pBlendImage->convertToBumpMask(); // create the bump mask // texture and upload m_pBumpMask = displayManager() .texturePool() .createResource(cString("image mask")); m_pBumpMask->createTexture( 256, 256, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED); m_pBumpMask->uploadImage(pBlendImage); // we are now done with the blend image safe_release(pBlendImage); // load the ground surface materials // create a surface material // and load our textures into it m_pColorMaterial = displayManager() .surfaceMaterialPool() .loadResource("media\\materials\\terrain.mat"); m_pColorMaterial->setTexture(0, m_pBlendMap); m_pBumpMaterial = displayManager() .surfaceMaterialPool() .loadResource("media\\materials\\terrain_bump.mat"); m_pBumpMaterial->setTexture(0, m_pBumpMask); m_pRenderMethod->setMaterial( 0, m_pColorMaterial); m_pRenderMethod->setMaterial( 1, m_pColorMaterial); m_pRenderMethod->setMaterial( 2, m_pBumpMaterial); m_pRenderMethod->setMaterial( 3, m_pColorMaterial); // give the final render method to the terrain m_terrainSystem.setRenderMethod(m_pRenderMethod); // generate the sky box m_skyDome.create(cSkyModel::k_hemisphere); m_cloudLayer.create(cSkyModel::k_dome); m_pSkyMethod = TheGameHost .displayManager() .renderMethodPool() .createResource("sky box method"); m_pSkyMethod->loadEffect( cRenderMethod::k_defaultMethod, "media\\shaders\\sunlit_skydome.fx"); m_pCloudMethod = TheGameHost .displayManager() .renderMethodPool() .createResource("sky cloud method"); m_pCloudMethod->loadEffect( cRenderMethod::k_defaultMethod, "media\\shaders\\animated_clouds.fx"); m_pSkyClouds = TheGameHost .displayManager() .texturePool() .createResource("sky cloud texture"); m_pSkyClouds->loadResource("media\\textures\\clouds.dds"); m_pSkyMaterial = TheGameHost .displayManager() .surfaceMaterialPool() .createResource("sky box material"); m_pSkyMaterial->setTexture(0, m_pSkyClouds); m_pCloudMethod->setMaterial( 0, m_pSkyMaterial); m_skyDome.setRenderMethod(m_pSkyMethod); m_cloudLayer.setRenderMethod(m_pCloudMethod); // force-update the camera to make sure // we start above the terrain defaultCamera().update(); updateCamera( 10.0f, 0.1f, &m_terrainSystem, 75.0f, true); //<- true forces an update m_lensFlare.create(); return S_OK; }
HRESULT cMyHost::InitDeviceObjects() { cGameHost::InitDeviceObjects(); // generate a random height map m_pHeightMap = displayManager() .texturePool() .createResource(cString("height map")); m_pHeightMap->createTexture( 128, 128, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED); m_pHeightMap->generatePerlinNoise( 0.01f, 5, 0.6f); // create a terrain from this map m_terrainSystem.create( &rootNode(), m_pHeightMap, worldExtents(), 3); // load our render method m_pRenderMethod = TheGameHost .displayManager() .renderMethodPool() .createResource("terrain method"); m_pRenderMethod->loadEffect( cRenderMethod::k_defaultMethod, "media\\shaders\\simple_terrain.fx"); // generate three elevation structures cTerrain::elevationData elevation[3]; // grass (all elevations and slopes) elevation[0].minElevation = 0; elevation[0].maxElevation = 500; elevation[0].minNormalZ = -1.0f; elevation[0].maxNormalZ = 1.0f; elevation[0].strength = 1.0f; // rock (all elevations, steep slopes) elevation[1].minElevation = 0; elevation[1].maxElevation = 500; elevation[1].minNormalZ = 0.0f; elevation[1].maxNormalZ = 0.85f; elevation[1].strength = 10.0f; // dirt (high elevation, flat slope) elevation[2].minElevation = 300; elevation[2].maxElevation = 500; elevation[2].minNormalZ = 0.75f; elevation[2].maxNormalZ = 1.0f; elevation[2].strength = 20.0f; // generate the blend image cImage* pBlendImage; pBlendImage = displayManager() .imagePool() .createResource(cString("image map 3")); pBlendImage->create( 256, 256, cImage::k_32bit); m_terrainSystem.generateBlendImage( pBlendImage, elevation, 3); pBlendImage->randomChannelNoise(3, 200, 255); // upload the blend image to a texture m_pBlendMap = displayManager() .texturePool() .createResource(cString("image map")); m_pBlendMap->createTexture( 256, 256, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED); m_pBlendMap->uploadImage(pBlendImage); safe_release(pBlendImage); // load the ground surface textures m_pGrass = displayManager() .texturePool() .createResource(cString("grass")); m_pRock = displayManager() .texturePool() .createResource(cString("rock")); m_pDirt = displayManager() .texturePool() .createResource(cString("dirt")); m_pGrass->loadResource( "media\\textures\\grass.dds"); m_pRock->loadResource( "media\\textures\\rock.dds"); m_pDirt->loadResource( "media\\textures\\dirt.dds"); // create a surface material // and load our textures into it m_pSurfaceMaterial = displayManager() .surfaceMaterialPool() .createResource(cString("ground material")); m_pSurfaceMaterial->setTexture(0, m_pBlendMap); m_pSurfaceMaterial->setTexture(1, m_pGrass); m_pSurfaceMaterial->setTexture(2, m_pRock); m_pSurfaceMaterial->setTexture(3, m_pDirt); // give the render method and material to the terrain m_pRenderMethod->setMaterial(0, m_pSurfaceMaterial); m_terrainSystem.setRenderMethod(m_pRenderMethod); // force-update the camera to make sure // we start above the terrain defaultCamera().update(); updateCamera( 10.0f, 0.1f, &m_terrainSystem, 30.0f, true); //<- true forces an update return S_OK; }