Модератор: Mikle
нажимаешь вперед камера тоже вперед едет
d3dDevice.SetTexture 0, myTex
Option Explicit
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
Dim dx As New DirectX8
Dim d3d As Direct3D8
Dim d3dx As New D3DX8
Dim d3dDevice As Direct3DDevice8
Dim vBuffer As Direct3DVertexBuffer8
Dim pTexture As Direct3DTexture8
Dim Mtrx As D3DMATRIX
Dim Keyb(255) As Boolean
Dim t1 As Single, t2 As Single
Dim CamPos As D3DVECTOR, Angle As Single, SinA As Single, CosA As Single
Private Type vFormat
Position As D3DVECTOR
Color As Long
End Type
Dim v As vFormat, SizeOfVertex As Long
Private Const vFlag = (D3DFVF_XYZ Or D3DFVF_DIFFUSE)
Private Const pi = 3.141592
Private Const NumTri As Long = 100
Private Const SkyColor = &H455070
Private Sub Form_Load()
Me.Show
ShowCursor False
InitD3D
InitGeometry
CamPos = vec3(0, 1, 0)
Angle = 0
t2 = Timer
SetCam
Do Until Keyb(vbKeyEscape)
DoEvents
AscKey
Render
Loop
ShowCursor True
Unload Me
End Sub
Private Sub AscKey()
Dim t As Single
t1 = Timer
t = (t1 - t2) * 2
t2 = t1
If Keyb(vbKeyShift) Then t = t * 2
CosA = Cos(Angle)
SinA = Sin(Angle)
If Keyb(vbKeyLeft) Then
If Keyb(vbKeyMenu) Then
CamPos.x = CamPos.x - CosA * t
CamPos.z = CamPos.z - SinA * t
SetCam
Else
Angle = Angle + t: SetCam
End If
End If
If Keyb(vbKeyRight) Then
If Keyb(vbKeyMenu) Then
CamPos.x = CamPos.x + CosA * t
CamPos.z = CamPos.z + SinA * t
SetCam
Else
Angle = Angle - t: SetCam
End If
End If
If Keyb(vbKeyUp) Then
CamPos.x = CamPos.x - SinA * t
CamPos.z = CamPos.z + CosA * t
SetCam
End If
If Keyb(vbKeyDown) Then
CamPos.x = CamPos.x + SinA * t
CamPos.z = CamPos.z - CosA * t
SetCam
End If
End Sub
Private Sub SetCam()
D3DXMatrixRotationY Mtrx, Angle
d3dDevice.SetTransform D3DTS_VIEW, Mtrx
D3DXMatrixTranslation Mtrx, -CamPos.x, -CamPos.y, -CamPos.z
d3dDevice.MultiplyTransform D3DTS_VIEW, Mtrx
End Sub
Private Sub InitD3D()
Dim DispMode As D3DDISPLAYMODE
Dim d3dpp As D3DPRESENT_PARAMETERS
Set d3d = dx.Direct3DCreate
d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, DispMode
d3dpp.Windowed = False
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD
d3dpp.BackBufferFormat = DispMode.Format
d3dpp.BackBufferWidth = DispMode.Width
d3dpp.BackBufferHeight = DispMode.Height
d3dpp.BackBufferCount = 1
d3dpp.EnableAutoDepthStencil = True
d3dpp.AutoDepthStencilFormat = D3DFMT_D16
Set d3dDevice = d3d.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Me.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)
d3dDevice.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
d3dDevice.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE
d3dDevice.SetRenderState D3DRS_LIGHTING, False
d3dDevice.SetRenderState D3DRS_FOGCOLOR, SkyColor
d3dDevice.SetRenderState D3DRS_FOGDENSITY, &H3D800000
d3dDevice.SetRenderState D3DRS_FOGTABLEMODE, D3DFOG_EXP
d3dDevice.SetRenderState D3DRS_FOGENABLE, True
SizeOfVertex = Len(v)
D3DXMatrixPerspectiveFovLH Mtrx, pi / 3, 0.75, 0.1, 100
d3dDevice.SetTransform D3DTS_PROJECTION, Mtrx
D3DXMatrixRotationY Mtrx, 0
d3dDevice.SetTransform D3DTS_WORLD, Mtrx
End Sub
Private Sub InitGeometry()
Dim Vert(NumTri * 3 - 1) As vFormat
Dim n As Long, x As Single, y As Single, z As Single, c As Long
Set vBuffer = d3dDevice.CreateVertexBuffer((NumTri * 3) * SizeOfVertex, 0, vFlag, D3DPOOL_DEFAULT)
Vert(0).Position = vec3(-10, 0, -10)
Vert(0).Color = &H404040
Vert(1).Position = vec3(-10, 0, 10)
Vert(1).Color = &H404040
Vert(2).Position = vec3(10, 0, -10)
Vert(2).Color = &H404040
Vert(3).Position = vec3(-10, 0, 10)
Vert(3).Color = &H404040
Vert(4).Position = vec3(10, 0, 10)
Vert(4).Color = &H404040
Vert(5).Position = vec3(10, 0, -10)
Vert(5).Color = &H404040
For n = 2 To NumTri - 1
x = Rnd * 20 - 10
y = Rnd * 3
z = Rnd * 20 - 10
c = Rnd * &HFFFFFF
Vert(n * 3 + 0).Position = vec3(x + Rnd, y + Rnd, z + Rnd)
Vert(n * 3 + 0).Color = c
Vert(n * 3 + 1).Position = vec3(x + Rnd, y + Rnd, z + Rnd)
Vert(n * 3 + 1).Color = c
Vert(n * 3 + 2).Position = vec3(x + Rnd, y + Rnd, z + Rnd)
Vert(n * 3 + 2).Color = c
Next n
D3DVertexBuffer8SetData vBuffer, 0, SizeOfVertex * (NumTri * 3), 0, Vert(0)
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Keyb(KeyCode) = True
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Keyb(KeyCode) = False
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set vBuffer = Nothing
Set d3dDevice = Nothing
Set d3dx = Nothing
Set d3d = Nothing
Set dx = Nothing
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 Render()
d3dDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, SkyColor, 1, 0
d3dDevice.BeginScene
d3dDevice.SetStreamSource 0, vBuffer, SizeOfVertex
d3dDevice.SetVertexShader vFlag
d3dDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, NumTri
d3dDevice.EndScene
d3dDevice.Present ByVal 0, ByVal 0, 0, ByVal 0
End Sub
Me.Width = 8060
Me.Height = 6060
ты извини, если я тебя обидел, но когда я отвечал, они были...так что извиняй, если чё не так...Mikle писал(а):Vovik
Если ты не заметил - эти строки я уже удалил. Просто проект переделывался из другого, и я их сразу не заметил.
Mikle писал(а):После второго MatrixRotation должно быть Multiply, а не SetTransform. Или вместо двух MatrixRotation применяй один MatrixRotationAxis, но придется рассчитывать ось.
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type vFormat
Position As D3DVECTOR
Color As Long
End Type
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim dx As New DirectX8
Dim d3d As Direct3D8
Dim d3dx As New D3DX8
Dim d3dDevice As Direct3DDevice8
Dim vBuffer As Direct3DVertexBuffer8
Dim pTexture As Direct3DTexture8
Dim Mtrx As D3DMATRIX
Dim Keyb(255) As Boolean
Dim t1 As Single, t2 As Single
Dim CamPos As D3DVECTOR, Angle As Single, Diff As Single, SinA As Single, CosA As Single
Dim MousePos As POINTAPI, CenterX As Long, CenterY As Long
Dim MouseSpeedX As Single, MouseSpeedY As Single
Dim v As vFormat, SizeOfVertex As Long
Private Const vFlag = (D3DFVF_XYZ Or D3DFVF_DIFFUSE)
Private Const pi = 3.141592
Private Const NumTri As Long = 100
Private Const SkyColor = &H252550
Private Sub Form_Load()
Me.Show
InitD3D
ShowCursor False
SetCursorPos CenterX, CenterY
InitGeometry
CamPos = vec3(0, 1, 0)
Angle = 0
Diff = 0
MouseSpeedX = -0.005 'Чувствительность мыши
MouseSpeedY = 0.005 'Чтобы убрать инверсию - ставь отрицательное число
t2 = Timer
SetCam
Do Until Keyb(vbKeyEscape)
DoEvents
AscKey
Render
Loop
ShowCursor True
Unload Me
End Sub
Private Sub AscKey()
Dim t As Single
t1 = Timer
t = (t1 - t2) * 4
t2 = t1
CosA = Cos(Angle)
SinA = Sin(Angle)
GetCursorPos MousePos
If Keyb(vbKeyLeft) Then
CamPos.x = CamPos.x - CosA * t
CamPos.z = CamPos.z - SinA * t
SetCam
End If
If Keyb(vbKeyRight) Then
CamPos.x = CamPos.x + CosA * t
CamPos.z = CamPos.z + SinA * t
SetCam
End If
If Keyb(vbKeyUp) Then
CamPos.x = CamPos.x - SinA * t
CamPos.z = CamPos.z + CosA * t
SetCam
End If
If Keyb(vbKeyDown) Then
CamPos.x = CamPos.x + SinA * t
CamPos.z = CamPos.z - CosA * t
SetCam
End If
If (MousePos.x <> CenterX) Or (MousePos.y <> CenterY) Then
Angle = Angle + (MousePos.x - CenterX) * MouseSpeedX
Diff = Diff + (MousePos.y - CenterY) * MouseSpeedY
If Diff > 1.3 Then Diff = 1.3 'Предел
If Diff < -1.3 Then Diff = -1.3 'наклона
SetCursorPos CenterX, CenterY
SetCam
End If
End Sub
Private Sub SetCam()
D3DXMatrixRotationX Mtrx, Diff
d3dDevice.SetTransform D3DTS_VIEW, Mtrx
D3DXMatrixRotationY Mtrx, Angle
d3dDevice.MultiplyTransform D3DTS_VIEW, Mtrx
D3DXMatrixTranslation Mtrx, -CamPos.x, -CamPos.y, -CamPos.z
d3dDevice.MultiplyTransform D3DTS_VIEW, Mtrx
End Sub
Private Sub InitD3D()
Dim DispMode As D3DDISPLAYMODE
Dim d3dpp As D3DPRESENT_PARAMETERS
Set d3d = dx.Direct3DCreate
d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, DispMode
CenterX = DispMode.Width \ 2
CenterY = DispMode.Height \ 2
d3dpp.Windowed = False
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD
d3dpp.BackBufferFormat = DispMode.Format
d3dpp.BackBufferWidth = DispMode.Width
d3dpp.BackBufferHeight = DispMode.Height
d3dpp.BackBufferCount = 1
d3dpp.EnableAutoDepthStencil = True
d3dpp.AutoDepthStencilFormat = D3DFMT_D16
Set d3dDevice = d3d.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Me.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)
d3dDevice.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
d3dDevice.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE
d3dDevice.SetRenderState D3DRS_LIGHTING, False
d3dDevice.SetRenderState D3DRS_FOGCOLOR, SkyColor
d3dDevice.SetRenderState D3DRS_FOGDENSITY, &H3D800000
d3dDevice.SetRenderState D3DRS_FOGTABLEMODE, D3DFOG_EXP
d3dDevice.SetRenderState D3DRS_FOGENABLE, True
SizeOfVertex = Len(v)
D3DXMatrixPerspectiveFovLH Mtrx, pi / 3, 0.75, 0.1, 100
d3dDevice.SetTransform D3DTS_PROJECTION, Mtrx
D3DXMatrixRotationY Mtrx, 0
d3dDevice.SetTransform D3DTS_WORLD, Mtrx
End Sub
Private Sub InitGeometry()
Dim Vert(NumTri * 3 - 1) As vFormat
Dim n As Long, x As Single, y As Single, z As Single, c As Long
Set vBuffer = d3dDevice.CreateVertexBuffer((NumTri * 3) * SizeOfVertex, 0, vFlag, D3DPOOL_DEFAULT)
Vert(0).Position = vec3(-10, 0, -10)
Vert(0).Color = &H404040
Vert(1).Position = vec3(-10, 0, 10)
Vert(1).Color = &H404040
Vert(2).Position = vec3(10, 0, -10)
Vert(2).Color = &H404040
Vert(3).Position = vec3(-10, 0, 10)
Vert(3).Color = &H404040
Vert(4).Position = vec3(10, 0, 10)
Vert(4).Color = &H404040
Vert(5).Position = vec3(10, 0, -10)
Vert(5).Color = &H404040
For n = 2 To NumTri - 1
x = Rnd * 20 - 10
y = Rnd * 3
z = Rnd * 20 - 10
c = Rnd * &HFFFFFF
Vert(n * 3 + 0).Position = vec3(x + Rnd, y + Rnd, z + Rnd)
Vert(n * 3 + 0).Color = c
Vert(n * 3 + 1).Position = vec3(x + Rnd, y + Rnd, z + Rnd)
Vert(n * 3 + 1).Color = c
Vert(n * 3 + 2).Position = vec3(x + Rnd, y + Rnd, z + Rnd)
Vert(n * 3 + 2).Color = c
Next n
D3DVertexBuffer8SetData vBuffer, 0, SizeOfVertex * (NumTri * 3), 0, Vert(0)
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Keyb(KeyCode) = True
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Keyb(KeyCode) = False
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set vBuffer = Nothing
Set d3dDevice = Nothing
Set d3dx = Nothing
Set d3d = Nothing
Set dx = Nothing
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 Render()
d3dDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, SkyColor, 1, 0
d3dDevice.BeginScene
d3dDevice.SetStreamSource 0, vBuffer, SizeOfVertex
d3dDevice.SetVertexShader vFlag
d3dDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, NumTri
d3dDevice.EndScene
d3dDevice.Present ByVal 0, ByVal 0, 0, ByVal 0
End Sub
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 3