gluLookAt alternative

Started by
5 comments, last by polymorphed 15 years, 7 months ago
Is there an gluLookAt alternative? I dont want to move the object in order to simulate camera movement. Or ... if I am using glTranslate, is there a way to RESET the objects coordinates so where the object is moved is now 0,0,0?
Advertisement
Quote:Original post by McCoder
Or ... if I am using glTranslate, is there a way to RESET the objects coordinates so where the object is moved is now 0,0,0?


Use glGetFloatv() and then glTranslatef(), like this:

// untestedfloat matrix[16];glGetFloatv(GL_MODELVIEW_MATRIX, matrix);glTranslatef(-matrix[12], -matrix[13], -matrix[14]); // x - x = 0


[Edited by - polymorphed on September 19, 2008 2:06:39 PM]
while (tired) DrinkCoffee();
Quote:Original post by polymorphed
Quote:Original post by McCoder
Or ... if I am using glTranslate, is there a way to RESET the objects coordinates so where the object is moved is now 0,0,0?


Use glGetFloatv() and then glTranslatef(), like this:

*** Source Snippet Removed ***



Thanks, I cant understand your code though. I declare a 4x4 matrix, empty. I retrieve the info of the current modelview matrix ... and then what? I can't understand why 13, 14, 15. So it's element 13, 14, 15 as x, y, z, but why with minus?

Use other method.

[Edited by - polymorphed on September 19, 2008 2:30:37 PM]
while (tired) DrinkCoffee();
The previous method won't work well if you're planning on doing some rotation as well though [smile].
Here's a better method:

float matrix[16];glGetFloatv(GL_MODELVIEW_MATRIX, matrix);matrix[12] = 0;matrix[13] = 0;matrix[14] = 0;glMatrixMode(GL_MODELVIEW);glLoadMatrixf(matrix);
while (tired) DrinkCoffee();
Ah, makes sense, I did not know that when you translate you actually add to the matrix. Good thing to keep in mind, also, must brush up on my matrix math. So OpenGL uses the 13, 14, 15 element in a matrix for x coord stuff?
Well it actually doesn't add to it, that was the wrong way to put it, sorry.
What really happens is a matrix multiplication.
When you call glTranslatef(x, y, z), a temporary matrix is constructed, which looks like this:

1, 0, 0, x0, 1, 0, y0, 0, 1, z0, 0, 0, 1


This matrix is then multiplied with the current matrix (modelview matrix, for example). This allows for the current orientation (stored in the upper left 3x3 quadrant of the matrix) to affect the translation.

And yes, since OpenGL stores its matrices in column-major order, when you map the matrix from 2D to 1D, it'll look like this:

A, B, C, D 	E, F, G, H  -->  A, E, I, M, B, F, J, N, C, G, K, O, D, H, L, P	I, J, K, L 	M, N, O, P	


D, H and L is the translation component.
while (tired) DrinkCoffee();

This topic is closed to new replies.

Advertisement