【问题标题】:How to set a specific eye point using perspective view with matrices如何使用带有矩阵的透视图设置特定的视点
【发布时间】:2014-04-26 02:08:34
【问题描述】:

目前我正在通过《学习现代 3D 图形编程》一书学习 3D 渲染理论,目前正陷入第四章复习的“进一步学习”活动之一,特别是最后一个活动。

this question回答了第三个活动,我理解没有问题。然而,这最后一项活动要求我这次只使用矩阵来完成所有这些工作。

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

我对第三个问题的解决方案涉及在任意范围内振荡 3d 矢量E 的 x、y 和 z 分量,并生成一个缩小的立方体(从左下角开始增长,每个 OpenGL 原点) .我想使用矩阵再次执行此操作,它看起来像这样:

  

  

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

  

  

现在开始代码...

该矩阵是一个名为theMatrix 的浮点数[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;
} 

我做错了什么,或者我感到困惑?感谢您花时间阅读所有这些内容。

【问题讨论】:

  • 编辑您的问题并正确添加图片。

标签: c++ opengl matrix shader perspectivecamera


【解决方案1】:

在 OpenGL 中,您需要了解三个主要的矩阵:

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

  • View Matrix V:将顶点从世界坐标系映射到相机坐标系。

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

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

    更改模型视图矩阵的特定元素会导致相机的某些精细变换。

    例如,最右边一列 的3 个矩阵元素用于平移变换。对角线元素 用于缩放变换。适当地改变子矩阵的元素

    用于沿相机轴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;

那么你的shader会变成:


#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;
} 

参考书目:

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

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


【讨论】:

  • 非常感谢!据我了解,我试图使用一个矩阵作为模型和视图矩阵,考虑到这一点,代码现在可以工作了,再次感谢!
  • @rlam12 总是乐于提供帮助。顺便说一句,如果您发现答案有帮助,您可以检查您的问题是否已回答:)。
猜你喜欢
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 2023-04-02
  • 2015-04-15
相关资源
最近更新 更多