void Asteroids::CreateGUIResources() { auto font = mGUI->Font(); D3D11_TEXTURE2D_DESC textureDesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_A8_UNORM, font->BitmapWidth(), font->BitmapHeight(), 1, 1); D3D11_SUBRESOURCE_DATA initialData = {}; initialData.pSysMem = font->Pixels(); initialData.SysMemPitch = font->BitmapWidth(); ID3D11Texture2D* texture = nullptr; ThrowIfFailed(mDevice->CreateTexture2D(&textureDesc, &initialData, &texture)); ThrowIfFailed(mDevice->CreateShaderResourceView(texture, nullptr, &mFontTextureSRV)); SafeRelease(&texture); // Load any GUI sprite textures for (size_t i = 0; i < mGUI->size(); ++i) { auto control = (*mGUI)[i]; if (control->TextureFile().length() > 0 && mSpriteTextures.find(control->TextureFile()) == mSpriteTextures.end()) { std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; ID3D11ShaderResourceView* textureSRV = nullptr; ThrowIfFailed(CreateDDSTextureFromFile(mDevice, converter.from_bytes(control->TextureFile()).c_str(), &textureSRV, true)); mSpriteTextures[control->TextureFile()] = textureSRV; } } }
int JRenderServer::CreateTexture( const TextureProperties& texProp ) { int texID = GetTextureID( texProp.m_Name ); if (texID != -1) { if (m_Textures[texID].m_pTexture) { return texID; } } else { m_Textures.push_back( TextureFile() ); texID = m_Textures.size() - 1; } DWORD usage = 0; D3DFORMAT format = ConvertColorFormat( texProp.m_Format ); D3DPOOL pool = ConvertPoolType( texProp.m_PoolType ); IDirect3DTexture8* pTexture = NULL; if (texProp.m_bRenderTarget) { usage = D3DUSAGE_RENDERTARGET; } if (pool == D3DPOOL_DEFAULT) { m_pDevice->ResourceManagerDiscardBytes( 0 ); } HRESULT hRes = m_pDevice->CreateTexture( texProp.m_Width, texProp.m_Height, texProp.m_NMips, usage, format, pool, &pTexture ); if (hRes != S_OK) { rlog.err( "Could not reate texture %s (width=%d, height=%d).", texProp.m_Name, texProp.m_Width, texProp.m_Height ); return -1; } TextureFile& tex = m_Textures[texID]; tex.m_Prop = texProp; tex.m_pTexture = pTexture; tex.m_pDepthStencil = NULL; tex.m_Name = texProp.m_Name; return texID; } // JRenderServer::CreateTexture
void VsPlane::CreateParts(Simulator *lpSim, Structure *lpStructure) { TRACE_DEBUG("Statring Parts Creation for " + m_strName); TRACE_DETAIL_NS("PartType: Plane"); VsSimulator *lpVsSim = dynamic_cast<VsSimulator *>(lpSim); if(!lpVsSim) THROW_ERROR(Vs_Err_lUnableToConvertToVsSimulator, Vs_Err_strUnableToConvertToVsSimulator); int iImageWidth, iImageHeight; float fltImageDx, fltImageDy; if(Std_IsBlank(m_strHeightFieldImage)) { m_aryGroundTM[3][1] = m_fltHeight; m_lpGeometryID = McdPlaneCreate(); } else { m_lpGeometryID = McduRGHeighFieldCreateFromBmpEx((char *) AnimatLibrary::GetFilePath(lpSim->ProjectPath(), m_strHeightFieldImage).c_str(), m_ptMapLocation.x, m_ptMapLocation.y, m_ptMapSize.x, m_ptMapSize.y, m_fltMapScale, m_fltMapScale, m_fltMapScale, m_fltHeight, &iImageWidth, &iImageHeight, &fltImageDx, &fltImageDy); if(!m_lpGeometryID) THROW_PARAM_ERROR(Vs_Err_lCreatingGeometry, Vs_Err_strCreatingGeometry, "BodyID", m_strName); McdRGHeightFieldSetForceTriangleNormalTolerance(m_lpGeometryID, 1); } m_lpModelID = MstFixedModelCreate(lpVsSim->Universe(), m_lpGeometryID, m_aryGroundTM); if(Std_IsBlank(m_strHeightFieldImage)) m_lpGraphic = RGraphicGroundPlaneCreate(lpVsSim->RenderContext(), 244, 2, m_aryColor, m_aryGroundTM[3][1]); else { m_lpGraphic = RGraphicRGHeightfieldCreate(lpVsSim->RenderContext(), iImageWidth, iImageHeight, fltImageDx, fltImageDy, m_ptMapLocation.x, m_ptMapLocation.y, McdRGHeightFieldGetHeightArray(m_lpGeometryID), m_aryColor, McdModelGetTransformPtr(m_lpModelID)); } if(!Std_IsBlank(m_strTexture)) RGraphicSetTexture(lpVsSim->RenderContext(), m_lpGraphic, TextureFile(lpSim, m_strTexture).c_str()); Plane::CreateParts(lpSim, lpStructure); McdModelSetUserData(m_lpModelID, (void*) this); TRACE_DEBUG("Ending Parts Creation for " + m_strName); }
void Asteroids::Render(float frameTime, const OrbitCamera& camera, const Settings& settings) { ProfileBeginFrame(0); ProfileBeginRender(); // Frame data ProfileBeginSimUpdate(); mAsteroids->Update(frameTime, camera.Eye(), settings); auto staticAsteroidData = mAsteroids->StaticData(); auto dynamicAsteroidData = mAsteroids->DynamicData(); ProfileEndSimUpdate(); // Clear the render target float clearcol[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; mDeviceCtxt->ClearRenderTargetView(mRenderTargetView, clearcol); mDeviceCtxt->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH, 0.0f, 0); { ID3D11Buffer* ia_buffers[] = { mVertexBuffer }; UINT ia_strides[] = { sizeof(Vertex) }; UINT ia_offsets[] = { 0 }; mDeviceCtxt->IASetInputLayout(mInputLayout); mDeviceCtxt->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); mDeviceCtxt->IASetVertexBuffers(0, 1, ia_buffers, ia_strides, ia_offsets); mDeviceCtxt->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R16_UINT, 0); } mDeviceCtxt->VSSetShader(mVertexShader, nullptr, 0); mDeviceCtxt->VSSetConstantBuffers(0, 1, &mDrawConstantBuffer); mDeviceCtxt->RSSetViewports(1, &mViewPort); mDeviceCtxt->RSSetScissorRects(1, &mScissorRect); mDeviceCtxt->PSSetShader(mPixelShader, nullptr, 0); mDeviceCtxt->PSSetSamplers(0, 1, &mSamplerState); mDeviceCtxt->OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView); mDeviceCtxt->OMSetDepthStencilState(mDepthStencilState, 0); mDeviceCtxt->OMSetBlendState(mBlendState, nullptr, 0xFFFFFFFF); ProfileBeginRenderSubset(); auto viewProjection = camera.ViewProjection(); for (UINT drawIdx = 0; drawIdx < NUM_ASTEROIDS; ++drawIdx) { auto staticData = &staticAsteroidData[drawIdx]; auto dynamicData = &dynamicAsteroidData[drawIdx]; D3D11_MAPPED_SUBRESOURCE mapped = {}; ThrowIfFailed(mDeviceCtxt->Map(mDrawConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); auto drawConstants = (DrawConstantBuffer*) mapped.pData; XMStoreFloat4x4(&drawConstants->mWorld, dynamicData->world); XMStoreFloat4x4(&drawConstants->mViewProjection, viewProjection); drawConstants->mSurfaceColor = staticData->surfaceColor; drawConstants->mDeepColor = staticData->deepColor; mDeviceCtxt->Unmap(mDrawConstantBuffer, 0); mDeviceCtxt->PSSetShaderResources(0, 1, &mTextureSRVs[staticData->textureIndex]); mDeviceCtxt->DrawIndexedInstanced(dynamicData->indexCount, 1, dynamicData->indexStart, staticData->vertexStart, 0); } ProfileEndRenderSubset(); // Draw skybox { D3D11_MAPPED_SUBRESOURCE mapped = {}; ThrowIfFailed(mDeviceCtxt->Map(mSkyboxConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); auto skyboxConstants = (SkyboxConstantBuffer*) mapped.pData; XMStoreFloat4x4(&skyboxConstants->mViewProjection, camera.ViewProjection()); mDeviceCtxt->Unmap(mSkyboxConstantBuffer, 0); ID3D11Buffer* ia_buffers[] = { mSkyboxVertexBuffer }; UINT ia_strides[] = { sizeof(SkyboxVertex) }; UINT ia_offsets[] = { 0 }; mDeviceCtxt->IASetInputLayout(mSkyboxInputLayout); mDeviceCtxt->IASetVertexBuffers(0, 1, ia_buffers, ia_strides, ia_offsets); mDeviceCtxt->VSSetShader(mSkyboxVertexShader, nullptr, 0); mDeviceCtxt->VSSetConstantBuffers(0, 1, &mSkyboxConstantBuffer); mDeviceCtxt->PSSetShader(mSkyboxPixelShader, nullptr, 0); mDeviceCtxt->PSSetSamplers(0, 1, &mSamplerState); mDeviceCtxt->PSSetShaderResources(0, 1, &mSkyboxSRV); mDeviceCtxt->Draw(6*6, 0); } mDeviceCtxt->OMSetRenderTargets(1, &mRenderTargetView, 0); // No more depth buffer // Draw sprites and fonts { // Fill in vertices (TODO: could move this vector to be a member - not a big deal) std::vector<UINT> controlVertices; controlVertices.reserve(mGUI->size()); { D3D11_MAPPED_SUBRESOURCE mapped = {}; ThrowIfFailed(mDeviceCtxt->Map(mSpriteVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); auto vertexBase = (SpriteVertex*)mapped.pData; auto vertexEnd = vertexBase; for (size_t i = 0; i < mGUI->size(); ++i) { auto control = (*mGUI)[i]; controlVertices.push_back((UINT)(control->Draw(mViewPort.Width, mViewPort.Height, vertexEnd) - vertexEnd)); vertexEnd += controlVertices.back(); } mDeviceCtxt->Unmap(mSpriteVertexBuffer, 0); } ID3D11Buffer* ia_buffers[] = { mSpriteVertexBuffer }; UINT ia_strides[] = { sizeof(SpriteVertex) }; UINT ia_offsets[] = { 0 }; mDeviceCtxt->IASetInputLayout(mSpriteInputLayout); mDeviceCtxt->IASetVertexBuffers(0, 1, ia_buffers, ia_strides, ia_offsets); mDeviceCtxt->VSSetShader(mSpriteVertexShader, 0, 0); mDeviceCtxt->OMSetBlendState(mSpriteBlendState, nullptr, 0xFFFFFFFF); // Draw UINT vertexStart = 0; for (size_t i = 0; i < mGUI->size(); ++i) { auto control = (*mGUI)[i]; if (control->Visible()) { if (control->TextureFile().length() == 0) { // Font mDeviceCtxt->PSSetShader(mFontPixelShader, 0, 0); mDeviceCtxt->PSSetShaderResources(0, 1, &mFontTextureSRV); } else { // Sprite auto textureSRV = mSpriteTextures[control->TextureFile()]; mDeviceCtxt->PSSetShader(mSpritePixelShader, 0, 0); mDeviceCtxt->PSSetShaderResources(0, 1, &textureSRV); } mDeviceCtxt->Draw(controlVertices[i], vertexStart); } vertexStart += controlVertices[i]; } } ProfileEndRender(); ProfileBeginPresent(); mSwapChain->Present(settings.vsync ? 1 : 0, 0); ProfileEndPresent(); ProfileEndFrame(); }