PDA

View Full Version : texturing a geodesic sphere

Snow Spider
12-28-2011, 05:33 PM
Hi,

I'm trying to render a geodesic sphere and make it look like the Earth by applying a single piece of texture on it.

I'm familiar with Java, C++, gmax and gimp. But I'm new to OpenGL. I went whrough www.videotutorialsrock.com (http://www.videotutorialsrock.com) so I can do some basic stuff. Also I have downloaded some codes from Bullet Physics to make my life easier and play with vectors.

Here's what I have accomplished so far:
1. Draw 20 triangles to form an icosahedron
2. Apply the texture on each triangle by assigning texture coordinates to each vertex. I simply used the top-middle, bottom-left and botton-right positions on the 256-by-256 bitmap file.
3. Subdivide each triangle into 4 little triangles and pull the new vertices out so they are separated from the center just as far as the old vertices. Draw triangles all over again, with texture.
4. Perform such subdivision k times until it reaches the desired shape, where k = 0, 1, 2, 3...
5. Make the "planet" rotate

Goal:
Instead of painting a picture on individual triangles, I want the picture to cover the entire planet. Also, it should work with any number of subdivisions.

A Wikipedia article on Dymaxion map provides some clue, but it doesn't explain the math behind it.

There are many ways to unfold the polygon for Dymaxion map, but I prefer the way discussed in
http://freeciv.wikia.com/wiki/Talk:Sphere
It will fit into a rectangular texture file. Also, the equator will look straight, which is a good thing.

I'll probably have to subdivide the picture and assign coorninates to each vertex. I'm unable to devise an algorithm that does that. The vertices in my sphere are not indexed in any way.

Any suggestion?

awhig
12-29-2011, 08:09 AM
Instead of painting a picture on individual triangles, I want the picture to cover the entire planet. Also, it should work with any number of subdivisions.

You can first do refinements on your polyhedron to a degree to make it appear like a sphere and then apply texture over it. The texture coordinates distribution should follow spherical parametric equations then.

OR

If you wish to apply texture along with every round of refinement (on each triangle) then you should keep track of mapping. i.e texel 0 maps to vertex 0 of triangle 0 , and texel 1.0 to vertex n of your last triangle.

With every subdivision, (k = 0,1,2...N), the first vertex in your list would always map to texel 0.0 and last vertex to 1.0. In between, you can interpolate texel value using barycentric coordinate system or some kind of interpolation (linear etc. ) but I warn you that the finish won't be smooth till you subdivide your polyhedron to a level where C2 continuity is achieved.

I prefer the first method as applying texture to already refined polyhedron would be fast and inexpensive as compared to second approach.

Hope this helps you.

Snow Spider
12-29-2011, 08:24 PM
I have found a solution that looks a lot easier:

glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, _textureId);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glutSolidSphere(1,50,50);
glDisable(GL_TEXTURE_2D);

By adding these lines in the middle of my code, I can paint the planet with the actual map of the world. This will hopefully allow me to have any number of subdivision without ruining the texture.

Still, everything is skewed about 60 degrees to the left. I think it has to do with the fact that my sphere is made out of triangles.
Secondly, the texture only stretches to half the sphere, and then projects its mirror image on the other side.

awhig
12-30-2011, 12:03 PM
glutSolidSphere() may not be generating texture coordinates accurately. Rely on the model you create and generate texels by yourself or use cubemapping.

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);

glTexGenf instead of glTexGeni to generate texels in floats. (Suggestion) and try GL_SPHERE_MAP for GL_S.

Snow Spider
01-11-2012, 03:01 PM
Thank you all. The solutions provided in the library didn't work out for me, so I ended up using longitude & latitude for tex coord at every vertex, which caused zig-zag seam on the longitude zero. I have fixed that, but I'm now struggling with the two poles.

As you can see in the picture uploaded, the texture swirls at the north pole as well as at the south pole because the particular vertex is linked to the tex coord (0,1) for all 6 triangles around it.

If I can squeeze the line from (0,1) to (1,1) into a point as a tex coord, this will a cakewalk. In fact, that's how Mercator maps work. I just don't know what OpenGL needs to be told to do it. I'm pretty sure this is an elementary issue, but I find no alternative to asking.

ZbuffeR
01-12-2012, 04:29 AM
Simple solution : keep a pole vertex for each triangle around it, each with a different texcoord.

Snow Spider
01-15-2012, 10:05 PM
Well... in fact I've tried that before posting the last question. It is better than the "fail" case above, but it still cuts 1/2 of the pole areas off the texture in triangular sections.

ZbuffeR
01-16-2012, 03:51 AM
Indeed. The method discussed in the freeciv article you linked *have to* be applied with a specially crafted texture like this one :
http://freeciv.wikia.com/index.php?title=Talk:Sphere&amp;image=Bucky-earth-20-gif
Note how the triangles leading to the poles are disjoint.