예제 #1
0
//-------------------------------------------------------
void Ground::LoadFromHeightMap(std::shared_ptr<Ogre::Image> hmap, Ogre::SceneNode* parentNode)
{
    mImage = hmap;
    Ogre::Box box = Ogre::Box(0, 0, mImage->getWidth(), mImage->getHeight());

    mRootNode = parentNode->createChildSceneNode();
    
    mGlobalBoundingBox.setNull();

    Ogre::Material* groundMaterial = CreateGroundMaterialTextured("Material/" + CLASS_NAME + "/Textured", mImage.get());
    
    static const float VERTEX_STEP = 1.0f;
    static const float HEIGHT_STEP = 8.0f;

    float offsetX = GROUND_SIZE * VERTEX_STEP / 2.0f;
    float offsetY = GROUND_SIZE * VERTEX_STEP / 2.0f;

    size_t width  = mImage->getWidth();
    size_t height = mImage->getHeight();

    float texStep = 1.0f / REGIONS_NUMBER;

    size_t texRegionWidth  = static_cast<size_t>(std::ceil(static_cast<float>(width) / REGIONS_NUMBER));
    size_t texRegionHeight = static_cast<size_t>(std::ceil(static_cast<float>(height) / REGIONS_NUMBER));
    for (size_t y = 0; y < REGIONS_NUMBER; ++y)
    {
        size_t top = y * texRegionHeight;
        for (size_t x = 0; x < REGIONS_NUMBER; ++x)
        {
            size_t left = x * texRegionWidth;
            Ogre::Box roi = Ogre::Box(left, height - std::min(top + texRegionHeight + 1, height), std::min(left + texRegionWidth + 1, width), height - top);
                
            Ogre::MeshPtr mesh = CreateRegion(y * REGIONS_NUMBER + x, groundMaterial->getName(), roi,
                Ogre::Vector3(x * VERTEX_STEP * REGION_SIZE - offsetX, y * VERTEX_STEP * REGION_SIZE - offsetY, 0.0f),
                Ogre::Vector3(VERTEX_STEP, VERTEX_STEP, HEIGHT_STEP),
                Ogre::Vector2(x * texStep, 1.0f - (y + 1) * texStep));

            Ogre::Entity* entity = mSceneManager->createEntity(mesh);
            
            auto node = mRootNode->createChildSceneNode();
            node->attachObject(entity);
            node->showBoundingBox(true);

            mGlobalBoundingBox.merge(entity->getBoundingBox());

            mEntities.push_back(entity);
        }
    }
}