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.
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;
}