[转]DrawPrimitive 详解Direct3DDevice8

Direct3DDevice8 函数

05-39  DrawPrimitive 详解

 

 

  费了好大的劲,终于搞清楚 DirectX 3D 三维图像中 DrawPrimitive 的用法(自嘲:未必)。
  DrawPrimitive 是 Direct3DDevice8 对象的一个绘图方法:根据指定的顶点数组,绘制指定的图形:点、线,或三角形,从而组成复杂的图像。在 VB 中用法如下:
    DrawPrimitive PrimitiveType, StartVertex, PrimitiveCount
  参数说明:
  StartVertex:Long 类型,指定从哪个顶点开始绘制图形
  PrimitiveCount:Long 类型,指定绘制的图形个数
  PrimitiveType: CONST_D3DPRIMITIVETYPE 枚举类型,要绘制的图形类型。共有 6 个取值:
  1 D3DPT_PointList显示所有顶点



   2 D3DPT_LineList 不相连的线段,线段的总数 = Int(顶点数/2)
   顶点连线的方式:0-1,2-3,4-5,等


 

  3 D3DPT_LineStrip首尾相连的线段,线段的总数 = 顶点数-1
  顶点连线的方式:0-1,1-2,2-3,3-4,4-5,等


 

  4 D3DPT_TriangleList 不相连的三角形,三角形总数 = Int(顶点数/3)
  三角形组成的方式:0-1-2,3-4-5,等。每个三角形的的第三个点应在前两个点顺时针方向的一侧,否则会忽略此三角形。
  下图中的0-1-2三角形,假定一人站在点0,面向点1,点2在顺时针方向(右方),所以此三角形会正确绘出。
    3-4-5三角形中,站在点3,面向点4,点5在逆时针方向(上方),所以此三角形会被忽略。


 

  5 D3DPT_TriangleStrip 以一条边为公共边的三角形,三角形总数 = 顶点数-2
  三角形组成的方式:0-1-2(顺),1-2-3(逆),2-3-4(顺),3-4-5(逆),4-5-6(顺),等
  第1个三角形的点遵循顺时针原则,第2个三角形的点遵循逆时针原则,以此类推,第5个三角形应遵循顺时针原则。下图中点6在逆时针方向上,所以被忽略。


 

  6 D3DPT_TriangleList 以顶点0为圆心,顺时针扇形排列的一系列三角形,三角形总数 = 顶点数-2。
  三角形组成的方式:0-1-2,0-2-3,0-4-5,等。三角形的点全部遵循顺时针原则,否则忽略此三角形。
  下图三角形 0-6-7 中的点7在逆时针方向上,所以被忽略。0-7-8 符合顺时针原则,所以也会正确绘出。


注意:以上讨论基于“消隐设置”为默认值的情况,可更改“消隐设置”的状态:
   MyDirect3DDevice8.SetRenderState D3DRS_CULLMODE, nMode
nMode的取值:
  1 D3DCULL_NONE 无背面消隐,不忽略任何三角形
  2 D3DCULL_CW   顺时针消隐,与上文所说效果相反
  3 D3DCULL_CCW  逆时针消隐(默认,与上文所说效果同)

演示程序截图:



用作纹理贴图的图片文件:纹理图1.jpg

 

原文:http://hi.baidu.com/100bd/item/516e52d9751d1356d63aae12

转载于:https://www.cnblogs.com/moher/p/3722674.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
directx 3d 实例#include #include "d3d9.h" #include "d3dx9.h" #include "Direct3D.h" // Direct3D objects IDirect3D9 *g_pD3D = NULL; IDirect3DDevice9 *g_pD3DDevice = NULL; // Sky vertex structure, fvf, vertex buffer, and texture typedef struct { float x, y, z, rhw; float u, v; } sSkyVertex; #define SKYFVF (D3DFVF_XYZRHW | D3DFVF_TEX1) IDirect3DVertexBuffer9 *g_SkyVB = NULL; IDirect3DTexture9 *g_SkyTexture = NULL; // Land and water meshes D3DXMESHCONTAINER_EX *g_WaterMesh = NULL; D3DXMESHCONTAINER_EX *g_LandMesh = NULL; // Window class and caption text char g_szClass[] = "TextureTransformationClass"; char g_szCaption[] = "Texture Transformation Demo by Jim Adams"; // Function prototypes int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow); long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); void Matrix4x4To3x3(D3DXMATRIX *matOut, D3DXMATRIX *matIn); BOOL DoInit(HWND hWnd, BOOL Windowed = TRUE); void DoShutdown(); void DoFrame(); int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow) { WNDCLASSEX wcex; MSG Msg; HWND hWnd; // Initialize the COM system CoInitialize(NULL); // Create the window class here and register it wcex.cbSize = sizeof(wcex); wcex.style = CS_CLASSDC; wcex.lpfnWndProc = WindowProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInst; wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = NULL; wcex.lpszMenuName = NULL; wcex.lpszClassName = g_szClass; wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wcex)) return FALSE; // Create the main window hWnd = CreateWindow(g_szClass, g_szCaption, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, 0, 0, 640, 480, NULL, NULL, hInst, NULL); if(!hWnd) return FALSE; ShowWindow(hWnd, SW_NORMAL); UpdateWindow(hWnd); // Call init function and enter message pump if(DoInit(hWnd) == TRUE) { // Start message pump, waiting for user to exit ZeroMemory(&Msg, sizeof(MSG)); while(Msg.message != WM_QUIT) { if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } // Render a single frame DoFrame(); } } // Call shutdown DoShutdown(); // Unregister the window class UnregisterClass(g_szClass, hInst); // Shut down the COM system CoUninitialize(); return 0; } long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, \ WPARAM wParam, LPARAM lParam) { // Only handle window destruction messages switch(uMsg) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0; } BOOL DoInit(HWND hWnd, BOOL Windowed) { // Initialize Direct3D InitD3D(&g_pD3D, &g_pD3DDevice, hWnd); // Load the land and water meshes LoadMesh(&g_WaterMesh, g_pD3DDevice, "..\\Data\\Water.x", "..\\Data\\"); LoadMesh(&g_LandMesh, g_pD3DDevice, "..\\Data\\Land.x", "..\\Data\\"); // Create the sky backdrop sSkyVertex SkyVerts[4] = { { 0.0f, 0.0, 1.0, 1.0f, 0.0f, 0.0f }, { 640.0f, 0.0, 1.0, 1.0f, 1.0f, 0.0f }, { 0.0f, 480.0, 1.0, 1.0f, 0.0f, 1.0f }, { 640.0f, 480.0, 1.0, 1.0f, 1.0f, 1.0f } }; g_pD3DDevice->CreateVertexBuffer(sizeof(SkyVerts), D3DUSAGE_WRITEONLY, SKYFVF, D3DPOOL_DEFAULT, &g_SkyVB, NULL); char *Ptr; g_SkyVB->Lock(0,0, (void**)&Ptr, 0); memcpy(Ptr, SkyVerts, sizeof(SkyVerts)); g_SkyVB->Unlock(); D3DXCreateTextureFromFile(g_pD3DDevice, "..\\Data\\Sky.bmp", &g_SkyTexture); // Setup a light D3DLIGHT9 Light; ZeroMemory(&Light, sizeof(Light)); Light.Diffuse.r = Light.Diffuse.g = Light.Diffuse.b = Light.Diffuse.a = 1.0f; Light.Type = D3DLIGHT_DIRECTIONAL; D3DXVECTOR3 vecLight = D3DXVECTOR3(-1.0f, -1.0f, 0.5f); D3DXVec3Normalize(&vecLight, &vecLight); Light.Direction = vecLight; g_pD3DDevice->SetLight(0, &Light); g_pD3DDevice->LightEnable(0, TRUE); // Start playing a waterfall sound PlaySound("..\\Data\\Waterfall.wav", NULL, SND_ASYNC | SND_LOOP); return TRUE; } void DoShutdown() { // Stop playing an ocean sound PlaySound(NULL, NULL, 0); // Free meshes delete g_WaterMesh; g_WaterMesh = NULL; delete g_LandMesh; g_LandMesh = NULL; // Release sky data ReleaseCOM(g_SkyVB); ReleaseCOM(g_SkyTexture); // Release D3D objects ReleaseCOM(g_pD3DDevice); ReleaseCOM(g_pD3D); } void DoFrame() { // Create and set the view transformation D3DXMATRIX matView; D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(360.0f, -170.0f, -430.0f), &D3DXVECTOR3(65.0f, 70.0f, -15.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView); // Clear the device and start drawing the scene g_pD3DDevice->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0,0,64,255), 1.0, 0); if(SUCCEEDED(g_pD3DDevice->BeginScene())) { // Set identity matrix for world transformation D3DXMATRIX matWorld; D3DXMatrixIdentity(&matWorld); g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld); // Draw the sky g_pD3DDevice->SetFVF(SKYFVF); g_pD3DDevice->SetStreamSource(0, g_SkyVB, 0, sizeof(sSkyVertex)); g_pD3DDevice->SetTexture(0, g_SkyTexture); g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); // Enable lighting g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE); // Draw the land meshes DrawMeshes(g_LandMesh); // Setup the texture transformation float TimeFactor = (float)(timeGetTime() / 500.0f); D3DXMATRIX matTexture; D3DXMatrixTranslation(&matTexture, 0.0f, -TimeFactor, 0.0f); Matrix4x4To3x3(&matTexture, &matTexture); g_pD3DDevice->SetTransform(D3DTS_TEXTURE0, &matTexture); g_pD3DDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2); // Draw the water (using alpha blending) DrawMeshes(g_WaterMesh); g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); // Disable lighting g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); // Turn off texture transformations g_pD3DDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); // End the scene g_pD3DDevice->EndScene(); } // Present the scene to the user g_pD3DDevice->Present(NULL, NULL, NULL, NULL); } void Matrix4x4To3x3(D3DXMATRIX *matOut, D3DXMATRIX *matIn) { matOut->_11 = matIn->_11; // Copy over 1st row matOut->_12 = matIn->_12; matOut->_13 = matIn->_13; matOut->_14 = 0.0f; matOut->_21 = matIn->_21; // Copy over 2nd row matOut->_22 = matIn->_22; matOut->_23 = matIn->_23; matOut->_24 = 0.0f; matOut->_31 = matIn->_41; // Copy bottom row matOut->_32 = matIn->_42; // used for translation matOut->_33 = matIn->_43; matOut->_34 = 0.0f; matOut->_41 = 0.0f; // Clear the bottom row matOut->_42 = 0.0f; matOut->_43 = 0.0f; matOut->_44 = 1.0f; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值