Rotation about Camera - opengl

In OpenGL, if you rotate first and translate afterwards, then all objects will rotate about their own origins. If you translate first and then rotate, objects should rotate around the origin (camera). But this doesn't happen. Everything rotates about the world axis, not the camera.
This:
transformationMatrix.setIdentity();
transformationMatrix.mul(rotationMatrixX);
transformationMatrix.mul(rotationMatrixZ);
transformationMatrix.mul(rotationMatrixY);
transformationMatrix.mul(translationMatrix);
doesn't work. How do I rotate everything around the camera? For object in the center of the "world" (a cube at 0,0,0), translation and rotation are relative to the camera. The code above rotates the whole world, but the center of the world never changes its coordinates relative to camera. The camera has always the same direction of view.
===========================================
ADDED:
THERE IS NO lwjgl.util IN lwjgl 3 ANYMORE. SO NO gluLookAt FUNCTION.
I'v solved the problem by rotation around arbitrary axis. Tries to use JOGL, but somethig is wrong: JOGL setPerspective wrong?

Related

Rotating object around camera and getting transformed coordinates?

I'm using OpenGL to draw an object in my scene, but I can't seem to get it to rotate around the camera rather than just rotating in place.
What can I do to have it rotate around the camera instead? I know that I can draw it in camera/view space, but I need to get the object's position and rotation from after the transformation (so that I can give that object a rigid body with Bullet). If the position and rotation can still be retrieved from an object rendered in the view space, then please disregard the first part of the post.
In other words, how can I move and rotate the object around the camera and get it's coordinates and rotation from the ModelMatrix so that I can fill these:
glm::vec3 objectPosition;
glm::quat objectRotation;
I've seen similar posts online that use older OpenGL/not the MVP framework, so I'm not sure how to do this. Cheers.
Have you tried using MVP2 without the projection matrix? The projection matrix splats it on to the screen. So, taking that out might help.

Render object in front of camera in OpenGL

I wanna have an object (e.g. a sphere) which is placed directly in front of the camera (let's say "5cm" in front). When the camera is moved, the object should always follow the movement of the camera.
In my rendering loop, I calculate the position of the camera in world space using the inverse of the view matrix:
glm::mat4 viewMatrixInverse = glm::inverse(camera->GetViewMatrix());
glm::vec3 cameraPositionWorldSpace = glm::vec3(viewMatrixInverse[3][0], viewMatrixInverse[3][1], viewMatrixInverse[3][2]);
This seems to work fine and as I move towards the world origin, the camera position approaches (0,0,0) as well. Now when I try to render a sphere at the camera position, this works perfectly fine and the camera is always positioned in the centre of the sphere.
The rendering part looks like this:
sphere->ResetModelMatrix();
sphere->TranslateModel(cameraPositionWorldSpace);
sphere->Render(GL_TRIANGLES);
Now, I don't want to "sit" inside the sphere, I'd rather have the sphere positioned a few units in front of the camera. I cannot seem to figure out how I have to translate the sphere correctly so that it is always aligned with the direction vector of the camera.
add an offset vector to the position in the direction the camera is looking:
sphere->TranslateModel(cameraPositionWorldSpace+camera->LookingDirection().normalized()*5);

DirectX 11 C++ How to rotate a texture coordinate along a point?

I'm trying to make a whirling effect where the screen is rotating along the center point.
So the end result would be a whirlpool continuously spinning with the center of the screen being the focal point.
I tried to rotate the texture coordinate in the vertex shader by XMMatrixRotationZ, but it rotates the whole screen obviously.
I need to rotate around the center point (0.5, 0.5) but unsure how to go about it.

OpenGL (simple) scene & object navigation basics for "lookthrough" camera

I've read through several over-complicated articles on rendering a 3D scene with OpenGL, and haven't really found something that helps me to visualize the basic concepts of scene navigation with respect to glRotate and glTranslate calls.
From experimenting with the examples provided by LWJGL (my particular OpenGL library), I understand very basically what effect comes of their use. I can use glTranslate to move to a point, glRotate to rotate about that point, and glLoadIdentity to snap back to the origin or glPopMatrix to go back to the last glPushMatrix, which are essentially snapshots of a location and rotation. Finally, the scene will render to screen with respect to the origin.
So basically, to put a cube at point A with rotation B:
glTranslate(A.x,A.y,A.z) [now focused on point A]
glRotate(B.,,*,*) for pitch, yaw, and roll; [now rotated to rotation B]
glBegin(GL_QUADS) and glVertex3f()x4 for each 'side'(quad) relative to object's origin
glEnd()
glLoadIdentity() [reset to origin for other objects, not needed if only drawing the cube]
As for the "camera", since openGL's "eye" is fixed, the scene has to move inversely from the camera in order to simulate moving the eye with the camera. This is my understanding so far, and if this is wrong, please put me on the right path.
Now for my specific situation and question:
My 'scene' consists of terrain and a player (some arbitrary shape, located at a terrain-relevant location and a 'camera' head). The view should rotate with respect to the player, and the player move with respect to the terrain. I want the final rendering to essentially "look through" the player's head, or camera. My confusion is with the order of translate/rotate calls for rendering each part, and the direction of translation. Is the following the proper way to render the terrain and player with respect to the player's "camera"?
translate away from the player by the player's distance from origin (move the scene)
rotate away from the player's rotation (player looks 40 degrees right, so rotate scene 40 left)
render terrain
reset via glLoadIdentity
render player's head (if needed)
translate to body/hands/whatever position
rotate away from the player's rotation (step 2 again)
render body/hands/whatever
Also, does rotating have an effect on translation? Aka, does OpenGL translate with respect to the rotation, or does rotation have no bearing on translation?
I can use glTranslate to move to a point, glRotate to rotate about that point, and glLoadIdentity to snap back to the origin or glPopMatrix to go back to the last glPushMatrix, which are essentially snapshots of a location and rotation.
No not quite. Those functions have now idea of what a scene is. All they do is manipulating matrices. Here's an excellent article about it:
http://www.opengl.org/wiki/Viewing_and_Transformations
One important thing to keep in mind when working with OpenGL is, that it is not a scene graph. All it does is rendering flat points, lines and triangles to the framebuffer. There's no such thing like a scene you navigate.
So basically, to put a cube at point A with rotation B:
(...)
Yes, you got that one right.
As for the "camera", since openGL's "eye" is fixed
Well, OpenGL got no camera. It's only pushing vertices through the modelview matrix, does lighting calculations on them, then passes them through the projection and maps them to the viewport.

Better modifying the render or moving the camera?

Since I gogled for it without finding anything interesting, I would like to ask you for some suggestions regarding if it is better to scale/translate the render itself keeping the camera position fixed or maybe moving closer/further or rotate the camera keeping the render position fixed?
I need a zooming out/in, rotation in all the 3 axes and also this kind of rotation
http://www.reknow.de/downloads/opengl/video.mp4
that is, if I first translate my render and then I apply a rotation, this rotation should consider the center always the windows center, and not the translated one
I need a zooming out/in, rotation in all the 3 axes and also this kind of rotation
What you mean is probably not "zooming" but "panning". And in OpenGL you place the "camera" by moving the scene around, because there is no camera.
Zooming is a change in the focal length, and would be implemented by changeing the FOV of the perspective.