Sphere mapping problem

Started by
1 comment, last by _WeirdCat_ 8 years, 9 months ago

hey guys

i have a problem to calculate the right u v coordinates.

im using opengl es 2.0 and written is the code in java.

Screenshot_2015_06_28_22_57_34.png


 private List<Float>calcUVCords(List<Float> sphereCords)
    {
        List<Float> uvCords=new ArrayList<Float>();
        Float x,y,z, nx,ny,nz;
        Double lenght;
        Double u1,v1,u2,v2,u3,v3;

        for(int vertex=0;vertex<sphereCords.size();vertex+=3)
        {
            x=sphereCords.get(vertex);
            y=sphereCords.get(vertex+1);
            z=sphereCords.get(vertex+2);

            lenght=Math.sqrt((x*x)+(y*y)+(z*z));

            nx=x/lenght.floatValue();
            ny=y/lenght.floatValue();
            nz=z/lenght.floatValue();

            u1=(Math.atan2(nx,nz)/ (2.0 * PI)) + 0.5;
            v1=0.5+(Math.asin(ny) / PI);

            uvCords.add(u1.floatValue());
            uvCords.add(v1.floatValue());
        }
   return uvCords;
}

I m using subdivion a octahedron to calculate my Sphere.

here is the code


private List<Float> calcSiteCords(int destLevel, List<Float> cords) {
        List<Float> cordsBuffer = new ArrayList<Float>();

        for (int currentLevel = 0; currentLevel < destLevel; currentLevel++) {
            cordsBuffer.clear();
            for (int i = 0; i <= cords.size() - 9; i += 9) {
                cordsBuffer.addAll(subdivide(cords.get(i), cords.get(i + 1), cords.get(i + 2),
                        cords.get(i + 3), cords.get(i + 4), cords.get(i + 5),
                        cords.get(i + 6), cords.get(i + 7), cords.get(i + 8)));

            }
            cords.clear();
            cords.addAll(cordsBuffer);
        }

        return cords;
    }

private List<Float> subdivide(float v1x, float v1y, float v1z,
                                  float v2x, float v2y, float v2z,
                                  float v3x, float v3y, float v3z) {

        List<Float> sortedCords=new ArrayList<Float>();

        Float v12x = new Float(0);
        Float v12y = new Float(0);
        Float v12z = new Float(0);
        Float v13x = new Float(0);
        Float v13y = new Float(0);
        Float v13z = new Float(0);
        Float v23x = new Float(0);
        Float v23y = new Float(0);
        Float v23z = new Float(0);

        Double s;


        // Calculate middle of first edge...
        v12x = 0.5f * (v1x + v2x);
        v12y = 0.5f * (v1y + v2y);
        v12z = 0.5f * (v1z + v2z);
        // ... and renormalize it to get a point on the sphere.

        s = (1 / (Math.sqrt(v12x.doubleValue() * v12x.doubleValue() + v12y.doubleValue()
                * v12y.doubleValue() + v12z.doubleValue() * v12z.doubleValue())));
        v12x *= s.floatValue();
        v12y *= s.floatValue();
        v12z *= s.floatValue();


        // Same thing for the middle of the other two edges.
        v13x = 0.5f * (v1x + v3x);
        v13y = 0.5f * (v1y + v3y);
        v13z = 0.5f * (v1z + v3z);
        s = (1 / (Math.sqrt(v13x.doubleValue() * v13x.doubleValue() + v13y.doubleValue()
                * v13y.doubleValue() + v13z.doubleValue() * v13z.doubleValue())));
        v13x *= s.floatValue();
        v13y *= s.floatValue();
        v13z *= s.floatValue();


        v23x = 0.5f * (v2x + v3x);
        v23y = 0.5f * (v2y + v3y);
        v23z = 0.5f * (v2z + v3z);
        s = (1 / (Math.sqrt(v23x.doubleValue() * v23x.doubleValue() + v23y.doubleValue()
                * v23y.doubleValue() + v23z.doubleValue() * v23z.doubleValue())));
        v23x *= s.floatValue();
        v23y *= s.floatValue();
        v23z *= s.floatValue();

        sortedCords.add(v3x);sortedCords.add(v3y);sortedCords.add(v3z);
        sortedCords.add(v13x);sortedCords.add(v13y);sortedCords.add(v13z);
        sortedCords.add(v23x);sortedCords.add(v23y);sortedCords.add(v23z);

        sortedCords.add(v13x);sortedCords.add(v13y);sortedCords.add(v13z);
        sortedCords.add(v12x);sortedCords.add(v12y);sortedCords.add(v12z);
        sortedCords.add(v23x);sortedCords.add(v23y);sortedCords.add(v23z);

        sortedCords.add(v13x);sortedCords.add(v13y);sortedCords.add(v13z);
        sortedCords.add(v1x);sortedCords.add(v1y);sortedCords.add(v1z);
        sortedCords.add(v12x);sortedCords.add(v12y);sortedCords.add(v12z);

        sortedCords.add(v23x);sortedCords.add(v23y);sortedCords.add(v23z);
        sortedCords.add(v12x);sortedCords.add(v12y);sortedCords.add(v12z);
        sortedCords.add(v2x);sortedCords.add(v2y);sortedCords.add(v2z);

        return sortedCords;
    }
Advertisement

Why don't you just load an .OBJ or something like that. Why bother?

the texture coordinate for a sphere is like that

x - angle around a center / 360.0 gives you U coordinate

y is a little bit more tricky because you need angles between -90..90

there are 1000 ways of doing it i could post my code but yesterday i reviewed it and didint seem to output proper values (anyway it works for me that wont work for you so i post this working code)


procedure CreateSphere(CX, CY, CZ, Radius : glFloat; N : Integer);  // N = precision
var I, J : Integer;
    theta1,theta2,theta3 : glFloat;
    X, Y, Z, px, py, pz : glFloat;
begin

    if Radius < 0 then Radius :=-Radius;
    if n < 0 then n := -n;
    if (n < 4) OR (Radius <= 0) then
    begin
      glBegin(GL_POINTS);
        glVertex3f(CX, CY, CZ);
      glEnd();
      exit;
    end;

    for J :=0 to N DIV 2 -1 do
    begin
      theta1 := J*2*PI/N - PI/2;
      theta2 := (J+1)*2*PI/n - PI/2;
      glBegin(GL_QUAD_STRIP);
        For I :=0 to N do
        begin
          theta3 := i*2*PI/N;
          x := cos(theta2) * cos(theta3);
          y := sin(theta2);
          z := cos(theta2) * sin(theta3);
          px := CX + Radius*x;
          py := CY + Radius*y;
          pz := CZ + Radius*z;

         glNormal3f(-X, -Y, -Z);
          glTexCoord2f(1*(1-I/n), 1*(1*(J+1)/n));

          glVertex3f(px,py,pz);

          X := cos(theta1) * cos(theta3);
          Y := sin(theta1);
          Z := cos(theta1) * sin(theta3);
          px := CX + Radius*X;
          py := CY + Radius*Y;
          pz := CZ + Radius*Z;

          glNormal3f(-X, -Y, -Z);
          glTexCoord2f(1*(1-i/n), 1*(1*j/n));
          glVertex3f(px,py,pz);
        end;
      glEnd();
    end;
end;

i dont know... either you get angles and compute texcoord or you change the sphere creation algo.

be aware

X := cos(tilt) * cos(heading);
Y := sin(tilt);
Z := cos(tilt) * sin(heading);

This topic is closed to new replies.

Advertisement