Thứ Tư, 26 tháng 8, 2015

Làm thế nào để xoay tròn một hình dạng 3D

Câu hỏi :
Làm thế nào để xoay tròn một hình dạng 3D

Trả lời :
Khi chơi các game, có những màn bạn có thể thấy nhân vật của mình có thể được quay 360 độ, nhất là trong phần giới thiệu nhân vật chẳng hạn, làm thế nào chúng ta có thể làm như vậy. Xin mời các bạn tham khảo
Bạn có thể nạp nhiều hình của nhân vật ở các góc độ khác nhau và số lần redraw bằng 360/số lần quay
Dim dAngle As Double

Const NUM_TURNS = 36
Const PI = 3.14159265358979
Const CENTER_X = 4000

Const SRCCOPY = &HCC0020

Private Declare Function StretchBlt Lib ''gdi32'' (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function BitBlt Lib ''gdi32'' (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Private Sub Form_Load()
picForward.AutoSize = True
picBackward.AutoSize = True
picBuffer.Width = picForward.Width
picBuffer.Height = picForward.Height
picDisplay.Width = picForward.Width
picDisplay.Height = picForward.Height
picBuffer.Visible = False
picForward.Visible = False
picBackward.Visible = False
picBuffer.AutoRedraw = True
picForward.AutoRedraw = True
picBackward.AutoRedraw = True
picDisplay.AutoRedraw = True
picForward.BorderStyle = 0
picBackward.BorderStyle = 0
picBuffer.BorderStyle = 0
picDisplay.BorderStyle = 0
End Sub


Private Sub Timer1_Timer()
picBuffer.Cls
If Cos(dAngle * PI / 180) >= 0 Then
Call StretchBlt(picBuffer.hdc, (picForward.Width - Abs(Cos(dAngle * PI / 180) * picForward.Width)) / (2 * Screen.TwipsPerPixelX), 0, Abs(Cos(dAngle * PI / 180) * picForward.Width) / Screen.TwipsPerPixelX, picForward.Height / Screen.TwipsPerPixelY, picForward.hdc, 0, 0, picForward.Width / Screen.TwipsPerPixelX, picForward.Height / Screen.TwipsPerPixelY, SRCCOPY)
ElseIf Cos(dAngle * PI / 180) < 0 Then
Call StretchBlt(picBuffer.hdc, (picBackward.Width - Abs(Cos(dAngle * PI / 180) * picBackward.Width)) / (2 * Screen.TwipsPerPixelX), 0, Abs(Cos(dAngle * PI / 180) * picBackward.Width) / Screen.TwipsPerPixelX, picBackward.Height / Screen.TwipsPerPixelY, picBackward.hdc, 0, 0, picBackward.Width / Screen.TwipsPerPixelX, picBackward.Height / Screen.TwipsPerPixelY, SRCCOPY)
End If

Call BitBlt(picDisplay.hdc, 0, 0, picBuffer.Width / Screen.TwipsPerPixelX, picBuffer.Height / Screen.TwipsPerPixelY, picBuffer.hdc, 0, 0, SRCCOPY)
picDisplay.Refresh
dAngle = dAngle + 360 / NUM_TURNS
dAngle = dAngle Mod 360
End Sub
 

Bạn có thể tải source tại đây

Không có nhận xét nào:

Đăng nhận xét