无法在directx9中渲染方形
由于某种原因,无法渲染正方形。我找了一个小时,但找不到答案。 有人知道怎么画吗?我使用的是directx9。无法在directx9中渲染方形,directx,directx-9,square,Directx,Directx 9,Square,由于某种原因,无法渲染正方形。我找了一个小时,但找不到答案。 有人知道怎么画吗?我使用的是directx9。 尝试使用DrawindExperiive,但结果相同。这里可能发生了一些事情: 您没有设置顶点或像素着色器,因此使用的是传统的固定函数“渲染管道”。此管道要求您使用SetTransform设置视图/投影矩阵。因为您还没有这样做,所以您在“屏幕空间”中提供的顶点位置并不是您认为它们的意思。看 您没有设置viaSetRenderState,因此它默认为D3DCULL\u CCW(即剔除逆时
尝试使用DrawindExperiive,但结果相同。这里可能发生了一些事情:
SetTransform
设置视图/投影矩阵。因为您还没有这样做,所以您在“屏幕空间”中提供的顶点位置并不是您认为它们的意思。看
SetRenderState
,因此它默认为D3DCULL\u CCW
(即剔除逆时针缠绕的三角形)。因此,顶点位置会导致其中一个三角形被拒绝。您可能需要调用SetRenderState(D3DRS\u CULLMODE,D3DCULL\u NONE)代码>开始
TRIANGLESTRIP
,只有4个点。使用TRIANGELIST
和6个点,您可能会发现最初更容易获得正确答案
现在Direct3D 9已经有15年多的历史了,如果您是DirectX新手,那么Direct3D 11是一个更好的选择,并且有许多现代教程和实用程序。例如
// this is the function used to render a single frame
void render_frame(void)
{
init_graphics();
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
// select which vertex format we are using
d3ddev->SetFVF(CUSTOMFVF);
// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
// copy the vertex buffer to the back buffer
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
// d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX vertices[] =
{
{ 100.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 0, 255), },
{ 300.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 0, 255), },
{ 300.f, 80.f, 0.f, D3DCOLOR_XRGB(0, 0, 255), },
{ 100.f, 80.f, 0.f, D3DCOLOR_XRGB(0, 0, 255), },
};
// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(6 * sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
}