Camera movement around a sphere - c++

I am trying to convert moving around the xz plane to movement around a sphere. Originally, I had my camera moving forward at a constant speed at a constant height on the y axis. The user can move left/right by adjusting the yaw, however they can not move up/down or roll. Here is how I accomplished that:
float turnRate = 1.0;
float stepDist = 0.1;
if (keys['d']) {
yaw += M_PI * (turnRate / 180.0);
}
if (keys['a']) {
yaw += M_PI * (-turnRate / 180.0);
}
position.x += stepDist * sinf(yaw);
position.z += stepDist * cosf(yaw + M_PI);
...
glm::mat4 P = glm::rotate(pitch, glm::vec3(-1.0, 0.0, 0.0));
glm::mat4 Y = glm::rotate(yaw, glm::vec3(0.0, -1.0, 0.0));
glm::mat4 T = glm::translate(position);
glm::mat4 C = T * Y * P;
This worked well for me. Now what I am trying to achieve is instead of moving across the xz plane, I would like to move across the surface of a sphere. I still want to be moving forward at a constant speed at a constant radius from the center of the sphere with only allowing adjustment of the yaw. This is what I have currently:
float turnRate = 1.0;
float stepDist = 0.01;
step += stepDist;
if (keys['d']) {
yaw += M_PI * (turnRate / 180.0);
}
if (keys['a']) {
yaw += M_PI * (-turnRate / 180.0);
}
pitch = M_PI / 8.0; // angle the camera down towards the sphere a bit
glm::vec3 f = glm::vec3(sinf(yaw), 0.0, cosf(yaw + M_PI));
glm::vec3 up = glm::vec3(0.0, 1.0, 0.0);
glm::vec3 forward = glm::normalize(f);
right = glm::cross(up, forward);
float sphereRadius = 50.0;
float camRadius = sphereRadius + 8.0;
radius = glm::vec3(0.0, camRadius, 0.0);
...
glm::mat4 P = glm::rotate(pitch, glm::vec3(-1.0, 0.0, 0.0));
glm::mat4 Y = glm::rotate(yaw, glm::vec3(0.0, -1.0, 0.0));
glm::mat4 T = glm::translate(radius);
glm::mat4 S = glm::rotate(step, right);
glm::mat4 C = S * T * Y * P;
This works when the camera is simply moving forward. However, the problem is that when the yaw is changing (the user is turning right/left) and the step gets to certain values the sphere will start rotating in the wrong direction underneath the camera. It is not an instant switch. It seems to be reversed when step is between increments of PI to 2PI (i.e. it is reversed between pi and 2pi, 3pi and 4pi, etc..).
Any ideas on how I might fix this or what I am doing wrong?
UPDATE:
Here is a similar example of what I am trying to achieve:
http://www.youtube.com/watch?v=izVtTcq_his&t=0m23s
Except I want to be able to rotate the camera at smaller intervals than 90 degrees. If anyone has any links to example code/tutorials of how to achieve this I would really appreciate it.

Related

Passing a vec3 to glm::lookAt appears to modify it

I have encountered a situation where passing a glm::vec3 to the glm::lookAt function appears to modify it.
The following code is about shadow frustum calculation in a C++ / OpenGL game engine. The problem arises in the glm::lookAt function, at the end.
void Shadows::updateFrustumBoundingBox()
{
// Here we convert main camera frustum coordinates in light view space
std::array<glm::vec3,8> points = {
// Near plane points
lightView * glm::vec4(cameraPtr->ntl, 1.0),
lightView * glm::vec4(cameraPtr->ntr, 1.0),
lightView * glm::vec4(cameraPtr->nbl, 1.0),
lightView * glm::vec4(cameraPtr->nbr, 1.0),
// Far plane points
lightView * glm::vec4(cameraPtr->ftl, 1.0),
lightView * glm::vec4(cameraPtr->ftr, 1.0),
lightView * glm::vec4(cameraPtr->fbl, 1.0),
lightView * glm::vec4(cameraPtr->fbr, 1.0)};
// Here we find the shadow bounding box dimensions
bool first = true;
for (int i=0; i<7; ++i)
{
glm::vec3* point = &points[i];
if (first)
{
minX = point->x;
maxX = point->x;
minY = point->y;
maxY = point->y;
minZ = point->z;
maxZ = point->z;
first = false;
}
if (point->x > maxX)
maxX = point->x;
else if (point->x < minX)
minX = point->x;
if (point->y > maxY)
maxY = point->y;
else if (point->y < minY)
minY = point->y;
if (point->z > maxZ)
maxZ = point->z;
else if (point->z < minZ)
minZ = point->z;
}
frustumWidth = maxX - minX;
frustumHeight = maxY - minY;
frustumLength = maxZ - minZ;
// Here we find the bounding box center, in light view space
float x = (minX + maxX) / 2.0f;
float y = (minY + maxY) / 2.0f;
float z = (minZ + maxZ) / 2.0f;
glm::vec4 frustumCenter = glm::vec4(x, y, z, 1.0f);
// Here we convert the bounding box center in world space
glm::mat4 invertedLight = glm::mat4(1.0f);
invertedLight = glm::inverse(lightView);
frustumCenter = invertedLight * frustumCenter;
// Here we define the light projection matrix (shadow frustum dimensions)
lightProjection = glm::ortho(
-frustumWidth/2.0f, // left
frustumWidth/2.0f, // right
-frustumHeight/2.0f, // down
frustumHeight/2.0f, // top
0.01f, // near
SHADOW_DISTANCE); // far
// Here we define the light view matrix (shadow frustum position and orientation)
lightDirection = glm::normalize(lightDirection);
target = glm::vec3(0.0f, 100.0f, 200.0f) + lightDirection;
lightView = glm::lookAt(
// Shadow box center
glm::vec3(0.0f, 100.0f, 200.0f), // THIS LINE
// glm::vec3(frustumCenter), // ALTERNATIVELY, THIS LINE. Here I convert it as a vec3 because it is a vec4
// Light orientation
target,
// Up vector
glm::vec3( 0.0f, 1.0f, 0.0f));
cout << "frustumCenter: " << frustumCenter.x << " " << frustumCenter.y << " " << frustumCenter.z << endl;
// Final matrix calculation
lightSpaceMatrix = lightProjection * lightView;
}
As is, the first glm::lookAt parameter is glm::vec3(0.0f, 100.0f, 200.0f), and it works correctly. The glm::vec4 frustumCenter variable isn't used by glm::lookAt, and outputs correct values each frame.
frustumCenter: 573.41 -93.2823 -133.848 1
But if I change the first glm::lookAt parameter to "glm::vec3(frustumCenter)":
frustumCenter: nan nan nan nan
How can it be?
I have encountered a situation where passing a glm::vec3 to the glm::lookAt function appears to modify it."
I don't think so. You use frustumCenter to caclucalte lightView, but before you do that, you use lightView to calculate frustumCenter: frustumCenter = invertedLight * frustumCenter;
So my educated guess on what happens here is:
The lightView matrix is not properly initialized / initialized to a singular matrix (like all zeros). As such, the inverse will be not defined, resulting in frustumCenter becoming all NaN, which in turn results in lightView becoming all NaN.
But if you not use frustumCenter in the first iteration, lightView will be properly initialized, and frustumCenter will be calculated to a sane value in the next iteration.

How to change quaternion from mouse input?

I would like to make a 3D viewer which is controllable from mouse input with OpenGL.
The camera moves around the object using a polar coordinate system. I use Euler angle and glm::lookat, this system works with some limitation. At this case, pitch and yaw should be changed from mouse inputs, however, we have to care about gimbal lock. So I make a limitation to the range of pitch.
This is a part of code
auto V = camera.getViewMatrix();
float xOffset = x - lastX_pos;
float yOffset = lastY_pos - y;
lastX_pos = x;
lastY_pos = y;
float sensitivity = 0.05;
xOffset *= sensitivity;
yOffset *= sensitivity;
yaw += xOffset;
pitch += yOffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 cameraPos;
cameraPos.x = distance * cos(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraPos.y = distance * sin(glm::radians(pitch));
cameraPos.z = distance * sin(glm::radians(yaw)) * cos(glm::radians(pitch));
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(cameraPos);
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));
glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);
V = glm::lookAt(cameraPos + sphereCenter, sphereCenter, cameraUp);
camera.setViewMatrix(V);
I would like to move the camera without any limitations like Meshlab viewer. So I think I have to use not Euler angle but quaternion and calculate view matrix without glm::lookat.
If I did so, how I would correctly deal with mouse input and calculate camera position and direction from quaternion?

scaling is moving the object

Video Link for the issue.
https://www.youtube.com/watch?v=iqX1RPo4NDE&feature=youtu.be
This is what i want to attain.
https://www.youtube.com/watch?v=bWwYV0VhXqs
Here after scaling the object i can move pivot independently , position of the pivot does not affect the position of the object.
These are my matrices.
when i move the pivot point to one unit in x and if the scaling is set to 1 than everthing works fine.
the pivot point has moved to one unit and the cube has stayed in its position.
But when i first scale the object to 0.5 and than move the pivot point than the cube follows the pivot point , which should not be he case as i am only moving pivot point.
Please help me with this , how can i keep the cube in position.
Though i am moving the axis not the cube so the cube should stay in original position.
glm::mat4x4 Container::GetPositionMatrix()
{
// posx is the x position of the object.
// posy is the y position of the object.
// posz is the y position of the object.
glm::mat4 TransformationPosition = glm::translate(glm::mat4x4(1.0),
glm::vec3(posx, posy, posz ));
return TransformationPosition;
}
glm::mat4x4 Container::GetRotationMatrix()
{
// posx is the x positon of the object
// pivotx is the x position on the pivot point
// rotx is the x rotation of the object
glm::vec3 pivotVector(posx - pivotx, posy - pivoty, posz - pivotz);
glm::mat4 TransPivot = glm::translate(glm::mat4x4(1.0f), pivotVector);
glm::mat4 TransPivotInverse = glm::translate(glm::mat4x4(1.0f),
glm::vec3( -pivotVector.x , -pivotVector.y , -pivotVector.z));
glm::mat4 TransformationRotation(1.0);
TransformationRotation = glm::rotate(TransformationRotation,
glm::radians(rotx), glm::vec3(1.0, 0.0, 0.0));
TransformationRotation = glm::rotate(TransformationRotation,
glm::radians(roty), glm::vec3(0.0, 1.0, 0.0));
TransformationRotation = glm::rotate(TransformationRotation,
glm::radians(rotz ), glm::vec3(0.0, 0.0, 1.0));
return TransPivotInverse * TransformationRotation * TransPivot;
}
glm::mat4x4 Container::GetScalingMatrix()
{
// posx is the x positon of the object
// pivotx is the x position on the pivot point
// scax is the x scaling of the object
glm::vec3 pivotVector(posx - pivotx, posy - pivoty, posz - pivotz);
glm::mat4 TransPivot = glm::translate(glm::mat4x4(1.0f), pivotVector);
glm::mat4 TransPivotInverse = glm::translate(glm::mat4x4(1.0f),
glm::vec3(-pivotVector.x, -pivotVector.y, -pivotVector.z));
glm::mat4 TransformationScale = glm::scale(glm::mat4x4(1.0 ),
glm::vec3(scax, scay, scaz));
return TransPivotInverse * TransformationScale * TransPivot;
}
final matrix for the Object position.
TransformationPosition * TransformationRotation * TransformationScaling
This is my final matrix for the pivot point
PivotPointPosition = MatrixContainerPosition * MatrixPivotPointPosition *
MatrixRotationContainer * MatrixScaleContainer
The object should be orientated and located as follows:
The reference point (pivotx, pivoty, pivotz) is a point in object space.
The object has to be scaled (scax, scay, scaz) and rotated (rotx, roty, rotz) relative to the reference point (pivotx, pivoty, pivotz).
The point (posx, posy, posz) defines the position of the object in the scene, where the reference point has finally to be placed.
Do the following steps:
Scale the object to the desired size, with respect to the reference point:
glm::mat4 GetScalingMatrix()
{
glm::vec3 refVector(pivotx, pivoty, pivotz);
glm::mat4 TransRefToOrigin = glm::translate(glm::mat4(1.0f), -refVector);
glm::mat4 TransRefFromOrigin = glm::translate(glm::mat4(1.0f), refVector);
glm::vec3 scale = glm::vec3(scax, scay, scaz);
glm::mat4 TransformationScale = glm::scale(glm::mat4(1.0), scale);
return TransRefFromOrigin * TransformationScale * TransRefToOrigin;
}
Rotate the scaled object around the pivot, like in the answer to one of your previous questions (How to use Pivot Point in Transformations):
glm::mat4 GetRotationMatrix()
{
glm::vec3 pivotVector(pivotx, pivoty, pivotz);
glm::mat4 TransPivotToOrigin = glm::translate(glm::mat4(1.0f), -pivotVector);
glm::mat4 TransPivotFromOrigin = glm::translate(glm::mat4(1.0f), pivotVector);
glm::mat4 TransformationRotation(1.0);
TransformationRotation = glm::rotate(TransformationRotation,
glm::radians(rotx), glm::vec3(1.0, 0.0, 0.0));
TransformationRotation = glm::rotate(TransformationRotation,
glm::radians(roty), glm::vec3(0.0, 1.0, 0.0));
TransformationRotation = glm::rotate(TransformationRotation,
glm::radians(rotz), glm::vec3(0.0, 0.0, 1.0));
return TransPivotFromOrigin * TransformationRotation * TransPivotToOrigin;
}
Move the scaled and rotated object to its final position (posx, posy, posz).
glm::mat4 GetPositionMatrix()
{
glm::vec3 trans = glm::vec3(posx-pivotx, posy-pivoty, posz-pivotz);
glm::mat4 TransformationPosition = glm::translate(glm::mat4(1.0), trans);
return TransformationPosition;
}
The order matters:
glm::mat4 model = GetPositionMatrix() * GetRotationMatrix() * GetScalingMatrix();
All this can be simplified:
// translate "pivot" to origin
glm::mat4 ref2originM = glm::translate(glm::mat4(1.0f), -glm::vec3(pivotx, pivoty, pivotz));
// scale
glm::mat4 scaleM = glm::scale(glm::mat4(1.0), glm::vec3(scax, scay, scaz));
// rotate
glm::mat4 rotationM(1.0);
rotationM = glm::rotate(rotationM, glm::radians(rotx), glm::vec3(1.0, 0.0, 0.0));
rotationM = glm::rotate(rotationM, glm::radians(roty), glm::vec3(0.0, 1.0, 0.0));
rotationM = glm::rotate(rotationM, glm::radians(rotz), glm::vec3(0.0, 0.0, 1.0));
// translate to "pos"
glm::mat4 origin2posM = glm::translate(glm::mat4(1.0), glm::vec3(posx, posy, posz));
// concatenate matrices
glm::mat4 model = origin2posM * rotationM * scaleM * ref2originM;

Rotating an object with quaternion

I have a question in regards to using quaternions for the rotation of my graphics object.
I have a Transform class which has the following constructor with default parameters:
Transform(const glm::vec3& pos = glm::vec3(0.0), const glm::quat& rot = glm::quat(1.0, 0.0, 0.0, 0.0),
const glm::vec3& scale = glm::vec3(1.0))
{
m_pos = pos;
m_rot = rot;
m_scale = scale;
}
In my Transform class calculate the MVP as follows:
glm::mat4 Transform::GetModelMatrix() const
{
glm::mat4 translate = glm::translate(glm::mat4(1.0), m_pos);
glm::mat4 rotate = glm::mat4_cast(m_rot);
glm::mat4 scale = glm::scale(glm::mat4(1.0), m_scale);
return translate * rotate * scale;
}
The issue I'm facing is that when I use const glm::quat& rot = glm::quat(1.0, 0.0, 0.0, 0.0) my object appears normal on screen. The following image shows it:
However if I try to use for example const glm::quat& rot = glm::quat(glm::radians(90.0f), 0.0, 1.0, 0.0) (rotating on y axis by 90 degrees) my object appears as if it has been scaled. The following image shows it:
I can't figure out what is causing it to become like this when I try to rotate it. Am I missing something important?
If it's of any relevance, the following is how I calculate my view matrix:
glm::mat4 Camera::GetView() const
{
glm::mat4 view = glm::lookAt(m_pos, m_pos + m_forward, m_up);
return view;
}
AFAIK you can init a glm::quat using:
glm::vec3 angles(degToRad(rotx), degToRad(roty), degToRad(rotz));
glm::quat rotation(angles);
Where rotx, roty, rotz are the rotation angles around x, y and z axis and degToRad converts angles to radians. Therefore for your case:
glm::vec3 angles(degToRad(0), degToRad(90), degToRad(0));
glm::quat rotation(angles);
Regards

Implements turn left function in openGL

Basically, I need to change the eye and up vectors correctly when pressing the left key (turning the view to right). My implementation is as follows but it does not seem to pass the tests. Anyone can help?
// Transforms the camera left around the "crystal ball" interface
void Transform::left(float degrees, vec3& eye, vec3& up) {
// YOUR CODE FOR HW1 HERE
eye = rotate(degrees, vec3(0, 1, 0)) * eye;
up = rotate(degrees, vec3(0, 1, 0)) * up;
}
The rotation function takes two arguments degree and axis, and returns the rotation matrix which is a 3 by 3 matrix:
mat3 Transform::rotate(const float degrees, const vec3& axis) {
// YOUR CODE FOR HW1 HERE
mat3 rot, I(1.0);
mat3 a_x;
a_x[0][0] = 0;
a_x[0][1] = -axis[2];
a_x[0][2] = axis[1];
a_x[1][0] = axis[2];
a_x[1][1] = 0;
a_x[1][2] = -axis[0];
a_x[2][0] = -axis[1];
a_x[2][1] = axis[0];
a_x[2][2] = 0;
float theta = degrees / 180 * pi;
rot = I * cos(theta) + glm::outerProduct(axis, axis) *(1-cos(theta)) + a_x*sin(theta);
return rot;
}
Try if something like this fixes it:
glm::mat3 Transform::rotate(float angle, const glm::vec3& axis) {
glm::mat3 a_x( 0.0f, axis.z, -axis.y,
-axis.z, 0.0f, axis.x,
axis.y, -axis.x, 0.0f);
angle = glm::radians(angle);
return glm::mat3() * cos(angle) + sin(angle) * a_x
+ (1.0f - cos(angle)) * glm::outerProduct(axis, axis);
}
I googled around and find a solution:
// Transforms the camera left around the "crystal ball" interface
void Transform::left(float degrees, vec3& eye, vec3& up) {
// YOUR CODE FOR HW1 HERE
eye = eye * rotate(degrees, up);
}
The rotation function is correct.