void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer g_pDevice->ClearRenderTargetView(pRenderTargetView, &vClearColor[0]); g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH|D3D10_CLEAR_STENCIL, 1.0f, 0); Matrix4x4 camera_matrix = g_Control.GetCameraMatrix(); Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix = g_Control.GetObjectMatrix(); g_Model_DX10.SetLight(0, g_Lights[0]); g_Model_DX10.SetLight(1, g_Lights[1]); g_Model_DX10.EnableLighting(true); g_Model_DX10.SetProjectionMatrix(g_proj_matrix); g_Model_DX10.SetWorldMatrix(world_matrix); g_Model_DX10.SetViewMatrix(view_matrix); g_Model_DX10.SetInvViewMatrix(camera_matrix); g_Model_DX10.UpdateMatrix(); g_Model_DX10.Render(); // 等待硬體掃結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vector4); UINT offset = 0; // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定vertex buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 設定要畫線 g_pDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_LINELIST ); // 畫線 g_pDevice->Draw(16, 0); // 更新畫面 pSwapChain->Present(0, 0); }
void RenderFrameDX10(void) { int w, h; GutGetWindowSize(w, h); Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vertex_VT); UINT offset = 0; ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer D3D10_VIEWPORT vp = {0, 0, w, h, 0.0f, 1.0f}; g_pDevice->RSSetViewports(1, &vp); g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL); // 設定Shader g_pDevice->VSSetShader(g_pBlurVS); g_pDevice->PSSetShader(g_pRGBAPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pVSConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); g_pDevice->PSSetShaderResources(0, 1, &g_pTexture); g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); g_pDevice->Draw(4, 0); if ( g_bPosteffect ) { ID3D10ShaderResourceView *pRSView = NULL; pRSView = BrightnessImage(g_pTexture, &g_ImageInfo); pRSView = BlurImage(pRSView, &g_ImageInfo); g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL); g_pDevice->RSSetViewports(1, &vp); float dummy[] = {0,0,0,0}; g_pDevice->OMSetBlendState(g_pAddBlend, dummy, 0xffffffff); // 設定Shader g_pDevice->VSSetShader(g_pBlurVS); g_pDevice->PSSetShader(g_pRGBAPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pVSConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); g_pDevice->PSSetShaderResources(0, 1, &pRSView); g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); g_pDevice->Draw(4, 0); g_pDevice->OMSetBlendState(g_pNoBlend, dummy, 0xffffffff); } // 等待硬體掃結束, 然後才更新畫面 IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); pSwapChain->Present(1, 0); }
//------------------------------------------------------------------ //! @brief ゲームの描画 //! //! @param なし //! //! @return なし //------------------------------------------------------------------ void GameMain::Render() { // ダイレクト3Dの取得 Direct3D* direct3D = Direct3D::GetInstance(); // デバイスリソースの取得 ID3D11DeviceContext* pDeviceContext = direct3D->GetDeviceContext(); ID3D11RenderTargetView* pRenderTargetView = direct3D->GetRenderTargetView(); //深度バッファの取得 ID3D11DepthStencilView* pDepthStencilView = direct3D->GetDepthStencilView(); IDXGISwapChain* pSwapChain = direct3D->GetSwapChain(); // レンダーターゲットのクリア pDeviceContext->ClearRenderTargetView(pRenderTargetView, Colors::DarkBlue); //深度バッファのクリア pDeviceContext->ClearDepthStencilView(pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); // シーンマネージャの描画 m_pSceneManager->Render(); // 画面のフリップ pSwapChain->Present(0, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // /* sModelLight light; light.vPosition = g_Lights[0].m_Position; light.vDirection = g_Lights[0].m_Direction; light.vDiffuse = g_Lights[0].m_Diffuse; light.vSpecular = g_Lights[0].m_Specular; */ //g_Heightmap.Render(g_Control.GetObjectMatrix(), g_Control.GetViewMatrix(), g_proj_matrix, &light); g_Heightmap.Render(g_Control.GetObjectMatrix(), g_Control.GetViewMatrix(), g_proj_matrix); // 等待硬體掃結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix = g_Control.GetObjectMatrix(); Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vertex_VT); UINT offset = 0; ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); g_pDevice->OMSetRenderTargets(1, &g_pRTView[FULLSIZE], pDepthStencilView); g_pDevice->ClearRenderTargetView(g_pRTView[FULLSIZE], &vClearColor[0]); g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH|D3D10_CLEAR_STENCIL, 1.0f, 0); SetViewport(FULLSIZE); CGutModel_DX10::SetViewMatrix(view_matrix); CGutModel_DX10::SetWorldMatrix(world_matrix); CGutModel_DX10::SetProjectionMatrix(g_proj_matrix); CGutModel_DX10::UpdateMatrix(); g_Model_DX10.Render(); ConvertToLuminance(); Average(); AdaptiveLuminance(); AutoExposure(); if ( g_iMode & 0x01 ) { g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, pDepthStencilView); SetViewport(FULLSIZE); float x = -1.0f; float y = -1.0f; float w = 0.2f; float h = 0.2f; DrawImage(g_pSRView[LUMINANCE_CURRENT], x, y, w, h); x+=w; DrawImage(g_pSRView[LUMINANCE_PREVIOUS], x, y, w, h); x+=w; } // 等待硬體掃結束, 然後才更新畫面 IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); // frame buffer ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); // depth/stencil buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); // front/back buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 取得轉換矩陣 Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix = g_Control.GetObjectMatrix(); Matrix4x4 ident_matrix = Matrix4x4::IdentityMatrix(); CGutModel_DX10::SetProjectionMatrix(g_proj_matrix); CGutModel_DX10::SetViewMatrix(view_matrix); // 畫出茶具組 { g_pDevice->OMSetDepthStencilState(NULL, 1); CGutModel_DX10::SetWorldMatrix(world_matrix); CGutModel_DX10::UpdateMatrix(); g_Model_DX10.Render(); } // stencil pass, 在stencil buffer里標出光源照射到的像素 { g_pDevice->OMSetDepthStencilState(g_pZStencil_Mask, 1); CGutModel_DX10::SetWorldMatrix(ident_matrix); CGutModel_DX10::UpdateMatrix(); CGutModel_DX10::SetMaterialOverwrite(&g_material_stencilpass); g_SpotLightModel_DX10.Render(); } // 畫出被光線照射到的區域 { g_pDevice->OMSetDepthStencilState(g_pZStencil_Test, 1); CGutModel_DX10::SetMaterialOverwrite(&g_material_spotlightpass); g_SpotLightModel_DX10.Render(); } // 畫出光柱圓錐 { g_pDevice->OMSetDepthStencilState(NULL, 1); CGutModel_DX10::SetMaterialOverwrite(NULL); g_SpotLightModel_DX10.Render(); } // 等待硬體掃結束, 然後才更新畫面. pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vertex_VCN); UINT offset = 0; // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定vertex buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 設定index buffer g_pDevice->IASetIndexBuffer(g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // 計算矩陣 Matrix4x4 view_proj_matrix = g_view_matrix * g_proj_matrix; Matrix4x4 world_view_proj_matrix = g_world_matrix * view_proj_matrix; // 設定shader參數 Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // 更新頂點資料 Vertex_VCN *pVertex; g_pVertexBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pVertex ); memcpy(pVertex, g_pGridVertices, sizeof(Vertex_VCN) * g_iNumGridVertices); g_pVertexBuffer->Unmap(); // 更新索引值陣列 unsigned short *pIndex; g_pIndexBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pIndex ); memcpy(pIndex, g_pGridIndices, sizeof(unsigned short) * g_iNumGridIndices); g_pIndexBuffer->Unmap(); // 畫出格子 g_pDevice->DrawIndexed(g_iNumGridIndices, 0, 0); // 等待硬體掃描結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 取得主畫面 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINESTRIP); ID3D10EffectTechnique *g_pShader = NULL; switch(g_iMode) { default: case 1: g_pShader = g_pDrawCurveFX->GetTechniqueByName("SinCurve"); break; case 2: { g_pShader = g_pDrawCurveFX->GetTechniqueByName("ZCurve"); Vector4 vNearFar(1.0f, 100.0f, 0.0f, 0.0f); Matrix4x4 proj_matrix = GutMatrixPerspectiveRH_DirectX(45.0f, 1.0f, vNearFar[0], vNearFar[1]); //Matrix4x4 proj_matrix = GutMatrixOrthoRH_DirectX(10.0f, 10.0f, vNearFar[0], vNearFar[1]); //Matrix4x4 proj_matrix = GutMatrixOrthoRH_OpenGL(10.0f, 10.0f, vNearFar[0], vNearFar[1]); //Matrix4x4 proj_matrix = GutMatrixPerspectiveRH_OpenGL(45.0f, 1.0f, vNearFar[0], vNearFar[1]); ID3D10EffectMatrixVariable *pMatrix = g_pDrawCurveFX->GetVariableByName("proj_matrix")->AsMatrix(); ID3D10EffectVectorVariable *pNearFarPlane = g_pDrawCurveFX->GetVariableByName("NearFarPlane")->AsVector(); pMatrix->SetMatrix(&proj_matrix[0][0]); pNearFarPlane->SetFloatVector(&vNearFar[0]); break; } } g_pShader->GetPassByIndex(0)->Apply(0); g_pDevice->Draw(2, 0); IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); // 清除晝面 Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 畫出文字 g_Font.Render(); // 等待硬體掃結束, 然後才更新畫面 IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vertex_VC); UINT offset = 0; // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定vertex buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 設定index buffer g_pDevice->IASetIndexBuffer(g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0); // 設定要畫線 g_pDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); // 設定座標轉換矩陣 Matrix4x4 world_view_proj_matrix = g_object_matrix * g_view_proj_matrix; // 設定shader參數 Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // 畫面 g_pDevice->DrawIndexed(18, 0, 0); // 等待硬體掃結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vertex_VT); UINT offset = 0; // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer int w, h; GutGetWindowSize(w, h); D3D10_RECT rect = {0, 0, w, h/2}; g_pDevice->RSSetScissorRects(1, &rect); // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 套用貼圖 g_pDevice->PSSetShaderResources(0, 1, &g_pTexture); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定vertex buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // 計算矩陣 Matrix4x4 view_proj_matrix = g_view_matrix * g_proj_matrix; Matrix4x4 world_view_proj_matrix = g_world_matrix * view_proj_matrix; // 更新shader參數 Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // 畫出格子 g_pDevice->Draw(4, 0); // 等待硬體掃結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vertex_VT); UINT offset = 0; // `取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件` ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); // front/back buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // `清除顏色` g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // `清除Depth/Stencil buffer` g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // `設定vertex shader` g_pDevice->VSSetShader(g_pVertexShader); // `設定pixel shader` g_pDevice->PSSetShader(g_pPixelShader); // `設定vertex shader讀取參數的記憶體位罝` g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // `套用貼圖` g_pDevice->PSSetShaderResources(0, 1, &g_pTexture); // `設定vertex資料格式` g_pDevice->IASetInputLayout(g_pVertexLayout); // `設定vertex buffer` g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // `設定三角形頂點索引值資料排列是triangle strip` g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // `計算矩陣` Matrix4x4 view_proj_matrix = g_view_matrix * g_proj_matrix; Matrix4x4 world_view_proj_matrix = g_world_matrix * view_proj_matrix; // `更新shader參數` Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); pConstData[0] = world_view_proj_matrix; pConstData[1] = g_texture_matrix; g_pConstantBuffer->Unmap(); // `畫出矩形` g_pDevice->Draw(4, 0); // `等待硬體掃結束, 然後才更新畫面.` pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f); Vector4 vObjectColor(0.0f, 1.0f, 0.0f); ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer g_pDevice->ClearRenderTargetView(pRenderTargetView, &vClearColor[0]); g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH|D3D10_CLEAR_STENCIL, 1.0f, 0); // background image DrawImage(g_pTexture); // refraction Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix = g_Control.GetObjectMatrix(); Matrix4x4 wvp_matrix = world_matrix * view_matrix * g_proj_matrix; Matrix4x4 wv_matrix = world_matrix * view_matrix; ID3D10EffectTechnique *pShader = g_pRefractionFX->GetTechniqueByName("Refraction"); ID3D10EffectShaderResourceVariable *texture_param = g_pRefractionFX->GetVariableByName("BackgroundImage")->AsShaderResource(); ID3D10EffectMatrixVariable *wvpmatrix_param = g_pRefractionFX->GetVariableByName("wvp_matrix")->AsMatrix(); ID3D10EffectMatrixVariable *wvmatrix_param = g_pRefractionFX->GetVariableByName("wv_matrix")->AsMatrix(); ID3D10EffectVectorVariable *color_param = g_pRefractionFX->GetVariableByName("object_color")->AsVector(); texture_param->SetResource(g_pTexture); wvpmatrix_param->SetMatrix( (float *)&wvp_matrix ); wvmatrix_param->SetMatrix( (float *)&wv_matrix ); color_param->SetFloatVector( (float *)&vObjectColor ); pShader->GetPassByIndex(0)->Apply(0); g_pDevice->IASetInputLayout(g_pRefractionLayout); g_Model_DX10.Render(SUBMIT_CULLFACE); // 等待硬體掃結束, 然後才更新畫面 IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.4f, 0.4f, 0.4f, 1.0f); // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer // 清除畫面 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 設定shader g_pDevice->VSSetShader(g_pVertexShader); g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 更新shader常數參數 Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 object_matrix = g_Control.GetObjectMatrix(); Matrix4x4 *pShaderConstant; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pShaderConstant ); pShaderConstant[0] = object_matrix * view_matrix * g_proj_matrix; pShaderConstant[1] = object_matrix; g_pConstantBuffer->Unmap(); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定index buffer & vertex buffer g_pDevice->IASetIndexBuffer(g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0); UINT stride = sizeof(CompressedVertex); UINT offset = 0; g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 畫出球 g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // 等待硬體掃結束, 然後才更新畫面 IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vertex_VC); UINT offset = 0; // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定index buffer g_pDevice->IASetIndexBuffer(g_pSphereIndexBuffer, DXGI_FORMAT_R16_UINT, 0); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 計算出一個可以轉換到鏡頭座標系的矩陣 Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up); Matrix4x4 view_proj_matrix = view_matrix * g_proj_matrix; Matrix4x4 world_view_proj_matrix; // sun world_view_proj_matrix = g_sun_matrix * view_proj_matrix; // 設定shader參數 Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // g_pDevice->IASetVertexBuffers(0, 1, &g_pSunVertexBuffer, &stride, &offset); g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // earth world_view_proj_matrix = g_earth_matrix * view_proj_matrix; // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // g_pDevice->IASetVertexBuffers(0, 1, &g_pEarthVertexBuffer, &stride, &offset); g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // moon world_view_proj_matrix = g_moon_matrix * view_proj_matrix; // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // g_pDevice->IASetVertexBuffers(0, 1, &g_pMoonVertexBuffer, &stride, &offset); g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // 等待硬體掃結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
// WinMain int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow ) { HRESULT hr; // ウィンドウクラスを登録 WNDCLASSEX wcex = { sizeof( WNDCLASSEX ), // cbSize CS_HREDRAW | CS_VREDRAW, // style WndProc, // lpfnWndProc 0, // cbClsExtra 0, // cbWndExtra hInstance, // hInstance NULL, // hIcon NULL, // hCursor ( HBRUSH )( COLOR_WINDOW + 1 ), // hbrBackGround NULL, // lpszMenuName g_className, // lpszClassName NULL // hIconSm }; if ( ! RegisterClassEx( &wcex ) ) { MessageBox( NULL, _T( "失敗: RegisterClassEx()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "RegisterClassEx: ok\n" ) ); // ウィンドウサイズを計算 RECT r = { 0, 0, 800, 450 }; // 800x450 (16:9) if ( ! AdjustWindowRect( &r, WS_OVERLAPPEDWINDOW, FALSE ) ) { MessageBox( NULL, _T( "失敗: AdjustWindowRect()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "AdjustWindowRect: ok (%d, %d)-(%d, %d)\n" ), r.left, r.top, r.right, r.bottom ); // ウィンドウ生成 HWND hWnd; hWnd = CreateWindow( g_className, g_windowName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, r.right - r.left, r.bottom - r.top, NULL, NULL, hInstance, NULL ); if ( hWnd == NULL ) { MessageBox( NULL, _T( "失敗: CreateWindow()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "CreateWindow: ok\n" ) ); // ウィンドウ表示 ShowWindow(hWnd, nCmdShow); dtprintf( _T( "ShowWindow: ok\n" ) ); // スワップチェイン設定 DXGI_SWAP_CHAIN_DESC scDesc = { { 1280, // BufferDesc.Width 720, // BufferDesc.Height { 60, // BufferDesc.RefreshRate.Numerator 1 // BufferDesc.RefreshRate.Denominator }, DXGI_FORMAT_R16G16B16A16_FLOAT, // BufferDesc.Format DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED, // BufferDesc.ScanlineOrdering DXGI_MODE_SCALING_CENTERED // BufferDesc.Scaling }, { 1, // SampleDesc.Count 0 // SampleDesc.Quality }, DXGI_USAGE_RENDER_TARGET_OUTPUT, // BufferUsage 1, // BufferCount hWnd, // OutputWindow TRUE, // Windowed DXGI_SWAP_EFFECT_DISCARD, // SwapEffect DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH // Flags }; // Direct3D11 デバイス・デバイスコンテキスト・スワップチェーンを生成 ID3D11Device * pDevice = NULL; ID3D11DeviceContext * pDeviceContext = NULL; IDXGISwapChain * pSwapChain = NULL; D3D_FEATURE_LEVEL feature; hr = D3D11CreateDeviceAndSwapChain( NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &scDesc, &pSwapChain, &pDevice, &feature, &pDeviceContext ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: D3D11CreateDeviceAndSwapChain()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "D3D11CreateDeviceAndSwapChain: ok (pDevice: 0x%p, pDeviceContext: 0x%p, pSwapChain: 0x%p, feature: 0x%4x)\n" ), pDevice, pDeviceContext, pSwapChain, ( int ) feature ); // バックバッファテクスチャを取得 ID3D11Texture2D * pBackBuffer = NULL; hr = pSwapChain->GetBuffer( 0, __uuidof( pBackBuffer ), reinterpret_cast< void ** >( &pBackBuffer ) ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: IDXGISwapChain::GetBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "IDXGISwapChain::GetBuffer: ok (pBackBuffer: 0x%p)\n" ), pBackBuffer ); // レンダーターゲットビューを生成 ID3D11RenderTargetView * pRenderTargetView = NULL; hr = pDevice->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateRenderTargetView()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateRenderTargetView: ok (pRenderTargetView: 0x%p)\n" ), pRenderTargetView ); // デプス・ステンシルバッファとなるテクスチャを生成 D3D11_TEXTURE2D_DESC depthStencilBufferDesc = { 1280, // Width 720, // Height 1, // MipLevels 1, // ArraySize DXGI_FORMAT_D32_FLOAT, // Format { 1, // SampleDesc.Count 0 // SampleDesc.Quality }, D3D11_USAGE_DEFAULT, // Usage D3D11_BIND_DEPTH_STENCIL, // BindFlags 0, // CPUAccessFlags 0 // MiscFlags }; ID3D11Texture2D * pDepthStencilBuffer = NULL; hr = pDevice->CreateTexture2D( &depthStencilBufferDesc, NULL, &pDepthStencilBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateTexture2D()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateTexture2D: ok (pDepthStencilBuffer: 0x%p)\n" ), pDepthStencilBuffer ); // デプス・ステンシルビューを生成 ID3D11DepthStencilView * pDepthStencilView = NULL; hr = pDevice->CreateDepthStencilView( pDepthStencilBuffer, NULL, &pDepthStencilView ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateDepthStencilView()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateDepthStencilView: ok (pDepthStencilView: 0x%p)\n" ), pDepthStencilView ); // レンダーターゲットビューとデプス・ステンシルビューをバインド ID3D11RenderTargetView * pRenderTargetViews[] = { pRenderTargetView }; pDeviceContext->OMSetRenderTargets( 1, pRenderTargetViews, pDepthStencilView ); dtprintf( _T( "ID3D11DeviceContext::OMSetRenderTargets: ok\n" ) ); // バックバッファはもうここでは使わない COM_SAFE_RELEASE( pBackBuffer ); // ビューポートをバインド D3D11_VIEWPORT viewport = { 0.0f, // TopLeftX 0.0f, // TopLeftY 1280.0f, // Width 720.0f, // Height 0.0f, // MinDepth 1.0f // MaxDepth }; pDeviceContext->RSSetViewports( 1, &viewport ); dtprintf( _T( "ID3D11DeviceContext::RSSetViewports: ok\n" ) ); // 頂点データ float vertices[ 8 ][ 7 ] = { // Xaxis Yaxis Zaxis 赤 緑 青 Alpha { -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f }, // 手前左上 { 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f }, // 手前右上 { 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f }, // 手前右下 { -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f }, // 手前左下 { -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f }, // 奥左上 { 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f }, // 奥右上 { 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f }, // 奥右下 { -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f } // 奥左下 }; // 頂点バッファを生成 D3D11_BUFFER_DESC vertexBufferDesc = { sizeof( vertices ), // ByteWidth D3D11_USAGE_DEFAULT, // Usage D3D11_BIND_VERTEX_BUFFER, // BindFlags 0, // CPUAccessFlags 0, // MiscFlags 0 // StructureByteStride }; D3D11_SUBRESOURCE_DATA vertexResourceData = { vertices }; ID3D11Buffer * pVertexBuffer = NULL; hr = pDevice->CreateBuffer( &vertexBufferDesc, &vertexResourceData, &pVertexBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateBuffer: ok (pVertexBuffer: 0x%p)\n" ), pVertexBuffer ); // 頂点バッファをバインド UINT strides[] = { sizeof( float ) * 7 }; UINT offsets[] = { 0 }; pDeviceContext->IASetVertexBuffers( 0, 1, &pVertexBuffer, strides, offsets ); dtprintf( _T( "ID3D11DeviceContext::IASetVertexBuffers: ok\n" ) ); // インデックスデータ unsigned int indices[] = { 0, 1, 2, 0, 2, 3, // 手前 4, 0, 3, 4, 3, 7, // 左 1, 5, 6, 1, 6, 2, // 右 0, 4, 5, 0, 5, 1, // 上 2, 6, 7, 2, 7, 3, // 下 5, 4, 7, 5, 7, 6 }; // 裏 // インデックスバッファを生成 D3D11_BUFFER_DESC indexBufferDesc = { sizeof( indices ), // ByteWidth D3D11_USAGE_DEFAULT, // Usage D3D11_BIND_INDEX_BUFFER, // BindFlags 0, // CPUAccessFlags 0, // MiscFlags 0 // StructureByteStride }; D3D11_SUBRESOURCE_DATA indexResourceData = { indices }; ID3D11Buffer * pIndexBuffer = NULL; hr = pDevice->CreateBuffer( &indexBufferDesc, &indexResourceData, &pIndexBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateBuffer: ok (pIndexBuffer: 0x%p)\n" ), pIndexBuffer ); // インデックスバッファをバインド pDeviceContext->IASetIndexBuffer( pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 ); dtprintf( _T( "ID3D11DeviceContext::IASetIndexBuffer: ok\n" ) ); // プリミティブタイプを設定 pDeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); dtprintf( _T( "ID3D11DeviceContext::IASetPrimitiveTopology: ok\n" ) ); // 頂点シェーダ用の定数バッファを作成 D3D11_BUFFER_DESC VSConstantBufferDesc = { sizeof( D3DXMATRIX ) * 3, // ByteWidth D3D11_USAGE_DYNAMIC, // Usage D3D11_BIND_CONSTANT_BUFFER, // BindFlags D3D11_CPU_ACCESS_WRITE, // CPUAccessFlags 0, // MiscFlags 0 // StructureByteStride }; ID3D11Buffer * pVSConstantBuffer = NULL; hr = pDevice->CreateBuffer( &VSConstantBufferDesc, NULL, &pVSConstantBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateBuffer: ok (pVSConstantBuffer: 0x%p)\n" ), pVSConstantBuffer ); // 定数バッファをバインド pDeviceContext->VSSetConstantBuffers( 0, 1, &pVSConstantBuffer ); dtprintf( _T( "ID3D11DeviceContext::VSSetConstantBuffers: ok\n" ) ); // 頂点シェーダを作成 ID3D11VertexShader * pVertexShader = NULL; hr = pDevice->CreateVertexShader( g_vs_perspective, sizeof( g_vs_perspective ), NULL, &pVertexShader ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateVertexShader()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateVertexShader: ok (pVertexShader: 0x%p)\n" ), pVertexShader ); // ピクセルシェーダを作成 ID3D11PixelShader * pPixelShader = NULL; hr = pDevice->CreatePixelShader( g_ps_constant, sizeof( g_ps_constant ), NULL, &pPixelShader ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreatePixelShader()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreatePixelShader: ok (pPixelShader: 0x%p)\n" ), pPixelShader ); // シェーダをバインド pDeviceContext->VSSetShader( pVertexShader, NULL, 0 ); dtprintf( _T( "ID3D11DeviceContext::VSSetShader: ok\n" ) ); pDeviceContext->PSSetShader( pPixelShader, NULL, 0 ); dtprintf( _T( "ID3D11DeviceContext::PSSetShader: ok\n" ) ); pDeviceContext->GSSetShader( NULL, NULL, 0 ); pDeviceContext->HSSetShader( NULL, NULL, 0 ); pDeviceContext->DSSetShader( NULL, NULL, 0 ); // 入力エレメント記述子 D3D11_INPUT_ELEMENT_DESC verticesDesc[] = { { "IN_POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "IN_COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, sizeof(float)*3, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; // 入力レイアウトを生成 ID3D11InputLayout * pInputLayout = NULL; hr = pDevice->CreateInputLayout( verticesDesc, 2, g_vs_perspective, sizeof( g_vs_perspective ), &pInputLayout ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateInputLayout()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateInputLayout: ok (pInputLayout: 0x%p)\n" ), pInputLayout ); // 入力レイアウトをバインド pDeviceContext->IASetInputLayout( pInputLayout ); dtprintf( _T( "ID3D11DeviceContext::IASetInputLayout: ok\n" ) ); // ラスタライザステートを生成 D3D11_RASTERIZER_DESC rasterizerStateDesc = { D3D11_FILL_SOLID, // FillMode // D3D11_FILL_WIREFRAME, // FillMode (ワイヤーフレーム表示) D3D11_CULL_BACK, // CullMode // D3D11_CULL_NONE, // CullMode (カリングなし) FALSE, // FrontCounterClockwise 0, // DepthBias 0.0f, // DepthBiasClamp 0.0f, // SlopeScaledDepthBias TRUE, // DepthClipEnable FALSE, // ScissorEnable FALSE, // MultisampleEnable FALSE // AntialiasedLineEnable }; ID3D11RasterizerState * pRasterizerState = NULL; hr = pDevice->CreateRasterizerState( &rasterizerStateDesc, &pRasterizerState ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateRasterizerState()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateRasterizerState: ok (pRasterizerState: 0x%p)\n" ), pRasterizerState ); // ラスタライザステートをバインド pDeviceContext->RSSetState( pRasterizerState ); dtprintf( _T( "ID3D11DeviceContext::RSSetState: ok\n" ) ); // デプス・ステンシルステートを生成 D3D11_DEPTH_STENCIL_DESC depthStencilStateDesc = { TRUE, // DepthEnable D3D11_DEPTH_WRITE_MASK_ALL, // DepthWriteMask D3D11_COMPARISON_LESS, // DepthFunc FALSE, // StencilEnable D3D11_DEFAULT_STENCIL_READ_MASK, // StencilReadMask D3D11_DEFAULT_STENCIL_WRITE_MASK, // StencilWriteMask { D3D11_STENCIL_OP_KEEP, // FrontFace.StencilFailOp D3D11_STENCIL_OP_KEEP, // FrontFace.StencilDepthFailOp D3D11_STENCIL_OP_KEEP, // FrontFace.StencilPassOp D3D11_COMPARISON_ALWAYS // FrontFace.StencilFunc }, { D3D11_STENCIL_OP_KEEP, // BackFace.StencilFailOp D3D11_STENCIL_OP_KEEP, // BackFace.StencilDepthFailOp D3D11_STENCIL_OP_KEEP, // BackFace.StencilPassOp D3D11_COMPARISON_ALWAYS // BackFace.StencilFunc } }; ID3D11DepthStencilState * pDepthStencilState = NULL; hr = pDevice->CreateDepthStencilState( &depthStencilStateDesc, &pDepthStencilState ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateDepthStencilState()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateDepthStencilState: ok (pDepthStencilState: 0x%p)\n" ), pDepthStencilState ); // デプス・ステンシルステートをバインド pDeviceContext->OMSetDepthStencilState( pDepthStencilState, 0 ); dtprintf( _T( "ID3D11DeviceContext::OMSetDepthStencilState: ok\n" ) ); MSG msg; while ( 1 ) { // メッセージを取得 if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if ( msg.message == WM_QUIT ) { dtprintf( _T( "PeekMessage: WM_QUIT\n" ) ); break; } // メッセージ処理 DispatchMessage( &msg ); } else { HRESULT hr; static unsigned int count = 0; float theta = ( count++ / 200.0f ) * ( 3.141593f / 2.0f ); // World-View-Projection 行列をそれぞれ生成 D3DXMATRIX world, view, projection; D3DXMatrixIdentity( &world ); const D3DXVECTOR3 eye( 1.8f * 1.414214f * -cosf( theta ), 1.8f, 1.8f * 1.414214f * sinf( theta ) ); const D3DXVECTOR3 at( 0.0f, 0.0f, 0.0f ); const D3DXVECTOR3 up( 0.0f, 1.0f, 0.0f ); D3DXMatrixLookAtRH( &view, &eye, &at, &up ); D3DXMatrixPerspectiveFovRH( &projection, 3.141593f / 4.0f, 1280.0f / 720.0f, 1.0f, 10000.0f ); // 頂点シェーダ用定数バッファへアクセス D3D11_MAPPED_SUBRESOURCE mapped; hr = pDeviceContext->Map( pVSConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped ); if ( SUCCEEDED( hr ) ) { D3DXMATRIX * mapped_m = static_cast< D3DXMATRIX * >( mapped.pData ); mapped_m[0] = world; mapped_m[1] = view; mapped_m[2] = projection; // 後始末 pDeviceContext->Unmap( pVSConstantBuffer, 0 ); } // レンダーターゲットビューをクリア const float clear[ 4 ] = { 0.0f, 0.0f, 0.3f, 1.0f }; // RGBA pDeviceContext->ClearRenderTargetView( pRenderTargetView, clear ); // デプス・ステンシルビューをクリア pDeviceContext->ClearDepthStencilView( pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0 ); // 描画 pDeviceContext->DrawIndexed( 36, 0, 0 ); pSwapChain->Present( 1, 0 ); // ちょっとだけ待つ Sleep( 5 ); } } // シェーダをアンバインド pDeviceContext->VSSetShader( NULL, NULL, 0 ); pDeviceContext->PSSetShader( NULL, NULL, 0 ); // デバイス・リソース解放 COM_SAFE_RELEASE( pDepthStencilState ); COM_SAFE_RELEASE( pRasterizerState ); COM_SAFE_RELEASE( pInputLayout ); COM_SAFE_RELEASE( pPixelShader ); COM_SAFE_RELEASE( pVertexShader ); COM_SAFE_RELEASE( pVSConstantBuffer ); COM_SAFE_RELEASE( pIndexBuffer ); COM_SAFE_RELEASE( pVertexBuffer ); COM_SAFE_RELEASE( pDepthStencilView ); COM_SAFE_RELEASE( pDepthStencilBuffer ); COM_SAFE_RELEASE( pRenderTargetView ); COM_SAFE_RELEASE( pSwapChain ); COM_SAFE_RELEASE( pDeviceContext ); COM_SAFE_RELEASE( pDevice ); return msg.wParam; }
bool nsWindow::OnPaint(HDC aDC, uint32_t aNestingLevel) { // We never have reentrant paint events, except when we're running our RPC // windows event spin loop. If we don't trap for this, we'll try to paint, // but view manager will refuse to paint the surface, resulting is black // flashes on the plugin rendering surface. if (mozilla::ipc::MessageChannel::IsSpinLoopActive() && mPainting) return false; if (gfxWindowsPlatform::GetPlatform()->DidRenderingDeviceReset()) { gfxWindowsPlatform::GetPlatform()->UpdateRenderMode(); EnumAllWindows(ClearCompositor); return false; } // After we CallUpdateWindow to the child, occasionally a WM_PAINT message // is posted to the parent event loop with an empty update rect. Do a // dummy paint so that Windows stops dispatching WM_PAINT in an inifinite // loop. See bug 543788. if (IsPlugin()) { RECT updateRect; if (!GetUpdateRect(mWnd, &updateRect, FALSE) || (updateRect.left == updateRect.right && updateRect.top == updateRect.bottom)) { PAINTSTRUCT ps; BeginPaint(mWnd, &ps); EndPaint(mWnd, &ps); return true; } if (mWindowType == eWindowType_plugin_ipc_chrome) { // Fire off an async request to the plugin to paint its window mozilla::dom::ContentParent::SendAsyncUpdate(this); ValidateRect(mWnd, nullptr); return true; } PluginInstanceParent* instance = reinterpret_cast<PluginInstanceParent*>( ::GetPropW(mWnd, L"PluginInstanceParentProperty")); if (instance) { Unused << instance->CallUpdateWindow(); } else { // We should never get here since in-process plugins should have // subclassed our HWND and handled WM_PAINT, but in some cases that // could fail. Return without asserting since it's not our fault. NS_WARNING("Plugin failed to subclass our window"); } ValidateRect(mWnd, nullptr); return true; } ClientLayerManager *clientLayerManager = (GetLayerManager()->GetBackendType() == LayersBackend::LAYERS_CLIENT) ? static_cast<ClientLayerManager*>(GetLayerManager()) : nullptr; if (clientLayerManager && mCompositorParent && !mBounds.IsEqualEdges(mLastPaintBounds)) { // Do an early async composite so that we at least have something on the // screen in the right place, even if the content is out of date. mCompositorParent->ScheduleRenderOnCompositorThread(); } mLastPaintBounds = mBounds; PAINTSTRUCT ps; #ifdef MOZ_XUL if (!aDC && (eTransparencyTransparent == mTransparencyMode)) { // For layered translucent windows all drawing should go to memory DC and no // WM_PAINT messages are normally generated. To support asynchronous painting // we force generation of WM_PAINT messages by invalidating window areas with // RedrawWindow, InvalidateRect or InvalidateRgn function calls. // BeginPaint/EndPaint must be called to make Windows think that invalid area // is painted. Otherwise it will continue sending the same message endlessly. ::BeginPaint(mWnd, &ps); ::EndPaint(mWnd, &ps); aDC = mMemoryDC; } #endif mPainting = true; #ifdef WIDGET_DEBUG_OUTPUT HRGN debugPaintFlashRegion = nullptr; HDC debugPaintFlashDC = nullptr; if (debug_WantPaintFlashing()) { debugPaintFlashRegion = ::CreateRectRgn(0, 0, 0, 0); ::GetUpdateRgn(mWnd, debugPaintFlashRegion, TRUE); debugPaintFlashDC = ::GetDC(mWnd); } #endif // WIDGET_DEBUG_OUTPUT HDC hDC = aDC ? aDC : (::BeginPaint(mWnd, &ps)); mPaintDC = hDC; #ifdef MOZ_XUL bool forceRepaint = aDC || (eTransparencyTransparent == mTransparencyMode); #else bool forceRepaint = nullptr != aDC; #endif nsIntRegion region = GetRegionToPaint(forceRepaint, ps, hDC); if (clientLayerManager && mCompositorParent) { // We need to paint to the screen even if nothing changed, since if we // don't have a compositing window manager, our pixels could be stale. clientLayerManager->SetNeedsComposite(true); clientLayerManager->SendInvalidRegion(region); } nsIWidgetListener* listener = GetPaintListener(); if (listener) { listener->WillPaintWindow(this); } // Re-get the listener since the will paint notification may have killed it. listener = GetPaintListener(); if (!listener) { return false; } if (clientLayerManager && mCompositorParent && clientLayerManager->NeedsComposite()) { mCompositorParent->ScheduleRenderOnCompositorThread(); clientLayerManager->SetNeedsComposite(false); } bool result = true; if (!region.IsEmpty() && listener) { // Should probably pass in a real region here, using GetRandomRgn // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/clipping_4q0e.asp #ifdef WIDGET_DEBUG_OUTPUT debug_DumpPaintEvent(stdout, this, region, nsAutoCString("noname"), (int32_t) mWnd); #endif // WIDGET_DEBUG_OUTPUT switch (GetLayerManager()->GetBackendType()) { case LayersBackend::LAYERS_BASIC: { RefPtr<gfxASurface> targetSurface; #if defined(MOZ_XUL) // don't support transparency for non-GDI rendering, for now if ((IsRenderMode(gfxWindowsPlatform::RENDER_GDI) || IsRenderMode(gfxWindowsPlatform::RENDER_DIRECT2D)) && eTransparencyTransparent == mTransparencyMode) { if (mTransparentSurface == nullptr) SetupTranslucentWindowMemoryBitmap(mTransparencyMode); targetSurface = mTransparentSurface; } #endif RefPtr<gfxWindowsSurface> targetSurfaceWin; if (!targetSurface && (IsRenderMode(gfxWindowsPlatform::RENDER_GDI) || IsRenderMode(gfxWindowsPlatform::RENDER_DIRECT2D))) { uint32_t flags = (mTransparencyMode == eTransparencyOpaque) ? 0 : gfxWindowsSurface::FLAG_IS_TRANSPARENT; targetSurfaceWin = new gfxWindowsSurface(hDC, flags); targetSurface = targetSurfaceWin; } RefPtr<gfxImageSurface> targetSurfaceImage; if (!targetSurface && (IsRenderMode(gfxWindowsPlatform::RENDER_IMAGE_STRETCH32) || IsRenderMode(gfxWindowsPlatform::RENDER_IMAGE_STRETCH24))) { IntSize surfaceSize(ps.rcPaint.right - ps.rcPaint.left, ps.rcPaint.bottom - ps.rcPaint.top); if (!EnsureSharedSurfaceSize(surfaceSize)) { NS_ERROR("Couldn't allocate a shared image surface!"); return false; } // don't use the shared surface directly; instead, create a new one // that just reuses its buffer. targetSurfaceImage = new gfxImageSurface(sSharedSurfaceData.get(), surfaceSize, surfaceSize.width * 4, SurfaceFormat::X8R8G8B8_UINT32); if (targetSurfaceImage && !targetSurfaceImage->CairoStatus()) { targetSurfaceImage->SetDeviceOffset(gfxPoint(-ps.rcPaint.left, -ps.rcPaint.top)); targetSurface = targetSurfaceImage; } } if (!targetSurface) { NS_ERROR("Invalid RenderMode!"); return false; } RECT paintRect; ::GetClientRect(mWnd, &paintRect); RefPtr<DrawTarget> dt = gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(targetSurface, IntSize(paintRect.right - paintRect.left, paintRect.bottom - paintRect.top)); if (!dt) { gfxWarning() << "nsWindow::OnPaint failed in CreateDrawTargetForSurface"; return false; } // don't need to double buffer with anything but GDI BufferMode doubleBuffering = mozilla::layers::BufferMode::BUFFER_NONE; if (IsRenderMode(gfxWindowsPlatform::RENDER_GDI) || IsRenderMode(gfxWindowsPlatform::RENDER_DIRECT2D)) { #ifdef MOZ_XUL switch (mTransparencyMode) { case eTransparencyGlass: case eTransparencyBorderlessGlass: default: // If we're not doing translucency, then double buffer doubleBuffering = mozilla::layers::BufferMode::BUFFERED; break; case eTransparencyTransparent: // If we're rendering with translucency, we're going to be // rendering the whole window; make sure we clear it first dt->ClearRect(Rect(0.f, 0.f, dt->GetSize().width, dt->GetSize().height)); break; } #else doubleBuffering = mozilla::layers::BufferMode::BUFFERED; #endif } RefPtr<gfxContext> thebesContext = new gfxContext(dt); { AutoLayerManagerSetup setupLayerManager(this, thebesContext, doubleBuffering); result = listener->PaintWindow( this, LayoutDeviceIntRegion::FromUnknownRegion(region)); } #ifdef MOZ_XUL if ((IsRenderMode(gfxWindowsPlatform::RENDER_GDI) || IsRenderMode(gfxWindowsPlatform::RENDER_DIRECT2D))&& eTransparencyTransparent == mTransparencyMode) { // Data from offscreen drawing surface was copied to memory bitmap of transparent // bitmap. Now it can be read from memory bitmap to apply alpha channel and after // that displayed on the screen. UpdateTranslucentWindow(); } else #endif if (result) { if (IsRenderMode(gfxWindowsPlatform::RENDER_IMAGE_STRETCH24) || IsRenderMode(gfxWindowsPlatform::RENDER_IMAGE_STRETCH32)) { IntSize surfaceSize = targetSurfaceImage->GetSize(); // Just blit this directly BITMAPINFOHEADER bi; memset(&bi, 0, sizeof(BITMAPINFOHEADER)); bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = surfaceSize.width; bi.biHeight = - surfaceSize.height; bi.biPlanes = 1; bi.biBitCount = 32; bi.biCompression = BI_RGB; if (IsRenderMode(gfxWindowsPlatform::RENDER_IMAGE_STRETCH24)) { // On Windows CE/Windows Mobile, 24bpp packed-pixel sources // seem to be far faster to blit than 32bpp (see bug 484864). // So, convert the bits to 24bpp by stripping out the unused // alpha byte. 24bpp DIBs also have scanlines that are 4-byte // aligned though, so that must be taken into account. int srcstride = surfaceSize.width*4; int dststride = surfaceSize.width*3; dststride = (dststride + 3) & ~3; // Convert in place for (int j = 0; j < surfaceSize.height; ++j) { unsigned int *src = (unsigned int*) (targetSurfaceImage->Data() + j*srcstride); unsigned int *dst = (unsigned int*) (targetSurfaceImage->Data() + j*dststride); // go 4 pixels at a time, since each 4 pixels // turns into 3 DWORDs when converted into BGR: // BGRx BGRx BGRx BGRx -> BGRB GRBG RBGR // // However, since we're dealing with little-endian ints, this is actually: // xRGB xrgb xRGB xrgb -> bRGB GBrg rgbR int width_left = surfaceSize.width; while (width_left >= 4) { unsigned int a = *src++; unsigned int b = *src++; unsigned int c = *src++; unsigned int d = *src++; *dst++ = (a & 0x00ffffff) | (b << 24); *dst++ = ((b & 0x00ffff00) >> 8) | (c << 16); *dst++ = ((c & 0x00ff0000) >> 16) | (d << 8); width_left -= 4; } // then finish up whatever number of pixels are left, // using bytes. unsigned char *bsrc = (unsigned char*) src; unsigned char *bdst = (unsigned char*) dst; switch (width_left) { case 3: *bdst++ = *bsrc++; *bdst++ = *bsrc++; *bdst++ = *bsrc++; bsrc++; case 2: *bdst++ = *bsrc++; *bdst++ = *bsrc++; *bdst++ = *bsrc++; bsrc++; case 1: *bdst++ = *bsrc++; *bdst++ = *bsrc++; *bdst++ = *bsrc++; bsrc++; case 0: break; } } bi.biBitCount = 24; } StretchDIBits(hDC, ps.rcPaint.left, ps.rcPaint.top, surfaceSize.width, surfaceSize.height, 0, 0, surfaceSize.width, surfaceSize.height, targetSurfaceImage->Data(), (BITMAPINFO*) &bi, DIB_RGB_COLORS, SRCCOPY); } } } break; case LayersBackend::LAYERS_CLIENT: { result = listener->PaintWindow( this, LayoutDeviceIntRegion::FromUnknownRegion(region)); ClientLayerManager* clientLM = static_cast<ClientLayerManager*>(GetLayerManager()); IDXGISwapChain* swapChain = clientLM->GetTextureFactoryIdentifier().mSwapChain.get(); if (swapChain) { // Workaround for bug 1232042. On some devices artifacts will occur // is we don't do a main thread present call from the WM_PAINT event. swapChain->Present(0, 0); } } break; default: NS_ERROR("Unknown layers backend used!"); break; }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); // frame buffer ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); // depth/stencil buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); // front/back buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); Matrix4x4 ident_matrix; ident_matrix.Identity(); Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix = g_Control.GetObjectMatrix(); Matrix4x4 world_view_proj_matrix = world_matrix * view_matrix * g_proj_matrix; // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 畫出茶壼, 并更新 Stencil Buffer. { g_pDevice->OMSetDepthStencilState(g_pZStencil_Incr, 0); CGutModel_DX10::SetProjectionMatrix(g_proj_matrix); CGutModel_DX10::SetViewMatrix(view_matrix); CGutModel_DX10::SetWorldMatrix(world_matrix); CGutModel_DX10::UpdateMatrix(); g_Model_DX10.Render(); } // 套用 Shader { UINT stride = sizeof(Vertex_V); UINT offset = 0; // 設定Shader g_pDevice->VSSetShader(g_pVertexShader); g_pDevice->PSSetShader(g_pPixelShader); // 設定Shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); g_pDevice->PSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定Vertex Buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); } ShaderConstant *pConstData = NULL; // 用綠色標示出只更新1次的像素 { // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); pConstData->m_wvp_matrix = ident_matrix; pConstData->m_color.Set(0,1,0,1); g_pConstantBuffer->Unmap(); // 畫出看板 g_pDevice->OMSetDepthStencilState(g_pZStencil_Test, 1); g_pDevice->Draw(4, 0); } // 用藍色標示出更新2次的像素 { // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); pConstData->m_wvp_matrix = ident_matrix; pConstData->m_color.Set(0,0,1,1); g_pConstantBuffer->Unmap(); // 畫出看板 g_pDevice->OMSetDepthStencilState(g_pZStencil_Test, 2); g_pDevice->Draw(4, 0); } // 用紅色標示出更新3次的像素 { // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); pConstData->m_wvp_matrix = ident_matrix; pConstData->m_color.Set(1,0,0,1); g_pConstantBuffer->Unmap(); // 畫出看板 g_pDevice->OMSetDepthStencilState(g_pZStencil_Test, 3); g_pDevice->Draw(4, 0); } // 用白色標示出更新超過3次的像素 { // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); pConstData->m_wvp_matrix = ident_matrix; pConstData->m_color.Set(1,1,1,1); g_pConstantBuffer->Unmap(); // 畫出看板 g_pDevice->OMSetDepthStencilState(g_pZStencil_Less, 3); g_pDevice->Draw(4, 0); } // 等待硬體掃結束, 然後才更新畫面. pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { UINT stride = sizeof(Vertex_VT); UINT offset = 0; // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 設定pixel shader讀取參數的記憶體位罝 g_pDevice->PSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 套用貼圖 // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); FLOAT dummy[4]; { g_pDevice->OMSetBlendState(NULL, dummy, 0xffffffff); g_pDevice->OMSetDepthStencilState(g_pDepthStencilState_ZDisable, 0); // 設定vertex buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBufferFullScreen, &stride, &offset); // 套用第1張貼圖 g_pDevice->PSSetShaderResources(0, 1, &g_pTexture0); // 計算矩陣 Matrix4x4 world_view_proj_matrix; world_view_proj_matrix.Identity(); // 更新shader參數 Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // 畫出格子 g_pDevice->Draw(4, 0); } { g_pDevice->OMSetBlendState(g_pBlendState, dummy, 0xffffffff); g_pDevice->OMSetDepthStencilState(NULL, 0); // 設定vertex buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); Vertex_VT *pVertices = NULL; g_pVertexBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pVertices ); memcpy(pVertices, g_Quad, sizeof(g_Quad)); g_pVertexBuffer->Unmap(); // 套用第1張貼圖 g_pDevice->PSSetShaderResources(0, 1, &g_pTexture1); // 計算矩陣 Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up); Matrix4x4 world_view_proj_matrix = g_world_matrix * view_matrix * g_proj_matrix; // 更新shader參數 Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // 畫出格子 g_pDevice->Draw(4, 0); } // 等待硬體掃結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
HRESULT STDMETHODCALLTYPE CDXGISwapChainDWM::Present(UINT SyncInterval, UINT Flags) { return m_pSwapChain->Present(SyncInterval, Flags); }
void RenderFrameDX10(void) { Vector4 vClearColor(0.4f, 0.4f, 0.4f, 1.0f); // frame buffer ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); // depth/stencil buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); // front/back buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // `清除顏色` g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // `清除`Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); Vector4 camera_lookat(0.0f, 0.0f, 0.0f); int w, h; GutGetWindowSize(w, h); Matrix4x4 view_matrix; Matrix4x4 ortho_proj = GutMatrixOrthoRH_DirectX(20.0f, 20.0f, 0.1f, 100.0f); // `前視圖` { D3D10_VIEWPORT vp = {0, 0, w/2, h/2, 0.0f, 1.0f}; g_pDevice->RSSetViewports(1, &vp); // view matrix Vector4 camera_pos(0.0f, -20.0f, 0.0f); Vector4 camera_up(0.0f, 0.0f, 1.0f); view_matrix = GutMatrixLookAtRH(camera_pos, camera_lookat, camera_up); // render objects RenderSolarSystemDX10(view_matrix, ortho_proj); } // `上視圖` { D3D10_VIEWPORT vp = {w/2, 0, w/2, h/2, 0.0f, 1.0f}; g_pDevice->RSSetViewports(1, &vp); // view matrix Vector4 camera_pos(0.0f, 0.0f, 20.0f); Vector4 camera_up(0.0f, 1.0f, 0.0f); view_matrix = GutMatrixLookAtRH(camera_pos, camera_lookat, camera_up); // render objects RenderSolarSystemDX10(view_matrix, ortho_proj); } // `右視圖` { D3D10_VIEWPORT vp = {0, h/2, w/2, h/2, 0.0f, 1.0f}; g_pDevice->RSSetViewports(1, &vp); // view matrix Vector4 camera_pos(20.0f, 0.0f, 0.0f); Vector4 camera_up(0.0f, 0.0f, 1.0f); view_matrix = GutMatrixLookAtRH(camera_pos, camera_lookat, camera_up); // render objects RenderSolarSystemDX10(view_matrix, ortho_proj); } // `使用者視角` { D3D10_VIEWPORT vp = {w/2, h/2, w/2, h/2, 0.0f, 1.0f}; g_pDevice->RSSetViewports(1, &vp); // view matrix Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 object_matrix = g_Control.GetObjectMatrix(); view_matrix = object_matrix * view_matrix; // render objects RenderSolarSystemDX10(view_matrix, g_proj_matrix); } // `畫邊線` { UINT stride = sizeof(Vertex_VC); UINT offset = 0; D3D10_VIEWPORT vp = {0, 0, w, h, 0.0f, 1.0f}; g_pDevice->RSSetViewports(1, &vp); // `設定`vertex shader g_pDevice->VSSetShader(g_pVertexShader); // `設定`pixel shader g_pDevice->PSSetShader(g_pPixelShader); // `設定vertex shader讀取參數的記憶體位置` g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); pConstData->Identity(); g_pConstantBuffer->Unmap(); g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINELIST); g_pDevice->IASetVertexBuffers(0, 1, &g_pBorderVertexBuffer, &stride, &offset); g_pDevice->Draw(4, 0); } // `等待硬體掃結束, 然後才更新畫面.` pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vColorDarkBlue(0.0f, 0.0f, 0.5f, 1.0f); Vector4 vColorBlue(0.0f, 0.0f, 0.8f, 1.0f); Vector4 vPlane(0.0f, 0.0f, 1.0f, -g_mirror_z); D3D10_VIEWPORT mainVP, VP; // `取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件` ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); // front/back buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // `在動態貼圖中畫出鏡射的茶壼` { UINT nViewports = 1; g_pDevice->RSGetViewports(&nViewports, &mainVP); // new rendertarget viewport size VP.TopLeftX = VP.TopLeftY = 0; VP.Width = VP.Height = 512; VP.MinDepth = 0.0f; VP.MaxDepth = 1.0f; ID3D10ShaderResourceView *null_views[4] = {NULL, NULL, NULL, NULL}; g_pDevice->PSSetShaderResources(0, 4, null_views); // `使用代表動態貼圖的`RenderTarget g_pDevice->OMSetRenderTargets(1, &g_pRGBAView, g_pDepthStencilView); // g_pDevice->RSSetViewports(1, &VP); // `清除顏色` g_pDevice->ClearRenderTargetView(g_pRGBAView, (float *)&vColorDarkBlue); // `清除`Depth/Stencil buffer g_pDevice->ClearDepthStencilView(g_pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // `畫鏡射的茶壼` RenderModelDX10(true, &vPlane); } // `在主Framebuffer中畫出正常的茶壼` { // `使用主`framebuffer g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, pDepthStencilView); // g_pDevice->RSSetViewports(1, &mainVP); // `清除顏色` g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vColorBlue); // `清除`Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // `畫茶壼` RenderModelDX10(false, &vPlane); } // `在主framebuffer中畫出鏡面` { UINT stride = sizeof(Vertex_VT); UINT offset = 0; // `設定`vertex shader g_pDevice->VSSetShader(g_pVertexShader); // `設定`pixel shader g_pDevice->PSSetShader(g_pPixelShader); // `設定`Shader Constants g_pDevice->VSSetConstantBuffers(0, 1, &g_pVSConstBuffer); // `設定vertex資料格式` g_pDevice->IASetInputLayout(g_pVertexLayout); // `設定三角形頂點索引值資料排列是triangle strip` g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // Vertex Buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // `計算`texture matrix Matrix4x4 uv_offset_matrix; uv_offset_matrix.Scale_Replace(0.5f, -0.5f, 1.0f); uv_offset_matrix[3].Set(0.5f, 0.5f, 0.0f, 1.0f); Matrix4x4 texture_matrix = g_mirror_view_matrix * g_proj_matrix * uv_offset_matrix; Vertex_VT *pVertices = NULL; g_pVertexBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pVertices); for ( int i=0; i<4; i++ ) { Vector4 vPosition = g_Quad[i].m_Position; Vector4 vTexcoord = vPosition * texture_matrix; vTexcoord /= vTexcoord.GetW(); pVertices[i].m_Position = vPosition; pVertices[i].m_Texcoord = vTexcoord; } g_pVertexBuffer->Unmap(); // `計算矩陣` Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 wvp_matrix = view_matrix * g_proj_matrix; // `更新shader參數` Matrix4x4 *pConstData; g_pVSConstBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = wvp_matrix; g_pVSConstBuffer->Unmap(); // `套用第1張貼圖` g_pDevice->PSSetShaderResources(0, 1, &g_pTextureView); g_pDevice->PSSetSamplers(0, 1, &g_pSamplerState); // `畫出格子` g_pDevice->Draw(4, 0); } // `等待硬體掃結束, 然後才更新畫面.` pSwapChain->Present(1, 0); }
void RenderFrameDX10(void) { Vector4 vColorDarkBlue(0.0f, 0.0f, 0.5f, 1.0f); Vector4 vColorBlue(0.0f, 0.0f, 0.8f, 1.0f); Vector4 vPlane(0.0f, 0.0f, 1.0f, -g_mirror_z); D3D10_VIEWPORT mainVP, VP; // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer // 在動態貼圖中畫出鏡射的茶壼 { UINT nViewports = 1; g_pDevice->RSGetViewports(&nViewports, &mainVP); // new rendertarget viewport size VP.TopLeftX = VP.TopLeftY = 0; VP.Width = VP.Height = 512; VP.MinDepth = 0.0f; VP.MaxDepth = 1.0f; ID3D10ShaderResourceView *null_views[4] = {NULL, NULL, NULL, NULL}; g_pDevice->PSSetShaderResources(0, 4, null_views); // 使用代表動態貼圖的RenderTarget g_pDevice->OMSetRenderTargets(1, &g_pRGBAView, g_pDepthStencilView); // g_pDevice->RSSetViewports(1, &VP); // 清除顏色 g_pDevice->ClearRenderTargetView(g_pRGBAView, (float *)&vColorDarkBlue); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(g_pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 畫鏡射的茶壼 RenderModelDX10(true, &vPlane); } // 在主Framebuffer中畫出正常的茶壼 { // 使用主framebuffer g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, pDepthStencilView); // g_pDevice->RSSetViewports(1, &mainVP); // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vColorBlue); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 畫茶壼 RenderModelDX10(false, &vPlane); } // 在主framebuffer中畫出鏡面 { UINT stride = sizeof(Vertex_V); UINT offset = 0; // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定Shader Constants g_pDevice->VSSetConstantBuffers(0, 1, &g_pVSConstBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // Vertex Buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 計算texture matrix Matrix4x4 uv_offset_matrix; uv_offset_matrix.Scale_Replace(0.5f, -0.5f, 1.0f); uv_offset_matrix[3].Set(0.5f, 0.5f, 0.0f, 1.0f); Matrix4x4 texture_matrix = g_mirror_view_matrix * g_proj_matrix * uv_offset_matrix; // 計算矩陣 Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 wvp_matrix = view_matrix * g_proj_matrix; // 更新shader參數 Matrix4x4 *pConstData; g_pVSConstBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); pConstData[0] = wvp_matrix; pConstData[1] = texture_matrix; g_pVSConstBuffer->Unmap(); // 套用第1張貼圖 g_pDevice->PSSetShaderResources(0, 1, &g_pTextureView); // 畫出鏡面 g_pDevice->Draw(4, 0); } // 等待硬體掃結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
HRESULT STDMETHODCALLTYPE SwapPresentHook(UINT syncInterval, UINT flags) { IDXGISwapChain *swap = (IDXGISwapChain*)this; if(lpCurrentSwap == NULL && !bTargetAcquired) { lpCurrentSwap = swap; SetupD3D11(swap); bTargetAcquired = true; } if(lpCurrentSwap == swap) { ID3D11Device *device = NULL; HRESULT chi; if(SUCCEEDED(chi = swap->GetDevice(__uuidof(ID3D11Device), (void**)&device))) { if(!lpCurrentDevice) { lpCurrentDevice = device; oldD3D11Release = GetVTable(device, (8/4)); newD3D11Release = ConvertClassProcToFarproc((CLASSPROC)&D3D11Override::DeviceReleaseHook); SetVTable(device, (8/4), newD3D11Release); } ID3D11DeviceContext *context; device->GetImmediateContext(&context); if(!bHasTextures && bCapturing) { if(dxgiFormat) { if(!hwndReceiver) hwndReceiver = FindWindow(RECEIVER_WINDOWCLASS, NULL); if(hwndReceiver) { D3D11_TEXTURE2D_DESC texDesc; ZeroMemory(&texDesc, sizeof(texDesc)); texDesc.Width = d3d11CaptureInfo.cx; texDesc.Height = d3d11CaptureInfo.cy; texDesc.MipLevels = 1; texDesc.ArraySize = 1; texDesc.Format = dxgiFormat; texDesc.SampleDesc.Count = 1; texDesc.Usage = D3D11_USAGE_STAGING; texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; bool bSuccess = true; UINT pitch; for(UINT i=0; i<2; i++) { HRESULT ching; if(FAILED(ching = device->CreateTexture2D(&texDesc, NULL, &d3d11Textures[i]))) { bSuccess = false; break; } if(i == 0) { ID3D11Resource *resource; if(FAILED(d3d11Textures[i]->QueryInterface(__uuidof(ID3D11Resource), (void**)&resource))) { bSuccess = false; break; } D3D11_MAPPED_SUBRESOURCE map; if(FAILED(context->Map(resource, 0, D3D11_MAP_READ, 0, &map))) { bSuccess = false; break; } pitch = map.RowPitch; context->Unmap(resource, 0); resource->Release(); } } if(bSuccess) { d3d11CaptureInfo.mapID = InitializeSharedMemory(pitch*d3d11CaptureInfo.cy, &d3d11CaptureInfo.mapSize, ©Data, textureBuffers); if(!d3d11CaptureInfo.mapID) bSuccess = false; } if(bSuccess) { bHasTextures = true; d3d11CaptureInfo.captureType = CAPTURETYPE_MEMORY; d3d11CaptureInfo.hwndSender = hwndSender; d3d11CaptureInfo.pitch = pitch; d3d11CaptureInfo.bFlip = FALSE; PostMessage(hwndReceiver, RECEIVER_NEWCAPTURE, 0, (LPARAM)&d3d11CaptureInfo); } else { for(UINT i=0; i<2; i++) { SafeRelease(d3d11Textures[i]); if(textureBuffers[i]) { free(textureBuffers[i]); textureBuffers[i] = NULL; } } } } } } if(bHasTextures) { if(bCapturing) { DWORD nextCapture = curCapture == 0 ? 1 : 0; ID3D11Texture2D *texture = d3d11Textures[curCapture]; ID3D11Resource *backBuffer = NULL; if(SUCCEEDED(swap->GetBuffer(0, IID_ID3D11Resource, (void**)&backBuffer))) { if(bIsMultisampled) context->ResolveSubresource(texture, 0, backBuffer, 0, dxgiFormat); else context->CopyResource(texture, backBuffer); backBuffer->Release(); ID3D11Texture2D *lastTexture = d3d11Textures[nextCapture]; ID3D11Resource *resource; if(SUCCEEDED(lastTexture->QueryInterface(__uuidof(ID3D11Resource), (void**)&resource))) { D3D11_MAPPED_SUBRESOURCE map; if(SUCCEEDED(context->Map(resource, 0, D3D11_MAP_READ, 0, &map))) { LPBYTE *pTextureBuffer = NULL; int lastRendered = -1; //under no circumstances do we -ever- allow a stall if(WaitForSingleObject(textureMutexes[curCapture], 0) == WAIT_OBJECT_0) lastRendered = (int)curCapture; else if(WaitForSingleObject(textureMutexes[nextCapture], 0) == WAIT_OBJECT_0) lastRendered = (int)nextCapture; if(lastRendered != -1) { SSECopy(textureBuffers[lastRendered], map.pData, map.RowPitch*d3d11CaptureInfo.cy); ReleaseMutex(textureMutexes[lastRendered]); } context->Unmap(resource, 0); copyData->lastRendered = (UINT)lastRendered; } resource->Release(); } } curCapture = nextCapture; } else ClearD3D11Data(); } device->Release(); context->Release(); } } gi11swapPresent.Unhook(); HRESULT hRes = swap->Present(syncInterval, flags); gi11swapPresent.Rehook(); return hRes; }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); float fDummy[4] = {0,0,0,0}; UINT stride = sizeof(Vertex_VT); UINT offset = 0; //frame buffer ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //depth/stencil buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); // front/back buffer chain IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定vertex buffer g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // 畫墻壁 { Matrix4x4 IdentityMatrix; IdentityMatrix.Identity(); // 更新shader參數 Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = IdentityMatrix; g_pConstantBuffer->Unmap(); // 把混色關閉 g_pDevice->OMSetBlendState(NULL, fDummy, 0xff); // 把墻壁所填充的像素 Stencil 值設定 1, 最後一個參數是 Stencil 參考值. g_pDevice->OMSetDepthStencilState(g_pStencilState_Replace, 1); // 套用貼圖 g_pDevice->PSSetShaderResources(0, 1, &g_pTexture0); // 畫出墻壁 g_pDevice->Draw(4, 0); } // 畫手電筒光源 { // 計算矩陣 //Matrix4x4 view_proj_matrix = g_view_matrix * g_proj_matrix; Matrix4x4 world_view_proj_matrix = g_world_matrix * g_view_matrix * g_proj_matrix; // 更新shader參數 Matrix4x4 *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // 使用加色混色 g_pDevice->OMSetBlendState(g_pBlendState, fDummy, 0xff); // 把墻壁所填充的像素Stencil值設定1 if ( g_bStencil ) g_pDevice->OMSetDepthStencilState(g_pStencilState_Test, 1); else g_pDevice->OMSetDepthStencilState(NULL, 1); // 套用貼圖 g_pDevice->PSSetShaderResources(0, 1, &g_pTexture1); // 畫出光點 g_pDevice->Draw(4, 0); } // 等待硬體掃結束, 然後才更新畫面. pSwapChain->Present(1, 0); }
bool DEMO_APP::Run() { theTime.Signal(); float dt = (float)theTime.Delta(); devContext->OMSetRenderTargets(1, &anotherView, DepthStencilView); devContext->RSSetViewports(1, &viewPort); float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; devContext->ClearRenderTargetView(anotherView, ClearColor); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); scene[0] = Inverse4x4(secondCamera); D3D11_MAPPED_SUBRESOURCE sceneMap; ZeroMemory(&sceneMap, sizeof(sceneMap)); devContext->Map(sceneMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sceneMap); memcpy(sceneMap.pData, scene, sizeof(scene)); devContext->Unmap(sceneMatrixBuffer, 0); devContext->VSSetConstantBuffers(1, 1, &sceneMatrixBuffer); devContext->PSSetConstantBuffers(0, 1, &lightBuff); devContext->PSSetConstantBuffers(1, 1, &ambientBuff); devContext->PSSetConstantBuffers(2, 1, &ptltBuff); devContext->PSSetConstantBuffers(3, 1, &spotBuff); devContext->GSSetConstantBuffers(1, 1, &sceneMatrixBuffer); //Pyramid.worldMatrices[0] = RotateY(50.0f * dt) * Pyramid.worldMatrices[0]; //Pyramid.worldMatrices[1] = RotateZ(50.0f * dt) * Pyramid.worldMatrices[1]; //Pyramid.worldMatrices[2] = RotateX(50.0f * dt) * Pyramid.worldMatrices[2]; //Pyramid.worldMatrices[3] = RotateY(200.0f * dt) * RotateZ(200.0f * dt) * RotateX(200.0f * dt) * Pyramid.worldMatrices[3]; //SKYBOX RENDERING///// devContext->RSSetState(pOtherState); //SkyBox.worldMatrix = viewMatrix; SkyBox.worldMatrix.M[3][0] = secondCamera.M[3][0]; SkyBox.worldMatrix.M[3][1] = secondCamera.M[3][1]; SkyBox.worldMatrix.M[3][2] = secondCamera.M[3][2]; SkyBox.Render(); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); /////////////////////// devContext->RSSetState(pRasterState); Pyramid.Render(); Quad.Render(); Tree.Render(); Tree2.Render(); Tree1.Render(); devContext->ResolveSubresource(fixerTexture, D3D11CalcSubresource(0, 0, 1), renderTexture, D3D11CalcSubresource(0, 0, 1), DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); QuadSeed.Render(); devContext->GenerateMips(QuadSeed.pShaderResource); ///////////////////////////////////////////////////////////////////////////////////////////////////////// devContext->OMSetRenderTargets(1, &postProcessView, DepthStencilView); devContext->RSSetViewports(1, &viewPort); //float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; devContext->ClearRenderTargetView(postProcessView, ClearColor); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); scene[0] = Inverse4x4(viewMatrix); //D3D11_MAPPED_SUBRESOURCE sceneMap; ZeroMemory(&sceneMap, sizeof(sceneMap)); devContext->Map(sceneMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sceneMap); memcpy(sceneMap.pData, scene, sizeof(scene)); devContext->Unmap(sceneMatrixBuffer, 0); devContext->VSSetConstantBuffers(1, 1, &sceneMatrixBuffer); devContext->PSSetConstantBuffers(0, 1, &lightBuff); devContext->PSSetConstantBuffers(1, 1, &ambientBuff); devContext->PSSetConstantBuffers(2, 1, &ptltBuff); devContext->PSSetConstantBuffers(3, 1, &spotBuff); devContext->GSSetConstantBuffers(1, 1, &sceneMatrixBuffer); //Pyramid.worldMatrices[0] = RotateY(50.0f * dt) * Pyramid.worldMatrices[0]; //Pyramid.worldMatrices[1] = RotateZ(50.0f * dt) * Pyramid.worldMatrices[1]; //Pyramid.worldMatrices[2] = RotateX(50.0f * dt) * Pyramid.worldMatrices[2]; //Pyramid.worldMatrices[3] = RotateY(200.0f * dt) * RotateZ(200.0f * dt) * RotateX(200.0f * dt) * Pyramid.worldMatrices[3]; //SKYBOX RENDERING///// devContext->RSSetState(pOtherState); //SkyBox.worldMatrix = viewMatrix; SkyBox.worldMatrix.M[3][0] = viewMatrix.M[3][0]; SkyBox.worldMatrix.M[3][1] = viewMatrix.M[3][1]; SkyBox.worldMatrix.M[3][2] = viewMatrix.M[3][2]; SkyBox.Render(); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); /////////////////////// devContext->RSSetState(pRasterState); Pyramid.Render(); Quad.Render(); Tree.Render(); Tree1.Render(); Tree2.Render(); devContext->ResolveSubresource(fixerTexture, D3D11CalcSubresource(0, 0, 1), renderTexture, D3D11CalcSubresource(0, 0, 1), DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); QuadSeed.Render(); devContext->GenerateMips(QuadSeed.pShaderResource); devContext->ResolveSubresource(fixerTexture, D3D11CalcSubresource(0, 0, 1), postTexture, D3D11CalcSubresource(0, 0, 1), DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); PostQuad.Render(); ///////////////////////////////////////////////////////////////// devContext->OMSetRenderTargets(1, &targetView, DepthStencilView); //devContext->RSSetViewports(1, &viewPort); //float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; devContext->ClearRenderTargetView(targetView, ClearColor); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); MoveCamera(5.0f, 200.0f, dt); D3D11_MAPPED_SUBRESOURCE ambMap; ZeroMemory(&ambMap, sizeof(ambMap)); devContext->Map(ambientBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &ambMap); memcpy(ambMap.pData, &ambientLight, sizeof(ambientLight)); devContext->Unmap(ambientBuff, 0); D3D11_MAPPED_SUBRESOURCE lightMap; ZeroMemory(&lightMap, sizeof(lightMap)); devContext->Map(lightBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &lightMap); memcpy(lightMap.pData, &theLight, sizeof(theLight)); devContext->Unmap(lightBuff, 0); D3D11_MAPPED_SUBRESOURCE ptLtMap; ZeroMemory(&ptLtMap, sizeof(ptLtMap)); devContext->Map(ptltBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &ptLtMap); memcpy(ptLtMap.pData, &thePtLight, sizeof(thePtLight)); devContext->Unmap(ptltBuff, 0); D3D11_MAPPED_SUBRESOURCE spotMap; ZeroMemory(&spotMap, sizeof(spotMap)); devContext->Map(spotBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &spotMap); memcpy(spotMap.pData, &theSpotLight, sizeof(theSpotLight)); devContext->Unmap(spotBuff, 0); scene[0] = Inverse4x4(viewMatrix); SkyBox.worldMatrix.M[3][0] = viewMatrix.M[3][0]; SkyBox.worldMatrix.M[3][1] = viewMatrix.M[3][1]; SkyBox.worldMatrix.M[3][2] = viewMatrix.M[3][2]; //D3D11_MAPPED_SUBRESOURCE sceneMap; ZeroMemory(&sceneMap, sizeof(sceneMap)); devContext->Map(sceneMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sceneMap); memcpy(sceneMap.pData, scene, sizeof(scene)); devContext->Unmap(sceneMatrixBuffer, 0); devContext->VSSetConstantBuffers(1, 1, &sceneMatrixBuffer); devContext->PSSetConstantBuffers(0, 1, &lightBuff); devContext->PSSetConstantBuffers(1, 1, &ambientBuff); devContext->PSSetConstantBuffers(2, 1, &ptltBuff); devContext->PSSetConstantBuffers(3, 1, &spotBuff); devContext->GSSetConstantBuffers(1, 1, &sceneMatrixBuffer); //Pyramid.worldMatrices[0] = RotateY(50.0f * dt) * Pyramid.worldMatrices[0]; //Pyramid.worldMatrices[1] = RotateZ(50.0f * dt) * Pyramid.worldMatrices[1]; //Pyramid.worldMatrices[2] = RotateX(50.0f * dt) * Pyramid.worldMatrices[2]; //Pyramid.worldMatrices[3] = RotateY(200.0f * dt) * RotateZ(200.0f * dt) * RotateX(200.0f * dt) * Pyramid.worldMatrices[3]; //SKYBOX RENDERING///// devContext->RSSetState(pOtherState); //SkyBox.worldMatrix = viewMatrix; SkyBox.Render(); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); /////////////////////// devContext->RSSetState(pRasterState); Pyramid.Render(); Quad.Render(); Tree.Render(); Tree1.Render(); Tree2.Render(); devContext->ResolveSubresource(fixerTexture, D3D11CalcSubresource(0, 0, 1), renderTexture, D3D11CalcSubresource(0, 0, 1), DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); QuadSeed.Render(); devContext->GenerateMips(QuadSeed.pShaderResource); //WHY BROKEN? devContext->ResolveSubresource(fixerTexture, D3D11CalcSubresource(0, 0, 1), postTexture, D3D11CalcSubresource(0, 0, 1), DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); PostQuad.Render(); /////////////////////2nd VIEWPORT///////////////////////////////////////////////////////////////////////////////////////////////// devContext->OMSetRenderTargets(1, &anotherView, DepthStencilView); devContext->RSSetViewports(1, &otherPort); //float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; devContext->ClearRenderTargetView(anotherView, ClearColor); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); scene[0] = secondCamera; //D3D11_MAPPED_SUBRESOURCE sceneMap; ZeroMemory(&sceneMap, sizeof(sceneMap)); devContext->Map(sceneMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sceneMap); memcpy(sceneMap.pData, scene, sizeof(scene)); devContext->Unmap(sceneMatrixBuffer, 0); devContext->VSSetConstantBuffers(1, 1, &sceneMatrixBuffer); devContext->PSSetConstantBuffers(0, 1, &lightBuff); devContext->PSSetConstantBuffers(1, 1, &ambientBuff); devContext->PSSetConstantBuffers(2, 1, &ptltBuff); devContext->PSSetConstantBuffers(3, 1, &spotBuff); devContext->GSSetConstantBuffers(1, 1, &sceneMatrixBuffer); //Pyramid.worldMatrices[0] = RotateY(50.0f * dt) * Pyramid.worldMatrices[0]; //Pyramid.worldMatrices[1] = RotateZ(50.0f * dt) * Pyramid.worldMatrices[1]; //Pyramid.worldMatrices[2] = RotateX(50.0f * dt) * Pyramid.worldMatrices[2]; //Pyramid.worldMatrices[3] = RotateY(200.0f * dt) * RotateZ(200.0f * dt) * RotateX(200.0f * dt) * Pyramid.worldMatrices[3]; //SKYBOX RENDERING///// devContext->RSSetState(pOtherState); //SkyBox.worldMatrix = viewMatrix; SkyBox.worldMatrix.M[3][0] = secondCamera.M[3][0]; SkyBox.worldMatrix.M[3][1] = secondCamera.M[3][1]; SkyBox.worldMatrix.M[3][2] = secondCamera.M[3][2]; SkyBox.Render(); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); /////////////////////// devContext->RSSetState(pRasterState); Pyramid.Render(); Quad.Render(); Tree.Render(); Tree1.Render(); Tree2.Render(); devContext->ResolveSubresource(fixerTexture, D3D11CalcSubresource(0, 0, 1), renderTexture, D3D11CalcSubresource(0, 0, 1), DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); QuadSeed.Render(); devContext->GenerateMips(QuadSeed.pShaderResource); //PostQuad.Render(); ///////////////////////////////////////////////////////////////// devContext->OMSetRenderTargets(1, &targetView, DepthStencilView); //devContext->RSSetViewports(1, &viewPort); //float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; //devContext->ClearRenderTargetView(targetView, ClearColor); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); //MoveCamera(5.0f, 200.0f, dt); //D3D11_MAPPED_SUBRESOURCE ambMap; ZeroMemory(&ambMap, sizeof(ambMap)); devContext->Map(ambientBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &ambMap); memcpy(ambMap.pData, &ambientLight, sizeof(ambientLight)); devContext->Unmap(ambientBuff, 0); //D3D11_MAPPED_SUBRESOURCE lightMap; ZeroMemory(&lightMap, sizeof(lightMap)); devContext->Map(lightBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &lightMap); memcpy(lightMap.pData, &theLight, sizeof(theLight)); devContext->Unmap(lightBuff, 0); //D3D11_MAPPED_SUBRESOURCE ptLtMap; ZeroMemory(&ptLtMap, sizeof(ptLtMap)); devContext->Map(ptltBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &ptLtMap); memcpy(ptLtMap.pData, &thePtLight, sizeof(thePtLight)); devContext->Unmap(ptltBuff, 0); //D3D11_MAPPED_SUBRESOURCE spotMap; ZeroMemory(&spotMap, sizeof(spotMap)); devContext->Map(spotBuff, 0, D3D11_MAP_WRITE_DISCARD, 0, &spotMap); memcpy(spotMap.pData, &theSpotLight, sizeof(theSpotLight)); devContext->Unmap(spotBuff, 0); scene[0] = secondCamera;//Inverse4x4(viewMatrix); //D3D11_MAPPED_SUBRESOURCE sceneMap; ZeroMemory(&sceneMap, sizeof(sceneMap)); devContext->Map(sceneMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sceneMap); memcpy(sceneMap.pData, scene, sizeof(scene)); devContext->Unmap(sceneMatrixBuffer, 0); devContext->VSSetConstantBuffers(1, 1, &sceneMatrixBuffer); devContext->PSSetConstantBuffers(0, 1, &lightBuff); devContext->PSSetConstantBuffers(1, 1, &ambientBuff); devContext->PSSetConstantBuffers(2, 1, &ptltBuff); devContext->PSSetConstantBuffers(3, 1, &spotBuff); devContext->GSSetConstantBuffers(1, 1, &sceneMatrixBuffer); Pyramid.worldMatrices[0] = RotateY(50.0f * dt) * Pyramid.worldMatrices[0]; Pyramid.worldMatrices[1] = RotateZ(50.0f * dt) * Pyramid.worldMatrices[1]; Pyramid.worldMatrices[2] = RotateX(50.0f * dt) * Pyramid.worldMatrices[2]; Pyramid.worldMatrices[3] = RotateY(200.0f * dt) * RotateZ(200.0f * dt) * RotateX(200.0f * dt) * Pyramid.worldMatrices[3]; //SKYBOX RENDERING///// devContext->RSSetState(pOtherState); //SkyBox.worldMatrix = viewMatrix; SkyBox.worldMatrix.M[3][0] = secondCamera.M[3][0]; SkyBox.worldMatrix.M[3][1] = secondCamera.M[3][1]; SkyBox.worldMatrix.M[3][2] = secondCamera.M[3][2]; SkyBox.Render(); devContext->ClearDepthStencilView(DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); /////////////////////// devContext->RSSetState(pRasterState); Pyramid.Render(); Quad.Render(); Tree.Render(); Tree1.Render(); Tree2.Render(); devContext->ResolveSubresource(fixerTexture, D3D11CalcSubresource(0, 0, 1), renderTexture, D3D11CalcSubresource(0, 0, 1), DXGI_FORMAT_R8G8B8A8_UNORM_SRGB); QuadSeed.Render(); devContext->GenerateMips(QuadSeed.pShaderResource); swapChain->Present(0, 0); return true; }
void RenderFrameDX10(void) { Vector4 vClearColor(0.0f, 0.0f, 0.0f, 0.0f); UINT stride = sizeof(Vertex_VC); UINT offset = 0; // 取得呼叫GutCreateGraphicsDeviceDX10時所產生的D3D10物件 ID3D10RenderTargetView *pRenderTargetView = GutGetDX10RenderTargetView(); //frame buffer ID3D10DepthStencilView *pDepthStencilView = GutGetDX10DepthStencilView(); //depth/stencil buffer IDXGISwapChain *pSwapChain = GutGetDX10SwapChain(); // front/back buffer // 清除顏色 g_pDevice->ClearRenderTargetView(pRenderTargetView, (float *)&vClearColor); // 清除Depth/Stencil buffer g_pDevice->ClearDepthStencilView(pDepthStencilView, D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0); // 設定vertex shader g_pDevice->VSSetShader(g_pVertexShader); // 設定pixel shader g_pDevice->PSSetShader(g_pPixelShader); // 設定vertex shader讀取參數的記憶體位罝 g_pDevice->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); // 設定vertex資料格式 g_pDevice->IASetInputLayout(g_pVertexLayout); // 設定index buffer g_pDevice->IASetIndexBuffer(g_pSphereIndexBuffer, DXGI_FORMAT_R16_UINT, 0); // 設定三角形頂點索引值資料排列是triangle strip g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 計算出一個可以轉換到鏡頭座標系的矩陣 D3DXMATRIX temp_matrix; D3DXMATRIX view_matrix, view_proj_matrix, world_view_proj_matrix; D3DXMatrixLookAtRH(&view_matrix, (D3DXVECTOR3 *)&g_eye, (D3DXVECTOR3 *)&g_lookat, (D3DXVECTOR3 *)&g_up); D3DXMatrixMultiply(&view_proj_matrix, &view_matrix, &g_proj_matrix); // 太陽, 帶入滑鼠的旋轉控制 D3DXMATRIX sun_matrix, sun_ry, sun_rx; D3DXMatrixRotationX(&sun_rx, g_fRotate_X); D3DXMatrixRotationY(&sun_ry, g_fRotate_Y); D3DXMatrixMultiply(&sun_matrix, &sun_ry, &sun_rx); D3DXMatrixMultiply(&world_view_proj_matrix, &sun_matrix, &view_proj_matrix); // 設定shader參數 D3DXMATRIX *pConstData; g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); // g_pDevice->IASetVertexBuffers(0, 1, &g_pSunVertexBuffer, &stride, &offset); g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // 水星 float mercury_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year_mercury); D3DXMATRIX mercury_matrix, mercury_translate, mercury_rotate; D3DXMatrixRotationY(&mercury_rotate, mercury_rotate_y); D3DXMatrixTranslation(&mercury_translate, mercury_to_sun_distance, 0.0f, 0.0f); D3DXMatrixMultiply(&temp_matrix, &mercury_translate, &mercury_rotate); D3DXMatrixMultiply(&mercury_matrix, &temp_matrix, &sun_matrix); D3DXMatrixMultiply(&world_view_proj_matrix, &mercury_matrix, &view_proj_matrix); // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); g_pDevice->IASetVertexBuffers(0, 1, &g_pMoonVertexBuffer, &stride, &offset); g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // 金星 float venus_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year_venus); D3DXMATRIX venus_matrix, venus_rotate, venus_translate; D3DXMatrixRotationY(&venus_rotate, venus_rotate_y); D3DXMatrixTranslation(&venus_translate, venus_to_sun_distance, 0.0f, 0.0f); D3DXMatrixMultiply(&temp_matrix, &venus_translate, &venus_rotate); D3DXMatrixMultiply(&venus_matrix, &temp_matrix, &sun_matrix); D3DXMatrixMultiply(&world_view_proj_matrix, &venus_matrix, &view_proj_matrix); // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); g_pDevice->IASetVertexBuffers(0, 1, &g_pMoonVertexBuffer, &stride, &offset); g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // 地球 float earth_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year); D3DXMATRIX earth_matrix, earth_rotate, earth_translate; D3DXMatrixRotationY(&earth_rotate, earth_rotate_y); D3DXMatrixTranslation(&earth_translate, earth_to_sun_distance, 0.0f, 0.0f); D3DXMatrixMultiply(&temp_matrix, &earth_translate, &earth_rotate); D3DXMatrixMultiply(&earth_matrix, &temp_matrix, &sun_matrix); D3DXMatrixMultiply(&world_view_proj_matrix, &earth_matrix, &view_proj_matrix); // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); g_pDevice->IASetVertexBuffers(0, 1, &g_pEarthVertexBuffer, &stride, &offset); g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // 月亮 float moon_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_month); D3DXMATRIX moon_matrix, moon_rotate, moon_translate; D3DXMatrixRotationY(&moon_rotate, moon_rotate_y); D3DXMatrixTranslation(&moon_translate, moon_to_earth_distance, 0.0f, 0.0f); D3DXMatrixMultiply(&temp_matrix, &moon_translate, &moon_rotate); D3DXMatrixMultiply(&moon_matrix, &temp_matrix, &earth_matrix); D3DXMatrixMultiply(&world_view_proj_matrix, &moon_matrix, &view_proj_matrix); // 設定shader參數 g_pConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstData ); *pConstData = world_view_proj_matrix; g_pConstantBuffer->Unmap(); g_pDevice->IASetVertexBuffers(0, 1, &g_pMoonVertexBuffer, &stride, &offset); g_pDevice->DrawIndexed(g_iNumSphereIndices, 0, 0); // 等待硬體掃結束, 然後才更新畫面 pSwapChain->Present(1, 0); }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow) { DXTInputHandlerDefault inputHandler; DXTWindowEventHandlerDefault eventHandler; DXTWindow window(hInstance, &inputHandler, &eventHandler); DXTRenderParams params; params.Extent = { 800, 600 }; params.UseVSync = true; params.Windowed = true; HRESULT result; result = window.Initialize(params, "DXT Example (DirectX 11)"); if (SUCCEEDED(result)) { ID3D11Device* device; ID3D11DeviceContext* context; IDXGISwapChain* swapChain; result = DXTInitDevice(params, &window, &swapChain, &device, &context); if (SUCCEEDED(result)) { eventHandler.SetSwapChain(swapChain); FLOAT clearColor[] = { 0.5f, 0.5f, 1.0f, 1.0f }; D3D11_INPUT_ELEMENT_DESC inputDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, sizeof(float) * 3, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, sizeof(float) * 5, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; UINT elementCount = 3; UINT stride = 8 * sizeof(FLOAT); UINT offset = 0; UINT indexCount = 0; FLOAT deltaTime = 0.016f; DXTSphericalCamera camera; DXTFirstPersonCameraController cameraController(&camera, &inputHandler); inputHandler.AddInputInterface(&cameraController); cameraController.Velocity = 40.0f; cameraController.RotationVelocity = 0.005f; camera.Position = DirectX::XMFLOAT3(20.0f, 20.0f, 20.0f); camera.LookAt(DirectX::XMFLOAT3(0.0f, 0.0f, 0.0f)); ID3D11RenderTargetView* renderTargetView; ID3D11Texture2D* depthBuffer; ID3D11DepthStencilView* depthBufferView; ID3D11VertexShader* vertexShader; ID3D11PixelShader* pixelShader; DXTBytecodeBlob vertexBytecode; ID3D11DepthStencilState* depthState; ID3D11RasterizerState* rasterizerState; ID3D11Buffer* vertexBuffer; ID3D11Buffer* indexBuffer; ID3D11InputLayout* inputLayout; ID3D11Buffer* transformBuffer; DXTCreateRenderTargetFromBackBuffer(swapChain, device, &renderTargetView); DXTCreateDepthStencilBuffer(device, params.Extent.Width, params.Extent.Height, DXGI_FORMAT_D24_UNORM_S8_UINT, &depthBuffer, &depthBufferView); DXTVertexShaderFromFile(device, "VertexShader.cso", &vertexShader, &vertexBytecode); DXTPixelShaderFromFile(device, "PixelShader.cso", &pixelShader); DXTCreateDepthStencilStateDepthTestEnabled(device, &depthState); DXTCreateRasterizerStateSolid(device, &rasterizerState); DXTLoadStaticMeshFromFile(device, "mesh.ase", DXTVertexAttributePosition | DXTVertexAttributeUV | DXTVertexAttributeNormal, DXTIndexTypeShort, &vertexBuffer, &indexBuffer, &indexCount); DXTCreateBuffer(device, sizeof(DirectX::XMFLOAT4X4) * 2, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE, D3D11_USAGE_DYNAMIC, &transformBuffer); device->CreateInputLayout(inputDesc, elementCount, vertexBytecode.Bytecode, vertexBytecode.BytecodeLength, &inputLayout); vertexBytecode.Destroy(); window.Present(false); while (!window.QuitMessageReceived()) { window.MessagePump(); if (inputHandler.IsKeyDown(VK_ESCAPE)) break; cameraController.Update(deltaTime); XMFLOAT4X4 ViewProj; XMFLOAT4X4 World; camera.GetViewProjectionMatrix(&ViewProj, params.Extent); XMStoreFloat4x4(&World, XMMatrixIdentity()); D3D11_MAPPED_SUBRESOURCE subres; context->Map(transformBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &subres); XMFLOAT4X4* ptr = (XMFLOAT4X4*)subres.pData; ptr[0] = World; ptr[1] = ViewProj; context->Unmap(transformBuffer, 0); D3D11_VIEWPORT viewport = { 0.0f, 0.0f, (FLOAT)params.Extent.Width, (FLOAT)params.Extent.Height, 0.0f, 1.0f }; context->ClearRenderTargetView(renderTargetView, clearColor); context->ClearDepthStencilView(depthBufferView, D3D11_CLEAR_DEPTH, 1.0f, 0); context->OMSetDepthStencilState(depthState, 0); context->OMSetRenderTargets(1, &renderTargetView, depthBufferView); context->RSSetState(rasterizerState); context->RSSetViewports(1, &viewport); context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); context->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset); context->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0); context->IASetInputLayout(inputLayout); context->VSSetShader(vertexShader, nullptr, 0); context->PSSetShader(pixelShader, nullptr, 0); context->VSSetConstantBuffers(0, 1, &transformBuffer); context->DrawIndexed(indexCount, 0, 0); swapChain->Present(1, 0); } swapChain->SetFullscreenState(false, nullptr); transformBuffer->Release(); depthBufferView->Release(); depthBuffer->Release(); inputLayout->Release(); vertexBuffer->Release(); indexBuffer->Release(); depthState->Release(); rasterizerState->Release(); vertexShader->Release(); pixelShader->Release(); renderTargetView->Release(); swapChain->Release(); context->Release(); device->Release(); } window.Destroy(); } }
// WinMain int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow ) { HRESULT hr; // ウィンドウクラスを登録 WNDCLASSEX wcex = { sizeof( WNDCLASSEX ), // cbSize CS_HREDRAW | CS_VREDRAW, // style WndProc, // lpfnWndProc 0, // cbClsExtra 0, // cbWndExtra hInstance, // hInstance NULL, // hIcon NULL, // hCursor ( HBRUSH )( COLOR_WINDOW + 1 ), // hbrBackGround NULL, // lpszMenuName g_className, // lpszClassName NULL // hIconSm }; if ( ! RegisterClassEx( &wcex ) ) { MessageBox( NULL, _T( "失敗: RegisterClassEx()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "RegisterClassEx: ok\n" ) ); // ウィンドウサイズを計算 RECT r = { 0, 0, 800, 450 }; // 800x450 (16:9) if ( ! AdjustWindowRect( &r, WS_OVERLAPPEDWINDOW, FALSE ) ) { MessageBox( NULL, _T( "失敗: AdjustWindowRect()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "AdjustWindowRect: ok (%d, %d)-(%d, %d)\n" ), r.left, r.top, r.right, r.bottom ); // ウィンドウ生成 HWND hWnd; hWnd = CreateWindow( g_className, g_windowName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, r.right - r.left, r.bottom - r.top, NULL, NULL, hInstance, NULL ); if ( hWnd == NULL ) { MessageBox( NULL, _T( "失敗: CreateWindow()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "CreateWindow: ok\n" ) ); // ウィンドウ表示 ShowWindow(hWnd, nCmdShow); dtprintf( _T( "ShowWindow: ok\n" ) ); // スワップチェイン設定 DXGI_SWAP_CHAIN_DESC scDesc = { { 1280, // BufferDesc.Width 720, // BufferDesc.Height { 60, // BufferDesc.RefreshRate.Numerator 1 // BufferDesc.RefreshRate.Denominator }, DXGI_FORMAT_R16G16B16A16_FLOAT, // BufferDesc.Format DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED, // BufferDesc.ScanlineOrdering DXGI_MODE_SCALING_CENTERED // BufferDesc.Scaling }, { 1, // SampleDesc.Count 0 // SampleDesc.Quality }, DXGI_USAGE_RENDER_TARGET_OUTPUT, // BufferUsage 1, // BufferCount hWnd, // OutputWindow TRUE, // Windowed DXGI_SWAP_EFFECT_DISCARD, // SwapEffect DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH // Flags }; // Direct3D11 デバイス・デバイスコンテキスト・スワップチェーン生成 ID3D11Device * pDevice = NULL; ID3D11DeviceContext * pDeviceContext = NULL; IDXGISwapChain * pSwapChain = NULL; D3D_FEATURE_LEVEL feature; hr = D3D11CreateDeviceAndSwapChain( NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &scDesc, &pSwapChain, &pDevice, &feature, &pDeviceContext ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: D3D11CreateDeviceAndSwapChain()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "D3D11CreateDeviceAndSwapChain: ok (pDevice: 0x%p, pDeviceContext: 0x%p, pSwapChain: 0x%p, feature: 0x%4x)\n" ), pDevice, pDeviceContext, pSwapChain, ( int ) feature ); // バックバッファテクスチャ取得 ID3D11Texture2D * pBackBuffer = NULL; hr = pSwapChain->GetBuffer( 0, __uuidof( pBackBuffer ), reinterpret_cast< void ** >( &pBackBuffer ) ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: IDXGISwapChain::GetBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "IDXGISwapChain::GetBuffer: ok (pBackBuffer: 0x%p)\n" ), pBackBuffer ); // レンダーターゲットビュー生成 ID3D11RenderTargetView * pRenderTargetView = NULL; hr = pDevice->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateRenderTargetView()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateRenderTargetView: ok (pRenderTargetView: 0x%p)\n" ), pRenderTargetView ); // レンダーターゲットビューをバインド pDeviceContext->OMSetRenderTargets( 1, &pRenderTargetView, NULL ); dtprintf( _T( "ID3D11DeviceContext::OMSetRenderTargets: ok\n" ) ); // バックバッファはもうここでは使わない COM_SAFE_RELEASE( pBackBuffer ); // ビューポートをバインド D3D11_VIEWPORT viewport = { 0.0f, // TopLeftX 0.0f, // TopLeftY 1280.0f, // Width 720.0f, // Height 0.0f, // MinDepth 1.0f // MaxDepth }; pDeviceContext->RSSetViewports( 1, &viewport ); dtprintf( _T( "ID3D11DeviceContext::RSSetViewports: ok\n" ) ); // 頂点データ float vertices[ 5 ][ 7 ] = { // Xaxis Yaxis Zaxis 赤 緑 青 Alpha { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f }, // 原点 { -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f }, // 左上 { 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f }, // 右上 { 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f }, // 右下 { -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f } // 左下 }; // 入力エレメント記述子 D3D11_INPUT_ELEMENT_DESC verticesDesc[] = { { "IN_POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "IN_COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, sizeof(float)*3, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; // 頂点バッファを生成 D3D11_BUFFER_DESC vertexBufferDesc = { 5 * sizeof( float ) * 7, // ByteWidth D3D11_USAGE_DEFAULT, // Usage D3D11_BIND_VERTEX_BUFFER, // BindFlags 0, // CPUAccessFlags 0, // MiscFlags 0 // StructureByteStride }; D3D11_SUBRESOURCE_DATA vertexResourceData = { vertices }; ID3D11Buffer * pVertexBuffer = NULL; hr = pDevice->CreateBuffer( &vertexBufferDesc, &vertexResourceData, &pVertexBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateBuffer: ok (pVertexBuffer: 0x%p)\n" ), pVertexBuffer ); // 頂点バッファをバインド UINT strides[] = { sizeof( float ) * 7 }; UINT offsets[] = { 0 }; pDeviceContext->IASetVertexBuffers( 0, 1, &pVertexBuffer, strides, offsets ); dtprintf( _T( "ID3D11DeviceContext::IASetVertexBuffers: ok\n" ) ); // インデックスデータ unsigned int indices[] = { 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 1 }; // インデックスバッファを生成 D3D11_BUFFER_DESC indexBufferDesc = { sizeof( unsigned int ) * 12, // ByteWidth D3D11_USAGE_DEFAULT, // Usage D3D11_BIND_INDEX_BUFFER, // BindFlags 0, // CPUAccessFlags 0, // MiscFlags 0 // StructureByteStride }; D3D11_SUBRESOURCE_DATA indexResourceData = { indices }; ID3D11Buffer * pIndexBuffer = NULL; hr = pDevice->CreateBuffer( &indexBufferDesc, &indexResourceData, &pIndexBuffer ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateBuffer()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateBuffer: ok (pIndexBuffer: 0x%p)\n" ), pIndexBuffer ); // インデックスバッファをバインド pDeviceContext->IASetIndexBuffer( pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 ); dtprintf( _T( "ID3D11DeviceContext::IASetIndexBuffer: ok\n" ) ); // プリミティブタイプを設定 pDeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); dtprintf( _T( "ID3D11DeviceContext::IASetPrimitiveTopology: ok\n" ) ); // 頂点シェーダを作成 ID3D11VertexShader * pVertexShader = NULL; hr = pDevice->CreateVertexShader( g_vs_constant, sizeof( g_vs_constant ), NULL, &pVertexShader ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateVertexShader()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateVertexShader: ok (pVertexShader: 0x%p)\n" ), pVertexShader ); // ピクセルシェーダを作成 ID3D11PixelShader * pPixelShader = NULL; hr = pDevice->CreatePixelShader( g_ps_constant, sizeof( g_ps_constant ), NULL, &pPixelShader ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreatePixelShader()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreatePixelShader: ok (pPixelShader: 0x%p)\n" ), pPixelShader ); // シェーダをバインド pDeviceContext->VSSetShader( pVertexShader, NULL, 0 ); dtprintf( _T( "ID3D11DeviceContext::VSSetShader: ok\n" ) ); pDeviceContext->PSSetShader( pPixelShader, NULL, 0 ); dtprintf( _T( "ID3D11DeviceContext::PSSetShader: ok\n" ) ); pDeviceContext->GSSetShader( NULL, NULL, 0 ); pDeviceContext->HSSetShader( NULL, NULL, 0 ); pDeviceContext->DSSetShader( NULL, NULL, 0 ); // 入力レイアウトを生成 ID3D11InputLayout * pInputLayout = NULL; hr = pDevice->CreateInputLayout( verticesDesc, 2, g_vs_constant, sizeof( g_vs_constant ), &pInputLayout ); if ( FAILED( hr ) ) { MessageBox( NULL, _T( "失敗: ID3D11Device::CreateInputLayout()" ), _T( "エラー" ), MB_OK | MB_ICONERROR ); return 0; } dtprintf( _T( "ID3D11Device::CreateInputLayout: ok (pInputLayout: 0x%p)\n" ), pInputLayout ); // 入力レイアウトをバインド pDeviceContext->IASetInputLayout( pInputLayout ); dtprintf( _T( "ID3D11DeviceContext::IASetInputLayout: ok\n" ) ); MSG msg; while ( 1 ) { // メッセージを取得 if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if ( msg.message == WM_QUIT ) { dtprintf( _T( "PeekMessage: WM_QUIT\n" ) ); break; } // メッセージ処理 DispatchMessage( &msg ); } else { // レンダーターゲットをクリア const float clear[ 4 ] = { 0.0f, 0.0f, 1.0f, 1.0f }; // RGBA pDeviceContext->ClearRenderTargetView( pRenderTargetView, clear ); // 描画 pDeviceContext->DrawIndexed( 12, 0, 0 ); pSwapChain->Present( 1, 0 ); // ちょっとだけ待つ Sleep( 5 ); } } // シェーダをアンバインド pDeviceContext->VSSetShader( NULL, NULL, 0 ); pDeviceContext->PSSetShader( NULL, NULL, 0 ); // デバイス・リソース解放 COM_SAFE_RELEASE( pInputLayout ); COM_SAFE_RELEASE( pPixelShader ); COM_SAFE_RELEASE( pVertexShader ); COM_SAFE_RELEASE( pIndexBuffer ); COM_SAFE_RELEASE( pVertexBuffer ); COM_SAFE_RELEASE( pRenderTargetView ); COM_SAFE_RELEASE( pSwapChain ); COM_SAFE_RELEASE( pDeviceContext ); COM_SAFE_RELEASE( pDevice ); return msg.wParam; }