Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: ScottyBrosious on December 24, 2006

Title: Opengl Sphere's
Post by: ScottyBrosious on December 24, 2006
Does anyone know how to program
OpenGL Sphere's in freebasic?
I could use some demo code
explaining how to use it.
Thanks!
Title: Re: Opengl Sphere's
Post by: Jim on December 26, 2006
Hi ScottyBrosious, welcome to the forums!

The very easiest way is to use glu, the GL Utility library.  It has spheres and a few other primitive shapes built in.  If not then you'll need to build your own.  There are 2 main ways to construct a sphere, one which is just a set of horizontal circular slices, and another which uses an icosohedron as a starting point and which then subdivides all the faces to produce what's known as a geodesic sphere.
The important information you need is, for all points on a sphere, x^2+y^2+z^2 = radius^2.
If you need more help with any of that please ask away.

Jim
Title: Re: Opengl Sphere's
Post by: ninogenio on December 27, 2006
actually could you go into it a little further jim as im very intrested in this too.

i was messing around earlyer and got to this for the verts.

Code: [Select]
         dim temp as integer = 0
         dim as double rad2
         dim as double rad = 5
         for y = -rad to rad
             
               rad2 = y^2
               for x = 0 to rad2
           
                     s_model.vertex(temp).x = x+rad2
                     s_model.vertex(temp).y = y
                     s_model.vertex(temp).z = z+rad2
                 
                 
                     print "x: " , s_model.vertex(temp).x
                     print "y: " , s_model.vertex(temp).y
                     print "z: " , s_model.vertex(temp).z
                     
                     temp += 1
                     
                next
               
         next
         
         sleep

obviously this is wrong but i just cant get my head around creating a static 3d sphere.
Title: Re: Opengl Sphere's
Post by: Jim on December 27, 2006
You want the radius at any given y slice, which is
Code: [Select]
radius_at_y = sqrt(radius^2 - y^2)
Then for the x loop, you want to be drawing a circle of that radius.  So you want to go round a whole circle or 360degress or 2*PI radians.  I think fb uses radians.  You need to split that circle into segments, I'll use 10
So
Code: [Select]
a=0
astep = 2*PI/10
for s = 0 to 9
  x = cos(a) * radius_at_y
  z = sin(a) * radius_at_y
  y = ...we already have y
  a=a+astep
next
I think that's it.  You might want to increase the step rate of y in your For loop.

Remember at y = -rad or +rad, the radius_at_y will be 0 and all the x and z points will be the same.  That's good because this is the pole of the sphere.  In my engine I don't generate this circle.  I make triangles at the top and bottom and quads for the rest of the shape.

Jim
Title: Re: Opengl Sphere's
Post by: .:] Druid [:. on December 28, 2006
I'm really interrested in that GLU (first time I heard about it :s)

Do you know any good tut about that?
Title: Re: Opengl Sphere's
Post by: taj on December 28, 2006
I'm really interrested in that GLU (first time I heard about it :s)

Do you know any good tut about that?

See useful links thread...
http://dbfinteractive.com/index.php?topic=887.msg16065#msg16065
Title: Re: Opengl Sphere's
Post by: .:] Druid [:. on December 28, 2006
thanks a lot!
Title: Re: Opengl Sphere's
Post by: Dr_D on December 29, 2006
Hey, I've had some people report severe problems with ATI cards+GluQuadrics, so be warned. I'm not trying to be negatve or anything, but it was kinda hard to figure out what the problem was. Just remember that people might start complaning about a display bug, if you don't include an alternative to GluQuadrics. ;)
Title: Re: Opengl Sphere's
Post by: Jim on December 30, 2006
Scary!

Quick bit of research
Quote
       - GLUQuadrics error callback function was
          not being properly initialized. Would crash
          if the GLUQuadrics object ran into an error
          and a callback was not defined by the user.
This happens on ATI FireGL cards.
The fix is to add your own error callback
Code: [Select]
void mygluerrorfunc()
{
  /* an error occurred */
}
And then somewhere in your code you need to call (for each shape you create with gluNewQuadric)
Code: [Select]
  gluQuadricCallback(qobj, GLU_ERROR, mygluerrorfunc);
That will stop the problem.

Jim
Title: Re: Opengl Sphere's
Post by: taj on December 30, 2006
I can honestly say in all the time Ive used gluQuadrics, I havent had any problems on ATI or NVIDIA. I'm amazed!
Title: Re: Opengl Sphere's
Post by: Jim on December 30, 2006
I think it might be a Linux specific problem?  It's hard to know, the errata I found came straight out the middle of what was just a log file.

Jim
Title: Re: Opengl Sphere's
Post by: taj on December 31, 2006
Just remember that people might start complaning about a display bug, if you don't include an alternative to GluQuadrics. ;)

Remember all glu does is make calls to gl...its just a convenience library layered on top of GL. So if glu is failing, its quite possible whatever you do to get around it might fail too, unless you get lucky.
Title: Re: Opengl Sphere's
Post by: Dr_D on December 31, 2006
Whoa... I had no idea about any of this! Nice place to be, I reckon!  :updance: