Understanding glm::lookAt()
The up
vector is basically a vector defining your world's "upwards" direction. In almost all normal cases, this will be the vector (0, 1, 0)
i.e. towards positive Y. eye
is the position of the camera's viewpoint, and center
is where you are looking at (a position). If you want to use a direction vector D
instead of a center position, you can simply use eye + D
as the center position, where D
can be a unit vector for example.
As for the inner workings, or more details, this is a common basic function for building a view matrix. Try reading the docs for gluLookAt() which is functionally equivalent.
glm::lookat, perspective clarification in OpenGL
I would expect my origin-centered object to move right in the resulting screen projection. But I see the opposite is the case.
glm::lookAt
defines a view matrix. The parameters of glm::lookAt
are in world space and the center
parameter of glm::lookAt
defines the position where you look at.
The view space is the local system which is defined by the point of view onto the scene.
The position of the view, the line of sight and the upwards direction of the view, define a coordinate system relative to the world coordinate system. The view matrix transforms from the wolrd space to the view (eye) space.
If the coordiante system of the view space is a Right-handed system, then the X-axis points to the left, the Y-axis up and the Z-axis out of the view (Note in a right hand system the Z-Axis is the cross product of the X-Axis and the Y-Axis).
The line of sight is the vector form the eye
position to the center
positon:
eye = (0, 0, 10)
center = (2, 0, 0)
up = (0, 1, 0)
los = center - eye = (2, 0, -10)
In this case, if the center of the object is at (0, 0, 0) and you look at (0, 0, 2), the you look at a position at the right of the object, this means that the object is shifted to the left.
This will change, if you change the point of view e.g. (0, 0, -10) or flip the up vector e.g. (0, -1, 0).
About LookAt() function
All what LookAt does is a translation T (so that the new origin is at the point eye) followed by a rotation R. The rotation is defined by building an orthonormal basis from the 3 vectors (direction defined by the vector from eye to center, the up vector directly specified, and the right vector which is defined to be perpendicular to both). The final transorm produced by LookAt would be R*T.
You can use LookAt without any gimbal lock problems if you specify your input vectors correctly, but you can also describe your camera by a position vector (defining T) and orientation quaternion (defining R), and wouldn't have to use LookAt at all.
Opengl GLM lookAt strange behaviour
the three parameters to glm::lookAt
are eye
, center
and up
. So, you set your eye wherever you want it to be and it should not move by itself. setting it to the sun's position looks correct (but make sure it's in world coordinates)
what looks a bit fishy is your center parameter. it's supposed to be the point the camera should look at. negating some world position in there doesn't make sense to me :)
the reason why you have anything on screen despite that negation might be your near plane (second to last parameter in glm::ortho
). Usually that's the distance you want objects to be visible in front of your camera. With a negative value, you will see objects that are behind your camera.
edit: to clarify, it can make sense to have negative near planes in orthografic projections, i just think it's easier to have the sun at some distant location and then let it render everything that is in front of it. that might make this conceptually easier.
long story short, try not negating the target position and using some positive value for the near plane.
glm::lookAt gives unexpected result for 2D axis
That's not how view and projection work. The view matrix tells you which point should be mapped to [0,0] in view space. It seems you try to map the center of the visible area to [0,0], but then you use a projection matrix which assumes that [0,0] is the top-left corner.
Since you first apply the view matrix, that gives a result of view * glm::vec4(v, 0.0f, 1.0f) = [0,0]
. Then the projection matrix get's applied where you defined that 0,0 as the top-left corner, thus proj * [0,0]
will result in [-1,-1].
I'm not 100% sure what you want to achieve, but if you want to use the given projection matrix, then the view matrix has to transform the scene in a way that the top-left point of the visible area get's mapped to [0,0].
You can also adjust the project to use the range [-320, 320] (and respectively [-240, 240]) and keep mapping the center to [0,0] with the view matrix.
Passing a vec3 to glm::lookAt appears to modify it
I have encountered a situation where passing a
glm::vec3
to theglm::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.
Related Topics
Virtual Dispatch Implementation Details
C++ Standard Practice: Virtual Interface Classes VS. Templates
Returning Std::String/Std::List from Dll
Linear Index Upper Triangular Matrix
Operator << Must Take Exactly One Argument
Large 2D Array Gives Segmentation Fault
Templates: Template Function Not Playing Well with Class's Template Member Function
Lru Implementation in Production Code
When to Use 'Asio_Handler_Invoke'
What Are Potential Dangers When Using Boost::Shared_Ptr
C++ Objects: When Should I Use Pointer or Reference
"Roll-Back" or Undo Any Manipulators Applied to a Stream Without Knowing What the Manipulators Were
Qt: How to Handle the Event of the User Pressing the 'X' (Close) Button
Copying Derived Entities Using Only Base Class Pointers, (Without Exhaustive Testing!) - C++
Accessing Elements of a Cv::Mat with At<Float>(I, J). Is It (X,Y) or (Row,Col)