注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

刘邓

每天收获一点点-目标:富足

 
 
 

日志

 
 

Direct3D纹理,灯光,网格,材质跨越式模仿。  

2012-03-08 15:55:28|  分类: 技术类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
今天照着书抄了一段常常的代码里面包括了对.x文件的操作,网格,纹理以及灯光的许多高级内容,进一步增加了我的兴趣:
Direct3D纹理,灯光,网格,材质跨越式模仿。 - 刘邓 - 刘邓
 
Direct3D纹理,灯光,网格,材质跨越式模仿。 - 刘邓 - 刘邓
 
Direct3D纹理,灯光,网格,材质跨越式模仿。 - 刘邓 - 刘邓
 
Direct3D纹理,灯光,网格,材质跨越式模仿。 - 刘邓 - 刘邓
 
总结下D3D绘制的过程:

1.声明D3D设备,纹理,灯光,材质
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;//声明Direct3D设备接口
LPD3DXMESH  g_pMeshTiny  = NULL;//声明网格对象
D3DMATERIAL9*  g_pMaterials = NULL;//声明材质信息
LPDIRECT3DTEXTURE9* g_pTextures = NULL;//网格的纹理信息
DWORD  g_dwNumMtrls = 0; //材质的数目
2.初始化设备:
//创建IDirect3D接口
LPDIRECT3D9 pD3D = NULL;//IDirect interface
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (pD3D == NULL)
{
return E_FAIL;
}
//获取硬件设备信息
D3DCAPS9 caps;
int vp = 0;
pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps);
if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

//创建Direct3D设备接口
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.BackBufferWidth = 640;
d3dpp.BackBufferHeight = 480;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount  = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.Windowed = true;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,vp,&d3dpp,&g_pd3dDevice);//创建设备
pD3D->Release();
3.从.x文件读入网格信息,并初始化
//以下部分为从X文件加载网格模型
LPD3DXBUFFER pAdjBuffer = NULL;//网格模型缓冲区
LPD3DXBUFFER pMtrlBuffer = NULL;//材质缓冲区
D3DXLoadMeshFromX ("tiny.x",D3DXMESH_MANAGED,g_pd3dDevice,&pAdjBuffer,&pMtrlBuffer,NULL
,&g_dwNumMtrls,&g_pMeshTiny);//从tiny.x创建网格
//以下部分为读取材质和纹理数据
D3DXMATERIAL *pMtrls = (D3DXMATERIAL *)pMtrlBuffer->GetBufferPointer();
g_pMaterials = new D3DMATERIAL9 [g_dwNumMtrls];
g_pTextures = new LPDIRECT3DTEXTURE9[g_dwNumMtrls];

for (DWORD i = 0;i<g_dwNumMtrls;i++)
{
g_pMaterials[i] = pMtrls[i].MatD3D;
g_pMaterials[i].Ambient = g_pMaterials[i].Diffuse;
g_pTextures[i] = NULL;
D3DXCreateTextureFromFile(g_pd3dDevice,pMtrls[i].pTextureFilename,&g_pTextures[i]);
}
pAdjBuffer->Release();
pMtrlBuffer->Release();
//下面设置纹理过滤方式  什么是纹理过滤???
g_pd3dDevice->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
4.设置取景变换和投影变换:
//以下部分为取景变换矩阵
D3DXMATRIX matView;
D3DXVECTOR3 vEye(0.0f,0.0,-50.0f);//设置观察距离为1000
D3DXVECTOR3 vAt(0.0f,0.0f,0.0f);
D3DXVECTOR3 vUp(0.0f,1.0f,0.0f);
D3DXMatrixLookAtLH(&matView,&vEye,&vAt,&vUp);
g_pd3dDevice->SetTransform(D3DTS_VIEW,&matView);

//设置投影变换矩阵
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4.0f,1.0f,1.0f,2000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION,&matProj);
5.设置灯光:
//以下部分设置光照
D3DLIGHT9 light;
ZeroMemory(&light,sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = D3DXCOLOR(0.3f,0.3f,0.3f,1.0f);
light.Diffuse = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
light.Specular = D3DXCOLOR(0.0f,0.0f,0.0f,1.0f);
light.Direction = D3DXVECTOR3(1.0f,0.0f,1.0f);
g_pd3dDevice->SetLight(0,&light);
g_pd3dDevice->LightEnable(0,true);
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS,true);
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE,true);
6.绘制的过程:
void Direct3DRender()
{
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
g_pd3dDevice->BeginScene(); //开始绘制
/*
...
...
其中为具体绘制的内容
...
...
*/
//以下内容设置世界变换
D3DXMATRIX matWorld,Rx,Ry;
D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationX(&Rx,-D3DX_PI/2.0f);
D3DXMatrixRotationY(&Ry,D3DX_PI+::timeGetTime()/360.f);  //此语句实现绕Y轴旋转
matWorld = Rx * Ry *matWorld;
g_pd3dDevice->SetTransform(D3DTS_WORLD ,&matWorld);

//以下内容设置填充方式
if(::GetAsyncKeyState('F')&0x8000f)   //线框填充状态
g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
if(::GetAsyncKeyState('S')&0x8000f)    //面填充状态
g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);

//以下内容为绘制网格
for(DWORD i=0;i<g_dwNumMtrls;i++)
{
g_pd3dDevice->SetMaterial(&g_pMaterials[i]);
g_pd3dDevice->SetTexture(0,g_pTextures[i]);
g_pMeshTiny ->DrawSubset(i);
}
g_pd3dDevice->EndScene();
g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
}
7,释放内存:
文件开始时设置宏:
//安全释放宏定义
#define SAFE_DELETE(p){if (p){delete(p);(p) = NULL;}}
#define SAFE_RELEASE(p){if(p){(p)->Release();(p)= NULL;}}

void Direct3DCleanup()
{
for (DWORD i=0;i<g_dwNumMtrls;i++)
{
SAFE_RELEASE(g_pTextures[i])
}
SAFE_DELETE(g_pTextures);
SAFE_DELETE(g_pMaterials);
SAFE_RELEASE(g_pMeshTiny);
SAFE_RELEASE(g_pd3dDevice);
}
  评论这张
 
阅读(457)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018