456
社区成员
发帖
与我相关
我的任务
分享
'思路就是先定义两组纹理座标
Private Type TextureCoorSet
tu As Single
tv As Single
End Type
Public Type MAP_CUSTOMVERTEX
Postion As D3DVECTOR '3d position for vertex
Color As Long 'color of the vertex
Tex(1) As TextureCoorSet '设两组纹理座标..大图和小图各一
End Type
Public Const D3DFVF_MAP_CUSTOMVERTEX = D3DFVF_XYZ Or D3DFVF_DIFFUSE Or D3DFVF_TEX2 '定义座标格式(D3DFVF_TEX2 是代表有两组纹理座标,如是者D3DFVF_TEX3含有是三组纹理座标
Private Type My_face
A As Long
B As Long
C As Long
x As Long
Y As Long
Z As Long
End Type
Public Sub Set_Terrain_Mesh() '做一个mesh
Dim i As Long, j As Long, x As Long, Y As Long, Z As Long
Dim MapVertices() As MAP_CUSTOMVERTEX
Dim TexCorr As Single, hr As Long
Dim LengthInByte As Long
'建立一个mesh
Set Terrain_Mesh = g_D3DX.CreateMeshFVF((7 * 7) * 2&, 7 * 7 * 4, D3DXMESH_VB_MANAGED, D3DFVF_MAP_CUSTOMVERTEX, g_D3DDevice)
ReDim MapVertices((7 * 7 * 4&) - 1) '座标分成8乘8的方塊,每个方塊4个座标点(0至7一共是8)
TexCorr = 1 / 8 '大材质的座标分成8格
x = 0
For i = 0 To 6
For j = 0 To 6
Nu = j Mod 4
MapVertices(x).Postion.x = j
MapVertices(x).Postion.Z = i
MapVertices(x).Postion.Y = TerrainData(j, i) 'TerrainData(x,y)为点x,y的地型高度
MapVertices(x).Color = &HE0E0E0E0
MapVertices(x).Tex(0).tu = j * TexCorr
MapVertices(x).Tex(0).tv = i * TexCorr
MapVertices(x).Tex(1).tu = 0
MapVertices(x).Tex(1).tv = 0
x = x + 1
MapVertices(x).Postion.x = j + 1
MapVertices(x).Postion.Z = i
MapVertices(x).Postion.Y = TerrainData(j + 1, i)
MapVertices(x).Color = &HE0E0E0E0
MapVertices(x).Tex(0).tu = (j + 1) * TexCorr
MapVertices(x).Tex(0).tv = i * TexCorr
MapVertices(x).Tex(1).tu = 1
MapVertices(x).Tex(1).tv = 0
x = x + 1
MapVertices(x).Postion.x = j + 1
MapVertices(x).Postion.Z = i + 1
MapVertices(x).Postion.Y = TerrainData(j + 1, i + 1)
MapVertices(x).Color = &HE0E0E0E0
MapVertices(x).Tex(0).tu = (j + 1) * TexCorr
MapVertices(x).Tex(0).tv = (i + 1) * TexCorr
MapVertices(x).Tex(1).tu = 1
MapVertices(x).Tex(1).tv = 1
x = x + 1
MapVertices(x).Postion.x = j
MapVertices(x).Postion.Z = i + 1
MapVertices(x).Postion.Y = TerrainData(j, i + 1)
MapVertices(x).Color = &HE0E0E0E0
MapVertices(x).Tex(0).tu = j * TexCorr
MapVertices(x).Tex(0).tv = (i + 1) * TexCorr
MapVertices(x).Tex(1).tu = 0
MapVertices(x).Tex(1).tv = 1
x = x + 1
Next j
Next i
LengthInByte = LenB(MapVertices(0)) * x '取得所有座标数据的長度(byte)
hr = D3DXMeshVertexBuffer8SetData(Terrain_Mesh, 0, LengthInByte, 0, MapVertices(0)) '把座标放入mesh
Dim TmpFace() As My_face, T As Long
T = (7 * 7) - 1
x = 0
ReDim TmpFace(T)
For i = 0 To 6
For j = 0 To 6
'一个方格用两个三角形组成,所以每4个座标点做一次三角型
TmpFace((i * 511&) + j).A = x
TmpFace((i * 511&) + j).B = x + 1
TmpFace((i * 511&) + j).C = x + 3
TmpFace((i * 511&) + j).x = x + 3
TmpFace((i * 511&) + j).Y = x + 1
TmpFace((i * 511&) + j).Z = x + 2
x = x + 4
Next j
Next i
'把三角形数据放入mesh
hr = D3DXMeshIndexBuffer8SetData(Terrain_Mesh, 0, LenB(MapFaces(0)) * T, 0, MapFaces(0))
End Sub
Public Sub Rander() '渲染
g_D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFF&, 1#, 0 '清理畫面
g_D3DDevice.BeginScene '开始渲染
g_D3DDevice.SetMaterial Terrain_Material '设定材质
g_D3DDevice.SetTexture 0, Terrain_Texture(0) '设纹理0...这个是代表texture C
g_D3DDevice.SetTextureStageState 0, D3DTSS_TEXCOORDINDEX, 0 '设定纹理0使用第一组纹理座标
g_D3DDevice.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE '颜色混合选择1为当前贴图(0)
g_D3DDevice.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 '颜色混合 只显示颜色混合选择1的紋理不作任何修改
g_D3DDevice.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE '不使用透明混合
'=====纹理混合第一步完成,接着是去混合第二个纹理=====
g_D3DDevice.SetTexture 1, Terrain_Texture(1) '设纹理1...这个是代表texture A
g_D3DDevice.SetTextureStageState 1, D3DTSS_TEXCOORDINDEX, 1 '设定纹理1使用第二组纹理座标,这个很重要,决定两个纹理一个是整个盖在地面还是一格一格的平
g_D3DDevice.SetTextureStageState 1, D3DTSS_COLORARG1, D3DTA_CURRENT '颜色混合选择1:上一步做好的影像
g_D3DDevice.SetTextureStageState 1, D3DTSS_COLORARG2, D3DTA_TEXTURE '颜色混合选择2: 当前贴图(1)
g_D3DDevice.SetTextureStageState 1, D3DTSS_COLOROP, D3DTOP_MODULATE '颜色混合: 把选择1和选择2的RGB颜色相乘,通常合併颜色都是这个
g_D3DDevice.SetTextureStageState 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE '不使用透明混合
'=====纹理混合完成(如果多个纹理併合应该是使用DrawIndexedPrimitive,以及用二维数组决定采用那个纹理来设定成纹理 1 进行混合)=====
Terrain_Mesh.DrawSubset 0 把'做好纹理混合的mesh畫出来
g_D3DDevice.EndScene '渲染完成
g_D3DDevice.Present ByVal 0, ByVal 0,0, ByVal 0 '把畫面显示去屏幕
End Sub