如何使用透视投影矩阵设置特定的观察点?

3

目前我正在学习3D渲染理论,使用的是《现代3D图形编程》一书,并且现在卡在了第四章复习中的一个“进一步研究”活动上,具体来说是最后一个活动。

第三个活动已经在这个问题中得到解答,我没有任何问题地理解了它。然而,这个最后一个活动要求我这次只使用矩阵来完成所有操作。

我有一个部分工作的解决方案,但对我来说感觉很麻烦,可能不是正确的方法。

我的第三个问题的解决方案涉及将3D向量E的x、y和z分量振荡在任意范围内,并产生一个缩放的立方体(从左下角开始增长,根据OpenGL原点)。我想再次使用矩阵来完成这个过程,它看起来像这样:

  ss1

  ss2

但是我使用矩阵得到的结果如下(忽略背景颜色的变化):

  ss3

  ss4

现在看代码...

矩阵是一个名为theMatrix的float[16],表示一个4x4矩阵,数据按列主序写入,除以下元素外,所有元素都初始化为零:

float fFrustumScale = 1.0f; float fzNear = 1.0f; float fzFar = 3.0f;

theMatrix[0] = fFrustumScale;
theMatrix[5] = fFrustumScale;
theMatrix[10] = (fzFar + fzNear) / (fzNear - fzFar);
theMatrix[14] = (2 * fzFar * fzNear) / (fzNear - fzFar);
theMatrix[11] = -1.0f;

然后,剩下的代码保持不变,就像matrixPerspective教程中的那样,直到我们到达void display()函数:

//Hacked-up variables pretending to be a single vector (E)
float x = 0.0f, y = 0.0f, z = -1.0f;

//variables used for the oscilating zoom-in-out
int counter = 0;
float increment = -0.005f;
int steps = 250;

void display()
{
    glClearColor(0.15f, 0.15f, 0.2f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(theProgram);

    //Oscillating values
    while (counter <= steps)
    {
        x += increment;
        y += increment;
        z += increment;

        counter++;

        if (counter >= steps)
        {
            counter = 0;
            increment *= -1.0f;
        }
        break;
    }

    //Introduce the new data to the array before sending as a 4x4 matrix to the shader
    theMatrix[0] = -x * -z;
    theMatrix[5] = -y * -z;

    //Update the matrix with the new values after processing with E
    glUniformMatrix4fv(perspectiveMatrixUniform, 1, GL_FALSE, theMatrix);

    /*
    cube rendering code ommited for simplification
    */

glutSwapBuffers();
glutPostRedisplay();
}

这里是使用矩阵的顶点着色器代码:

#version 330

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;

smooth out vec4 theColor;

uniform vec2 offset;
uniform mat4 perspectiveMatrix;

void main()
{
        vec4 cameraPos = position + vec4(offset.x, offset.y, 0.0, 0.0);

        gl_Position = perspectiveMatrix * cameraPos;
        theColor = color;
} 

我在做什么错误的事情,或者我在混淆什么?感谢您花时间阅读所有这些。

编辑您的问题并正确添加图片。 - 101010
1个回答

2

在OpenGL中,有三个重要的矩阵需要注意:

  • 模型矩阵D:将顶点从对象的局部坐标系映射到世界坐标系。

  • 视图矩阵V:将顶点从世界坐标系映射到相机坐标系。

  • 投影矩阵P:将(或更适合地说是“投影”)相机空间中的顶点映射到屏幕上。

  • 将模型和视图矩阵相乘得到所谓的模型视图矩阵M,它将顶点从对象的局部坐标映射到相机坐标系。

    enter image description here

    Model-view Matrix

    修改模型视图矩阵的特定元素会导致相机的某些仿射变换。

    例如,最右侧列的3个矩阵元素enter image description here用于平移变换。对角线元素enter image description here用于缩放变换。适当修改子矩阵的元素

    enter image description here

    用于相机轴XYZ旋转变换


以下是C++代码中的上述变换,非常简单:

  void translate(GLfloat const dx, GLfloat const dy, GLfloat dz, GLfloat *M)
  {
    M[12] = dx; M[13] = dy; M[14] = dz;
  }

  void scale(GLfloat const sx, GLfloat sy, GLfloat sz, GLfloat *M)  
  {
    M[0] = sx; M[5] = sy; M[10] = sz;
  }

  void rotateX(GLfloat const radians, GLfloat *M)  
  {
    M[5] = std::cosf(radians); M[6]  = -std::sinf(radians);
    M[9] = -M[6];              M[10] = M[5];
  }

  void rotateY(GLfloat const radians, GLfloat *M)  
  {
    M[0] = std::cosf(radians); M[2]  = std::sinf(radians);
    M[8] = -M[2];              M[10] = M[0];
  }

  void rotateZ(GLfloat const radians, GLfloat *M)  
  {
    M[0] = std::cosf(radians); M[1] = std::sinf(radians);
    M[4] = -M[1];              M[5] = M[0];
  }

现在您需要定义投影矩阵P
  • 正交投影:

// These paramaters are lens properties.
// The "near" and "far" create the Depth of Field.
// The "left", "right", "bottom" and "top" represent the rectangle formed
// by the near area, this rectangle will also be the size of the visible area.
GLfloat near   = 0.001, far   = 100.0;
GLfloat left   = 0.0,   right = 320.0; 
GLfloat bottom = 480.0, top   = 0.0;

// First Column
P[0] = 2.0 / (right - left);
P[1] = 0.0;
P[2] = 0.0;
P[3] = 0.0;

// Second Column
P[4] = 0.0;
P[5] = 2.0 / (top - bottom);
P[6] = 0.0;
P[7] = 0.0;

// Third Column
P[8] = 0.0;
P[9] = 0.0;
P[10] = -2.0 / (far - near);
P[11] = 0.0;

// Fourth Column
P[12] = -(right + left) / (right - left);
P[13] = -(top + bottom) / (top - bottom);
P[14] = -(far + near) / (far - near);
P[15] = 1;
  • 透视投影:

// These paramaters are about lens properties.
// The "near" and "far" create the Depth of Field.
// The "angleOfView", as the name suggests, is the angle of view.
// The "aspectRatio" is the cool thing about this matrix. OpenGL doesn't
// has any information about the screen you are rendering for. So the
// results could seem stretched. But this variable puts the thing into the
// right path. The aspect ratio is your device screen (or desired area) width
// divided by its height. This will give you a number < 1.0 the the area 
// has more vertical space and a number > 1.0 is the area has more horizontal 
// space. Aspect Ratio of 1.0 represents a square area.
GLfloat near        = 0.001;
GLfloat far         = 100.0;
GLfloat angleOfView = 0.25 * 3.1415;
GLfloat aspectRatio = 0.75;

// Some calculus before the formula.
GLfloat size   =  near * std::tanf(0.5 * angleOfView); 
GLfloat left   = -size
GLfloat right  =  size;
GLfloat bottom = -size / aspectRatio;
GLfloat top    =  size / aspectRatio;

// First Column
P[0] = 2.0 * near / (right - left);
P[1] = 0.0;
P[2] = 0.0;
P[3] = 0.0;

// Second Column
P[4] = 0.0;
P[5] = 2.0 * near / (top - bottom);
P[6] = 0.0;
P[7] = 0.0;

// Third Column
P[8]  = (right + left) / (right - left);
P[9]  = (top + bottom) / (top - bottom);
P[10] = -(far + near) / (far - near);
P[11] = -1.0;

// Fourth Column
P[12] = 0.0;
P[13] = 0.0;
P[14] = -(2.0 * far * near) / (far - near);
P[15] = 0.0;

然后你的着色器将变成:
#version 330

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;

smooth out vec4 theColor;

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

void main()
{
  gl_Position = projectionMatrix * modelViewMatrix * position;
  theColor    = color;
} 

Bibliography:

http://blog.db-in.com/cameras-on-opengl-es-2-x/

http://www.songho.ca/opengl/gl_transform.html



非常感谢!从我的理解来看,我试图使用一个矩阵作为模型和视图矩阵,有了这个想法,现在代码可以正常工作了,再次感谢! - rlam12
@rlam12 很高兴能帮忙。顺便说一下,如果您觉得这个答案有帮助的话,可以将您的问题标记为已解决 :)。 - 101010

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接