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); }
HRESULT SceneObjectGrid::InitializeMeshStreams(__in ISceneObjectMesh* pMesh, const XMVECTOR xAxis, const XMVECTOR yAxis, const XMFLOAT3& origin, UINT xResolution, UINT yResolution, XMFLOAT4 color) { if (xResolution == 0 || yResolution == 0 || pMesh == NULL) { return E_INVALIDARG; } HRESULT hr = S_OK; xResolution = max(1, xResolution); yResolution = max(1, yResolution); // Build grid geometry INT iLineCount = xResolution + yResolution + 2; VertexType* pLines = new VertexType[2 * iLineCount]; if (!pLines) { hr = E_OUTOFMEMORY; } else { XMVECTOR vOrigin = XMLoadFloat3(&origin); for (UINT i = 0; i <= xResolution; i++) { FLOAT fPercent = (FLOAT)i / (FLOAT)xResolution; fPercent = (fPercent * 2.0f) - 1.0f; XMVECTOR vScale = XMVectorScale(xAxis, fPercent); vScale = XMVectorAdd( vScale, vOrigin ); XMStoreFloat3(&pLines[(i * 2)].position, XMVectorSubtract(vScale, yAxis)); pLines[(i * 2)].color = color; XMStoreFloat3(&pLines[(i * 2) + 1].position, XMVectorAdd(vScale, yAxis)); pLines[(i * 2) + 1].color = color; } UINT iStartIndex = (xResolution + 1) * 2; for (UINT i = 0; i <= yResolution; i++) { FLOAT fPercent = (FLOAT)i / (FLOAT)yResolution; fPercent = (fPercent * 2.0f) - 1.0f; XMVECTOR vScale = XMVectorScale(yAxis, fPercent); vScale = XMVectorAdd(vScale, vOrigin); XMStoreFloat3(&pLines[(i * 2) + iStartIndex].position, XMVectorSubtract(vScale, xAxis)); pLines[(i * 2) + iStartIndex].color = color; XMStoreFloat3(&pLines[(i * 2) + 1 + iStartIndex].position, XMVectorAdd(vScale, xAxis)); pLines[(i * 2) + 1 + iStartIndex].color = color; } SmartPtr<IVertexStream> spVertexStream; hr = CreateVertexStream(2 * iLineCount, pLines, &spVertexStream); if (SUCCEEDED(hr)) { hr = pMesh->SetMeshStream(MeshStreamType::MST_VERTEX_POSITIONS, spVertexStream.CastTo<IMeshStream>()); } pMesh->SetPrimitiveTopologyType(PrimitiveTopologyType::PTT_LINELIST); } delete[] pLines; return hr; }