VBGAME-DIRECT3D

坚持并快乐着!——繁星
posts - 15, comments - 15, trackbacks - 0, articles - 1
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

矩阵在D3D中的应用

Posted on 2006-04-29 20:41 繁星 阅读(1273) 评论(0)  编辑 收藏 引用

矩阵在D3D中的作用就不用我说了。大部分的变换使用矩阵可以变的非常方便,像世界变换,投影变换,视图变换等等。以下我将交给大家怎样在d3d使用矩阵来实现多个模型的不同运动形式。
1.首先我们手动建立两个八面体(使用三角扇)和一个平面(使用三角条):
Private Type CUSTOMVERTEX
x As Single
y As Single
z As Single
color As Long
End Type

Dim Vertices(28) As CUSTOMVERTEX

With Vertices(0): .x = 0: .y = 0: .z = 0:  .color = &HFF: End With              '0-11顶点为第一个八面体模型顶点
With Vertices(1): .x = -0.5: .y = 1: .z = -0.5:  .color = &HFF00FF: End With
With Vertices(2): .x = 0.5: .y = 1: .z = -0.5:  .color = &HFF00FF: End With
With Vertices(3): .x = 0.5: .y = 1: .z = 0.5:  .color = &HFF00FF: End With
With Vertices(4): .x = -0.5: .y = 1: .z = 0.5:  .color = &HFF00FF: End With
With Vertices(5): .x = -0.5: .y = 1: .z = -0.5:  .color = &HFF00FF: End With

With Vertices(6): .x = 0: .y = 2: .z = 0:  .color = &HFF: End With
With Vertices(7): .x = -0.5: .y = 1: .z = -0.5:  .color = &HFF00FF: End With
With Vertices(8): .x = 0.5: .y = 1: .z = -0.5:  .color = &HFF00FF: End With
With Vertices(9): .x = 0.5: .y = 1: .z = 0.5:  .color = &HFF00FF: End With
With Vertices(10): .x = -0.5: .y = 1: .z = 0.5:  .color = &HFF00FF: End With
With Vertices(11): .x = -0.5: .y = 1: .z = -0.5:  .color = &HFF00FF: End With

With Vertices(12): .x = 0: .y = 2: .z = 0:  .color = &HFF: End With           '12-23顶点为第二个八面体模型顶点
With Vertices(13): .x = -0.5: .y = 3: .z = -0.5:  .color = &HFFFF: End With
With Vertices(14): .x = 0.5: .y = 3: .z = -0.5:  .color = &HFFFF: End With
With Vertices(15): .x = 0.5: .y = 3: .z = 0.5:  .color = &HFFFF: End With
With Vertices(16): .x = -0.5: .y = 3: .z = 0.5:  .color = &HFFFF: End With
With Vertices(17): .x = -0.5: .y = 3: .z = -0.5:  .color = &HFFFF: End With

With Vertices(18): .x = 0: .y = 4: .z = 0:  .color = &HFF: End With
With Vertices(19): .x = -0.5: .y = 3: .z = -0.5:  .color = &HFFFF: End With
With Vertices(20): .x = 0.5: .y = 3: .z = -0.5:  .color = &HFFFF: End With
With Vertices(21): .x = 0.5: .y = 3: .z = 0.5:  .color = &HFFFF: End With
With Vertices(22): .x = -0.5: .y = 3: .z = 0.5:  .color = &HFFFF: End With
With Vertices(23): .x = -0.5: .y = 3: .z = -0.5:  .color = &HFFFF: End With

With Vertices(24): .x = -2: .y = 0: .z = 2:  .color = &HFFFF: End With             '24-27顶点为平面模型顶点
With Vertices(25): .x = 2: .y = 0: .z = 2:  .color = &HFF: End With
With Vertices(26): .x = -2: .y = 0: .z = -2:  .color = &HFF: End With
With Vertices(27): .x = 2: .y = 0: .z = -2:  .color = &HFFFF: End With

Set g_VB = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 28, 0, D3DFVF_XYZ Or D3DFVF_DIFFUSE, D3DPOOL_DEFAULT)
2.接着我们在每次渲染时设置变换矩阵,来变换模型。当设置完世界矩阵后,选择要变换的模型进行渲染。然后再设置下一个模型的世界矩阵,再次渲染。
Sub render()
Dim matView As D3DMATRIX
D3DXMatrixLookAtLH matView, vec3(0#, 7#, 7#), vec3(0#, 2#, 0#), vec3(0#, 1#, 0#)   '设置视图矩阵
g_D3DDevice.SetTransform D3DTS_VIEW, matView
Dim matProj As D3DMATRIX
D3DXMatrixPerspectiveFovLH matProj, g_pi / 6, 1, 1, 1000    '设置投影矩阵
g_D3DDevice.SetTransform D3DTS_PROJECTION, matProj          '使用投影矩阵
Dim matworld As D3DMATRIX
Dim v As CUSTOMVERTEX
Dim sizeOfVertex As Long
sizeOfVertex = Len(v)
g_D3DDevice.SetStreamSource 0, g_VB, sizeOfVertex            '使用数据流0
g_D3DDevice.SetVertexShader D3DFVF_CUSTOMVERTEX
g_D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, 0, 1#, 0
g_D3DDevice.BeginScene
D3DXMatrixIdentity matworld                                   '归一矩阵
D3DXMatrixRotationY matworld, Timer                           '设置第一个八面体的世界变换矩阵(旋转)
g_D3DDevice.SetTransform D3DTS_WORLD, matworld                '使用矩阵              
g_D3DDevice.DrawPrimitive D3DPT_TRIANGLEFAN, 0, 4             '渲染第一个八面体(使用三角扇)    
g_D3DDevice.DrawPrimitive D3DPT_TRIANGLEFAN, 6, 4             '渲染第一个八面体 
D3DXMatrixIdentity matworld                                   '归一矩阵
D3DXMatrixRotationY matworld, -Timer                          '设置第二个八边形的世界变换矩阵(相反方向旋转)
g_D3DDevice.SetTransform D3DTS_WORLD, matworld                '使用矩阵
g_D3DDevice.DrawPrimitive D3DPT_TRIANGLEFAN, 12, 4            '渲染第二个八面体g_D3DDevice.DrawPrimitive D3DPT_TRIANGLEFAN, 18, 4            '渲染第二个八面体
D3DXMatrixIdentity matworld                                   '归一矩阵
g_D3DDevice.SetTransform D3DTS_WORLD, matworld                '应用平面的变换矩阵(不动)
g_D3DDevice.DrawPrimitive D3DPT_TRIANGLESTRIP, 24, 2
g_D3DDevice.EndScene
g_D3DDevice.Present ByVal 0, ByVal 0, 0, ByVal 0
End Sub

Function vec3(x As Single, y As Single, z As Single) As D3DVECTOR
vec3.x = x
vec3.y = y
vec3.z = z
End Function

如图:
八边形.bmp

原代码下载:http://www.cnitblog.com/Files/seesea/矩阵的用法.rar

只有注册用户登录后才能发表评论。