OpenGL 旋转矩阵和 ArcBall

发布于 2024-07-25 02:56:21 字数 1530 浏览 2 评论 0原文

我的任务是创建一个 OpenGL 场景,实现简单移动和 Arcball 界面等想法。 我遇到的问题是处理 NeHe 的 Arcball 类的旋转矩阵 ( http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=48)计算。

到目前为止,我拥有的是一个非常简单的太阳系(只有地球、月球和太阳),看起来很棒。 我想要的是让相机跟随用户选择的任何一颗行星(通过单击他们想要的行星),并且让他们能够使用鼠标拖动(弧形球)以固定距离围绕行星旋转。 正如我一开始所说的,NeHe 的类正在根据鼠标单击和拖动生成旋转矩阵。 我想要应用矩阵的是相机位置。 然而,当我这样做时,我的相机只是晃动,而没有绕地球旋转。 所以我猜测我要么错过了某些步骤,要么我对我想要做的事情有一个可怕的理解。

以下是我的相机类中的一些代码,需要仔细研究:

// transform is the matrix from NeHe's arcball interface
void camera::update(Matrix4fT transform) {
    glm::mat4 transform_m = glm::mat4(0.0f);

    // convert nehe's matrices to GLM matrix
    for(int i=0; i < 4; i++)
        for(int j=0; j < 4; j++)
            transform_m[i][j] = transform.M[i*4+j];

    // apply matrix to the position
    glm::vec4 pos4 = glm::vec4(this->pos, 1.0f);
    pos4 = transform_m * pos4;

    this->pos = glm::vec3(pos4);
}

void camera::apply(planet *target) {
    // called at the beginning of GLPaint
    gluLookAt(this->pos.x,this->pos.y,this->pos.z,       // cam->position
              target->pos.x,target->pos.y,target->pos.z, // moving
              this->up.x,this->up.y,this->up.z);         // (0,1,0)
}

除此之外,NeHe 的函数在正确的位置调用(在单击和拖动期间)... 所以说真的,我不知道从这里该去哪里。 我希望有人可以帮助我解决这个问题,如果您想查看整个代码库(用 C++ 编程并推送到 QTPanel 中),请给我发送电子邮件。

谢谢,

Carl Sverre(carl at Carlsverre dot com)

I have been tasked with creating a OpenGL scene implementing ideas such as simple movement, and an Arcball interface. The problem I am having is dealing with the rotation matrix which NeHe's Arcball class (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=48) computes.

What I have so far is a very simple solar system (just the earth, moon and sun) which looks great. What I want is for the camera to follow whichever planet the user selects (by clicking on the one they want), and for them to be able to rotate around the planet at a fixed distance using mouse-drag (arcball). As I said at the beginning, NeHe's class is generating a rotation matrix based on the mouse clicking and dragging. What I want to apply the matrix to is the camera position. However, when I do that my camera just wobbles without ever rotating around the planet. So I am guessing that I am either missing some step, or that I have a horrible understanding of what I am trying to do.

Here is some code from my camera class to crunch on:

// transform is the matrix from NeHe's arcball interface
void camera::update(Matrix4fT transform) {
    glm::mat4 transform_m = glm::mat4(0.0f);

    // convert nehe's matrices to GLM matrix
    for(int i=0; i < 4; i++)
        for(int j=0; j < 4; j++)
            transform_m[i][j] = transform.M[i*4+j];

    // apply matrix to the position
    glm::vec4 pos4 = glm::vec4(this->pos, 1.0f);
    pos4 = transform_m * pos4;

    this->pos = glm::vec3(pos4);
}

void camera::apply(planet *target) {
    // called at the beginning of GLPaint
    gluLookAt(this->pos.x,this->pos.y,this->pos.z,       // cam->position
              target->pos.x,target->pos.y,target->pos.z, // moving
              this->up.x,this->up.y,this->up.z);         // (0,1,0)
}

Other than that, NeHe's functions are called in the right places (during click and drag)... So really, I have no idea where to go from here. I hope someone can help me with this, and if you want to see the whole code base (its programmed in C++ and pushed into a QTPanel) just send me an email.

Thanks,

Carl Sverre (carl at carlsverre dot com)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉栀 2024-08-01 02:56:21

好吧,也许我错了,但我认为发生在你身上的是你正在围绕坐标中心旋转,而不是围绕地球旋转(这就是你想要做的)。 要纠正这个问题,您需要做的是:

  • 将您想要旋转的点(行星的中心)平移到坐标中心,应用其位置的否定平移
  • 在进行时旋转
  • 撤消之前的平移完毕。

需要理解的是,旋转是围绕坐标中心完成的,如果你想围绕不同的地方旋转,你必须首先将该点移动到坐标中心。

希望有帮助。

Well, maybe I am wrong, but what I think that is happening to you is that you are rotating around the center of coordinates and not around the planet (that it's what you want to do). To correct that what you have to do is:

  • Translate the point you want to rotate around (the center of the planet) to the center of coordinates, applying a translation of the negation of its position
  • Rotate as you are doing it
  • Undo the translation previously done.

The thing to understand is that rotations are done around the center of coordinates, and if you want to rotate around somewhere different, you must first move that point to the center of coordinates.

Hope that it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文