Public Group

# Translating camera after lookat

This topic is 3524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm quite new to OpenGl and am having problems with implementing my camera class. I've coded my own version of gluLookAt (no Glu on target platform) which works fine. However, I would like the option of being able to being able to rotate/move camera after using lookAt, so I believe I should really store the matrix locally. As I understand it, lookAt sets the view matrix directly.. however, if i cache the viewmatrix calculated in my lookAt function and load it seperately (so i can hopefully make functions later to modify it), it doesn't seem to work at all. Here's my code to explain it better. Firstly my lookAt function that sets view matrix directly and works perfectly:
- (void)lookAt:(GLfloat) upx: (GLfloat) upy: (GLfloat) upz{

GLfloat x[3], y[3], z[3];
/* Make rotation matrix */
/* Z vector */
z[0] = Position[0] - targetX;
z[1] = Position[1] - targetY;
z[2] = Position[2] - targetZ;
normalizeVec(z);
/* Y vector */
y[0] = upx;
y[1] = upy;
y[2] = upz;
/* X vector = Y cross Z */
CrossPrd(y[0], y[1], y[2], z[0], z[1], z[2], x);
/* Recompute Y = Z cross X */
CrossPrd(z[0], z[1], z[2], x[0], x[1], x[2], y);

normalizeVec(x);
normalizeVec(y);
float m[] = {x[0], y[0], z[0], 0, x[1], y[1], z[1], 0, x[2], y[2], z[2], 0, 0, 0, 0, 1,};
glMatrixMode(GL_MODELVIEW);
/* Translate Eye to Origin */
glTranslatef(-Position[0], -Position[1], -Position[2]);
}

Now, this is the function I've derived from the lookAt function that does the same thing but stores the result in a matrix to load in another function.
- (void) pointAt{
GLfloat x[3], y[3], z[3];
/* Make rotation matrix */
/* Z vector */
z[0] = Position[0] - targetX;
z[1] = Position[1] - targetY;
z[2] = Position[2] - targetZ;

normalizeVec(z);
float upx = 0.0f;
float upy = 1.0f;
float upz = 0.0f;
/* Y vector */
y[0] = upx;
y[1] = upy;
y[2] = upz;
/* X vector = Y cross Z */
CrossPrd(y[0], y[1], y[2], z[0], z[1], z[2], x);
/* Recompute Y = Z cross X */
CrossPrd(z[0], z[1], z[2], x[0], x[1], x[2], y);

normalizeVec(x);
normalizeVec(y);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
setViewMatrix(viewMatrix, x[0], y[0], z[0], 0, x[1], y[1], z[1], 0, x[2], y[2], z[2], 0, 0, 0, 0, 1);
glTranslatef(-Position[0], -Position[1], -Position[2]);
glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix);
glPopMatrix();
}

And I load the matrix in this function:
- (void)useView{
glMatrixMode(GL_MODELVIEW);
}

Yet I get a totally different result, for some reason. Anybody got any ideas..?

##### Share on other sites
Oh and this is the code for the setViewMatrix function I use, to ensure there aren't any inconsistencies there:

void setViewMatrix(float res[16], float x1, float x2, float x3, float x4, float y1, float y2, float y3, float y4, float z1, float z2, float z3, float z4, float l1, float l2, float l3, float l4){res[0] = x1;res[1] = y1;res[2] = z1;res[3] = l1;res[4] = x2;res[5] = y2;res[6] = z2;res[7] = l2;res[8] = x3;res[9] = y3;res[10] = z3;res[11] = l3;res[12] = x4;res[13] = y4;res[14] = z4;res[15] = l4;}

##### Share on other sites
Bump!

Nobody has any ideas..? Still stuck with this, to be honest!

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631371
• Total Posts
2999605
×