定位人物以使用OpenGL观察相机 [英] Orientating Figures to look at the Camera with OpenGL

查看:102
本文介绍了定位人物以使用OpenGL观察相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的opengl应用程序中,我想确定人物的方位以看着相机,为此,我为所有对象定义了2个矢量(从上到下).

我使用gluLookAt来控制相机,因此很容易知道我需要的向量newFront和newUp.

我用来控制每个图形方向的代码是:

m4D orientate(v3D newFront, v3D newUp)
{
    double angle = angle_between(front, newFront);
    v3D cross = normalize(cross_product(front, newFront));

    m4D matrix = rotate_from_axis(angle, cross);

    up = normalize(up * matrix);

    angle = angle_between(up, newUp);
    cross = normalize(cross_product(up, newUp));

    return(rotate_from_axis(angle, cross) * matrix);
}

当矩阵堆栈仅具有此矩阵时,此代码运行良好,但是如果我推动先前的矩阵旋转(当然是前向和向上矢量旋转),则会失败.

我怎么了?

解决方案

为什么有一个更简单的方法,那些复杂的我求逆向旋转并将其乘到模型视图上"的广告牌/广告发布者解决方案?

令M是要从中确定广告牌矩阵的模型视图矩阵.矩阵是4×4实值类型.左上方的3×3定义旋转和缩放.对于广告牌来说,这部分就是身份.

因此,将当前模型视图矩阵的左上部分替换为恒等,其余部分保持不变.

 1  0  0 tx
 0  1  0 ty
 0  0  1 tz
wx wy wz ww

,并使用该矩阵进行进一步的转换,您将获得所需的效果.如果应用了缩放比例,则用缩放矩阵替换左上角的标识.

In my opengl app, i want to orientate figures to look at the camera, to make this, i define for all the objects 2 vectors, front and up.

Im using gluLookAt to control the camera, so the vectors newFront and newUp i need are easily known.

The code i use to control the orientation for each figure is :

m4D orientate(v3D newFront, v3D newUp)
{
    double angle = angle_between(front, newFront);
    v3D cross = normalize(cross_product(front, newFront));

    m4D matrix = rotate_from_axis(angle, cross);

    up = normalize(up * matrix);

    angle = angle_between(up, newUp);
    cross = normalize(cross_product(up, newUp));

    return(rotate_from_axis(angle, cross) * matrix);
}

This code works well when the matrix stack has only this matrix, but if i push a previous matrix rotation (rotating of course front and up vectors) it fails.

What's my fault?

解决方案

Why always those complicated "I solve for an inverse rotation and multiply that onto the modelview" billboard/impostor solutions, when there's a much simpler method?

Let M be the modelview matrix from which a billboard matrix is to be determined. The matrix is a 4×4 real valued type. The upper left 3×3 defines rotation and scaling. For a billboard this part is to be identity.

So by replacing the upper left part of the current modelview matrix with identity, and keeping the rest as is i.e.

 1  0  0 tx
 0  1  0 ty
 0  0  1 tz
wx wy wz ww

and using that matrix for further transformations you get exactly the desired effect. If there was a scaling applied, replace the upper left identity with a scaling matrix.

这篇关于定位人物以使用OpenGL观察相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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