LookAt函数:我快疯了 [英] LookAt function: I'm going crazy

查看:152
本文介绍了LookAt函数:我快疯了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做作业,然后尝试实现lookAt函数.我尝试了很多方法,但是唯一得到的结果是蓝屏.我程序的其余部分都可以很好地工作,如果我使用glm :: look实际上就很好.这是我的代码:

I must do a homework and I try to implement a lookAt function. I tried in many ways but the only result I got is a blue screen. The rest of my program works greatly, infact if I use glm::lookAt all is good. This is my code:

mat4 Transform::lookAt(const vec3 &eye, const vec3 &center, const vec3 &up) 
{
    vec3 w(glm::normalize(eye - center)) ;
        vec3 u(glm::normalize(glm::cross(up, w)));
        vec3 v(glm::cross(w, u)) ;
    mat4 ret = mat4 (
        vec4 (u.x,v.x,w.x,0),
            vec4 (u.y,v.y,w.y,0),
            vec4 (u.z,v.z,w.z,0),
        vec4 (-u.x*eye.x-u.y*eye.y-u.z*eye.z,
             -v.x*eye.x-v.y*eye.y-v.z*eye.z,
             -w.x*eye.x-w.y*eye.y-w.z*eye.z,
             1)
    );
  return ret;

推荐答案

我看到您将glm库用于矩阵运算,因此从glm代码中,lookat实现如下所示:

I saw you use the glm library for matrix operations, so from the glm code the lookat implementation looks like this:

mat4x4 lookAt(vec3  const & eye, vec3  const & center, vec3  const & up)
{
    vec3  f = normalize(center - eye);
    vec3  u = normalize(up);
    vec3  s = normalize(cross(f, u));
    u = cross(s, f);

    mat4x4 Result(1);
    Result[0][0] = s.x;
    Result[1][0] = s.y;
    Result[2][0] = s.z;
    Result[0][1] = u.x;
    Result[1][1] = u.y;
    Result[2][1] = u.z;
    Result[0][2] =-f.x;
    Result[1][2] =-f.y;
    Result[2][2] =-f.z;
    Result[3][0] =-dot(s, eye);
    Result[3][1] =-dot(u, eye);
    Result[3][2] = dot(f, eye);
    return Result;
}

您首先将要使用的向量归一化( f 是您查看的方向, u 向上, s 是正确的向量).然后,要确保上矢量方向右矢量垂直,请将其重新计算为叉积,因为当您给出向上向量,您不能确保其垂直于眼中心向量(视图方向)​​,它们只是形成一个平面,为您提供右向量.

You first normalize the vectors you will use(f is the direction you look at, u the up and s is the right vector). Then to make sure the up vector is perpendicular to the direction and right vectors you recalculate it as their cross product, because when you give an up vector you can't make sure its perpendicular to the eye-center vector(view direction), they're just form a plane which gives you the right vector.

矩阵是由这些构成的.有关更多信息,请参见 http://www.songho.ca/opengl/gl_transform.html 页. 简而言之:这是一个矩阵,可为您创建一个新的坐标系,因此列是轴.然后在最后一列应用翻译矩阵.

The matrix is constructed from these. For more detail how does it works check the http://www.songho.ca/opengl/gl_transform.html page. In short:this is a matrix which creates you a new coordinate system, so the coloumns are the axises. Then at the last coloumn the translation matrix is applied.

(看一下身份矩阵:

AXIS     TRANSFORM
x  y  z  transl.
1, 0, 0, 0
0, 1, 0, 0,
0, 0, 1, 0
0, 0, 0, 1

这为您提供了没有平移的标准坐标系.)

This gives you the standard coordinate system with no translation.)

然后将其与投影矩阵和模型矩阵(p * v * m)相乘,顺序很重要. 编写实现时,请确保使用因opengl导致的大主矩阵或转置它们.

Then you multiply this with projection and model matrixes (p*v*m), the order is important. When you write your implementation make sure you use coloumn major matrixes, because of opengl, or transpose them.

希望对您有帮助.

这篇关于LookAt函数:我快疯了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆