Ejemplo n.º 1
0
void UrhoRenderer::Initialize()
{
    framework->RegisterRenderer(this);

    // Connect to scene change signals.
    framework->Scene()->SceneCreated.Connect(this, &UrhoRenderer::CreateGraphicsWorld);
    framework->Scene()->SceneAboutToBeRemoved.Connect(this, &UrhoRenderer::RemoveGraphicsWorld);

    // Enable the main (full-screen) viewport
    Urho3D::Renderer* rend = GetSubsystem<Urho3D::Renderer>();
    if (rend)
    {
        rend->SetNumViewports(1);
        rend->SetViewport(0, new Urho3D::Viewport(context_));

        // Track window position and screen mode changes to keep config up-to-date
        SubscribeToEvent(Urho3D::E_WINDOWPOS, URHO3D_HANDLER(UrhoRenderer, HandleScreenModeChange));
        SubscribeToEvent(Urho3D::E_SCREENMODE, URHO3D_HANDLER(UrhoRenderer, HandleScreenModeChange));
    
        // Disable shadows completely for now on mobile devices, as the shadow bias is problematic, and it consumes GPU performance
        // Also disable specular highlights for per-pixel lighting
        if (Urho3D::GetPlatform() == "Android" || Urho3D::GetPlatform() == "iOS")
        {
            rend->SetDrawShadows(false);
            rend->SetSpecularLighting(false);
        }
    }
}
Ejemplo n.º 2
0
void UrhoRenderer::Uninitialize()
{
    framework->RegisterRenderer(0);
    Urho3D::Renderer* rend = GetSubsystem<Urho3D::Renderer>();
    // Let go of the viewport that we created. If done later at Urho Context destruction time, may cause a crash
    if (rend)
        rend->SetViewport(0, nullptr);
}
Ejemplo n.º 3
0
void UrhoRenderer::SetMainCamera(Entity *mainCameraEntity)
{
    activeMainCamera = mainCameraEntity;

    Urho3D::Camera *newActiveCamera = 0;
    Camera *cameraComponent = mainCameraEntity ? mainCameraEntity->Component<Camera>().Get() : 0;
    if (cameraComponent)
        newActiveCamera = cameraComponent->UrhoCamera();
    else
    {
        activeMainCamera.Reset();
        if (mainCameraEntity)
            LogWarning("Cannot activate camera '" + mainCameraEntity->Name() + "': It does not have a Camera component!");
    }
    if (mainCameraEntity && !mainCameraEntity->ParentScene()) // If the new to-be camera is not in a scene, don't add it as active.
    {
        LogWarning("Cannot activate camera \"" + mainCameraEntity->Name() + "\": It is not attached to a scene!");
        activeMainCamera.Reset();
        newActiveCamera = 0;
    }

    if (!activeMainCamera.Lock() || !newActiveCamera)
        LogWarning("Setting main window camera to null!");

    Urho3D::Renderer* rend = GetSubsystem<Urho3D::Renderer>();
    if (!rend)
        return; // In headless mode the renderer doesn't exist

    Urho3D::Viewport* vp = rend->GetViewport(0);
    if (vp)
    {
        vp->SetCamera(newActiveCamera);
        vp->SetScene(mainCameraEntity->ParentScene()->Subsystem<GraphicsWorld>()->UrhoScene());
    }
    else
        LogWarning("Could not set active camera, no viewport defined");

    MainCameraChanged.Emit(mainCameraEntity);
}
Ejemplo n.º 4
0
void MainGameState::Start() {
    std::cout << "starting main game " << std::endl;
    Urho3D::ResourceCache *cache = GetSubsystem<Urho3D::ResourceCache>();
    // We will be needing to load resources.
    // All the resources used in this example comes with Urho3D.
    // If the engine can't find them, check the ResourcePrefixPath.

    // Seems like the mouse must be in cursor mode before creating the UI or it won't work.

    // Let's use the default style that comes with Urho3D.
    // GetSubsystem<Urho3D::UI>()->GetRoot()->AddChild(texts["FPS_text"]);
    // Let's create some text to display.
    // Add a button, just as an interactive UI sample.
    // Note, must be part of the UI system before SetSize calls!

    GetSubsystem<Urho3D::UI>()->GetRoot()->AddChild(ui_elements["side_bar"]);

    // Now we can change the mouse mode.

    // Let's setup a scene to render.
    scene_ = new Urho3D::Scene(context_);
    // Let the scene have an Octree component!
    scene_->CreateComponent<Urho3D::Octree>();
    // Let's add an additional scene component for fun.
    scene_->CreateComponent<Urho3D::DebugRenderer>();

    // Let's put some sky in there.
    // Again, if the engine can't find these resources you need to check
    // the "ResourcePrefixPath". These files come with Urho3D.
    Urho3D::Node *skyNode = scene_->CreateChild("Sky");
    skyNode->SetScale(500.0f); // The scale actually does not matter
    Urho3D::Skybox *skybox = skyNode->CreateComponent<Urho3D::Skybox>();
    skybox->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));
    skybox->SetMaterial(cache->GetResource<Urho3D::Material>("Materials/Skybox.xml"));
    int worldsize = 40;
    Urho3D::SharedPtr<Urho3D::Node> bed_rock(new Urho3D::Node(context_));
    for (int x = -worldsize; x < worldsize; x++) {
        for (int y = -worldsize; y < worldsize; y++) {
            Urho3D::Vector3 pos = Urho3D::Vector3(x, y, 0);
            EntityNode *boxNode_ = new EntityNode(context_, &m_entity_manager);
            bed_rock->AddChild(boxNode_);
            boxNode_->SetPosition(pos);
            Urho3D::StaticModel *boxObject = boxNode_->CreateComponent<Urho3D::StaticModel>();
            boxObject->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));

            boxObject->SetMaterial(cache->GetResource<Urho3D::Material>("Materials/Mushroom.xml"));
            boxObject->SetCastShadows(true);
            boxNode_->assign<terrain_type>("Stone", STONE);
        }
    }
    z_levels.push_back(bed_rock);
    for (int z = 1; z <= m_world_height; z++) {

        Urho3D::SharedPtr<Urho3D::Node> cur_level(new Urho3D::Node(context_));
        for (int x = -worldsize; x < worldsize; x++)
            for (int y = -worldsize; y < worldsize; y++) {
                /*  if (sqrt(x * x + y * y) / 30 <
                      20 +
                          4 * sin(5 * sin(6 * (glm::dot(glm::normalize(glm::vec2(x, y)),
                                                        glm::vec2(0, 1))))))           */

                double noise = m_noise_gen.noise_3D(0.02 * x, 0.002 * y, 0.02 * z) +
                               m_noise_gen.noise_3D(x, 4 * y, z);

                std::cout << 2 - ((z) / (float)m_world_height) << " "
                          << m_noise_gen.noise_2D(x, y) + 1 << std::endl;
                if ((z / (float)m_world_height) <
                    0.1 * m_noise_gen.noise_2D(0.1 * x, y * 0.1) + 1) {
                    if (noise > 0.2) {

                        Urho3D::Vector3 pos = Urho3D::Vector3(x, y, z);
                        auto lol = new TerrainFactory(context_, &m_entity_manager,cache );
                    
                        cur_level->AddChild(lol->create_stone_terrain(pos));
                         
                    } else if (true) {
                        Urho3D::Vector3 pos = Urho3D::Vector3(x, y, z);
                        EntityNode *boxNode_ = new EntityNode(context_, &m_entity_manager);
                        cur_level->AddChild(boxNode_);
                        boxNode_->SetPosition(pos);
                        Urho3D::StaticModel *boxObject =
                            boxNode_->CreateComponent<Urho3D::StaticModel>();
                        boxObject->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));

                        boxObject->SetMaterial(cache->GetResource<Urho3D::Material>(
                            "Materials/NinjaSnowWar/Snow.xml"));
                        boxObject->SetCastShadows(true);
                        boxNode_->assign<terrain_type>("Stone", STONE);
                    }
                }
            }
        z_levels.push_back(cur_level);
        if (z > m_world_height - m_world_render_depth)
            scene_->AddChild(cur_level);
    }
    std::cout << z_levels.size() << std::endl;
    // We need a camera from which the viewport can render.
    cameraNode_ = scene_->CreateChild("Camera");
    m_camera = cameraNode_->CreateComponent<Urho3D::Camera>();
    m_camera->SetFarClip(2000);
    cameraNode_->Pitch(45);
    cameraNode_->Yaw(180);
    cameraNode_->SetPosition(Urho3D::Vector3(0, 0, m_world_height + m_world_render_depth));

    // Create two lights
    {
        Urho3D::Node *lightNode = scene_->CreateChild("Light");
        lightNode->SetPosition(Urho3D::Vector3(-5, 0, m_world_height + 20));
        Urho3D::Light *light = lightNode->CreateComponent<Urho3D::Light>();
        light->SetLightType(Urho3D::LIGHT_POINT);
        light->SetRange(50);
        light->SetBrightness(1.2);
        light->SetColor(Urho3D::Color(1, .5, .8, 1));
        light->SetCastShadows(true);
    }
    // add one to the camera node as well
    // Now we setup the viewport. Ofcourse, you can have more than one!
    Urho3D::Renderer *renderer = GetSubsystem<Urho3D::Renderer>();
    Urho3D::SharedPtr<Urho3D::Viewport> viewport(
        new Urho3D::Viewport(context_, scene_, cameraNode_->GetComponent<Urho3D::Camera>()));
    renderer->SetViewport(0, viewport);

    // We subscribe to the events we'd like to handle.
    // In this example we will be showing what most of them do,
    // but in reality you would only subscribe to the events
    // you really need to handle.
    // These are sort of subscribed in the order in which the engine
    // would send the events. Read each handler method's comment for
    // details.
    std::cout << "start main done" << std::endl;
}
Ejemplo n.º 5
0
void GraphicsWorld::HandlePostRenderUpdate(StringHash /*eventType*/, VariantMap& /*eventData*/)
{
    URHO3D_PROFILE(GraphicsWorld_PostRenderUpdate);

    visibleEntities_.Clear();

    Urho3D::Renderer* renderer = GetSubsystem<Urho3D::Renderer>();
    Camera* cameraComp = renderer_->MainCameraComponent();

    if (IsActive() && renderer)
    {
        Urho3D::Camera* cam = cameraComp ? cameraComp->UrhoCamera() : nullptr;
        Urho3D::Viewport* vp = renderer->GetViewport(0);
        Urho3D::View* view = vp ? vp->GetView() : nullptr;
        if (view)
        {
            const Urho3D::PODVector<Urho3D::Drawable*>& geometries = view->GetGeometries();
            for (uint i = 0; i < geometries.Size(); ++i)
            {
                // Verify that the geometry is in main camera view, as also eg. shadow geometries get listed
                Urho3D::Drawable* dr = geometries[i];
                if (!dr || !dr->IsInView(cam))
                    continue;
                EntityWeakPtr ent(static_cast<Entity*>(dr->GetNode()->GetVar(entityLink).GetPtr()));
                if (!ent)
                    continue;
                
                visibleEntities_.Insert(ent);
            }
        }
    }

    // Perform visibility change tracking
    for (HashMap<EntityWeakPtr, bool>::Iterator i = visibilityTrackedEntities_.Begin(); i != visibilityTrackedEntities_.End();)
    {
        // Check whether entity has expired
        if (!i->first_)
            i = visibilityTrackedEntities_.Erase(i);
        else
        {
            bool current = visibleEntities_.Contains(i->first_);
            bool prev = i->second_;
            if (current != prev)
            {
                i->second_ = current;
                if (current)
                {
                    i->first_->EmitEnterView(cameraComp);
                    EntityEnterView.Emit(i->first_.Get());
                }
                else
                {
                    i->first_->EmitLeaveView(cameraComp);
                    EntityLeaveView.Emit(i->first_.Get());
                }
            }

            ++i;
        }
    }
}