SetupRotation()函数我觉得是在render()里调用的;但没在render()里面出现,却调用了
#include <windows.h>#include <d3d9.h>
#include "d3dx9.h"
#include "mmsystem.h"
struct CUSTOMVERTEX
{
FLOAT x, y, z; // The transformed position for the vertex.
DWORD colour; // The vertex colour.
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}
BOOL InitWindow(HINSTANCE hInstance, int nCmdShow,char *pszClassName );
HRESULT InitD3D(HWND hWnd);
HRESULT InitVertexBuffer();
void Render();
void CleanUp();
void SetupRotation();
void SetupPerspective(HWND hWnd);
void SetupViewer();
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL; // Buffer to hold vertices
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
//Register the window class
MSG msg;
char *pszClassName = "DrawPrimtive";
if (!InitWindow(hInstance,nCmdShow,pszClassName))
{
CleanUp();
return FALSE;
}
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
}
}
CleanUp();
return msg.wParam ;
}
BOOL InitWindow(HINSTANCE hInstance,int nCmdShow,char *pszClassName)
{
WNDCLASS wc;
HWND hWnd;
wc.style = CS_HREDRAW | CS_VREDRAW ;//NULL;
wc.lpfnWndProc =WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);//CreateSolidBrush(RGB(100,0,0));
wc.lpszMenuName = NULL;
wc.lpszClassName = pszClassName;//"DrawPrimtive";
RegisterClass(&wc);
hWnd = CreateWindow(pszClassName,//"DrawPrimtive",
"DrawPrimtive",
//WS_POPUP|WS_MAXIMIZE,0,0,
WS_OVERLAPPEDWINDOW,0,0,
//GetSystemMetrics(SM_CXSCREEN),
//GetSystemMetrics(SM_CYSCREEN),
500,
500,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd) return FALSE;
if (SUCCEEDED(InitD3D(hWnd)))
{
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
if(SUCCEEDED(InitVertexBuffer()))
{
//Start game running: Enter the game loop
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
HRESULT InitD3D(HWND hWnd)
{
//First of all, create the main D3D object. If it is created successfully we
//should get a pointer to an IDirect3D8 interface.
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_pD3D == NULL)
{
return E_FAIL;
}
//Get the current display mode
D3DDISPLAYMODE d3ddm;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
//Create a structure to hold the settings for our device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
//Fill the structure.
//We want our program to be windowed, and set the back buffer to a format
//that matches our current display mode
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
D3DCAPS9 caps;
g_pD3D ->GetDeviceCaps(
D3DADAPTER_DEFAULT, // 主显示设备
D3DDEVTYPE_HAL, // 硬件处理的方式
&caps //返回填充后的D3DCAPS9结构,包含主显示设备的能力
); // 是否可以使用硬件顶点处理?
int vp = 0;
if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
{
// 是,支持硬件顶点处理
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else
{
// 不,只能用软件顶点处理
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
//Create a Direct3D device.
if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
vp,
&d3dpp,
&g_pD3DDevice)))
{
return E_FAIL;
}
//g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
//g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );//让逆时针的三角形现实出来
g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );//
SetupPerspective(hWnd);
SetupViewer();
return S_OK;
}
HRESULT InitVertexBuffer()
{
VOID* pVertices;
//设置三角形的顶点
CUSTOMVERTEX cvVertices[] =
{
//Top Face
{-1.0f , 1.0f , -1.0f, D3DCOLOR_XRGB(0, 0, 255)}, //Vertex 0 - Blue
{-1.0f , 1.0f , 1.0f, D3DCOLOR_XRGB(255, 0, 0)}, //Vertex 1 - Red
{1.0f , 1.0f , -1.0f, D3DCOLOR_XRGB(255, 0, 0)}, //Vertex 2 - Red
{1.0f , 1.0f , 1.0f, D3DCOLOR_XRGB(0, 255, 0)}, //Vertex 3 - Green
//Face 1
//请添加相应的顶点数据
{-1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0)}, //Vertex 4
{-1.0f, 1.0f, -1.0f,D3DCOLOR_XRGB(0, 0, 255)}, //Vertex 5
{1.0f, -1.0f, -1.0f,D3DCOLOR_XRGB(0, 255, 0)}, //Vertex 6
{1.0f, 1.0f, -1.0f,D3DCOLOR_XRGB(255, 0, 0)}, //Vertex 7
//Face 2
//请添加相应的顶点数据
{1.0f, -1.0f, 1.0f,D3DCOLOR_XRGB(0, 0, 255)}, //Vertex 8
{1.0f, 1.0f, 1.0f,D3DCOLOR_XRGB(0, 255, 0)}, //Vertex 9
//Face 3
//请添加相应的顶点数据
{-1.0f, -1.0f, 1.0f,D3DCOLOR_XRGB(0, 255, 0)}, //Vertex 10
{-1.0f, 1.0f, 1.0f,D3DCOLOR_XRGB(255, 0, 0)}, //Vertex 11
//Face 4
//请添加相应的顶点数据
{-1.0f, -1.0f, -1.0f,D3DCOLOR_XRGB(255, 0, 0)}, //Vertex 12
{-1.0f, 1.0f, -1.0f,D3DCOLOR_XRGB(0, 0, 255)}, //Vertex 13
//Bottom Face
//请添加相应的顶点数据
{1.0f, -1.0f, -1.0f,D3DCOLOR_XRGB(0, 255, 0)}, //Vertex 14
{1.0f, -1.0f, 1.0f,D3DCOLOR_XRGB(0, 0, 255)}, //Vertex 15
{-1.0f, -1.0f, -1.0f,D3DCOLOR_XRGB(255, 0, 0)}, //Vertex 16
{-1.0f, -1.0f, 1.0f,D3DCOLOR_XRGB(0, 255, 0)} //Vertex 17
};
//为D3D设备创建顶点缓冲
if(FAILED(g_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUSTOMVERTEX),
0,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&g_pVertexBuffer,
NULL)))
{
return E_FAIL;
}
//获得指向顶点缓冲的指针并且锁定准备写入数据
if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), &pVertices, 0 )))
{
return E_FAIL;
}
//把设定好的顶点数据复制入顶点缓冲的地址
memcpy(pVertices, cvVertices, sizeof(cvVertices));
//解锁
g_pVertexBuffer->Unlock();
return S_OK;
}
void Render()
{
if(g_pD3DDevice)
{
//清屏
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,120,0), 1.0f, 0);
//开始场景渲染
if (SUCCEEDED(g_pD3DDevice->BeginScene()))
{
//渲染三角形
g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer,0, sizeof(CUSTOMVERTEX));
g_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
//*************原来的一条渲染语句改成三条************
// g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //Top
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8); //Sides
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2); //Bottom
//结束场景渲染
g_pD3DDevice->EndScene();
}
//后备表面呈现出来
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
}
void CleanUp()
{
SafeRelease(g_pVertexBuffer);
SafeRelease(g_pD3DDevice);
SafeRelease(g_pD3D);
}
//The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
switch (wParam)
{
case VK_ESCAPE:
//User has pressed the escape key, so quit
DestroyWindow(hWnd);
return 0;
case 0x30:
case 0x31:
case 0x32:
case 0x33:
case 0x34:
case 0x35:
case 0x36:
return 0;
break;
}
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//世界变换,实际上是个旋转变换
void SetupRotation()
{
D3DXMATRIX matWorld, matWorldX, matWorldY, matWorldZ;
//创建三个分别绕x,y,z轴变换矩阵
D3DXMatrixRotationX(&matWorldX, timeGetTime()/360.0f);
D3DXMatrixRotationY(&matWorldY, timeGetTime()/360.0f);
D3DXMatrixRotationZ(&matWorldZ, timeGetTime()/360.0f);
//三个矩阵相乘得到最终的旋转矩阵
D3DXMatrixMultiply(&matWorld, &matWorldX, &matWorldY);
D3DXMatrixMultiply(&matWorld, &matWorld, &matWorldZ);
//应用变换
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}
//投影变换
void SetupPerspective(HWND hWnd)
{
double dx;
double dy;
RECT rect;
double dAspect;
GetWindowRect(hWnd,&rect);
//纵横比
dAspect = (double)(rect.right - rect.left) / (double)(rect.bottom - rect.top);
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, dAspect , 1.0f, 600.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
//观察变换
void SetupViewer()
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f,-5.0f), //观察点
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), //视线点
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //观察上方向
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}
为什么没出现
void Render()
{
·······
SetupRotation();
}
这种方式呢?究竟在那里调用?
[ 本帖最后由 q345918550q 于 2010-11-2 23:15 编辑 ]