Esempio n. 1
0
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    SetUpCurrentDirectory();

    //create window
    CustomWindow* pWindow = new CustomWindow;
    pWindow->setSize(800, 600);
    pWindow->setTitle(L"NFEngine Demo - Initializing engine...");
    pWindow->open();

    //initialize engine
    if (EngineInit() != Result::OK)
        return 1;

    Demo_InitEditorBar();

    //create scene and camera
    g_pScene = EngineCreateScene();

    // --------------------------------
    // Build scene
    // --------------------------------

#ifdef DEMO_SEC_CAMERA_ON
    InitSecondaryCamera();
#endif

    //set ambient & background color
    EnviromentDesc envDesc;
    envDesc.ambientLight = Vector(0.02f, 0.03f, 0.04f, 0.0f);
    //envDesc.m_BackgroundColor = Vector(0.04f, 0.05f, 0.07f, 0.03f);
    envDesc.backgroundColor = Vector(0.02f, 0.03f, 0.04f, 0.01f);
    g_pScene->SetEnvironment(&envDesc);

    CollisionShape* pFloorShape = ENGINE_GET_COLLISION_SHAPE("shape_floor");
    pFloorShape->SetCallbacks(OnLoadCustomShapeResource, NULL);
    pFloorShape->Load();
    pFloorShape->AddRef();

    CollisionShape* pFrameShape = ENGINE_GET_COLLISION_SHAPE("shape_frame");
    pFrameShape->SetCallbacks(OnLoadCustomShapeResource, NULL);
    pFrameShape->Load();
    pFrameShape->AddRef();

    CollisionShape* pBoxShape = ENGINE_GET_COLLISION_SHAPE("shape_box");
    pBoxShape->SetCallbacks(OnLoadCustomShapeResource, NULL);
    pBoxShape->Load();
    pBoxShape->AddRef();

    CollisionShape* pBarrelShape = ENGINE_GET_COLLISION_SHAPE("shape_barrel");
    pBarrelShape->SetCallbacks(OnLoadCustomShapeResource, NULL);
    pBarrelShape->Load();
    pBarrelShape->AddRef();

    CollisionShape* pChamberShape = ENGINE_GET_COLLISION_SHAPE("chamber_collision_shape.nfcs");
    pChamberShape->Load();
    pChamberShape->AddRef();


    pWindow->InitCamera();
    pWindow->setTitle(L"NFEngine Demo");


#ifdef SCENE_MINECRAFT
    // SUNLIGHT
    Entity* pDirLightEnt = g_pScene->CreateEntity();
    XOrientation orient;
    orient.x = Vector(0.0f, -0.0f, -0.0f, 0.0f);
    orient.z = Vector(-1.5f, -1.0f, 0.5f, 0.0f);
    orient.y = Vector(0.0f, 1.0f, 0.0f, 0.0f);
    pDirLightEnt->SetOrientation(&orient);
    DirLightDesc dirLight;
    dirLight.m_Far = 100.0f;
    dirLight.m_Splits = 4;
    dirLight.m_LightDist = 1000.0f;

    LightComponent* pDirLight = new LightComponent(pDirLightEnt);
    pDirLight->SetDirLight(&dirLight);
    pDirLight->SetColor(Float3(2.2, 2, 1.8));
    pDirLight->SetShadowMap(1024);

    // MINECRAFT
    Entity* pEnt = g_pScene->CreateEntity();
    pEnt->SetPosition(Vector(0, -70.0f, 0));

    MeshComponent* pMesh = new MeshComponent(pEnt);
    pMesh->SetMeshResource("minecraft.nfm");
#endif

#ifdef SCENE_SPONZA
    // SPONZA
    Entity* pEnt = g_pScene->CreateEntity();
    pEnt->SetPosition(Vector(0, 0, 0));

    MeshComponent* pMesh = new MeshComponent(pEnt);
    pMesh->SetMeshResource("sponza.nfm");

    CollisionShape* pSponzaShape = ENGINE_GET_COLLISION_SHAPE("sponza_collision_shape.nfcs");
    //pSponzaShape->Load();

    BodyComponent* pFloorBody = new BodyComponent(pEnt);
    pFloorBody->EnablePhysics(pSponzaShape);
    pFloorBody->SetMass(0.0);

    pEnt = g_pScene->CreateEntity();
    pEnt->SetPosition(Vector(0.0f, 3.5f, 0.0f));
    LightComponent* pLight = new LightComponent(pEnt);
    OmniLightDesc omni;
    omni.m_ShadowFadeStart = 12.0f;
    omni.m_ShadowFadeEnd = 120.0f;
    omni.m_Radius = 90.0f;
    pLight->SetOmniLight(&omni);
    pLight->SetColor(Float3(50, 50, 50));
    pLight->SetShadowMap(512);
    /*
    // SUNLIGHT
    Entity* pDirLightEnt = g_pScene->CreateEntity();
    XOrientation orient;
    orient.x = Vector(0.0f, -0.0f, -0.0f, 0.0f);
    orient.z = Vector(0.1, -2.3f, 1.05, 0.0f);
    orient.y = Vector(0.0f, 1.0f, 0.0f, 0.0f);
    pDirLightEnt->SetOrientation(&orient);
    DirLightDesc dirLight;
    dirLight.m_Far = 100.0f;
    dirLight.m_Splits = 4;
    dirLight.m_LightDist = 1000.0;

    LightComponent* pDirLight = new LightComponent(pDirLightEnt);
    pDirLight->SetDirLight(&dirLight);
    pDirLight->SetColor(Float3(2.2, 1.3, 0.8));
    pDirLight->SetShadowMap(2048);
    */
#endif

//performance test (many objects and shadowmaps)
#ifdef SCENE_SEGMENTS_PERF_TEST
    for (int x = -4; x < 5; x++)
    {
        for (int z = -4; z < 5; z++)
        {
            Entity* pEntity = g_pScene->CreateEntity();
            pEntity->SetPosition(12.0f * Vector((float)x, 0, (float)z));
            MeshComponent* pMesh = new MeshComponent(pEntity);
            pMesh->SetMeshResource("chamber.nfm");
            BodyComponent* pBody = new BodyComponent(pEntity);
            pBody->EnablePhysics(pChamberShape);


            LightComponent* pLight;
            OmniLightDesc omni;
            pEntity = g_pScene->CreateEntity();
            pEntity->SetPosition(12.0f * Vector(x, 0, z) + Vector(0.0f, 3.5f, 0.0f));
            pLight = new LightComponent(pEntity);

            omni.shadowFadeStart = 80.0f;
            omni.shadowFadeEnd = 120.0f;
            omni.radius = 8.0f;
            pLight->SetOmniLight(&omni);
            pLight->SetColor(Float3(50, 50, 50));
            pLight->SetShadowMap(32);


            pEntity = g_pScene->CreateEntity();
            pEntity->SetPosition(12.0f * Vector(x, 0, z) + Vector(6.0f, 1.8f, 0.0f));
            pLight = new LightComponent(pEntity);
            omni.radius = 3.0f;
            pLight->SetOmniLight(&omni);
            pLight->SetColor(Float3(5.0f, 0.5f, 0.25f));

            pEntity = g_pScene->CreateEntity();
            pEntity->SetPosition(12.0f * Vector(x, 0, z) + Vector(0.0f, 1.8f, 6.0f));
            pLight = new LightComponent(pEntity);
            omni.radius = 3.0f;
            pLight->SetOmniLight(&omni);
            pLight->SetColor(Float3(5.0f, 0.5f, 0.25f));


            /*
            for (int i = -3; i<=3; i++)
            {
            for (int j = 0; j<4; j++)
            {
                for (int k = -3; k<=3; k++)
                {
                    Entity* pCube = g_pScene->CreateEntity();
                    pCube->SetPosition(12.0f * Vector(x,0,z) + 0.6f * Vector(i,j,k) + Vector(0.0f, 0.25f, 0.0f));

                    MeshComponent* pMesh = new MeshComponent(pCube);
                    pMesh->SetMeshResource("cube.nfm");

                    BodyComponent* pBody = new BodyComponent(pCube);
                    pBody->SetMass(0.0f);
                    pBody->EnablePhysics((CollisionShape*)Engine_GetResource(Mesh::COLLISION_SHAPE, "shape_box"));
                }
            }
            }*/
        }
    }
#endif

// infinite looped scene
#ifdef SCENE_SEGMENTS
    BufferOutputStream segmentDesc;

    Matrix mat = MatrixRotationNormal(Vector(0, 1, 0), NFE_MATH_PI / 4.0f);

    // create segments description buffer
    {
        OmniLightDesc omni;
        LightComponent* pLight;
        Entity entity;
        entity.SetPosition(Vector());
        //pEntity->SetMatrix(mat);
        MeshComponent* pMesh = new MeshComponent(&entity);
        pMesh->SetMeshResource("chamber.nfm");
        BodyComponent* pBody = new BodyComponent(&entity);
        pBody->EnablePhysics(pChamberShape);
        entity.Serialize(&segmentDesc, Vector());

        entity.RemoveAllComponents();
        entity.SetPosition(Vector(0.0f, 3.5f, 0.0f));
        /*
            pLight = new LightComponent(&entity);
            omni.m_ShadowFadeEnd = 12.0f;
            omni.m_ShadowFadeStart = 8.0f;
            omni.m_Radius = 9.0f;
            pLight->SetOmniLight(&omni);
            pLight->SetColor(Float3(50, 50, 50));
            pLight->SetShadowMap(512);
                entity.Serialize(&segmentDesc, Vector());
                */
        entity.RemoveAllComponents();
        entity.SetPosition(Vector(6.0f, 1.8f, 0.0f));
        pLight = new LightComponent(&entity);
        omni.m_Radius = 3.0f;
        pLight->SetOmniLight(&omni);
        pLight->SetColor(Float3(5.0f, 0.5f, 0.25f));
        entity.Serialize(&segmentDesc, Vector());

        entity.RemoveAllComponents();
        entity.SetPosition(Vector(0.0f, 1.8f, 6.0f));
        pLight = new LightComponent(&entity);
        omni.m_Radius = 3.0f;
        pLight->SetOmniLight(&omni);
        pLight->SetColor(Float3(5.0f, 0.5f, 0.25f));
        entity.Serialize(&segmentDesc, Vector());
    }

#define SEG_AXIS_NUM 12

    Segment* pSegments[SEG_AXIS_NUM][SEG_AXIS_NUM];

    // create segments array
    for (int i = 0; i < SEG_AXIS_NUM; i++)
    {
        for (int j = 0; j < SEG_AXIS_NUM; j++)
        {
            char segName[32];
            sprintf_s(segName, "seg_%i_%i", i, j);
            pSegments[i][j] = g_pScene->CreateSegment(segName, Vector(5.99f, 1000.0f, 5.99f));
            pSegments[i][j]->AddEntityFromRawBuffer(segmentDesc.GetData(), segmentDesc.GetSize());
        }
    }

    // create links
    for (int x = 0; x < SEG_AXIS_NUM; x++)
    {
        for (int z = 0; z < SEG_AXIS_NUM; z++)
        {
            //make inifinite loop
            for (int depth = 1; depth <= 5; depth++)
            {
                g_pScene->CreateLink(pSegments[x][z], pSegments[(x + depth) % SEG_AXIS_NUM][z],
                                     Vector(depth * 12.0f, 0.0f, 0.0f));
                g_pScene->CreateLink(pSegments[x][z], pSegments[x][(z + depth) % SEG_AXIS_NUM], Vector(0.0, 0.0f,
                                     depth * 12.0f));
            }
        }
    }

    // Set focus
    g_pScene->SetFocusSegment(pSegments[0][0]);
#endif

    /*
    for (int i = -4; i<4; i++)
    {
        for (int j = -10; j<10; j++)
        {
            for (int k = -4; k<4; k++)
            {
                Entity* pCube = g_pScene->CreateEntity();
                pCube->SetPosition(0.75f * Vector(i,j,k));

                MeshComponent* pMesh = new MeshComponent(pCube);
                pMesh->SetMeshResource("cube.nfm");

                BodyComponent* pBody = new BodyComponent(pCube);
                pBody->SetMass(1.0f);
                pBody->EnablePhysics((CollisionShape*)Engine_GetResource(Mesh::COLLISION_SHAPE, "shape_box"));
            }
        }
    }

    //set ambient & background color
    envDesc.m_AmbientLight = Vector(0.001f, 0.001f, 0.001f, 0.0f);
    envDesc.m_BackgroundColor = Vector(0.0f, 0.0f, 0.0f, 0.0f);
    g_pScene->SetEnvironment(&envDesc);

    // SUNLIGHT
    Entity* pDirLightEnt = g_pScene->CreateEntity();
    XOrientation orient;
    orient.x = Vector(0.0f, -0.0f, -0.0f, 0.0f);
    orient.z = Vector(-0.5f, -1.1f, 1.2f, 0.0f);
    orient.y = Vector(0.0f, 1.0f, 0.0f, 0.0f);
    pDirLightEnt->SetOrientation(&orient);
    DirLightDesc dirLight;
    dirLight.m_Far = 100.0f;
    dirLight.m_Splits = 4;
    dirLight.m_LightDist = 1000.0f;

    LightComponent* pDirLight = new LightComponent(pDirLightEnt);
    pDirLight->SetDirLight(&dirLight);
    pDirLight->SetColor(Float3(2.2, 2, 1.8));
    pDirLight->SetShadowMap(1024);

    pFirstWindow->cameraEntity->SetPosition(Vector(0.0f, 1.6f, -20.0f, 0.0f));
    */

    // message loop

    DrawRequest drawRequests[2];

    Common::Timer timer;
    timer.Start();
    while (!pWindow->isClosed())
    {
        //measure delta time
        g_DeltaTime = (float)timer.Stop();
        timer.Start();

        UpdateRequest updateReq;
        updateReq.pScene = g_pScene;
        updateReq.deltaTime = g_DeltaTime;


        pWindow->processMessages();
        pWindow->UpdateCamera();

        drawRequests[0].deltaTime = g_DeltaTime;
        drawRequests[0].pView = pWindow->view;
        drawRequests[1].deltaTime = g_DeltaTime;
        drawRequests[1].pView = g_pSecondaryCameraView;
        EngineAdvance(drawRequests, 2, &updateReq, 1);

        ProcessSceneEvents();

        // print focus segment name
        wchar_t str[128];
        Segment* pFocus = g_pScene->GetFocusSegment();
        swprintf(str, L"NFEngine Demo (%S) - focus: %S", PLATFORM_STR,
                 (pFocus != 0) ? pFocus->GetName() : "NONE");
        pWindow->setTitle(str);
    }

    // for testing purposes
    Common::FileOutputStream test_stream("test.xml");
    g_pScene->Serialize(&test_stream, SerializationFormat::Xml, false);

    EngineDeleteScene(g_pScene);
    delete pWindow;
    EngineRelease();

//detect memory leaks
#ifdef _DEBUG
    _CrtDumpMemoryLeaks();
#endif

    return 0;
}