Look from a moving camera to a fix point opengl - c++

My camera is place on a moving object, but it always be pointed to a point in scene. How can I do that? How can I calculate the perpendiculars? How, if the position of the observator always moves, the direction stay focused on that point?

My camera is place on a moving object, but it always be pointed to a point in scene. How can I do that? How, if the position of the observator [sic] always moves, the direction stay focused on that point?
gluLookAt().
How can I calculate the perpendiculars?
Cross-product.

Which version of OpenGL are you using. This depends a lot on which environment you are working on. If you are in OpenGL ES you need to do it yourself, otherwise you can achieve the wanted result by playing with glLookat. Let us know.

Related

OpenGL - skeletal animation - bone rotates around origin

I am following a tutorial series about skeletal animation on Youtube (https://www.youtube.com/watch?v=f3Cr8Yx3GGA) and have ran into a problem – everything works fine, except when I rotate one of the bones (or "joints"), they get rotated around the scene origin, meaning they do not stay in place but are translated. The following image illustrates the problem:
How can I make it so that the translation doesn't happen? I have been going over the tutorial series multiple times now, but cannot identify which step would prevent this from happening.
The code is very large, split into around a dozen files, and I don't know which section might be causing the issue, so I do not think there's much point in posting it all here (it should be similar to the code in the tutorial, even though I am using C++ while he's working in Java. The tutorial code can be found here: https://github.com/TheThinMatrix/OpenGL-Animation). If you could give me even general advice on how this issue is normally solved in skeletal animation, it should hopefully be enough for me to at least identify the part that's wrong and try moving from there.
Rotation matrices on their own can only describe rotations around the origin (Wikipedia). However, rotations can be used in conjunction with translations to change where the origin is to get the desired effect. For example, you could:
Translate the object so that it is centered around the origin
Rotate the object to the desired orientation
Translate the object back to the original position
Or, to phrase it in a different, but functionally equivalent way:
Move the origin to the object's position
Rotate the object to the desired orientation
Reset the origin back to its original position
Related question: Rotating an object around a fixed point in opengl
You just need to pay attention to what you are rotating around.
A way to fix this: Rotate it first and then translate it. Rotate the object while it is at the origin and then translate the object to where you want it.
Repeatedly do this when things change throughout your program. Start the object at the origin, do the desired rotation, and then translate out to it's final resting position.

How do you make a shape static while the camera moves C++ OpenGL?

I want to make a maze game, with shapes in the form of a maze and a person walking through ( or a square or something) in 2d. I know how to make the camera move, but what I don't know is how to make the person/square move with the camera. I'm using C++ with OpenGL, and at the moment I'm using freeglut but I realize this isn't the best thing to be using. I'm still a beginner, so if I can do it in freeglut and not have to download anything that would be optimal. Thanks!
So, I figured out the answer to my own question. I have already built the quad that takes parameters quadx and quad y for the bottom left vertex. I have built a function that moves the camera based on the key that is pressed, and to have the quad appear to stay with the camera, just have it move the same amount as the camera does.
P.S. Make sure to have the thing you want to move with the camera in view at the starting location of the camera, else it will never be on the screen because it will move away just as much as you look for it.

moving object in the world towards a stationary camera

I want to move the camera forward, which is equivalent to moving the world back towards camera. I'm using Glut and glTranslate would do the job, but my question is how should I use it?
Suppose initially I start with glLoadIdentity(), then I set up the look at point using gluLookAt, and then I did some translation/rotation to the model. In this case how should I use glTranslate to translate the object in the world so that they can move with respect to the camera instead of their own origin/coordinate?
I thought I could save the current matrix using glGet, load Identity matrix, then do the translation I wanted, and then multiply the previous matrix back using glMultmatrix. But this didn't work for me.
And also if I want to enable yaw/pitch using glrotate, how should I do? (Also in the sense to rotate the world to make it seems rotating camera)
Sorry for my poor wording or conceptual mistake if there is any. I'm quite new to opengl and graphic programming in general and I'm still trying to fully understand the opengl pipeline, especially the matrix part. Any detailed explanation to that will also be greatly appreciated!
From reading your question, it sounds to me like what you're trying to do is simulate camera movement by translating every other object in the world about a fixed point (the camera)
While you're correct in saying that moving the camera actually moves everything else in the world about it, you seem to be going about it the wrong way. After all, look how much difficulty you're having just moving one box. Now imagine you have hundreds! Not much fun :)
Fortunately, there is a function that can help you, and you're already using it! gluLookAt (http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml) is your guy. What it does under the hood is it creates a matrix (Not sure what a matrix is? Give this a read: http://solarianprogrammer.com/2013/05/22/opengl-101-matrices-projection-view-model/) that every other point in the world is multiplied by. This multiplication translates each point until its in its correct position relative to the camera. So you are correct in saying that moving the camera actually moves the whole world relative to the camera, this way we can do it all in one pass instead of having to calculate the new positions of each point manually.
So, you want to move the camera forward on the z axis? Just call gluLookAt, but pass in a value of eyez that is less than when you previously called gluLookAt. Here's an example:
gluLookAt(0,3,0,0,0,0,0,1,0);//This is out starting position, (0,3,0)
gluLookAt(0,2,0,0,0,0,0,1,0);//And this is out ending position. Notice that the eyez value has decreased by one
As for how to rotate, take a look at the second group of three parameters, the "center" parameters. Those determine what point is in the center of the camera, that is, what the camera is looking at. In the previous example, the center point was (0,0,0). You can rotate the camera by moving these points around. How you do it is a pretty complicated topic with a good bit of math thrown in, but the following links should help a bit:
http://ogldev.atspace.co.uk/www/tutorial15/tutorial15.html
http://www.fastgraph.com/makegames/3drotation/
http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
Don't get discouraged if it seems too hard, keep at it! Feel free to ask me if you need clarification on this answer.

OpenGL perspective x-y-coordinates to orhogonal

Let's say that I have a perspective view in OpenGL and choose a point at a given depth. Let's say that it's at z=-10. How do I know which actual x- and y-coordinates this point have? It is easy when having an orth view because it then is the same for every depth. but for the perspective one: How do I find these x-y-values?
The coordinates you supply are basically "world" coordinates -- i.e., where things exist in the virtual world you build. In other words, the coordinates you work with are always orthogonal.
Just for example, if I was going to do a 3D model of a house, I could set it up so I was working in actual feet, so when I could draw a line from 0,0 to 0,10 to represent something exactly 10 feet long. That would remain 10 feet whether I viewed it up close, so it filled my view, or from a long ways away so it was only a couple of pixels long.
Only when objects are being displayed is perspective transformation done. I don't do it on the coordinates being fed into the system at all.
If you're asking about computing the screen coordinates for an object, yes, you can. The usual way to do this is with gluUnProject. At least in my experience it's relatively unusual that you end up needing to do this though.
The one time you sort of care is when you're selecting something on-screen using the mouse. Though it's possible to do with with gluUnProject, OpenGL has a selection mode that's intended specifically for this kind of purpose, and it works pretty well.
Look at gluProject as a way of projecting your cursor position into a "world" position (and gluUnproject as a way of finding out where your object is on screen).

OpenGL: the ultimate coordinate system confusion solution?

I'm tired of thinking how the hell my coodinates are working at each case.
I heard that I could flip the Y-axis by this code:
glScalef(1, -1, 1);
But should I? Doesnt this break some other external functions and lighting etc?
i may say, getting used to opengl defaults is the fastest way of walking out of confusing.
There's no correct yes/no answer to this question. Calling glScalef(1, -1, 1) means using a left-handed instead of a right handed coordinate system, this has historically been the choice of for instance Direct3D and the RenderMan Interface. Some people feel it's more intuitive to have the positive z-axis pointing in to the screen instead of out of it (which is one version of a left handed coordinate system, the y-axis pointing down is another).
If you chose to switch coordinate system, you will have to change some of the standard settings in OpenGL to make it work. For instance you probably want to call glCullFace(GL_FRONT) (or reverse the order of all triangles sent to OpenGL).
I could be wrong, but it might turn your models inside-out. You will probably also need to swap your vertex winding order. If you use two-sided polygons, though, then you don't need to worry.