void CEditorScene::setupInitialScene() { math::SAxisAlignedBox aabb; aabb.Center = XMFLOAT3(0, 0.0f, 0); aabb.Extents = XMFLOAT3(3000.0f, 1000.0f, 3000.0f); ISceneManager* smgr = mDevice->createSceneManager(aabb); mSceneManager = smgr; ISimpleMesh* planeMesh = mMeshManager->createPlaneMesh("ground_plane", 40, 40, 40, 40); IMeshNode* planeNode = smgr->addMeshNode(planeMesh, nullptr, nullptr, true); planeNode->setMaterialName("ground_wire_material"); mCamera = smgr->addFpsCameraNode(1, nullptr, XMFLOAT3(0, 10.0f, -15.0f), XMFLOAT3(0, 5, 0.0f), XMFLOAT3(0, 1.0f, 0), true); //mCamera->setShadowRange(300.0f); ILightNode* light = smgr->addDirectionalLight(1, nullptr, XMFLOAT3(3.0f, -2.0f, 1.5f)); light->setSpecular(XMFLOAT4(1.0f, 1.0f, 1.0f, 32.0f)); light->setDiffuse(XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f)); light->enableShadow(true); smgr->setAmbient(XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f)); mCubeMesh = mMeshManager->createCubeMesh("cube_bounding"); mSphereMesh = mMeshManager->createSphereMesh("sphere_bounding"); mCylinderMesh = mMeshManager->createCylinderMesh("cylinder_bounding"); mTimer = mDevice->getTimer(); smgr->update(0); // init all mesh names std::vector<std::string> names; mResourceGroupManager->listResourceNames(ERFT_MESH, names); std::set<std::string> nameSet; for (u32 i = 0; i < names.size(); i++) { nameSet.insert(names[i]); } for (auto it = nameSet.begin(); it != nameSet.end(); it++) { std::string s = *it; mMeshNames.push_back(s); } for (u32 i = 0; i < mMeshNames.size(); i++) { mMeshNodeInfos.insert(std::make_pair(mMeshNames[i], SMeshNodeInfo())); } CXmlFileManager::Read("main.physics.xml"); for (auto it = mMeshNodeInfos.begin(); it != mMeshNodeInfos.end(); it++) { it->second.SetVisible(false); } }
ISceneManager* setupScene(IDevice* device) { // create scene manager ISceneManager* smgr = device->createSceneManager(); // get mesh manager IMeshManager* meshManager = IMeshManager::getInstance(); // set up ground. ISimpleMesh* groundMesh = meshManager->createPlaneMesh("ground", 100.0f, 100.0f, 10, 10, 50, 50); IMeshNode* groundNode = smgr->addMeshNode(groundMesh, "ground_material", nullptr, true); // create a sphere ISimpleMesh* sphereMesh = meshManager->createSphereMesh("sphere", 1.0f, 100, 100); IMeshNode* sphereNode = smgr->addMeshNode(sphereMesh, "sphere_material", nullptr, true, XMFLOAT3(-1.2f, 1.0f, 0)); // create a box ISimpleMesh* boxMesh = meshManager->createCubeMesh("box", 1.2f, 3.0f, 1.2f); IMeshNode* boxNode = smgr->addMeshNode(boxMesh, "box_material", nullptr, true, XMFLOAT3(1.0f, 1.5f, 1.0f)); // create a teapot IModelMesh* teapotMesh = meshManager->getModelMesh("teapot.mesh"); IMeshNode* teapot = smgr->addModelMeshNode(teapotMesh, nullptr, true, XMFLOAT3(1.2f, 0, -1.0f)); teapot->setMaterialName("teapot_material"); // add 100 point light for (u32 i = 0; i < 100; i++) { XMFLOAT3 pos(math::RandomFloat(-20.0, 20.0f), math::RandomFloat(3.0, 10.0f), math::RandomFloat(-20.0, 20.0f)); XMFLOAT4 diffuse(math::RandomFloat(0, 0.3f), math::RandomFloat(0, 0.3f), math::RandomFloat(0, 0.3f), 1.0f); XMFLOAT4 specular(math::RandomFloat(0, 0.3f), math::RandomFloat(0, 0.3f), math::RandomFloat(0, 0.3f), 32.0f); ILightNode* light = smgr->addPointLight(i, nullptr, false, pos, 20.0f); light->setSpecular(specular); light->setDiffuse(diffuse); light->setAttenuation(1.0f, 0.05f, 0); } sphereNode->addShadow(1); boxNode->addShadow(1); teapot->addShadow(1); // create a fps camera node IFpsCameraNode* camera = smgr->addFpsCameraNode(1, nullptr, XMFLOAT3(0, 1.0f, -6.0f), XMFLOAT3(0, 1.0f, 0.0f), XMFLOAT3(0, 1.0f, 0)); camera->setNearZ(1.0f); camera->setFarZ(1000.0f); // set ambient in the environment. smgr->setAmbient(XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f)); return smgr; }
void EditorScene::setupInitialScene() { math::SAxisAlignedBox aabb; aabb.Center = XMFLOAT3(0, 0.0f, 0); aabb.Extents = XMFLOAT3(3000.0f, 1000.0f, 3000.0f); ISceneManager* smgr = mDevice->createSceneManager(aabb); mSceneManager = smgr; ITextureCube* skyTexture = mTextureManager->loadCubeTexture("skybox1.dds"); smgr->setSkyDome(skyTexture); IMeshNode* skyNode = mSceneManager->getSkyNode(); if (skyNode) skyNode->setMaterialName("skydome_material"); mTimer = mDevice->getTimer(); ISimpleMesh* cubeMesh = mMeshManager->createCubeMesh("cube1"); mSelectedCubeNode = smgr->addMeshNode(cubeMesh, nullptr, nullptr, false); mSelectedCubeNode->setMaterialName("selected_cube_material"); mSelectedCubeNode->setVisible(false); mSelectedCubeNode->setTag(WIRE_NODE_TAG); mPickingCubeNode = smgr->addMeshNode(cubeMesh, nullptr, nullptr, false); mPickingCubeNode->setMaterialName("picking_cube_material"); mPickingCubeNode->setVisible(false); mPickingCubeNode->setTag(WIRE_NODE_TAG); ISimpleMesh* sphereMesh = mMeshManager->createSphereMesh("pointlight_mesh", 1.0f, 10, 10); mPointLightCollectionNode = smgr->addInstanceCollectionNode(sphereMesh, nullptr, 4000, sizeof(XMFLOAT4)); mPointLightCollectionNode->setMaterialName("multi_wire_material"); mPointLightCollectionNode->setTag(WIRE_NODE_TAG); mPointLightCollectionNode->setVisible(false); mSceneManager->update(0); IVideoDriver* driver = IVideoDriver::getInstance(); mVideoDriver->setDeferredShading(true); driver->setDeferredShadingAlgorithm(EDSA_CS_TILE_BASED_DEFERRED_SHADING); IShaderManager* shaderManager = IShaderManager::getInstance(); mTileBasedDSShader = shaderManager->load(EST_COMPUTE_SHADER, "defer_shader_cs.hlsl", "cs_main"); smgr->setTileBasedDeferredShadingCS(mTileBasedDSShader); if (!CFileParser::ReadScene("main.scene")) { setupTerrain(); ILightNode* light = smgr->addDirectionalLight(1, nullptr, XMFLOAT3(3.0f, -2.0f, 1.5f)); light->setSpecular(XMFLOAT4(1.0f, 1.0f, 1.0f, 32.0f)); light->setDiffuse(XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f)); light->enableShadow(true); mDirectionalLightNode = light; smgr->setAmbient(XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f)); mCamera = smgr->addFpsCameraNode(1, nullptr, XMFLOAT3(0, 30.0f, -4.0f), XMFLOAT3(0, 30.0f, 0.0f), XMFLOAT3(0, 1.0f, 0), true); mCamera->setShadowRange(300.0f); mVideoDriver->setDeferredShading(true); mDeferredShading = true; } UpdateGlobalLighting(); smgr->update(); }
int main() { SDeviceContextSettings settings; settings.MultiSamplingCount = 4; settings.MultiSamplingQuality = 32; IApplication* device = gf::createDevice(EDT_DIRECT3D11, SCREEN_WIDTH, SCREEN_HEIGHT, EWS_NONE, true, settings); IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->createSceneManager(); IMeshManager* meshManager = driver->getMeshManager(); IMaterialManager* materialManager = driver->getMaterialManager(); ITextureManager* textureManager = driver->getTextureManager(); IPipelineManager* pipelineManager = driver->getPipelineManager(); IResourceGroupManager* resourceGroupManager = driver->getResourceGroupManager(); resourceGroupManager->init("Resources.cfg"); const f32 groundSize = 1000.0f; ISimpleMesh* groundMesh = meshManager->createPlaneMesh("ground", groundSize, groundSize, 100, 100, 100.0f, 100.0f); IMeshNode* groundNode = smgr->addMeshNode(groundMesh, nullptr, nullptr, false); groundNode->setMaterialName("ground_material"); // add point lights const u32 lightColNum = 10; const u32 lightRowNum = 10; f32 xLightSpace = groundSize / (lightColNum - 1); f32 zLightSpace = groundSize / (lightRowNum - 1); for (u32 i = 0; i < lightRowNum; i++) { for (u32 j = 0; j < lightColNum; j++) { u32 id = i * lightColNum + j; f32 x = -groundSize * 0.5f + xLightSpace * j; f32 z = groundSize * 0.5f - zLightSpace * i; ILightNode* light = smgr->addPointLight(id, nullptr, true, XMFLOAT3(x, 50.0f, z), 200.0f); light->setSpecular(XMFLOAT4(12.0f, 12.0f, 12.0f, 32.0f)); light->setDiffuse(XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f)); light->setAttenuation(1.0f, 0.1f, 0); } } /* IModelMesh* carMesh = meshManager->getModelMesh("myfirst.mesh"); IMeshNode* carNode = smgr->addModelMeshNode(carMesh, NULL, false); carNode->translate(0, 0.1f, 0); carNode->setNeedCulling(false); carNode->scale(0.05f, 0.05f, 0.05f); carNode->pitch(XM_PIDIV2); */ IModelMesh* carMesh = meshManager->getModelMesh("car B red.mesh"); IMeshNode* carNode = smgr->addModelMeshNode(carMesh, NULL, false); carNode->translate(0, 1.0f, 0); //carNode->setNeedCulling(false); //carNode->scale(0.05f, 0.05f, 0.05f); //carNode->pitch(XM_PIDIV2); IModelMesh* wheelleftFrontMesh = meshManager->getModelMesh("wheel front left.mesh"); IMeshNode* wheelleftFront = smgr->addModelMeshNode(wheelleftFrontMesh, carNode, false); //carNode->setNeedCulling(false); ICameraNode* camera = smgr->addFpsCameraNode(1, nullptr, XMFLOAT3(0, 1.0f, -4.0f), XMFLOAT3(0, 1.0f, 0.0f)); camera->setFarZ(3000.0f); smgr->setAmbient(XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f)); char caption[200]; ITimer* timer = device->getTimer(); timer->reset(); const f32 color2[] = { 1.0f, 0.0f, 0.0f, 1.0f }; while (device->run()) { const float clearColor[] = { 0.0f, 0.0f, 0.0f, 1.0f }; driver->beginScene(true, true, clearColor); u32 ms = timer->tick(); float dt = ms * 0.001f; updateCamera(camera, dt); //updateCarPosition(dt, carNode, camera); smgr->update(ms); smgr->drawAll(); driver->endScene(); f32 fps = getFps(dt); sprintf_s(caption, "FPS:%f, delta:%f", fps, 1000.0f/fps); device->setWindowCaption(caption); } //sphereMaterial.drop(); smgr->destroy(); device->drop(); return 0; }
int _tmain(int argc, _TCHAR* argv[]) { srand(time(0)); SDeviceContextSettings settings; settings.MultiSamplingCount = 4; settings.MultiSamplingQuality = 32; IDevice* device = gf::createDevice(EDT_DIRECT3D11, SCREEN_WIDTH, SCREEN_HEIGHT, EWS_NONE, true, settings); IVideoDriver* driver = device->getVideoDriver(); math::SAxisAlignedBox aabb; aabb.Center = XMFLOAT3(0, 10.0f, 0); aabb.Extents = XMFLOAT3(500.0f, 500.0f, 500.0f); ISceneManager* smgr = device->createSceneManager(aabb); IMeshManager* meshManager = driver->getMeshManager(); IMaterialManager* materialManager = driver->getMaterialManager(); ITextureManager* textureManager = driver->getTextureManager(); IPipelineManager* pipelineManager = driver->getPipelineManager(); IResourceGroupManager* resourceGroupManager = driver->getResourceGroupManager(); resourceGroupManager->init("Resources.cfg"); PhysicsEngine::getInstance(); setupPhysics(smgr); // create ground ISimpleMesh* groundMesh = meshManager->createPlaneMesh("ground", groundSize, groundSize, 10, 10, 30.0f, 30.0f); IMeshNode* groundNode = smgr->addMeshNode(groundMesh, nullptr, nullptr, false); groundNode->setMaterialName("ground_material"); createBoxes(smgr); // add directional light XMFLOAT3 light_dir(5.0f, -5.0f, -2.0f); f32 lightDistance = -20; ILightNode* light = smgr->addDirectionalLight(1, nullptr, light_dir); light->setSpecular(XMFLOAT4(1.0f, 1.0, 1.0f, 32.0f)); light->setDiffuse(XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f)); //light->setShadowCameraOrthographicSize(10.0f, 7.0f); XMFLOAT4 unit_dir = light->getDirection(); light->setPosition(XMFLOAT3(unit_dir.x * lightDistance, unit_dir.y * lightDistance, unit_dir.z * lightDistance)); light->setShadowMapSize(SCREEN_WIDTH, SCREEN_HEIGHT); light->setShadowCameraOrthographicSize(100.0f, 100.0f); light->enableShadow(true); ICameraNode* camera = smgr->addFpsCameraNode(1, nullptr, XMFLOAT3(0, 1.0f, -4.0f), XMFLOAT3(0, 1.0f, 0.0f), XMFLOAT3(0, 1.0f, 0), true); camera->setPosition(XMFLOAT3(0, 20.0f, -40.0f)); camera->lookAt(XMFLOAT3(0, 0, 0)); camera->setNearZ(1.0f); camera->setFarZ(1000.0f); camera->setShadowRange(200.0f); smgr->setAmbient(XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f)); updateLightDirection(10.0f, light); char caption[200]; ITimer* timer = device->getTimer(); timer->reset(); const f32 color2[] = { 1.0f, 0.0f, 0.0f, 1.0f }; while (device->run()) { const float clearColor[] = { 0.0f, 0.0f, 0.0f, 1.0f }; driver->beginScene(true, true, clearColor); u32 ms = timer->tick(); float dt = ms * 0.001f; updatePhysics(dt); updateCamera(camera, dt); //updateLightDirection(dt, light); smgr->update(ms); smgr->drawAll(); driver->endScene(); sprintf_s(caption, "FPS:%f", getFps(dt)); device->setWindowCaption(caption); //Sleep(10); } //sphereMaterial.drop(); smgr->destroy(); device->drop(); delete g_ground; delete g_box; PhysicsEngine::deleteInstance(); return 0; }