TexFiltering.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:7k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "zfxd3dUtility.h"
- #include <stdio.h>
- // 自定义顶点结构体
- typedef struct tagCUSTOMVERTEX{
- D3DXVECTOR3 position; // The position of the vertex.
- FLOAT tu, tv; // The color
- }CUSTOMVERTEX;
- // 自定义顶点格式
- #define D3DFVF_CUSTOMVERTEX D3DFVF_XYZ|D3DFVF_TEX1
- typedef enum tagTEXTUREFILTERTYPE{
- SAMPLE_POINT = 0,
- SAMPLE_LINEAR = 1,
- SAMPLE_ANISOTROPIC = 2,
- }TEXTUREFILTERTYPE;
- // D3D对象接口
- IDirect3D9* g_pd3d;
- // D3D设备接口
- IDirect3DDevice9* g_pd3dDevice;
- // 字体
- ID3DXFont* g_pFont = NULL;
- // 顶点缓冲和纹理接口
- IDirect3DVertexBuffer9* g_pVB = NULL;
- IDirect3DTexture9* g_pTex = NULL;
- // 设备可支持的最大各向异性滤波值
- int g_maxAnisotropic;
- // 枚举变量,纹理过滤模式
- TEXTUREFILTERTYPE g_filtertype = SAMPLE_POINT;
- // 布尔变量,是否开启明细纹理过滤功能
- bool g_mipmap = false;
- // 建立渲染环境和初始化
- bool Setup()
- {
- // 得到最大的各向异性值
- D3DCAPS9 caps;
- g_pd3dDevice->GetDeviceCaps(&caps);
- g_maxAnisotropic = caps.MaxAnisotropy;
- // 加载纹理
- HRESULT hr;
- hr = D3DXCreateTextureFromFile(g_pd3dDevice, "texture.bmp", &g_pTex);
- if( FAILED(hr))
- {
- MessageBox(0, "Failed to create texture from file", 0, 0);
- return false;
- }
- else
- g_pd3dDevice->SetTexture(0, g_pTex);
- // 顶点缓冲初始化
- CUSTOMVERTEX vertices[4] = {
- { D3DXVECTOR3(-100, 0, -100), 0, 0, },
- { D3DXVECTOR3( 100, 0, -100), 11, 0, },
- { D3DXVECTOR3(-100, 0, 100), 0, 11, },
- { D3DXVECTOR3( 100, 0, 100), 11, 11, },
- };
- if( FAILED(g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
- , D3DPOOL_DEFAULT, &g_pVB, NULL)))
- return FALSE;
- void* pData;
- g_pVB->Lock(0, 4*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
- memcpy(pData, vertices, 4*sizeof(CUSTOMVERTEX));
- g_pVB->Unlock();
- // 设定渲染状态
- g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
- g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
- // 设置投影矩阵
- D3DXMATRIX proj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI * 0.25f, // 45 - degree
- 1,
- 1.0f,
- 1000.0f);
- g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
- return true;
- }
- bool DrawTextX()
- {
- //用于渲染侦FPS
- static int tmLastTime = GetTickCount();
- int tmNow = GetTickCount();
- //用于计算渲染FPS
- static int nFrameCount = 0;
- static int nFPS = 0;
- if(tmNow - tmLastTime > 1000)
- {
- //计算FPS
- tmLastTime = tmNow;
- nFPS = nFrameCount;
- nFrameCount = 0;
- }
- nFrameCount ++;
- char str[500];
- RECT rect;
- rect.left = 10;
- rect.top = 10;
- rect.bottom = rect.top + 30;
- rect.right = rect.left + 360;
- sprintf(str, "Current FPS:%d, please press 1~5 to enable a different texture filtering mode", nFPS);
- g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
- switch(g_filtertype)
- {
- case SAMPLE_POINT:
- sprintf(str, "filter type: nearest-point sampling");
- break;
- case SAMPLE_LINEAR:
- sprintf(str, "filter type: linear texture filtering");
- break;
- case SAMPLE_ANISOTROPIC:
- sprintf(str, "filter type: anisotropic texture filtering");
- break;
- }
- rect.left = 10;
- rect.top = 50;
- rect.bottom = rect.top + 10;
- rect.right = rect.left + 360;
- g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
- if(g_mipmap)
- sprintf(str, "mipmap texturing on");
- else
- sprintf(str, "mipmap texturing off");
- rect.left = 10;
- rect.top = 65;
- rect.bottom = rect.top + 10;
- rect.right = rect.left + 360;
- g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
- return true;
- }
- bool Display()
- {
- // 设定视口矩阵
- float angle = (3.0f * D3DX_PI) / 2.0f;
- float height = 5.0f;
- D3DXVECTOR3 eyept( cosf(angle) * 50.0f, height, sinf(angle) * 50.0f );
- D3DXVECTOR3 lookAt(0.0f, 0.0f, 0.0f);
- D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
- D3DXMATRIX view_mat;
- D3DXMatrixLookAtLH(&view_mat, &eyept, &lookAt, &up);
- g_pd3dDevice->SetTransform(D3DTS_VIEW, &view_mat);
- // 绘制场景
- g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
- g_pd3dDevice->BeginScene();
- // 输出顶点流
- g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
- g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
- // 在屏幕上写字
- if(g_pFont)
- DrawTextX();
- g_pd3dDevice->EndScene();
- g_pd3dDevice->Present(0, 0, 0, 0);
- return true;
- }
- bool CleanUp()
- {
- SAFE_RELEASE(g_pd3dDevice);
- SAFE_RELEASE(g_pd3d);
- SAFE_RELEASE(g_pTex);
- SAFE_RELEASE(g_pVB);
- return true;
- }
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int showCmd)
- {
- // 应用程序初始化
- if(!InitD3DApp(hInstance, true/*true代表窗口模式*/,
- D3DDEVTYPE_HAL, &g_pd3d, &g_pd3dDevice, "Texture Filtering"/*窗口标题*/, 400, 400))
- {
- MessageBoxW(0, L"应用程序初始化失败", 0, 0);
- return 0;
- }
- // 创建字体
- if(!InitDXFont(g_pd3dDevice, &g_pFont, "Times New Roman"))
- g_pFont = NULL;
- if(!Setup())
- {
- MessageBox(0, "SetUp() failed!", 0, 0);
- return 0;
- }
- // 进入消息循环
- MSG msg;
- ZeroMemory(&msg, sizeof(msg));
- while(msg.message != WM_QUIT)
- {
- if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- if(!Display())
- {
- MessageBox(0, "Display() failed!", 0, 0);
- return 0;
- }
- }
- }
- CleanUp();
- return 0;
- }
- // 消息循环函数
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- static bool bWireFrame = false;
- switch( msg )
- {
- case WM_DESTROY:
- ::PostQuitMessage(0);
- break;
- case WM_KEYDOWN:
- if( wParam == VK_ESCAPE )
- ::DestroyWindow(hwnd);
- break;
- case WM_CHAR:
- if(wParam == 49)// 数字键1
- {
- // 开启最近点采样
- g_filtertype = SAMPLE_POINT;
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
- }
- else if(wParam == 50)// 数字键2
- {
- // 开启线性纹理采样
- g_filtertype = SAMPLE_LINEAR;
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
- }
- else if(wParam == 51)// 数字键3
- {
- // 开启各向异性纹理采样
- g_filtertype = SAMPLE_ANISOTROPIC;
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 4);
- }
- else if(wParam == 52)// 数字键4
- {
- // 开启明细纹理
- g_mipmap = true;
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 1);
- }
- else if(wParam == 53)// 数字键5
- {
- // 禁用明细纹理
- g_mipmap = false;
- g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
- }
- break;
- }
- return ::DefWindowProc(hwnd, msg, wParam, lParam);
- }