Calculation of three-dimensional coordinates on a unit sphere from a two-dimensional point

I have a square of a bitmap image of a circle, and I want to calculate the normals of all the pixels in this circle, as if it were an area of ​​radius 1:

enter image description here

The sphere / circle is centered in the bitmap.

What is the equation for this?

+5
source share
4 answers

I don’t know much about how people program 3D material, so I’ll just give you clean math and hope it will be useful.

A sphere of radius 1 centered on origin is a set of points satisfying:

x 2 + y 2 + z 2 = 1

3D- , . , z:

z = ± sqrt (1 - x 2 - y 2).

, . , (x, y, z), , , < x, y, z > .

, , (x, y, z), x, y z, at (x 0, y 0, z 0). :

x 0 x + y 0 y + z 0 z = 1

, .


():

- :

const int R = 31, SZ = power_of_two(R*2);
std::vector<vec4_t> p;
for(int y=0; y<SZ; y++) {
    for(int x=0; x<SZ; x++) {
        const float rx = (float)(x-R)/R, ry = (float)(y-R)/R;
        if(rx*rx+ry*ry > 1) { // outside sphere
            p.push_back(vec4_t(0,0,0,0));
        } else {
            vec3_t normal(rx,sqrt(1.-rx*rx-ry*ry),ry);
            p.push_back(vec4_t(normal,1));
        }
    }
}

, , blit; ?


(TZ)

, ++. , .

+8

, , , - . , .

, , .

, , , .

( ). , , , . , , .

+1

, , : . , .

Ray-Sphere:

http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter1.htm

, U/V- ( , 2). .

, (point - origin, 1 ).

:

:

  • Ray: R (t) = R0 + t * Rd, t > 0 R0 = [X0, Y0, Z0] Rd = [Xd, Yd, Zd]
  • : S = [xs, ys, zs], (xs - xc) 2 + (ys - yc) 2 + (zs - zc) 2 = Sr2

, (x * /, y * /, z: 1), :

  • A = Xd ^ 2 + Yd ^ 2 + Zd ^ 2
  • B = 2 * (Xd * (X0 - Xc) + Yd * (Y0 - Yc) + Zd * (Z0 - Zc))
  • C = (X0 - Xc) ^ 2 + (Y0 - Yc) ^ 2 + (Z0 - Zc) ^ 2 - Sr ^ 2

:

  • t0, t1 = (- B + (B ^ 2 - 4 * C) ^ 1/2)/2

(B ^ 2 - 4 * C), , :

  • Ri = [xi, yi, zi] = [x0 + xd * ti, y0 + yd * ti, z0 + zd * ti]

:

  • SN = [(xi - xc)/Sr, (yi - yc)/Sr, (zi - zc)/Sr]

:

, , Z ( x y ), :

Ray:

  • X0 = 2 * pixelX/
    Y0 = 2 * /
    Z0 = 0

  • Xd = 0
    Yd = 0
    Zd = 1 a >

:

  • Xc = 1
    Yc = 1
    Zc = 1 a >

:

  • A = 1 ( )
  • B
    = 2 * (0 + 0 + (0 - 1))
    = -2 ( x/y-)
  • C
    = (X0 - 1) ^ 2 + (Y0 - 1) ^ 2 + (0 - 1) ^ 2 - 1
    = (X0 - 1) ^ 2 + (Y0 - 1) ^ 2


  • = (-2) ^ 2 - 4 * 1 * C
    = 4 - 4 * C

:

If discriminant < 0:
  Z = ?, Normal = ?
Else:
  t = (2 + (discriminant) ^ 1 / 2) / 2
If t < 0 (hopefully never or always the case)
  t = -t

:

  • Z: t
  • Nx: Xi - 1
    Ny: Yi - 1
    Nz: t - 1

:

Intuitively, it looks like C ( X^2 + Y^2), and the square root are the most prominent figures. If I had a better memory of my math (in particular, of transformations in terms of sums), I would argue that I could illustrate this with what Tom Zich gave you. Since I cannot, I will just leave it as above.

+1
source

This formula is often used for the fake envmapping effect.

double x = 2.0 * pixel_x / bitmap_size - 1.0;
double y = 2.0 * pixel_y / bitmap_size - 1.0;
double r2 = x*x + y*y;
if (r2 < 1)
{
    // Inside the circle
    double z = sqrt(1 - r2);
    .. here the normal is (x, y, z) ...
}
+1
source

All Articles