void LightingApp::DrawScene() { md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::LightSteelBlue)); md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0); md3dImmediateContext->IASetInputLayout(mInputLayout); md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); md3dImmediateContext->PSSetShader(mPixelShader, NULL, 0); md3dImmediateContext->VSSetShader(mVertexShader, NULL, 0); UINT stride = sizeof(Vertex); UINT offset = 0; XMMATRIX view = XMLoadFloat4x4(&mView); XMMATRIX proj = XMLoadFloat4x4(&mProj); XMMATRIX viewProj = view*proj; // Set per frame constants. mFrameConstantBuffer.Data.mDirLight = mDirLight; mFrameConstantBuffer.Data.mPointLight = mPointLight; mFrameConstantBuffer.Data.mSpotLight = mSpotLight; mFrameConstantBuffer.Data.mEyePosW = mEyePosW; // // Draw the hills. // md3dImmediateContext->IASetVertexBuffers(0, 1, &mLandVB, &stride, &offset); md3dImmediateContext->IASetIndexBuffer(mLandIB, DXGI_FORMAT_R32_UINT, 0); // Set per object constants. XMMATRIX world = XMLoadFloat4x4(&mLandWorld); XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world); XMMATRIX worldViewProj = XMMatrixTranspose(world*view*proj); mObjectConstantBuffer.Data.mWorld = mLandWorld; XMStoreFloat4x4(&mObjectConstantBuffer.Data.mWorldInvTranspose, worldInvTranspose); XMStoreFloat4x4(&mObjectConstantBuffer.Data.mWorldViewProj, worldViewProj); mObjectConstantBuffer.Data.mMaterial = mLandMat; mObjectConstantBuffer.ApplyChanges(md3dImmediateContext); mFrameConstantBuffer.ApplyChanges(md3dImmediateContext); ID3D11Buffer* buffer[2] = { mObjectConstantBuffer.Buffer(), mFrameConstantBuffer.Buffer() }; md3dImmediateContext->VSSetConstantBuffers(0, 1, &buffer[0]); md3dImmediateContext->PSSetConstantBuffers(0, 2, buffer); md3dImmediateContext->DrawIndexed(mLandIndexCount, 0, 0); // // Draw the waves. // md3dImmediateContext->IASetVertexBuffers(0, 1, &mWavesVB, &stride, &offset); md3dImmediateContext->IASetIndexBuffer(mWavesIB, DXGI_FORMAT_R32_UINT, 0); // Set per object constants. world = XMLoadFloat4x4(&mWavesWorld); worldInvTranspose = MathHelper::InverseTranspose(world); worldViewProj = XMMatrixTranspose(world*view*proj); mObjectConstantBuffer.Data.mWorld = mWavesWorld; XMStoreFloat4x4(&mObjectConstantBuffer.Data.mWorldInvTranspose, worldInvTranspose); XMStoreFloat4x4(&mObjectConstantBuffer.Data.mWorldViewProj, worldViewProj); mObjectConstantBuffer.Data.mMaterial = mWavesMat; mObjectConstantBuffer.ApplyChanges(md3dImmediateContext); mFrameConstantBuffer.ApplyChanges(md3dImmediateContext); md3dImmediateContext->VSSetConstantBuffers(0, 1, &buffer[0]); md3dImmediateContext->PSSetConstantBuffers(0, 2, buffer); md3dImmediateContext->DrawIndexed(3 * mWaves.TriangleCount(), 0, 0); HR(mSwapChain->Present(0, 0)); }
void LightingApp::OnResize() { D3DApp::OnResize(); XMMATRIX P = XMMatrixPerspectiveFovLH(0.25f*MathHelper::Pi, AspectRatio(), 1.0f, 1000.0f); XMStoreFloat4x4(&mProj, P); }
void uniformMatrix4fv(GLint uniform, Matrix mat) { DirectX::XMFLOAT4X4 fMat; XMStoreFloat4x4(&fMat, mat.getXMMatrix()); glUniformMatrix4fv(uniform, 1, GL_FALSE, &fMat.m[0][0]); }
void LightingApp::UpdateScene(float dt) { // Convert Spherical to Cartesian coordinates. float x = mRadius*sinf(mPhi)*cosf(mTheta); float z = mRadius*sinf(mPhi)*sinf(mTheta); float y = mRadius*cosf(mPhi); mEyePosW = XMFLOAT3(x, y, z); // Build the view matrix. XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX V = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&mView, V); // // Every quarter second, generate a random wave. // static float t_base = 0.0f; if( (mTimer.TotalTime() - t_base) >= 0.25f ) { t_base += 0.25f; DWORD i = 5 + rand() % (mWaves.RowCount()-10); DWORD j = 5 + rand() % (mWaves.ColumnCount()-10); float r = MathHelper::RandF(1.0f, 2.0f); mWaves.Disturb(i, j, r); } mWaves.Update(dt); // // Update the wave vertex buffer with the new solution. // D3D11_MAPPED_SUBRESOURCE mappedData; HR(md3dImmediateContext->Map(mWavesVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData)); Vertex* v = reinterpret_cast<Vertex*>(mappedData.pData); for(UINT i = 0; i < mWaves.VertexCount(); ++i) { v[i].Pos = mWaves[i]; v[i].Normal = mWaves.Normal(i); } md3dImmediateContext->Unmap(mWavesVB, 0); // // Animate the lights. // // Circle light over the land surface. mPointLight.Position.x = 70.0f*cosf( 0.2f*mTimer.TotalTime() ); mPointLight.Position.z = 70.0f*sinf( 0.2f*mTimer.TotalTime() ); mPointLight.Position.y = MathHelper::Max(GetHillHeight(mPointLight.Position.x, mPointLight.Position.z), -3.0f) + 10.0f; // The spotlight takes on the camera position and is aimed in the // same direction the camera is looking. In this way, it looks // like we are holding a flashlight. mSpotLight.Position = mEyePosW; XMStoreFloat3(&mSpotLight.Direction, XMVector3Normalize(target - pos)); }
bool DXBase::Initialize(HWND hwnd, int width, int height, bool fullscreen, bool vsync_enabled) { // Store the vsync setting. m_vsync_enabled = vsync_enabled; // Describe our Buffer DXGI_MODE_DESC bufferDesc; ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC)); bufferDesc.Width = width; bufferDesc.Height = height; bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; // Set the refresh rate of the back buffer. if (m_vsync_enabled) { bufferDesc.RefreshRate.Numerator = 60; bufferDesc.RefreshRate.Denominator = 1; } else { bufferDesc.RefreshRate.Numerator = 0; bufferDesc.RefreshRate.Denominator = 1; } // Describe our SwapChain DXGI_SWAP_CHAIN_DESC swapChainDesc; ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC)); swapChainDesc.BufferDesc = bufferDesc; swapChainDesc.SampleDesc.Count = 1; swapChainDesc.SampleDesc.Quality = 0; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.BufferCount = 1; swapChainDesc.OutputWindow = hwnd; swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // Set to full screen or windowed mode. if (fullscreen) { swapChainDesc.Windowed = FALSE; } else { swapChainDesc.Windowed = TRUE; } //D3D_FEATURE_LEVEL featurelevel = D3D_FEATURE_LEVEL_11_0; // Create our SwapChain D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &swapChainDesc, &m_swapChain, &m_device, NULL, &m_deviceContext); // Create our BackBuffer ID3D11Texture2D* BackBuffer; m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer); // Create our Render Target m_device->CreateRenderTargetView(BackBuffer, NULL, &m_renderTargetView); BackBuffer->Release(); // Describe our Depth/Stencil Buffer D3D11_TEXTURE2D_DESC depthBufferDesc; depthBufferDesc.Width = width; depthBufferDesc.Height = height; depthBufferDesc.MipLevels = 1; depthBufferDesc.ArraySize = 1; depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthBufferDesc.SampleDesc.Count = 1; depthBufferDesc.SampleDesc.Quality = 0; depthBufferDesc.Usage = D3D11_USAGE_DEFAULT; depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; depthBufferDesc.CPUAccessFlags = 0; depthBufferDesc.MiscFlags = 0; // Create the Depth/Stencil View m_device->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer); // Create disabled depth stencil state for 2d objects CreateDisabledDepthStencilState(); // Create default depth stencil state CreateDepthStencilState(); // Set the depth stencil state. m_deviceContext->OMSetDepthStencilState(m_depthStencilState, 1); // Initialize the depth stencil view. D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc; ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc)); // Set up the depth stencil view description. depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; depthStencilViewDesc.Texture2D.MipSlice = 0; // Initialize the depth stencil view. m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView); // Set our Render Target m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView); // Create the Viewport //D3D11_VIEWPORT viewport; //ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT)); m_viewport.Width = (float)width; m_viewport.Height = (float)height; m_viewport.MinDepth = 0.0f; m_viewport.MaxDepth = 1.0f; m_viewport.TopLeftX = 0.0f; m_viewport.TopLeftY = 0.0f; // Set the Viewport m_deviceContext->RSSetViewports(1, &m_viewport); // Create default raster state for opaque objects CreateRasterState(); // Create no cull raster state to render primitives behind alpha CreateNoCullRasterState(); CreateEnabledAlphaBlendingState(); CreateDisabledAlphaBlendingState(); TurnOnDefaultCulling(); // Set global matrices XMMATRIX world = XMMatrixIdentity(); XMStoreFloat4x4(&m_world, world); XMMATRIX projection = XMMatrixPerspectiveFovLH(XM_PIDIV4, (float)width / (float)height, 0.1f, 1000.0f); XMStoreFloat4x4(&m_projection, projection); XMMATRIX ortho = XMMatrixOrthographicLH((float)width, (float)height, 0.1f, 1000.0f); XMStoreFloat4x4(&m_ortho, ortho); return true; }
bool Projekt::Init() { if (!D3D11App::Init()) return false; // Initialize effects, input layouts and texture manager Effects::InitAll(mDirect3D->GetDevice()); InputLayouts::InitAll(mDirect3D->GetDevice()); mTextureMgr.init(mDirect3D->GetDevice()); // Initialize wireframe render state D3D11_RASTERIZER_DESC wireFrameDesc; ZeroMemory(&wireFrameDesc, sizeof(D3D11_RASTERIZER_DESC)); wireFrameDesc.FillMode = D3D11_FILL_WIREFRAME; wireFrameDesc.CullMode = D3D11_CULL_BACK; wireFrameDesc.FrontCounterClockwise = false; wireFrameDesc.DepthClipEnable = true; HR(mDirect3D->GetDevice()->CreateRasterizerState(&wireFrameDesc, &WireFrameRS)); //-------------------------------------------------------- // Create sky //-------------------------------------------------------- mSky = new Sky(mDirect3D->GetDevice(), L"Data/Textures/snowcube1024.dds", 5000.0f); //-------------------------------------------------------- // Create terrain //-------------------------------------------------------- // Describe terrain Terrain::InitInfo tInitInfo; tInitInfo.HeightMapFilename = L"Data/Textures/Heightmaps/terrain.raw"; tInitInfo.LayerMapFilename0 = L"Data/Textures/grass.dds"; tInitInfo.LayerMapFilename1 = L"Data/Textures/darkdirt.dds"; tInitInfo.LayerMapFilename2 = L"Data/Textures/stone.dds"; tInitInfo.LayerMapFilename3 = L"Data/Textures/lightdirt.dds"; tInitInfo.LayerMapFilename4 = L"Data/Textures/snow.dds"; tInitInfo.BlendMapFilename = L"Data/Textures/blend.dds"; tInitInfo.HeightScale = 50.0f; tInitInfo.HeightmapWidth = 2049; tInitInfo.HeightmapHeight = 2049; tInitInfo.CellSpacing = 0.5f; // Initialize terrain mTerrain.Init(mDirect3D->GetDevice(), mDirect3D->GetImmediateContext(), tInitInfo); //-------------------------------------------------------- // Particle systems //-------------------------------------------------------- mRandomTexSRV = d3dHelper::CreateRandomTexture1DSRV(mDirect3D->GetDevice()); std::vector<std::wstring> flares; flares.push_back(L"Data\\Textures\\flare0.dds"); mFlareTexSRV = d3dHelper::CreateTexture2DArraySRV(mDirect3D->GetDevice(), mDirect3D->GetImmediateContext(), flares); mFire.init(mDirect3D->GetDevice(), Effects::FireFX, mFlareTexSRV, mRandomTexSRV, 500); mFire.setEmitPos(XMFLOAT3(65.0f, 5.0f, 0.0f)); //-------------------------------------------------------- // Create shadow map //-------------------------------------------------------- mShadowMapSize = 2048; mShadowMap = new ShadowMap(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize); //-------------------------------------------------------- // Load models //-------------------------------------------------------- mGenericModel = new GenericModel(mDirect3D->GetDevice(), mTextureMgr, "Data\\Models\\Collada\\duck.dae", L"Data\\Models\\Collada\\"); //mSkinnedModel = new GenericSkinnedModel(mDirect3D->GetDevice(), mTextureMgr, "Data\\Models\\Collada\\AnimTest\\test_Collada_DAE.dae", L"Data\\Models\\Collada\\AnimTest\\"); mPlayerModel = new GenericModel(mDirect3D->GetDevice(), mTextureMgr, "Data\\Models\\OBJ\\Cop\\cop.obj", L"Data\\Models\\OBJ\\Cop\\"); Player::InitProperties playerProp; playerProp.PlayerID = 0; playerProp.Nickname = "Hyzor"; playerProp.Speed = 1.0f; playerProp.Health = 1.0f; playerProp.Position = XMFLOAT3(0.0f, 0.0f, 0.0f); playerProp.Scale = XMFLOAT3(1.0f, 1.0f, 1.0f); playerProp.Angle = 0.0f; playerProp.Model = mPlayerModel; mPlayer.Init(playerProp); //-------------------------------------------------------- // Create model instances //-------------------------------------------------------- GenericModelInstance genericInstance; genericInstance.model = mGenericModel; // GenericSkinnedModelInstance genSkinnedInstance; // genSkinnedInstance.model = mSkinnedModel; // genSkinnedInstance.FinalTransforms.resize(mSkinnedModel->skinnedData.getBoneCount()); // genSkinnedInstance.ClipName = "animation"; // genSkinnedInstance.TimePos = 0.0f; //-------------------------------------------------------- // Scale, rotate and move model instances //-------------------------------------------------------- XMMATRIX modelScale = XMMatrixScaling(1.0f, 1.0f, 1.0f); XMMATRIX modelRot = XMMatrixRotationY(0.0f); XMMATRIX modelOffset = XMMatrixTranslation(0.0f, 0.0f, 0.0f); //modelScale = XMMatrixScaling(0.1f, 0.1f, 0.1f); modelOffset = XMMatrixTranslation(-30.0f, 15.0f, -110.0f); XMStoreFloat4x4(&genericInstance.world, modelScale*modelRot*modelOffset); // modelOffset = XMMatrixTranslation(50.0f, 15.0f, -110.0f); // XMStoreFloat4x4(&genSkinnedInstance.world, modelScale*modelRot*modelOffset); //-------------------------------------------------------- // Insert model instances to the vector //-------------------------------------------------------- mGenericInstances.push_back(genericInstance); /* mGenSkinnedInstances.push_back(genSkinnedInstance);*/ //-------------------------------------------------------- // Compute scene bounding box //-------------------------------------------------------- XMFLOAT3 minPt(+MathHelper::infinity, +MathHelper::infinity, +MathHelper::infinity); XMFLOAT3 maxPt(-MathHelper::infinity, -MathHelper::infinity, -MathHelper::infinity); // Get vertex positions from all models for (UINT i = 0; i < mGenericInstances.size(); ++i) { for (UINT j = 0; j < mGenericInstances[i].model->vertices.size(); ++j) { XMFLOAT3 vPos = mGenericInstances[i].model->vertices[j]->position; minPt.x = MathHelper::getMin(minPt.x, vPos.x); minPt.y = MathHelper::getMin(minPt.x, vPos.x); minPt.z = MathHelper::getMin(minPt.x, vPos.x); maxPt.x = MathHelper::getMax(maxPt.x, vPos.x); maxPt.y = MathHelper::getMax(maxPt.x, vPos.x); maxPt.z = MathHelper::getMax(maxPt.x, vPos.x); } } // Get vertex positions from all skinned models // for (UINT i = 0; i < mGenSkinnedInstances.size(); ++i) // { // for (UINT j = 0; j < mGenSkinnedInstances[i].model->vertices.size(); ++j) // { // XMFLOAT3 vPos = mGenSkinnedInstances[i].model->vertices[j]->position; // // minPt.x = MathHelper::getMin(minPt.x, vPos.x); // minPt.y = MathHelper::getMin(minPt.x, vPos.x); // minPt.z = MathHelper::getMin(minPt.x, vPos.x); // // maxPt.x = MathHelper::getMax(maxPt.x, vPos.x); // maxPt.y = MathHelper::getMax(maxPt.x, vPos.x); // maxPt.z = MathHelper::getMax(maxPt.x, vPos.x); // } // } // Now continue with terrain vertex positions for (UINT i = 0; i < mTerrain.getPatchVertices().size(); ++i) { XMFLOAT3 vPos = mTerrain.getPatchVertices()[i].position; minPt.x = MathHelper::getMin(minPt.x, vPos.x); minPt.y = MathHelper::getMin(minPt.x, vPos.x); minPt.z = MathHelper::getMin(minPt.x, vPos.x); maxPt.x = MathHelper::getMax(maxPt.x, vPos.x); maxPt.y = MathHelper::getMax(maxPt.x, vPos.x); maxPt.z = MathHelper::getMax(maxPt.x, vPos.x); } // Sphere center is at half of these new dimensions mSceneBounds.Center = XMFLOAT3( 0.5f*(minPt.x + maxPt.x), 0.5f*(minPt.y + maxPt.y), 0.5f*(minPt.z + maxPt.z)); // Calculate the sphere radius XMFLOAT3 extent(0.5f*(maxPt.x - minPt.x), 0.5f*(maxPt.y - minPt.y), 0.5f*(maxPt.z - minPt.z)); mSceneBounds.Radius = sqrtf(extent.x*extent.x + extent.y*extent.y + extent.z*extent.z); OnResize(); return true; }
void Camera::SetPerspective(float p_Angle, float p_Width, float p_Height, float p_Near, float p_Far) { XMMATRIX p_Proj = XMMatrixPerspectiveFovLH(p_Angle, p_Width/p_Height, p_Near, p_Far); XMStoreFloat4x4(&m_Proj, p_Proj); }
void Camera::SetOrtogonal(float p_Width, float p_Height, float p_Near, float p_Far) { XMMATRIX p_Proj = XMMatrixOrthographicLH(p_Width, p_Height, p_Near, p_Far); XMStoreFloat4x4(&m_Proj, p_Proj); }
void RenderEngine::Render(){ static float rot = 0.00f; UINT32 vertexSize = sizeof(float) * 8; UINT32 offset = 0; rot += 0.01; float clearColor[] = { 0.15f,0.6f,1.0f, 0.2f }; gDeviceContext->OMSetBlendState(0, 0, 0xffffffff); gDeviceContext->ClearRenderTargetView(gBackRufferRenderTargetView, clearColor); gDeviceContext->ClearDepthStencilView(gDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); mainCamera.setPlayerXPos(theCharacter->xPos); mainCamera.setPlayerYPos(theCharacter->yPos); mainCamera.updateCamera(); //WORLD XMMATRIX YRotation = XMMatrixRotationY(rot); //The Camera Matrices are now defined in the camera class (mainCamera) XMMATRIX CamView = mainCamera.getCamView(); XMMATRIX CamProjection = mainCamera.getCamProjection(); XMMATRIX identityM = XMMatrixIdentity(); XMMATRIX WorldInv = XMMatrixInverse(nullptr, XMMatrixIdentity()); World perObjCBData; XMMATRIX WVP; WVP = identityM* CamView*CamProjection; XMStoreFloat4x4(&perObjCBData.WVP, XMMatrixTranspose(WVP)); XMStoreFloat4x4(&perObjCBData.View, XMMatrixTranspose(CamView)); XMStoreFloat4x4(&perObjCBData.Projection, XMMatrixTranspose(CamProjection)); XMStoreFloat4x4(&perObjCBData.WorldSpace, XMMatrixTranspose(XMMatrixIdentity())); XMStoreFloat4x4(&perObjCBData.InvWorld, XMMatrixTranspose(WorldInv)); gDeviceContext->UpdateSubresource(gWorld, 0, NULL, &perObjCBData, 0, 0); gDeviceContext->VSSetConstantBuffers(0, 1, &gWorld); gDeviceContext->IASetVertexBuffers(0, 1, &gVertexBuffer, &vertexSize, &offset); ID3D11Buffer *pShaderBuffers[2] = { matConstBuff, lightConstBuff }; gDeviceContext->PSSetConstantBuffers(0, 2, pShaderBuffers); //RENDER OBJ FILES for each (GameObject var in gamePlatforms) { gDeviceContext->PSSetShaderResources(0, 1, &ddsTex1); gDeviceContext->IASetInputLayout(gVertexLayout); gDeviceContext->IASetVertexBuffers(0, 1, &var.vertexBuffer, &vertexSize, &offset); gDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); gDeviceContext->VSSetShader(gVertexShader, nullptr, 0); gDeviceContext->HSSetShader(nullptr, nullptr, 0); gDeviceContext->DSSetShader(nullptr, nullptr, 0); gDeviceContext->PSSetShader(gPixelShader, nullptr, 0); var.CalculateWorld(); var.material = MatPresets::Emerald; var.material.SpecPow = 38.0f; matProperties.Material = var.material; gDeviceContext->UpdateSubresource(matConstBuff, 0, nullptr, &matProperties, 0, 0); XMStoreFloat4x4(&perObjCBData.InvWorld, XMMatrixTranspose(XMMatrixInverse(nullptr, var.world))); XMStoreFloat4x4(&perObjCBData.WorldSpace, XMMatrixTranspose(var.world)); WVP = XMMatrixIdentity(); WVP = var.world * CamView *CamProjection; XMStoreFloat4x4(&perObjCBData.WVP, XMMatrixTranspose(WVP)); gDeviceContext->UpdateSubresource(gWorld, 0, NULL, &perObjCBData, 0, 0); gDeviceContext->VSSetConstantBuffers(0, 1, &gWorld); gDeviceContext->Draw(var.nrElements * 3, 0); }
void Triangle::setPosition(CXMVECTOR a_Position) { XMMATRIX transform = XMMatrixTranslationFromVector(a_Position); XMStoreFloat4x4(&m_Transform, transform); }
void CameraObj::UpdateCBuffer(UINT screenWidth, UINT screenHeight) { TransformData tDataTemp = transform->transformData; //XMVECTOR pos = XMVectorSet(tDataTemp.pos.x, tDataTemp.pos.y, tDataTemp.pos.z, 1.0f); //XMVECTOR rotTemp = XMVectorSet(tDataTemp.rot.x, tDataTemp.rot.y, tDataTemp.rot.z, tDataTemp.rot.w); ////Load the stuff XMFLOAT4 rotQuad = XMFLOAT4(tDataTemp.rot.x, tDataTemp.rot.y, tDataTemp.rot.z, tDataTemp.rot.w); //använd denna sen //XMFLOAT4 rotQuad = XMFLOAT4(0, 0, -1, 0); //hårkodad XMVECTOR rotQuadVec = XMLoadFloat4(&rotQuad); rotQuadVec = XMVector4Normalize(rotQuadVec); XMFLOAT3 pos = XMFLOAT3(tDataTemp.pos.x, tDataTemp.pos.y, tDataTemp.pos.z); //XMFLOAT3 pos = XMFLOAT3(0, 0, 200);//hårkodad XMVECTOR posVec = XMLoadFloat3(&pos); ////Load standard vectors XMFLOAT3 startUp = XMFLOAT3(0, 1, 0); XMVECTOR startUpVec = XMLoadFloat3(&startUp); XMFLOAT3 startTar = XMFLOAT3(0, 0, 1); XMVECTOR startTarVec = XMLoadFloat3(&startTar); XMMATRIX rotMatrix = XMMatrixRotationQuaternion(rotQuadVec); startUpVec = XMVector3Transform(startUpVec, rotMatrix); startTarVec = XMVector3Transform(startTarVec, rotMatrix); XMFLOAT3 derpTar; XMStoreFloat3(&derpTar, startTarVec); XMMATRIX cameraMat = XMMatrixLookToLH(posVec, startTarVec, startUpVec); //XMVECTOR rot = XMVector3Rotate(XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f), rotTemp); //+ positionen eller nått sånt, se denna //XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); //XMMATRIX view = XMMatrixLookAtRH(pos, rot, up); XMMATRIX projection; if (cameraData.isOrtho == 0) { float aspect = (float)screenWidth / (float)screenHeight; projection = XMMatrixPerspectiveFovLH( cameraData.hAngle,//cameraData.hAngle, aspect, //aspect ratio? 1.0f, 4000 ); } else { float fovinv = (float)screenHeight / (float)screenWidth; projection = XMMatrixOrthographicLH(cameraData.hAngle, cameraData.hAngle * fovinv, 1.0f, 4000.0f); } XMMATRIX view = cameraMat; XMStoreFloat4x4(&cameraCBufferData.view, XMMatrixTranspose(view)); XMStoreFloat4x4(&cameraCBufferData.projection, XMMatrixTranspose(projection)); cameraCBufferData.cameraPos[0] = transform->transformData.pos.x; cameraCBufferData.cameraPos[1] = transform->transformData.pos.y; cameraCBufferData.cameraPos[2] = transform->transformData.pos.z; cameraCBufferData.cameraPos[3] = 1; gDeviceContext->UpdateSubresource(cameraCbuffer, 0, NULL, &cameraCBufferData, 0, 0); }
LitSkullApp::LitSkullApp(HINSTANCE hInstance) : D3DApp(hInstance), mShapesVB(0), mShapesIB(0), mSkullVB(0), mSkullIB(0), mSkullIndexCount(0), mLightCount(1), mEyePosW(0.0f, 0.0f, 0.0f), mTheta(1.5f*MathHelper::Pi), mPhi(0.1f*MathHelper::Pi), mRadius(15.0f) { mMainWndCaption = L"LitSkull Demo"; mLastMousePos.x = 0; mLastMousePos.y = 0; XMMATRIX I = XMMatrixIdentity(); XMStoreFloat4x4(&mGridWorld, I); XMStoreFloat4x4(&mView, I); XMStoreFloat4x4(&mProj, I); XMMATRIX boxScale = XMMatrixScaling(3.0f, 1.0f, 3.0f); XMMATRIX boxOffset = XMMatrixTranslation(0.0f, 0.5f, 0.0f); XMStoreFloat4x4(&mBoxWorld, XMMatrixMultiply(boxScale, boxOffset)); XMMATRIX skullScale = XMMatrixScaling(0.5f, 0.5f, 0.5f); XMMATRIX skullOffset = XMMatrixTranslation(0.0f, 1.0f, 0.0f); XMStoreFloat4x4(&mSkullWorld, XMMatrixMultiply(skullScale, skullOffset)); for(int i = 0; i < 5; ++i) { XMStoreFloat4x4(&mCylWorld[i*2+0], XMMatrixTranslation(-5.0f, 1.5f, -10.0f + i*5.0f)); XMStoreFloat4x4(&mCylWorld[i*2+1], XMMatrixTranslation(+5.0f, 1.5f, -10.0f + i*5.0f)); XMStoreFloat4x4(&mSphereWorld[i*2+0], XMMatrixTranslation(-5.0f, 3.5f, -10.0f + i*5.0f)); XMStoreFloat4x4(&mSphereWorld[i*2+1], XMMatrixTranslation(+5.0f, 3.5f, -10.0f + i*5.0f)); } mDirLights[0].Ambient = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f); mDirLights[0].Diffuse = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f); mDirLights[0].Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f); mDirLights[0].Direction = XMFLOAT3(0.57735f, -0.57735f, 0.57735f); mDirLights[1].Ambient = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f); mDirLights[1].Diffuse = XMFLOAT4(0.20f, 0.20f, 0.20f, 1.0f); mDirLights[1].Specular = XMFLOAT4(0.25f, 0.25f, 0.25f, 1.0f); mDirLights[1].Direction = XMFLOAT3(-0.57735f, -0.57735f, 0.57735f); mDirLights[2].Ambient = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f); mDirLights[2].Diffuse = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f); mDirLights[2].Specular = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f); mDirLights[2].Direction = XMFLOAT3(0.0f, -0.707f, -0.707f); mGridMat.Ambient = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f); mGridMat.Diffuse = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f); mGridMat.Specular = XMFLOAT4(0.2f, 0.2f, 0.2f, 16.0f); mCylinderMat.Ambient = XMFLOAT4(0.7f, 0.85f, 0.7f, 1.0f); mCylinderMat.Diffuse = XMFLOAT4(0.7f, 0.85f, 0.7f, 1.0f); mCylinderMat.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 16.0f); mSphereMat.Ambient = XMFLOAT4(0.1f, 0.2f, 0.3f, 1.0f); mSphereMat.Diffuse = XMFLOAT4(0.2f, 0.4f, 0.6f, 1.0f); mSphereMat.Specular = XMFLOAT4(0.9f, 0.9f, 0.9f, 16.0f); mBoxMat.Ambient = XMFLOAT4(0.651f, 0.5f, 0.392f, 1.0f); mBoxMat.Diffuse = XMFLOAT4(0.651f, 0.5f, 0.392f, 1.0f); mBoxMat.Specular = XMFLOAT4(0.2f, 0.2f, 0.2f, 16.0f); mSkullMat.Ambient = XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f); mSkullMat.Diffuse = XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f); mSkullMat.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 16.0f); }
void CascadedShadowMapper::VRender(ISceneGraph* graph) { IRenderer* renderer = CmGetApp()->VGetHumanView()->VGetRenderer(); IDeviceTexture* vn[4]; for(UCHAR i = 0; i < m_cascades + 1; ++i) { vn[i] = NULL; } renderer->VSetTextures(eEffect0, (vn+1), m_cascades); ICamera* playerView = graph->VGetCamera().get(); Frustum cascadeFrusta; Frustum ortographicFrusta; CameraComponent* lcc = GetActorCompnent<CameraComponent>(m_lightActorCamera, CM_CMP_CAMERA); //std::shared_ptr<chimera::CameraComponent> vcc = m_viewActor->GetComponent<chimera::CameraComponent>(chimera::CameraComponent::COMPONENT_ID).lock(); renderer->VPushViewTransform(lcc->GetCamera()->GetView(), lcc->GetCamera()->GetIView(), lcc->GetCamera()->GetEyePos(), lcc->GetCamera()->GetViewDir()); ICamera* lightCamera = lcc->GetCamera().get(); //util::ICamera* viewCamera = vcc->GetCamera().get(); ICamera* viewCamera = playerView; //util::Mat4 viewToLight = util::Mat4::Mul(lightCamera->GetView(), viewCamera->GetIView());//vcc->GetCamera()->GetIView()); float distances[3]; for(UCHAR ci = 0; ci < m_cascades; ++ci) { CascadeSettings& settings = m_pCascadesSettings[ci]; /*util::StaticCamera* staticCam; staticCam = (util::StaticCamera*)(m_cascadeCameraActor[ci]->GetComponent<chimera::CameraComponent>(chimera::CameraComponent::COMPONENT_ID).lock()->GetCamera().get());*/ float farr = settings.end; float nnear = settings.start; cascadeFrusta.CreatePerspective(viewCamera->GetAspect(), viewCamera->GetFoV(), nnear, farr); util::Vec3 vmin(FLT_MAX, FLT_MAX, FLT_MAX); util::Vec3 vmax(FLT_MIN, FLT_MIN, FLT_MIN); util::Vec3 vFrustumPoints[8]; util::Vec3 vecs[8]; for(uint i = 0; i < 8; ++i) { util::Vec3 v = util::Mat4::Transform(viewCamera->GetIView(), cascadeFrusta.GetPoints()[i]); vFrustumPoints[i] = v; util::Vec3 pos = util::Mat4::Transform(lightCamera->GetView(), v); vmax = util::Vec3::Max(vmax, pos); vmin = util::Vec3::Min(vmin, pos); } /*FLOAT bound = (farr - nnear) / 1024.0f; util::Vec3 fmin((INT)vmin.x / bound, (INT)vmin.y / bound, (INT)vmin.y / bound); fmin.Scale(bound); vmin = fmin; util::Vec3 fmax((INT)(vmax.x / bound), (INT)(vmax.y / bound), (INT)(vmax.y / bound)); fmax.Scale(bound); vmax = fmax; */ /*vmax = util::Mat4::Transform(lightCamera->GetIView(), vmax); vmin = util::Mat4::Transform(lightCamera->GetIView(), vmin); */ /*util::Vec3 vDiagonal = vFrustumPoints[tbd::rightUpNear] - vFrustumPoints[tbd::leftDownFar]; FLOAT l = vDiagonal.Length(); vDiagonal.Set(l, l, l); // The offset calculated will pad the ortho projection so that it is always the same size // and big enough to cover the entire cascade interval. util::Vec3 diff = vmax - vmin; util::Vec3 vBoarderOffset = vDiagonal - diff; vBoarderOffset.Scale(0.5f); vBoarderOffset.z = 0; // Add the offsets to the projection. vmax = vmax + vBoarderOffset; vmin = vmin - vBoarderOffset; */ float n = min(-120, vmin.z); //todo float ff = vmax.z; distances[ci] = ff - n; XMMATRIX mat = XMMatrixOrthographicOffCenterLH(vmin.x, vmax.x, vmin.y, vmax.y, n, ff); XMStoreFloat4x4(&settings.m_projection.m_m, mat); /* util::StaticCamera staticCam(1,1,1,1); #ifdef CSM_DEBUG staticCam.SetOrthographicProjectionOffCenter(vmin.x, vmax.x, vmin.y, vmax.y, n, ff); staticCam.SetView(lightCamera->GetView(), lightCamera->GetIView()); ortographicFrusta = staticCam.GetFrustum(); #endif*/ ortographicFrusta.CreateOrthographicOffCenter(vmin.x, vmax.x, vmin.y, vmax.y, n, ff); ortographicFrusta.Transform(lightCamera->GetIView()); renderer->VPushProjectionTransform(settings.m_projection, ff - n, 1); m_ppTargets[ci]->VClear(); m_ppTargets[ci]->VBind(); graph->VPushFrustum(&ortographicFrusta); m_pProgram->VBind(); graph->VOnRender(CM_RENDERPATH_SHADOWMAP); m_pProgramInstanced->VBind(); graph->VOnRender(CM_RENDERPATH_SHADOWMAP_INSTANCED); /*m_pProgramInstanced->VBind(); graph->VOnRender(eRenderPath_DrawToShadowMapInstanced);*/ graph->VPopFrustum(); renderer->VPopProjectionTransform(); m_ppBlurChain[ci]->VProcess(); // d3d::GetContext()->GenerateMips(m_ppBlurredTargets[ci]->GetShaderRessourceView()); } renderer->VPopViewTransform(); CmGetApp()->VGetHumanView()->VGetRenderer()->VGetCurrentRenderTarget()->VBind(); IDeviceTexture* v[3]; for(UCHAR i = 0; i < m_cascades; ++i) { v[i] = m_ppBlurredTargets[i]->VGetTexture(); } renderer->VSetTextures(eEffect0, v, m_cascades); //ID3D11ShaderResourceView* debugView = m_ppBlurredTargets[0]->GetShaderRessourceView(); //d3d::GetContext()->PSSetShaderResources(d3d::eEffect3, 1, &debugView); //debugging samplers util::Mat4 mats[3]; //TODO //FLOAT distances[3]; for(UCHAR i = 0; i < m_cascades; ++i) { mats[i] = m_pCascadesSettings[i].m_projection; } IConstShaderBuffer* lb = renderer->VGetConstShaderBuffer(eEnvLightingBuffer); _LightingBuffer* _lb = (_LightingBuffer*)lb->VMap(); _lb->m_view = lightCamera->GetView().m_m; _lb->m_iView = lightCamera->GetIView().m_m; _lb->m_projection[0] = mats[0].m_m; _lb->m_projection[1] = mats[1].m_m; _lb->m_projection[2] = mats[2].m_m; _lb->m_lightPos.x = lcc->GetCamera()->GetEyePos().x; _lb->m_lightPos.y = lcc->GetCamera()->GetEyePos().y; _lb->m_lightPos.z = lcc->GetCamera()->GetEyePos().z; _lb->m_intensity.x = m_intensity.x; _lb->m_intensity.y = m_intensity.y; _lb->m_intensity.z = m_intensity.z; _lb->m_ambient.x = m_ambient.x; _lb->m_ambient.y = m_ambient.y; _lb->m_ambient.z = m_ambient.z; _lb->m_distances.x = distances[0]; _lb->m_distances.y = distances[1]; _lb->m_distances.z = distances[2]; lb->VUnmap(); // renderer->SetCSMSettings(lightCamera->GetView(), lightCamera->GetIView(), mats, lcc->GetCamera()->GetEyePos(), distances); }