Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started 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!
-
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
-
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.
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.
-
You want the radius at any given y slice, which is
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
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
-
I'm really interrested in that GLU (first time I heard about it :s)
Do you know any good tut about that?
-
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
-
thanks a lot!
-
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. ;)
-
Scary!
Quick bit of research
- 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
void mygluerrorfunc()
{
/* an error occurred */
}
And then somewhere in your code you need to call (for each shape you create with gluNewQuadric)
gluQuadricCallback(qobj, GLU_ERROR, mygluerrorfunc);
That will stop the problem.
Jim
-
I can honestly say in all the time Ive used gluQuadrics, I havent had any problems on ATI or NVIDIA. I'm amazed!
-
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
-
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.
-
Whoa... I had no idea about any of this! Nice place to be, I reckon! :updance: