bool DX9RenderSystem::Begin2d( ) { SetCullMode( CullMode::NONE ); SetFillMode( FillMode::SOLID ); SetProjectionMatrix( Matrix4::CreateOrthoLH( (float)mSize.x, -(float)mSize.y, 0.0f, 1.0f ) ); SetViewMatrix( Matrix4::IDENTITY ); SetWorldMatrix( Matrix4::IDENTITY ); SetVertexDeclaration( m2dDecl ); SetTextureStageState( 0, TextureState::TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE ); SetTextureStageState( 0, TextureState::COLOROP, BlendOp::MODULATE ); SetTextureStageState( 0, TextureState::COLORARG1, BlendArg::TEXTURE ); SetTextureStageState( 0, TextureState::COLORARG2, BlendArg::DIFFUSE ); SetTextureStageState( 0, TextureState::ALPHAOP, BlendOp::MODULATE ); SetTextureStageState( 0, TextureState::ALPHAARG1, BlendArg::TEXTURE ); SetTextureStageState( 0, TextureState::ALPHAARG2, BlendArg::DIFFUSE ); SetTextureStageState( 1, TextureState::COLOROP, BlendOp::DISABLE ); SetTextureStageState( 1, TextureState::ALPHAOP, BlendOp::DISABLE ); SetAlphaBlendEnabled( true ); SetAlphaTestEnabled( false ); SetDepthWriteEnabled( true ); SetDepthBufferEnabled( false ); SetSpecularEnabled( false ); return true; };
//============================================================================== // //------------------------------------------------------------------------------ void Sprite3D::draw(const Renderer* renderer) { auto pDevice = renderer->getDevice(); auto shader = renderer->getShader(); shader->setVtxShader(_vtxShaderID); auto vtxShader = shader->getNowVtxShader(); UINT nSamplerIndex = shader->getNowPixShader()->_constTable->GetSamplerIndex("TexSamp0"); if(UINT_MAX != nSamplerIndex) { pDevice->SetTexture(nSamplerIndex,renderer->getTexture()->getTexture(_textureID)); } // 色 vtxShader->_constTable->SetFloatArray(pDevice,"gMaterial",_color,4); Matrix norMtx; D3DXMatrixInverse(&norMtx,nullptr,&getWorldMtx()); D3DXMatrixTranspose(&norMtx,&norMtx); vtxShader->_constTable->SetMatrix(pDevice,"gNorWorld",&norMtx); Matrix wvp = getWorldMtx() * renderer->getCamera()->getViewMtx() * renderer->getProjMtx(); vtxShader->_constTable->SetMatrix(pDevice,"gWorld",&getWorldMtx()); vtxShader->_constTable->SetMatrix(pDevice,"gWVP",&wvp); // デクラレーション設定 pDevice->SetVertexDeclaration(_p3DDec); // 頂点送信 pDevice->SetStreamSource(0,_vtxBuff,0,sizeof(VERTEX_3D)); // 描画 pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2); }
void OvRenderer::RenderUnitRect( OvVertexShaderSPtr v_shader , OvPixelShaderSPtr p_shader ) { struct SScreenRect { OvPoint3 pos; OvPoint2 tex; }; static D3DVERTEXELEMENT9 rect_elem[] = { { 0, 0 , D3DDECLTYPE_FLOAT3 , D3DDECLMETHOD_DEFAULT , D3DDECLUSAGE_POSITION, 0 }, { 0, 12 , D3DDECLTYPE_FLOAT2 , D3DDECLMETHOD_DEFAULT , D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() }; static SScreenRect rect[] = { {OvPoint3(-1,-1,0),OvPoint2(0,1)} , {OvPoint3(-1,+1,0),OvPoint2(0,0)} , {OvPoint3(+1,+1,0),OvPoint2(1,0)} , {OvPoint3(+1,-1,0),OvPoint2(1,1)}}; static LPDIRECT3DVERTEXBUFFER9 rectVertBuffer = CreateVertexStream( (void*)&rect[0], sizeof( SScreenRect ), 4 ); static LPDIRECT3DVERTEXDECLARATION9 rectDecl = CreateVertexDeclaration( rect_elem ); if ( v_shader ) SetVertexShader( v_shader ); if ( p_shader ) SetPixelShader( p_shader ); SetVertexStream( 0, SVertexStreamInfo( rectVertBuffer, sizeof( SScreenRect ), 0) ); SetVertexDeclaration( rectDecl ); DrawPrimitive( D3DPT_TRIANGLEFAN, 2); }
void RenderSystem::DrawIndexedPrimitive(sVertexBuffer& vb, int offestVertex, int nVertex, sIndexBuffer& ib, int offsetPolygon, int nPolygon) { SetVertexDeclaration(vb); SetIndices(ib); SetStreamSource(vb); HRESULT hr = lpD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,offestVertex,nVertex,3*offsetPolygon,nPolygon); assert(hr== D3D_OK); }
void TerrainBorderRenderable::Render(HippoD3d9Device* pdevice, unsigned int escapeTime) { auto device = Globals::GetDevice()->GetDeviceD3D9(); RS_HELP_OBJ(device, D3DRS_ALPHABLENDENABLE, TRUE); RS_HELP_OBJ(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); RS_HELP_OBJ(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); //RS_HELP_OBJ(device, D3DRS_ZWRITEENABLE, false); RS_HELP_OBJ(device, D3DRS_CULLMODE, D3DCULL_CCW); D3DXMATRIX matrix; D3DXMatrixIdentity(&matrix); HRESULT v = m_fxhandle->SetMatrix("g_mWorld", &matrix); //wvp matrix auto proj = Globals::GetRender()->GetProjMatrix(); auto view = Globals::GetRender()->GetViewMatrix(); D3DXMATRIX vp = (*view)*(*proj); v = m_fxhandle->SetMatrix("g_mViewProjection", &vp); //time float time = GetTickCount()*0.001f; v = m_fxhandle->SetValue("g_fTime", &time, sizeof(float)); //viewmatrix const D3DXVECTOR3* camPos = Globals::GetCurrentCamera()->GetPos(); v = m_fxhandle->SetMatrix("ViewMatrix", view); v = m_fxhandle->SetValue("g_camerapos", camPos, sizeof(D3DXVECTOR3)); //texture v = m_fxhandle->SetTexture("g_MeshTexture", m_pTextrue.get()); //threshold D3DXVECTOR4 threshold = D3DXVECTOR4(800.f, 0.99f, 0, 0); v = m_fxhandle->SetValue("threshold", &threshold, sizeof(D3DXVECTOR4)); device->SetStreamSource(0, m_pVB.get(), 0, sizeof(TerrainVertex)); device->SetVertexDeclaration(m_pVertexDecl.get()); device->SetIndices(m_pIB.get()); UINT iPass, totalPasses; m_fxhandle->Begin(&totalPasses, 0); m_fxhandle->BeginPass(0); device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, // 将要绘制的索引缓冲区的起始地址 0, // 索引数组中最小的索引值 16, // 要绘制的索引数组中的顶点数量 0, // 从索引数组中的第几个元素开始绘制图元 8); // 绘制的图元数量 m_fxhandle->EndPass(); m_fxhandle->End(); }
//============================================================================= // update //============================================================================= void Application::Update(void) { auto graphic_device = system_->GetGraphicDevice(); float4x4 projection_matrix; float4x4 view_matrix; float4x4 world_matrix; view_matrix = utility::math::Identity(); projection_matrix = utility::math::Identity(); projection_matrix = utility::math::OrthoLH(800.0f,600.0f,0.0f,1.0f); //vertex_shader_->SetValue("_view_matrix",&view_matrix,sizeof(view_matrix)); //vertex_shader_->SetValue("_projection_matrix",&projection_matrix,sizeof(projection_matrix)); //vertex_shader_->SetValue("_world_matrix",&world_matrix,sizeof(world_matrix)); // begin draw if(graphic_device->BeginDraw()) { // set render target // set depth buffer // clear buffer graphic_device->Clear(); // set shader //shader_->Set(); // set vertex declaration DEBUG_ASSERT(graphic_device->SetVertexDeclaration(vertex_declaration_)); // set vertex buffer DEBUG_ASSERT(graphic_device->SetVertexBuffer(vertex_buffer_,0,sizeof(float3))); // draw //DEBUG_ASSERT(graphic_device->Draw(graphic::GraphicDevice::PRIMITIVE_TYPE::TRIANGLESTRIP,0,2)); graphic_device->Draw(); // end draw graphic_device->EndDraw(); } }
int main(){ auto pLogger = CB::Log::CLogger::GetInstance(); pLogger->AddStream(CB::IO::File::Open(L"main.log", CB::IO::File::AccessType::WriteOnly, CB::IO::File::OpenAction::AlwaysCreate).Cast<CB::IO::IStream>()); pLogger->AddStream(CB::IO::Console::Create().Cast<CB::IO::IStream>(), CB::Log::CTextFormatter::Create(CB::String::Encoding::ANSI).Cast<CB::Log::IEntryFormatter>()); pLogger->SetDebugMode(true); try{ auto pWinDriver = CB::Window::LoadDriver(L"MSWindowDriver"); auto pGraphicDriver = CB::Graphic::LoadDriver(L"OGLGraphicDriver"); { auto pWinManager = pWinDriver->CreateManager(); auto pGraphicManager = pGraphicDriver->CreateManager(pWinManager); CB::Math::CSize outSize(640, 480); auto pWindow = pWinManager->CreateWindow(L"GraphicTest", CB::Window::Style::Single, outSize); auto pGraphicAdapter = pGraphicManager->GetDefaultAdapter(); CB::Graphic::CDisplayMode dispMode(pWindow->GetSize(), 0, CB::Graphic::BufferFormat::B8G8R8X8); CB::Graphic::CDeviceDesc devDesc(pWindow, dispMode, CB::Graphic::BufferFormat::D24S8, false); CB::Collection::CList<CB::Graphic::FeatureLevel> featureLevels; featureLevels.Add(CB::Graphic::FeatureLevel::Level_1); auto pGraphicDevice = pGraphicAdapter->CreateDevice(pWindow, devDesc, featureLevels); pWindow->OnClose += CB::Signals::CFunc<const bool, CB::CRefPtr<CB::Window::IWindow>>(CloseEvent); pWindow->SetVisible(true); CB::Graphic::CDepthStencilStateDesc depthDesc; depthDesc.bDepthTestEnabled = true; depthDesc.uDepthFunction = CB::Graphic::CompareFunc::LessEqual; auto pDepthState = pGraphicDevice->CreateState(depthDesc); pGraphicDevice->SetState(pDepthState.Cast<CB::Graphic::IDeviceState>()); CB::Graphic::CRasterizerStateDesc rastDesc; rastDesc.uCullMode = CB::Graphic::CullMode::None; auto pRastState = pGraphicDevice->CreateState(rastDesc); pGraphicDevice->SetState(pRastState.Cast<CB::Graphic::IDeviceState>()); CB::Graphic::CBlendStateDesc blendDesc; blendDesc.ColorBlend.uDestOperand = CB::Graphic::BlendOption::OneMinusSourceAlpha; blendDesc.ColorBlend.uSourceOperand = CB::Graphic::BlendOption::SourceAlpha; blendDesc.ColorBlend.uOperation = CB::Graphic::BlendOperation::Add; blendDesc.AlphaBlend.uDestOperand = CB::Graphic::BlendOption::OneMinusSourceAlpha; blendDesc.AlphaBlend.uSourceOperand = CB::Graphic::BlendOption::SourceAlpha; blendDesc.AlphaBlend.uOperation = CB::Graphic::BlendOperation::Add; blendDesc.bEnabled[0] = true; auto pBlendState = pGraphicDevice->CreateState(blendDesc); pGraphicDevice->SetState(pBlendState.Cast<CB::Graphic::IDeviceState>()); auto pFontManager = CB::Font::CManager::Create(); auto pFontStream = CB::IO::File::Open(L"Assets/font.ttf").Cast<CB::IO::IStream>(); auto pFont = pFontManager->Load(pFontStream); pFont->SelectFace(0); pFont->SetSize(24); CB::Collection::CList<CB::Tools::CFontCharDesc> charDescs; CB::Tools::CFontTextureGenerator fontGen(pGraphicDevice); fontGen.MaxTextureSize.Set(512, 512); auto pTexture = fontGen.Generate(pFont, charDescs); CB::Tools::CTextMeshGenerator textGen(charDescs); CB::Tools::CMeshRawIVT textMesh; textGen.Generate(L"Marek M³ynarski!", textMesh); CB::Collection::CList<CB::Graphic::CVertexElement> vEls; vEls.Add(CB::Graphic::CVertexElement(0, L"vinput.vPosition", CB::Graphic::VertexType::Float, 3, 0)); vEls.Add(CB::Graphic::CVertexElement(1, L"vinput.vTexCoord", CB::Graphic::VertexType::Float, 2, 0)); GraphicTest::CShaderLoader shaders(pGraphicDevice, L"Shaders/TextureShader.cg"); auto pTextDecl = pGraphicDevice->CreateVertexDeclaration(shaders.pVertexShader, vEls); auto pTextVertexBuffer = pGraphicDevice->CreateBuffer(CB::Graphic::BufferType::Vertex, CB::Graphic::BufferUsage::Dynamic, CB::Graphic::BufferAccess::Write, textMesh.Vertices); auto pTextTCoordBuffer = pGraphicDevice->CreateBuffer(CB::Graphic::BufferType::Vertex, CB::Graphic::BufferUsage::Dynamic, CB::Graphic::BufferAccess::Write, textMesh.TexCoords); auto pTextIndexBuffer = pGraphicDevice->CreateBuffer(CB::Graphic::BufferType::Index, CB::Graphic::BufferUsage::Dynamic, CB::Graphic::BufferAccess::Write, textMesh.Indices); float32 fAspect = (float32)outSize.Width / (float32)outSize.Height; CB::Math::CMatrix mProj = CB::Math::CMatrix::GetPerspective(fAspect, 60.0f, 1.0f, 100.0f); CB::Math::CMatrix mView = CB::Math::CMatrix::GetTranslation(-4.0f, 0.0f, -3.4f); CB::Math::CMatrix mModel = CB::Math::CMatrix::GetIdentity(); shaders.pFragmentShader->SetSampler(L"texDiffuse", pTexture.Cast<CB::Graphic::IBaseTexture>()); pTexture->SetFilters(CB::Graphic::TextureFilter::Linear, CB::Graphic::TextureFilter::Linear, CB::Graphic::TextureFilter::Linear); pTexture->SetAnisotropy(8); //g_pTexture = texture.pTexture; while(g_bRun){ pGraphicDevice->Clear(1.0f, 1); pGraphicDevice->Clear(CB::Math::CColor(1.0f, 0.5f, 0.0f, 1.0f)); pGraphicDevice->BeginRender(); pGraphicDevice->SetShader(shaders.pVertexShader); pGraphicDevice->SetShader(shaders.pFragmentShader); static float32 fV = 0.0f; fV += 20 * g_Timer.GetTimeDelta(); mModel = CB::Math::CMatrix::GetRotation(CB::Math::AxisOrientation::AxisX, fV); shaders.pVertexShader->SetUniform(L"vinput.mProj", mProj); shaders.pVertexShader->SetUniform(L"vinput.mView", mView); shaders.pVertexShader->SetUniform(L"vinput.mModel", mModel); pGraphicDevice->SetVertexDeclaration(pTextDecl); pGraphicDevice->SetVertexBuffer(0, pTextVertexBuffer); pGraphicDevice->SetVertexBuffer(1, pTextTCoordBuffer); pGraphicDevice->SetIndexBuffer(pTextIndexBuffer); pGraphicDevice->RenderIndexed(textMesh.uNumberOfPolygons); pGraphicDevice->EndRender(); g_Timer.Update(); pWinManager->ProcessEvents(); float fFPS = 1.0f / (g_Timer.GetTimeDelta() == 0.0f ? 1.0f : g_Timer.GetTimeDelta()); uint32 uFPS = (uint32)fFPS; textMesh.Clear(); textGen.Generate(L"FPS: " + CB::String::ToString(uFPS), textMesh); pTextVertexBuffer->LoadData(textMesh.Vertices); pTextTCoordBuffer->LoadData(textMesh.TexCoords); pTextIndexBuffer->LoadData(textMesh.Indices); pGraphicDevice->Swap(); } g_pTexture.Release(); } } catch(CB::Exception::CException& Exception){ CB::Log::Write(Exception, CB::Log::LogLevel::Fatal); CB::Message::Show(Exception, CB::Message::Icon::Error); } return 0; }
void RenderSystem::SetVertexDeclaration(sVertexBuffer& vb) { SetVertexDeclaration(vb.declaration); }
void TerrainRenderablePlane::Render(HippoD3d9Device* pdevice, unsigned int escapeTime) { auto d3d9device = pdevice->GetDeviceD3D9(); //d3d9device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); //world matrix D3DXMATRIX tmpMatrix; D3DXMatrixMultiply(&tmpMatrix, &m_local_matrix, m_parent->GetWorldTransform()); HRESULT v = m_fxhandle->SetMatrix("g_mWorld", &tmpMatrix); //wvp matrix auto proj = Globals::GetRender()->GetProjMatrix(); auto view = Globals::GetRender()->GetViewMatrix(); D3DXMATRIX vp = (*view)*(*proj); v = m_fxhandle->SetMatrix("g_mViewProjection", &vp); //camera const D3DXVECTOR3* pos = Globals::GetCurrentCamera()->GetPos(); v = m_fxhandle->SetValue("g_camera_pos", pos, sizeof(D3DXVECTOR3)); //material D3DXCOLOR vWhite = D3DXCOLOR(0.5, 0.3, 0.2, 1); v = m_fxhandle->SetValue("g_MaterialDiffuseColor", &vWhite, sizeof(D3DXCOLOR)); //texture v = m_fxhandle->SetTexture("g_MeshTexture", m_pTextrue[0]); //tower info auto alltowers = Globals::GetWorld()->GetAllTower(); auto itr = alltowers.begin(); D3DXVECTOR4 tmp[2]; int idx = 0; while (itr != alltowers.end()) { TowerEntity* pEntity = (TowerEntity*)((*itr).get()); D3DXVECTOR3* pos = pEntity->GetPos(); float r = pEntity->GetRange(); tmp[idx] = D3DXVECTOR4(pos->x, pos->y, pos->z, r); ++itr; ++idx; } v = m_fxhandle->SetVectorArray("towerInfo", tmp, 2); //robot info auto player = Globals::GetWorld()->GetPlayer(); pos = player->GetPos(); D3DXVECTOR4 robotpos = D3DXVECTOR4(pos->x, pos->z, 12.f, 1.0f); v = m_fxhandle->SetVector("robotInfo", &robotpos); d3d9device->SetStreamSource(0, m_pVB, 0, sizeof(TerrainVertex)); d3d9device->SetVertexDeclaration(m_pVertexDecl); d3d9device->SetIndices(m_pIB); UINT iPass, totalPasses; m_fxhandle->Begin(&totalPasses, 0); for (iPass = 0; iPass < totalPasses; iPass++) { m_fxhandle->BeginPass(iPass); d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, // 将要绘制的索引缓冲区的起始地址 0, // 索引数组中最小的索引值 GetVertexNum(), // 要绘制的索引数组中的顶点数量 0, // 从索引数组中的第几个元素开始绘制图元 GetTriangleNum()); // 绘制的图元数量 m_fxhandle->EndPass(); } m_fxhandle->End(); m_border_renderable->Render(pdevice, escapeTime); }
void TerrainRenderable::Render(HippoD3d9Device* pdevice, unsigned int escapeTime) { auto d3d9device = pdevice->GetDeviceD3D9(); //*d3d9device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);*/ //world matrix D3DXMATRIX tmpMatrix; D3DXMatrixMultiply(&tmpMatrix, &m_local_matrix, m_parent->GetWorldTransform()); HRESULT v = m_fxhandle->SetMatrix("g_mWorld", &tmpMatrix); //wvp matrix auto proj = Globals::GetRender()->GetProjMatrix(); auto view = Globals::GetRender()->GetViewMatrix(); D3DXMATRIX vp = (*view)*(*proj); v = m_fxhandle->SetMatrix("g_mViewProjection", &vp); //camera const D3DXVECTOR3* pos=Globals::GetCurrentCamera()->GetPos(); v = m_fxhandle->SetValue("g_camera_pos", pos, sizeof(D3DXVECTOR3)); //texture v = m_fxhandle->SetTexture("g_DiffuseTex", m_pTextrue[0]); v = m_fxhandle->SetTexture("g_DetailTex", m_pTextrue[1]); v = m_fxhandle->SetTexture("g_NormalTex", m_pTextrue[2]); //tower info const GameEntityPtrCon& con = Globals::GetWorld()->GetAllTower(); GameEntityPtrCon::const_iterator itr = con.begin(); GameEntityPtrCon::const_iterator itrend = con.end(); D3DXVECTOR4 tmp[2]; int idx = 0; while (itr != itrend) { auto towerptr = (TowerEntity*)itr->get(); D3DXVECTOR3* pos = towerptr->GetPos(); float r = towerptr->GetRange(); tmp[idx] = D3DXVECTOR4(pos->x, pos->y, pos->z, r); ++itr; ++idx; } v=m_fxhandle->SetVectorArray("towerInfo", tmp, 2); //robot info auto robot=Globals::GetWorld()->GetPlayer(); D3DXVECTOR4 robotpos = D3DXVECTOR4(robot->GetPos()->x, robot->GetPos()->z, 2, 1); v=m_fxhandle->SetVector("robotInfo", &robotpos); d3d9device->SetStreamSource(0, m_pVB, 0, sizeof(TerrainVertex)); d3d9device->SetVertexDeclaration(m_pVertexDecl); d3d9device->SetIndices(m_pIB); UINT iPass, totalPasses; m_fxhandle->Begin(&totalPasses, 0); for (iPass = 0; iPass < totalPasses; iPass++) { m_fxhandle->BeginPass(iPass); //d3d9device->DrawPrimitive(D3DPT_POINTLIST, // 0, // 将要绘制的索引缓冲区的起始地址 // GetVertexNum()); // 绘制的图元数量 d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, // 将要绘制的索引缓冲区的起始地址 0, // 索引数组中最小的索引值 GetVertexNum(), // 要绘制的索引数组中的顶点数量 0, // 从索引数组中的第几个元素开始绘制图元 GetTriangleNum()); // 绘制的图元数量 m_fxhandle->EndPass(); } m_fxhandle->End(); return; //patch auto pthitr = m_all_patch.begin(); while (pthitr != m_all_patch.end()) { auto patch_fx = (*pthitr)->GetPatchFx(); auto patch_ib = (*pthitr)->GetIB(); int tri_count = (*pthitr)->GetIndexCount() / 3; d3d9device->SetStreamSource(0, m_pVB, 0, sizeof(TerrainVertex)); d3d9device->SetVertexDeclaration(m_pVertexDecl); HRESULT v = d3d9device->SetIndices(patch_ib); v=patch_fx->SetMatrix("g_mWorld", &tmpMatrix); v = patch_fx->SetMatrix("g_mViewProjection", &vp); v = patch_fx->SetValue("g_camera_pos", pos, sizeof(D3DXVECTOR3)); UINT totalPasses; v=patch_fx->Begin(&totalPasses, 0); v = patch_fx->BeginPass(0); v = d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, // 将要绘制的索引缓冲区的起始地址 0, // 索引数组中最小的索引值 GetVertexNum(), // 要绘制的索引数组中的顶点数量 0, // 从索引数组中的第几个元素开始绘制图元 tri_count); // 绘制的图元数量 v = patch_fx->EndPass(); ++pthitr; } }