3
\$\begingroup\$

I'd like a little help here calculating the view matrix for OpenGL based on the lookat type function:

vec3f eye    = {0, 0, 0};
vec3f center = {10, 2, 21};
vec3f up     = {0, 1, 0};
mat4* vm;
mat4LookAt(vm, &eye, &center, &up);

mat4print("lookat",vm);

Matrix output:

  lookat  
[ -0.902861   -0.036833   -0.428353   0.000000    ]
[ 0.000000    0.996324    -0.085671   0.000000    ]
[ 0.429934    -0.077349   -0.899541   0.000000    ]
[ 0.000000    0.000000    -0.000000   1.000000    ]

The code that I used to create this matrix:

vec3f x = {1, 0, 0};
vec3f y = {0, 1, 0};
vec3f z = {0, 0, 1};

vec3fsub(&z, eye, center);
vec3fprint("z unnorm    ",&z);
vec3fnorm(&z, &z);
vec3fprint("z norm      ",&z);

vec3fcross(&x, &y, &z);

vec3fprint("x = y cros z",&x);
vec3fcross(&y, &z, &x);

vec3fprint("y = z cros x",&y);
vec3fprint("x unnorm    ",&x);
vec3fnorm(&x, &x);
vec3fprint("x norm      ",&x);
vec3fprint("y unnorm    ",&y);
vec3fnorm(&y, &y);
vec3fprint("y norm      ",&y);

double xx, yy, zz, x1, y1, z1;
xx = vec3fdotr(&x, eye);
yy = vec3fdotr(&y, eye);
zz = vec3fdotr(&z, eye);

x1 = vec3fdotr(eye, &x);
y1 = vec3fdotr(eye, &y);
z1 = vec3fdotr(eye, &z);

printf("x:%f\ty:%f\tz:%f\t\n",xx, yy, zz);
printf("x:%f\ty:%f\tz:%f\t\n",x1, y1, z1);

double data[] = { (&x)->p[0], (&y)->p[0], (&z)->p[0], 0,
                  (&x)->p[1], (&y)->p[1], (&z)->p[1], 0,
                  (&x)->p[2], (&y)->p[2], (&z)->p[2], 0,
                  xx,        yy,       zz,        1      };

copyMatrix(out, data);

Is this the proper way to calculate the lookat matrix that will take my models from object to view space?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.