Модератор: Mikle
Option Explicit
Dim dx As New DirectX8
Dim d3d As Direct3D8
Dim d3dx As New D3DX8
Dim dev As Direct3DDevice8
Dim Mtr As D3DMATRIX
Private Type DX_BALL
Mesh As D3DXBaseMesh
Mat As D3DMATERIAL8
Mtrx As D3DMATRIX
MRot As D3DMATRIX
MMove As D3DMATRIX
mePos As D3DVECTOR
End Type
Dim CubeTex As Direct3DCubeTexture8
Dim CubeC As Direct3DSurface8
Dim CubeZ As Direct3DSurface8
Dim MainC As Direct3DSurface8
Dim MainZ As Direct3DSurface8
Dim I, n As Long
Dim LUp, x As Single
Dim Ball(2) As DX_BALL
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then I = 1
End Sub
Private Sub Form_Load()
Dim DM As D3DDISPLAYMODE
Dim pp As D3DPRESENT_PARAMETERS
Me.Show
Set d3d = dx.Direct3DCreate
d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, DM
pp.Windowed = 1
pp.BackBufferCount = 1
pp.BackBufferWidth = Me.ScaleWidth
pp.BackBufferHeight = Me.ScaleHeight
pp.BackBufferFormat = DM.Format
pp.AutoDepthStencilFormat = D3DFMT_D16
pp.EnableAutoDepthStencil = 1
pp.SwapEffect = D3DSWAPEFFECT_DISCARD
pp.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES
Set dev = d3d.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Me.hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, pp)
dev.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
For n = 0 To 1
dev.SetTextureStageState n, D3DTSS_MAGFILTER, D3DTEXF_LINEAR
dev.SetTextureStageState n, D3DTSS_MIPFILTER, D3DTEXF_LINEAR
dev.SetTextureStageState n, D3DTSS_MINFILTER, D3DTEXF_LINEAR
dev.SetTextureStageState n, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2
Next n
Set MainC = dev.GetRenderTarget
Set MainZ = dev.GetDepthStencilSurface
Set CubeZ = dev.CreateDepthStencilSurface(128, 128, D3DFMT_D16, D3DMULTISAMPLE_NONE)
Set CubeTex = d3dx.CreateCubeTexture(dev, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R5G6B5, D3DPOOL_DEFAULT)
For n = 0 To 1
Init Ball(n)
Rotate Ball(n), 0, 0, 0
MoveM Ball(n), 0, 0, 0
Next
InitLight
Do Until I <> 0: DoEvents
dev.BeginScene
If Timer - LUp > 0.01! Then
MoveM Ball(0), 0, 2 * Cos(x), 2 * Sin(x)
getCub
D3DXMatrixLookAtLH Mtr, vec3(5, 0, 0), vec3(0, 0, 0), vec3(0, 1, 0)
dev.SetTransform D3DTS_VIEW, Mtr
D3DXMatrixPerspectiveFovLH Mtr, 1, 0.75!, 0.1!, 20
dev.SetTransform D3DTS_PROJECTION, Mtr
D3DXMatrixIdentity Mtr
dev.SetTransform D3DTS_WORLD, Mtr
LUp = Timer: x = x + 0.05
End If
dev.SetRenderTarget MainC, MainZ, 0
dev.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFF0F0F0F, 1, 0
Render Ball(0)
Render Ball(1)
dev.EndScene
dev.Present ByVal 0, ByVal 0, 0, ByVal 0
Loop
Set dev = Nothing: Set d3d = Nothing
Set Ball(0).Mesh = Nothing
Set Ball(1).Mesh = Nothing
Unload Me
End Sub
Private Function vec3(x As Single, y As Single, Z As Single) As D3DVECTOR
vec3.x = x: vec3.y = y: vec3.Z = Z
End Function
Private Sub Rotate(D_Mash As DX_BALL, ByVal AngleX As Single, ByVal AngleY As Single, ByVal AngleZ As Single)
Dim MtrxT(3) As D3DMATRIX
D3DXMatrixRotationX MtrxT(0), AngleX
D3DXMatrixRotationY MtrxT(1), AngleY
D3DXMatrixRotationZ MtrxT(2), AngleZ
D3DXMatrixMultiply MtrxT(3), MtrxT(0), MtrxT(1)
D3DXMatrixMultiply D_Mash.MRot, MtrxT(2), MtrxT(3)
End Sub
Private Sub MoveM(D_Mash As DX_BALL, ByVal x As Single, ByVal y As Single, ByVal Z As Single)
D_Mash.mePos.x = x
D_Mash.mePos.y = y
D_Mash.mePos.Z = Z
D3DXMatrixTranslation D_Mash.MMove, D_Mash.mePos.x, D_Mash.mePos.y, D_Mash.mePos.Z
End Sub
Private Sub Init(D_Mash As DX_BALL)
Set D_Mash.Mesh = d3dx.CreateSphere(dev, n + 0.3, 24, 64, Nothing)
D_Mash.Mat.diffuse.r = 0.4
D_Mash.Mat.diffuse.g = 0.01
D_Mash.Mat.diffuse.b = 0.01
D_Mash.Mat.diffuse.a = 0
D_Mash.Mat.Ambient.r = 0.21
D_Mash.Mat.Ambient.g = 0.1
D_Mash.Mat.Ambient.b = 0.1
End Sub
Private Sub InitLight()
Dim Light As D3DLIGHT8
Light.Type = D3DLIGHT_DIRECTIONAL
Light.Direction = vec3(3, -3, -3)
Light.diffuse.r = 1
Light.diffuse.g = 1
Light.diffuse.b = 1
Light.Ambient.r = 68 / 255
Light.Ambient.g = 70 / 255
Light.Ambient.b = 90 / 255
dev.SetLight 0, Light
dev.LightEnable 0, 1
End Sub
Private Sub Render(D_Mash As DX_BALL)
D3DXMatrixMultiply D_Mash.Mtrx, D_Mash.MRot, D_Mash.MMove
dev.SetTransform D3DTS_WORLD, D_Mash.Mtrx
dev.SetMaterial D_Mash.Mat
dev.SetRenderState D3DRS_LIGHTING, 1
dev.SetRenderState D3DRS_TEXTUREFACTOR, D3DColorXRGB(100, 100, 100)
dev.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
dev.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
dev.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_TFACTOR
dev.SetTextureStageState 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3
dev.SetTextureStageState 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
dev.SetTextureStageState 1, D3DTSS_COLOROP, D3DTOP_ADD
dev.SetTextureStageState 1, D3DTSS_COLORARG1, D3DTA_CURRENT
dev.SetTextureStageState 1, D3DTSS_COLORARG2, D3DTA_DIFFUSE
dev.SetTexture 0, CubeTex
D_Mash.Mesh.DrawSubset 0
End Sub
Private Sub getCub()
D3DXMatrixPerspectiveFovLH Mtr, 3.141593 / 2, 1, 0.1, 100
dev.SetTransform D3DTS_PROJECTION, Mtr
For n = 0 To 5
Mtr = GetCubeMapViewMatrix(n, vec3(0, 0, 0))
dev.SetTransform D3DTS_VIEW, Mtr
Set CubeC = CubeTex.GetCubeMapSurface(n, 0)
dev.SetRenderTarget CubeC, CubeZ, 0
dev.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, D3DColorXRGB(50, 50, 50), 1, 0
Render Ball(0)
Next n
End Sub
Public Function GetCubeMapViewMatrix(Face As Long, vEye As D3DVECTOR) As D3DMATRIX
Dim vLook As D3DVECTOR
Dim vUp As D3DVECTOR
Select Case Face
Case D3DCUBEMAP_FACE_POSITIVE_X
vLook = vec3(vEye.x + 1!, vEye.y, vEye.Z)
vUp = vec3(0!, 1!, 0!)
Case D3DCUBEMAP_FACE_NEGATIVE_X
vLook = vec3(vEye.x - 1!, vEye.y, vEye.Z)
vUp = vec3(0!, 1!, 0!)
Case D3DCUBEMAP_FACE_POSITIVE_Y
vLook = vec3(vEye.x, vEye.y + 1!, vEye.Z)
vUp = vec3(0!, 0!, -1!)
Case D3DCUBEMAP_FACE_NEGATIVE_Y
vLook = vec3(vEye.x, vEye.y - 1!, vEye.Z)
vUp = vec3(0!, 0!, 1!)
Case D3DCUBEMAP_FACE_POSITIVE_Z
vLook = vec3(vEye.x, vEye.y, vEye.Z + 1!)
vUp = vec3(0!, 1!, 0!)
Case D3DCUBEMAP_FACE_NEGATIVE_Z
vLook = vec3(vEye.x, vEye.y, vEye.Z - 1!)
vUp = vec3(0!, 1!, 0!)
End Select
D3DXMatrixLookAtLH GetCubeMapViewMatrix, vEye, vLook, vUp
End Function
DODge писал(а):Кстати, я от них уже избавился. Сейчас у меня проблема в другом - при отдалении (а точнее, при крутом повороте камеры от нормали зеркала) отражение срезается, но это ес-сно времено.
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 37