Help-trig for sphere z points

BlitzMax Forums/BlitzMax OpenGL Programming/Help-trig for sphere z points



Ok it's a little rushed, but I'll show what I have so far...
Right now, this demo uses the blitzmax lists to store point objects but common sence would dictate the use of the gl lists.
Basically there is one list called sphere_list. This list is what the create functions throw their data into and the objects get drawn in the while loop.

Don't be too harsh, I just started OpenGL recently and dont understand the more complex features.

also please not that the lofted cone command without the x,y and z points if for drawing the other lofted cone2 command is used for belts in the spheres and tori. The sphere and torus were the most difficult shapes for me because finding points for them meant moving along all 3 axes.

If you change the draw command to line loop in the sphere and torius commands you will notice that an extra set of lines happens, it the problem lies within the second draw lofted cone command.

I have really enjoyed the puzzles and challanged of constructing these shapes. They still have a minor problem with the draw lofted cone2 command in the sphere and torus though as stated previously.

I have written a set color hsv command too!

the code:
PLEASE NOTE THAT THE SPHERE AND TORUS ROUTINES USE THE SHPERE_LIST FOR DRAWING THE OTHERS DON'T YET.

Strict
GLGraphics 800,600

glEnable GL_DEPTH_TEST								'Enables Depth Testing
glMatrixMode GL_PROJECTION						'Select The Projection Matrix
glLoadIdentity										'Reset The Projection Matrix
glOrtho(0,640,480,0,-1000,1000)				'Setup The Projection Matrix Frustum
glMatrixMode GL_MODELVIEW							'Select The ModelView Matrix
glLoadIdentity	
' set some rotation values for  checking
Global rotx%=0
Global roty%=0

Global sphere_list:TList = New TList
Function setdaycolor(r#,g#,b#,r2#,g2#,b2#,percent#)
	Local r_dif#=r2-r
	Local g_dif#=g2-g
	Local b_dif#=b2-b
	glColor3f r+(r_dif*percent),g+(g_dif*percent),b+(b_dif*percent)
End Function
Function setHSV(H#,S#,V#)
'H=0-360
'S=0-1
'v=0-255


While H < 0
	H:+360
Wend
While H> 360
	H:-360
Wend
While S <0
	s:+1
Wend
While s>1
	s:-1
Wend
While V<0
	V:+255
Wend
While V> 255
	v:-255
Wend

Local Hi%=(H/60) Mod 6.0

Local f#=(H/60)-Hi
Local p#=V*(1-S)
Local q#=V*(1-(f*S))
Local t#=V*(1-((1-f)*s))
Local R#=0
Local G#=0
Local B#=0

If Hi = 0
	R=V
	G=t
	B=P
EndIf

If Hi = 1
	R=q
	G=V
	B=P
EndIf

If Hi = 2
	R=p
	G=V
	B=t
EndIf

If Hi = 3
	R=p
	G=q
	B=V
EndIf

If Hi = 4
	R=t
	G=p
	B=V
EndIf

If Hi = 5
	R=V
	G=p
	B=q
EndIf
glcolor3f R/255.0,g/255.0,b/255.0

EndFunction
Global thing_list:TList = New TList
Function create_cuboid(width#,height#,depth#)
	Local half_width#=width/2.0
	Local half_height#=height/2.0
	Local half_depth#=depth/2
	'save first point
	Local a:point = New point
	a.x=-half_width
	a.y=-half_height
	a.z=-half_depth
	' save second point
	Local b:point = New point
	b.x=half_width
	b.y=-half_height
	b.z=-half_depth
	' save thrid point
	Local c:point = New point
	c.x=half_width
	c.y=-half_height
	c.z=half_depth
	' save the foruth point
	Local d:point = New point
	d.x=half_width
	d.y=half_height
	d.z=half_depth
	' save the fifth point
	Local e:point = New point
	e.x=-half_width
	e.y=half_height
	e.z=half_depth
	'save the sixth point
	Local f:point = New point
	f.x=-half_width
	f.y=half_height
	f.z=-half_depth
	' save the seventh point
	Local g:point = New point
	g.x=half_width
	g.y=half_height
	g.z=-half_depth
	'save the eigth point
	Local h:point = New point
	h.x=-half_width
	h.y=-half_height
	h.z=half_depth
	


	
	glBegin GL_QUADS
	
	'face 1
	glVertex3f a.x,a.y,a.z
	glVertex3f b.x,b.y,b.z
	glVertex3f g.x,g.y,g.z
	glVertex3f f.x,f.y,f.z
	'face 2
	glVertex3f a.x,a.y,a.z
	glVertex3f h.x,h.y,h.z
	glVertex3f e.x,e.y,e.z
	glVertex3f f.x,f.y,f.z
	' face 3
	glVertex3f h.x,h.y,h.z
	glVertex3f c.x,c.y,c.z
	glVertex3f d.x,d.y,d.z
	glVertex3f e.x,e.y,e.z
	' face 4
	glVertex3f b.x,b.y,b.z
	glVertex3f c.x,c.y,c.z
	glVertex3f d.x,d.y,d.z
	glVertex3f g.x,g.y,g.z
	'face 5
	glVertex3f g.x,g.y,g.z
	glVertex3f d.x,d.y,d.z
	glVertex3f e.x,e.y,e.z
	glVertex3f f.x,f.y,f.z
	' face 6
	glVertex3f a.x,a.y,a.z
	glVertex3f b.x,b.y,b.z
	glVertex3f c.x,c.y,c.z
	glVertex3f h.x,h.y,h.z
	glend
End Function
Function create_cone(width#,height#,sides#,cap#)
	Local sub_angle#=360.0/sides

		For Local i#=0 To sides-1
			
			Local x1#=Cos(i*sub_angle)*(width/2.0)
			Local y1#= height/2.0
			Local z1#=Sin(i*sub_angle)*(width/2.0)
			Local x2#=Cos((i+1)*sub_angle)*(width/2.0)
			Local z2#=Sin((i+1)*sub_angle)*(width/2.0)
			Local color#=(i*sub_angle)/360
			glBegin GL_TRIANGLES
				glcolor3f 1,1,0
				glVertex3f x1,-y1,z1
				' second line point 2
				glcolor3f 1,0,1
				glVertex3f x2,-y1,z2
				glVertex3f 0,y1,0
				glEnd
				' take care of cap
				If cap <> 0
					glBegin GL_triangles
				glcolor3f 1,1,0
				glVertex3f x1,-y1,z1
				' second line point 2
				glcolor3f 0,0,1
				glVertex3f x2,-y1,z2
				glVertex3f 0,-y1,0
				glEnd
				End If
		Next
EndFunction
Function create_tube(width#,height#,sides#,cap#)
	Local sub_angle#=360.0/sides

	
		
		For Local i#=0 To sides-1
			glBegin GL_QUADS
			Local x1#=Cos(i*sub_angle)*(width/2.0)
			Local y1#= height/2.0
			Local z1#=Sin(i*sub_angle)*(width/2.0)
			Local x2#=Cos((i+1)*sub_angle)*(width/2.0)
			Local z2#=Sin((i+1)*sub_angle)*(width/2.0)
			' first  line point 1
			glcolor3f 1,0,0
			glVertex3f x1,y1,z1
			'first line point 2
			glcolor3f 1.0,1.0,0
			glVertex3f x1,-y1,z1
			
			' second line point 2
			glVertex3f x2,-y1,z2
			' second line point 1
			glVertex3f x2,y1,z2
			glEnd
			
			If cap <> 0
			glBegin GL_TRIANGLES
				glcolor3f 1,1,0
				glVertex3f x1,-y1,z1
				' second line point 2
				glcolor3f 0,0,1
				glVertex3f x2,-y1,z2
				glVertex3f 0,-y1,0
				glEnd
				
				glBegin GL_TRIANGLES
				glcolor3f 1,0,0
			glVertex3f x1,y1,z1
				' second line point 2
				glcolor3f 0,0,1
				glVertex3f x2,y1,z2
				glVertex3f 0,y1,0
				glEnd	
			EndIf
		Next
		
	
End Function
Function create_lofted_cone(height#,sides#,radius1#,radius2#,cap#)
Local sub_angle#=360.0/sides
	
	glBegin GL_line_loop
		For Local i#=0 To sides
			
			Local x1#=Cos(sub_angle*i)*radius1
			Local y1#=height/2
			Local z1#=Sin(sub_angle*i)*radius1
			Local x2#=Cos(sub_angle*(i+1))*radius2
			Local y2#=height/-2
			Local z2#=Sin(sub_angle*(i+1))*radius2
			' create a point and save it to the list
			Local a:point = New point
			a.x=x1
			a.y=y1
			a.z=z1
			ListAddLast(sphere_list,a)
			glcolor3f i/sides,1,1
			glVertex3f x1,y1,z1
			Local b:point = New point
			b.x=x2
			b.y=y2
			b.z=z2
			ListAddLast(sphere_list,b)
			glcolor3f 0,0,i/sides
			glVertex3f x2,y2,z2
			
		Next
	glEnd
EndFunction
Function create_lofted_cone2(x#,y#,z#,height#,sides#,radius1#,radius2#)
' this is made for other drawing commands do not use it on it's own
Local sub_angle#=360.0/sides
	
	
		For Local i#=0 To sides
			
			Local x1#=Cos(sub_angle*i)*radius1
			Local y1#=height/2.0
			Local z1#=Sin(sub_angle*i)*radius1
			Local x2#=Cos(sub_angle*(i+1))*radius2
			Local y2#=height/-2.0
			Local z2#=Sin(sub_angle*(i+1))*radius2
			Local x3#=Cos(sub_angle*(i+2))*radius1
			Local x4#=Cos(sub_angle*(i+2))*radius2
			' create a point and save it to the list
			Local a:point = New point
			a.x=x+x1
			a.y=y+y1
			a.z=z+z1
			ListAddLast(sphere_list,a)
			
			Local b:point = New point
			b.x=x+x2
			b.y=y+y2
			b.z=z+z2
			ListAddLast(sphere_list,b)
	
			
		Next
	glEnd
EndFunction
Function create_sphere(x_divisions#,y_divisions#,radius#)
	Local small_angle#=180.0/y_divisions ' this is the angle used for y divisions
	Local belt_height#=(radius*2.0)/y_divisions
	Local start_y#=-radius+(belt_height/2.0) ' the first point for the first cone
	' now we find the y value for all the belts and their radii
	Local last_width#=0.0
	For Local i=0 To y_divisions-1
		Local height#=Abs(Cos(i*small_angle)*radius-Cos((i+1.0)*small_angle)*radius)
		Local first_radius#=Sin(i*small_angle)*radius
		Local second_radius#=Sin((i+1)*small_angle)*radius
		Local y_location#=-radius+last_width+(height/2.0) ' use this one to test
		glLoadIdentity
		
		
		create_lofted_cone2(0,y_location,0,height,x_divisions,second_radius,first_radius)
		glEnd
		last_width:+height
	Next
End Function
Function create_torus(x_divisions#,y_divisions#,radius#,radius2#)
	

	Local small_angle#=360.0/y_divisions ' this is the angle used for y divisions
	Local belt_height#=(radius*2.0)/y_divisions
	
	' now we find the y value for all the belts and their radii
	Local last_width#=0.0
	For Local i=0 To y_divisions-1
		Local height#=Cos(i*small_angle)*radius-Cos((i+1.0)*small_angle)*radius
		Local first_radius#=Sin(i*small_angle)*radius
		Local second_radius#=Sin((i+1)*small_angle)*radius
		Local y_location#=-radius+last_width+(height/2.0) ' use this one to test
		glLoadIdentity
		
		
		create_lofted_cone2(0,y_location,0,height,x_divisions,second_radius+radius2,first_radius+radius2)
		glEnd
		last_width:+height
	Next
End Function
Type thing
	Field x#,y#,zrot#
	Function create(x#,y#,zrot#)
		Local temp:thing = New thing
		temp.x=x
		temp.y=y
		temp.zrot=zrot
		ListAddLast(thing_list,temp)
	End Function
	Method draw()
	glLoadIdentity
	glTranslatef x,y,0
	glrotatef rotx,1,0,0
	glrotatef roty,0,1,0
	glcolor3f 1,1,1
	glBegin GL_triangle_strip
	
	Local b=0
For Local i:point = EachIn(sphere_list)
Local listlength#=CountList(sphere_list)
'normally this would be a dumb wat to calculate colors but for these few triangles it works
setHSV((b/listlength)*360,33,255)
glVertex3f i.x,i.y,i.z
b:+1
Next

glEnd
	
	
	
	

	

	End Method
End Type
Type point
	Field x#,y#,z
End Type

' create a test ring

	create_torus(24,10,32,256)

	thing.create (320,240,0)






While Not KeyHit( KEY_ESCAPE )
	rotx:+(KeyDown(key_up)-KeyDown(key_down))*3
	roty:+(KeyDown(key_right)-KeyDown(key_left))*3


	glClear GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT
	
	For Local i:thing = EachIn(thing_list)
		
		i.draw()
	Next


glend
	Flip

Wend