title: 3D Computer Graphic Animation : Back-Face Culling not working properly
body: <p>So I've tried to do a backface culling on the cube with Visual Basic. I'm sure that the code that i wrote is right, but somehow the it's not the back face that i want.</p>
  
  <p>This is the data structure</p>
  
  <pre><code>Structure TLine
      Dim p1, p2 As Integer
  End Structure
  
  Structure TLine2
      Dim p1, p2 As TPoint
  End Structure
  
  Structure TPoint
      Dim x, y, z, w As Double
  End Structure
  
  Structure TPolygon
      Dim p1, p2, p3 As Integer
  End Structure
  </code></pre>
  
  <p>This is the function</p>
  
  <pre><code>Sub SetPoint(ByRef V As TPoint, ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double)
      V.x = a
      V.y = b
      V.z = c
      V.w = 1
  End Sub
  
  Sub SetLine(ByVal idx As Integer, ByVal p1 As Integer, ByVal p2 As Integer)
      edge(idx).p1 = p1
      edge(idx).p2 = p2
  End Sub
  
  Sub SetPolygon(ByVal idx As Integer, ByVal p1 As Integer, ByVal p2 As Integer, ByVal p3 As Integer)
      polygon(idx).p1 = p1
      polygon(idx).p2 = p2
      polygon(idx).p3 = p3
  End Sub
  </code></pre>
  
  <p>This is the draw function</p>
  
  <pre><code>Sub draw()
      bmp = New Bitmap(400, 400)
      Dim grp As Graphics = Graphics.FromImage(bmp)
      Dim i As Integer
      Dim p As New Pen(Color.Black)
  
      DOP.x = 0
      DOP.y = 0
      DOP.z = -2
  
      For i = 0 To 11
          temp_v1.x = v(polygon(i).p2).x - v(polygon(i).p1).x
          temp_v1.y = v(polygon(i).p2).y - v(polygon(i).p1).y
          temp_v1.z = v(polygon(i).p2).z - v(polygon(i).p1).z
  
          temp_v2.x = v(polygon(i).p3).x - v(polygon(i).p1).x
          temp_v2.y = v(polygon(i).p3).y - v(polygon(i).p1).y
          temp_v2.z = v(polygon(i).p3).z - v(polygon(i).p1).z
  
          normal.x = (temp_v1.y * temp_v2.z) - (temp_v2.y * temp_v1.z)
          normal.y = (temp_v1.z * temp_v2.x) - (temp_v2.z * temp_v1.x)
          normal.z = (temp_v1.x * temp_v2.y) - (temp_v2.x * temp_v1.y)
  
          result = (DOP.x * normal.x) + (DOP.y * normal.y) + (DOP.z * normal.z)
  
          If result &lt; 0 Then
              grp.DrawLine(p, CInt(vs(polygon(i).p1).x), CInt(vs(polygon(i).p1).y), CInt(vs(polygon(i).p2).x), CInt(vs(polygon(i).p2).y))
              grp.DrawLine(p, CInt(vs(polygon(i).p1).x), CInt(vs(polygon(i).p1).y), CInt(vs(polygon(i).p3).x), CInt(vs(polygon(i).p3).y))
              grp.DrawLine(p, CInt(vs(polygon(i).p2).x), CInt(vs(polygon(i).p2).y), CInt(vs(polygon(i).p3).x), CInt(vs(polygon(i).p3).y))
              PictureBox1.Image = bmp
          End If
      Next
      'PictureBox1.Image = bmp
  End Sub
  </code></pre>
  
  <p>And this is the set of point and polygon in one cube </p>
  
  <pre><code>SetPoint(v(0), -1, -1, 1, 1)
      SetPoint(v(1), 1, -1, 1, 1)
      SetPoint(v(2), 1, 1, 1, 1)
      SetPoint(v(3), -1, 1, 1, 1)
      SetPoint(v(4), -1, -1, -1, 1)
      SetPoint(v(5), 1, -1, -1, 1)
      SetPoint(v(6), 1, 1, -1, 1)
      SetPoint(v(7), -1, 1, -1, 1)
  
      SetPolygon(0, 0, 1, 2)
      SetPolygon(1, 0, 3, 2)
      SetPolygon(2, 5, 4, 7)
      SetPolygon(3, 5, 6, 7)
      SetPolygon(4, 3, 2, 6)
      SetPolygon(5, 3, 7, 6)
      SetPolygon(6, 4, 5, 1)
      SetPolygon(7, 4, 0, 1)
      SetPolygon(8, 1, 5, 6)
      SetPolygon(9, 1, 2, 6)
      SetPolygon(10, 4, 0, 3)
      SetPolygon(11, 4, 7, 3)
  </code></pre>
  
  <p>This is the <a href="https://i.stack.imgur.com/o6x4g.gif" rel="nofollow noreferrer">preview</a> of the cube that i tried to back face culling with a rotation.</p>