Qt配置OpenGL相机踩的坑

devtools/2025/3/13 23:08:55/

项目根据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.ppmy.cn/devtools/166878.html

相关文章

Prompt engineering设计原则(一)

目录 一、清晰具体的prompt1. 使用分隔符2. 结构化的输出&#xff08;JSON&#xff09;3. 要求模型检查是否满足条件4. 提供少量案例 二、给模型时间去思考1.指定完成任务所需的步骤2. 指导模型在下结论之前找出一个自己的解法 一、清晰具体的prompt 一个合理的prompt设计决定…

大语言模型学习--向量数据库Milvus实践

Milvus是目前比较流行的开源向量数据库&#xff0c;其官网地址 Milvus 是什么&#xff1f; | Milvus 文档 1.Milvus简介 Milvus 是一种高性能、高扩展性的向量数据库。Milvus 提供强大的数据建模功能&#xff0c;能够将非结构化或多模式数据组织成结构化的 Collections。它支…

一、Jenkins简单配置(使用语言、凭证、SSH)

这里简单讲一下jenkins的使用配置。 一、登陆系统 我们访问jenkins的界面的时候&#xff0c;被要求输入管理员密码&#xff0c;密码可以通过以下方式获取。 # 查看密码&#xff0c; 需要记住这个初始密码 # 在创建角色之后&#xff0c;这个保存密码的文件就会被删除 docker …

NGINX介绍--鱼皮老师课程学习笔记

世界上最受欢迎的web服务器、高性能负载均衡器、反向代理、API网关和内容缓存 Nginx能部署网站&#xff0c;比其他服务器用更少的资源&#xff0c;同时处理更多的用户请求&#xff0c;让网站速度更快更稳定 一、安装nginx windows双击exe启动 linux系统手动编译该目录 sudo …

CTFshow 【WEB入门】信息搜集 【VIP限免】 web1-web17

CTFshow 【 WEB入门】、【VIP限免】 web1 ----源码泄露 首先第一步&#xff0c;看源代码 web2----前台JS绕过 简单点击查看不了源代码&#xff0c;可以强制查看 比如 Ctrl Shift ICtrl U或者在url前加一个view-source: view-source:http://79999ca1-7403-46da-b25b-7ba9…

机器人匹诺曹机制,真话假话平衡机制

摘要&#xff1a; 本文聚焦于机器人所采用的一种“匹诺曹机制”&#xff0c;该机制旨在以大概率保持“虚拟鼻子”&#xff08;一种象征虚假程度的概念&#xff09;不会过长&#xff0c;通过在对话中夹杂真话与假话来实现。文章深入探讨了这一机制的原理&#xff0c;分析其背后的…

什么是zookeeper

ZooKeeper 是一个由 Apache 开源的分布式协调服务&#xff0c;专门为分布式系统提供高效、可靠的协同管理功能。它通过简单的接口和高效的设计&#xff0c;帮助开发者解决分布式环境中的常见问题&#xff0c;如配置管理、服务注册与发现、分布式锁、集群选举等。 --- ### 核心…

easy-poi导出and导入一对多数据excel

easy-poi导出and导入一对多数据excel 一、导入jar包 <!-- easy-poi --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>4.4.0</version></dependency> 二…