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.
This is the data structure
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
This is the function
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
This is the draw function
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 < 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
And this is the set of point and polygon in one cube
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)
This is the preview of the cube that i tried to back face culling with a rotation.
Back-Face culling depends on the order of your vertices on the screen. You assume that either clockwise or counterclockwise are "front" and you draw only those.
In your preview I see that you lack some of the front faces and you draw some back faces that you shouldn't. You just need to find those few broken triangles and revert their direction by swapping 2nd and 3rd vertex.