CreateTorus

Blitz3D Forums/Blitz3D Programming/CreateTorus

BODYPRINT(Posted 2004) [#1]
I was looking for a Torus function, but couldn't find any, so I made this one today.

It is a CreateTorus function for everybody to use.

Should I add it to the Code Archives?

;Create Torus Function
;Written by Philip Merwarth

;CreateTorus(radius#,width#,segments,sides[,parent])
;
;radius# = torus radius
;width# = radius of tube in the torus
;segments = the number of segments around the torus
;sides = the number of segments, or sides of the tube in the torus
;parent = parent entity handle

Function CreateTorus(torrad#,torwidth#,segments,sides,parent=0)

	torusmesh=CreateMesh(parent)
	surf=CreateSurface(torusmesh)
	
	FATSTEP#=360.0/sides
	DEGSTEP#=360.0/segments

	radius#=0
	x#=0
	y#=0
	z#=0
	
	fat#=0
	Repeat
		radius = torrad + (torwidth)*Sin(fat)
		deg#=0
		z=torwidth*Cos(fat)
		Repeat
			x=radius*Cos(deg)
			y=radius*Sin(deg)
			AddVertex surf,x,y,z,x,y,z			
			deg=deg+DEGSTEP	
		Until deg>=360
		fat=fat+FATSTEP
	Until fat>=360
	
	For vert=0 To segments*sides-1
		v0=vert
		v1=vert+segments
		v2=vert+1
		v3=vert+1+segments
		
		If v1>=(segments*sides) Then v1=v1-(segments*sides)
		If v2>=(segments*sides) Then v2=v2-(segments*sides)
		If v3>=(segments*sides) Then v3=v3-(segments*sides)
		
		AddTriangle surf,v0,v1,v2
		AddTriangle surf,v1,v3,v2	
	Next
	
	UpdateNormals torusmesh

	Return torusmesh
End Function


;------EXAMPLE--------------
Graphics3D 800,600,32
SetBuffer BackBuffer()

cam=CreateCamera(dummy)
light=CreateLight(2)

dummy=CreatePivot()

torus=createtorus(45,10,50,36,dummy)
EntityColor torus,255,60,60

MoveEntity cam,0,0,-100

MoveEntity light,-100,100,-100

While Not KeyDown(1)
TurnEntity dummy,0,1,0
TurnEntity torus,1,0,0
UpdateWorld
RenderWorld
Flip 
Wend

End



big10p(Posted 2004) [#2]
Im pretty sure I remember someone else doing torus prims - I could be wrong, though. Yours is very nice but you're not setting the UVs so it can't be textured.

Definately worth putting in the code arcs, though. Well done. :)


BODYPRINT(Posted 2004) [#3]
OK, I fixed the UV coordinates and tested it with a texture :-)
I thought the UV's defaulted to the vertices.

I'm pretty new at creating my own meshes, so this was a good learning curve. Doesn't seem quite as daunting as it used to now.

I also added the all important parent option.

Thanks big10p for letting me know about the UV's :-)


Beaker(Posted 2004) [#4]
In the code archive:
http://www.blitzbasic.com/codearcs/codearcs.php?code=109

Worth doing again tho, if only to learn a lot of stuff. :)


BODYPRINT(Posted 2004) [#5]
Poo !!

I looked in there too !!
I agree though, it was worth going through the process. (Brushed up on my Sin Cos stuff too :-)

Just my opinion, but I think my code is nicer than that one!! But he has more options too.