游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1636|回复: 0

源码:如何在VB中实现镜面反射

[复制链接]

29

主题

354

帖子

359

积分

中级会员

Rank: 3Rank: 3

积分
359
QQ
发表于 2006-9-11 18:47:00 | 显示全部楼层 |阅读模式
代码下载:http://justgame.gbaopan.com/


D3DDevice.BeginScene
        
        '//1. Draw the "reflection"
            D3DDevice.SetVertexShader FVF_VERTEX
            D3DDevice.SetRenderState D3DRS_LIGHTING, 1
            
            Dim plReflect As D3DPLANE
            plReflect.B = 1 'simple y=0 flat plane...
            
            D3DXMatrixReflect matReflection, plReflect
            D3DXMatrixMultiply matReflection, matReflection, matView
                       
            D3DDevice.SetTransform D3DTS_VIEW, matReflection
            
            D3DXMatrixTranslation matTemp, 0, 40, 0
            D3DXMatrixMultiply matWorld2, matWorld, matTemp
            
            D3DDevice.SetTransform D3DTS_WORLD, matWorld2
            D3DDevice.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
   Dim matViewSaved As D3DMATRIX
   D3DDevice.GetTransform D3DTS_VIEW, matViewSaved ' //取得视口

'//定义一个翻转的参考平面,可根据需要自己定义位置及法线
   Dim vPoint As D3DXVECTOR3
   Dim vNormal As D3DXVECTOR3

';
'    D3DXMATRIXA16 matView, matReflect;
'    D3DXPLANE plane;
'    D3DXPlaneFromPointNormal( &plane, &vPoint, &vNormal ); //生成这个平面

'    D3DXMatrixReflect( &matReflect, &plane );   //取得该平面的反射矩阵

'    D3DXMatrixMultiply( &matView, &matReflect, &matViewSaved ); //使用反射矩阵翻转视口,摄影机被翻转到反射位置

'    m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );  //设置视口,这样摄影机渲染出的内容就是翻转的了

'//设置剪切平面,使反射面上的内容被渲染,面下的被丢弃
'    m_pd3dDevice->SetClipPlane( 0, plane );
'    m_pd3dDevice->SetRenderState( D3DRS_CLIPPLANEENABLE, 0x01 );


            For i = 0 To nMaterials - 1
                If bSpecular Then
                    D3DDevice.SetMaterial SpecularMat
                Else
                    D3DDevice.SetMaterial MeshMaterials(i)
                End If
                DiamondMesh.DrawSubset i
            Next i
            
            D3DDevice.SetTransform D3DTS_VIEW, matView
        
        '//2. Draw the floor
            D3DDevice.SetVertexShader FVF_LVERTEX
            D3DDevice.SetRenderState D3DRS_LIGHTING, 0
            D3DDevice.SetTexture 0, TexFloor
            D3DXMatrixIdentity matTemp
            D3DDevice.SetTransform D3DTS_WORLD, matTemp
            D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, 1
            
            D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, vertFloor(0), Len(vertFloor(0))
            
            D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, 0
            D3DDevice.SetTransform D3DTS_WORLD, matWorld
        
        '//3. Draw the main object
            D3DDevice.SetVertexShader FVF_VERTEX
            D3DDevice.SetRenderState D3DRS_LIGHTING, 1
            
            For i = 0 To nMaterials - 1
                If bSpecular Then
                    D3DDevice.SetMaterial SpecularMat
                Else
                    D3DDevice.SetMaterial MeshMaterials(i)
                End If
                DiamondMesh.DrawSubset i
            Next i
        
        '//4. Draw the shadow
            v4Light.X = 500 '.577
            v4Light.Y = -500 '-.577
            v4Light.Z = -500 '-.577
            v4Light.w = 0  '1=point light, 0=directional light
            
            Dim plPlane As D3DPLANE
            'D3DXPlaneFromPoints plPlane, MakeVector(0, -20, 0), MakeVector(1, -20, 1), MakeVector(-1, -20, 1)
'            D3DXPlaneFromPointNormal plPlane, MakeVector(0, -60, 0), MakeVector(0, -1, 0)
'            D3DXMatrixShadow matShadow, v4Light, plPlane
'            D3DXMatrixMultiply matFinal, matWorld, matShadow
            
'            D3DDevice.SetTransform D3DTS_WORLD, matFinal
            
            'use a black material to get a black shadow...
'            D3DDevice.SetRenderState D3DRS_ZFUNC, D3DCMP_ALWAYS
'            D3DDevice.SetMaterial Material
'            For i = 0 To nMaterials - 1
                'DiamondMesh.DrawSubset i
'            Next i
'            D3DDevice.SetRenderState D3DRS_ZFUNC, D3DCMP_LESSEQUAL
        
        '//5. Draw the text output
            TextRect.Top = 0
            TextRect.bottom = 15
            TextRect.Right = 75
            D3DX.DrawText MainFont, &HFFFF80FF, CStr(FrameRate) & "fps", TextRect, DT_TOP Or DT_LEFT
            
            TextRect.Top = 15
            TextRect.bottom = 33
            TextRect.Right = 275
            D3DX.DrawText MainFont, &HFFFF80FF, "Spec. Lighting: " & IIf(bSpecular, "Enabled", "Disabled"), TextRect, DT_TOP Or DT_LEFT
            
            TextRect.Top = 33
            TextRect.bottom = 50
            TextRect.Right = 275
            D3DX.DrawText MainFont, &HFFFF80FF, "Local Viewer: " & IIf(bLocalViewer, "Enabled", "Disabled"), TextRect, DT_TOP Or DT_LEFT
            
            TextRect.Top = 50
            TextRect.bottom = 67
            TextRect.Right = 275
            D3DX.DrawText MainFont, &HFFFF80FF, "Spec. Power: " & SpecularMat.power, TextRect, DT_TOP Or DT_LEFT
               
               

D3DDevice.EndScene
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-25 10:09

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表