OpenGL ortho,透视图和frustum投影。

时间:2021-12-25 05:55:46

45I am trying to understand OpenGL projections on a single point. I am using QGLWidget for rendering context and QMatrix4x4 for projection matrix. Here is the draw function

我正在试着理解OpenGL对一个点的投影。我正在使用QGLWidget渲染上下文和QMatrix4x4的投影矩阵。这是绘制函数。

    attribute vec4 vPosition;      
        uniform mat4 projection;  
        uniform mat4 modelView; 
        void main()
        {
          gl_Position = projection* vPosition;
        }       

        void OpenGLView::Draw()
        {
           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glUseProgram(programObject);
    glViewport(0, 0, width(), height());

    qreal aspect = (qreal)800 / ((qreal)600);
    const qreal zNear = 3.0f, zFar = 7.0f, fov = 45.0f;

    QMatrix4x4 projection;
    projection.setToIdentity();
    projection.ortho(-1.0f,1.0f,-1.0f,1.0f,-20.0f,20.0f);
   // projection.frustum(-1.0f,1.0f,-1.0f,1.0f,-20.0f,20.0f);
   // projection.perspective(fov,aspect,zNear, zFar);

   position.setToIdentity();
   position.translate(0.0f, 0.0f, -5.0f);
   position.rotate(0,0,0, 0);

    QMatrix4x4 mvpMatrix =   projection * position;

    for (int r=0; r<4; r++)
        for (int c=0; c<4; c++)
            tempMat[r][c] = mvpMatrix.constData()[ r*4 + c ];

    glUniformMatrix4fv(projection, 1, GL_FALSE, (float*)&tempMat[0][0]);

    //Draw point at 0,0
    GLfloat f_RefPoint[2];
    glUniform4f(color,1, 0,1,1);
    glPointSize(15);
    f_RefPoint[0] = 0;
    f_RefPoint[1] = 0;
    glEnableVertexAttribArray(vertexLoc);
    glVertexAttribPointer(vertexLoc, 2, GL_FLOAT, 0, 0, f_RefPoint);
    glDrawArrays (GL_POINTS, 0, 1);            
        }

Observations:

观察:

1) projection.ortho: the point rendered on the window and translating the point with different z-axis value has no effect

1)投影。在窗口上渲染的点和用不同的z轴值来翻译点没有效果。

2) projection.frustum: the point is drawn on the windown only the point is translated as translate(0.0f, 0.0f, -20.0f)

2)投影。frustum:在windown上绘制的点只被翻译为翻译(0.0f, 0.0f, -20.0f)

3) projection.perspective: the point is never rendered on the screen.

3)投影。透视:这个点永远不会在屏幕上呈现。

Could someone help me understand this behaviour?

有人能帮助我理解这种行为吗?

1 个解决方案

#1


2  

  1. The ortho projection works this way. I suggest you search for some images or some videos about the differences between different projections.
  2. 正投影是这样的。我建议你搜索一些图片或者一些关于不同投影之间差异的视频。
  3. I don't know how you see a point translation in Z coordinate but if you would have a square it would become smaller by translating it further away (with ortho it would stay the same). There is an issue here as you use -20.0f for zNear while this value should be positive. The values inserted into this method should in most cases be generated with field of view, aspect ratio... Anyway you will not be able to see anything closer then zNear and anything further then zFar.
  4. 我不知道如何在Z坐标中看到一个点平移但如果你有一个平方,它会变得更小,通过把它平移到更远的地方(用正态,它会保持不变)。这里有一个问题,当你使用-20.0f的时候,这个值应该是正的。在大多数情况下,插入到该方法中的值应该由视场、纵横比来生成。无论如何,你将无法看到任何更近的东西,然后再进一步。
  5. This is the same as frustum but already takes parameters as field of view, aspect ratio. The reason you do not see anything is your zNear is at 3.0f and the point is .0f length away. By translating the point you will be able to see it but try translating it by anything from 3.0f to 7.0f (3.0f is your zNear and 7.0f is your zFar). Alternatives are increasing zFar or translating the projection matrix backwards. Or mostly in your case I suggest adding some "look at" system on the projection matrix as it will give you some easy-to-use tools to manipulate your "camera", in most cases you can set a point you are looking from, a point you are looking at and up vector.
  6. 这与frustum相同,但已经将参数作为视图的视场,宽比。你看不到任何东西的原因是你的zNear在3。0f,点是。0f长度。通过翻译,你将能够看到它,但是尝试将它翻译成任何东西,从3.0f到7。0f(3。0f是你的zNear, 7。0f是你的zFar)。替代方法是增加zFar或将投影矩阵向后平移。或者大多数情况下,我建议在投影矩阵上添加一些“观察”系统,因为它会给你一些易于使用的工具来操作你的“相机”,在大多数情况下,你可以设置一个你正在观察的点,一个你正在观察的点和向上的矢量。

#1


2  

  1. The ortho projection works this way. I suggest you search for some images or some videos about the differences between different projections.
  2. 正投影是这样的。我建议你搜索一些图片或者一些关于不同投影之间差异的视频。
  3. I don't know how you see a point translation in Z coordinate but if you would have a square it would become smaller by translating it further away (with ortho it would stay the same). There is an issue here as you use -20.0f for zNear while this value should be positive. The values inserted into this method should in most cases be generated with field of view, aspect ratio... Anyway you will not be able to see anything closer then zNear and anything further then zFar.
  4. 我不知道如何在Z坐标中看到一个点平移但如果你有一个平方,它会变得更小,通过把它平移到更远的地方(用正态,它会保持不变)。这里有一个问题,当你使用-20.0f的时候,这个值应该是正的。在大多数情况下,插入到该方法中的值应该由视场、纵横比来生成。无论如何,你将无法看到任何更近的东西,然后再进一步。
  5. This is the same as frustum but already takes parameters as field of view, aspect ratio. The reason you do not see anything is your zNear is at 3.0f and the point is .0f length away. By translating the point you will be able to see it but try translating it by anything from 3.0f to 7.0f (3.0f is your zNear and 7.0f is your zFar). Alternatives are increasing zFar or translating the projection matrix backwards. Or mostly in your case I suggest adding some "look at" system on the projection matrix as it will give you some easy-to-use tools to manipulate your "camera", in most cases you can set a point you are looking from, a point you are looking at and up vector.
  6. 这与frustum相同,但已经将参数作为视图的视场,宽比。你看不到任何东西的原因是你的zNear在3。0f,点是。0f长度。通过翻译,你将能够看到它,但是尝试将它翻译成任何东西,从3.0f到7。0f(3。0f是你的zNear, 7。0f是你的zFar)。替代方法是增加zFar或将投影矩阵向后平移。或者大多数情况下,我建议在投影矩阵上添加一些“观察”系统,因为它会给你一些易于使用的工具来操作你的“相机”,在大多数情况下,你可以设置一个你正在观察的点,一个你正在观察的点和向上的矢量。