Jump to content

  • Log In with Google      Sign In   
  • Create Account


Camera positioning


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 SM ForeveR   Members   -  Reputation: 138

Like
0Likes
Like

Posted 08 February 2012 - 10:32 AM

Hi, I have been thinking for a few days on how to properly create camera matrix so I could specify it just as any other model in scene and the view would still be from camera object.
I use these methods to get camera matrix:
public static float[] getViewMatrix(float[] position, float[] rotation) {
  float[] matTrans = Matrix3D.copy(position);
  float[] matRot = Matrix3D.copy(rotation);
  // Transpose rotation matrix.
  float tmp;
  tmp = matRot[0 * 4 + 1]; matRot[0 * 4 + 1] = matRot[1 * 4 + 0]; matRot[1 * 4 + 0] = tmp;
  tmp = matRot[0 * 4 + 2]; matRot[0 * 4 + 2] = matRot[2 * 4 + 0]; matRot[2 * 4 + 0] = tmp;
  tmp = matRot[1 * 4 + 2]; matRot[1 * 4 + 2] = matRot[2 * 4 + 1]; matRot[2 * 4 + 1] = tmp;
  // Invert translation.
  matTrans[3 * 4 + 0] *= -1.0f;
  matTrans[3 * 4 + 1] *= -1.0f;
  matTrans[3 * 4 + 2] *= -1.0f;
  return Matrix3D.multiply(matRot, matTrans);
}
public static final float[] multiply(float[] b, float[] a) {
  // Return identity if matrices aren't 4x4.
  if (a.length != b.length && a.length != 16)
   return identity();
  // Initialize empty matrix.
  float[] result = new float[16];
  // Multiply.
  int k = 15;
  for (int i = 3; i >= 0; i--) {
   for (int j = 3; j >= 0; j--) {
	result[k] += a[i * 4] * b[j];
	result[k] += a[i * 4 + 1] * b[4 + j];
	result[k] += a[i * 4 + 2] * b[8 + j];
	result[k] += a[i * 4 + 3] * b[12 + j];
	k--;
   }
  }
  return result;
}
position and rotation float arrays are matrices.
The problem is, that viewing arround doesn't quite work on some directions unless I comment these lines:
tmp = matRot[0 * 4 + 1]; matRot[0 * 4 + 1] = matRot[1 * 4 + 0]; matRot[1 * 4 + 0] = tmp;
  tmp = matRot[0 * 4 + 2]; matRot[0 * 4 + 2] = matRot[2 * 4 + 0]; matRot[2 * 4 + 0] = tmp;
  tmp = matRot[1 * 4 + 2]; matRot[1 * 4 + 2] = matRot[2 * 4 + 1]; matRot[2 * 4 + 1] = tmp;

Any ideas what I'm doing wrong? This is on android.
Multiply method is tested and is working.
Thank you, Martin.

EDIT:
if I understand correctly, I need to inverse my camera position and rotation matrix to be able to get view from where camera is positioned with original matrix. Is there something wrong with my inverse matrix implementation?

Sponsor:

#2 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 08 February 2012 - 02:30 PM

I'm a little bit rusty here so take this with a little bit of skepticism if I'm incorrect.

To get the view matrix from the position of an object, I believe you want the inverse of that object's model matrix. With typical opengl notation (translate (T) than rotate ®), you'd get the model matrix by M = R*T. To get the inverse of M (M-1), you would find (R*T)-1, or (T-1*R-1). You are finding T-1 and R-1, but it looks like you're multiplying (R-1*T-1) instead of (T-1*R-1) (if I'm understanding your matrix multiply arguments correctly).

Long story short, maybe you should do
Matrix3D.multiply(matTrans, matRot);

instead?
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#3 SM ForeveR   Members   -  Reputation: 138

Like
0Likes
Like

Posted 08 February 2012 - 10:36 PM

Hi, karwosts, unfortunately it doesn't work that way, camera just starts rotating around one point.

#4 meeshoo   Members   -  Reputation: 508

Like
0Likes
Like

Posted 09 February 2012 - 02:43 PM

I forgot a lot of this, but these tutorials might help a bit: http://www.arcsynthesis.org/gltut/Positioning/Tutorial%2008.html.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS