Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing the 3D coordinates on a unit sphere from a 2D point

I have a square bitmap of a circle and I want to compute the normals of all the pixels in that circle as if it were a sphere of radius 1:

enter image description here

The sphere/circle is centered in the bitmap.

What is the equation for this?

like image 880
Will Avatar asked Aug 28 '11 08:08

Will


People also ask

How do you find the coordinates of a point on a sphere?

Since r=ρsinϕ, these components can be rewritten as x=ρsinϕcosθ and y=ρsinϕsinθ. In summary, the formulas for Cartesian coordinates in terms of spherical coordinates are x=ρsinϕcosθy=ρsinϕsinθz=ρcosϕ.


1 Answers

Don't know much about how people program 3D stuff, so I'll just give the pure math and hope it's useful.

Sphere of radius 1, centered on origin, is the set of points satisfying:

x2 + y2 + z2 = 1

We want the 3D coordinates of a point on the sphere where x and y are known. So, just solve for z:

z = ±sqrt(1 - x2 - y2).

Now, let us consider a unit vector pointing outward from the sphere. It's a unit sphere, so we can just use the vector from the origin to (x, y, z), which is, of course, <x, y, z>.

Now we want the equation of a plane tangent to the sphere at (x, y, z), but this will be using its own x, y, and z variables, so instead I'll make it tangent to the sphere at (x0, y0, z0). This is simply:

x0x + y0y + z0z = 1

Hope this helps.


(OP):

you mean something like:

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

It does make a nice spherical shading-like shading if I treat the normals as colours and blit it; is it right?


(TZ)

Sorry, I'm not familiar with those aspects of C++. Haven't used the language very much, nor recently.

like image 158
Tom Zych Avatar answered Nov 15 '22 13:11

Tom Zych