首页 > 学院 > 开发设计 > 正文

Direct3D---纹理映射

2019-11-10 20:25:02
字体:
来源:转载
供稿:网友
//--------------------------------------------------------------------------------------// 【纹理绘制四步曲之一】:顶点的定义 //--------------------------------------------------------------------------------------struct CUSTOMVERTEX { FLOAT _x, _y, _z; // 顶点的位置 FLOAT _u, _v; // 纹理坐标 CUSTOMVERTEX(FLOAT x, FLOAT y, FLOAT z, FLOAT u, FLOAT v) : _x(x), _y(y), _z(z), _u(u), _v(v) {}};//D3DFVF_TEX1: 一层纹理#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_TEX1)// 【纹理绘制四步曲之二】:顶点的访问 //-------------------------------------------------------------------------------------- //填充顶点缓存 CUSTOMVERTEX* pVertices; if( FAILED( g_pVertexBuffer->Lock( 0, sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 ) ) ) return E_FAIL; // 正面顶点数据 // 前3个数据为顶点位置,后2个数据为纹理坐标 pVertices[0] = CUSTOMVERTEX(-10.0f, 10.0f, -10.0f, 0.0f, 0.0f); pVertices[1] = CUSTOMVERTEX( 10.0f, 10.0f, -10.0f, 2.0f, 0.0f); pVertices[2] = CUSTOMVERTEX( 10.0f, -10.0f, -10.0f, 2.0f, 2.0f); pVertices[3] = CUSTOMVERTEX(-10.0f, -10.0f, -10.0f, 0.0f, 2.0f); // 背面顶点数据 pVertices[4] = CUSTOMVERTEX( 10.0f, 10.0f, 10.0f, 0.0f, 0.0f); pVertices[5] = CUSTOMVERTEX(-10.0f, 10.0f, 10.0f, 2.0f, 0.0f); pVertices[6] = CUSTOMVERTEX(-10.0f, -10.0f, 10.0f, 2.0f, 2.0f); pVertices[7] = CUSTOMVERTEX( 10.0f, -10.0f, 10.0f, 0.0f, 2.0f); // 顶面顶点数据 pVertices[8] = CUSTOMVERTEX(-10.0f, 10.0f, 10.0f, 0.0f, 0.0f); pVertices[9] = CUSTOMVERTEX( 10.0f, 10.0f, 10.0f, 2.0f, 0.0f); pVertices[10] = CUSTOMVERTEX( 10.0f, 10.0f, -10.0f, 2.0f, 2.0f); pVertices[11] = CUSTOMVERTEX(-10.0f, 10.0f, -10.0f, 0.0f, 2.0f); // 底面顶点数据 pVertices[12] = CUSTOMVERTEX(-10.0f, -10.0f, -10.0f, 0.0f, 0.0f); pVertices[13] = CUSTOMVERTEX( 10.0f, -10.0f, -10.0f, 2.0f, 0.0f); pVertices[14] = CUSTOMVERTEX( 10.0f, -10.0f, 10.0f, 2.0f, 2.0f); pVertices[15] = CUSTOMVERTEX(-10.0f, -10.0f, 10.0f, 0.0f, 2.0f); // 左侧面顶点数据 pVertices[16] = CUSTOMVERTEX(-10.0f, 10.0f, 10.0f, 0.0f, 0.0f); pVertices[17] = CUSTOMVERTEX(-10.0f, 10.0f, -10.0f, 1.0f, 0.0f); pVertices[18] = CUSTOMVERTEX(-10.0f, -10.0f, -10.0f, 1.0f, 1.0f); pVertices[19] = CUSTOMVERTEX(-10.0f, -10.0f, 10.0f, 0.0f, 1.0f); // 右侧面顶点数据 pVertices[20] = CUSTOMVERTEX( 10.0f, 10.0f, -10.0f, 0.0f, 0.0f); pVertices[21] = CUSTOMVERTEX( 10.0f, 10.0f, 10.0f, 1.0f, 0.0f); pVertices[22] = CUSTOMVERTEX( 10.0f, -10.0f, 10.0f, 1.0f, 1.0f); pVertices[23] = CUSTOMVERTEX( 10.0f, -10.0f, -10.0f, 0.0f, 1.0f); g_pVertexBuffer->Unlock();//-------------------------------------------------------------------------------------- // 【纹理绘制四步曲之三】:纹理的创建 //-------------------------------------------------------------------------------------- // 创建纹理 //g_pd3dDevice: D3Drect3D设备对象 //L"pal5q.jpg": 创建纹理的图标文件名字的字符串 //0: 创建纹理对象的宽度,0表示使用贴图宽度 //0: 创建纹理对象的高度,0表示使用贴图高度 //6: 创建纹理的渐进级别 //0: 纹理的使用方式,一般是0 //D3DFMT_X8R8G8B8: 纹理中保存每个颜色成分所使用的位数 //D3DPOOL_MANAGED: 如何管理纹理,一般为D3DPOOL_MANAGED //D3DX_DEFAULT: 图像像素的过滤方式 //D3DX_DEFAULT: Mip像素过滤方式 //0xFF000000: 透明色,设定这个颜色,在显示时,这图像中的这个颜色将忽略 //0: 记录载入图片信息,通常为NULL //0: 记录调色板信息,通常为NULL //g_pMipTexture: 保存最终创建的纹理 D3DXCreateTextureFromFileEx(g_pd3dDevice, L"pal5q.jpg", 0, 0, 6, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0xFF000000, 0, 0, &g_pMipTexture);//各向异性过滤 //设置最大各项异性的程度值 g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 3); //0: 为哪一层纹理设置采样状态 //D3DSAMP_MAGFILTER: 对哪种纹理采样属性进行操作,设置放大过滤器 //D3DTEXF_ANISOTROPIC: 对第二个属性进行值的设定 g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC); //D3DSAMP_MINFILTER: 对哪种纹理采样属性进行操作,设置缩小过滤器 g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC); //线性纹理过滤 //g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); //g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); //最近点采样过滤 //g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); //g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); //渐进纹理过滤 //g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); //g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 16);//-------------------------------------------------------------------------------------- // 【纹理绘制四步曲之四】:纹理的启用 //-------------------------------------------------------------------------------------- //0: 指定了应用纹理是哪一层 //g_pMipTexture: 将要启用纹理的IDrect3DBaseTexture9接口对象,为第二步中定义过的g_pMipTexture参数 g_pd3dDevice->SetTexture(0, g_pMipTexture); //启用纹理

第一步为全局变量

第二步与第三步位于函数Objects_Init()中,Objects_Init()是渲染资源初始化函数,在此函数中进行要被渲染的物体的资源的初始化 Objects_Init()调用位置:Direct3D_Init()末尾,即Direct3D初始化完成后。 Direct3D_Init():Direct3D初始化函数,进行Direct3D的初始化。 Direct3D_Init()调用位置:Windows窗口创建后,即函数CreateWindow()后面

最后一步位于函数Direct3D_Render()中,Direct3D_Render()是使用Direct3D进行渲染的函数,位于该函数的“【Direct3D渲染五步曲之三】:正式绘制,利用顶点缓存绘制图形 ”中 Direct3D_Render()调用位置: 窗口过程函数WndPRoc()中,接收到消息WM_PAINT后

设置纹理的寻址方式:

// 根据键盘按键的按下,设置为纹理寻址方式 if (g_pDInput->IsKeyDown(DIK_1)) //键盘上1键被按下 { // 设置重复纹理寻址模式 g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); } if (g_pDInput->IsKeyDown(DIK_2)) //键盘上2键被按下 { // 设置镜像纹理寻址模式 g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR); g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR); } if (g_pDInput->IsKeyDown(DIK_3)) //键盘上3键被按下 { // 设置夹取纹理寻址模式 g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); } if (g_pDInput->IsKeyDown(DIK_4)) //键盘上4键被按下 { // 设置边框纹理寻址模式 g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER); }

位于函数Direct3D_Update()中 Direct3D_Update(): 不是即时渲染代码但是需要即时调用的,如按键后的坐标的更改,都放在这里。 Direct3D_Update()调用位置: WinMain中的消息循环过程:

//【5】消息循环过程 MSG msg = { 0 }; //初始化msg while( msg.message != WM_QUIT ) //使用while循环 { if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) //查看应用程序消息队列,有消息时将队列中的消息派发出去。 { TranslateMessage( &msg ); //将虚拟键消息转换为字符消息 DispatchMessage( &msg ); //该函数分发一个消息给窗口程序。 } else { Direct3D_Update(hwnd); //调用更新函数,进行画面的更新 Direct3D_Render(hwnd); //调用渲染函数,进行画面的渲染 } }

这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表