Using Normals for backface-culling...

Blitz3D Forums/Blitz3D Programming/Using Normals for backface-culling...

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I am using normals for backface culling on b+.
I am experiencing with 3d, as opposet to most of you guys, who know everything about it.
Well, my backface-culling algorythm seems to be working... as long as I don´t rotate around the X-axis.
Here is the code.
Please let me know what is wrong.
And, please, don´t pay too much attention to code correctness or ellegance... I am learning 3d, so I probably made more mistakes than one would explect.
Now, I would like someone to explain to me what I am doing wrong...
Thanks a lot,

Rui Barbosa Jr.

<CODE>
Dim x#(16, 9)
Dim y#(16, 9)
Dim z#(16, 9)
;
Dim rx#(16, 9)
Dim ry#(16, 9)
Dim rz#(16, 9)
;
Dim nx#(16, 9)
Dim ny#(16, 9)
Dim nz#(16, 9)
;
Dim rnx#(16, 9)
Dim rny#(16, 9)
Dim rnz#(16, 9)
;
Global a# = 0.00
Global b# = 0.00
Global g# = 0.00



Graphics 1024, 768, 16, 0



For i% = 0 To 15
For j% = 0 To 8
r# = Sin#(22.50 * j%)
x#(i%, j%) = Cos#(22.50 * i%) * r#
y#(i%, j%) = Cos#(22.50 * j%)
z#(i%, j%) = -Sin#(22.50 * i%) * r#
Next
Next



For i% = 0 To 15
For j% = 0 To 8
x0# = +x#(i%, j%)
y0# = -y#(i%, j%)
z0# = +z#(i%, j%)
x1# = +x#((i% + 1) Mod 16, j%)
y1# = -y#((i% + 1) Mod 16, j%)
z1# = +z#((i% + 1) Mod 16, j%)
x2# = +x#((i% + 1) Mod 16, (j% + 1) Mod 9)
y2# = -y#((i% + 1) Mod 16, (j% + 1) Mod 9)
z2# = +z#((i% + 1) Mod 16, (j% + 1) Mod 9)
x3# = +x#(i%, (j% + 1) Mod 9)
y3# = -y#(i%, (j% + 1) Mod 9)
z3# = +z#(i%, (j% + 1) Mod 9)
If Abs(x2# - x3#) < 0.001 Then
dx1# = x2# - x0#
dy1# = y2# - y0#
dz1# = z2# - z0#
dx2# = x2# - x1#
dy2# = y2# - y1#
dz2# = z2# - z1#
Else
dx1# = x3# - x1#
dy1# = y3# - y1#
dz1# = z3# - z1#
dx2# = x3# - x2#
dy2# = y3# - y2#
dz2# = z3# - z2#
EndIf
nx#(i%, j%) = (dy1# * dz2#) - (dz1# * dy2#)
ny#(i%, j%) = (dz1# * dx2#) - (dx1# * dz2#)
nz#(i%, j%) = (dx1# * dy2#) - (dy1# * dx2#)
l# = Sqr#((nx#(i%, j%) * nx#(i%, j%)) + (ny#(i%, j%) * ny#(i%, j%)) + (nz#(i%, j%) * nz#(i%, j%)))
nx#(i%, j%) = nx#(i%, j%) / l#
ny#(i%, j%) = ny#(i%, j%) / l#
nz#(i%, j%) = nz#(i%, j%) / l#
Next
Next



Repeat
Cls()
rotateVertices(a#, b#, g#)
rotateNormals(a#, b#, g#)
drawSphere()
Flip()
If KeyDown(30) Then
a# = a# + 1.00
While a# > 360.00
a# = a# - 360.00
Wend
EndIf
If KeyDown(44) Then
a# = a# - 1.00
While a# < 0.00
a# = a# + 360.00
Wend
EndIf
If KeyDown(31) Then
b# = b# + 1.00
While b# > 360.00
b# = b# - 360.00
Wend
EndIf
If KeyDown(45) Then
b# = b# - 1.00
While b# < 0.00
b# = b# + 360.00
Wend
EndIf
If KeyDown(32) Then
g# = g# + 1.00
While g# > 360.00
g# = g# - 360.00
Wend
EndIf
If KeyDown(46) Then
g# = g# - 1.00
While g# < 0.00
g# = g# + 360.00
Wend
EndIf
Until KeyHit(1)
End



Function rotateVertices(alpha#, betha#, gamma#)
For i% = 0 To 15
For j% = 0 To 8
xl0# = x#(i%, j%)
yl0# = y#(i%, j%)
zl0# = z#(i%, j%)
xl1# = (Sin#(betha#) * zl0#) + (Cos#(betha#) * xl0#)
yl1# = yl0#
zl1# = (Cos#(betha#) * zl0#) - (Sin#(betha#) * xl0#)
xl2# = xl1#
yl2# = yl1# * Cos#(alpha#) - zl1# * Sin#(alpha#)
zl2# = yl1# * Sin#(alpha#) + zl1# * Cos#(alpha#)
rx#(i%, j%) = yl2# * Sin#(gamma#) + xl2# * Cos#(gamma#)
ry#(i%, j%) = yl2# * Cos#(gamma#) - xl2# * Sin#(gamma#)
rz#(i%, j%) = zl2#
Next
Next
End Function



Function rotateNormals(alpha#, betha#, gamma#)
For i% = 0 To 15
For j% = 0 To 8
xl0# = nx#(i%, j%)
yl0# = ny#(i%, j%)
zl0# = nz#(i%, j%)
xl1# = zl0# * Sin#(betha#) + xl0# * Cos#(betha#)
yl1# = yl0#
zl1# = zl0# * Cos#(betha#) - xl0# * Sin#(betha#)
xl2# = xl1#
yl2# = yl1# * Cos#(alpha#) - zl1# * Sin#(alpha#)
zl2# = yl1# * Sin#(alpha#) + zl1# * Cos#(alpha#)
rnx#(i%, j%) = yl2# * Sin#(gamma#) + xl2# * Cos#(gamma#)
rny#(i%, j%) = yl2# * Cos#(gamma#) - xl2# * Sin#(gamma#)
rnz#(i%, j%) = zl2#
Color 255, 255, 255
Text 100 + 100 * j%, 100 + 20 * i%, rnz#(i%, j%)
Next
Next
End Function



Function drawSphere()
For i% = 0 To 15
For j% = 0 To 7
x0# = +rx#(i%, j%) * 100.00 + 512.00
y0# = -ry#(i%, j%) * 100.00 + 384.00
x1# = +rx#((i% + 1) Mod 16, j%) * 100.00 + 512.00
y1# = -ry#((i% + 1) Mod 16, j%) * 100.00 + 384.00
x2# = +rx#((i% + 1) Mod 16, (j% + 1) Mod 9) * 100.00 + 512.00
y2# = -ry#((i% + 1) Mod 16, (j% + 1) Mod 9) * 100.00 + 384.00
x3# = +rx#(i%, (j% + 1) Mod 9) * 100.00 + 512.00
y3# = -ry#(i%, (j% + 1) Mod 9) * 100.00 + 384.00
; If Abs(x2# - x3#) < 0.01 Then
; dx1# = x2# - x0#
; dy1# = y2# - y0#
; dx2# = x2# - x1#
; dy2# = y2# - y1#
; Else
; dx1# = x3# - x1#
; dy1# = y3# - y1#
; dx2# = x3# - x2#
; dy2# = y3# - y2#
; EndIf
; bf# = (dx1# * (dy2# - dy1#)) - ((dx2# - dx1#) * dy1#)
; If bf# > 0.00 Then
If rnz#(i%, j%) > 0.00 Then
If (j And 1) Then
Color 255, 0, 0
Else
Color 0, 255, 0
EndIf
Line x0#, y0#, x1#, y1#
Line x1#, y1#, x2#, y2#
Line x2#, y2#, x3#, y3#
Line x3#, y3#, x0#, y0#
EndIf
Next
Next
End Function
<ENDCODE>