//---------------------------------------------------------------------------- void Player::OnAttach() { mpNode = DynamicCast<Node>(GetSceneObject()); WIRE_ASSERT(mpNode); // Init gun mpGun = DynamicCast<Node>(mpNode->FindChild("Gun")); WIRE_ASSERT(mpGun); Node* pMuzzleFlash = DynamicCast<Node>(mpGun->FindChild("muzzleFlash")); WIRE_ASSERT(pMuzzleFlash); mspMuzzleflashMaterialState = pMuzzleFlash->GetState<StateMaterial>(); WIRE_ASSERT(mspMuzzleflashMaterialState); NodeLight* pLightNode = mpNode->FindChild<NodeLight>(); WIRE_ASSERT(pLightNode); mspMuzzleflashLight = pLightNode->Get(); mMuzzleflashLightColor = mspMuzzleflashLight->Color; // since render state cannot be extracted from Unity materials, we have // to set the appropriate states manually for the muzzle flash StateCull* pCullState = WIRE_NEW StateCull; pCullState->CullFace = StateCull::CM_OFF; pMuzzleFlash->AttachState(pCullState); StateAlpha* pAlphaState = WIRE_NEW StateAlpha; pAlphaState->BlendEnabled = true; pAlphaState->SrcBlend = StateAlpha::SBM_SRC_ALPHA; pAlphaState->DstBlend = StateAlpha::DBM_ONE; pMuzzleFlash->AttachState(pAlphaState); StateZBuffer* pZBufferState = WIRE_NEW StateZBuffer; pZBufferState->Writable = false; pMuzzleFlash->AttachState(pZBufferState); pMuzzleFlash->UpdateRS(); WIRE_ASSERT(pMuzzleFlash->GetRenderObject()); pMuzzleFlash->GetRenderObject()->GetLights()->RemoveAll(); Light* pLight = WIRE_NEW Light(Light::LT_POINT); pLight->Ambient = ColorRGB::WHITE; pMuzzleFlash->GetRenderObject()->GetLights()->Append(pLight); pMuzzleFlash->AttachChild(WIRE_NEW NodeLight(pLight)); pMuzzleFlash->GetRenderObject()->GetMaterial()->SetBlendMode(Material::BM_MODULATE); mspCharacter = mpNode->GetController<CharacterController>(); WIRE_ASSERT(mspCharacter); // Add a reference to Player in the physics object mspCharacter->Get()->setUserPointer(this); }
//---------------------------------------------------------------------------- Bool Sample5::OnInitialize() { if (!Parent::OnInitialize()) { return false; } // We are building the following scenegraph: // +Root // +->LitGroup (with StateMaterial, Light1, Light2 attached) // | +->Cube1 (textured cube) // | +->Cube2 (flat shaded cube) // | // +->LightNode1 (owning Light1) // | +->LightSrc1 (cube representing Light1) // | // +->LightNode2 (owning Light2) // +->LightSrc2 (cube representing Light2) mspRoot = WIRE_NEW Node; Node* pLitGroup = WIRE_NEW Node; mspRoot->AttachChild(pLitGroup); // create a green and a red point light and a material state // which defines how to reflect the lights. Light* pLight1 = WIRE_NEW Light(Light::LT_POINT); pLight1->Color = ColorRGB::GREEN; Light* pLight2 = WIRE_NEW Light(Light::LT_POINT); pLight2->Color = ColorRGB::RED; pLight2->Ambient = ColorRGB(0.1F, 0.1F, 0.1F); StateMaterial* pMaterial = WIRE_NEW StateMaterial; pMaterial->Ambient = ColorRGBA(1, 1, 0, 1); // attach lights and material state pLitGroup->AttachState(pMaterial); pLitGroup->AttachLight(pLight1); pLitGroup->AttachLight(pLight2); // create and position the actual objects to be lit Spatial* pCube1 = CreateCube(); Spatial* pCube2 = CreateCube(false, true); pCube1->Local.SetTranslate(Vector3F(-2.0F, 0, 0)); pCube2->Local.SetTranslate(Vector3F(2.5F, 0, 0)); pLitGroup->AttachChild(pCube1); pLitGroup->AttachChild(pCube2); // create light nodes. These act the same as normal nodes, except that // they own a light and apply scene graph transformations on that light. NodeLight* pLightNode1 = WIRE_NEW NodeLight; mspRoot->AttachChild(pLightNode1); pLightNode1->Set(pLight1); NodeLight* pLightNode2 = WIRE_NEW NodeLight; mspRoot->AttachChild(pLightNode2); pLightNode2->Set(pLight2); // create two cubes representing the position and color of the lights Spatial* pLightSrc1 = CreateCube(false, false, true, ColorRGBA::GREEN); pLightSrc1->Local.SetUniformScale(0.15F); pLightNode1->AttachChild(pLightSrc1); Spatial* pLightSrc2 = CreateCube(false, false, true, ColorRGBA::RED); pLightSrc2->Local.SetUniformScale(0.15F); pLightNode2->AttachChild(pLightSrc2); // update all render state and light objects in the scene graph // (i.e. Light1, Light2 and StateMaterial are propagated from LitGroup // to Cube1 and Cube2) mspRoot->UpdateRS(); // create the bottom plane, which already has a white light attached, // to be rendered manually in the render loop. This is only for the // purpose of demonstrating scene graph and manual rendering with lights. mspPlane = CreatePlane(); NodePtr spWhiteCubeNode = CreateCube(false, false, true, ColorRGBA::WHITE); mspWhiteCube = spWhiteCubeNode->GetRenderObject(); // Setup the position and orientation of the camera to look down // the -z axis with y up. Vector3F cameraLocation(0.0F, -1.0F, 10.0F); Vector3F viewDirection(0.0F, 0.0F, -1.0F); Vector3F up(0.0F, 1.0F, 0.0F); Vector3F right = viewDirection.Cross(up); mspCamera = WIRE_NEW Camera; mspCamera->SetFrame(cameraLocation, viewDirection, up, right); // By providing a field of view (FOV) in degrees, aspect ratio, // near and far plane, we define a viewing frustum used for culling. mspCamera->SetFrustum(45, GetWidthF() / GetHeightF() , 0.1F, 300.0F); mCuller.SetCamera(mspCamera); // Initialize working variables used in the render loop (i.e. OnIdle()). mAngle = 0.0F; mLastTime = System::GetTime(); return true; }