none
DX11 3D 绘制矩形看不到效果,请大家看看问题出在哪? RRS feed

  • 问题

  • 目标:绘制两个三角形,组成一个矩形对象

    说明:shader使用的是默认生成的

    代码:

    COLOR4F clr = m_SytleInterface->GetBackGroundDefaultClr();
    		VPOINTF3 ptDes[4];
    		VFloat iWidth = 256.0;
    		VFloat iHeight = 256.0;
    		VFloat fLX = -466.0;
    		VFloat fBY = -819.0;
    
    		g_constantBufferData.world = 
    XMMatrixTranslation(-116.5, -204.75, 0) * CVBGL::m_world;
    		g_constantBufferData.view = CVBGL::m_view;
    		g_constantBufferData.projection = (XMMATRIX)CVBGL::m_projection;
    
    //矩阵设置
    D3D11_BUFFER_DESC constantBufferDesc = {0};
    constantBufferDesc.ByteWidth = sizeof(g_constantBufferData);
            constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
            constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
            constantBufferDesc.CPUAccessFlags = 0;
            constantBufferDesc.MiscFlags = 0;
            //constantBufferDesc.StructureByteStride = 0;
            DX::ThrowIfFailed(
                m_d3dDevice->CreateBuffer(
                    &constantBufferDesc,
                    nullptr,
                    &m_constantBuffer
                    )
                );
    		m_d3dContext->UpdateSubresource(
    				m_constantBuffer.Get(),
    				0,
    				nullptr,
    				&g_constantBufferData,
    				0,
    				0
                );
    
            ptDes[0].x = 0.0f;
            ptDes[0].y = 0.0f;
            ptDes[0].z = 0.0f;
            ptDes[1].x = 0.0f;
            ptDes[1].y = 256;
            ptDes[1].z = 0.0f;
            ptDes[2].x = 256;
            ptDes[2].y = 256;
            ptDes[2].z = 0.0f;
            ptDes[3].x = 256;
            ptDes[3].y = 0.0f;
            ptDes[3].z = 0.0f;
    
    
    		ByteArray loadVSTask = ReadData("SimpleVertexShader.cso");
    		ByteArray loadPSTask = ReadData("SimplePixelShader.cso");
    
    		DX::ThrowIfFailed(
    			m_d3dDevice->CreateVertexShader(
    				bytecodeVS->Data,
    				bytecodeVS->Length,
    				nullptr,
    				&vertexShader
    				)
    			);
    
    		const D3D11_INPUT_ELEMENT_DESC basicVertexLayoutDesc[] =
    		{
    			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    			{ "COLOR",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    		};
    
    		ComPtr<ID3D11InputLayout> inputLayout;
    		DX::ThrowIfFailed(
    			m_d3dDevice->CreateInputLayout(
    							  basicVertexLayoutDesc,
                                  ARRAYSIZE(basicVertexLayoutDesc),
    							  bytecodeVS->Data, 
    							  bytecodeVS->Length,
    							  &inputLayout)
    							);
    
    
    		m_d3dContext->IASetInputLayout(inputLayout.Get());
    
    		ComPtr<ID3D11PixelShader> pixelShader;
    		DX::ThrowIfFailed(
    			m_d3dDevice->CreatePixelShader(
    				bytecodePS->Data,
    				bytecodePS->Length,
    				nullptr,
    				&pixelShader
    				)
    			);
    
    		VertexPositionColor  Vertices[4] =
            {
    			{ XMFLOAT3(ptDes[0].x, ptDes[0].y, ptDes[0].z), XMFLOAT4( clr.fRed, clr.fGreen, clr.fBlue, clr.fAlpha) }, // +Y (top face)
                { XMFLOAT3(ptDes[1].x, ptDes[1].y, ptDes[1].z), XMFLOAT4( clr.fRed, clr.fGreen, clr.fBlue, clr.fAlpha) },
                { XMFLOAT3(ptDes[2].x, ptDes[2].y, ptDes[2].z), XMFLOAT4( clr.fRed, clr.fGreen, clr.fBlue, clr.fAlpha) },
                { XMFLOAT3(ptDes[3].x, ptDes[3].y, ptDes[3].z), XMFLOAT4( clr.fRed, clr.fGreen, clr.fBlue, clr.fAlpha) },
            };
    
    		D3D11_BUFFER_DESC vertexBufferDesc = {0};
    		vertexBufferDesc.ByteWidth = sizeof(VertexPositionColor) * ARRAYSIZE(Vertices);
    		vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    		vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    		vertexBufferDesc.CPUAccessFlags = 0;
    		vertexBufferDesc.MiscFlags = 0;
    		//vertexBufferDesc.StructureByteStride = 0;
    
    
    		D3D11_SUBRESOURCE_DATA vertexBufferData;
    		vertexBufferData.pSysMem =Vertices;
    		vertexBufferData.SysMemPitch = 0;
    		vertexBufferData.SysMemSlicePitch = 0;
    
    		ComPtr<ID3D11Buffer> vertexBuffer;
    		DX::ThrowIfFailed(
    			m_d3dDevice->CreateBuffer(
    				&vertexBufferDesc,
    				&vertexBufferData,
    				&vertexBuffer
    				)
    			);
    		
    		UINT stride = sizeof(VertexPositionColor);
    		UINT offset = 0;
    		m_d3dContext->IASetVertexBuffers(
    			0,
    			1,
    			vertexBuffer.GetAddressOf(),
    			&stride,
    			&offset
    			);
    
    
    		// Specify the render target and depth stencil we created as the output target.
                m_d3dContext->OMSetRenderTargets(
                    1,
                    m_renderTargetView.GetAddressOf(),
                    m_depthStencilView.Get()
                    );
    
                // Clear the render target to a solid color, and reset the depth stencil.
                const float clearColor[4] = { 0.071f, 0.04f, 0.561f, 1.0f };
                m_d3dContext->ClearRenderTargetView(
                    m_renderTargetView.Get(),
                    clearColor
                    );
    
    	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
    
    		m_d3dContext->VSSetShader(
    			vertexShader.Get(),
    			nullptr,
    			0
    			);
    
    		m_d3dContext->PSSetShader(
    			pixelShader.Get(),
    			nullptr,
    			0
    			);
    
    		m_d3dContext->Draw(4,0);
    
    		DX::ThrowIfFailed(
    			m_swapChain->Present(1, 0)
    			);
    

    2012年9月18日 7:35

答案

  • D2D的BeginDraw  和EndDrwa() 只是为了积聚元素一次性提交到pipeline,提高效率。D3D11现在只能自己实现了,不过灵活性也大大增加了。

    Win8 Developer QQ Group 95331609

    • 已标记为答案 1096 2012年9月25日 2:14
    2012年9月24日 12:04

全部回复