Results 1 to 4 of 4

Thread: Rotating 3D Cube Using VB.NET and GDI+

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    2

    Rotating 3D Cube Using VB.NET and GDI+

    Here I am sharing code to make a rotating cube using just GDI+.
    The code should be divided in 2 files. The code can be run using any .NET IDE of your choice.

    First, I list below the code for the Point3D class that should be saved in a file named Point3D.vb.

    vb.net Code:
    1. '
    2. ' Defines the Point3D class that represents points in 3D space.
    3. ' Developed by leonelmachava <leonelmachava@gmail.com>
    4. ' http://codentronix.com
    5. '
    6. Option Explicit On
    7.  
    8. Public Class Point3D
    9.     Protected m_x As Double, m_y As Double, m_z As Double
    10.  
    11.     Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double)
    12.         Me.X = x
    13.         Me.Y = y
    14.         Me.Z = z
    15.     End Sub
    16.  
    17.     Public Property X() As Double
    18.         Get
    19.             Return m_x
    20.         End Get
    21.         Set(ByVal value As Double)
    22.             m_x = value
    23.         End Set
    24.     End Property
    25.  
    26.     Public Property Y() As Double
    27.         Get
    28.             Return m_y
    29.         End Get
    30.         Set(ByVal value As Double)
    31.             m_y = value
    32.         End Set
    33.     End Property
    34.  
    35.     Public Property Z() As Double
    36.         Get
    37.             Return m_z
    38.         End Get
    39.         Set(ByVal value As Double)
    40.             m_z = value
    41.         End Set
    42.     End Property
    43.  
    44.     Public Function RotateX(ByVal angle As Integer) As Point3D
    45.         Dim rad As Double, cosa As Double, sina As Double, yn As Double, zn As Double
    46.  
    47.         rad = angle * Math.PI / 180
    48.         cosa = Math.Cos(rad)
    49.         sina = Math.Sin(rad)
    50.         yn = Me.Y * cosa - Me.Z * sina
    51.         zn = Me.Y * sina + Me.Z * cosa
    52.         Return New Point3D(Me.X, yn, zn)
    53.     End Function
    54.  
    55.     Public Function RotateY(ByVal angle As Integer) As Point3D
    56.         Dim rad As Double, cosa As Double, sina As Double, Xn As Double, Zn As Double
    57.  
    58.         rad = angle * Math.PI / 180
    59.         cosa = Math.Cos(rad)
    60.         sina = Math.Sin(rad)
    61.         Zn = Me.Z * cosa - Me.X * sina
    62.         Xn = Me.Z * sina + Me.X * cosa
    63.  
    64.         Return New Point3D(Xn, Me.Y, Zn)
    65.     End Function
    66.  
    67.     Public Function RotateZ(ByVal angle As Integer) As Point3D
    68.         Dim rad As Double, cosa As Double, sina As Double, Xn As Double, Yn As Double
    69.  
    70.         rad = angle * Math.PI / 180
    71.         cosa = Math.Cos(rad)
    72.         sina = Math.Sin(rad)
    73.         Xn = Me.X * cosa - Me.Y * sina
    74.         Yn = Me.X * sina + Me.Y * cosa
    75.         Return New Point3D(Xn, Yn, Me.Z)
    76.     End Function
    77.  
    78.     Public Function Project(ByVal viewWidth, ByVal viewHeight, ByVal fov, ByVal viewDistance)
    79.         Dim factor As Double, Xn As Double, Yn As Double
    80.         factor = fov / (viewDistance + Me.Z)
    81.         Xn = Me.X * factor + viewWidth / 2
    82.         Yn = Me.Y * factor + viewHeight / 2
    83.         Return New Point3D(Xn, Yn, Me.Z)
    84.     End Function
    85. End Class

    Next, you should create a Windows Form, and add the code below. Save the form as Main.vb

    vb.net Code:
    1. '
    2. ' Simulation of a Rotating Cube using GDI+
    3. ' Developed by leonelmachava <leonelmachava@gmail.com>
    4. ' http://codentronix.com
    5. '
    6. Imports System.Drawing.Graphics
    7. Imports System.Drawing.Color
    8. Imports System.Drawing.Brush
    9. Imports System.Drawing.Point
    10.  
    11. Public Class Main
    12.     Protected m_timer As Timer
    13.     Protected m_vertices(8) As Point3D
    14.     Protected m_faces(6, 4) As Integer
    15.     Protected m_colors(6) As Color
    16.     Protected m_brushes(6) As Brush
    17.     Protected m_angle As Integer
    18.  
    19.     Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    20.         ' Enable double-buffering to eliminate flickering.
    21.         Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    22.         Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
    23.  
    24.         InitCube()
    25.  
    26.         ' Create the timer.
    27.         m_timer = New Timer()
    28.  
    29.         ' Set the timer interval to 25 milliseconds. This will give us 1000/25 ~ 40 frames per second.
    30.         m_timer.Interval = 25
    31.  
    32.         ' Set the callback for the timer.
    33.         AddHandler m_timer.Tick, AddressOf AnimationLoop
    34.  
    35.         ' Start the timer.
    36.         m_timer.Start()
    37.     End Sub
    38.  
    39.     Private Sub InitCube()
    40.         ' Create the cube vertices.
    41.         m_vertices = New Point3D() {
    42.                      New Point3D(-1, 1, -1),
    43.                      New Point3D(1, 1, -1),
    44.                      New Point3D(1, -1, -1),
    45.                      New Point3D(-1, -1, -1),
    46.                      New Point3D(-1, 1, 1),
    47.                      New Point3D(1, 1, 1),
    48.                      New Point3D(1, -1, 1),
    49.                      New Point3D(-1, -1, 1)}
    50.  
    51.         ' Create an array representing the 6 faces of a cube. Each face is composed by indices to the vertex array
    52.         ' above.
    53.         m_faces = New Integer(,) {{0, 1, 2, 3}, {1, 5, 6, 2}, {5, 4, 7, 6}, {4, 0, 3, 7}, {0, 4, 5, 1}, {3, 2, 6, 7}}
    54.  
    55.         ' Define the colors of each face.
    56.         m_colors = New Color() {Color.BlueViolet, Color.Cyan, Color.Green, Color.Yellow, Color.Violet, Color.LightSkyBlue}
    57.  
    58.         ' Create the brushes to draw each face. Brushes are used to draw filled polygons.
    59.         For i = 0 To 5
    60.             m_brushes(i) = New SolidBrush(m_colors(i))
    61.         Next
    62.     End Sub
    63.  
    64.     Private Sub AnimationLoop()
    65.         ' Forces the Paint event to be called.
    66.         Me.Invalidate()
    67.  
    68.         ' Update the variable after each frame.
    69.         m_angle += 1
    70.     End Sub
    71.  
    72.     Private Sub Main_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    73.         Dim t(8) As Point3D
    74.         Dim f(4) As Integer
    75.         Dim v As Point3D
    76.         Dim avgZ(6) As Double
    77.         Dim order(6) As Integer
    78.         Dim tmp As Double
    79.         Dim iMax As Integer
    80.  
    81.         ' Clear the window
    82.         e.Graphics.Clear(Color.LightBlue)
    83.  
    84.         ' Transform all the points and store them on the "t" array.
    85.         For i = 0 To 7
    86.             Dim b As Brush = New SolidBrush(Color.White)
    87.             v = m_vertices(i)
    88.             t(i) = v.RotateX(m_angle).RotateY(m_angle).RotateZ(Me.m_angle)
    89.             t(i) = t(i).Project(Me.ClientSize.Width, Me.ClientSize.Height, 256, 4)
    90.         Next
    91.  
    92.         ' Compute the average Z value of each face.
    93.         For i = 0 To 5
    94.             avgZ(i) = (t(m_faces(i, 0)).Z + t(m_faces(i, 1)).Z + t(m_faces(i, 2)).Z + t(m_faces(i, 3)).Z) / 4.0
    95.             order(i) = i
    96.         Next
    97.  
    98.         ' Next we sort the faces in descending order based on the Z value.
    99.         ' The objective is to draw distant faces first. This is called
    100.         ' the PAINTERS ALGORITHM. So, the visible faces will hide the invisible ones.
    101.         ' The sorting algorithm used is the SELECTION SORT.
    102.         For i = 0 To 4
    103.             iMax = i
    104.             For j = i + 1 To 5
    105.                 If avgZ(j) > avgZ(iMax) Then
    106.                     iMax = j
    107.                 End If
    108.             Next
    109.             If iMax <> i Then
    110.                 tmp = avgZ(i)
    111.                 avgZ(i) = avgZ(iMax)
    112.                 avgZ(iMax) = tmp
    113.  
    114.                 tmp = order(i)
    115.                 order(i) = order(iMax)
    116.                 order(iMax) = tmp
    117.             End If
    118.         Next
    119.  
    120.         ' Draw the faces using the PAINTERS ALGORITHM (distant faces first, closer faces last).
    121.         For i = 0 To 5
    122.             Dim points() As Point
    123.             Dim index As Integer = order(i)
    124.             points = New Point() {
    125.                 New Point(CInt(t(m_faces(index, 0)).X), CInt(t(m_faces(index, 0)).Y)),
    126.                 New Point(CInt(t(m_faces(index, 1)).X), CInt(t(m_faces(index, 1)).Y)),
    127.                 New Point(CInt(t(m_faces(index, 2)).X), CInt(t(m_faces(index, 2)).Y)),
    128.                 New Point(CInt(t(m_faces(index, 3)).X), CInt(t(m_faces(index, 3)).Y))
    129.             }
    130.             e.Graphics.FillPolygon(m_brushes(index), points)
    131.         Next
    132.     End Sub
    133. End Class

    The code is pretty much self-explanatory.
    Comments are pretty much welcome.
    Last edited by Hack; May 25th, 2011 at 11:35 AM. Reason: Fixed Highlight Tags

  2. #2

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    2

    Re: Rotating 3D Cube Using VB.NET and GDI+

    I have attached a zip file containing the project.
    Attached Files Attached Files

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Rotating 3D Cube Using VB.NET and GDI+

    Cool

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    Hyperactive Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    Near the User32.dll
    Posts
    383

    Re: Rotating 3D Cube Using VB.NET and GDI+

    maybe helps me )
    you cant believe a random 11 year old turkish kid using VB6 and feeling nostalgic to XP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width