Qt-OpenGL-05 坐标系统Coordinate

写在开头:文章是基于坐标系统 - LearnOpenGL CN 教程的学习记录,强烈建议在网站上先弄清楚原理再看此文章。以Qt-GL窗口代替GLFW的写法,Qt库中一些类代替教程中的类,一起入坑。

OpenGL希望在每次顶点着色器运行后,我们可见的所有顶点都为标准化设备坐标(Normalized Device Coordinate, NDC)。也就是说,每个顶点的xyz坐标都应该在-1.01.0之间,超出这个坐标范围的顶点都将不可见。我们通常会自己设定一个坐标的范围,之后再在顶点着色器中将这些坐标变换为标准化设备坐标。然后将这些标准化设备坐标传入光栅器(Rasterizer),将它们变换为屏幕上的二维坐标或像素。

将坐标变换为标准化设备坐标,接着再转化为屏幕坐标的过程通常是分步进行的,也就是类似于流水线那样子。在流水线中,物体的顶点在最终转化为屏幕坐标之前还会被变换到多个坐标系统(Coordinate System)。将物体的坐标变换到几个过渡坐标系(Intermediate Coordinate System)的优点在于,在这些特定的坐标系统中,一些操作或运算更加方便和容易,这一点很快就会变得很明显。对我们来说比较重要的总共有5个不同的坐标系统:

  • 局部空间(Local Space,或者称为物体空间(Object Space))
  • 世界空间(World Space)
  • 观察空间(View Space,或者称为视觉空间(Eye Space))
  • 裁剪空间(Clip Space)
  • 屏幕空间(Screen Space)

这就是一个顶点在最终被转化为片段之前需要经历的所有不同状态。

你现在可能会对什么是坐标空间,什么是坐标系统感到非常困惑,所以我们将用一种更加通俗的方式来解释它们。下面,我们将显示一个整体的图片,之后我们会讲解每个空间的具体功能。

为了将坐标从一个坐标系变换到另一个坐标系,我们需要用到几个变换矩阵,最重要的几个分别是模型(Model)、观察(View)、投影(Projection)三个矩阵。我们的顶点坐标起始于局部空间(Local Space),在这里它称为局部坐标(Local Coordinate),它在之后会变为世界坐标(World Coordinate),观察坐标(View Coordinate),裁剪坐标(Clip Coordinate),并最后以屏幕坐标(Screen Coordinate)的形式结束

  1. 局部坐标是对象相对于局部原点的坐标,也是物体起始的坐标。
  2. 下一步是将局部坐标变换为世界空间坐标,世界空间坐标是处于一个更大的空间范围的。这些坐标相对于世界的全局原点,它们会和其它物体一起相对于世界的原点进行摆放。
  3. 接下来我们将世界坐标变换为观察空间坐标,使得每个坐标都是从摄像机或者说观察者的角度进行观察的。
  4. 坐标到达观察空间之后,我们需要将其投影到裁剪坐标。裁剪坐标会被处理至-1.0到1.0的范围内,并判断哪些顶点将会出现在屏幕上。
  5. 最后,我们将裁剪坐标变换为屏幕坐标,我们将使用一个叫做视口变换(Viewport Transform)的过程。视口变换将位于-1.0到1.0范围的坐标变换到由glViewport函数所定义的坐标范围内。最后变换出来的坐标将会送到光栅器,将其转化为片段

写到这我感觉教程写的很明白,复制即可。

 理解两个投影:正射投影和透视投影

正射投影:

透视投影:

 在b站上面看到一个教程比较直观的描述相机空间:

 其他的也没啥好写的啦。上代码:

.h

#ifndef COORDINATEWIDGET_H
#define COORDINATEWIDGET_H

#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLExtraFunctions>
#include <QDebug>
#include <QOpenGLTexture>
#include "Shader.h"
#include <QElapsedTimer>

namespace Ui {
class CoordinateWidget;
}

class HelloCoordinate;

class CoordinateWidget : public QWidget
{
    Q_OBJECT

public:
    explicit CoordinateWidget(QWidget *parent = nullptr);
    ~CoordinateWidget();

private:
    Ui::CoordinateWidget *ui;
    HelloCoordinate *m_contentWidget;
};

class HelloCoordinate : public QOpenGLWidget,protected QOpenGLExtraFunctions
{

    enum TARGET_STATUS
    {
        Depth,
        Exercise
    };

public:
    HelloCoordinate();
    ~HelloCoordinate();

private:
    void InitDepth();
    void InitExercise();

protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();
private:
    Shader *m_shader;
    QOpenGLTexture *m_combine_texture1;
    QOpenGLTexture *m_combine_texture2;
    QElapsedTimer m_time;
    TARGET_STATUS m_status;
};


#endif // COORDINATEWIDGET_H

 .cpp

#include "coordinatewidget.h"
#include "ui_coordinatewidget.h"

CoordinateWidget::CoordinateWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CoordinateWidget)
{
    ui->setupUi(this);
    m_contentWidget = new HelloCoordinate();
    ui->verticalLayout->addWidget(m_contentWidget);
}

CoordinateWidget::~CoordinateWidget()
{
    delete ui;
}

HelloCoordinate::HelloCoordinate()
{

}

HelloCoordinate::~HelloCoordinate()
{

}

void HelloCoordinate::InitDepth()
{
    m_status = Depth;
    m_shader = new Shader(":/shader/res/shaders/getting_started/6.2.coordinate_systems.vs"
    ,":/shader/res/shaders/getting_started/6.2.coordinate_systems.fs");


}

void HelloCoordinate::InitExercise()
{
    m_status = Exercise;
    m_shader = new Shader(":/shader/res/shaders/getting_started/6.3.coordinate_systems.vs"
    ,":/shader/res/shaders/getting_started/6.3.coordinate_systems.fs");
}
static GLuint VBO, VAO = 0;

// world space positions of our cubes
static QVector3D cubePositions[] = {
    QVector3D( 0.0f,  0.0f,  0.0f),
    QVector3D( 2.0f,  5.0f, -15.0f),
    QVector3D(-1.5f, -2.2f, -2.5f),
    QVector3D(-3.8f, -2.0f, -12.3f),
    QVector3D( 2.4f, -0.4f, -3.5f),
    QVector3D(-1.7f,  3.0f, -7.5f),
    QVector3D( 1.3f, -2.0f, -2.5f),
    QVector3D( 1.5f,  2.0f, -2.5f),
    QVector3D( 1.5f,  0.2f, -1.5f),
    QVector3D(-1.3f,  1.0f, -1.5f)
};

void HelloCoordinate::initializeGL()
{
    this->initializeOpenGLFunctions();
    glEnable(GL_DEPTH_TEST);

    float vertices[] = {
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
        0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };



    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    // texture coord attribute
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);


    //执行选项
    //InitDepth();
    InitExercise();

    //垂直镜像mirrored
    m_combine_texture1 = new QOpenGLTexture(QImage(":/texture/res/textures/container.jpg").mirrored());
    if(!m_combine_texture1->isCreated()){
        qDebug() << "Failed to load texture";
    }
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    m_combine_texture1->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    m_combine_texture1->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);

    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    m_combine_texture1->setMinificationFilter(QOpenGLTexture::Linear);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    m_combine_texture1->setMagnificationFilter(QOpenGLTexture::Linear);


    m_combine_texture2 = new QOpenGLTexture(QImage(":/texture/res/textures/awesomeface.png").mirrored());
    if(!m_combine_texture2->isCreated()){
        qDebug() << "Failed to load texture";
    }
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    m_combine_texture2->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    m_combine_texture2->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);

    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    m_combine_texture2->setMinificationFilter(QOpenGLTexture::Linear);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    m_combine_texture2->setMagnificationFilter(QOpenGLTexture::Linear);

    //设置纹理单元编号
    m_shader->use();
    m_shader->m_shaderProgram.setUniformValue(m_shader->m_shaderProgram.uniformLocation("texture1"), 0);
    m_shader->m_shaderProgram.setUniformValue(m_shader->m_shaderProgram.uniformLocation("texture2"), 1);

    m_time.start();
}

void HelloCoordinate::resizeGL(int w, int h)
{
    this->glViewport(0,0,w,h);
}

void HelloCoordinate::paintGL()
{
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    //激活纹理单元0
    glActiveTexture(GL_TEXTURE0);
    m_combine_texture1->bind();
    glActiveTexture(GL_TEXTURE1);
    m_combine_texture2->bind();


    // render container
    m_shader->use();

    if(m_status == Depth)
    {
        // create transformations
        QMatrix4x4 model; // make sure to initialize matrix to identity matrix first
        QMatrix4x4 view;
        QMatrix4x4 projection;

        model.rotate((float)m_time.elapsed()/10,QVector3D(0.5f, 1.0f, 0.0f));
        view.translate(QVector3D(0.0f, 0.0f, -3.0f));
        projection.perspective(45.0f,(float)width() / (float)height(), 0.1f, 100.0f);

        m_shader->m_shaderProgram.setUniformValue("model",model);
        m_shader->m_shaderProgram.setUniformValue("view",view);
        m_shader->m_shaderProgram.setUniformValue("projection",projection);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 36);
    }else
    {

        QMatrix4x4 view;
        QMatrix4x4 projection;

        view.translate(QVector3D(0.0f, 0.0f, -3.0f));
        projection.perspective(45.0f,(float)width() / (float)height(), 0.1f, 100.0f);

        for (unsigned int i = 0; i < 10; i++)
        {
            QMatrix4x4 model;
            model.translate(cubePositions[i]);
            float angle;
            if(i % 3 == 0)
            {
               angle  = (float)m_time.elapsed()/10;
            }else
            {
               angle = i * 20.0f;
            }
            model.rotate(angle, QVector3D(1.0f, 0.3f, 0.5f));
            m_shader->m_shaderProgram.setUniformValue("model",model);
            m_shader->m_shaderProgram.setUniformValue("view",view);
            m_shader->m_shaderProgram.setUniformValue("projection",projection);
            glDrawArrays(GL_TRIANGLES, 0, 36);
        }
    }
    update();
}

下一篇:Qt-OpenGL-06 摄像机类Camare

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值