Author Topic: Opengl Sphere's  (Read 5814 times)

0 Members and 1 Guest are viewing this topic.

Offline ScottyBrosious

  • C= 64
  • **
  • Posts: 25
  • Karma: 3
    • View Profile
Opengl Sphere's
« 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!

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Opengl Sphere's
« Reply #1 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
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Opengl Sphere's
« Reply #2 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.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Opengl Sphere's
« Reply #3 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
Challenge Trophies Won:

Offline .:] Druid [:.

  • freebasic n00b
  • Pentium
  • *****
  • Posts: 563
  • Karma: 47
    • View Profile
    • Intro-Inferno
Re: Opengl Sphere's
« Reply #4 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?
[sheep]: im sure he wants to goto prison.. they didnt get him last time.. he was promised a big cock up his arse.. and no doubt looking forward to it.. lets hope he gets his wish this year.

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Opengl Sphere's
« Reply #5 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
Challenge Trophies Won:

Offline .:] Druid [:.

  • freebasic n00b
  • Pentium
  • *****
  • Posts: 563
  • Karma: 47
    • View Profile
    • Intro-Inferno
Re: Opengl Sphere's
« Reply #6 on: December 28, 2006 »
thanks a lot!
[sheep]: im sure he wants to goto prison.. they didnt get him last time.. he was promised a big cock up his arse.. and no doubt looking forward to it.. lets hope he gets his wish this year.

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Opengl Sphere's
« Reply #7 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. ;)
The Dr. is INsane!!!

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Opengl Sphere's
« Reply #8 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
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Opengl Sphere's
« Reply #9 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!
« Last Edit: December 31, 2006 by taj »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Opengl Sphere's
« Reply #10 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
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Opengl Sphere's
« Reply #11 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.
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Opengl Sphere's
« Reply #12 on: December 31, 2006 »
Whoa... I had no idea about any of this! Nice place to be, I reckon!  :updance:
The Dr. is INsane!!!