Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Options for generating a sphere mesh  (Read 666 times)
0 Members and 1 Guest are viewing this topic.
Offline StrideColossus

Senior Member


Medals: 3
Projects: 1



« Posted 2012-12-06 11:31:43 »

I'm working on the generation of spherical meshes and am unsure what the best approach is for dealing with the 'poles' of the sphere - I was planning on using triangle-strips for the entire sphere (to leverage the various mesh building classes I've developed over time) but can't see how the poles can be defined using triangle-strips.

I've searched for online examples and tutorials but they always seem to fall down on one issue or another: either there are no texture coordinates (which I require), or the examples are using old-school OpenGL methods (I would prefer to stick with 4.x), or GLUT quadrics, etc.

Seems to me I have the following options:

1. Use triangle fans at the poles and strips for the 'body', i.e. the sphere is comprised of three meshes - apparently a common method, this is easy to visualise but is my least preferred solution since I would have to refactor the 'builder' code to generate multiple meshes with different drawing primitives (currently assumes one mesh with one primitive, perhaps this is a limitation, but it's the only time I've come across this scenario).

2. Use triangles rather than triangle strips - easiest to implement given the code I currently have, but less performant?  does it really make much/any difference whether I use triangles or strips?  (Probably not, I've implemented an MD5 mesh/animation loader which uses triangles throughout).  This approach (and 1 above) also allows the sphere builder to generate other 'conic' shapes such as discs, cones, hemispheres, cylinders, etc. which is a nice-to-have.

3. Create 'degenerate' slices at the poles - the sphere builder I have at the moment uses a unit-circle to generate 'slices' from pole to pole, I could just set the poles to be zero radius circles (i.e. degenerate triangles) and therefore still use the triangle-strip building code.  Good idea?  Sucks?  Easy to implement but wouldn't support generation of the other conic shapes.

4. Implement icosphere splitting, i.e. start with a icosahedron model and recursively split the triangles to achieve a sphere (without poles as such) - as far as I can tell this is the 'other' way to generate spheres, I was planning to implement this in the future but perhaps I should focus on this method instead?  Note that (at the moment anyway) I'm not too concerned about having the vertices 'squashed up' around the poles.

Does anyone have any opinions or experience of which of the above is the best approach (if indeed there is one)?  Or suggestions for other approaches I've not thought of?

Any thoughts or pointers to on-line threads, tutorials or books appreciated in advance?

- stride
Offline RobinB
« Reply #1 - Posted 2012-12-06 11:47:13 »

I used some fairly simple code for my planets.
Its intermediate mode code, but its peanuts to put in a vbo.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
    public static void drawSphere(float basex, float basey, float r, int lats, int longs) {
       int i, j;
       float lat0, lat1, z0, z1, zr0, zr1, lng, x, y, prevlat;
       float xc = 1f / lats;
       float yc = 1f / longs;
       
       for(i = 0; i <= lats; i++) {
           prevlat = i - 1;
           lat0 = (float)(pi * (-0.5 + prevlat / lats));
           z0  = (float)SimpleMath.sin(lat0);
           zr0 =  (float)SimpleMath.cos(lat0);
   
           lat1 = (float)(pi * (-0.5 + (float) i / lats));
           z1 = (float)SimpleMath.sin(lat1);
           zr1 = (float)SimpleMath.cos(lat1);
   
           GL11.glBegin(GL11.GL_QUAD_STRIP);
           for(j = 0; j <= longs; j++) {
               lng = (float)pi2 * (float) (j - 1) / longs;
               x = (float)SimpleMath.cos(lng)*r;
               y = (float)SimpleMath.sin(lng)*r;
 
               GL11.glTexCoord2f(xc*j, yc*prevlat);
               GL11.glNormal3f(basex + x * zr0, basey + y * zr0, z0*r);
               GL11.glVertex3f(basex + x * zr0, basey + y * zr0, z0*r);
               
               GL11.glTexCoord2f(xc*j, yc*i);
               GL11.glNormal3f(basex + x * zr1, basey + y * zr1, z1*r);
               GL11.glVertex3f(basex + x * zr1, basey + y * zr1, z1*r);
           }
           GL11.glEnd();
       }
   }
Offline Roquen

JGO Ninja


Medals: 66



« Reply #2 - Posted 2012-12-06 12:08:25 »

Search: sphere tessellation.  Didn't I just say this recently?  Smiley  Sphere are troublesome if you want uniform-ish fragment (texel/pixel) coverage...not an issue if you can have poles will little statically generated texture information or the textures are dynamically created.  So, like in most things, there is no one-size-fits-all solution.
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline StrideColossus

Senior Member


Medals: 3
Projects: 1



« Reply #3 - Posted 2012-12-06 13:30:04 »

Search: sphere tessellation.  Didn't I just say this recently?  Smiley  Sphere are troublesome if you want uniform-ish fragment (texel/pixel) coverage...not an issue if you can have poles will little statically generated texture information or the textures are dynamically created.  So, like in most things, there is no one-size-fits-all solution.

Sphere tesselation, never thought of that term, my google powers are weak - I'll try that as well.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (43 views)
2013-05-17 21:29:12

alaslipknot (52 views)
2013-05-16 21:24:48

gouessej (81 views)
2013-05-16 00:53:38

gouessej (80 views)
2013-05-16 00:17:58

theagentd (89 views)
2013-05-15 15:01:13

theagentd (82 views)
2013-05-15 15:00:54

StreetDoggy (126 views)
2013-05-14 15:56:26

kutucuk (148 views)
2013-05-12 17:10:36

kutucuk (148 views)
2013-05-12 15:36:09

UnluckyDevil (158 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.103 seconds with 20 queries.