[求助]我怎么解决这个问题??
来源:百度文库 编辑:超级军网 时间:2024/04/19 08:40:11
我用Dx9 想画2个正方形 一个红色,一个蓝色,使用VertexBuffer 和2个IndexBuffer
我用DX9.0c ,编译器是VS 2005,代码能通过编译。奇怪的是这2个方形不能同时画出来,但是分开的话就只能画一个出来,
有哪个帮我看看是怎么回事?
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
IDirect3D9* g_pD3D = NULL;
IDirect3DDevice9* g_pD3DD = NULL;
IDirect3DVertexBuffer9* g_pD3DVB = NULL;
IDirect3DIndexBuffer9* g_pD3DIB1 = NULL;
IDirect3DIndexBuffer9* g_pD3DIB2 = NULL;
D3DXMATRIXA16 matWorld;
struct Vertex
{
// Vertex(float x,float y,float z,DWORD color) {x=x;y=y;z=z;color=color;}
float x,y,z;
DWORD color;
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define RED D3DCOLOR_ARGB(0,255,0,0)
#define BLUE D3DCOLOR_ARGB(0,0,0,255)
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_pD3DD ) ) )
{
return E_FAIL;
}
// Turn off culling, so we see the front and back of the triangle
// Turn off D3D lighting, since we are providing our own vertex colors
g_pD3DD->SetRenderState( D3DRS_LIGHTING,FALSE );
return S_OK;
}
HRESULT InitGeometry()
{
// Initialize three vertices for rendering a triangle
// Create the vertex buffer.
if( FAILED( g_pD3DD->CreateVertexBuffer( 16*sizeof(Vertex),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pD3DVB, NULL ) ) )
{
return E_FAIL;
}
/*
//1st Cube Vertices
pVertices[0]={-1.0f,-1.0f,-2.0f,RED,};
pVertices[1]={-1.0f,1.0f,-2.0f,RED,};
pVertices[2]={ 1.0f, 1.0f,-2.0f,RED,};
pVertices[3]={1.0f,-1.0f,-2.0f,RED,};
pVertices[4]={-1.0f,-1.0f, 2.0f,RED,};
pVertices[5]={-1.0f,1.0f, 2.0f,RED,};
pVertices[6]={ 1.0f, 1.0f, 2.0f,RED,};
pVertices[7]={1.0f,-1.0f, 2.0f,RED,};
//2nd Cube Vertices
pVertices[8]={-1.0f,-1.0f,-1.0f,BLUE,};
pVertices[9]={-1.0f,1.0f,-1.0f,BLUE,};
pVertices[10]={ 1.0f,1.0f,-1.0f,BLUE,};
pVertices[11]={1.0f,-1.0f,-1.0f,BLUE,};
pVertices[12]={-1.0f,-1.0f, 1.0f,BLUE,};
pVertices[13]={-1.0f,1.0f, 1.0f,BLUE,};
pVertices[14]={ 1.0f,1.0f, 1.0f,BLUE,};
pVertices[15]={1.0f,-1.0f, 1.0f,BLUE,};
*/
Vertex g_Vertices[] ={
//1st Cube Vertices
{-1.0f,-1.0f,-2.0f,RED,},
{-1.0f, 1.0f,-2.0f,RED,},
{ 1.0f, 1.0f,-2.0f,RED,},
{ 1.0f,-1.0f,-2.0f,RED,},
{-1.0f,-1.0f, 2.0f,RED,},
{-1.0f, 1.0f, 2.0f,RED,},
{ 1.0f, 1.0f, 2.0f,RED,},
{ 1.0f,-1.0f, 2.0f,RED,},
//2nd Cube Vertices
{-1.0f,-1.0f,-1.0f,BLUE,},
{-1.0f, 1.0f,-1.0f,BLUE,},
{ 1.0f, 1.0f,-1.0f,BLUE,},
{ 1.0f,-1.0f,-1.0f,BLUE,},
{-1.0f,-1.0f, 1.0f,BLUE,},
{-1.0f, 1.0f, 1.0f,BLUE,},
{ 1.0f, 1.0f, 1.0f,BLUE,},
{ 1.0f,-1.0f, 1.0f,BLUE,},
};
// Fill the vertex buffer.
VOID* pVertices;
// Vertex* pVertices;
if( FAILED( g_pD3DVB->Lock( 0, 16*sizeof(Vertex) , (void**)&pVertices, 0 ) ) )
{
return E_FAIL;
}
/*
//1st Cube Vertices
pVertices[0]=Vertex(-1.0f,-1.0f,-2.0f,RED);
pVertices[1]=Vertex(-1.0f, 1.0f,-2.0f,RED);
pVertices[2]=Vertex( 1.0f, 1.0f,-2.0f,RED);
pVertices[3]=Vertex( 1.0f,-1.0f,-2.0f,RED);
pVertices[4]=Vertex(-1.0f,-1.0f, 2.0f,RED);
pVertices[5]=Vertex(-1.0f, 1.0f, 2.0f,RED);
pVertices[6]=Vertex( 1.0f, 1.0f, 2.0f,RED);
pVertices[7]=Vertex( 1.0f,-1.0f, 2.0f,RED);
/* //2nd Cube Vertices
pVertices[8]={-1.0f,-1.0f,-1.0f,BLUE,};
pVertices[9]={-1.0f, 1.0f,-1.0f,BLUE,};
pVertices[10]={ 1.0f, 1.0f,-1.0f,BLUE,};
pVertices[11]={ 1.0f,-1.0f,-1.0f,BLUE,};
pVertices[12]={-1.0f,-1.0f, 1.0f,BLUE,};
pVertices[13]={-1.0f, 1.0f, 1.0f,BLUE,};
pVertices[14]={ 1.0f, 1.0f, 1.0f,BLUE,};
pVertices[15]={ 1.0f,-1.0f, 1.0f,BLUE,};*/
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pD3DVB->Unlock();
// Create the Index buffer.
if( FAILED( g_pD3DD->CreateIndexBuffer( 36*sizeof(WORD),
0, D3DFMT_INDEX16 ,
D3DPOOL_DEFAULT, &g_pD3DIB1, NULL ) ) )
{
return E_FAIL;
}
WORD* pIndices = NULL;
if( FAILED( g_pD3DIB1->Lock( 0, 0, (void**)&pIndices, 0 ) ) )
return E_FAIL;
//bottom of 1st cube
pIndices[0]=0; pIndices[1]=1; pIndices[2]=2;
pIndices[3]=0; pIndices[4]=2; pIndices[5]=3;
//front of 1st cube
pIndices[6]=0; pIndices[7]=4; pIndices[8]=7;
pIndices[9]=0; pIndices[10]=7; pIndices[11]=3;
//back of 1st cube
pIndices[12]=1; pIndices[13]=5; pIndices[14]=6;
pIndices[15]=1; pIndices[16]=6; pIndices[17]=2;
//right side of 1st cube
pIndices[18]=3; pIndices[19]=7; pIndices[20]=6;
pIndices[21]=3; pIndices[22]=6; pIndices[23]=2;
//left side of 1st cube
pIndices[24]=0; pIndices[25]=4; pIndices[26]=5;
pIndices[27]=0; pIndices[28]=5; pIndices[29]=1;
//top of 1st cube
pIndices[30]=4; pIndices[31]=5; pIndices[32]=6;
pIndices[33]=4; pIndices[34]=6; pIndices[35]=7;
/*
//bottom of 2nd cube
pIndices[36]=8; pIndices[37]=9; pIndices[38]=10;
pIndices[39]=8; pIndices[40]=10; pIndices[41]=11;
//front of 2nd cube
pIndices[42]=8; pIndices[43]=12; pIndices[44]=15;
pIndices[45]=8; pIndices[46]=15; pIndices[47]=11;
//back of 2nd cube
pIndices[48]=9; pIndices[49]=13; pIndices[50]=14;
pIndices[51]=9; pIndices[52]=14; pIndices[53]=10;
//right side of 2nd cube
pIndices[54]=11; pIndices[55]=15; pIndices[56]=14;
pIndices[57]=11; pIndices[58]=14; pIndices[59]=10;
//left side of 2nd cube
pIndices[60]=8; pIndices[61]=12; pIndices[62]=13;
pIndices[63]=8; pIndices[64]=13; pIndices[65]=9;
//top of 2nd cube
pIndices[66]=12; pIndices[67]=13; pIndices[68]=14;
pIndices[69]=12; pIndices[70]=14; pIndices[71]=15;*/
g_pD3DIB1->Unlock();
if( FAILED( g_pD3DD->CreateIndexBuffer( 36*sizeof(WORD),
0, D3DFMT_INDEX16 ,
D3DPOOL_DEFAULT, &g_pD3DIB2, NULL ) ) )
{
return E_FAIL;
}
WORD* ppIndices = NULL;;
if( FAILED( g_pD3DIB2->Lock( 0, 0, (void**)&ppIndices, 0 ) ) )
return E_FAIL;
//bottom of 1st cube
ppIndices[0]=8; ppIndices[1]=9; ppIndices[2]=10;
ppIndices[3]=8; ppIndices[4]=10; ppIndices[5]=11;
//front of 1st cube
ppIndices[6]=8; ppIndices[7]=12; ppIndices[8]=15;
ppIndices[9]=8; ppIndices[10]=15; ppIndices[11]=11;
//back of 1st cube
ppIndices[12]=9; ppIndices[13]=13; ppIndices[14]=14;
ppIndices[15]=9; ppIndices[16]=14; ppIndices[17]=10;
//right side of 1st cube
ppIndices[18]=11; ppIndices[19]=15; ppIndices[20]=14;
ppIndices[21]=11; ppIndices[22]=14; ppIndices[23]=10;
//left side of 1st cube
ppIndices[24]=8; ppIndices[25]=12; ppIndices[26]=13;
ppIndices[27]=8; ppIndices[28]=13; ppIndices[29]=9;
//top of 1st cube
ppIndices[30]=12; ppIndices[31]=13; ppIndices[32]=14;
ppIndices[33]=12; ppIndices[34]=14; ppIndices[35]=15;
g_pD3DIB2->Unlock();
return S_OK;
}
VOID SetupMatrices()
{
// D3DXMatrixIdentity(&matWorld);
// D3DXMatrixTranslation(&matWorld, 1.0f,1.0f,0.0f);
// g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
// UINT iTime = timeGetTime() % 1000;
// FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
// D3DXMatrixRotationX( &matWorld, fAngle );
// D3DXMatrixRotationY( &matWorld, fAngle );
// D3DXMatrixRotationZ( &matWorld, fAngle );
//Setup Camara
D3DXVECTOR3 vEyePt(4.0f, 4.0f, 6.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f,0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 0.0f, 1.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pD3DD->SetTransform( D3DTS_VIEW, &matView );
//Setup projection
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI*2/3, 1.0f, 1.0f, 100.0f );
g_pD3DD->SetTransform( D3DTS_PROJECTION, &matProj );
}
VOID Render()
{
// Clear the backbuffer to a black color
g_pD3DD->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(100,100,100), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pD3DD->BeginScene() ) )
{
// Setup the world, view, and projection matrices
SetupMatrices();
// Render the vertex buffer contents
g_pD3DD->SetStreamSource( 0, g_pD3DVB, 0, sizeof(Vertex));
g_pD3DD->SetIndices(g_pD3DIB1);
g_pD3DD->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pD3DD->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// g_pD3DD->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT);
g_pD3DD->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
//set the 1st cube position in the world and draw it
// D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation(&matWorld, 0.0f,3.0f,0.0f);
g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
g_pD3DD->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12 );
// g_pD3DD->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
//set the 2nd cube position in the world and draw it
g_pD3DD->SetIndices(g_pD3DIB2);
D3DXMatrixTranslation(&matWorld ,0.0f,-3.0f,0.0f);
g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
g_pD3DD->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,8,0,8,0,12);
// g_pD3DD->DrawPrimitive(D3DPT_TRIANGLELIST,8,1);
// End the scene*/
g_pD3DD->EndScene();
}
// Present the backbuffer contents to the display
g_pD3DD->Present( NULL, NULL, NULL, NULL );
}
VOID Cleanup()
{
if( g_pD3DVB != NULL )
g_pD3DVB->Release();
if( g_pD3DIB1 != NULL )
g_pD3DIB1->Release();
if( g_pD3DIB2 != NULL )
g_pD3DIB2->Release();
if( g_pD3DD != NULL )
g_pD3DD->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
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 wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"D3D 01", NULL };
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D 01", L"D3D Draw two cubes ",
WS_OVERLAPPEDWINDOW, 200, 200, 800,600,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) )
{
// 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( L"D3D 01", wc.hInstance );
return 0;
}我用Dx9 想画2个正方形 一个红色,一个蓝色,使用VertexBuffer 和2个IndexBuffer
我用DX9.0c ,编译器是VS 2005,代码能通过编译。奇怪的是这2个方形不能同时画出来,但是分开的话就只能画一个出来,
有哪个帮我看看是怎么回事?
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
IDirect3D9* g_pD3D = NULL;
IDirect3DDevice9* g_pD3DD = NULL;
IDirect3DVertexBuffer9* g_pD3DVB = NULL;
IDirect3DIndexBuffer9* g_pD3DIB1 = NULL;
IDirect3DIndexBuffer9* g_pD3DIB2 = NULL;
D3DXMATRIXA16 matWorld;
struct Vertex
{
// Vertex(float x,float y,float z,DWORD color) {x=x;y=y;z=z;color=color;}
float x,y,z;
DWORD color;
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define RED D3DCOLOR_ARGB(0,255,0,0)
#define BLUE D3DCOLOR_ARGB(0,0,0,255)
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_pD3DD ) ) )
{
return E_FAIL;
}
// Turn off culling, so we see the front and back of the triangle
// Turn off D3D lighting, since we are providing our own vertex colors
g_pD3DD->SetRenderState( D3DRS_LIGHTING,FALSE );
return S_OK;
}
HRESULT InitGeometry()
{
// Initialize three vertices for rendering a triangle
// Create the vertex buffer.
if( FAILED( g_pD3DD->CreateVertexBuffer( 16*sizeof(Vertex),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pD3DVB, NULL ) ) )
{
return E_FAIL;
}
/*
//1st Cube Vertices
pVertices[0]={-1.0f,-1.0f,-2.0f,RED,};
pVertices[1]={-1.0f,1.0f,-2.0f,RED,};
pVertices[2]={ 1.0f, 1.0f,-2.0f,RED,};
pVertices[3]={1.0f,-1.0f,-2.0f,RED,};
pVertices[4]={-1.0f,-1.0f, 2.0f,RED,};
pVertices[5]={-1.0f,1.0f, 2.0f,RED,};
pVertices[6]={ 1.0f, 1.0f, 2.0f,RED,};
pVertices[7]={1.0f,-1.0f, 2.0f,RED,};
//2nd Cube Vertices
pVertices[8]={-1.0f,-1.0f,-1.0f,BLUE,};
pVertices[9]={-1.0f,1.0f,-1.0f,BLUE,};
pVertices[10]={ 1.0f,1.0f,-1.0f,BLUE,};
pVertices[11]={1.0f,-1.0f,-1.0f,BLUE,};
pVertices[12]={-1.0f,-1.0f, 1.0f,BLUE,};
pVertices[13]={-1.0f,1.0f, 1.0f,BLUE,};
pVertices[14]={ 1.0f,1.0f, 1.0f,BLUE,};
pVertices[15]={1.0f,-1.0f, 1.0f,BLUE,};
*/
Vertex g_Vertices[] ={
//1st Cube Vertices
{-1.0f,-1.0f,-2.0f,RED,},
{-1.0f, 1.0f,-2.0f,RED,},
{ 1.0f, 1.0f,-2.0f,RED,},
{ 1.0f,-1.0f,-2.0f,RED,},
{-1.0f,-1.0f, 2.0f,RED,},
{-1.0f, 1.0f, 2.0f,RED,},
{ 1.0f, 1.0f, 2.0f,RED,},
{ 1.0f,-1.0f, 2.0f,RED,},
//2nd Cube Vertices
{-1.0f,-1.0f,-1.0f,BLUE,},
{-1.0f, 1.0f,-1.0f,BLUE,},
{ 1.0f, 1.0f,-1.0f,BLUE,},
{ 1.0f,-1.0f,-1.0f,BLUE,},
{-1.0f,-1.0f, 1.0f,BLUE,},
{-1.0f, 1.0f, 1.0f,BLUE,},
{ 1.0f, 1.0f, 1.0f,BLUE,},
{ 1.0f,-1.0f, 1.0f,BLUE,},
};
// Fill the vertex buffer.
VOID* pVertices;
// Vertex* pVertices;
if( FAILED( g_pD3DVB->Lock( 0, 16*sizeof(Vertex) , (void**)&pVertices, 0 ) ) )
{
return E_FAIL;
}
/*
//1st Cube Vertices
pVertices[0]=Vertex(-1.0f,-1.0f,-2.0f,RED);
pVertices[1]=Vertex(-1.0f, 1.0f,-2.0f,RED);
pVertices[2]=Vertex( 1.0f, 1.0f,-2.0f,RED);
pVertices[3]=Vertex( 1.0f,-1.0f,-2.0f,RED);
pVertices[4]=Vertex(-1.0f,-1.0f, 2.0f,RED);
pVertices[5]=Vertex(-1.0f, 1.0f, 2.0f,RED);
pVertices[6]=Vertex( 1.0f, 1.0f, 2.0f,RED);
pVertices[7]=Vertex( 1.0f,-1.0f, 2.0f,RED);
/* //2nd Cube Vertices
pVertices[8]={-1.0f,-1.0f,-1.0f,BLUE,};
pVertices[9]={-1.0f, 1.0f,-1.0f,BLUE,};
pVertices[10]={ 1.0f, 1.0f,-1.0f,BLUE,};
pVertices[11]={ 1.0f,-1.0f,-1.0f,BLUE,};
pVertices[12]={-1.0f,-1.0f, 1.0f,BLUE,};
pVertices[13]={-1.0f, 1.0f, 1.0f,BLUE,};
pVertices[14]={ 1.0f, 1.0f, 1.0f,BLUE,};
pVertices[15]={ 1.0f,-1.0f, 1.0f,BLUE,};*/
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pD3DVB->Unlock();
// Create the Index buffer.
if( FAILED( g_pD3DD->CreateIndexBuffer( 36*sizeof(WORD),
0, D3DFMT_INDEX16 ,
D3DPOOL_DEFAULT, &g_pD3DIB1, NULL ) ) )
{
return E_FAIL;
}
WORD* pIndices = NULL;
if( FAILED( g_pD3DIB1->Lock( 0, 0, (void**)&pIndices, 0 ) ) )
return E_FAIL;
//bottom of 1st cube
pIndices[0]=0; pIndices[1]=1; pIndices[2]=2;
pIndices[3]=0; pIndices[4]=2; pIndices[5]=3;
//front of 1st cube
pIndices[6]=0; pIndices[7]=4; pIndices[8]=7;
pIndices[9]=0; pIndices[10]=7; pIndices[11]=3;
//back of 1st cube
pIndices[12]=1; pIndices[13]=5; pIndices[14]=6;
pIndices[15]=1; pIndices[16]=6; pIndices[17]=2;
//right side of 1st cube
pIndices[18]=3; pIndices[19]=7; pIndices[20]=6;
pIndices[21]=3; pIndices[22]=6; pIndices[23]=2;
//left side of 1st cube
pIndices[24]=0; pIndices[25]=4; pIndices[26]=5;
pIndices[27]=0; pIndices[28]=5; pIndices[29]=1;
//top of 1st cube
pIndices[30]=4; pIndices[31]=5; pIndices[32]=6;
pIndices[33]=4; pIndices[34]=6; pIndices[35]=7;
/*
//bottom of 2nd cube
pIndices[36]=8; pIndices[37]=9; pIndices[38]=10;
pIndices[39]=8; pIndices[40]=10; pIndices[41]=11;
//front of 2nd cube
pIndices[42]=8; pIndices[43]=12; pIndices[44]=15;
pIndices[45]=8; pIndices[46]=15; pIndices[47]=11;
//back of 2nd cube
pIndices[48]=9; pIndices[49]=13; pIndices[50]=14;
pIndices[51]=9; pIndices[52]=14; pIndices[53]=10;
//right side of 2nd cube
pIndices[54]=11; pIndices[55]=15; pIndices[56]=14;
pIndices[57]=11; pIndices[58]=14; pIndices[59]=10;
//left side of 2nd cube
pIndices[60]=8; pIndices[61]=12; pIndices[62]=13;
pIndices[63]=8; pIndices[64]=13; pIndices[65]=9;
//top of 2nd cube
pIndices[66]=12; pIndices[67]=13; pIndices[68]=14;
pIndices[69]=12; pIndices[70]=14; pIndices[71]=15;*/
g_pD3DIB1->Unlock();
if( FAILED( g_pD3DD->CreateIndexBuffer( 36*sizeof(WORD),
0, D3DFMT_INDEX16 ,
D3DPOOL_DEFAULT, &g_pD3DIB2, NULL ) ) )
{
return E_FAIL;
}
WORD* ppIndices = NULL;;
if( FAILED( g_pD3DIB2->Lock( 0, 0, (void**)&ppIndices, 0 ) ) )
return E_FAIL;
//bottom of 1st cube
ppIndices[0]=8; ppIndices[1]=9; ppIndices[2]=10;
ppIndices[3]=8; ppIndices[4]=10; ppIndices[5]=11;
//front of 1st cube
ppIndices[6]=8; ppIndices[7]=12; ppIndices[8]=15;
ppIndices[9]=8; ppIndices[10]=15; ppIndices[11]=11;
//back of 1st cube
ppIndices[12]=9; ppIndices[13]=13; ppIndices[14]=14;
ppIndices[15]=9; ppIndices[16]=14; ppIndices[17]=10;
//right side of 1st cube
ppIndices[18]=11; ppIndices[19]=15; ppIndices[20]=14;
ppIndices[21]=11; ppIndices[22]=14; ppIndices[23]=10;
//left side of 1st cube
ppIndices[24]=8; ppIndices[25]=12; ppIndices[26]=13;
ppIndices[27]=8; ppIndices[28]=13; ppIndices[29]=9;
//top of 1st cube
ppIndices[30]=12; ppIndices[31]=13; ppIndices[32]=14;
ppIndices[33]=12; ppIndices[34]=14; ppIndices[35]=15;
g_pD3DIB2->Unlock();
return S_OK;
}
VOID SetupMatrices()
{
// D3DXMatrixIdentity(&matWorld);
// D3DXMatrixTranslation(&matWorld, 1.0f,1.0f,0.0f);
// g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
// UINT iTime = timeGetTime() % 1000;
// FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
// D3DXMatrixRotationX( &matWorld, fAngle );
// D3DXMatrixRotationY( &matWorld, fAngle );
// D3DXMatrixRotationZ( &matWorld, fAngle );
//Setup Camara
D3DXVECTOR3 vEyePt(4.0f, 4.0f, 6.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f,0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 0.0f, 1.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pD3DD->SetTransform( D3DTS_VIEW, &matView );
//Setup projection
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI*2/3, 1.0f, 1.0f, 100.0f );
g_pD3DD->SetTransform( D3DTS_PROJECTION, &matProj );
}
VOID Render()
{
// Clear the backbuffer to a black color
g_pD3DD->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(100,100,100), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pD3DD->BeginScene() ) )
{
// Setup the world, view, and projection matrices
SetupMatrices();
// Render the vertex buffer contents
g_pD3DD->SetStreamSource( 0, g_pD3DVB, 0, sizeof(Vertex));
g_pD3DD->SetIndices(g_pD3DIB1);
g_pD3DD->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pD3DD->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// g_pD3DD->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT);
g_pD3DD->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
//set the 1st cube position in the world and draw it
// D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation(&matWorld, 0.0f,3.0f,0.0f);
g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
g_pD3DD->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12 );
// g_pD3DD->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
//set the 2nd cube position in the world and draw it
g_pD3DD->SetIndices(g_pD3DIB2);
D3DXMatrixTranslation(&matWorld ,0.0f,-3.0f,0.0f);
g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
g_pD3DD->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,8,0,8,0,12);
// g_pD3DD->DrawPrimitive(D3DPT_TRIANGLELIST,8,1);
// End the scene*/
g_pD3DD->EndScene();
}
// Present the backbuffer contents to the display
g_pD3DD->Present( NULL, NULL, NULL, NULL );
}
VOID Cleanup()
{
if( g_pD3DVB != NULL )
g_pD3DVB->Release();
if( g_pD3DIB1 != NULL )
g_pD3DIB1->Release();
if( g_pD3DIB2 != NULL )
g_pD3DIB2->Release();
if( g_pD3DD != NULL )
g_pD3DD->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
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 wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"D3D 01", NULL };
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D 01", L"D3D Draw two cubes ",
WS_OVERLAPPEDWINDOW, 200, 200, 800,600,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) )
{
// 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( L"D3D 01", wc.hInstance );
return 0;
}
我用DX9.0c ,编译器是VS 2005,代码能通过编译。奇怪的是这2个方形不能同时画出来,但是分开的话就只能画一个出来,
有哪个帮我看看是怎么回事?
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
IDirect3D9* g_pD3D = NULL;
IDirect3DDevice9* g_pD3DD = NULL;
IDirect3DVertexBuffer9* g_pD3DVB = NULL;
IDirect3DIndexBuffer9* g_pD3DIB1 = NULL;
IDirect3DIndexBuffer9* g_pD3DIB2 = NULL;
D3DXMATRIXA16 matWorld;
struct Vertex
{
// Vertex(float x,float y,float z,DWORD color) {x=x;y=y;z=z;color=color;}
float x,y,z;
DWORD color;
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define RED D3DCOLOR_ARGB(0,255,0,0)
#define BLUE D3DCOLOR_ARGB(0,0,0,255)
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_pD3DD ) ) )
{
return E_FAIL;
}
// Turn off culling, so we see the front and back of the triangle
// Turn off D3D lighting, since we are providing our own vertex colors
g_pD3DD->SetRenderState( D3DRS_LIGHTING,FALSE );
return S_OK;
}
HRESULT InitGeometry()
{
// Initialize three vertices for rendering a triangle
// Create the vertex buffer.
if( FAILED( g_pD3DD->CreateVertexBuffer( 16*sizeof(Vertex),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pD3DVB, NULL ) ) )
{
return E_FAIL;
}
/*
//1st Cube Vertices
pVertices[0]={-1.0f,-1.0f,-2.0f,RED,};
pVertices[1]={-1.0f,1.0f,-2.0f,RED,};
pVertices[2]={ 1.0f, 1.0f,-2.0f,RED,};
pVertices[3]={1.0f,-1.0f,-2.0f,RED,};
pVertices[4]={-1.0f,-1.0f, 2.0f,RED,};
pVertices[5]={-1.0f,1.0f, 2.0f,RED,};
pVertices[6]={ 1.0f, 1.0f, 2.0f,RED,};
pVertices[7]={1.0f,-1.0f, 2.0f,RED,};
//2nd Cube Vertices
pVertices[8]={-1.0f,-1.0f,-1.0f,BLUE,};
pVertices[9]={-1.0f,1.0f,-1.0f,BLUE,};
pVertices[10]={ 1.0f,1.0f,-1.0f,BLUE,};
pVertices[11]={1.0f,-1.0f,-1.0f,BLUE,};
pVertices[12]={-1.0f,-1.0f, 1.0f,BLUE,};
pVertices[13]={-1.0f,1.0f, 1.0f,BLUE,};
pVertices[14]={ 1.0f,1.0f, 1.0f,BLUE,};
pVertices[15]={1.0f,-1.0f, 1.0f,BLUE,};
*/
Vertex g_Vertices[] ={
//1st Cube Vertices
{-1.0f,-1.0f,-2.0f,RED,},
{-1.0f, 1.0f,-2.0f,RED,},
{ 1.0f, 1.0f,-2.0f,RED,},
{ 1.0f,-1.0f,-2.0f,RED,},
{-1.0f,-1.0f, 2.0f,RED,},
{-1.0f, 1.0f, 2.0f,RED,},
{ 1.0f, 1.0f, 2.0f,RED,},
{ 1.0f,-1.0f, 2.0f,RED,},
//2nd Cube Vertices
{-1.0f,-1.0f,-1.0f,BLUE,},
{-1.0f, 1.0f,-1.0f,BLUE,},
{ 1.0f, 1.0f,-1.0f,BLUE,},
{ 1.0f,-1.0f,-1.0f,BLUE,},
{-1.0f,-1.0f, 1.0f,BLUE,},
{-1.0f, 1.0f, 1.0f,BLUE,},
{ 1.0f, 1.0f, 1.0f,BLUE,},
{ 1.0f,-1.0f, 1.0f,BLUE,},
};
// Fill the vertex buffer.
VOID* pVertices;
// Vertex* pVertices;
if( FAILED( g_pD3DVB->Lock( 0, 16*sizeof(Vertex) , (void**)&pVertices, 0 ) ) )
{
return E_FAIL;
}
/*
//1st Cube Vertices
pVertices[0]=Vertex(-1.0f,-1.0f,-2.0f,RED);
pVertices[1]=Vertex(-1.0f, 1.0f,-2.0f,RED);
pVertices[2]=Vertex( 1.0f, 1.0f,-2.0f,RED);
pVertices[3]=Vertex( 1.0f,-1.0f,-2.0f,RED);
pVertices[4]=Vertex(-1.0f,-1.0f, 2.0f,RED);
pVertices[5]=Vertex(-1.0f, 1.0f, 2.0f,RED);
pVertices[6]=Vertex( 1.0f, 1.0f, 2.0f,RED);
pVertices[7]=Vertex( 1.0f,-1.0f, 2.0f,RED);
/* //2nd Cube Vertices
pVertices[8]={-1.0f,-1.0f,-1.0f,BLUE,};
pVertices[9]={-1.0f, 1.0f,-1.0f,BLUE,};
pVertices[10]={ 1.0f, 1.0f,-1.0f,BLUE,};
pVertices[11]={ 1.0f,-1.0f,-1.0f,BLUE,};
pVertices[12]={-1.0f,-1.0f, 1.0f,BLUE,};
pVertices[13]={-1.0f, 1.0f, 1.0f,BLUE,};
pVertices[14]={ 1.0f, 1.0f, 1.0f,BLUE,};
pVertices[15]={ 1.0f,-1.0f, 1.0f,BLUE,};*/
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pD3DVB->Unlock();
// Create the Index buffer.
if( FAILED( g_pD3DD->CreateIndexBuffer( 36*sizeof(WORD),
0, D3DFMT_INDEX16 ,
D3DPOOL_DEFAULT, &g_pD3DIB1, NULL ) ) )
{
return E_FAIL;
}
WORD* pIndices = NULL;
if( FAILED( g_pD3DIB1->Lock( 0, 0, (void**)&pIndices, 0 ) ) )
return E_FAIL;
//bottom of 1st cube
pIndices[0]=0; pIndices[1]=1; pIndices[2]=2;
pIndices[3]=0; pIndices[4]=2; pIndices[5]=3;
//front of 1st cube
pIndices[6]=0; pIndices[7]=4; pIndices[8]=7;
pIndices[9]=0; pIndices[10]=7; pIndices[11]=3;
//back of 1st cube
pIndices[12]=1; pIndices[13]=5; pIndices[14]=6;
pIndices[15]=1; pIndices[16]=6; pIndices[17]=2;
//right side of 1st cube
pIndices[18]=3; pIndices[19]=7; pIndices[20]=6;
pIndices[21]=3; pIndices[22]=6; pIndices[23]=2;
//left side of 1st cube
pIndices[24]=0; pIndices[25]=4; pIndices[26]=5;
pIndices[27]=0; pIndices[28]=5; pIndices[29]=1;
//top of 1st cube
pIndices[30]=4; pIndices[31]=5; pIndices[32]=6;
pIndices[33]=4; pIndices[34]=6; pIndices[35]=7;
/*
//bottom of 2nd cube
pIndices[36]=8; pIndices[37]=9; pIndices[38]=10;
pIndices[39]=8; pIndices[40]=10; pIndices[41]=11;
//front of 2nd cube
pIndices[42]=8; pIndices[43]=12; pIndices[44]=15;
pIndices[45]=8; pIndices[46]=15; pIndices[47]=11;
//back of 2nd cube
pIndices[48]=9; pIndices[49]=13; pIndices[50]=14;
pIndices[51]=9; pIndices[52]=14; pIndices[53]=10;
//right side of 2nd cube
pIndices[54]=11; pIndices[55]=15; pIndices[56]=14;
pIndices[57]=11; pIndices[58]=14; pIndices[59]=10;
//left side of 2nd cube
pIndices[60]=8; pIndices[61]=12; pIndices[62]=13;
pIndices[63]=8; pIndices[64]=13; pIndices[65]=9;
//top of 2nd cube
pIndices[66]=12; pIndices[67]=13; pIndices[68]=14;
pIndices[69]=12; pIndices[70]=14; pIndices[71]=15;*/
g_pD3DIB1->Unlock();
if( FAILED( g_pD3DD->CreateIndexBuffer( 36*sizeof(WORD),
0, D3DFMT_INDEX16 ,
D3DPOOL_DEFAULT, &g_pD3DIB2, NULL ) ) )
{
return E_FAIL;
}
WORD* ppIndices = NULL;;
if( FAILED( g_pD3DIB2->Lock( 0, 0, (void**)&ppIndices, 0 ) ) )
return E_FAIL;
//bottom of 1st cube
ppIndices[0]=8; ppIndices[1]=9; ppIndices[2]=10;
ppIndices[3]=8; ppIndices[4]=10; ppIndices[5]=11;
//front of 1st cube
ppIndices[6]=8; ppIndices[7]=12; ppIndices[8]=15;
ppIndices[9]=8; ppIndices[10]=15; ppIndices[11]=11;
//back of 1st cube
ppIndices[12]=9; ppIndices[13]=13; ppIndices[14]=14;
ppIndices[15]=9; ppIndices[16]=14; ppIndices[17]=10;
//right side of 1st cube
ppIndices[18]=11; ppIndices[19]=15; ppIndices[20]=14;
ppIndices[21]=11; ppIndices[22]=14; ppIndices[23]=10;
//left side of 1st cube
ppIndices[24]=8; ppIndices[25]=12; ppIndices[26]=13;
ppIndices[27]=8; ppIndices[28]=13; ppIndices[29]=9;
//top of 1st cube
ppIndices[30]=12; ppIndices[31]=13; ppIndices[32]=14;
ppIndices[33]=12; ppIndices[34]=14; ppIndices[35]=15;
g_pD3DIB2->Unlock();
return S_OK;
}
VOID SetupMatrices()
{
// D3DXMatrixIdentity(&matWorld);
// D3DXMatrixTranslation(&matWorld, 1.0f,1.0f,0.0f);
// g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
// UINT iTime = timeGetTime() % 1000;
// FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
// D3DXMatrixRotationX( &matWorld, fAngle );
// D3DXMatrixRotationY( &matWorld, fAngle );
// D3DXMatrixRotationZ( &matWorld, fAngle );
//Setup Camara
D3DXVECTOR3 vEyePt(4.0f, 4.0f, 6.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f,0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 0.0f, 1.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pD3DD->SetTransform( D3DTS_VIEW, &matView );
//Setup projection
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI*2/3, 1.0f, 1.0f, 100.0f );
g_pD3DD->SetTransform( D3DTS_PROJECTION, &matProj );
}
VOID Render()
{
// Clear the backbuffer to a black color
g_pD3DD->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(100,100,100), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pD3DD->BeginScene() ) )
{
// Setup the world, view, and projection matrices
SetupMatrices();
// Render the vertex buffer contents
g_pD3DD->SetStreamSource( 0, g_pD3DVB, 0, sizeof(Vertex));
g_pD3DD->SetIndices(g_pD3DIB1);
g_pD3DD->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pD3DD->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// g_pD3DD->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT);
g_pD3DD->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
//set the 1st cube position in the world and draw it
// D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation(&matWorld, 0.0f,3.0f,0.0f);
g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
g_pD3DD->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12 );
// g_pD3DD->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
//set the 2nd cube position in the world and draw it
g_pD3DD->SetIndices(g_pD3DIB2);
D3DXMatrixTranslation(&matWorld ,0.0f,-3.0f,0.0f);
g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
g_pD3DD->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,8,0,8,0,12);
// g_pD3DD->DrawPrimitive(D3DPT_TRIANGLELIST,8,1);
// End the scene*/
g_pD3DD->EndScene();
}
// Present the backbuffer contents to the display
g_pD3DD->Present( NULL, NULL, NULL, NULL );
}
VOID Cleanup()
{
if( g_pD3DVB != NULL )
g_pD3DVB->Release();
if( g_pD3DIB1 != NULL )
g_pD3DIB1->Release();
if( g_pD3DIB2 != NULL )
g_pD3DIB2->Release();
if( g_pD3DD != NULL )
g_pD3DD->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
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 wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"D3D 01", NULL };
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D 01", L"D3D Draw two cubes ",
WS_OVERLAPPEDWINDOW, 200, 200, 800,600,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) )
{
// 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( L"D3D 01", wc.hInstance );
return 0;
}我用Dx9 想画2个正方形 一个红色,一个蓝色,使用VertexBuffer 和2个IndexBuffer
我用DX9.0c ,编译器是VS 2005,代码能通过编译。奇怪的是这2个方形不能同时画出来,但是分开的话就只能画一个出来,
有哪个帮我看看是怎么回事?
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
IDirect3D9* g_pD3D = NULL;
IDirect3DDevice9* g_pD3DD = NULL;
IDirect3DVertexBuffer9* g_pD3DVB = NULL;
IDirect3DIndexBuffer9* g_pD3DIB1 = NULL;
IDirect3DIndexBuffer9* g_pD3DIB2 = NULL;
D3DXMATRIXA16 matWorld;
struct Vertex
{
// Vertex(float x,float y,float z,DWORD color) {x=x;y=y;z=z;color=color;}
float x,y,z;
DWORD color;
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define RED D3DCOLOR_ARGB(0,255,0,0)
#define BLUE D3DCOLOR_ARGB(0,0,0,255)
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_pD3DD ) ) )
{
return E_FAIL;
}
// Turn off culling, so we see the front and back of the triangle
// Turn off D3D lighting, since we are providing our own vertex colors
g_pD3DD->SetRenderState( D3DRS_LIGHTING,FALSE );
return S_OK;
}
HRESULT InitGeometry()
{
// Initialize three vertices for rendering a triangle
// Create the vertex buffer.
if( FAILED( g_pD3DD->CreateVertexBuffer( 16*sizeof(Vertex),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pD3DVB, NULL ) ) )
{
return E_FAIL;
}
/*
//1st Cube Vertices
pVertices[0]={-1.0f,-1.0f,-2.0f,RED,};
pVertices[1]={-1.0f,1.0f,-2.0f,RED,};
pVertices[2]={ 1.0f, 1.0f,-2.0f,RED,};
pVertices[3]={1.0f,-1.0f,-2.0f,RED,};
pVertices[4]={-1.0f,-1.0f, 2.0f,RED,};
pVertices[5]={-1.0f,1.0f, 2.0f,RED,};
pVertices[6]={ 1.0f, 1.0f, 2.0f,RED,};
pVertices[7]={1.0f,-1.0f, 2.0f,RED,};
//2nd Cube Vertices
pVertices[8]={-1.0f,-1.0f,-1.0f,BLUE,};
pVertices[9]={-1.0f,1.0f,-1.0f,BLUE,};
pVertices[10]={ 1.0f,1.0f,-1.0f,BLUE,};
pVertices[11]={1.0f,-1.0f,-1.0f,BLUE,};
pVertices[12]={-1.0f,-1.0f, 1.0f,BLUE,};
pVertices[13]={-1.0f,1.0f, 1.0f,BLUE,};
pVertices[14]={ 1.0f,1.0f, 1.0f,BLUE,};
pVertices[15]={1.0f,-1.0f, 1.0f,BLUE,};
*/
Vertex g_Vertices[] ={
//1st Cube Vertices
{-1.0f,-1.0f,-2.0f,RED,},
{-1.0f, 1.0f,-2.0f,RED,},
{ 1.0f, 1.0f,-2.0f,RED,},
{ 1.0f,-1.0f,-2.0f,RED,},
{-1.0f,-1.0f, 2.0f,RED,},
{-1.0f, 1.0f, 2.0f,RED,},
{ 1.0f, 1.0f, 2.0f,RED,},
{ 1.0f,-1.0f, 2.0f,RED,},
//2nd Cube Vertices
{-1.0f,-1.0f,-1.0f,BLUE,},
{-1.0f, 1.0f,-1.0f,BLUE,},
{ 1.0f, 1.0f,-1.0f,BLUE,},
{ 1.0f,-1.0f,-1.0f,BLUE,},
{-1.0f,-1.0f, 1.0f,BLUE,},
{-1.0f, 1.0f, 1.0f,BLUE,},
{ 1.0f, 1.0f, 1.0f,BLUE,},
{ 1.0f,-1.0f, 1.0f,BLUE,},
};
// Fill the vertex buffer.
VOID* pVertices;
// Vertex* pVertices;
if( FAILED( g_pD3DVB->Lock( 0, 16*sizeof(Vertex) , (void**)&pVertices, 0 ) ) )
{
return E_FAIL;
}
/*
//1st Cube Vertices
pVertices[0]=Vertex(-1.0f,-1.0f,-2.0f,RED);
pVertices[1]=Vertex(-1.0f, 1.0f,-2.0f,RED);
pVertices[2]=Vertex( 1.0f, 1.0f,-2.0f,RED);
pVertices[3]=Vertex( 1.0f,-1.0f,-2.0f,RED);
pVertices[4]=Vertex(-1.0f,-1.0f, 2.0f,RED);
pVertices[5]=Vertex(-1.0f, 1.0f, 2.0f,RED);
pVertices[6]=Vertex( 1.0f, 1.0f, 2.0f,RED);
pVertices[7]=Vertex( 1.0f,-1.0f, 2.0f,RED);
/* //2nd Cube Vertices
pVertices[8]={-1.0f,-1.0f,-1.0f,BLUE,};
pVertices[9]={-1.0f, 1.0f,-1.0f,BLUE,};
pVertices[10]={ 1.0f, 1.0f,-1.0f,BLUE,};
pVertices[11]={ 1.0f,-1.0f,-1.0f,BLUE,};
pVertices[12]={-1.0f,-1.0f, 1.0f,BLUE,};
pVertices[13]={-1.0f, 1.0f, 1.0f,BLUE,};
pVertices[14]={ 1.0f, 1.0f, 1.0f,BLUE,};
pVertices[15]={ 1.0f,-1.0f, 1.0f,BLUE,};*/
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pD3DVB->Unlock();
// Create the Index buffer.
if( FAILED( g_pD3DD->CreateIndexBuffer( 36*sizeof(WORD),
0, D3DFMT_INDEX16 ,
D3DPOOL_DEFAULT, &g_pD3DIB1, NULL ) ) )
{
return E_FAIL;
}
WORD* pIndices = NULL;
if( FAILED( g_pD3DIB1->Lock( 0, 0, (void**)&pIndices, 0 ) ) )
return E_FAIL;
//bottom of 1st cube
pIndices[0]=0; pIndices[1]=1; pIndices[2]=2;
pIndices[3]=0; pIndices[4]=2; pIndices[5]=3;
//front of 1st cube
pIndices[6]=0; pIndices[7]=4; pIndices[8]=7;
pIndices[9]=0; pIndices[10]=7; pIndices[11]=3;
//back of 1st cube
pIndices[12]=1; pIndices[13]=5; pIndices[14]=6;
pIndices[15]=1; pIndices[16]=6; pIndices[17]=2;
//right side of 1st cube
pIndices[18]=3; pIndices[19]=7; pIndices[20]=6;
pIndices[21]=3; pIndices[22]=6; pIndices[23]=2;
//left side of 1st cube
pIndices[24]=0; pIndices[25]=4; pIndices[26]=5;
pIndices[27]=0; pIndices[28]=5; pIndices[29]=1;
//top of 1st cube
pIndices[30]=4; pIndices[31]=5; pIndices[32]=6;
pIndices[33]=4; pIndices[34]=6; pIndices[35]=7;
/*
//bottom of 2nd cube
pIndices[36]=8; pIndices[37]=9; pIndices[38]=10;
pIndices[39]=8; pIndices[40]=10; pIndices[41]=11;
//front of 2nd cube
pIndices[42]=8; pIndices[43]=12; pIndices[44]=15;
pIndices[45]=8; pIndices[46]=15; pIndices[47]=11;
//back of 2nd cube
pIndices[48]=9; pIndices[49]=13; pIndices[50]=14;
pIndices[51]=9; pIndices[52]=14; pIndices[53]=10;
//right side of 2nd cube
pIndices[54]=11; pIndices[55]=15; pIndices[56]=14;
pIndices[57]=11; pIndices[58]=14; pIndices[59]=10;
//left side of 2nd cube
pIndices[60]=8; pIndices[61]=12; pIndices[62]=13;
pIndices[63]=8; pIndices[64]=13; pIndices[65]=9;
//top of 2nd cube
pIndices[66]=12; pIndices[67]=13; pIndices[68]=14;
pIndices[69]=12; pIndices[70]=14; pIndices[71]=15;*/
g_pD3DIB1->Unlock();
if( FAILED( g_pD3DD->CreateIndexBuffer( 36*sizeof(WORD),
0, D3DFMT_INDEX16 ,
D3DPOOL_DEFAULT, &g_pD3DIB2, NULL ) ) )
{
return E_FAIL;
}
WORD* ppIndices = NULL;;
if( FAILED( g_pD3DIB2->Lock( 0, 0, (void**)&ppIndices, 0 ) ) )
return E_FAIL;
//bottom of 1st cube
ppIndices[0]=8; ppIndices[1]=9; ppIndices[2]=10;
ppIndices[3]=8; ppIndices[4]=10; ppIndices[5]=11;
//front of 1st cube
ppIndices[6]=8; ppIndices[7]=12; ppIndices[8]=15;
ppIndices[9]=8; ppIndices[10]=15; ppIndices[11]=11;
//back of 1st cube
ppIndices[12]=9; ppIndices[13]=13; ppIndices[14]=14;
ppIndices[15]=9; ppIndices[16]=14; ppIndices[17]=10;
//right side of 1st cube
ppIndices[18]=11; ppIndices[19]=15; ppIndices[20]=14;
ppIndices[21]=11; ppIndices[22]=14; ppIndices[23]=10;
//left side of 1st cube
ppIndices[24]=8; ppIndices[25]=12; ppIndices[26]=13;
ppIndices[27]=8; ppIndices[28]=13; ppIndices[29]=9;
//top of 1st cube
ppIndices[30]=12; ppIndices[31]=13; ppIndices[32]=14;
ppIndices[33]=12; ppIndices[34]=14; ppIndices[35]=15;
g_pD3DIB2->Unlock();
return S_OK;
}
VOID SetupMatrices()
{
// D3DXMatrixIdentity(&matWorld);
// D3DXMatrixTranslation(&matWorld, 1.0f,1.0f,0.0f);
// g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
// UINT iTime = timeGetTime() % 1000;
// FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
// D3DXMatrixRotationX( &matWorld, fAngle );
// D3DXMatrixRotationY( &matWorld, fAngle );
// D3DXMatrixRotationZ( &matWorld, fAngle );
//Setup Camara
D3DXVECTOR3 vEyePt(4.0f, 4.0f, 6.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f,0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 0.0f, 1.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pD3DD->SetTransform( D3DTS_VIEW, &matView );
//Setup projection
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI*2/3, 1.0f, 1.0f, 100.0f );
g_pD3DD->SetTransform( D3DTS_PROJECTION, &matProj );
}
VOID Render()
{
// Clear the backbuffer to a black color
g_pD3DD->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(100,100,100), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pD3DD->BeginScene() ) )
{
// Setup the world, view, and projection matrices
SetupMatrices();
// Render the vertex buffer contents
g_pD3DD->SetStreamSource( 0, g_pD3DVB, 0, sizeof(Vertex));
g_pD3DD->SetIndices(g_pD3DIB1);
g_pD3DD->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pD3DD->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// g_pD3DD->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT);
g_pD3DD->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
//set the 1st cube position in the world and draw it
// D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation(&matWorld, 0.0f,3.0f,0.0f);
g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
g_pD3DD->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12 );
// g_pD3DD->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
//set the 2nd cube position in the world and draw it
g_pD3DD->SetIndices(g_pD3DIB2);
D3DXMatrixTranslation(&matWorld ,0.0f,-3.0f,0.0f);
g_pD3DD->SetTransform( D3DTS_WORLD, &matWorld );
g_pD3DD->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,8,0,8,0,12);
// g_pD3DD->DrawPrimitive(D3DPT_TRIANGLELIST,8,1);
// End the scene*/
g_pD3DD->EndScene();
}
// Present the backbuffer contents to the display
g_pD3DD->Present( NULL, NULL, NULL, NULL );
}
VOID Cleanup()
{
if( g_pD3DVB != NULL )
g_pD3DVB->Release();
if( g_pD3DIB1 != NULL )
g_pD3DIB1->Release();
if( g_pD3DIB2 != NULL )
g_pD3DIB2->Release();
if( g_pD3DD != NULL )
g_pD3DD->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
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 wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"D3D 01", NULL };
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D 01", L"D3D Draw two cubes ",
WS_OVERLAPPEDWINDOW, 200, 200, 800,600,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) )
{
// 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( L"D3D 01", wc.hInstance );
return 0;
}
力不从心,存顶了:D
原帖由 p2p2p2 于 2007-7-4 01:10 发表
力不从心,存顶了:D
谢了:L
千万别客气,再次替你顶一次(虽然已经在最顶了):D :D :D
我也帮你顶一次。弄了半天原来是个学软件编程的,最终也只是个吃青春饭的可怜人!!!
替你默哀三分钟!![:a9:] [:a9:] [:a9:]
替你默哀三分钟!![:a9:] [:a9:] [:a9:]
:L 汗死,你慢慢看书吧,我还给老师了:D