0

I am rotating the camera around itself (when the camera is located at (0,0,0)) using the following:

glRotatef(x_camera_angle, 1.0, 0.0, 0.0);
glRotatef(y_camera_angle, 0.0, 1.0, 0.0);

I wish to move the camera in the direction its looking, and to move objects according to the camera direction (for example, to move object to the camera and away from camera).

I've tried achieving this using the modelview matrix as specified in here: https://stackoverflow.com/a/16137191/3362159 , but this doesn't seem to work. For example, I tried moving the camera forward (according to its direction) using the following code:

glTranslatef(front[0] * units_forward, front[1] * units_forward, front[2] * units_forward);

where "front" is the matrix specified in the answer. The camera doesn't move forward, It moves differently depending on its direction.

What am I doing wrong? Any help would be appreciated.

Community
  • 1
  • 1
user130955
  • 75
  • 10
  • 1
    What is an order of matrix operations? (both camera and drawing). Do you invert camera matrix? – keltar Jun 01 '14 at 10:50
  • [This is a related post](http://stackoverflow.com/questions/22715044/c-opengl-wireframe-cube/22715392#22715392) which has the details of what you want provided you're ready to invest some time to understand how a camera works in 3D graphics programming. Once you understand this you should be able to code it up yourself. – legends2k Jun 01 '14 at 11:08

1 Answers1

1

Did you try just glTranslatef(0,0,units_forward)? Is that maybe what you expect?

The root cause of your confusion is how matrices work. gl(Matrix) functions all multiply the current matrix with the matrix they build. As keltar corrected on the left side. One strange thing about matrices is that A*B != B*A. Usually the right side of a product "happens first". So if A is a rotation and B is moving forward, A*B means "move forward then rotate around the point I moved to" while B*A means "rotate and then move forward where we are pointing to ahead now".

The answer you reference takes a not very helpful shortcut.

You always have two option:

  • gl(Operation) multiplies left
  • glGet(tempmatrix), glLoadIdentity() gl(Operation) glMultMatrix(tempmatrix) multiplies right

Try both! See how they are different. One of them will do what you want with just Operation=Translate and (0,0,amount).

starmole
  • 4,974
  • 1
  • 28
  • 48
  • 1
    GL applies matrices so `result = new_multiplier * result`. It provides 'natural' order - `glRotate` + `glTranslate` = rotate, then translate. If reverse order is needed, it is usually better to just change order of operations. – keltar Jun 01 '14 at 10:48
  • Thanks for the correction. I always forget :) I will edit the answer to be right. – starmole Jun 01 '14 at 10:56
  • @keltar: It's the opposite. The matrix specified last is the one applied first. So if you call `glRotate` and the `glTranslate`, the translation is applied to your coordinates first, then the rotation. – Reto Koradi Jun 01 '14 at 14:26
  • @RetoKoradi opposite order is exactly what I've told. About the rest, https://www.opengl.org/sdk/docs/man2/xhtml/glMultMatrix.xml see 'notes' section. I have a feeling that we're saying the same things, but with different words. – keltar Jun 01 '14 at 14:53
  • @keltar: Look at your comment, you said "glRotate + glTranslate = rotate, then translate". That sequence of calls translates first, then rotates. The formatting of those man pages on opengl.org does not work in my browser. But if you look at http://msdn.microsoft.com/en-us/library/windows/desktop/ee872055(v=vs.85).aspx, it says "That is, if M is the current matrix and T is the matrix passed to glMultMatrix, then M is replaced with M • T", which is the correct order if multiplying the vectors as column vectors on the right. So in your notation, that's `result = result * new_multiplier`. – Reto Koradi Jun 01 '14 at 15:22
  • @RetoKoradi unfortunately it is a matter of perspective. Math definition of matrix multipltication is usually (wikipedia or any math book) row-major. GL internally uses column-major definition. However, changing rows and columns in mul have the same result as changing order of multiplied matrices, so technically both are correct. Still, translate+rotate logically will rotate vertices around new local centre (e.g. object centre), while rotate+translate will rotate local centre around global (0, 0, 0) point. Internally yes, operation order is reversed. – keltar Jun 01 '14 at 16:29
  • @RetoKoradi: post-multiplication of column-major matrices (GL) is the same thing as pre-multiplication of row-major. In math textbooks, matrix multiplication is of the row-major form. @keltar: With all that said, I have no idea what is implied by `glRotate` + `glTranslate`, that looks like ***adding*** two matrices together... which would be incorrect no matter what notation you use; it should be matrix multiplication. – Andon M. Coleman Jun 01 '14 at 23:21
  • @AndonM.Coleman sequence of actions... And if I would have written '*', someone surely be saying that I'm multiplying functions and not matrices. Senseless. That's enough already :-) – keltar Jun 02 '14 at 02:25