void InitScene(ID3D11Device* d3dDevice) { DestroyScene(); D3DXVECTOR3 cameraEye(0.0f, 0.0f, 0.0f); D3DXVECTOR3 cameraAt(0.0f, 0.0f, 0.0f); float sceneScaling = 1.0f; D3DXVECTOR3 sceneTranslation(0.0f, 0.0f, 0.0f); bool zAxisUp = false; #pragma region Pick Scene SCENE_SELECTION scene = static_cast<SCENE_SELECTION>(PtrToUlong(gSceneSelectCombo->GetSelectedData())); scene = CUBES; switch (scene) { case CUBE_WORLD: { #pragma region CUBE_WORLD sceneScaling = 1.0f; D3DXMatrixScaling(&gWorldMatrix, sceneScaling, sceneScaling, sceneScaling); if (zAxisUp) { D3DXMATRIXA16 m; D3DXMatrixRotationX(&m, -D3DX_PI / 2.0f); gWorldMatrix *= m; } { D3DXMATRIXA16 t; D3DXMatrixTranslation(&t, sceneTranslation.x, sceneTranslation.y, sceneTranslation.z); gWorldMatrix *= t; } sceneGraph.StartScene(gWorldMatrix,sceneScaling); sceneGraph.Add(d3dDevice, L"..\\media\\cube\\cube.sdkmesh",0,0,0,1,500,500); //gMeshOpaque.Create(d3dDevice, L"..\\media\\cube\\cube.sdkmesh"); LoadSkybox(d3dDevice, L"..\\media\\Skybox\\EmptySpace.dds"); cameraEye = sceneScaling * D3DXVECTOR3(100.0f, 5.0f, 5.0f); cameraAt = sceneScaling * D3DXVECTOR3(0.0f, 0.0f, 0.0f); } break; #pragma endregion case POWER_PLANT_SCENE: { #pragma region POWER_PLANT_SCENE sceneScaling = 1.0f; D3DXMatrixScaling(&gWorldMatrix, sceneScaling, sceneScaling, sceneScaling); if (zAxisUp) { D3DXMATRIXA16 m; D3DXMatrixRotationX(&m, -D3DX_PI / 2.0f); gWorldMatrix *= m; } { D3DXMATRIXA16 t; D3DXMatrixTranslation(&t, sceneTranslation.x, sceneTranslation.y, sceneTranslation.z); gWorldMatrix *= t; } sceneGraph.StartScene(gWorldMatrix,sceneScaling); sceneGraph.Add(d3dDevice, L"..\\media\\powerplant\\powerplant.sdkmesh"); //gMeshOpaque.Create(d3dDevice, L"..\\media\\powerplant\\powerplant.sdkmesh"); LoadSkybox(d3dDevice, L"..\\media\\Skybox\\EmptySpace.dds"); cameraEye = sceneScaling * D3DXVECTOR3(100.0f, 5.0f, 5.0f); cameraAt = sceneScaling * D3DXVECTOR3(0.0f, 0.0f, 0.0f); } break; #pragma endregion case SPONZA_SCENE: { #pragma region SPONZA_SCENE sceneScaling = 0.05f; D3DXMatrixScaling(&gWorldMatrix, sceneScaling, sceneScaling, sceneScaling); if (zAxisUp) { D3DXMATRIXA16 m; D3DXMatrixRotationX(&m, -D3DX_PI / 2.0f); gWorldMatrix *= m; } { D3DXMATRIXA16 t; D3DXMatrixTranslation(&t, sceneTranslation.x, sceneTranslation.y, sceneTranslation.z); gWorldMatrix *= t; } sceneGraph.StartScene(gWorldMatrix,sceneScaling); sceneGraph.Add(d3dDevice, L"..\\media\\Sponza\\sponza_dds.sdkmesh"); //gMeshOpaque.Create(d3dDevice, L"..\\media\\Sponza\\sponza_dds.sdkmesh"); LoadSkybox(d3dDevice, L"..\\media\\Skybox\\EmptySpace.dds"); cameraEye = sceneScaling * D3DXVECTOR3(1200.0f, 200.0f, 100.0f); cameraAt = sceneScaling * D3DXVECTOR3(0.0f, 0.0f, 0.0f); } break; #pragma endregion case MULTI_SCENE:{ #pragma region MULTI_SCENE sceneScaling = .05f; D3DXMatrixScaling(&gWorldMatrix, sceneScaling, sceneScaling, sceneScaling); if (zAxisUp) { D3DXMATRIXA16 m; D3DXMatrixRotationX(&m, -D3DX_PI / 2.0f); gWorldMatrix *= m; } { D3DXMATRIXA16 t; D3DXMatrixTranslation(&t, sceneTranslation.x, sceneTranslation.y, sceneTranslation.z); gWorldMatrix *= t; } sceneGraph.StartScene(gWorldMatrix,sceneScaling); sceneGraph.Add(d3dDevice, L"..\\media\\Sponza\\sponza_dds.sdkmesh"); D3DXMATRIXA16 translate; D3DXMatrixTranslation(&translate,0,10,0); sceneGraph.Add(d3dDevice,L"..\\media\\powerplant\\powerplant.sdkmesh",translate); //gMeshOpaque.Create(d3dDevice, L"..\\media\\Sponza\\sponza_dds.sdkmesh"); //gMeshOpaque2.Create(d3dDevice,L"..\\media\\powerplant\\powerplant.sdkmesh"); LoadSkybox(d3dDevice, L"..\\media\\Skybox\\EmptySpace.dds"); cameraEye = sceneScaling * D3DXVECTOR3(100.0f, 5.0f, 5.0f); cameraAt = sceneScaling * D3DXVECTOR3(0.0f, 0.0f, 0.0f); }break; #pragma endregion case CUBES: { #pragma region CUBES sceneScaling = 1.0f; D3DXMatrixScaling(&gWorldMatrix, sceneScaling, sceneScaling, sceneScaling); if (zAxisUp) { D3DXMATRIXA16 m; D3DXMatrixRotationX(&m, -D3DX_PI / 2.0f); gWorldMatrix *= m; } { D3DXMATRIXA16 t; D3DXMatrixTranslation(&t, sceneTranslation.x, sceneTranslation.y, sceneTranslation.z); gWorldMatrix *= t; } sceneGraph.StartScene(gWorldMatrix,sceneScaling); D3DXMATRIXA16 translate; D3DXMatrixTranslation(&translate,0,0,0); D3DXMATRIXA16 s; D3DXMatrixScaling(&s,100,0.01,100); s=s*translate; //sceneGraph.Add(d3dDevice, L"..\\media\\cube\\cube.sdkmesh",s); //sceneGraph.Add(d3dDevice, L"..\\media\\cube\\cube.sdkmesh",s); //Initializing PhysX if(!PXEngine) PXEngine = new PhysXEngine(); sceneGraph.AddMeshInstance(d3dDevice, L"..\\media\\cube\\cube.sdkmesh"); //sceneGraph.Add(d3dDevice,L"..\\media\\powerplant\\powerplant.sdkmesh",translate); if(PXEngine) PXEngine->InitializePhysX(cubeList, PhysXUnProject, PhysXProject); sceneGraph.Add(d3dDevice, L"..\\media\\cube\\cube.sdkmesh",0,0,0,1000,1000,1000); //Creating all of the cubes for(int i = 0; i < cubeList->size(); i++) { if(i%10==0) { sceneGraph.Add(d3dDevice, L"..\\media\\cube\\cube.sdkmesh",0,0,0,1,1,1); } else (*cubeList)[i]->id = sceneGraph.AddInstance(1,(*cubeList)[i]->x, (*cubeList)[i]->y, (*cubeList)[i]->z, (*cubeList)[i]->sx, (*cubeList)[i]->sy, (*cubeList)[i]->sz); } /* for(float x =0; x<15;x+=5) { for(float y=0; y<15; y+=5) { for(float z=0; z<15; z+=5) { D3DXMatrixTranslation(&translate,x,y,z); sceneGraph.Add(d3dDevice, L"..\\media\\cube\\cube.sdkmesh",x,y,z,1,1,1); } } } */ LoadSkybox(d3dDevice, L"..\\media\\Skybox\\Clouds.dds"); cameraEye = sceneScaling * D3DXVECTOR3(100.0f, 5.0f, 5.0f); cameraAt = sceneScaling * D3DXVECTOR3(0.0f, 0.0f, 0.0f); }break; #pragma endregion }; #pragma endregion cameraEye.x += 200; gViewerCamera.SetViewParams(&cameraEye, &cameraAt); gViewerCamera.SetScalers(0.01f, 100.0f); gViewerCamera.FrameMove(0.0f); // Zero out the elapsed time for the next frame gZeroNextFrameTime = true; }
View *CascadedShadowMap::createLightView(float nSlice1, float nSlice2, Camera *camera, Camera *lightCamera, View *view, Frustum *frustum) { glm::vec3 ntl,ntr,nbl,nbr,ftl,ftr,fbl,fbr,nc,fc,X,Y,Z; float nearDepth = (float)(view->getNear()+(view->getFar()-view->getNear())*nSlice1); float farDepth = (float)(view->getNear()+(view->getFar()-view->getNear())*nSlice2); float nh = (frustum->getHeightNear()+(frustum->getHeightFar()-frustum->getHeightNear())*nSlice1)/2.0f; float nw = (frustum->getWidthNear()+(frustum->getWidthFar()-frustum->getWidthNear())*nSlice1)/2.0f; float fh = (frustum->getHeightNear()+(frustum->getHeightFar()-frustum->getHeightNear())*nSlice2)/2.0f; float fw = (frustum->getWidthNear()+(frustum->getWidthFar()-frustum->getWidthNear())*nSlice2)/2.0f; glm::vec3 cameraRight(camera->getRight()[0], camera->getRight()[1], camera->getRight()[2]); glm::vec3 cameraUp(camera->getUp()[0], camera->getUp()[1], camera->getUp()[2]); glm::vec3 cameraLookAt(camera->getLookAt()[0],camera->getLookAt()[1],camera->getLookAt()[2]); glm::vec3 cameraEye(camera->getEyeV()[0],camera->getEyeV()[1],camera->getEyeV()[2]); X = cameraRight; X = glm::normalize(X); Y = cameraUp; Y = glm::normalize(Y); Z = cameraLookAt - cameraEye; Z = glm::normalize(Z); nc = cameraEye+Z*nearDepth; fc = cameraEye+Z*farDepth; ntl = nc + Y * nh - X * nw; ntr = nc + Y * nh + X * nw; nbl = nc - Y * nh - X * nw; nbr = nc - Y * nh + X * nw; ftl = fc + Y * fh - X * fw; ftr = fc + Y * fh + X * fw; fbl = fc - Y * fh - X * fw; fbr = fc - Y * fh + X * fw; // calculate the actual bounds for the ortho projection glm::mat4 lightMVP; lightMVP = glm::ortho(-1,1,-1,1,0,1); lightMVP = lightCamera->transformToMatrix(lightMVP); float left, right, up, down, back; glm::vec4 corners[] = {glm::vec4(ntl,1.0f), glm::vec4(ntr,1.0f), glm::vec4(nbl,1.0f), glm::vec4(nbr,1.0f), glm::vec4(ftl,1.0f), glm::vec4(ftr,1.0f), glm::vec4(fbl,1.0f), glm::vec4(fbr,1.0f)}; // get the projected positions of the corners for (int i=0; i<8; i++) { corners[i]=lightMVP*corners[i]; } left = corners[0][0]; right = corners[0][0]; up = corners[0][1]; down = corners[0][1]; back = corners[0][2]; // determine the bounds for (int i=1; i<8; i++) { if (corners[i][0]<left) { left=corners[i][0]; } if (corners[i][0]>right) { right=corners[i][0]; } if (corners[i][1]<down) { down=corners[i][1]; } if (corners[i][1]>up) { up=corners[i][1]; } if (corners[i][2]>back) { back=corners[i][2]; } } View *lightView = new View(); lightView->set2D(left,right,down,up,0.1,50); return lightView; }