Spheric Texturing

Sorry I crosspost, but I want this to work soon.
I use TexCoordGeneration(SPHERE_MAP),
but the texture is only on the side to the camera, and not around the whole sphere.

I have a planet and want the texture to be all around it.
How to do it?

Alonzo

Uh, I don’t know if this is what you need, but this makes a sphere with the proper texcoords:

public static Geometry getSphere(int divisions, int radius) {
            float   sign      = 1.0f;

            float[] texCoords = new float[(divisions + 1) * (2 * divisions + 1) * 2];
            float[] vertices  = new float[(divisions + 1) * (2 * divisions + 1) * 3];
            float[] normals   = new float[(divisions + 1) * (2 * divisions + 1) * 3];

            for (int i = 0;i <= divisions;i++) {
                  float rho = (float)((double)i * FastMath.PI / (double)divisions);

                  for (int j = 0;j <= 2 * divisions;j++) {
                        float theta = (float)((double)j * FastMath.PI / (double)divisions);
                        float vx = (float)(FastMath.cos(theta) * FastMath.sin(rho));
                        float vy = (float)(sign * FastMath.cos(rho));
                        float vz = (float)(FastMath.sin(theta) * FastMath.sin(rho));

                        texCoords[((i * (2 * divisions + 1)) + j) * 2 + 0] = 1f - (float)j / (2f * divisions);
                        texCoords[((i * (2 * divisions + 1)) + j) * 2 + 1] = (float)i / divisions;

                        vertices[((i * (2 * divisions + 1)) + j) * 3 + 0]  = vx * radius;
                        vertices[((i * (2 * divisions + 1)) + j) * 3 + 1]  = vy * radius;
                        vertices[((i * (2 * divisions + 1)) + j) * 3 + 2]  = vz * radius;

                        normals[((i * (2 * divisions + 1)) + j) * 3 + 0]   = vx * sign;
                        normals[((i * (2 * divisions + 1)) + j) * 3 + 1]   = vy * sign;
                        normals[((i * (2 * divisions + 1)) + j) * 3 + 2]   = vz * sign;
                  }
            }

            int[] vIdx = new int[divisions * 2 * divisions * 6];
            int   n = 0;

            for (int i = 0;i < divisions;i++) {
                  for (int j = 0;j < 2 * divisions;j++) {
                        int v00   = i * (2 * divisions + 1) + j;
                        int v01   = i * (2 * divisions + 1) + j + 1;
                        int v10   = (i + 1) * (2 * divisions + 1) + j;
                        int v11   = (i + 1) * (2 * divisions + 1) + j + 1;

                        vIdx[n++] = v11;
                        vIdx[n++] = v10;
                        vIdx[n++] = v00;
                        vIdx[n++] = v11;
                        vIdx[n++] = v00;
                        vIdx[n++] = v01;
                  }
            }

            IndexedTriangleArray shapeGeom = new IndexedTriangleArray(vertices.length / 3, 
                                                                                                  GeometryArray.TEXTURE_COORDINATE_2 | 
                                                                                                  TriangleArray.COORDINATES | 
                                                                                                  TriangleArray.NORMALS, vIdx.length);

            shapeGeom.setTextureCoordinates(0, 0, texCoords);
            shapeGeom.setValidVertexCount(vertices.length / 3);
            shapeGeom.setValidIndexCount(vIdx.length);
            shapeGeom.setCoordinates(0, vertices);
            shapeGeom.setNormals(0, normals);
            shapeGeom.setIndex(vIdx);

            return shapeGeom;
      }

Thanks itistoday, it’s working perfectly! :slight_smile:

Alonzo

BTW, you can get sphere’s and geospheres (less polys) with texcoords from the org.xith3d.geometry package (see a recent “Primitives Package” thread), this package is supported by the Xith3D dev team.

Will.