1

I've been trying to set up a camera in OpenGL for a few days now, but I just can't get it working properly. As the eye.X or eye.Y value increases/decreases, instead of "looking arround", the camera(I know there is no such thing as a camera...) moves away/gets closer to the drawings!

I hope following information can help you.(I also uploaded a Video, which is showing the error): https://www.youtube.com/watch?v=OlD5X0EzkUw

These are my ModelView calculations on OnUpdateFrame.

_worldPosition = Matrix4.CreateTranslation(_worldX, _worldY, _worldZ);
_cameraMatrix = Matrix4.LookAt(eye.X, eye.Y, eye.Z, center.X, center.Y, center.Z, up.X, up.Y, up.Z);

_modelViewMatrix = _worldPosition * _cameraMatrix;

Here, I adjust the values with OnMouseMove function:

if (_mouseDown)
{
     _mousePos.X = 2.0f * Mouse.X / Width - 1;
     _mousePos.Y = 2.0f * Mouse.Y / Width - 1;

     _mouseDeltaY = (_mousePosOld.Y - _mousePos.Y) / 10;
     _mouseDeltaX = (_mousePosOld.X - _mousePos.X) / 10;

     eye.X -= _mouseDeltaX;
     eye.Y -= _mouseDeltaY;
}

And finally my Vertex shader:

#version 440 core

layout (location = 0) in vec4 position;
layout(location = 1) in vec4 color;

layout(location = 2) uniform  mat4 projectionMatrix;
layout (location = 3) uniform  mat4 modelViewMatrix;

out vec4 vs_color;

void main(void)
{
    gl_Position = projectionMatrix * modelViewMatrix * position;
    vs_color = color;
}
BoyBaykiller
  • 109
  • 1
  • 7
  • Your matrix multiplication order is inconsistent. I don't know which conventions you are supposed to use, but your vertex transformation seems to end up as `proj * model * view * vertex` which does not make sense in any convention. – derhass Feb 27 '20 at 17:19
  • 1
    @derhass No, it's OpenTK. `_modelViewMatrix = _worldPosition * _cameraMatrix;` is fine. See [this](https://stackoverflow.com/questions/57976750/opengl-4-2-lookat-matrix-only-works-with-z-value-for-eye-position/57978635#57978635) – Rabbid76 Feb 27 '20 at 17:47
  • @noob. If you want to look around, then you have to modify `center` rather than `eye`. – Rabbid76 Feb 27 '20 at 17:51
  • @Rabbid76 Well I did not know that, thx :). But is it normal, that I can't do a full rotation? I mean it starts slowing down, as I further turn right/left or up/down. `Center` still increases. – BoyBaykiller Feb 27 '20 at 18:07
  • @noob. For a rotation `center` has to rotate around `eye`. – Rabbid76 Feb 27 '20 at 18:12
  • @Rabbid76 I am not sure if we are on the same level. Sorry for bad english. With rotation I do not mean to rotate arround a object. I want the camera to do a full 360 degress turn arround, so that I start seeing my objects again. Just like in real-life when you stand still and "rotate" in a circle. So that after 360 degress I have my old vision back. – BoyBaykiller Feb 27 '20 at 18:22
  • @noob Yes I no, You want to turn around "yourself". That is a rotation, isn't it. See the answer. Possibly you are interested in [this](https://github.com/Rabbid76/c_sharp_opengl) (especially in the projects "WPF Orbit, pan, zoom" and "WPF First person") – Rabbid76 Feb 27 '20 at 18:26
  • @Rabbid76: OK, but one has to throw in at least one matrix transposition (intentional or unintentional by mixing up the storage layout) to make this work. – derhass Feb 27 '20 at 21:29
  • @derhass No, you don't have to. In OpenTK, just the matrix multiplication operates in the wrong order. Just the C# overloaded `*`-operator is implemented the wrong way. Nice, isn't it. – Rabbid76 Feb 27 '20 at 21:32
  • 1
    @Rabbid76 actually, matrix operator working in the wrong direction can be interpreted as 3 unintentional transposes, since `A*B = transpose(transpose(B)*transpose(A))`. But yeah, it is rather silly no matter how one is going to interpret it. I actually had already upvoted your other answer there, so I must have seen it before, but I forgot about it, probably because that situation is so silly... ;) – derhass Feb 27 '20 at 21:39

1 Answers1

1

To look around you have to rotate center around eye.

e.g If you want to do a rotation around the x axis (up, down) the you have to setup a rotation matrix dependent on _mouseDeltaY:

float angle_degree = _mouseDeltaY;
Matrix4 rotateY = Matrix4.CreateRotationY(angle_degree * (float)Math.PI / 180.0f);

Compute the vector from eye to center and rotate it:

Vector3 toTarget = center - eye;
toTarget = Vector4.Transform(new Vector4(toTarget, 0.0f), rotateY).Xyz;

Finally compute the new center

center = eye + toTarget;

eye and center are assumed to be of type Vector3. Possibly you have to change the scale of _mouseDeltaY for your needs.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174