3DGDIRenderer/3DGDIRender/3D.vb

343 lines
13 KiB
VB.net
Raw Normal View History

2019-09-27 10:46:19 +01:00
Imports System.Drawing
Public Class Point3D
Implements ICoordinate3D
Protected _x As Single
Protected _y As Single
Protected _z As Single
Public Sub New(x As Single, y As Single, z As Single)
_x = x
_y = y
_z = z
End Sub
Public Property X() As Single Implements IPositionable.X
Get
Return _x
End Get
Set(value As Single)
_x = value
End Set
End Property
Public Property Y() As Single Implements IPositionable.Y
Get
Return _y
End Get
Set(value As Single)
_y = value
End Set
End Property
Public Property Z() As Single Implements IPositionable.Z
Get
Return _z
End Get
Set(value As Single)
_z = value
End Set
End Property
Public Sub RotateX(angle As Single) Implements IRotatable.RotateX
Dim rad As Single = angle * Math.PI / 180
Dim cosa As Single = Math.Cos(rad)
Dim sina As Single = Math.Sin(rad)
Dim yn As Single = _y * cosa - _z * sina
Dim zn As Single = _y * sina + _z * cosa
_y = yn
_z = zn
'Return New Point3D(_x, yn, zn)
End Sub
Public Sub RotateY(angle As Single) Implements IRotatable.RotateY
Dim rad As Single = angle * Math.PI / 180
Dim cosa As Single = Math.Cos(rad)
Dim sina As Single = Math.Sin(rad)
Dim Xn As Single = _z * cosa - _x * sina
Dim Zn As Single = _z * sina + _x * cosa
_x = Xn
_z = Zn
'Return New Point3D(Xn, _y, Zn)
End Sub
Public Sub RotateZ(angle As Single) Implements IRotatable.RotateZ
Dim rad As Single = angle * Math.PI / 180
Dim cosa As Single = Math.Cos(rad)
Dim sina As Single = Math.Sin(rad)
Dim Xn As Single = _x * cosa - _y * sina
Dim Yn As Single = _x * sina + _y * cosa
_x = Xn
_y = Yn
'Return New Point3D(Xn, Yn, _z)
End Sub
Public Sub Scale(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) Implements ICoordinate3D.scalePoint
Dim factor As Single = fov / (viewDistance + _z)
Dim Xn As Single = _x * factor + viewWidth / 2
Dim Yn As Single = _y * factor + viewHeight / 2
_x = Xn
_y = Yn
End Sub
Public Function returnPoint() As Point3D Implements ICoordinate3D.returnPoint
Return Me
End Function
Public Function returnScaledPoint(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) As Point3D Implements ICoordinate3D.returnScaledPoint
Dim factor As Single = fov / (viewDistance + _z)
Dim Xn As Single = _x * factor + viewWidth / 2
Dim Yn As Single = _y * factor + viewHeight / 2
Return New Point3D(Xn, Yn, _z)
End Function
End Class
Public Class Polygon3D
Implements ICoordinates3D
Protected pos As Point3D
Protected _points As PointF()
Public Sub New(position As Point3D, ParamArray points As PointF())
pos = position
_points = points
End Sub
Public Property X() As Single Implements IPositionable.X
Get
Return pos.X
End Get
Set(value As Single)
pos.X = value
End Set
End Property
Public Property Y() As Single Implements IPositionable.Y
Get
Return pos.Y
End Get
Set(value As Single)
pos.Y = value
End Set
End Property
Public Property Z() As Single Implements IPositionable.Z
Get
Return pos.Z
End Get
Set(value As Single)
pos.Z = value
End Set
End Property
Public Sub RotateX(angle As Single) Implements IRotatable.RotateX
pos.RotateX(angle)
'Return New Polygon3D(pos, _points)
End Sub
Public Sub RotateY(angle As Single) Implements IRotatable.RotateY
pos.RotateY(angle)
'Return New Polygon3D(pos, _points)
End Sub
Public Sub RotateZ(angle As Single) Implements IRotatable.RotateZ
pos.RotateZ(angle)
Dim ps(_points.Length - 1) As PointF
For i As Integer = 0 To _points.Length - 1 Step 1
'ps(i) = _points(i)
Dim rad As Single = angle * Math.PI / 180
Dim cosa As Single = Math.Cos(rad)
Dim sina As Single = Math.Sin(rad)
Dim Xn As Single = _points(i).X * cosa - _points(i).Y * sina
Dim Yn As Single = _points(i).X * sina + _points(i).Y * cosa
ps(i) = New PointF(Xn, Yn)
Next
_points = ps
'Return New Polygon3D(pos, ps)
End Sub
Public Function returnPoint() As Point3D Implements ICoordinate3D.returnPoint
Return pos
End Function
Public Function returnPoints() As Point3D() Implements ICoordinates3D.returnPoints
Dim pnts(_points.Length - 1) As Point3D
For i1 = 0 To _points.Length - 1
Dim p As PointF = _points(i1)
pnts(i1) = New Point3D(p.X + pos.X, p.Y + pos.Y, 0 + pos.Z)
Next
Return pnts
End Function
Public Sub scalePoint(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) Implements ICoordinate3D.scalePoint
Dim factor As Single = fov / (viewDistance + pos.Z)
Dim Xn As Single = pos.X * factor + viewWidth / 2
Dim Yn As Single = pos.Y * factor + viewHeight / 2
pos.X = Xn
pos.Y = Yn
'Return New Point3D(Xn, Yn, 0 + pos.Z)
End Sub
Public Function returnScaledPoint(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) As Point3D Implements ICoordinate3D.returnScaledPoint
Dim factor As Single = fov / (viewDistance + pos.Z)
Dim Xn As Single = pos.X * factor + viewWidth / 2
Dim Yn As Single = pos.Y * factor + viewHeight / 2
Return New Point3D(Xn, Yn, 0 + pos.Z)
End Function
Public Sub scalePoints(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) Implements ICoordinates3D.scalePoints
Dim pnts(_points.Length - 1) As PointF
For i1 = 0 To _points.Length - 1
Dim p As PointF = _points(i1)
Dim factor As Single = fov / (viewDistance + pos.Z)
Dim Xn As Single = (p.X + pos.X) * factor + viewWidth / 2
Dim Yn As Single = (p.Y + pos.Y) * factor + viewHeight / 2
pnts(i1) = New PointF(Xn, Yn)
Next
_points = pnts
End Sub
Public Function returnScaledPoints(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) As Point3D() Implements ICoordinates3D.returnScaledPoints
Dim pnts(_points.Length - 1) As Point3D
For i1 = 0 To _points.Length - 1
Dim p As PointF = _points(i1)
Dim factor As Single = fov / (viewDistance + pos.Z)
Dim Xn As Single = (p.X + pos.X) * factor + viewWidth / 2
Dim Yn As Single = (p.Y + pos.Y) * factor + viewHeight / 2
pnts(i1) = New Point3D(Xn, Yn, 0 + pos.Z)
Next
Return pnts
End Function
End Class
Public Class Mesh3D
Implements ICoordinates3D
Protected pos As Point3D
Protected _points As Point3D()
Public Sub New(position As Point3D, ParamArray points As Point3D())
pos = position
_points = points
End Sub
Public Sub New(polygon As Polygon3D)
pos = polygon.returnPoint()
_points = polygon.returnPoints()
For i As Integer = 0 To _points.Length - 1
Dim p As New Point3D(_points(i).X - pos.X, _points(i).Y - pos.Y, _points(i).Z - pos.Z)
_points(i) = p
Next
End Sub
Public Property X() As Single Implements IPositionable.X
Get
Return pos.X
End Get
Set(value As Single)
pos.X = value
End Set
End Property
Public Property Y() As Single Implements IPositionable.Y
Get
Return pos.Y
End Get
Set(value As Single)
pos.Y = value
End Set
End Property
Public Property Z() As Single Implements IPositionable.Z
Get
Return pos.Z
End Get
Set(value As Single)
pos.Z = value
End Set
End Property
Public Sub RotateX(angle As Single) Implements IRotatable.RotateX
pos.RotateX(angle)
For i As Integer = 0 To _points.Length - 1 Step 1
_points(i).RotateX(angle)
Next
'Return New Mesh3D(pos, ps)
End Sub
Public Sub RotateY(angle As Single) Implements IRotatable.RotateY
pos.RotateY(angle)
Dim ps(_points.Length - 1) As Point3D
For i As Integer = 0 To _points.Length - 1 Step 1
_points(i).RotateY(angle)
Next
'Return New Mesh3D(pos, ps)
End Sub
Public Sub RotateZ(angle As Single) Implements IRotatable.RotateZ
pos.RotateZ(angle)
Dim ps(_points.Length - 1) As Point3D
For i As Integer = 0 To _points.Length - 1 Step 1
_points(i).RotateZ(angle)
Next
'Return New Mesh3D(pos, ps)
End Sub
Public Function returnPolygons(facesArray As Integer()()) As Polygon3D()
Dim pg As New List(Of Polygon3D)
For i As Integer = 0 To facesArray.Length - 1
'For Each face As Integer() In facesArray
Dim face As Integer() = facesArray(i)
Dim pnts As New List(Of PointF)
For j As Integer = 0 To face.Length - 1
Dim indx As Integer = face(j)
pnts.Add(New PointF(_points(indx).X, _points(indx).Y))
Next
If face.Count > 0 Then _
pg.Add(New Polygon3D(pos, pnts.ToArray))
Next
Return pg.ToArray
End Function
Public Function returnFlatMeshes(facesArray As Integer()()) As Mesh3D()
Dim pg As New List(Of Mesh3D)
For i As Integer = 0 To facesArray.Length - 1
'For Each face As Integer() In facesArray
Dim face As Integer() = facesArray(i)
Dim pnts As New List(Of Point3D)
For j As Integer = 0 To face.Length - 1
Dim indx As Integer = face(j)
pnts.Add(_points(indx))
Next
If face.Count > 0 Then _
pg.Add(New Mesh3D(pos, pnts.ToArray))
Next
Return pg.ToArray
End Function
Public Function returnPoint() As Point3D Implements ICoordinate3D.returnPoint
Return pos
End Function
Public Function returnPoints() As Point3D() Implements ICoordinates3D.returnPoints
Dim pnts(_points.Length - 1) As Point3D
For i1 = 0 To _points.Length - 1
Dim p As Point3D = _points(i1)
pnts(i1) = New Point3D(p.X + pos.X, p.Y + pos.Y, p.Z + pos.Z)
Next
Return pnts
End Function
Public Sub scalePoint(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) Implements ICoordinate3D.scalePoint
Dim factor As Single = fov / (viewDistance + pos.Z)
Dim Xn As Single = pos.X * factor + viewWidth / 2
Dim Yn As Single = pos.Y * factor + viewHeight / 2
pos.X = Xn
pos.Y = Yn
'Return New Point3D(Xn, Yn, 0 + pos.Z)
End Sub
Public Function returnScaledPoint(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) As Point3D Implements ICoordinate3D.returnScaledPoint
Dim factor As Single = fov / (viewDistance + pos.Z)
Dim Xn As Single = pos.X * factor + viewWidth / 2
Dim Yn As Single = pos.Y * factor + viewHeight / 2
Return New Point3D(Xn, Yn, 0 + pos.Z)
End Function
Public Sub scalePoints(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) Implements ICoordinates3D.scalePoints
Dim pnts(_points.Length - 1) As Point3D
For i1 = 0 To _points.Length - 1
Dim p As Point3D = _points(i1)
Dim factor As Single = fov / (viewDistance + pos.Z)
Dim Xn As Single = (p.X + pos.X) * factor + viewWidth / 2
Dim Yn As Single = (p.Y + pos.Y) * factor + viewHeight / 2
pnts(i1) = New Point3D(Xn, Yn, p.Z + pos.Z)
Next
_points = pnts
End Sub
Public Function returnScaledPoints(viewWidth As Single, viewHeight As Single, fov As Single, viewDistance As Single) As Point3D() Implements ICoordinates3D.returnScaledPoints
Dim pnts(_points.Length - 1) As Point3D
For i1 = 0 To _points.Length - 1
Dim p As Point3D = _points(i1)
Dim factor As Single = fov / (viewDistance + pos.Z)
Dim Xn As Single = (p.X + pos.X) * factor + viewWidth / 2
Dim Yn As Single = (p.Y + pos.Y) * factor + viewHeight / 2
pnts(i1) = New Point3D(Xn, Yn, p.Z + pos.Z)
Next
Return pnts
End Function
End Class