计算gluLookAt,glOrtho和glFrustum [英] Calculating gluLookAt, glOrtho and glFrustum

查看:576
本文介绍了计算gluLookAt,glOrtho和glFrustum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作分配来绘制线框GLUT标准对象。这看起来很简单,但我们被告知,我们不能用gluAtLook(),glOrtho(),glFrustrum但我们必须用glTranslate(),glScale()和glRotate。如何你的项目的对象,而无需在glMatrixMode(GL_PROJECTION)使用这些功能?

这是我到目前为止有:

 的#includestdafx.h中
#包括<的iostream>
#包括< cstdlib>
#包括< CMATH>
#包括<文件math.h> //为SQRT()
#包括< glut.h>


无效的init(无效)
{
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_FLAT);
}

无效的显示(无效)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);
    glLoadIdentity(); / *清除矩阵* /
    / *视图变换* /
    的glTranslatef(0.0,0.0,-5.0);
    glScalef(1.0,2.0,1.0); / *模型变换* /
    glutWireCube(1.0);
    glFlush();

}

无效重塑(INT W,INT高)
{
    glViewport(0,0,(GLsizei)瓦特,(GLsizei)H);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);
    //glTranslatef(0.0,0.0,-5.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

INT主(INT ARGC,字符** argv的)
{
    glutInit(安培; ARGC,ARGV);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow(的argv [0]);
    在里面();
    glutDisplayFunc(显示);
    glutReshapeFunc(重塑);
    glutMainLoop();
    返回0;
}
 

是否有人可以帮助呢?

解决方案
  

我们被告知我们不能用gluAtLook(),glOrtho(),glFrustrum但我们必须用glTranslate(),glScale()和glRotate

gluLookAt 设置了世界,查看空间变换, glOrtho 确实以正射投影空间变换和 glFrustum 不以透视投影空间变换。当你说你的导师已经不允许使用它,这显然意味着,目的是要了解这些功能如何工作摆在首位。

目前是该教给你,在互联网上的资源。 这里有一个通过一个著名的加州大学伯克利分校教授拉维Ramamoorthi博士。 SongHo有好文章这将帮助你做同样的事情。


我可以证明在2D的简单情况。说,我们已经有对象定义的世界(为简单起见,我们取点P);我们希望相机位于(3,3),其X和Y轴指向相反的方向世界的X和Y轴。为简单起见,我们假定两个框架具有相同的缩放因子,即1个单位在X和Y两个方向上测量为两个系统中的相同的距离(数量级)。因此,两帧仅相差方向和原点位置(W <子> 0 和V <子> 0 的符号表示它们)。

我们需要派生中号<子>世界 - >查看即它映射点世界空间,查看空间矩阵。这就是现在的德precated gluLookAt 函数计算和乘用GL_MODELVIEW矩阵堆栈。该矩阵将用于获取世界从相机的视点的图。

我们知道,中号<子>世界 - >查看 = T <子>查看 - >全球。它映射框架A的分帧B的矩阵也将是其将B的帧分成A的帧矩阵。推导是这样的

在世界上的点P有(1,2)= P 是W 为坐标,我们有效地找到一个矩阵,当与P 是W 乘以会给P <子> v 即在图幅相同的点的坐标。该点被写成因为二维点的均匀延伸将是一个三维点一个三维点;齐次坐标会是1,因为它是一个点;它当时的载体,它会是0。

第一步是旋转;由-180°旋转视图的框架(右撇子系统,+已经旋转是逆时针);现在的轴沿着相同的方向这两个帧。我们已经解决原点差,这是通过翻译完成,这是第2步乘以两者都将给出所需的基质。请注意,每个步骤所转换的视图的框架更接近成为全球框架的后乘的。还有,每个转型是基于当前的本地帧我们的,而不是基于全球首发(世界)的框架上。

同样的想法可以扩展到三维也与一些更省力。在上面的推导我所需要的只是旋转矩阵,平移矩阵和矩阵乘法;没有 gluLookat .The联系我给了你应该在计算同样为3D的帮助。投影矩阵推导是较为复杂。但是,你仍然可以实现的结果,而无需使用 glOrtho ;链接我给上面的化学式为为最终矩阵;您可以使用构成一个矩阵,并乘以到GL_PROJECTION矩阵堆栈。

注意:上面的推导假设列向量,从而变换矩阵(如旋转)和乘法顺序是基于完成。如果假定行向量约定转,然后所有的矩阵和反向multplication的顺序,因为

  

(AB)^ T = B ^ T A ^ T

I am working on an assignment to draw wire frame GLUT standard objects. This seems simple but we are told we cannot use gluAtLook(), glOrtho(), glFrustrum but we must use glTranslate(), glScale() and glRotate. How to you project the object without using these functions in glMatrixMode(GL_PROJECTION)?

This is what I have so far:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <math.h> // for sqrt()
#include <glut.h>


void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glLoadIdentity();             /* clear the matrix */
    /* viewing transformation  */
    glTranslatef(0.0, 0.0, -5.0);
    glScalef(1.0, 2.0, 1.0);      /* modeling transformation */
    glutWireCube(1.0);
    glFlush();

}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    //glTranslatef(0.0, 0.0, -5.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Can someone please help with this?

解决方案

we are told we cannot use gluAtLook(), glOrtho(), glFrustrum but we must use glTranslate(), glScale() and glRotate

gluLookAt sets up the world to view space transformation, glOrtho does view to orthographic projection space transformation and glFrustum does view to perspective projection space transformation. When you say your tutor hasn't allowed to use it, it clearly means that the intention is to understand how these functions work in the first place.

There're many resources on the internet which teach you that. Here's one by a renowned UC Berkeley professor Dr. Ravi Ramamoorthi. SongHo has good articles which will help you do the same thing.


I can demonstrate a simple case in 2D. Say we've a world defined with objects (for simplicity we take a point P); we want the camera to be at (3, 3) with its X and Y axes pointing in directions opposite to world's X and Y axes. For simplicity we'll assume both frames have the same scaling factor i.e. 1 unit in both X and Y directions measure the same distance (magnitude) for both systems. So the two frames differ only by orientation and origin location (W0 and V0 are the symbols denoting them).

We need to derive Mworld->view i.e. the matrix which maps points in world space to view space. This is what the now-deprecated gluLookAt function calculates and multiplies with GL_MODELVIEW matrix stack. This matrix will be used to get a view of the world from the camera's viewpoint.

We know that Mworld->view = Tview->world. The matrix which maps points of frame A to frame B will also be the matrix which transforms B's frame into A's frame. The derivation goes like this

The point P in world has (1, 2) = Pw as coordinates, we're effectively finding a matrix, which when multiplied with Pw will give Pv i.e. the same point's coordinates in view frame. The point is written as a 3D point since homogeneous extension of a 2D point would be a 3D point; the homogeneous coordinate would be 1 since it's a point; had it been a vector, it'd be 0.

Step one is rotation; rotating view's frame by -180° (right-handed system where +ve rotation is counter-clockwise); now the axes are along the same direction for both frames. We've to tackle the origin difference, this is done by translation, which is step 2. Multiplying both will give the required matrix. Note that each step transforms the view's frame more closer into world's frame by post-multiplying. Also each transformation is based on that current local frame we're in and not based on the starting global (world) frame.

The same idea can be extended to 3D too, with some more effort. In the above derivation all I needed were just rotation matrix, translation matrix and matrix multiplication; no gluLookat.The links I gave you should help in calculating the same for 3D. The projection matrix derivation is a bit more involved. However, you can still achieve the result without using glOrtho; the links I gave above has the formula for the final matrix; you can compose a matrix using that and multiply that to the GL_PROJECTION matrix stack.

Note: The above derivation assumes column vectors and thus transformation matrices (like rotation) and multiplication order are done based on that. If you assume row vector convention then transpose all the matrices and reverse the order of multplication since

(AB)^T = B^T A^T

这篇关于计算gluLookAt,glOrtho和glFrustum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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