VBGAME-DIRECT3D

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

Dot3 bump mapping

Posted on 2006-10-06 15:27 繁星 阅读(804) 评论(0)  编辑 收藏 引用

刚完成的,用顶点着色器版本vs1.1.来几张图:
bump3.bmpbump4.bmp
凹凸感还是很强,使用了高光.
1.c5储存光源的位置,c6储存视点位置。
///////////////////////////////////////////////////////////////////////////////////////////////
Dim Constants(4) As Single
Constants(0) = 5
Constants(1) = 5
Constants(2) = 5
Constants(3) = 0
g_D3DDevice.SetVertexShaderConstant 5, Constants(0), 1 'light position
Constants(0) = 0
Constants(1) = 2
Constants(2) = 3
Constants(3) = 0
g_D3DDevice.SetVertexShaderConstant 6, Constants(0), 1 'eye position
Constants(0) = 0.5
Constants(1) = 0#
Constants(2) = 0#
Constants(3) = 0#
g_D3DDevice.SetVertexShaderConstant 7, Constants(0), 1
//////////////////////////////////////////////////////////////////////////////////////////////
2.把矩阵写入常量寄存器,手动变换。
//////////////////////////////////////////////////////////////////////////////////////////////
Sub SetupMatrices()
Dim tempmat As D3DMATRIX
Dim matWorld As D3DMATRIX
Dim matView As D3DMATRIX
Dim matProj As D3DMATRIX
D3DXMatrixRotationY matWorld, Timer / 2
D3DXMatrixLookAtLH matView, vec3(0#, 2#, 3#), vec3(0#, 0#, 0#), vec3(0#, 1#, 0#)
D3DXMatrixMultiply tempmat, matWorld, matView
D3DXMatrixPerspectiveFovLH matProj, g_pi / 3, 1, 1, 1000
D3DXMatrixMultiply tempmat, tempmat, matProj
D3DXMatrixTranspose tempmat, tempmat
g_D3DDevice.SetVertexShaderConstant 0, tempmat, 4
D3DXMatrixInverse tempmat, 1#, tempmat
D3DXMatrixTranspose tempmat, tempmat
g_D3DDevice.SetVertexShaderConstant 8, tempmat, 4
End Sub
////////////////////////////////////////////////////////////////////////////////////////////////
3.编写顶点着色器的汇编代码。
////////////////////////////////////////////////////////////////////////////////////////////////
vs.1.1
m4x4 r4, v0, c0           变换顶点到裁减空间
mov oPos,r4
m4x4 r1,v3,c8             变换法向量到裁减空间,用W+V+P的转置逆矩阵
m4x4 r2,v9,c0              变换切向量
m4x4 r3,v10,c0            变换副法向量

add r9,-c5,r4                计算光向量并把它变换到切线空间后,再归一化
dp3 r0.x, r2.xyz, r9
dp3 r0.y, r3.xyz, r9
dp3 r0.z, r1.xyz, r9
dp3 r0.w, r0, r0
rsq r0.w, r0.w
mul r0, r0, r0.w

add r5,-c6,r4               计算视向量并把它变换到切线空间后,再归一化
dp3 r6.x, r2.xyz, r5
dp3 r6.y, r3.xyz, r5
dp3 r6.z, r1.xyz, r5
dp3 r6.w, r6, r6
rsq r6.w, r6.w
mul r6, r6, r6.w

add r7,r6,r0                  计算H中间向量并归一
dp3 r8.w,r7,r7
rsq r8.w,r8.w
mul r7,r7,r8.w

mad oD0, r0, c7.x, c7.x           写入漫射光输出寄存器
mad oD1, r7, c7.x, c7.x            写入高光输出寄存器

mov oT0.x, v7.x
mov oT0.y, v7.y
mov oT1.x, v7.x
mov oT1.y, v7.y
/////////////////////////////////////////////////////////////////////////////////////////////////////////
4.连接代码,编译生成着色器。
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Function CreateVS() As Long
Dim shaderArray() As Long
Dim shader As Long
Dim decl(8) As Long
Dim shaderCode As D3DXBuffer
decl(0) = 536870912                          顶点流为0    详细请连接
decl(1) = 1073872896                         顶点位置                  
decl(2) = 1073872899                         法向量
decl(3) = 1073807367                         纹理坐标0
decl(4) = 1073807368                         纹理坐标1
decl(5) = 1073807369                         纹理坐标2,储存切向量
decl(6) = 1073807370                         纹理坐标3,储存副法向量
decl(7) = -1                                          end
Set shaderCode = d3dx.AssembleShaderFromFile(App.Path & "\bump.txt", 0, "", Nothing)
ReDim shaderArray(shaderCode.GetBufferSize() / 4)
d3dx.BufferGetData shaderCode, 0, 1, shaderCode.GetBufferSize(), shaderArray(0)
Set shaderCode = Nothing
g_D3DDevice.CreateVertexShader decl(0), shaderArray(0), shader, D3DUSAGE_SOFTWAREPROCESSING
CreateVS = shader
End Function
//////////////////////////////////////////////////////////////////////////////////////////////////////////
5.设置材质混合操作
///////////////////////////////////////////////////////////////////////////////////////////////////////////
Set d3dt(0) = d3dx.CreateTextureFromFileEx(g_D3DDevice, App.Path & "\earth.bmp", 512, 256, 0, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR, 0, ByVal 0, ByVal 0)
Set d3dt(1) = d3dx.CreateTextureFromFileEx(g_D3DDevice, App.Path & "\earthbump.bmp", 512, 256, 0, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR, 0, ByVal 0, ByVal 0)
g_D3DDevice.SetTexture 0, d3dt(1)
g_D3DDevice.SetTexture 1, d3dt(0)
g_D3DDevice.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_DOTPRODUCT3
g_D3DDevice.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_TEXTURE
g_D3DDevice.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_SPECULAR  '(或者D3DTA_DIFFUSE)
g_D3DDevice.SetTextureStageState 1, D3DTSS_COLOROP, D3DTOP_MODULATE
g_D3DDevice.SetTextureStageState 1, D3DTSS_COLORARG1, D3DTA_CURRENT
g_D3DDevice.SetTextureStageState 1, D3DTSS_COLORARG2, D3DTA_TEXTURE
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
材质混合操作为D3DTOP_DOTPRODUCT3,它混合插值了的diffuse和法向量贴图中的corresponding normal 。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MSDN中定义的 D3DTOP_DOTPRODUCT3 操作
Modulate the components of each argument as signed components, add their products; then replicate the sum to all color channels, including alpha. This operation is supported for color and alpha operations.

In DirectX 6.0 and 7.0 multitexture operations the above inputs are all shifted down by half (y = x - 0.5) before use to simulate signed data, and the scalar result is automatically clamped to positive values and replicated to all three output channels. Also, note that as a color operation this does not updated the alpha it just updates the RGB components.

However, in DirectX 8.x shaders, you can specify that the output be routed to the .rgb or the .a components or both (the default). You can also specify a separate scalar operation on the alpha channel.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DownLoad:http://www.cnitblog.com/Files/seesea/凹凸映射.rar

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