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

发布于 2024-10-29 04:46:21 字数 915 浏览 1 评论 0原文

我是 Direct3D 新手,当时我正在参与一个从网络摄像头拍照并在其前面绘制一些 3D 对象的项目。

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

//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();

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

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

你们有什么建议吗?

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.

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,¢er,&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.

Do you guys have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-11-05 04:46:21

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

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

应该有望满足您的要求。

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

        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.

生来就爱笑 2024-11-05 04:46:21

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

g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文