#include
#include
#include
#pragma warning( disable : 4996 ) // disable deprecated warning
#include
#pragma warning( default : 4996 )
#include
#pragma warning( default : 4996 )
#include
#pragma warning( default : 4996 )
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // 设备指针
//定义一个顶点缓冲区的指针
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold luoshao_8_21_sanjiao
LPDIRECT3DTEXTURE9 g_pTexture = NULL;//定义一个纹理指针
// 定义一个顶点的结构
struct CUSTOMVERTEX
{
//已经经过变换的顶点坐标(x/rhw,y/rhw,z/rhw),变换通常表示维度加一
FLOAT x, y, z;//, rhw; // The transformed position for the vertex
//DWORD color; // 漫反射的颜色
FLOAT tu, tv;//纹理坐标
};
// Our custom FVF, which describes our custom vertex structure
//漫反射 D3DFVF_DIFFUSE
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)//一个纹理
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D(HWND hWnd)
{
// Create the D3D object.
if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// Create the D3DDevice
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice)))
{
return E_FAIL;
}
// Device state would normally be set here
//关闭背面拣选
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
//开启灯光
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
//g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitGeometry()
// 数据准备阶段
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
// 定义一个顶点的数据结构,并初始化
CUSTOMVERTEX luoshao_8_21_sanjiao[] =
{
{ -300.0f, 300.0f, 0.0f, 0, 0 },
{ 300.0f, 300.0f, 0.0f, 1, 0 },
{ -300.0f, -300.0f, 0.0f, 0, 1 },//三角形带画两个三角形拼成正方形(保证第一个三角形为顺时针,并且前一个三角形的后两点作为后一个三角形的起始点)
{ 300.0f, -300.0f, 0.0f, 1, 1 },
};
// Use D3DX to create a texture from a file based image
if (FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "banana.jpg", &g_pTexture)))
{
// If texture is not in current folder, try parent folder
if (FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "..\\banana.jpg", &g_pTexture)))
{
MessageBox(NULL, "Could not find banana.jpg", "luoshao_8_21_sanjiao.exe", MB_OK);
return E_FAIL;
}
}
// 创建一个顶点缓冲区
if (FAILED(g_pd3dDevice->CreateVertexBuffer(sizeof(luoshao_8_21_sanjiao),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL)))
{
return E_FAIL;
}
// gain access to the luoshao_8_21_sanjiao. This mechanism is required becuase vertex
// 把顶点数组的数据拷贝到顶点缓冲区
VOID* pluoshao_8_21_sanjiao;
if (FAILED(g_pVB->Lock(0, sizeof(luoshao_8_21_sanjiao), (void**)&pluoshao_8_21_sanjiao, 0)))
return E_FAIL;
memcpy(pluoshao_8_21_sanjiao, luoshao_8_21_sanjiao, sizeof(luoshao_8_21_sanjiao));
g_pVB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if (g_pVB != NULL)
g_pVB->Release();
if (g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if (g_pD3D != NULL)
g_pD3D->Release();
}
VOID SetupMatrices()
{
// For our world matrix, we will just rotate the object about the y-axis.
D3DXMATRIXA16 matWorld;
UINT iTime = timeGetTime() % 1000;
FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
//获取一个平移矩阵
//D3DXMATRIXA16 mat;
//D3DXMatrixTranslation(&mat, 5, 0, 0);
//D3DXMatrixRotationY(&matWorld, fAngle);
//mat *= matWorld;
//D3DXMatrixTranslation(&matWorld, 3, 0, 0);
//mat *= matWorld;
//D3DXMatrixIdentity(&matWorld);
//g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
D3DXVECTOR3 vEyePt(0.0f, 0.0f, -1000.0f);
D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f);
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIXA16 matProj;
//夹角,纵横比,近平面,远屏幕
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f, 1.0f, 1100.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
VOID Render()
{
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
// Begin the scene
if (SUCCEEDED(g_pd3dDevice->BeginScene()))
{
SetupMatrices();
g_pd3dDevice->SetTexture(0, g_pTexture);//设置纹理
g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
//设置资源流
g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
//三角形列(D3DPT_TRIANGLELIST) 起始 终点 每三个点 一个三角形//三角形带 四个点两个三角形
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);//三角形带画的
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
Cleanup();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx(&wc);
// Create the application's window
HWND hWnd = CreateWindow("D3D Tutorial", "D3D Tutorial 02: luoshao_8_21_sanjiao",
WS_OVERLAPPEDWINDOW, 100, 100, 600, 600,
NULL, NULL, wc.hInstance, NULL);
// Initialize Direct3D
if (SUCCEEDED(InitD3D(hWnd)))
{
// Create the vertex buffer
if (SUCCEEDED(InitGeometry()))
{
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
// Enter the message loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
Render();
}
}
}
UnregisterClass("D3D Tutorial", wc.hInstance);
return 0;
}