• 全部
  • 问答

一个关于DirectX 变换的问题(进者有分)

ChinaShrimp 个人 其他  2005-01-05 02:48:46
我下载了DirectX 9的SDK,安装完毕后在Microsoft DirectX 9.0 SDK (December 2004)\Samples\C++\Direct3D\Tutorials\Tut02_Vertices有一个如何绘制三角形的例子以及Tut03_Matrices如何设置变换矩阵。但是我看完第二个例子后,想直接在上面增加变换矩阵即
void SetMatrix()
{
HRESULT result;
D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation(&matWorld, 50, 50, 0);

result = g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
assert(result == D3D_OK);

D3DXMATRIXA16 matView;
D3DXVECTOR3 vEyePt(0, 0, 6);
D3DXVECTOR3 vLookatPt(0, 0, -5);
D3DXVECTOR3 vUpVec(0, 1, 0);
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec);

g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);

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

在绘制函数里面加上了对SetMatrix的调用
VOID Render()
{
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
SetMatrix();

g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2);

// End the scene
g_pd3dDevice->EndScene();
}

// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

困扰我的就是不管我怎么设置这些变换矩阵,最终的显示结果都一样好像就没有设置过这些变换一样,我想请问大虾我哪里做错了或者少添加了什么东西?
...全文
229 点赞 收藏 20
写回复
20 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ChinaShrimp 2005-09-03
多谢大家
回复
chijingde 2005-03-04
应该解决了吧?
回复
IO_X 2005-03-04
关于RHW顶点格式的帖子见过N遍了...-.-b
回复
bright_chn 2005-03-03
up~
回复
mars_snailbaby 2005-03-03
看一下
回复
tomwen 2005-03-03
偶 不是很懂
也来学学
回复
Snow_1980 2005-01-12
惭愧,我还不知道呢……
回复
IO_X 2005-01-11
晕,有rhw的顶点是不经过变换管道的
回复
Leon8086 2005-01-11
仔细看看Vertex Format那章,D3DFVF_XYZRHW这种格式属于“Lit & Transformed”的顶点,就是说在他上面不会再做光照和坐标系变换了。这种定点格式直接对应于RenderTarget上面的像素点。
回复
Kshape 2005-01-09
up
回复
JackZhou2017 2005-01-08
该成这个试一试:D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1
回复
xiaolizi 2005-01-08
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
这个格式比较合适
回复
lucky420 2005-01-08
D3DFVF_XYZRHW:Vertex format includes the position of a transformed vertex.它把你设的位置值直接作为转换后的坐标,所以你的转换矩阵不起作用的。
回复
寻开心 2005-01-07
支持,iwaiwai的说法, 这种格式根本就不受变换矩阵的影响
回复
iwaiwai 2005-01-07
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

D3DFVF_XYZRHW这个参数的意义查一查
回复
ChinaShrimp 2005-01-06
困扰我的就是不管我怎么设置这些变换矩阵,最终的显示结果都一样好像就没有设置过这些变换一样,我想请问大虾我哪里做错了或者少添加了什么东西?
回复
潘李亮 2005-01-05
你想问什么啊
回复
ChinaShrimp 2005-01-05
很着急,请大家多多帮忙!
回复
ChinaShrimp 2005-01-05
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pVB != NULL )
g_pVB->Release();

if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();

if( g_pD3D != NULL )
g_pD3D->Release();
}


void SetMatrix()
{
HRESULT result;
D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation(&matWorld, 50, 50, 0);

result = g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
assert(result == D3D_OK);

D3DXMATRIXA16 matView;
D3DXVECTOR3 vEyePt(0, 0, 6);
D3DXVECTOR3 vLookatPt(0, 0, -5);
D3DXVECTOR3 vUpVec(0, 1, 0);
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec);

g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);

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


//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
SetMatrix();
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2);

// End the scene
g_pd3dDevice->EndScene();
}

// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"test", NULL };
RegisterClassEx( &wc );

// Create the application's window
HWND hWnd = CreateWindow( "test", "transform",
WS_OVERLAPPEDWINDOW, 100, 100, 512, 512,
GetDesktopWindow(), NULL, wc.hInstance, NULL );

// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the vertex buffer
if( SUCCEEDED( InitVB() ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );

// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}
}

UnregisterClass( "test", wc.hInstance );
return 0;
}
回复
ChinaShrimp 2005-01-05
多谢了!!!
全部代码:
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include <strsafe.h>
#include <assert.h>

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex
DWORD color; // The vertex color
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)




//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;

// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}

// Device state would normally be set here

return S_OK;
}




//-----------------------------------------------------------------------------
// Name: InitVB()
// Desc: Creates a vertex buffer and fills it with our vertices. The vertex
// buffer is basically just a chuck of memory that holds vertices. After
// creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
// also uses index buffers. The special thing about vertex and index
// buffers is that they can be created in device memory, allowing some
// cards to process them in hardware, resulting in a dramatic
// performance gain.
//-----------------------------------------------------------------------------
HRESULT InitVB()
{
float fWidth = 2*128.0f;
// Initialize three vertices for rendering a triangle
CUSTOMVERTEX vertices[] =
{
{ -fWidth, -fWidth, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ fWidth, -fWidth, 0.5f, 1.0f, 0xff00ff00, },
{ fWidth, fWidth, 0.5f, 1.0f, 0xff00ffff, },
{ -fWidth, fWidth, 0.5f, 1.0f, 0xff00ffff, }
};

// Create the vertex buffer. Here we are allocating enough memory
// (from the default pool) to hold all our 3 custom vertices. We also
// specify the FVF, so the vertex buffer knows what data it contains.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}

// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, vertices, sizeof(vertices) );
g_pVB->Unlock();

return S_OK;
}
回复
相关推荐
发帖
游戏开发
创建于2007-08-27

7920

社区成员

游戏开发相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2005-01-05 02:48
社区公告
暂无公告