It is currently Sun Sep 24, 2017 12:19 pm




 Page 1 of 1 [ 13 posts ] 
Author Message
 Post subject: Polygons for a sphere shape
PostPosted: Wed Nov 28, 2007 10:20 am 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
Hi guys and gals,

I'd like to ask if anyone knows of a good way to generate the polygons for a sphere generated from code?

I want to have a sphere shape as a primitive and use the equation for a sphere to generate the vertices for the sphere. However, I don't know how to group vertices to make up the triangles/quads for a poly on the shpere.

Basically want to do something similar to Marek's cube/cylinder classes.

Thanks,
Codeslasher



 


Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Wed Nov 28, 2007 9:57 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
The was that I have done this in the past is like this:

two nested for loops. one going from +90 to -90 degrees and the other going from 0 to 180 degrees. These two angles define where each point will sit on the sphere. the top and bottom of the sphere is created with triangles that connect at the top and bottom points of the sphere (+/-90). All other segments are joined together similar to how the flat grid works.


Last edited by Marek on Mon Jan 07, 2008 9:03 pm, edited 1 time in total.

Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Sat Dec 01, 2007 6:29 pm 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
Hi Marek,

I use the following code to generate the points on the sphere:

(I want to be able to control the increment for the angles so I specify phiInc and alphaInc as

phiInc = 360 / number_steps
alphaInc = 180 / number_steps

for(int phi = -180; phi <= 180; phi += phiInc)
{
phiRadian = (phi * PI) / 180.0;

for(int alpha = -90; alpha <= 90; alpha += alphaInc)
{
alphaRadian = (alpha * PI) / 180.0;

x = RADIUS*sin(phiRadian)*cos(alphaRadian);
y = RADIUS*sin(phiRadian)*sin(alphaRadian);
z = RADIUS*cos(phiRadian);

glBegin(GL_POINTS);
glVertex3f(x,y,z);
glEnd();
}
}

However, I seem to be getting double the number of points I want.

For instance, If I want sphere to be divided into steps of 5 points, I get 10 points.

Does my code look correct?

Thanks


Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Sat Dec 01, 2007 8:30 pm 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
Also the sphere seems a little "squashed".

Also I've changed the equations (swapped their places) so that

Y = RADIUS * cos (phiRadian)

and

Z = RADIUS * sin (phiRadians) * sin (alphaRadians)

I did this because the poles were not at the top and bottom but at the sides using the code in the previous post.


Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Sun Dec 02, 2007 7:12 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
I quickly sketched your setup and I think these are the coordinates that you should be using:

x = RADIUS * sin(alphaRadians) * cos (phiRadians);
y = RADIUS * cos(alphaRadians);
z = RADIUS * sin(alphaRadians) * sin (phiRadians);


Last edited by Marek on Mon Jan 07, 2008 9:05 pm, edited 1 time in total.

Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Sun Dec 02, 2007 8:45 am 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
Hi,

(Your equations work. But I want to understand why mine didn't)
I used the image at this link

http://upload.wikimedia.org/wikipedia/c ... re.svg.png

and books to come up with my equations.

However, they do not match exactly. To be able to go from -90 to 90 on Phi and -180 to 180 on alpha, I had to use these equations

for(int phi = -90; phi <= 90; phi += phiInc)
{
phiRadian = (phi * PI) / 180.0;
y = RADIUS*sin(phiRadian);

for(int alpha = -180; alpha <= 180; alpha += alphaInc)
{
alphaRadian = (alpha * PI) / 180.0;

x = RADIUS*cos(phiRadian)*sin(alphaRadian);
z = RADIUS*cos(phiRadian)*cos(alphaRadian);
}
}

Why couldn't I code it straight as it was in the image and math books?

Thanks.


Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Sun Dec 02, 2007 9:54 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
The difference between the equations that I gave you and the ones that you were using was the coordinate system.

Knowing that OpenGL has X to the right, Y up and Z in/out of the screen. You had said you want your poles to be at the top/bottom of the sphere.

Since alpha goes from -90 to +90 that tells me you want alpha to sweep from the south to the north pole.

Phi goes from -180 to +180 which says this is going to represent your latitude lines on the sphere.

I drew a sketch to show how everything is setup. http://www.marekknows.com/Misc/forum/Sphere.jpg

The dashed lines are the values you need to calculate for each point on the sphere.

The green dashed line on the XY plane is equal to RADIUS * sin(alphaRadians)

The blue and black ones arethe X and Z coordinates respectively. The red one is the Y coordinate


Last edited by Marek on Mon Jan 07, 2008 9:06 pm, edited 1 time in total.

Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Sun Dec 02, 2007 11:47 am 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
Thanks for the nice explanation.


Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Sun Dec 02, 2007 3:14 pm 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
I've attached the c code which uses glut and opengl.

I decided to use Marek's equations just to see what happens.

I can't seem to get any of the equations to give me the points I expect.

for example, I don't get 2 poles.

Using Marek's last set of equations I get a hemisphere.

If its not too much to ask, can you look at this and change it so I see what am doing wrong.

http://www.freewebs.com/fuzionstudio/sphere.cpp

Thanks


Offline
 Profile  
 
 Post subject: Re: Polygons for a sphere shape
PostPosted: Sun Dec 02, 2007 5:03 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
I took a look at your code and fixed it. Here is the updated version. http://www.marekknows.com/Misc/forum/sphere.cpp

When working with sin/cos in c++ you have to be careful with the values that you are going to get. To handle these special cases, notice that I added a few (?:) statements to make sure the sign of the result is correct. You also have to handle the special case where alpha=0 separately as shown.

You will notice that under certain cases when the number of sections is larger than 6, the lower and upper hemisphere do not line up. longitude lines don't meet at the equator. This is because the values returned from the cos/sin function are different for +ve and -ve numbers. I would say calculate all the points for the top hemisphere (excluding the equator) and then mirror these points for the bottom half just by swapping the y value to be -ve rather than calculating them. This way you will always have the same spacing for all the points in the top and bottom half.

Handle the equator separately.


Last edited by Marek on Mon Jan 07, 2008 9:07 pm, edited 1 time in total.

Offline
 Profile  
 
 Post subject:
PostPosted: Sat Dec 08, 2007 8:56 am 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
Just a little update,
Have gottent the sphere to render itself.
A video can be found at
http://www.youtube.com/watch?v=rbLT-xz8v-o

next thing am working on is lighting and normals, then textures.

Keep you posted.


Offline
 Profile  
 
 Post subject:
PostPosted: Sat Dec 08, 2007 9:42 pm 

Joined: Sat Jun 23, 2007 7:56 pm
Posts: 145
Hi people,
now have normals and shading on the sphere.

I used the the actual points on the sphere as the normals. (Tried many approaches before deciding to use the points generated along the sphere as the normals)
You can see the results below

http://www.youtube.com/watch?v=UJbmi-m_l4o

It is not 100% accurate, for example, if light strikes it from certain angles e.g straight from above, the shading is off.

Working on improving this. Also, I enabled GL_NORMALIZE to aid in keeping the normals accurate.

Marek, I tried generating normals using the plane equations with 3 points on a triangle, the results weren't great. Am I right to assume that the points along the sphere suffice as normals?


Offline
 Profile  
 
 Post subject:
PostPosted: Mon Jan 07, 2008 9:02 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
codeslasher wrote:
Marek, I tried generating normals using the plane equations with 3 points on a triangle, the results weren't great. Am I right to assume that the points along the sphere suffice as normals?


Nice video's on youTube! Great work.

I've decided to add a new geometry Sphere object to the physics engine so you'll be able to compare how I implement the same thing to your code. Check out VMK2 in the physics engine.

The normal at each vertex should point away from the sphere's surface. However if you extend the normal line inward, into the sphere, it should pass right through the centre of the sphere.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 13 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron