2D Sprite(背景)前面的3D对象,怎么样?

时间:2023-01-31 21:38:26

I'm new to Direct3D and I was on a project taking pictures from a webcam and draw some 3D objects in front of it.

我是Direct3D的新手,我参与了一个从网络摄像头拍摄照片并在其前面绘制一些3D物体的项目。

I was able to render webcam images as background using Orthogonal Projection.

我能够使用正交投影将网络摄像头图像渲染为背景。

//init matrix


     D3DXMatrixOrthoLH(&Ortho, frameWidth, frameHeight, 0.0f, 100.0f);

//some code

     D3DXVECTOR3 position = D3DXVECTOR3(0.0f, 0.0f, 100.0f);
     g_pSprite->Begin(D3DXSPRITE_OBJECTSPACE);
     g_pSprite->Draw(g_pTexture,NULL,&center,&position,0xFFFFFFFF);
     g_pSprite->End();

Then I tried to insert a simple triangle in front of it. The Matrices are setup as follow

然后我试着在它前面插入一个简单的三角形。矩阵设置如下

D3DXMATRIXA16 matWorld;


D3DXMatrixTranslation( &matWorld, 0.0f,0.0f,5.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );


D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

5.0 should be < 100.0 and the triangle is supposed to be appear in front of the images. However it does not appear unless set the z position to 0. At position 0, i can see the triangle but background is blank.

5.0应该<100.0并且三角形应该出现在图像的前面。但是,除非将z位置设置为0,否则它不会出现。在位置0,我可以看到三角形,但背景为空白。

Do you guys have any suggestions?

你们有什么建议吗?

2 个解决方案

#1


2  

I would not draw the webcam image in the object space (D3DXSPRITE_OBJECTSPACE) if you intend to use your image solely for background purpose; something like

如果您打算仅将图像用于背景目的,我不会在对象空间(D3DXSPRITE_OBJECTSPACE)中绘制网络摄像头图像;就像是

        D3DXVECTOR3 backPos (0.f, 0.f, 0.f);
        pBackgroundSprite->Begin(D3DXSPRITE_ALPHABLEND);
        pBackgroundSprite->Draw (pBackgroundTexture,
                           0, 
                           0,
                           &backPos,
                           0xFFFFFFFF);
        pBackgroundSprite->End();

should hopefully do what you're looking for.

应该有希望做你想要的。

#2


1  

As a quick fix you could disable depth testing as follows;

作为快速修复,您可以禁用深度测试,如下所示;

g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);

This way the z-index of the primitives being drawn should reflect the order in which they are drawn.

这样,绘制的图元的z索引应该反映它们的绘制顺序。

Also, try using the PIX debugging tool (this is bundled with the DirectX SDK). This is always my first port of call for drawing discrepancies as it allows you to debug each Draw call separately with access to the depth buffer and transformed vertices.

另外,尝试使用PIX调试工具(这与DirectX SDK捆绑在一起)。这始终是我绘制差异的第一个调用端口,因为它允许您通过访问深度缓冲区和变换顶点分别调试每个Draw调用。

#1


2  

I would not draw the webcam image in the object space (D3DXSPRITE_OBJECTSPACE) if you intend to use your image solely for background purpose; something like

如果您打算仅将图像用于背景目的,我不会在对象空间(D3DXSPRITE_OBJECTSPACE)中绘制网络摄像头图像;就像是

        D3DXVECTOR3 backPos (0.f, 0.f, 0.f);
        pBackgroundSprite->Begin(D3DXSPRITE_ALPHABLEND);
        pBackgroundSprite->Draw (pBackgroundTexture,
                           0, 
                           0,
                           &backPos,
                           0xFFFFFFFF);
        pBackgroundSprite->End();

should hopefully do what you're looking for.

应该有希望做你想要的。

#2


1  

As a quick fix you could disable depth testing as follows;

作为快速修复,您可以禁用深度测试,如下所示;

g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);

This way the z-index of the primitives being drawn should reflect the order in which they are drawn.

这样,绘制的图元的z索引应该反映它们的绘制顺序。

Also, try using the PIX debugging tool (this is bundled with the DirectX SDK). This is always my first port of call for drawing discrepancies as it allows you to debug each Draw call separately with access to the depth buffer and transformed vertices.

另外,尝试使用PIX调试工具(这与DirectX SDK捆绑在一起)。这始终是我绘制差异的第一个调用端口,因为它允许您通过访问深度缓冲区和变换顶点分别调试每个Draw调用。