It works with my cylinders. Wonder why I have to perform the 0,0,0 rotation difference twice to work 0_o
sync on : sync rate 0 : autocam off
null = make vector3(1)
null = make vector3(2)
null = make vector3(3)
global xstorenorm# = 0
global ystorenorm# = 0
global zstorenorm# = 0
ink rgb(255,0,0),0
box 0,0,8,8
get image 1,0,0,8,8,1
ink rgb(255,255,255),0
make object cube 1,10
texture object 1,1
LiT_CylinderNormal(2, 4.0, 12.0, 30)
`rem out next line and one set of holes will be left out 0_o
perform csg difference 1,2
perform csg difference 1,2
rotate object 2,90,0,0
perform csg difference 1,2
rotate object 2,90,90,0
perform csg difference 1,2
delete object 2
position camera 0,0,-20
do
turn object left 1,0.2
pitch object up 1,0.3
sync
loop
null = delete vector3(1)
null = delete vector3(2)
null = delete vector3(3)
end
`makes a normal cylinder with no ends
function LiT_CylinderNormal(obj, rad#, height#, numpolys)
`the actual number of polys is numpolys*2
`make array to store the x and z points on the circle
dim Point_hold#(numpolys-1, 1)
`set angle value steps fpr each point
step_val# = 360.0/numpolys
`set angle value placeholder
a# = 0
`set array placeholder
b = 0
`calculate points
for i = 0 to numpolys-1
`store z of current point
Point_hold#(b, 0) = sin(a#)*rad#
`store x of current point
Point_hold#(b, 1) = cos(a#)*rad#
inc a#, step_val#
dec b, 1
if b < 0 then b = numpolys-1
next i
`calc U step value
U_Step# = 1.0/numpolys
`make memblock to store mesh data
make memblock obj, 12+(((numpolys*3)*32)*2)
`write mesh data to memblock
write memblock dword obj, 0, 274
write memblock dword obj, 4, 32
write memblock dword obj, 8, (numpolys*3)*2
`store poly number
a = 0
`store memblock position
b = 12
Base_U# = 0.0
`write each poly
for i = 1 to numpolys
for k = 1 to 2
`write each vert of each poly
for j = 1 to 3
if k = 1
if j = 2
offset = 1
if a + offset > numpolys - 1
offset = 0-a
endif
`write vert x pos
write memblock float obj, b, Point_hold#(a+offset, 0)
`write vert y pos
write memblock float obj, b+4, height#/2
`write vert z pos
write memblock float obj, b+8, Point_hold#(a+offset, 1)
`write vert U data
write memblock float obj, b+24, Base_U# + U_Step#
`write vert V data
write memblock float obj, b+28, 0.0
else
`write vert x pos
write memblock float obj, b, Point_hold#(a, 0)
`write vert y pos
if j = 1
write memblock float obj, b+4, height#/2
`write vert U data
write memblock float obj, b+24, Base_U#
`write vert V data
write memblock float obj, b+28, 0.0
else
write memblock float obj, b+4, 0-(height#/2)
`write vert U data
write memblock float obj, b+24, Base_U#
`write vert V data
write memblock float obj, b+28, 1.0
endif
`write vert z pos
write memblock float obj, b+8, Point_hold#(a, 1)
endif
else
if j > 1
offset = 1
if a + offset > numpolys - 1
offset = 0-a
endif
`write vert x pos
write memblock float obj, b, Point_hold#(a+offset, 0)
`write vert y pos
if j = 2
write memblock float obj, b+4, height#/2
`write vert V data
write memblock float obj, b+28, 0.0
else
write memblock float obj, b+4, 0-(height#/2)
`write vert V data
write memblock float obj, b+28, 1.0
endif
`write vert z pos
write memblock float obj, b+8, Point_hold#(a+offset, 1)
`write vert U data
write memblock float obj, b+24, Base_U# + U_Step#
else
`write vert x pos
write memblock float obj, b, Point_hold#(a, 0)
`write vert y pos
write memblock float obj, b+4, 0-(height#/2)
`write vert z pos
write memblock float obj, b+8, Point_hold#(a, 1)
`write vert U data
write memblock float obj, b+24, Base_U#
`write vert U data
write memblock float obj, b+28, 1.0
endif
endif
if j = 3
`calc normals for polys
`Thanks to ADR for posting this code on the DBP forums :)
`acuire vert positions
P1X# = memblock float(obj, (b-64))
P1Y# = memblock float(obj, (b-60))
P1Z# = memblock float(obj, (b-56))
P2X# = memblock float(obj, (b-32))
P2Y# = memblock float(obj, (b-28))
P2Z# = memblock float(obj, (b-24))
P3X# = memblock float(obj, (b))
P3Y# = memblock float(obj, (b+4))
P3Z# = memblock float(obj, (b+8))
Calc_Normal(P1X#,P1Y#,P1Z#,P2X#,P2Y#,P2Z#,P3X#,P3Y#,P3Z#)
write memblock float obj, (b-52), xstorenorm#
write memblock float obj, (b-48), ystorenorm#
write memblock float obj, (b-44), zstorenorm#
write memblock float obj, (b-20), xstorenorm#
write memblock float obj, (b-16), ystorenorm#
write memblock float obj, (b-12), zstorenorm#
write memblock float obj, (b+12), xstorenorm#
write memblock float obj, (b+16), ystorenorm#
write memblock float obj, (b+20), zstorenorm#
endif
inc b, 32
next j
next k
inc a, 1
inc Base_U#, U_Step#
next i
undim Point_hold#(0)
`make mesh from memblock
make mesh from memblock obj, obj
delete memblock obj
`make object from mesh
make object obj, obj, 0
delete mesh obj
set object cull obj, 0
endfunction
Function Calc_Normal(P1X#,P1Y#,P1Z#,P2X#,P2Y#,P2Z#,P3X#,P3Y#,P3Z#)
` -- calculate the two directional vectors for the adj and opp edges...
set vector3 1, P1X#, P1Y#, P1Z#
set vector3 2, P2X#, P2Y#, P2Z#
set vector3 3, P3X#, P3Y#, P3Z#
subtract vector3 2, 2, 1
subtract vector3 3, 3, 1 ` -- vector 3 and 1 are now directional vectors
normalize vector3 2,2 ` -- normalize em
normalize vector3 3,3
cross product vector3 1, 2,3 ` -- use the origin vector (1) to store the face normal
normalize vector3 1,1
xstorenorm# = x vector3(1)
ystorenorm# = y vector3(1)
zstorenorm# = z vector3(1)
endfunction