前言:
本文的代码是 LearnOpenGL 中对应代码,这里提供学习,大家喜欢的可去官方网站去看看:
LearnOpenGL-CNhttps://learnopengl-cn.readthedocs.io/zh/latest/ 本章简单记录,使用旋转、平移、缩放。矩阵相关计算是基础东西,记得以前还问老师,学习高代有啥用。哈哈,一切都会用的。
缩放:
基本的矩阵变换:
这样,对应的x y z 对应放大对应倍数。
平移
基本的矩阵变换:
这样就是平移对应位置了。
旋转
沿x轴旋转:
沿y轴旋转:
沿z轴旋转:
任意轴:
矩阵的组合
我们有一个顶点(x, y,z), 我们希望将其缩放2倍, 然后用位移(1, 2, 3)来平移它。
这里注意:矩阵乘法是不可交换的。我们可以记录方向是←方向添加矩阵就好就好,第一缩放,第二,在左边放置位移。
举例说明:
正常状态:
我们先缩放,然后向x移动0.4
matrix.translate(0.4f,0.0,0.0);
matrix.scale(0.5);
结果图:
我们先向x移动0.4,然后缩放。
matrix.scale(0.5);
matrix.translate(0.4f,0.0,0.0);
我们发现,效果是不一样的。包括你使用旋转,代码使用的顺序与逻辑是相反过来的。
然后我们在加上旋转。
先缩放,在平移,在旋转。
matrix.rotate(45,0,0,1);
matrix.translate(0.4f,0.0,0.0);
matrix.scale(0.5);
先缩放,在旋转,在平移 。
matrix.translate(0.4f,0.0,0.0);
matrix.rotate(45,0,0,1);
matrix.scale(0.5);
本例中是采用qt类中的写法。书中采用glm库来对矩阵操作。
glm::mat4 trans;
trans = glm::rotate(trans, 90.0f, glm::vec3(0.0, 0.0, 1.0));
trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5));
道理都是一样的。
代码部分:
顶点着色器写法:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
uniform mat4 transform;
void main()
{
gl_Position = transform*vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}
片段着色器:
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
// texture samplers
uniform sampler2D texture1;
uniform sampler2D texture2;
void main()
{
// linearly interpolate between both textures (80% container, 20% awesomeface)
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
}
主要代码:
#include "bkqopenglw.h"
#include<iostream>
#include <QDebug>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
};
BKQOpenglW::BKQOpenglW(QWidget *parent) : QOpenGLWidget(parent)
{
m_Shape = Shape::None;
setFocusPolicy(Qt::StrongFocus); //强制聚焦 这样时刻可以监视一些东西
}
BKQOpenglW::~BKQOpenglW()
{
makeCurrent();
glDeleteVertexArrays(1,&VAO);
glDeleteBuffers(1,&VBO);
doneCurrent();
}
void BKQOpenglW::drawShapes(BKQOpenglW::Shape shape)
{
m_Shape = shape;
update();
}
void BKQOpenglW::setWireFrame(bool b)
{
makeCurrent();
if(b)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
update();
doneCurrent();
}
void BKQOpenglW::initializeGL()
{
initializeOpenGLFunctions();
// shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex,vertexShaderSource);
// shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment,fragmentShaderSource);
shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shader/shader.vs");
shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shader/shader.fs");
shaderProgram.link();
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//绑定ebo
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
//绑定纹理
pTexture = new QOpenGLTexture(QImage(":/images/images/brickwall.jpg").mirrored());
pTexture2 = new QOpenGLTexture(QImage(":/images/images/awesomeface.png").mirrored());
shaderProgram.bind();
shaderProgram.setUniformValue("texture1",0);
shaderProgram.setUniformValue("texture2",1);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
}
void BKQOpenglW::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
}
void BKQOpenglW::paintGL()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
QMatrix4x4 matrix;
matrix.setToIdentity();
matrix.translate(0.4f,0.0,0.0);
matrix.rotate(45,0,0,1);
matrix.scale(0.5);
shaderProgram.bind();
glBindVertexArray(VAO);
switch (m_Shape) {
case Triangle:
glDrawArrays(GL_TRIANGLES,0,3);
break;
case Rect:
pTexture->bind(0);
pTexture2->bind(1);
shaderProgram.setUniformValue("transform",matrix);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
break;
default:
break;
}
}