Multitexturing.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 rhw;
- FLOAT tu1, tv1; // The texture coordinate
- FLOAT tu2, tv2; // The texture coordinate
- }CUSTOMVERTEX;
- // 自定义顶点格式
- #define D3DFVF_CUSTOMVERTEX D3DFVF_XYZRHW|D3DFVF_TEX2
- // D3D对象接口
- IDirect3D9* g_pd3d;
- // D3D设备接口
- IDirect3DDevice9* g_pd3dDevice;
- // 字体
- ID3DXFont* g_pFont = NULL;
- // 顶点缓冲
- IDirect3DVertexBuffer9* g_pVB = NULL;
- // 纹理接口
- IDirect3DTexture9* g_pTex1 = NULL;
- IDirect3DTexture9* g_pTex2 = NULL;
- // 建立渲染环境和初始化
- bool Setup()
- {
- // 顶点缓冲初始化
- CUSTOMVERTEX vertices[4] = {
- { D3DXVECTOR3( 100, 100, 0), 1, 0, 0, 0, 0, },
- { D3DXVECTOR3( 100, 400, 0), 1, 0, 1, 0, 1, },
- { D3DXVECTOR3( 400, 100, 0), 1, 1, 0, 1, 0, },
- { D3DXVECTOR3( 400, 400, 0), 1, 1, 1, 1, 1, },
- };
- 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);
- // 加载纹理
- HRESULT hr;
- hr = D3DXCreateTextureFromFile(g_pd3dDevice, "wall.bmp", &g_pTex1);
- if( FAILED(hr))
- {
- MessageBox(0, "Failed to create texture from file", 0, 0);
- return false;
- }
- else
- g_pd3dDevice->SetTexture(0, g_pTex1);
- hr = D3DXCreateTextureFromFile(g_pd3dDevice, "lightmap.bmp", &g_pTex2);
- if( FAILED(hr))
- {
- MessageBox(0, "Failed to create texture from file", 0, 0);
- return false;
- }
- else
- g_pd3dDevice->SetTexture(1, g_pTex2);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
- 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 multi-texturing mode", nFPS);
- g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
- return true;
- }
- bool Display()
- {
- // 绘制场景
- 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_pTex1);
- SAFE_RELEASE(g_pTex2);
- 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, "MultiTexturing"/*窗口标题*/, 500, 500))
- {
- 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:
- switch(wParam)
- {
- case 49:// 数字键1,两块纹理相乘
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
- break;
- case 50:// 数字键2,两块纹理相加
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
- break;
- case 51:// 数字键3,两块纹理相减
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SUBTRACT);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_TEXTURE);
- break;
- case 52:// 数字键4,两块纹理相乘再乘以2
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE2X);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
- break;
- case 53:// 数字键5,禁止纹理混合
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
- break;
- }
- break;
- }
- return ::DefWindowProc(hwnd, msg, wParam, lParam);
- }