Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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 1220 times)
0 Members and 1 Guest are viewing this topic.
Offline StrideColossus
« 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

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« 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();
       }
   }
Online Roquen
« 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!
Legends of Yore - The Casual Retro Roguelike
Offline StrideColossus
« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (50 views)
2014-04-15 18:08:23

BurntPizza (46 views)
2014-04-15 03:46:01

UprightPath (62 views)
2014-04-14 17:39:50

UprightPath (44 views)
2014-04-14 17:35:47

Porlus (61 views)
2014-04-14 15:48:38

tom_mai78101 (85 views)
2014-04-10 04:04:31

BurntPizza (143 views)
2014-04-08 23:06:04

tom_mai78101 (242 views)
2014-04-05 13:34:39

trollwarrior1 (201 views)
2014-04-04 12:06:45

CJLetsGame (208 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!