写在开头:文章是基于坐标系统 - LearnOpenGL CN 教程的学习记录,强烈建议在网站上先弄清楚原理再看此文章 。以Qt-GL窗口代替GLFW的写法,Qt库中一些类代替教程中的类,一起入坑 。
OpenGL希望在每次顶点着色器运行后,我们可见的所有顶点都为标准化设备坐标(Normalized Device Coordinate, NDC) 。也就是说,每个顶点的x,y,z坐标都应该在-1.0到1.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.0到1.0的范围内,并判断哪些顶点将会出现在屏幕上 。
- 最后,我们将裁剪坐标变换为屏幕坐标,我们将使用一个叫做视口变换(Viewport Transform)的过程 。视口变换将位于-1.0到1.0范围的坐标变换到由glViewport函数所定义的坐标范围内 。最后变换出来的坐标将会送到光栅器,将其转化为片段
写到这我感觉教程写的很明白,复制即可 。
理解两个投影:正射投影和透视投影
正射投影:
透视投影:
在b站上面看到一个教程比较直观的描述相机空间:
其他的也没啥好写的啦 。上代码:
.h
#ifndef COORDINATEWIDGET_H#define COORDINATEWIDGET_H#include #include #include #include #include #include "Shader.h"#include namespace Ui {class CoordinateWidget;}class HelloCoordinate;class CoordinateWidget : public QWidget{Q_OBJECTpublic: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 cubesstatic 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 attributeglVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);glEnableVertexAttribArray(0);// texture coord attributeglVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));glEnableVertexAttribArray(1);//执行选项//InitDepth();InitExercise();//垂直镜像mirroredm_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);//激活纹理单元0glActiveTexture(GL_TEXTURE0);m_combine_texture1->bind();glActiveTexture(GL_TEXTURE1);m_combine_texture2->bind();// render containerm_shader->use();if(m_status == Depth){// create transformationsQMatrix4x4 model; // make sure to initialize matrix to identity matrix firstQMatrix4x4 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-05 坐标系统Coordinate】下一篇:Qt-OpenGL-06 摄像机类Camare- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
