Qt配置OpenGL相机踩的坑

项目根据LearnOpenGL配置Qt的相机,更新view矩阵和project矩阵的位移向量变得很大,我设置的明明相机位置是(0,0,3),理想的位移向量刚好是相反数(0,0,-3),对应的view矩阵位置向量可以变成(0,0,1200)…离模型非常远矩阵模型也看不见:

#include "UI/RobotView.h"
#include <QtCore/QtGlobal>
#include <QtCore/QFile>
#include <QtCore/QDebug>
#include <QtGui/QMouseEvent>
#include <QtGui/QWheelEvent>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QOpenGLBuffer>
#include <QtGui/QOpenGLVertexArrayObject>
#include <QtGui/QMatrix4x4>
#include <QtGui/QVector3D>
#include <QtWidgets/QOpenGLWidget>
#include <QElapsedTimer>
#include <QtMath>#include <urdf_model/model.h>
#include <urdf_parser/urdf_parser.h>RobotView::RobotView(QWidget *parent) : QOpenGLWidget(parent),VBO(QOpenGLBuffer::VertexBuffer),model_(nullptr),firstMouse(true),cameraZoom(45.0f),cameraYaw(-90.0f),cameraPitch(0.0f),cameraPosition(0.0f, 0.0f, 3.0f),worldUp(0.0f, 1.0f, 0.0f),cameraFront(0.0f, 0.0f, -1.0f), //*cameraUp(0.0f, 1.0f, 0.0f),mouseSensitivity(0.1f)
{setFocusPolicy(Qt::StrongFocus);frameCount = 0;fps = 0.0f;fpsTimer = new QTimer(this);connect(fpsTimer, &QTimer::timeout, this, [this](){fps = frameCount;frameCount = 0;emit sendFPS(fps); // 发送帧率信号});fpsTimer->start(1000);updateTimer = new QTimer(this);connect(updateTimer, &QTimer::timeout, this, [this](){ update(); });updateTimer->start(16); // 60 FPSmodel_ = new RobotModel();viewMatrix.lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);projectionMatrix.perspective(cameraZoom, float(width()) / float(height()), 0.01f, 200.0f);if (!model_->loadFromURDF(":/assets/instrument_sim/urdf/instrument_sim.urdf")){qCritical() << "Failed to load URDF file";delete model_;}
}RobotView::~RobotView()
{delete fpsTimer;delete updateTimer;delete model_;fpsTimer = nullptr;updateTimer = nullptr;model_ = nullptr;cleanupGL();
}void RobotView::initializeGL()
{initializeOpenGLFunctions();VAO.create();VBO.create();VAO.bind();VBO.bind();if (!initShaders()){qCritical() << "Failed to initialize shaders";return;}VAO.release();VBO.release();glEnable(GL_DEPTH_TEST);
}void RobotView::resizeGL(int w, int h)
{glViewport(0, 0, w, h);
}void RobotView::paintGL()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glClearColor(0.2f, 0.3f, 0.3f, 1.0f);shaderProgram.bind();projectionMatrix.perspective(cameraZoom, float(width()) / float(height()), 0.01f, 200.0f);viewMatrix.lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);// 将矩阵传递给着色器shaderProgram.setUniformValue("lightPos", lightPos);shaderProgram.setUniformValue("viewPos", cameraPosition);shaderProgram.setUniformValue("objectColor", objectColor);shaderProgram.setUniformValue("lightColor", lightColor);shaderProgram.setUniformValue("projectionMatrix", projectionMatrix);shaderProgram.setUniformValue("viewMatrix", viewMatrix);VAO.bind();// // Draw robot modelauto &link = model_->getLinks()[1];if (link.visual){VBO.bind();VBO.allocate(link.visual->getVertices(), link.visual->getVerticesSize() * sizeof(float));modelMatrix.setToIdentity();shaderProgram.setUniformValue("modelMatrix", modelMatrix);// Draw trianglesint positionAttribute = shaderProgram.attributeLocation("aPos");shaderProgram.enableAttributeArray(positionAttribute);shaderProgram.setAttributeBuffer(positionAttribute, GL_FLOAT, 0, 3, 6 * sizeof(GLfloat));// 设置顶点属性int normalAttribute = shaderProgram.attributeLocation("aNormal");shaderProgram.enableAttributeArray(normalAttribute);shaderProgram.setAttributeBuffer(normalAttribute, GL_FLOAT, 3 * sizeof(GLfloat), 3, 6 * sizeof(GLfloat));glDrawArrays(GL_TRIANGLES, 0, link.visual->getVerticesSize() / 6);VBO.release();}VAO.release();shaderProgram.release();frameCount++;
}bool RobotView::initShaders()
{// Load vertex shaderQFile vertShaderFile(":/assets/shaders/phongShader.vert");if (!vertShaderFile.open(QIODevice::ReadOnly | QIODevice::Text)){qCritical() << "Failed to open vertex shader file:" << vertShaderFile.fileName();doneCurrent();return false;}QString vertShaderSource = vertShaderFile.readAll();vertShaderFile.close();// Load fragment shaderQFile fragShaderFile(":/assets/shaders/phongShader.frag");if (!fragShaderFile.open(QIODevice::ReadOnly | QIODevice::Text)){qCritical() << "Failed to open fragment shader file:" << fragShaderFile.fileName();return false;}QString fragShaderSource = fragShaderFile.readAll();fragShaderFile.close();// Compile shadersif (!shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderSource)){qCritical() << "Failed to compile vertex shader:" << shaderProgram.log();return false;}if (!shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderSource)){qCritical() << "Failed to compile fragment shader:" << shaderProgram.log();return false;}// Link shader programif (!shaderProgram.link()){qCritical() << "Failed to link shader program:" << shaderProgram.log();return false;}return true;
}void RobotView::mouseMoveEvent(QMouseEvent *event)
{// update Front, Right and Up Vectors using the updated Euler anglesif (event->buttons() & Qt::LeftButton){float xPos = event->x();float yPos = event->y();if (firstMouse){lastMousePosX = xPos;lastMousePosY = yPos;firstMouse = false;}float xoffset = xPos - lastMousePosX;float yoffset = lastMousePosY - yPos;lastMousePosX = xPos;lastMousePosY = yPos;xoffset *= mouseSensitivity;yoffset *= mouseSensitivity;cameraYaw += xoffset;cameraPitch += yoffset;// make sure that when pitch is out of bounds, screen doesn't get flippedif (cameraPitch > 89.0f)cameraPitch = 89.0f;if (cameraPitch < -89.0f)cameraPitch = -89.0f;QVector3D front;front.setX(cos(qDegreesToRadians(cameraYaw)) * cos(qDegreesToRadians(cameraPitch)));front.setY(sin(qDegreesToRadians(cameraPitch)));front.setZ(sin(qDegreesToRadians(cameraYaw)) * cos(qDegreesToRadians(cameraPitch)));cameraFront = front.normalized();}}void RobotView::cleanupGL()
{makeCurrent();VAO.destroy();VBO.destroy();shaderProgram.removeAllShaders();doneCurrent();
}

解决办法:

  1. 在每一次使用lookat和perspective函数前都将矩阵置为identity,根据手册,这两个函数api和glm不一样,会一直连乘之前的矩阵,所以调用这个函数api,先得吧矩阵view和project变为单位阵,防止一直连乘跑飞
    在这里插入图片描述

在这里插入图片描述

projectionMatrix.setToIdentity();
projectionMatrix.perspective(cameraZoom, float(width()) / float(height()), 0.01f, 200.0f);
viewMatrix.setToIdentity();
viewMatrix.lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);

加了以后好一点了,但是鼠标拖拽有时候移动到其他位置再拖动,模型会突然跳一下,分析了一下原因是因为鼠标的位置没有及时更新:

  1. 在构造函数加上鼠标的跟踪,修改把获取位置更新上一时刻位置放在if判断外面:

void RobotView::mouseMoveEvent(QMouseEvent *event)
{// update Front, Right and Up Vectors using the updated Euler anglesfloat xPos = event->x();float yPos = event->y();if (event->buttons() & Qt::LeftButton){if (firstMouse){lastMousePosX = xPos;lastMousePosY = yPos;firstMouse = false;}float xoffset = xPos - lastMousePosX;float yoffset = lastMousePosY - yPos;xoffset *= mouseSensitivity;yoffset *= mouseSensitivity;cameraYaw += xoffset;cameraPitch += yoffset;// make sure that when pitch is out of bounds, screen doesn't get flippedif (cameraPitch > 89.0f)cameraPitch = 89.0f;if (cameraPitch < -89.0f)cameraPitch = -89.0f;QVector3D front;front.setX(cos(qDegreesToRadians(cameraYaw)) * cos(qDegreesToRadians(cameraPitch)));front.setY(sin(qDegreesToRadians(cameraPitch)));front.setZ(sin(qDegreesToRadians(cameraYaw)) * cos(qDegreesToRadians(cameraPitch)));cameraFront = front.normalized();// viewMatrix.setToIdentity();// viewMatrix.lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);}lastMousePosX = xPos;lastMousePosY = yPos;
}

但是发现还是有问题,貌似mouseMove事件需要按键按下才触发,鼠标的位置没有得到及时更新,经过查资料,推测可能是没有开启鼠标的跟踪,在构造函数加入下面的语句就可以了

setMouseTracking(true);

调bug还是看看用户手册的好

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/72120.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++设计模式】第十六篇:迭代器模式(Iterator)

注意&#xff1a;复现代码时&#xff0c;确保 VS2022 使用 C17/20 标准以支持现代特性。 遍历聚合对象的统一方式 1. 模式定义与用途 核心思想 ​迭代器模式&#xff1a;提供一种方法顺序访问聚合对象的元素&#xff0c;而无需暴露其内部表示。关键用途&#xff1a; 1.​统一…

关于WPS的Excel点击单元格打开别的文档的两种方法的探究【为单元格添加超链接】

问题需求 目录和文件结构如下&#xff1a; E:\Dir_Level1 │ Level1.txt │ └─Dir_Level2│ Level2.txt│ master.xlsx│└─Dir_Level3Level3.txt现在要在master.xlsx点击单元格进而访问Level1.txt、Level2.txt、Level3.txt这些文件。 方法一&#xff1a;“单元格右键…

聚类中的相似矩阵和拉普拉斯矩阵

前言&#xff08;可以略过&#xff09; 最近在看的是关于聚类的论文&#xff0c;之前对聚类的步骤和相关内容不太了解&#xff0c;为了读懂论文就去学习了一下&#xff0c;这里将自己的理解记录下来。学习的不全面&#xff0c;主要是为了看懂论文&#xff0c;后续如果有涉及到聚…

前端笔记 --- vue框架

目录 基础知识 指令的修饰符 计算属性 watch侦听器的写法 Vue的生命周期 工程化开发&脚手架 VUE CLI 组件注册的方式 scoped样式冲突与原理 data 组件之间的关系和组件通信 v-model详解 sync修饰符 Dom介绍 操作HTML标签 总结 ref 和 $refs $nextTick 自…

智能双剑合璧:基于语音识别与大模型的技术沙龙笔记整理实战

智能双剑合璧&#xff1a;基于语音识别与大模型的技术沙龙笔记整理实战 ——记一次网络安全技术沙龙的高效知识沉淀 引言&#xff1a;当网络安全遇上AI生产力工具 在绿盟科技举办的"AI驱动的未来网络安全"内部技术沙龙中&#xff0c;笔者亲历了一场关于网络安全攻…

数据结构(蓝桥杯常考点)

数据结构 前言&#xff1a;这个是针对于蓝桥杯竞赛常考的数据结构内容&#xff0c;基础算法比如高精度这些会在下期给大家总结 数据结构 竞赛中&#xff0c;时间复杂度不能超过10的7次方&#xff08;1秒&#xff09;到10的8次方&#xff08;2秒&#xff09; 空间限制&#x…

使用 UNIX 命令在设计中搜索标识符:vcsfind 的入门指南

在现代软件开发和硬件设计中&#xff0c;快速准确地定位和搜索特定标识符是提高开发效率的关键。本文将介绍如何使用 UNIX 命令 vcsfind 在设计中搜索标识符&#xff0c;帮助您更高效地管理您的项目。 什么是 vcsfind&#xff1f; vcsfind 是一个强大的 UNIX 命令行工具&#x…

第56天:Web攻防-SQL注入增删改查盲注延时布尔报错有无回显错误处理审计复盘

#知识点 1、Web攻防-SQL注入-操作方法&增删改查 2、Web攻防-SQL注入-布尔&延时&报错&盲注 一、增删改查 1、功能&#xff1a;数据查询 查询&#xff1a;SELECT * FROM news where id$id 2、功能&#xff1a;新增用户&#xff0c;添加新闻等 增加&#xff1a;IN…

跳表实现学习

1.介绍 2.源码 跳表节点&#xff1a; /* ZSETs use a specialized version of Skiplists */ /*** brief 定义跳跃表节点的数据结构。* * 该结构体用于表示跳跃表中的一个节点&#xff0c;包含元素、分数、后向指针和多层链表信息。*/ typedef struct zskiplistNode {sds ele;…

Python:正则表达式

正则表达式的基础和应用 一、正则表达式核心语法&#xff08;四大基石&#xff09; 1. ​元字符&#xff08;特殊符号&#xff09;​ ​定位符 ^&#xff1a;匹配字符串开始位置 $&#xff1a;匹配字符串结束位置 \b&#xff1a;匹配单词边界​&#xff08;如 \bword\b 匹配…

EB-Cable许可管理中的数据安全与隐私保护

在数字化时代&#xff0c;数据安全与隐私保护已成为企业关注的重中之重。作为专业的电缆管理软件&#xff0c;EB-Cable许可管理不仅在功能丰富和操作便捷方面表现出色&#xff0c;更在数据安全与隐私保护方面为用户提供了坚实的保障。本文将详细介绍EB-Cable许可管理在数据安全…

串口通信函数汇总-ing

谢谢各位佬的阅读&#xff0c;本文是我自己的理解&#xff0c;如果您发现错误&#xff0c;麻烦请您指出&#xff0c;谢谢 首先谈谈我自己对于串口的理解&#xff0c;随便拿一个嵌入式的板子&#xff0c;它上面有两个引脚&#xff0c;一个是rx&#xff0c;一个是tx&#xff0c;r…

如何用HTML5 Canvas实现电子签名功能✍️

&#x1f916; 作者简介&#xff1a;水煮白菜王&#xff0c;一位前端劝退师 &#x1f47b; &#x1f440; 文章专栏&#xff1a; 前端专栏 &#xff0c;记录一下平时在博客写作中&#xff0c;总结出的一些开发技巧和知识归纳总结✍。 感谢支持&#x1f495;&#x1f495;&#…

大模型开源的工具包有哪些特殊符号可以使用;SEP 是什么

大模型开源的工具包有哪些特殊符号可以使用 目录 大模型开源的工具包有哪些特殊符号可以使用自定义特殊token:special_tokens=True一、**对话轮次分隔符(必选)**二、**系统提示标记(提升指令理解)**三、**中文特色分隔符(贴合书写习惯)**四、**开源模型专属符号(按文档…

控制系统分类

文章目录 定义与特点1. 自治系统&#xff08;Autonomous System&#xff09;与非自治系统&#xff08;Non-Autonomous System&#xff09;自治系统非自治系统 2. 线性系统&#xff08;Linear System&#xff09;与非线性系统&#xff08;Nonlinear System&#xff09;线性系统非…

通过 ElasticSearch的Python API和`curl` 命令获取Elasticsearch 所有索引名称

导言 在大数据管理和实时搜索场景中&#xff0c;Elasticsearch 是一款不可或缺的工具。无论是开发调试、数据维护&#xff0c;还是系统监控&#xff0c;快速列出所有索引名称都是一个高频需求。本文将手把手教你如何通过 Python 客户端连接 Elasticsearch&#xff0c;并用两种方…

2024年广州市智能网联汽车创新实践年度报告

政策法规方面&#xff0c;积极推进《广州市智能网联汽车创新发展条例》的制定和发布&#xff0c;不断完善法规标准体系&#xff0c;为产业创新发展营造良好政策环境&#xff1b;技术创新方面&#xff0c;企业加大研发投入&#xff0c;在自动驾驶算法、车联网安全等关键领域取得…

计算机操作系统(一) 什么是操作系统

计算机操作系统&#xff08;一&#xff09; 什么是操作系统 前言一、什么是操作系统二、操作系统的作用三、推动操作系统发展的主要动力总结&#xff08;核心概念速记&#xff09;&#xff1a; 前言 当你打开电脑、点击应用、播放音乐时&#xff0c;是谁在背后默默协调这一切&…

韦伯望远镜的拉格朗日点计算推导过程,包含MATLAB和python运动轨迹仿真代码

研究过程 起源与提出&#xff1a;1687 年牛顿提出 “三体问题”&#xff0c;旨在研究三个可视为质点的天体在相互之间万有引力作用下的运动规律&#xff0c;但因运动方程过于复杂&#xff0c;难以得到完全解。欧拉的贡献1&#xff1a;1767 年&#xff0c;瑞士数学家莱昂哈德・…

Gateway:网关路由与登录鉴权

在微服务架构中&#xff0c;用户登录和身份校验的处理方式确实与单体应用有所不同。在单体架构中&#xff0c;一旦用户通过身份验证&#xff0c;其会话信息可以在整个应用范围内共享&#xff0c;所有模块都能访问到用户信息。然而&#xff0c;在微服务架构下&#xff0c;每个服…