Help-trig for sphere z points

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

Ryan Burnside(Posted 2007) [#1]
I've been trying for the past two days to get my draw sphere routing working. My problem comes with drawing the z value of the points. My function has seperate values for the horizontal and verticle divisions needed to define a sphere. I just want to get the z part working.

(X and Y values are working nicely)


my code ( I have set the z to zero for now, fix it if you can please.)
Function draw_sphere(x#,y#,z#,x_divisions#,y_divisions#,radius#)
	glLoadIdentity()
	glTranslatef x,y,z
	glRotatef rtri,1,1,1
	
	glBegin GL_POINTS
	For Local a#=0 To y_divisions
		For Local b#=0 To x_divisions
			Local direction#=a*(180.0/y_divisions)' diection for x and y
			Local variable_radius=Sin(b*(360.0/x_divisions))*radius ' make x move in towards tips
			Local variable_radius2=Cos(b*(360.0/x_divisions))*radius
			Local POINT_X#=Cos(direction-90)*variable_radius
			Local POINT_Y#=Sin(direction-90)*radius
			glVertex3f POINT_X,POINT_Y,0
	        Next	
	Next
	glEnd
End Function



Chris C(Posted 2007) [#2]
give us some working code, rather that expecting us to write some support code just so we can run your function and you might get some help...


Ryan Burnside(Posted 2007) [#3]
I've tried about 10 times and this code draws POINTS I would like the faces. I might have done this a stupid way but the points are correctly drawn, :)

Function draw_sphere(x#,y#,z#,x_divisions#,y_divisions#,radius#)

	
	Local sub#=180/y_divisions
	Local sub2#=360/x_divisions
	Local base=0
	For Local a#=0 To x_divisions
		glLoadIdentity()
		glTranslatef x,y,z
		glRotatef a*sub2+rtri,0,1,0
			glBegin GL_points
		For Local i#=0 To y_divisions
			' first point
			Local POINT_X#=Cos((i*sub)-90)*radius
			Local POINT_Y#=Sin((i*sub)-90)*radius
			
			glColor3f 1,1,1
			' draw points
			glVertex3f POINT_X,POINT_Y,0
			
	

			
		Next
		glEnd
	
	Next
	
End Function



tonyg(Posted 2007) [#4]
give us some working code, rather that expecting us to write some support code just so we can run your function and you might get some help...




Ryan Burnside(Posted 2007) [#5]
The second code block I posted works. Did you TRY it?
I posted a working code, it just draws the points, I need faces drawn. I don't know how to define the faces. I had assumed that somebody could make the jump from drawn vertices to drawn faces.

Here is a complete program showing it.
Strict
GLGraphics 640,480
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										'Reset The ModelView Matrix


Function draw_sphere(x#,y#,z#,x_divisions#,y_divisions#,radius#)

	
	Local sub#=180/y_divisions
	Local sub2#=360/x_divisions
	Local base=0
	For Local a#=0 To x_divisions
		glLoadIdentity()
		glTranslatef x,y,z
		glRotatef a*sub2,0,1,0
			glBegin GL_points
		For Local i#=0 To y_divisions
			' first point
			Local POINT_X#=Cos((i*sub)-90)*radius
			Local POINT_Y#=Sin((i*sub)-90)*radius
			
			glColor3f 1,1,1
			' draw points
			glVertex3f POINT_X,POINT_Y,0
			
	

			
		Next
		glEnd
	
	Next
	
End Function

While Not KeyHit( KEY_ESCAPE )

	glClear GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT		'Clear The Screen And The Depth Buffer
draw_sphere(320,240,0,20,20,100)
	Flip

Wend



fredborg(Posted 2007) [#6]
If you want triangle faces you can use this:
glBegin(GL_TRIANGLES)
  ' first triangle
  glVertex3f(0.0,1.0,0.0)
  glVertex3f(1.0,1.0,0.0)
  glVertex3f(1.0,0.0,0.0)
  ' second triangle
 glVertex3f ....
  etc...
glEnd()
If you want quads it's the same except you should use glBegin(GL_QUADS) and have 4 vertices defined per face.


tonyg(Posted 2007) [#7]
Did you TRY it?

expecting us to write some support code



Chris C(Posted 2007) [#8]
bin away, came back, started playing with some old C code I had floating around, then adapted it to max, then though really should stop messing around with this....

Some points to remember, calculating coords using sine and cosine each frame is fairly slow, better to pre-calculate a vertex list of a sphere radius 1 and then scale it

In this case I've even used an opengl display list which on most hardware *really* improve speed as it will optimise the list of opengl calls

I've added texture mapping as UV cords can be a nightmare, each vertex has a slightly different colour too, normally you'd just use white and the texture would colour the whole thing but handily with vertex colouring you can have different coloured textures (quite handy for multiple skins....)



Enjoy!


LAB[au](Posted 2007) [#9]
Nice! Still there are some faces inside the sphere, binding the apex of the first half to the middle of the second half.
This solves it although not elegantly...



Ryan Burnside(Posted 2007) [#10]
These work well thanks for taking the time to help me.


Chris C(Posted 2007) [#11]
np,

ps dont forget to post a complete example if you are posting code again...


jkrankie(Posted 2007) [#12]
Could some please explain which bits of this code make which bits of the sphere. it's useful code, but i don't understand the maths behind the constuction of the sphere.


Function makeSphere(vertList:Tlist)
	Const astep#=15
	For Local b# = 0 Until 90 Step astep
		For Local a# = 0 Until 360 Step astep
			Local v:vert = New vert
			v.x=Sin(a ) * Sin(b ) 	
			v.y = Cos( a ) * Sin(b )
			v.z = Cos( b ) 
			v.u = (2 * b) / 360
			v.v = a / 360
			ListAddLast vertList , v
			v = New vert
			v.x=Sin(a ) * Sin((b+astep) ) 	
			v.y = Cos( a ) * Sin( (b+astep) )
			v.z = Cos( (b+astep) )
			v.u = (2 * (b+astep)) / 360
			v.v = a / 360
			ListAddLast vertList , v
			v = New vert
			v.x=Sin((a+astep) ) * Sin(b ) 	
			v.y = Cos( (a+astep) ) * Sin(b )
			v.z = Cos( b )
			v.u = (2 * b) / 360
			v.v = (a+astep) / 360
			ListAddLast vertList , v
			v = New vert
			v.x=Sin((a+astep) ) * Sin((b+astep) ) 	
			v.y = Cos( (a+astep) ) * Sin((b+astep) )
			v.z = Cos( (b+astep) ) 
			v.u = (2 * (b+astep)) / 360
			v.v = (a+astep) / 360
			ListAddLast vertList , v
		Next
	Next
EndFunction


Cheers
Charlie


Ryan Burnside(Posted 2007) [#13]


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.




Chris C(Posted 2007) [#14]

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


At least you're man enough to share, which ranks you higher than some of the precious Uber haxors here... >:)

I'd drop the lists altogether, I just ended up using them cause I was aping some old C code I found lying around.

@jkrankie
have a play with max2d and coords like....

for local a#=0 until 360 step 7.5
x#=50+sin(a#)*40
y#=50+cos(a#)*40
plot x,y ' errm away from a machine with max on it(plot a pixel here) !!!!
next

try changing a differently ( cos(a*2) ) and generally mess with it

also look at http://www.euclideanspace.com/maths/index.htm
(a real gem that one...!)


jkrankie(Posted 2007) [#15]
thanks Chris.

Cheers
Charlie