cylinderShader.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:8k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "zfxd3dUtility.h"
- #include <stdio.h>
- // D3D对象接口
- IDirect3D9* g_pd3d;
- // D3D设备接口
- IDirect3DDevice9* g_pd3dDevice;
- // 自定义顶点结构体
- typedef struct tagCUSTOMVERTEX{
- D3DXVECTOR3 position; // The position of the vertex.
- D3DCOLOR color; // The color
- }CUSTOMVERTEX;
- // 自定义顶点格式
- #define D3DFVF_CUSTOMVERTEX D3DFVF_XYZ|D3DFVF_DIFFUSE
- IDirect3DVertexBuffer9* g_pVB1; //Vertex Buffer, 圆柱体的侧面
- IDirect3DVertexBuffer9* g_pVB2; //Vertex Buffer, 圆柱体的上表面
- IDirect3DVertexBuffer9* g_pVB3; //Vertex Buffer, 圆柱体的上表面
- BOOL CreateVertexBuf1( IDirect3DVertexBuffer9* & g_pVB);
- BOOL CreateVertexBuf2( IDirect3DVertexBuffer9* & g_pVB);
- BOOL CreateVertexBuf3( IDirect3DVertexBuffer9* & g_pVB);
- // 字体
- ID3DXFont* g_pFont = NULL;
- D3DXMATRIX g_proj;
- LPDIRECT3DVERTEXSHADER9 vertexShader;
- // 建立渲染环境
- bool Setup()
- {
- HRESULT hr;
- if( !CreateVertexBuf1(g_pVB1))
- {
- MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
- return false;
- }
- if( !CreateVertexBuf2(g_pVB2))
- {
- MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
- return false;
- }
- if( !CreateVertexBuf3(g_pVB3))
- {
- MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
- return false;
- }
- // 设定渲染状态
- g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
- // 设置投影矩阵
- D3DXMatrixPerspectiveFovLH(
- &g_proj,
- D3DX_PI * 0.25f, // 45 - degree
- 1,
- 1.0f,
- 1000.0f);
- LPD3DXBUFFER pShaderBuffer, pErrorBuffer;
- hr = D3DXAssembleShaderFromFile("vertex.vsd", 0, 0, D3DXSHADER_DEBUG, &pShaderBuffer, &pErrorBuffer);
- hr = g_pd3dDevice->CreateVertexShader((DWORD*)(pShaderBuffer->GetBufferPointer()), &vertexShader);
- 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 = rect.top = 10;
- rect.bottom = rect.top + 30;
- rect.right = rect.left + 260;
- sprintf(str, "Current FPS:%d, please press Q to toggle WireFrame mode,
- and press LButton to enable a different Light source", nFPS);
- g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
- return true;
- }
- bool Display()
- {
- // 获取两次进入函数入口的时间间隔
- static float lastTime = (float)timeGetTime();
- float curTime = (float)timeGetTime();
- float timeDelta = (curTime - lastTime)*0.001f;
- lastTime = curTime;
- // 设定视口矩阵,你可以通过按下上下左右键改变视角
- static float angle = (3.0f * D3DX_PI) / 2.0f;
- static float height = 5.0f;
- if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
- angle -= 0.5f * timeDelta;
- if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
- angle += 0.5f * timeDelta;
- if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
- height += 1.5f * timeDelta;
- if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
- height -= 1.5f * timeDelta;
- D3DXVECTOR3 eyept( cosf(angle) * 5.0f, height, sinf(angle) * 5.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);
- D3DXMATRIX matWorld, matWorldViewProj, matT;
- D3DXMatrixIdentity(&matWorld);
- D3DXMatrixMultiply(&matWorldViewProj, &g_proj, &view_mat);
- matWorldViewProj = matWorld * view_mat * g_proj;
- D3DXMatrixTranspose(&matT, &matWorldViewProj);
- g_pd3dDevice->SetVertexShaderConstantF(0, (float*)&matT, 4);
- // 设定灯光
- g_pd3dDevice->SetVertexShader(vertexShader);
- // 绘制场景
- g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
- g_pd3dDevice->BeginScene();
- // 输出顶点流
- g_pd3dDevice->SetStreamSource(0, g_pVB1, 0, sizeof(CUSTOMVERTEX));
- g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 98);
- g_pd3dDevice->SetStreamSource(0, g_pVB2, 0, sizeof(CUSTOMVERTEX));
- g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 49);
- g_pd3dDevice->SetStreamSource(0, g_pVB3, 0, sizeof(CUSTOMVERTEX));
- g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 49);
- g_pd3dDevice->SetVertexShader(NULL);
- // 在屏幕上写字
- if(g_pFont)
- DrawTextX();
- g_pd3dDevice->EndScene();
- g_pd3dDevice->Present(0, 0, 0, 0);
- return true;
- }
- bool CleanUp()
- {
- g_pd3dDevice->SetVertexShader(NULL);
- SAFE_RELEASE(vertexShader);
- SAFE_RELEASE(g_pd3dDevice);
- SAFE_RELEASE(g_pd3d);
- return true;
- }
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int showCmd)
- {
- // 应用程序初始化
- if(!InitD3DApp(hInstance, true/*true代表窗口模式*/,
- D3DDEVTYPE_HAL, &g_pd3d, &g_pd3dDevice, "light"/*窗口标题*/, 1024, 768))
- {
- MessageBoxW(0, L"应用程序初始化失败", 0, 0);
- return 0;
- }
- // 创建字体
- if(!InitDXFont(g_pd3dDevice, &g_pFont, "Times New Roman"))
- g_pFont = NULL;
- Setup();
- // 进入消息循环
- MSG msg;
- ZeroMemory(&msg, sizeof(msg));
- while(msg.message != WM_QUIT)
- {
- if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- Display();
- }
- }
- 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_LBUTTONDOWN:
- // g_nlight_Enabled++;
- break;
- case WM_KEYDOWN:
- if( wParam == VK_ESCAPE )
- ::DestroyWindow(hwnd);
- break;
- case WM_CHAR:
- if(wParam == 'q')
- bWireFrame = !bWireFrame;
- if(bWireFrame)
- g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
- else
- g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
- break;
- }
- return ::DefWindowProc(hwnd, msg, wParam, lParam);
- }
- //100 Vertices created.........
- BOOL CreateVertexBuf1(IDirect3DVertexBuffer9* & g_pVB)
- {
- CUSTOMVERTEX pVertices[100];
- ZeroMemory( pVertices, 100*sizeof( CUSTOMVERTEX ) );
- for(int i=0;i<50;i++)
- {
- FLOAT alpha = (2*D3DX_PI*i)/49;
- pVertices[2*i].position = D3DXVECTOR3( sinf(alpha), 1.0f, cosf(alpha));
- pVertices[2*i].color = 0x88ffff00;
- pVertices[2*i+1].position = D3DXVECTOR3( sinf(alpha),-1.0f, cosf(alpha));
- pVertices[2*i+1].color = 0x88ff00ff;
- }
- if( FAILED(g_pd3dDevice->CreateVertexBuffer( 100*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
- , D3DPOOL_DEFAULT, &g_pVB, NULL)))
- return FALSE;
- void* pData;
- g_pVB->Lock(0, 100*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
- memcpy(pData, pVertices, 100*sizeof(CUSTOMVERTEX));
- g_pVB->Unlock();
- return TRUE;
- }
- BOOL CreateVertexBuf2(IDirect3DVertexBuffer9* & g_pVB)
- {
- CUSTOMVERTEX pVertices[51];
- ZeroMemory( pVertices, 51*sizeof( CUSTOMVERTEX ) );
- pVertices[0].position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f);
- pVertices[0].color = 0x8800ffff;
- for(int i=0;i<50;i++)
- {
- FLOAT alpha = (2*D3DX_PI*i)/49;
- pVertices[i+1].position = D3DXVECTOR3( sinf(alpha), 1.0f, cosf(alpha));
- pVertices[i+1].color = 0x88ffffff;
- }
- if( FAILED(g_pd3dDevice->CreateVertexBuffer( 51*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
- , D3DPOOL_DEFAULT, &g_pVB, NULL)))
- return FALSE;
- void* pData;
- g_pVB->Lock(0, 51*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
- memcpy(pData, pVertices, 51*sizeof(CUSTOMVERTEX));
- g_pVB->Unlock();
- return TRUE;
- }
- BOOL CreateVertexBuf3(IDirect3DVertexBuffer9* & g_pVB)
- {
- CUSTOMVERTEX pVertices[51];
- ZeroMemory( pVertices, 51*sizeof( CUSTOMVERTEX ) );
- pVertices[0].position = D3DXVECTOR3( 0.0f, -1.0f, 0.0f);
- pVertices[0].color = 0x88ffff00;
- for(int i=0;i<50;i++)
- {
- FLOAT alpha = (2*D3DX_PI*i)/49;
- pVertices[i+1].position = D3DXVECTOR3( sinf(alpha), -1.0f, cosf(alpha));
- pVertices[i+1].color = 0x88ffffff;
- }
- if( FAILED(g_pd3dDevice->CreateVertexBuffer( 51*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
- , D3DPOOL_DEFAULT, &g_pVB, NULL)))
- return FALSE;
- void* pData;
- g_pVB->Lock(0, 51*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
- memcpy(pData, pVertices, 51*sizeof(CUSTOMVERTEX));
- g_pVB->Unlock();
- return TRUE;
- }