|
|
为什么下面这段程序执行完只显示白色的背景阿,我才开始学directx,请高手指点!
#include "d3dUtility.h"
IDirect3DDevice9* Device = 0;
ID3DXMesh* Teapot = 0; // the teapot
D3DMATERIAL9 TeapotMtrl; // the teapot's material
IDirect3DVertexBuffer9* BkGndQuad = 0; // background quad - crate
IDirect3DTexture9* BkGndTex = 0; // crate texture
D3DMATERIAL9 BkGndMtrl; // background material
const int Width = 800;
const int Height = 600;
void ComputeNormal(D3DXVECTOR3& p0,D3DXVECTOR3& p1,D3DXVECTOR3& p2,D3DXVECTOR3& out)
{
D3DXVECTOR3 u = p1 - p0;
D3DXVECTOR3 v = p2 - p0;
D3DXVec3Cross(&out, &u, &v);
D3DXVec3Normalize(&out,&out);
}
struct Vertex {
float _x, _y, _z;
float _nx, _ny, _nz;
float _u,_v;
static const DWORD FVF;
Vertex(){;}
Vertex(float x,float y,float z,float nx,float ny,float nz,float u,float v){
_x=x;
_y=y;
_z=z;
_nx=nx;
_ny=ny;
_nz=nz;
_u=u;
_v=v;}
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL|D3DFVF_TEX1;
bool Setup()
{
TeapotMtrl = d3d::RED_MTRL;
TeapotMtrl.Diffuse.a = 0.5f; // set alpha to 50% opacity
BkGndMtrl = d3d::WHITE_MTRL;
D3DXCreateTeapot(Device, &Teapot, 0);
Device->CreateVertexBuffer(6 * sizeof(Vertex),D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&BkGndQuad,0);
Vertex* v;
BkGndQuad->Lock(0, 0, (void**)&v, 0);
v[0] = Vertex(-1.0f, -1.0f, 6.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[1] = Vertex(-1.0f, 1.0f, 6.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[2] = Vertex( 1.0f, 1.0f, 6.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[3] = Vertex(-1.0f, -1.0f, 6.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[4] = Vertex( 1.0f, 1.0f, 6.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[5] = Vertex( 1.0f, -1.0f, 6.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
BkGndQuad->Unlock();
D3DXCreateTextureFromFile(Device,".\\crate.bmp",&BkGndTex);
Device->SetTexture(0, BkGndTex);
Device->SetRenderState(D3DRS_LIGHTING, false);
D3DXVECTOR3 position(0.0, 0.0f, -10.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
// 投影矩阵
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,1.0f,1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
Device->SetTextureStageState(0, 3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(BkGndQuad);
Teapot->Release();
Teapot = 0;
}
bool Display(float timeDelta)
{
if( Device )
{
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
Device->SetFVF(Vertex::FVF);
Device->SetStreamSource(0, BkGndQuad, 0, sizeof(Vertex));
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
D3DXMATRIX W;
D3DXMatrixScaling(&W, 1.5f, 1.5f, 1.5f);
Device->SetTransform(D3DTS_WORLD, &W);
Device->SetMaterial(&TeapotMtrl);
Device->SetTexture(0, 0);
Teapot->DrawSubset(0);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
Device->EndScene();
Device-> resent(0, 0, 0, 0); // 翻转表面
}
return true;
}
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE prevInstance,PSTR cmdLine,int showCmd)
{
if(!d3d::InitD3D(hinstance, 800, 600, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop(Display);
Cleanup();
Device->Release();
return 0;
} |
|