# OpenGL vector rotation matrix to a given point

I have a sphere with a radius of .5

The sphere is centred at 0,0,0

I am now trying to place items around my sphere based on a given x,y,z on the surface of the sphere.

My items have been draw/created in such a way that they are positioned on the surface when translated to 0,0,0 because their centre offset has been positioned at the centre of the sphere.

What I need is some help with the calculation needed to get the correct glRotate values so that my icons will show at the correct x,y,z on the surface.

To help explain what I mean, here is a picture:

So you can see 0,0,0 at the centre, and also rotating around the same point is the blue line, This represents the rotation of my object. The green part is the visible part of my item, and so will draw on the surface of the sphere.

I have everything ready to go for this, in that I can already draw my items in this manner, however I am not able to correctly calculate the rotation.

With the default rotation of Identity my items will draw on the bottom of the sphere.

In summary I am looking for something like this:

glRotate = GetRotationForSurface(X,Y,Z);

I hope that's clear, this is a bit tricky to explain.

Thanks,

• If you use no rotaton matrix, what 'default' x,y,z coordinates your objects have? Aug 5, 2018 at 11:19
• If you're talking about 3D, you'll need more than 1 value to represent the rotation. You'll either need spherical coordinates, or an axis of rotation and an angle around that axis, or something like that. Aug 5, 2018 at 16:02
• @HolyBlackCat 0,0,0 The object has an offset position so that it can rotate around the centre of the sphere while rendering on the surface. No translation needed. Aug 6, 2018 at 0:06
• What they meant was what's the 3D position of the point on the surface that the object's visual occupies before it's been rotated? To know how much rotation to apply to reach a particular destination orientation, we need to know what direction we're starting from. Judging by your answer below, it looks like the answer to HolyBlackCat's question is (0, -1, 0) Aug 6, 2018 at 4:07
• @DMGregory correct 0,-1,0 With no rotation the objects draws at the bottom of the sphere. Note that is a normalised vector. Its real position would have been a centre at 0,0,0 and a visual position of 0,-.5,0 Aug 6, 2018 at 4:29

Got it, thanks to euclideanspace

This is how its done:

//Representing the centre vector of my sphere
V1.X = 0;
V1.Y = -1;
V1.Z = 0;

//Representing where we want to rotate to
V2.X = SurfaceTargetX
V2.Y = SurfaceTargetY
V2.Z = SurfaceTargetZ

//Normalise both