PDA

View Full Version : Parametric model of a sphere



Lee_Jennifer_82
01-15-2016, 01:41 AM
A sphere centered at origin can be defined parametrically defined as follows:

x = r cos(theta) cos(phi)
y = r cos(theta) sin(phi)
z = r sin(theta)


Can the corresponding normal for a vertex be defined like this : cos(theta) cos(phi), cos(theta) sin(phi), sin(theta)?

What about the corresponding texture coordinates: in 2D and 3D?

Any clarification will be highly appreciated.
Thank you.

__bob__
01-15-2016, 06:30 AM
Why dont you made it easier?

normal = normalize( vec3(x,y,c)-SphereCenter)
with a sphere centered on the origin :
normal =normalize(vec3(x,y,z))

GClements
01-15-2016, 11:01 AM
The normal is the same as the vertex position, but without the scale factor of r.

Texture coordinates can be defined however you like. It's not uncommon to just use phi and theta (each mapped to the 0..1 range) as texture coordinates (that results in an equidistant cylindrical aka Plate Carée projection). The main problem is that any such mapping won't be affine, meaning that you get some amount of distortion when approximating a sphere by a triangle mesh unless you use shaders to create a non-linear mapping.

Lee_Jennifer_82
01-15-2016, 10:36 PM
Thank you again! For 2D texture, I think I need to do as follows:



const float da = PI/steps;
const float db = PI/steps;
for(b = -PI/2; b <= PI/2; b+=db)
for(a = 0; a <= 2*PI; a+=da){
// now tex coordinates are: (a*da , b*db)
}


Am I doing right? Can anyone also give me explanation about generating 3D texture coordinates for a sphere as well.

Thanks again advance.

GClements
01-15-2016, 11:32 PM
For 2D texture, I think I need to do as follows:


const float da = PI/steps;
const float db = PI/steps;
for(b = -PI/2; b <= PI/2; b+=db)
for(a = 0; a <= 2*PI; a+=da){
// now tex coordinates are: (a*da , b*db)
}

Am I doing right?

No.



for(int b = 0; b <= lat_steps; b++)
for(int a = 0; a <= lon_steps; a++) {
// texture coordinates
float tex_u = 1.0 * a / lon_steps;
float tex_v = 1.0 * b / lat_steps;
// lat/lon coordinates
float theta = (tex_v-0.5)*PI;
float phi = tex_u*2*PI;
}





Can anyone also give me explanation about generating 3D texture coordinates for a sphere as well.

3D texture coordinates? For a cube map, they would be the same as the vertex position or the normal (the magnitude doesn't matter for a cube map).

Lee_Jennifer_82
01-16-2016, 01:17 PM
Thank you so much again. But I still have question. For 2D texture, coordinate will range from (0,0) to (1,1), Why is it not the same for 3D texture, i.e. (0,0,0) to (1,1,1).

GClements
01-16-2016, 07:30 PM
Thank you so much again. But I still have question. For 2D texture, coordinate will range from (0,0) to (1,1), Why is it not the same for 3D texture, i.e. (0,0,0) to (1,1,1).
2D texture maps have the origin (0,0) at one corner of the image. Cube maps have the origin at the centre of the cube (the calculations are simpler that way).

Given (x,y,z) texture coordinates, a cube map lookup first determines which of the three components (x, y or z) has the largest absolute value, and its sign (positive or negative). The results determine which of the six faces are used.

Each of the two smaller components are divided by the absolute value of the larger component to generate s and t coordinates. Because the numerators are no larger than the denominator, the resulting s and t coordinates will always be between -1 and +1. These are then offset and scaled (s'=(s+1)/2, t'=(t+1)/2) to map them from -1..+1 to 0..1, producing 2D texture coordinates within the selected face.

E.g. if the z component is the largest, and is positive, then the +Z face would be selected and the texture coordinates are s'=(x/|z|+1)/2 and t'=(-y/|z|+1)/2.

(The choice of which component is used for s and which is used for t is somewhat arbitrary, as are their signs. For the +Z face, s=+x, t=-y. Table 8.19 in the 4.5 specification has the details).