void LookAt::LateUpdate() { Transform* looker_transform = GetGameObject()->GetTransform(); Transform* target_transform = nullptr; // Finds the target based on its instance id each update // to avoid having a dangling pointer // to an object that might have been destroyed. target = GameObject::GetGameObjectInstance(targetId); // TODO - comment this out and write your own solution target_transform = target->GetTransform(); Float4x4 lookerMatrix = looker_transform->GetLocalMatrix(); Float4x4 targetMatrix = target_transform->GetLocalMatrix(); Float3 lookerPos = Float3(lookerMatrix.WAxis); Float3 targetPos = Float3(targetMatrix.WAxis); Float3 xAxis = ZERO_VECTOR; Float3 yAxis = ZERO_VECTOR; Float3 zAxis = (targetPos - lookerPos).normalize(); CrossProduct(xAxis, zAxis, Float3(0.0f, 1.0f, 0.0f)); xAxis.normalize(); CrossProduct(yAxis, zAxis, xAxis); yAxis.normalize(); Float4x4 localMatrix = Float4x4( xAxis.x, xAxis.y, xAxis.z, lookerMatrix.padX, yAxis.x, yAxis.y, yAxis.z, lookerMatrix.padY, zAxis.x, zAxis.y, zAxis.z, lookerMatrix.padZ, lookerMatrix.WAxis.x, lookerMatrix.WAxis.y, lookerMatrix.WAxis.z, lookerMatrix.padW ); looker_transform->SetLocalMatrix(localMatrix); //LookAtSolution(); }
void SpotLight::ApplyLight() { // Set spot light position and direction Float3 dir3(GetWorldMatrixPtr()->_31, GetWorldMatrixPtr()->_32, GetWorldMatrixPtr()->_33); dir3.normalize(); XMFLOAT3 pos(&GetWorldMatrixPtr()->_41); cBufferData.spotLight.direction = XMFLOAT3(dir3.x, dir3.y, dir3.z); cBufferData.spotLight.position = pos; D3D11_MAPPED_SUBRESOURCE edit; Renderer::theContextPtr->Map(cBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &edit); memcpy(edit.pData, &cBufferData, sizeof(cBufferData)); Renderer::theContextPtr->Unmap(cBuffer, 0); Renderer::theContextPtr->VSSetConstantBuffers(cBufferData.SPOT_LIGHT_REGISTER_SLOT, 1, &cBuffer.p); Renderer::theContextPtr->PSSetConstantBuffers(cBufferData.SPOT_LIGHT_REGISTER_SLOT, 1, &cBuffer.p); // Check if the camera is inside the lit area Float3 toCamera = ViewPortManager::GetReference().GetActiveViewPosition() - *(Float3 *)&pos.x; toCamera.normalize(); if(toCamera.dotProduct(dir3) > cBufferData.spotLight.cutoff) techniqueNameOverride = "RenderSpotLightInside"; else techniqueNameOverride = "RenderSpotLightOutside"; AddToContextSet(); }
void Physics::OnUpdate(IBehavior* invokingBehavior, IMessage* message) { Physics* pPhysics = (Physics*)invokingBehavior; const Aabb& objLocalAabb = pPhysics->gameObject->GetLocalAabb(); for(unsigned int i = 0; i < 3; ++i) { if( pPhysics->localAabb.min.v[i] != objLocalAabb.min.v[i] || pPhysics->localAabb.max.v[i] != objLocalAabb.max.v[i] ) { pPhysics->Initialize(); return; } } if( pPhysics->physicsFlags & GRAVITY ) { Attribute<bool>* groundedAttrib = (Attribute<bool>*)pPhysics->GetAttribute( PhysicsAttributes::GROUNDED_ATTRIB); if( groundedAttrib->value == false ) pPhysics->ApplyGravity(); } Float3 com(0.0f, 0.0f, 0.0f); for(unsigned int i = 0; i < pPhysics->m_X.size(); ++i) com += pPhysics->m_X[i]; com *= (1.0f / pPhysics->m_X.size()); pPhysics->ApplyVerlet(); pPhysics->ApplyConstraints(); Float3 newcom(0.0f, 0.0f, 0.0f); for(unsigned int i = 0; i < pPhysics->m_X.size(); ++i) newcom += pPhysics->m_X[i]; newcom *= (1.0f / pPhysics->m_X.size()); Float3 delta = newcom - com; if( delta.magnitude() > 0.25f ) { delta.normalize(); delta *= 0.25f; } pPhysics->gameObject->TranslateGlobal( delta ); }
void RenderController::SetPerCameraShaderVariables() { ViewPortManager &vpm = ViewPortManager::GetReference(); XMVECTOR det; cbPerCamera camBuffer; camBuffer.gViewProj = *(XMFLOAT4X4 *)&ViewPortManager::GetReference().GetActiveViewProjection(); camBuffer.gCameraPos = XMFLOAT3(vpm.GetActiveViewPosition().v); Float3 camDir = vpm.GetActiveViewForward(); camDir.normalize(); camBuffer.gCameraDir = XMFLOAT3(camDir.v); camBuffer.gFarDist = vpm.GetActiveViewFarClip(); //camBuffer.gNearDist = vpm.GetActiveViewNearClip(); float nearDist = vpm.GetActiveViewNearClip(); camBuffer.gScreenSize.x = (float)Renderer::GetResolutionWidth(); camBuffer.gScreenSize.y = (float)Renderer::GetResolutionHeight(); XMStoreFloat4x4(&camBuffer.gInvViewProj, XMMatrixInverse(&det, XMLoadFloat4x4(&camBuffer.gViewProj))); camBuffer.gPlaneX = -camBuffer.gFarDist / (camBuffer.gFarDist - nearDist); camBuffer.gPlaneY = -camBuffer.gFarDist * nearDist / (camBuffer.gFarDist - nearDist); D3D11_MAPPED_SUBRESOURCE edit; Renderer::theContextPtr->Map(cameraCBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &edit); memcpy(edit.pData, &camBuffer, sizeof(camBuffer)); Renderer::theContextPtr->Unmap(cameraCBuffer, 0); Renderer::theContextPtr->VSSetConstantBuffers(camBuffer.REGISTER_SLOT, 1, &cameraCBuffer.p); Renderer::theContextPtr->PSSetConstantBuffers(camBuffer.REGISTER_SLOT, 1, &cameraCBuffer.p); Renderer::theContextPtr->GSSetConstantBuffers(camBuffer.REGISTER_SLOT, 1, &cameraCBuffer.p); Renderer::theContextPtr->HSSetConstantBuffers(camBuffer.REGISTER_SLOT, 1, &cameraCBuffer.p); Renderer::theContextPtr->DSSetConstantBuffers(camBuffer.REGISTER_SLOT, 1, &cameraCBuffer.p); }
void renderFrame(float dt) { if (rotate_light) { renderer::LightDir* light = render->light_dir(); Float3* dir = light->direction_world(); Float4x4::rotateMatYAxis(mat_tmp, dt); Float3 new_dir; Float3::affineTransformVec(new_dir, mat_tmp, *dir); dir->set(new_dir); } // Move the camera delta_pos.set(cur_dir); const Float3 zeros(0, 0, 0); if (!Float3::equal(delta_pos, zeros)) { delta_pos.normalize(); Float3::scale(delta_pos, camera_speed * dt); if (running) { Float3::scale(delta_pos, camera_run_mulitiplier); } render->camera()->moveCamera(&delta_pos); } float* cur_coeff; if (fit_right && fit_left) { cur_coeff = l_hand_coeffs[cur_image]->coeff(); models[0]->updateMatrices(l_hand_coeffs[cur_image]->coeff()); models[1]->updateMatrices(r_hand_coeffs[cur_image]->coeff()); } if (fit_right) { cur_coeff = r_hand_coeffs[cur_image]->coeff(); models[0]->updateMatrices(r_hand_coeffs[cur_image]->coeff()); } else if (fit_left) { cur_coeff = l_hand_coeffs[cur_image]->coeff(); models[0]->updateMatrices(l_hand_coeffs[cur_image]->coeff()); } HandGeometryMesh::setCurrentStaticHandProperties(cur_coeff); // Now render the final frame Float4x4 identity; identity.identity(); switch (render_output) { case 1: render->renderFrame(dt); if (render_depth) { for (uint32_t k = 0; k < std::min<uint32_t>(MAX_KINECTS, num_point_clouds_to_render); k++) { render->renderColoredPointCloud(geometry_points[k], &camera_view[k], point_cloud_scale * 1.5f * static_cast<float>(settings.width) / 4.0f); } } break; case 2: float coeff[num_models * num_coeff_fit]; if (fit_left && !fit_right) { memcpy(coeff, l_hand_coeffs[cur_image]->coeff(), sizeof(coeff[0])*num_coeff_fit); } else if (!fit_left && fit_right) { memcpy(coeff, r_hand_coeffs[cur_image]->coeff(), sizeof(coeff[0])*num_coeff_fit); } else { memcpy(coeff, l_hand_coeffs[cur_image]->coeff(), sizeof(coeff[0])*num_coeff_fit); memcpy(&coeff[num_coeff_fit], r_hand_coeffs[cur_image]->coeff(), sizeof(coeff[0])*num_coeff_fit); } fit->model_renderer()->drawDepthMap(coeff, num_coeff_fit, models, num_models, cur_kinect, false); fit->model_renderer()->visualizeDepthMap(wnd, cur_kinect); break; default: throw runtime_error("ERROR: render_output is an incorrect value"); } wnd->swapBackBuffer(); }
void Renderer::updateFlashlight(float dt) { static float time = 0; bool on; GET_SETTING("flashlight_on", bool, on); flashlight_->on() = on; flashlight_model_->setRenderHierarchy(on); // recursive if (on) { time += dt; float dist=10.0f, outer_fov=0.5f, inner_fov=0.4f, diffuse_intensity = 1.0f, scale = 1.0f; int csvm_count = 1; Float3 pos_view; Float3 target_view; GET_SETTING("flashlight_dist", float, dist); GET_SETTING("flashlight_outer_fov", float, outer_fov); GET_SETTING("flashlight_inner_fov", float, inner_fov); GET_SETTING("flashlight_csvm_count", int, csvm_count); GET_SETTING("flashlight_diffuse_intensity", float, diffuse_intensity); GET_SETTING("flashlight_pos_view", Float3, pos_view); GET_SETTING("flashlight_target_view", Float3, target_view); GET_SETTING("flashlight_scale", float, scale); flashlight_->near_far().set(0.1f, dist); flashlight_->outer_fov_deg() = outer_fov; flashlight_->diffuse_intensity() = diffuse_intensity; flashlight_->inner_fov_deg() =inner_fov; flashlight_->cvsm_count(csvm_count); // The light object itself... Float3::affineTransformPos(flashlight_->pos_world(), camera_->view_inv(), pos_view); // Add some noise to the world position Float3 rand_vec; rand_vec[0] = math::PerlinNoise::Noise(0.0f, 0.5f*time) * 0.15f; rand_vec[1] = math::PerlinNoise::Noise(100.0f, 0.5f*time) * 0.15f; rand_vec[2] = 0; Float3::add(flashlight_->pos_world(), rand_vec, flashlight_->pos_world()); Float3 dir_view; Float3::sub(dir_view, target_view, pos_view); dir_view.normalize(); Float3::affineTransformVec(flashlight_->dir_world(), camera_->view_inv(), dir_view); rand_vec[0] = math::PerlinNoise::Noise(0.0f, 0.5f*time) * 0.15f; rand_vec[1] = math::PerlinNoise::Noise(100.0f, 0.5f*time) * 0.15f; rand_vec[2] = 0; Float3::add(flashlight_->dir_world(), rand_vec, flashlight_->dir_world()); // Now the flashlight model Float4x4& world_mat = flashlight_model_->mat(); Float3 up(0, 1, 0); if (fabsf(fabsf(Float3::dot(flashlight_->dir_world(), up)) - 1) < EPSILON) { // preturb the up vector up[0] += (float)LOOSE_EPSILON; up[2] += (float)LOOSE_EPSILON; up.normalize(); } Float3 side; Float3::cross(side, up, flashlight_->dir_world()); side.normalize(); Float3::cross(up, flashlight_->dir_world(), side); up.normalize(); Float4x4::rotateMatBasis(world_mat, up, flashlight_->dir_world(), side); world_mat.rightMultScale(scale, scale, scale); world_mat.leftMultTranslation(flashlight_->pos_world()); } }
void Camera::Update(int width, int height, float delta) { Float3 translation = ZERO_VECTOR; float deltaX = 0; float deltaY = 0; float moveFactor = m_fSpeed * delta; if (GetAsyncKeyState('W')) translation += Float3(0.0f, 0.0f, -moveFactor); // back if (GetAsyncKeyState('S')) translation += Float3(0.0f, 0.0f, moveFactor); // left if (GetAsyncKeyState('A')) translation += Float3(moveFactor, 0.0f, 0.0f); // right if (GetAsyncKeyState('D')) translation += Float3(-moveFactor, 0.0f, 0.0f); // up if (GetAsyncKeyState('E')) translation += Float3(0.0f, -moveFactor, 0.0f); // down if (GetAsyncKeyState('Q')) translation += Float3(0.0f, moveFactor, 0.0f); if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) { GetCursorPos(&m_lpCurrentPosition); SetCursorPos((int)(width)* 0.65f, (int)(height)* 0.65f); GetCursorPos(&m_lpPreviousPosition); ShowCursor(false); deltaY = (float)(m_lpPreviousPosition.y - m_lpCurrentPosition.y); deltaX = (float)(m_lpPreviousPosition.x - m_lpCurrentPosition.x); } else { ShowCursor(true); } // deffered context Float4x4 rotationX = IDENTITY; Float4x4 rotationY = IDENTITY; rotationX.makeRotationX((float)deltaY); rotationY.makeRotationY((float)deltaX); m_mView.translateLocal(translation.negate()); m_mView.rotateLocalX((float)-deltaY * moveFactor * 0.1f); m_mView.rotateLocalY((float)-deltaX * moveFactor * 0.1f); Float3 forward = m_mView.ZAxis; Float3 up; Float3 right; forward.normalize(); CrossProduct(right, Float3(0, 1, 0), forward); right.normalize(); CrossProduct(up, forward, right); up.normalize(); m_mView.XAxis = right; m_mView.YAxis = up; m_mView.ZAxis = forward; XMFLOAT4X4 tempMat = *((XMFLOAT4X4*)&m_mView); XMMATRIX tempInv = XMLoadFloat4x4(&tempMat); tempInv = XMMatrixInverse(NULL, tempInv); XMStoreFloat4x4((XMFLOAT4X4*)&m_mInvView, tempInv); //XMMATRIX tempView = XMLoadFloat4x4(&m_mView); ///*tempTransform = XMMatrixMultiply(rotationX, tempTransform); //tempTransform = XMMatrixMultiply(rotationY, tempTransform);*/ //XMMATRIX rotation = XMMatrixRotationRollPitchYaw((float)deltaY * delta * 0.5f, (float)deltaX * delta * 0.5f, 0); //tempTransform = XMMatrixMultiply(rotation, tempTransform); //tempView = XMMatrixMultiply(tempView, tempTransform); //XMStoreFloat4x4(&m_mView, tempView); //XMFLOAT3 tempForward = XMFLOAT3(m_mView._31, m_mView._32, m_mView._33); //XMFLOAT3 tempUp; //XMFLOAT3 tempRight; //XMFLOAT3 tempWorldUp = XMFLOAT3(0, 1, 0); //XMVECTOR worldUp = XMLoadFloat3(&tempWorldUp); //XMVECTOR forward = XMLoadFloat3(&tempForward); //forward = XMVector3Normalize(forward); //XMVECTOR right = XMVector3Cross(worldUp, forward); //right = XMVector3Normalize(right); //XMVECTOR up = XMVector3Cross(right, forward); //up = XMVector3Normalize(up); //XMStoreFloat3(&tempUp, up); //XMStoreFloat3(&tempForward, forward); //XMStoreFloat3(&tempRight, right); ///*m_mView._11 = tempRight.x; m_mView._12 = tempRight.y; m_mView._13 = tempRight.z; //m_mView._21 = tempRight.x; m_mView._22 = tempRight.y; m_mView._23 = tempRight.z; //m_mView._31 = tempRight.x; m_mView._32 = tempRight.y; m_mView._33 = tempRight.z;*/ //ConstructMV(); }