void Transform::SetParent(Transform* newParent) { if( newParent == this ) return; if( parent == newParent ) return; if( parent != 0 ) parent->RemoveChild(this); parent = newParent; if( newParent != 0 ) { nextSibling = newParent->firstChild; newParent->firstChild = this; const Float4x4& world = newParent->GetWorldMatrix(); Float4x4 invWorld = world; invWorld.Wx = -DotProduct(world.XAxis, world.WAxis); invWorld.Wy = -DotProduct(world.YAxis, world.WAxis); invWorld.Wz = -DotProduct(world.ZAxis, world.WAxis); invWorld.transpose3x3(); Float4x4 newLocal; Multiply( newLocal, GetWorldMatrix(), invWorld ); SetLocalMatrix(newLocal); } }
void App::RenderText(const std::wstring& text, Float2 pos) { ID3D11DeviceContext* context = deviceManager.ImmediateContext(); // Set the backbuffer and viewport ID3D11RenderTargetView* rtvs[1] = { deviceManager.BackBuffer() }; context->OMSetRenderTargets(1, rtvs, NULL); float clearColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; context->ClearRenderTargetView(rtvs[0], clearColor); SetViewport(context, deviceManager.BackBufferWidth(), deviceManager.BackBufferHeight()); // Draw the text Float4x4 transform; transform.SetTranslation(Float3(pos.x, pos.y,0.0f)); spriteRenderer.Begin(context, SpriteRenderer::Point); spriteRenderer.RenderText(font, text.c_str(), transform.ToSIMD()); spriteRenderer.End(); // Present deviceManager.SwapChain()->Present(0, 0); // Pump the message loop window.MessageLoop(); }
BBox GetTransformedBBox(const BBox &bbox, const Float4x4 &transform) { /*Float3 vmax = Float3(bbox.Max); Float3 vmin = Float3(bbox.Min); vmax = Float3::Transform(vmax, transform); vmin = Float3::Transform(vmin, transform); XMFLOAT3 points[2] = { *(XMFLOAT3 *)&vmax, *(XMFLOAT3 *)&vmin }; return ComputeBoundingBoxFromPoints(points, 2, sizeof(XMFLOAT3));*/ Float3 xa = transform.Right() * bbox.Min.x; Float3 xb = transform.Right() * bbox.Max.x; Float3 ya = transform.Up() * bbox.Min.y; Float3 yb = transform.Up() * bbox.Max.y; Float3 za = transform.Forward() * bbox.Min.z; Float3 zb = transform.Forward() * bbox.Max.z; BBox out; out.Max = *(XMFLOAT3 *)&(Float3Max(xa, xb) + Float3Max(ya, yb) + Float3Max(za, zb) + transform.Translation()); out.Min = *(XMFLOAT3 *)&(Float3Min(xa, xb) + Float3Min(ya, yb) + Float3Min(za, zb) + transform.Translation()); return out; }
Float4x4 Scene::createBase(float scale, const Float3 &pos, const Quaternion &rot) { // Assume uniform scaling Quaternion rotNorm = Quaternion::Normalize(rot); Float4x4 ret = rotNorm.ToFloat4x4(); ret.Scale(Float3(scale)); ret.SetTranslation(pos); return ret; }
void ShadowsApp::RenderMainPass() { PIXEvent event(L"Main Pass"); ID3D11DeviceContextPtr context = deviceManager.ImmediateContext(); ID3D11RenderTargetView* renderTargets[1] = { NULL }; ID3D11DepthStencilView* ds = depthBuffer.DSView; context->OMSetRenderTargets(1, renderTargets, ds); D3D11_VIEWPORT vp; vp.Width = static_cast<float>(colorTarget.Width); vp.Height = static_cast<float>(colorTarget.Height); vp.TopLeftX = 0.0f; vp.TopLeftY = 0.0f; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; context->RSSetViewports(1, &vp); float clearColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; context->ClearRenderTargetView(colorTarget.RTView, clearColor); context->ClearDepthStencilView(ds, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0); Float4x4 meshWorld = Float4x4::ScaleMatrix(MeshScales[AppSettings::CurrentScene]); Float4x4 characterWorld = Float4x4::ScaleMatrix(CharacterScale); Float4x4 characterOrientation = Quaternion::ToFloat4x4(AppSettings::CharacterOrientation); characterWorld = characterWorld * characterOrientation; characterWorld.SetTranslation(CharacterPos); { ProfileBlock block(L"Depth Prepass"); if(AppSettings::GPUSceneSubmission) meshRenderer.RenderDepthGPU(context, camera, meshWorld, characterWorld, false); else meshRenderer.RenderDepthCPU(context, camera, meshWorld, characterWorld, false); } if(AppSettings::AutoComputeDepthBounds) meshRenderer.ReduceDepth(context, depthBuffer.SRView, camera); if(AppSettings::GPUSceneSubmission) meshRenderer.RenderShadowMapGPU(context, camera, meshWorld, characterWorld); else meshRenderer.RenderShadowMap(context, camera, meshWorld, characterWorld); renderTargets[0] = colorTarget.RTView; context->OMSetRenderTargets(1, renderTargets, ds); context->RSSetViewports(1, &vp); Float3 lightDir = AppSettings::LightDirection; meshRenderer.Render(context, camera, meshWorld, characterWorld); skybox.RenderSky(context, lightDir, true, camera.ViewMatrix(), camera.ProjectionMatrix()); }
void Collidable::InverseTransform(const Float4x4 &transform) { Float4x4 inverse = transform; inverse.transpose(); inverse.WAxis.x = -DotProduct( transform.WAxis, transform.XAxis ); inverse.WAxis.y = -DotProduct( transform.WAxis, transform.YAxis ); inverse.WAxis.z = -DotProduct( transform.WAxis, transform.ZAxis ); Transform(inverse); }
void ShadowsApp::Initialize() { App::Initialize(); AppSettings::Initialize(deviceManager.Device()); TwHelper::SetGlobalHelpText("Shadows Sample - implements various cascaded shadow map techniques, " "as well as several methods for filtering shadow maps"); ID3D11DevicePtr device = deviceManager.Device(); ID3D11DeviceContextPtr deviceContext = deviceManager.ImmediateContext(); // Camera setup camera.SetPosition(Float3(40.0f, 5.0f, 5.0f)); camera.SetYRotation(-XM_PIDIV2); // Load the meshes for(uint32 i = 0; i < uint32(Scene::NumValues); ++i) { wstring path(L"..\\Content\\Models\\"); path += MeshFileNames[i]; models[i].CreateFromSDKMeshFile(device, path.c_str()); } wstring characterPath(L"..\\Content\\Models\\Soldier\\Soldier.sdkmesh"); characterMesh.CreateFromSDKMeshFile(device, characterPath.c_str()); meshRenderer.Initialize(device, deviceManager.ImmediateContext()); ID3D11DeviceContext* context = deviceManager.ImmediateContext(); Float4x4 meshWorld = Float4x4::ScaleMatrix(MeshScales[(int)AppSettings::CurrentScene]); meshRenderer.SetSceneMesh(context, &models[(int)AppSettings::CurrentScene], meshWorld); Float4x4 characterWorld = Float4x4::ScaleMatrix(CharacterScale); Float4x4 characterOrientation = Quaternion::ToFloat4x4(AppSettings::CharacterOrientation); characterWorld = characterWorld * characterOrientation; characterWorld.SetTranslation(CharacterPos); meshRenderer.SetCharacterMesh(context, &characterMesh, characterWorld); skybox.Initialize(device); // Init the post processor postProcessor.Initialize(device); }
void TurnTo::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 toTarget = (targetPos - lookerPos).normalize(); float rotationX = DotProduct(toTarget, lookerMatrix.XAxis); float rotationY = -DotProduct(toTarget, lookerMatrix.YAxis); lookerMatrix.rotateLocalY(rotationX * EDGameCore::Game::GetDeltaTime()); lookerMatrix.rotateLocalX(rotationY * EDGameCore::Game::GetDeltaTime()); Float3 xAxis = ZERO_VECTOR; Float3 yAxis = ZERO_VECTOR; Float3 zAxis = lookerMatrix.ZAxis.normalize(); CrossProduct(xAxis, Float3(0.0f, 1.0f, 0.0f), zAxis); 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); //TurnToSolution(); }
void RenderController::CreateViewPorts(UINT viewPorts) { delete screenQuadMeshPtr; screenQuadMeshPtr = RenderMesh::LoadXML("GDMesh/ScreenQuad/ScreenQuadShape.xml", "VERTEX_POSTEX"); screenQuad.Initialize(screenQuadMeshPtr, twoDContext.GetContent(), 0); screenQuad.RenderFunc = RenderShapeTarget::IndexedPrimitiveNoTransformRenderFunc; // Creates data to be used for debug rendering of additional buffers InitializeDebugBuffers(); Float4x4 viewPortMat; if ( 1 == viewPorts ) { viewPortMat.makeIdentity(); ViewPortManager::GetReference().AddViewPort(screenQuadMeshPtr, twoDContext.GetContent(), &sceneTarget, viewPortMat); } else if ( 2 == viewPorts ) { viewPortMat.makeScale(1.0f, .5f, 1.0f); viewPortMat.translateGlobal(0.0f, 0.5f, 0.0f); ViewPortManager::GetReference().AddViewPort(screenQuadMeshPtr, twoDContext.GetContent(), &sceneTarget, viewPortMat); viewPortMat.translateGlobal(0.0f, -1.0f, 0.0f); ViewPortManager::GetReference().AddViewPort(screenQuadMeshPtr, twoDContext.GetContent(), &sceneTarget, viewPortMat); } else if ( 4 == viewPorts || 3 == viewPorts ) { viewPortMat.makeScale(0.5f, .5f, 1.0f); viewPortMat.translateGlobal(-.5f, 0.5f, 0.0f); ViewPortManager::GetReference().AddViewPort(screenQuadMeshPtr, twoDContext.GetContent(), &sceneTarget, viewPortMat); viewPortMat.translateGlobal(1.0f, 0.0f, 0.0f); ViewPortManager::GetReference().AddViewPort(screenQuadMeshPtr, twoDContext.GetContent(), &sceneTarget, viewPortMat); viewPortMat.translateGlobal(-1.0f, -1.0f, 0.0f); ViewPortManager::GetReference().AddViewPort(screenQuadMeshPtr, twoDContext.GetContent(), &sceneTarget, viewPortMat); viewPortMat.translateGlobal(1.0f, 0.0f, 0.0f); ViewPortManager::GetReference().AddViewPort(screenQuadMeshPtr, twoDContext.GetContent(), &sceneTarget, viewPortMat); } }
void LightPoint::calcMatWorld(Float4x4& ret, const float model_radius, const float desired_radius) const { // Before updating world matrix for this frame, save it for next frame // (to generate velocity data later on) ret.identity(); float scale = desired_radius / model_radius; ret.m[0] = scale; // (0,0) ret.m[5] = scale; // (1,1) ret.m[10] = scale; // (2,2); // Now offset by the light's postion #ifdef COLUMN_MAJOR ret.m[12] = pos_world_[0]; ret.m[13] = pos_world_[1]; ret.m[14] = pos_world_[2]; #else ret.m[3] = pos_world_[0]; ret.m[7] = pos_world_[1]; ret.m[11] = pos_world_[2]; #endif }
Float4x4 Float4x4::Transpose(const Float4x4& m) { return XMMatrixTranspose(m.ToSIMD()); }
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(); }
Float4 operator *(const Float4 &lhs, const Float4x4 &rhs) { return Float4(lhs.ToImpl() * rhs.ToImpl()); }
Float3 Float3::Transform(const Float3& v, const Float4x4& m) { XMVECTOR vec = v.ToSIMD(); vec = XMVector3TransformCoord(vec, m.ToSIMD()); return Float3(vec); }
Float4x4 Float4x4::TranslationMatrix(const Float3& t) { Float4x4 m; m.SetTranslation(t); return m; }
void Profiler::EndFrame(SpriteRenderer& spriteRenderer, SpriteFont& spriteFont) { // If any profile was previously active but wasn't used this frame, it could still // have outstanding queries that we need to keep running for(auto iter = profiles.begin(); iter != profiles.end(); iter++) { const std::wstring& name = (*iter).first; ProfileData& profile = (*iter).second; if(!profile.CPUProfile && !profile.Active && profile.DisjointQuery[0] != nullptr) { StartProfile(name); EndProfile(name); profile.Active = false; } } currFrame = (currFrame + 1) % QueryLatency; Float4x4 transform; transform.SetTranslation(Float3(25.0f, 100.0f, 0.0f)); // Iterate over all of the profiles for(auto iter = profiles.begin(); iter != profiles.end(); iter++) { ProfileData& profile = (*iter).second; profile.QueryFinished = false; float time = 0.0f; if(profile.CPUProfile) { time = (profile.EndTime - profile.StartTime) / 1000.0f; } else { if(profile.DisjointQuery[currFrame] == NULL) continue; // Get the query data uint64 startTime = 0; while(context->GetData(profile.TimestampStartQuery[currFrame], &startTime, sizeof(startTime), 0) != S_OK); uint64 endTime = 0; while(context->GetData(profile.TimestampEndQuery[currFrame], &endTime, sizeof(endTime), 0) != S_OK); D3D11_QUERY_DATA_TIMESTAMP_DISJOINT disjointData; while(context->GetData(profile.DisjointQuery[currFrame], &disjointData, sizeof(disjointData), 0) != S_OK); if(disjointData.Disjoint == false) { uint64 delta = endTime - startTime; float frequency = static_cast<float>(disjointData.Frequency); time = (delta / frequency) * 1000.0f; } } profile.TimeSamples[profile.CurrSample] = time; profile.CurrSample = (profile.CurrSample + 1) % ProfileData::FilterSize; float sum = 0.0f; for(UINT i = 0; i < ProfileData::FilterSize; ++i) sum += profile.TimeSamples[i]; time = sum / ProfileData::FilterSize; if(profile.Active) { wstring output = (*iter).first + L": " + ToString(time) + L"ms"; spriteRenderer.RenderText(spriteFont, output.c_str(), transform, Float4(1.0f, 1.0f, 0.0f, 1.0f)); transform._42 += 25.0f; } profile.Active = false; } }
Float3 Float3::TransformDirection(const Float3&v, const Float4x4& m) { XMVECTOR vec = v.ToSIMD(); vec = XMVector3TransformNormal(vec, m.ToSIMD()); return Float3(vec); }
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(); }
Float4x4 Float4x4::Invert(const Float4x4& m) { XMVECTOR det; return XMMatrixInverse(&det, m.ToSIMD()); }
Float4x4 Float4x4::operator*(const Float4x4& other) const { XMMATRIX result = this->ToSIMD() * other.ToSIMD(); return Float4x4(result); }