【问题标题】:opengl oblique projectionopengl斜投影
【发布时间】:2011-08-31 07:58:59
【问题描述】:

我想在 OpenGL 中创建一个倾斜(骑士)投影。我知道默认不支持此操作,而是我需要一个剪切矩阵,然后进行正交投影。

你能告诉我我必须做哪些 OpenGL 步骤/功能吗?

【问题讨论】:

    标签: c++ opengl matrix projection


    【解决方案1】:

    由于所谓的斜投影是通过将投影平面从右侧旋转一定角度获得的,这只会产生沿旋转轴的加长图像,因此我认为只需缩放法线正交投影就足够了沿该轴,系数为\csc\theta。这种说法可以通过三角等式来证明,例如,\sin\theta+\cos\theta \cot\theta=\csc\theta。如果您的倾斜投影由 \theta\phi 指定,就像卢克的回答一样,轴角可以根据这两个角度计算为三角学练习,例如 \arctan(\tan\theta\sqrt(1+\cot^2\phi))

    【讨论】:

      【解决方案2】:

      我之前没有使用过倾斜/骑士投影,但以下应该让您了解如何进行:

      创建一个 4x4 剪切矩阵,

      H(θ, Φ) = | 1, 0, -cot(θ), 0 |
                | 0, 1, -cot(Φ), 0 |
                | 0, 0,    1,    0 |
                | 0, 0,    0,    1 |
      

      θ 是 X 中的切变,Φ 是 Y 中的切变,Z 不受影响。

      (ref: slide 11 of http://www.cs.unm.edu/~angel/CS433/LECTURES/CS433_17.pdf)

      乘以你的正交投影,

      | 2/(r-l),     0,       0,    -(r+l)/(r-l) |
      |    0,    2/(t-b),     0,    -(t+b)/(t-b) |
      |    0,        0,    2/(f-n), -(f+n)/(f-n) |
      |    0,        0,       0,          1      |
      

      (由左、右、下、上、近、远描述)

      (参考:http://en.wikipedia.org/wiki/Orthographic_projection_%28geometry%29

      然后,OpenGL 允许您通过函数 glLoadMatrixf() 直接上传这个矩阵(作为 16 个浮点数的数组):

      GLfloat proj[16] = { ... };
      glMatrixMode(GL_PROJECTION);  // Make sure we're modifying the *projection* matrix
      glLoadMatrixf(proj);          // Load the projection
      

      要更深入地了解 OpenGL 中的查看和转换是如何工作的,我建议您参考Chapter 3 of the OpenGL "Red Book"。他们在那里使用glOrtho() 创建和应用正交投影。

      编辑:

      正如 datenwolf 指出的那样,请记住 OpenGL 中的矩阵元素是按列主要顺序指定的。

      【讨论】:

        【解决方案3】:

        OpenGL 允许您指定任意投影矩阵。自己构建所需的投影矩阵,将传入的顶点映射到每个维度的 -1 到 1 范围内,然后使用

        GLfloat custrom_projection[16] = {
             ...
        };
        glMatrixMode(GL_PROJECTION);
        glLoadMatrix(custom_projection);
        

        OpenGL 按列主要顺序索引矩阵元素,即

        0   4   8   12
        1   5   9   13
        2   6  10   14
        3   7  11   15
        

        【讨论】:

          猜你喜欢
          • 2016-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-17
          • 2013-11-26
          • 1970-01-01
          相关资源
          最近更新 更多