DirectX 3D 之C 开发

DirectX 3D 是一种图形渲染 API,用于在 Microsoft Windows 中的计算机应用程序中进行三维图形的渲染。它提供了一组基于硬件加速的 API,以及用于访问计算机上图形硬件的底层接口。使用 DirectX 3D,开发人员可以创建具有复杂的纹理、灯光效果和动画效果的三维模型,并将它们嵌入到其应用程序中。

C 语言是一种常见的编程语言,它被广泛用于系统级编程和嵌入式开发。在 DirectX 3D 开发中,C 语言可以被用来编写应用程序,包括图形渲染和实现用户交互。下面将介绍一些使用C开发DirectX 3D的方法以及一些示例。

1. 创建设备

使用C语言创建DirectX 3D设备,需要使用Microsoft提供的 DirectX SDK。首先是必要的引入Direct3D库,然后我们创建一个Direct3D对象和一个设备对象。

```

#include

LPDIRECT3D9 pD3D = NULL;

LPDIRECT3DDEVICE9 pd3dDevice = NULL;

BOOL InitD3D(HWND hWnd){

pD3D = Direct3DCreate9(D3D_SDK_VERSION);

if(pD3D == NULL) return FALSE;

ZeroMemory(&d3dpp, sizeof(d3dpp));

d3dpp.Windowed = FALSE;

d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

d3dpp.BackBufferCount = 1;

d3dpp.BackBufferHeight = SCREEN_HEIGHT;

d3dpp.BackBufferWidth = SCREEN_WIDTH;

d3dpp.hDeviceWindow = hWnd;

if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,

D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice))) {

return FALSE;

}

return TRUE;

}

```

2. 创建顶点数据

在DirectX 3D中,顶点被定义为一系列位置、颜色等属性的集合。我们可以使用C语言创建一个顶点缓冲区并存储这些顶点。以下是一个简单的顶点结构体:

```

typedef struct _VERTEX {

FLOAT x, y, z;

DWORD color;

} VERTEX, * LPVERTEX;

```

然后,我们可以使用以下代码创建一个顶点缓存:

```

LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL;

pd3dDevice->CreateVertexBuffer( 4 * sizeof(VERTEX), 0,

D3DFVF_XYZ | D3DFVF_DIFFUSE, D3DPOOL_MANAGED, &pVertexBuffer, NULL);

VERTEX vertices[] = {

{ 0.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0) },

{ 1.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0) },

{ 0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255) },

{ 1.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255) },

};

VOID* pVoid;

pVertexBuffer->Lock(0, 0, (void**)&pVoid, 0);

memcpy(pVoid, vertices, sizeof(vertices));

pVertexBuffer->Unlock();

```

该代码段创建了一个包含四个顶点的缓存区,并将其锁定以填充数据。

3. 绘制图形

使用C语言,我们可以使用以下代码将顶点缓存中的数据绘制到屏幕上:

```

pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

pd3dDevice->BeginScene();

pd3dDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);

pd3dDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(VERTEX));

pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

pd3dDevice->EndScene();

```

这些代码清空屏幕、开始场景、设置缓存、绘制图形和结束场景。

4. 加载纹理

在游戏中,通常需要将纹理应用于三维模型的表面。使用C语言,我们可以加载和应用纹理,以与顶点交互。

```

LPDIRECT3DTEXTURE9 pTexture;

HRESULT hr = D3DXCreateTextureFromFile(pd3dDevice, "texture.png", &pTexture);

if (FAILED(hr))

{

//处理加载失败的情况

}

pd3dDevice->SetTexture(0, pTexture);

```

该代码段使用D3DXCreateTextureFromFile方法从文件中加载纹理,并将其应用于设备上下文中。

5. 示例

以下是一个使用C语言和DirectX 3D创建的简单示例,它显示一个旋转的立方体,并允许用户交互。该示例使用了以上介绍的技术,涉及到对象创建、顶点缓存、纹理加载和应用,如下代码所示:

```

#include

#include

#include "d3dx9.h"

#pragma comment (lib, "d3d9.lib")

#pragma comment (lib, "d3dx9.lib")

#define SCREEN_WIDTH 640

#define SCREEN_HEIGHT 480

LPDIRECT3D9 g_pD3D = NULL;

LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;

LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL;

LPDIRECT3DTEXTURE9 g_pTexture = NULL;

D3DXMATRIX g_matWorld;

D3DXMATRIX g_matView;

D3DXMATRIX g_matProj;

FLOAT g_fAngle = 0.0f;

typedef struct _VERTEX {

FLOAT x, y, z;

FLOAT u, v;

}VERTEX, *LPVERTEX;

LPVERTEX g_pVertices;

BOOL InitDirect3D(HWND hWnd)

{

g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

if (g_pD3D == NULL)

return FALSE;

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));

d3dpp.Windowed = TRUE;

d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

d3dpp.BackBufferWidth = SCREEN_WIDTH;

d3dpp.BackBufferHeight = SCREEN_HEIGHT;

d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

d3dpp.EnableAutoDepthStencil = TRUE;

d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,

D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))

return FALSE;

return TRUE;

}

VOID Cleanup()

{

if (g_pVertexBuffer != NULL) g_pVertexBuffer->Release();

if (g_pTexture != NULL) g_pTexture->Release();

if (g_pd3dDevice != NULL) g_pd3dDevice->Release();

if (g_pD3D != NULL) g_pD3D->Release();

}

VOID InitVertices()

{

g_pd3dDevice->CreateVertexBuffer(24 * sizeof(VERTEX), D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_TEX1, D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL);

g_pVertexBuffer->Lock(0, 0, (void**)&g_pVertices, 0);

g_pVertices[0].x = -1.0f; g_pVertices[0].y = 1.0f; g_pVertices[0].z = -1.0f; g_pVertices[0].u = 0.0f; g_pVertices[0].v = 0.0f;

g_pVertices[1].x = 1.0f; g_pVertices[1].y = 1.0f; g_pVertices[1].z = -1.0f; g_pVertices[1].u = 1.0f; g_pVertices[1].v = 0.0f;

g_pVertices[2].x = 1.0f; g_pVertices[2].y = -1.0f; g_pVertices[2].z = -1.0f; g_pVertices[2].u = 1.0f; g_pVertices[2].v = 1.0f;

g_pVertices[3].x = -1.0f; g_pVertices[3].y = -1.0f; g_pVertices[3].z = -1.0f; g_pVertices[3].u = 0.0f; g_pVertices[3].v = 1.0f;

g_pVertices[4].x = -1.0f; g_pVertices[4].y = 1.0f; g_pVertices[4].z = 1.0f; g_pVertices[4].u = 0.0f; g_pVertices[4].v = 0.0f;

g_pVertices[5].x = 1.0f; g_pVertices[5].y = 1.0f; g_pVertices[5].z = 1.0f; g_pVertices[5].u = 1.0f; g_pVertices[5].v = 0.0f;

g_pVertices[6].x = 1.0f; g_pVertices[6].y = -1.0f; g_pVertices[6].z = 1.0f; g_pVertices[6].u = 1.0f; g_pVertices[6].v = 1.0f;

g_pVertices[7].x = -1.0f; g_pVertices[7].y = -1.0f; g_pVertices[7].z = 1.0f; g_pVertices[7].u = 0.0f; g_pVertices[7].v = 1.0f;

g_pVertices[8].x = -1.0f; g_pVertices[8].y = 1.0f; g_pVertices[8].z = -1.0f; g_pVertices[8].u = 0.0f; g_pVertices[8].v = 0.0f;

g_pVertices[9].x = -1.0f; g_pVertices[9].y = 1.0f; g_pVertices[9].z = 1.0f; g_pVertices[9].u = 1.0f; g_pVertices[9].v = 0.0f;

g_pVertices[10].x = -1.0f; g_pVertices[10].y = -1.0f; g_pVertices[10].z = 1.0f; g_pVertices[10].u = 1.0f; g_pVertices[10].v = 1.0f;

g_pVertices[11].x = -1.0f; g_pVertices[11].y = -1.0f; g_pVertices[11].z = -1.0f; g_pVertices[11].u = 0.0f; g_pVertices[11].v = 1.0f;

g_pVertices[12].x = 1.0f; g_pVertices[12].y = 1.0f; g_pVertices[12].z = -1.0f; g_pVertices[12].u = 0.0f; g_pVertices[12].v = 0.0f;

g_pVertices[13].x = 1.0f; g_pVertices[13].y = 1.0f; g_pVertices[13].z = 1.0f; g_pVertices[13].u = 1.0f; g_pVertices[13].v = 0.0f;

g_pVertices[14].x = 1.0f; g_pVertices[14].y = -1.0f; g_pVertices[14].z = 1.0f; g_pVertices[14].u = 1.0f; g_pVertices[14].v = 1.0f;

g_pVertices[15].x = 1.0f; g_pVertices[15].y = -1.0f; g_pVertices[15].z = -1.0f; g_pVertices[15].u = 0.0f; g_pVertices[15].v = 1.0f;

g_pVertices[16].x = -1.0f; g_pVertices[16].y = 1.0f; g_pVertices[16].z = 1.0f; g_pVertices[16].u = 0.0f; g_pVertices[16].v = 0.0f;

g_pVertices[17].x = 1.0f; g_pVertices[17].y = 1.0f; g_pVertices[17].z = 1.0f; g_pVertices[17].u = 1.0f; g_pVertices[17].v = 0.0f;

g_pVertices[18].x = 1.0f; g_pVertices[18].y = 1.0f; g_pVertices[18].z = -1.0f; g_pVertices[18].u = 1.0f; g_pVertices[18].v = 1.0f;

g_pVertices[19].x = -1.0f; g_pVertices[19].y = 1.0f; g_pVertices[19].z = -1.0f; g_pVertices[19].u = 0.0f; g_pVertices[19].v = 1.0f;

g_pVertices[20].x = -1.0f; g_pVertices[20].y = -1.0f; g_pVertices[20].z = 1.0f; g_pVertices[20].u = 0.0f; g_pVertices[20].v = 0.0f;

g_pVertices[21].x = 1.0f; g_pVertices[21].y = -1.0f; g_pVertices[21].z = 1.0f; g_pVertices[21].u = 1.0f; g_pVertices[21].v = 0.0f;

g_pVertices[22].x = 1.0f; g_pVertices[22].y = -1.0f; g_pVertices[22].z = -1.0f; g_pVertices[22].u = 1.0f; g_pVertices[22].v = 1.0f;

g_pVertices[23].x = -1.0f; g_pVertices[23].y = -1.0f; g_pVertices[23].z = -1.0f; g_pVertices[23].u = 0.0f; g_pVertices[23].v = 1.0f;

g_pVertexBuffer->Unlock();

}

VOID InitMatrices()

{

D3DXMATRIX matIdentity;

D3DXMatrixIdentity(&matIdentity);

g_pd3dDevice->SetTransform(D3DTS_WORLD, &matIdentity);

D3DXVECTOR3 vEyePt(0.0f, 3.0f, -5.0f);

D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f);

D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f);

D3DXMatrixLookAtLH(&g_matView, &vEyePt, &vLookatPt, &vUpVec);

FLOAT fAspect = (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT;

D3DXMatrixPerspectiveFovLH(&g_matProj, D3DX_PI / 4, fAspect, 1.0f, 100.0f);

g_pd3dDevice->SetTransform(D3DTS_VIEW, &g_matView);

g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &g_matProj);

}

VOID InitTexture()

{

D3DXCreateTextureFromFile(g_pd3dDevice, "texture.jpg", &g_pTexture);

}

VOID Render()

{

g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,

D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);

if (SUCCEEDED(g_pd3dDevice->BeginScene()))

{

D3DXMATRIX matRotationY;

D3DXMatrixRotationY(&mat 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.37seo.cn/

点赞(93) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部