Hi, Im trying to do something very similar, which is simply map a texture to a sphere. I dont see a way to do this easily with any 'built in'/'free' spherical mapping solution, so I am trying to port this code ( http://www.codeguru.com/Cpp/G-M/opengl/texturemapping/article.php/c5589/#more ) to processing. I am able to draw my sphere just fine, but I am missing a few peices, namely the author of the original code calls :
Code:
NormalVector(u,v,w,n);
glNormal3dv(n);
which I am assuming makes a normal vector from the 3 coordinate vectors u,v,w, and then runs glNormal3d(n) on the vector, before drawing the textures.
What can I do in processing that mimics this?
here is my altered code, for what its worth. Any help is appreciated!
Code:
//
// code ported from http://www.codeguru.com/Cpp/G-M/opengl/texturemapping/article.php/c5589/
//
float PI = 3.14159265359;
float TWOPI = 6.28318530718;
float DE2RA = 0.01745329252;
float RA2DE = 57.2957795129;
float FLATTENING = 1.0/298.26;
float PIOVER2 = 1.570796326795;
void sphereWithTexture(PImage texture, int detail, float radius)
{
beginShape(TRIANGLES);
texture(texture);
fill(200,200,200,100);
int NumLatitudes = detail;
int NumLongitudes = detail;
float start_lat = -90;
float start_lon = 0.0;
float R = radius;
float lat_incr = 180.0 / NumLatitudes;
float lon_incr = 360.0 / NumLongitudes;
int row, col;
for (col = 0; col < NumLongitudes; col++)
{
float phi1 = (start_lon + col * lon_incr) * DE2RA;
float phi2 = (start_lon + (col + 1) * lon_incr) * DE2RA;
for (row = 0; row < NumLatitudes; row++)
{
float theta1 = (start_lat + row * lat_incr) * DE2RA;
float theta2 = (start_lat + (row + 1) * lat_incr) * DE2RA;
float[] u = new float[3];
u[0] = R * cos(phi1) * cos(theta1); //x
u[1] = R * sin(theta1); //y
u[2] = R * sin(phi1) * cos(theta1); //z
float[] v = new float[3];
v[0] = R * cos(phi1) * cos(theta2); //x
v[1] = R * sin(theta2); //y
v[2] = R * sin(phi1) * cos(theta2); //z
float[] w = new float[3];
w[0] = R * cos(phi2) * cos(theta2); //x
w[1] = R * sin(theta2); //y
w[2] = R * sin(phi2) * cos(theta2); //z
// normal(u,v,w); // yeah, that totally doesnt work...
vertex(u[0],u[1],u[2], (180.0 - phi1*RA2DE)/360.0, (theta1 + PIOVER2)*RA2DE/180.0);
vertex(v[0],v[1],v[2], (180.0 - phi1*RA2DE)/360.0, (theta2 + PIOVER2)*RA2DE/180.0);
vertex(w[0],w[1],w[2], (180.0 - phi2*RA2DE)/360.0, (theta2 + PIOVER2)*RA2DE/180.0);
v[0] = R * cos(phi2) * cos(theta1); //x
v[1] = R * sin(theta1); //y
v[2] = R * sin(phi2) * cos(theta1); //z
//normal(u,v,w);
vertex(u[0],u[1],u[2], (180.0 - phi1*RA2DE)/360.0, (theta1 + PIOVER2)*RA2DE/180.0);
vertex(v[0],v[1],v[2], (180.0 - phi1*RA2DE)/360.0, (theta2 + PIOVER2)*RA2DE/180.0);
vertex(w[0],w[1],w[2], (180.0 - phi2*RA2DE)/360.0, (theta1 + PIOVER2)*RA2DE/180.0);
} // end NumLatitudes for loop
} // end NumLongitudes for loop
endShape();
}