1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
' Creating own definition
' represente un point3d
Type Point3d
X As Long
Y As Long
Z As Long
End Type
' represente un point2d d'ecran
Type pixel
X As Long
Y As Long
End Type
123 |
un simple form avec un timer droppe dessus : |
' Simple 3D Object (Cube)
' dessin avec methode Line(x,y)-(x2,y2)
' Variable declaration
Const CUBELINES = 12 - 1
Const PI As Double = 3.14152695
Dim CUBE(CUBELINES, 1) As Point3d 'real coords worlds
Dim s(CUBELINES, 1) As pixel ' coords screen
Dim eye As Point3d ' eye position (camera)
Dim maxFormX As Long ' width of screen
Dim maxFormY As Long ' height of screen
Dim centerFormX As Long ' center X form
Dim centerFormY As Long ' height Y form
Dim ANGLE As Double ' rotation angle
Dim strLines(CUBELINES) As String 'hold real coords as string
'explication CUBE: Name of our object
' decrit Coords World 3D(ou Coords Reelles) des sommets du Cube
'X1, Y1, Z1, X2, Y2, Z2
' -10, -10, -10, 10, -10, -10 'Line1(cote 1)
' -10, -10, -10, -10, 10, -10 'Line2
' -10, 10, -10, 10, 10, -10 'You know the rest
' 10, -10, -10, 10, 10, -10
' -10, -10, 10, 10, -10, 10
' -10, -10, 10, -10, 10, 10
' -10, 10, 10, 10, 10, 10
' 10, -10, 10, 10, 10, 10
' -10, -10, 10, -10, -10, -10
' -10, 10, 10, -10, 10, -10
' 10, 10, 10, 10, 10, -10
' 10, -10, 10, 10, -10, -10
Private Sub Form_Load()
' DrawMode
Me.DrawMode = vbPixels
' Scale
Me.Scale (0, 0)-(1000, 1000)
' Screen resolution ou viewport
maxFormX = Me.ScaleWidth
maxFormY = Me.ScaleHeight
' center X,Y form
centerFormX = Me.ScaleWidth / 2
centerFormY = Me.ScaleHeight / 2
' Definition of object
'X1, Y1, Z1, X2, Y2, Z2
strLines(0) = " -10, -10, -10, 10, -10, -10" 'Line1
strLines(1) = "-10, -10, -10, -10, 10, -10" 'Line2
strLines(2) = "-10, 10, -10, 10, 10, -10" 'You know the rest
strLines(3) = "10, -10, -10, 10, 10, -10"
strLines(4) = "-10, -10, 10, 10, -10, 10"
strLines(5) = "-10, -10, 10, -10, 10, 10"
strLines(6) = "-10, 10, 10, 10, 10, 10"
strLines(7) = "10, -10, 10, 10, 10, 10"
strLines(8) = "-10, -10, 10, -10, -10, -10"
strLines(9) = "-10, 10, 10, -10, 10, -10"
strLines(10) = "10, 10, 10, 10, 10, -10"
strLines(11) = "10, -10, 10, 10, -10, -10"
' Read Object Data Cube
Dim i As Integer
Dim tmp(5) As String
For i = 0 To CUBELINES
temp = Split(strLines(i), ",")
CUBE(i, 0).X = CLng(temp(0))
CUBE(i, 0).Y = CLng(temp(1))
CUBE(i, 0).Z = CLng(temp(2))
CUBE(i, 1).X = CLng(temp(3))
CUBE(i, 1).Y = CLng(temp(4))
CUBE(i, 1).Z = CLng(temp(5))
Next
' Set Eye position (c'est la Cameraa ou toi dans le vaiseau d'espace)
' peut etre change
eye.X = 10
eye.Y = 30
eye.Z = 100
' Calculate the eye coordinates(camera)
' convertit coords 3D du cube => repere 3d camera
For i = 0 To CUBELINES
CUBE(i, 0).X = CUBE(i, 0).X - eye.X
CUBE(i, 0).Y = CUBE(i, 0).Y - eye.Y
CUBE(i, 0).Z = CUBE(i, 0).Z - eye.Z
CUBE(i, 1).X = CUBE(i, 1).X - eye.X
CUBE(i, 1).Y = CUBE(i, 1).Y - eye.Y
CUBE(i, 1).Z = CUBE(i, 1).Z - eye.Z
Next i
' Calculate screen coordinates
' convertit coords 3D du cube => repere 2d Ecran
For i = 0 To CUBELINES
s(i, 0).X = maxFormX * (CUBE(i, 0).X / CUBE(i, 0).Z) + centerFormX
s(i, 0).Y = maxFormY * (CUBE(i, 0).Y / CUBE(i, 0).Z) + centerFormY
s(i, 1).X = maxFormX * (CUBE(i, 1).X / CUBE(i, 1).Z) + centerFormX
s(i, 1).Y = maxFormY * (CUBE(i, 1).Y / CUBE(i, 1).Z) + centerFormY
Next i
ANGLE = 0#
Me.Timer1.Interval = 25
Me.Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
AnimationLoop
End Sub
Private Sub AnimationLoop()
Me.Refresh
ANGLE = ANGLE + PI / 180
End Sub
Private Sub Form_Paint()
' Draw object
DrawObject
End Sub
Private Sub DrawObject()
' Rotation des points 3d du cube
Dim j As Integer
Dim pwork As Point3d
For j = 0 To CUBELINES
pwork = CUBE(j, 0)
rotateZ pwork, ANGLE
rotateY pwork, ANGLE
vec2pix pwork, s(j, 0)
pwork = CUBE(j, 1)
rotateZ pwork, ANGLE
rotateY pwork, ANGLE
vec2pix pwork, s(j, 1)
Next j
Me.BackColor = vbWhite
Me.FillColor = vbRed
Me.ForeColor = vbYellow
Me.FillStyle = vbSolid
Me.DrawWidth = 2#
Dim i As Integer
Dim X1, Y1, X2, Y2 As Long
For i = 0 To CUBELINES
X1 = s(i, 0).X
Y1 = s(i, 0).Y
X2 = s(i, 1).X
Y2 = s(i, 1).Y
'pour accelerer on ne dessine pas les cotes non -visibles
' du cube
If CUBE(i, 0).Z < eye.Z And CUBE(i, 1).Z < eye.Z Then
Line (X1, Y1)-(X2, Y2)
End If
Next i
End Sub
Private Sub Form_Resize()
maxFormX = Me.ScaleWidth
maxFormY = Me.ScaleHeight
centerFormX = Me.ScaleWidth / 2
centerFormY = Me.ScaleHeight / 2
' Update screen coordinates on resize
For i = 0 To CUBELINES
s(i, 0).X = maxFormX * (CUBE(i, 0).X / CUBE(i, 0).Z) + centerFormX
s(i, 0).Y = maxFormY * (CUBE(i, 0).Y / CUBE(i, 0).Z) + centerFormY
s(i, 1).X = maxFormX * (CUBE(i, 1).X / CUBE(i, 1).Z) + centerFormX
s(i, 1).Y = maxFormY * (CUBE(i, 1).Y / CUBE(i, 1).Z) + centerFormY
Next i
Me.Refresh
End Sub
' Calculating the eye coordinates(camera)
'& conversion en coords d'ecran
Private Sub vec2pix(objpt As Point3d, scrpix As pixel)
'update eye coords(camera)
objpt.X = objpt.X - eye.X
objpt.Y = objpt.Y - eye.Y
objpt.Z = objpt.Z - eye.Z
If objpt.Z = 0 Then Exit Sub
'update screen coords
scrpix.X = (objpt.X / objpt.Z) * maxFormX + centerFormX
scrpix.Y = (objpt.Y / objpt.Z) * maxFormY + centerFormY
End Sub
' Rotating Point around X-Axis
' objpt : point3d in world coordinates (!)
' alpha : angle to rotate around X-Axis (1 means 0.1 deg)
Private Sub rotateX(objpt As Point3d, alpha As Double)
Dim p As Point3d
p.X = (objpt.X * Math.Cos(alpha) - objpt.Y * Math.Sin(alpha))
p.Y = (objpt.Y * Math.Sin(alpha) + objpt.Y * Math.Cos(alpha))
objpt.X = p.X
objpt.Y = p.Y
End Sub
' Rotating Point around Y-Axis
' objpt : point3d in world coordinates (!)
' beta : angle to rotate around Y-Axis (1 means 0.1 deg)
Private Sub rotateY(objpt As Point3d, beta As Double)
Dim p As Point3d
p.X = (objpt.X * Math.Cos(beta) + objpt.Z * Math.Sin(beta))
p.Z = (objpt.X * -Math.Sin(beta) + objpt.Z * Math.Cos(beta))
objpt.X = p.X
objpt.Z = p.Z
End Sub
' Rotating Point around Z-Axis
' objpt : point3d in world coordinates (!)
' gamma : angle to rotate around Y-Axis (1 means 0.1 deg)
Private Sub rotateZ(objpt As Point3d, gamma As Double)
Dim p As Point3d
p.Y = (objpt.Y * Math.Cos(gamma) - objpt.Z * Math.Sin(gamma))
p.Z = (objpt.Y * Math.Sin(gamma) + objpt.Z * Math.Cos(gamma))
objpt.Y = p.Y
objpt.Z = p.Z
End Sub |
Partager