cube旋转立方体(Qt-OpenGL)

更新时间:2023-06-04 13:41:29 阅读: 评论:0

急诊有妇科吗cube旋转⽴⽅体(Qt-OpenGL)
cube旋转⽴⽅体(Qt-OpenGL)
opengl坐标系
按照惯例,OpenGL是⼀个右⼿坐标系。简单来说,就是正x轴在你的右⼿边,正y轴朝上,⽽正z轴是朝向后⽅的。想象你的屏幕处于三个轴的中⼼,则正z轴穿过你的屏幕朝向你。坐标系画起来如下:
coordinate_systems_right_handed
为了理解为什么被称为右⼿坐标系,按如下的步骤做:
沿着正y轴⽅向伸出你的右臂,⼿指着上⽅。
⼤拇指指向右⽅。
⾷指指向上⽅。
中指向下弯曲90度。
如果你的动作正确,那么你的⼤拇指指向正x轴⽅向,⾷指指向正y轴⽅向,中指指向正z轴⽅向。如果你⽤左臂来做这些动作,你会发现z轴的⽅向是相反的。这个叫做左⼿坐标系,它被DirectX⼴泛地使⽤。注意在标准化设备坐标系中OpenGL实际上使⽤的是左⼿坐标系(投影矩阵交换了左右⼿)。
纹理坐标
主窗⼝类MainWidget
MainWidget :public QOpenGLWidget,protected QOpenGLFunctions ⿏标事件,更新四元数⽤于旋转
void MainWidget::mouPressEvent(QMouEvent *e)
{
// Save mou press position
mouPressPosition =QVector2D(e->localPos());
}
//! @brief 释放⿏标,更新四元数
void MainWidget::mouReleaEvent(QMouEvent *e)
{
// Mou relea position - mou press position
QVector2D diff =QVector2D(e->localPos())- mouPressPosition;
// Rotation axis is perpendicular to the mou position difference
// vector
//--旋转轴(⽮量) 垂直于⿏标位置差
QVector3D n =QVector3D(diff.y(), diff.x(),0.0).normalized();
// Accelerate angular speed relative to the length of the mou sweep //--⾓加速度与⿏标扫过的长度关联
马栗提取物qreal acc = diff.length()/100.0;
// Calculate new rotation axis as weighted sum
//--计算新的四元数的旋转轴⽮量为加权和
//-- 旋转轴⽮量 = 原来的旋转轴⽮量 + 新的旋转轴⽮量
rotationAxis =(rotationAxis * angularSpeed + n * acc).normalized();
// Increa angular speed
//--增加⾓速度(四元数的偏转⾓度)
angularSpeed += acc;
}
定时器事件: 减少偏转⾓,更新四元数,更新UI
//! @brief 定时触发减少偏转⾓,更新四元数,更新UI
void MainWidget::timerEvent(QTimerEvent *)
{
// Decrea angular speed (friction)
//--降低⾓速度(摩擦)
angularSpeed *=0.99;
水浒传书// Stop rotation when speed goes below threshold
//--当速度低于阈值时停⽌旋转
if(angularSpeed <0.01){
angularSpeed =0.0;
}el{
// Update rotation
//--更新旋转(四元数)
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed)* rotation;
// Request an update
//--更新UI
update();
}
}
重写函数 initializeGL()
初始化OpenGL
创建⽴⽅体引擎GeometryEngine
启动定时器事件
void MainWidget::initializeGL()
{
//--初始化OpenGL
initializeOpenGLFunctions();
//--清除颜⾊\透明度
赶字组词glClearColor(0,0,0,1);
initShaders();
initTextures();
//! [2]
/
骷髅头图片/ Enable depth buffer
//--启⽤深度缓冲
glEnable(GL_DEPTH_TEST);
// Enable back face culling
//--启⽤背⾯剔除
glEnable(GL_CULL_FACE);
//! [2]
geometries =new GeometryEngine;
// U QBasicTimer becau its faster than QTimer
timer.start(12,this);
}
重写函数 resizeGL() ,窗体尺⼨改变时刷新
void MainWidget::resizeGL(int w,int h)
{
// Calculate aspect ratio
//--计算纵横⽐
qreal aspect =qreal(w)/qreal(h ? h :1);
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
//--设置近平⾯为3.0,远平⾯为7.0,视野45度
const qreal zNear =3.0, zFar =7.0, fov =45.0;
// Ret projection
//--重置投影
projection.tToIdentity();
// Set perspective projection
//--设置透视投影
projection.perspective(fov, aspect, zNear, zFar);
}
重写函数 paintGL(),画出cube⽴⽅体,并且启⽤四元数旋转
void MainWidget::paintGL()
{
// Clear color and depth buffer
//--清除颜⾊和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/
/--将此纹理绑定到当前活动的纹理单元
texture->bind();
// Calculate model view transformation
//--计算模型视图转换 MVP
QMatrix4x4 matrix;
//--按照给定的四元数旋转
绕口令练习
// Set modelview-projection matrix
//--设置MVP矩阵
//--MVP矩阵按照四元数旋转
program.tUniformValue("mvp_matrix", projection * matrix);
// U texture unit 0 which contains cube.png
//--使⽤包含cube.png的纹理单元0
program.tUniformValue("texture",0);
// Draw cube geometry
//--画⽴⽅体⼏何
geometries->drawCubeGeometry(&program);
}
初始化纹理
/*!
* \brief 初始化纹理
*/
void MainWidget::initTextures()
{
// Load cube.png image
//--加载纹理中国体操男队
texture =new QOpenGLTexture(QImage(":/cube.png").mirrored());
// Set nearest filtering mode for texture minification
//--设置纹理缩⼩为最近滤波模式
texture->tMinificationFilter(QOpenGLTexture::Nearest);
// Set bilinear filtering mode for texture magnification公务员日记
//--设置纹理放⼤为双线性滤波模式
texture->tMagnificationFilter(QOpenGLTexture::Linear);
// Wrap texture coordinates by repeating
// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
//--重复封装纹理坐标
texture->tWrapMode(QOpenGLTexture::Repeat);
}
初始化着⾊器
/*!
* \brief 初始化着⾊器
*/
void MainWidget::initShaders()
{
// Compile vertex shader
//--源码编译顶点着⾊器
if(!program.addShaderFromSourceFile(QOpenGLShader::Vertex,":/vshader.glsl")) clo();
// Compile fragment shader
//--源码编译⽚段着⾊器
if(!program.addShaderFromSourceFile(QOpenGLShader::Fragment,":/fshader.glsl")) clo();
// Link shader pipeline
//--链接着⾊器管道
if(!program.link())
clo();
/
/ Bind shader pipeline for u
//--绑定着⾊器管道以供使⽤
if(!program.bind())
clo();
}
⽴⽅体引擎GeometryEngine
GeometryEngine :protected QOpenGLFunctions
顶点结构体

本文发布于:2023-06-04 13:41:29,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/860643.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:坐标系   纹理   速度   深度   投影   屏幕   旋转   指向
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图