当前位置: 首页 > news >正文

qt-OPENGL-星系仿真

qt-OPENGL-星系仿真

  • 一、演示效果
  • 二、核心程序
  • 三、下载链接


一、演示效果

在这里插入图片描述

二、核心程序

#include "model.h"Model::Model(QOpenGLWidget *_glWidget)
{   glWidget = _glWidget;glWidget->makeCurrent();initializeOpenGLFunctions();
}Model::~Model()
{destroyVBOs();
}void Model::destroyVBOs()
{glDeleteBuffers(1, &vboVertices);glDeleteBuffers(1, &vboIndices);glDeleteBuffers(1, &vboNormals);glDeleteBuffers(1, &vboTexCoords);glDeleteBuffers(1, &vboTangents);glDeleteVertexArrays(1, &vao);vboVertices = 0;vboIndices = 0;vboNormals = 0;vboTexCoords = 0;vboTangents = 0;vao = 0;
}void Model::createVBOs()
{glWidget->makeCurrent();destroyVBOs();glGenVertexArrays(1, &vao);glBindVertexArray(vao);glGenBuffers(1, &vboVertices);glBindBuffer(GL_ARRAY_BUFFER, vboVertices);glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(QVector4D), vertices.get(), GL_STATIC_DRAW);glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, nullptr);glEnableVertexAttribArray(0);vertices.reset();glGenBuffers(1, &vboNormals);glBindBuffer(GL_ARRAY_BUFFER, vboNormals);glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(QVector3D), normals.get(), GL_STATIC_DRAW);glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);glEnableVertexAttribArray(1);normals.reset();glGenBuffers(1, &vboTexCoords);glBindBuffer(GL_ARRAY_BUFFER, vboTexCoords);glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(QVector2D), texCoords.get(), GL_STATIC_DRAW);glBindBuffer(GL_ARRAY_BUFFER, vboTexCoords);glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, nullptr);glEnableVertexAttribArray(2);texCoords.reset();glGenBuffers(1, &vboTangents);glBindBuffer(GL_ARRAY_BUFFER, vboTangents);glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(QVector4D), tangents.get(), GL_STATIC_DRAW);glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, nullptr);glEnableVertexAttribArray(3);tangents.reset();glGenBuffers(1, &vboIndices);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndices);glBufferData(GL_ELEMENT_ARRAY_BUFFER, numFaces * 3 * sizeof(unsigned int), indices.get(), GL_STATIC_DRAW);indices.reset();
}void Model::drawModel()
{float fixedAngle = -90.0f;modelMatrix.setToIdentity();modelMatrix.translate(position);modelMatrix.rotate(angle, 0.0, 1.0, 0.0);modelMatrix.rotate(fixedAngle, 1.0, 0.0, 0.0);modelMatrix.scale(invDiag * scale, invDiag * scale, invDiag*scale);modelMatrix.translate(-midPoint);GLuint locModel = 0;GLuint locNormalMatrix = 0;GLuint locShininess = 0;locModel = glGetUniformLocation(shaderProgram, "model");locNormalMatrix = glGetUniformLocation(shaderProgram, "normalMatrix");locShininess = glGetUniformLocation(shaderProgram, "shininess");glBindVertexArray(vao);// GL_CHECK(glUseProgram(shaderProgram[shaderIndex]));glUniformMatrix4fv(locModel, 1, GL_FALSE, modelMatrix.data());glUniformMatrix3fv(locNormalMatrix, 1, GL_FALSE, modelMatrix.normalMatrix().data());glUniform1f(locShininess, static_cast<GLfloat>(material.shininess));if (textureID){GLuint locColorTexture = 0;locColorTexture = glGetUniformLocation(shaderProgram, "colorTexture");glUniform1i(locColorTexture, 0);glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, textureID);}glDrawElements(GL_TRIANGLES, numFaces * 3, GL_UNSIGNED_INT, 0);
}void Model::readOFFFile(QString const &fileName)
{std::ifstream stream;stream.open(fileName.toUtf8(),std::ifstream::in);if (!stream.is_open()){qWarning("Cannot open file.");return;}std::string line;stream >> line;stream >> numVertices >> numFaces >> line;// http://en.cppreference.com/w/cpp/memory/unique_ptr/make_uniquevertices = std::make_unique<QVector4D[]>(numVertices);indices = std::make_unique<unsigned int[]>(numFaces * 3);if (numVertices > 0){float minLim = std::numeric_limits<float>::lowest();float maxLim = std::numeric_limits<float>::max();QVector4D max(minLim, minLim, minLim, 1.0);QVector4D min(maxLim, maxLim, maxLim, 1.0);for (unsigned int i = 0; i < numVertices; ++i){float x, y, z;stream >> x >> y >> z;max.setX(std::max(max.x(), x));max.setY(std::max(max.y(), y));max.setZ(std::max(max.z(), z));min.setX(std::min(min.x(), x));min.setY(std::min(min.y(), y));min.setZ(std::min(min.z(), z));vertices[i] = QVector4D(x, y, z, 1.0);}midPoint = QVector3D((min + max) * 0.5);invDiag = 1 / (max - min).length();}for (unsigned int i = 0; i < numFaces; ++i){unsigned int a, b, c;stream >> line >> a >> b >> c;indices[i * 3 + 0] = a;indices[i * 3 + 1] = b;indices[i * 3 + 2] = c;}stream.close();createNormals();createTexCoords();createTangents();createVBOs();
}void Model::createNormals()
{normals = std::make_unique<QVector3D[]>(numVertices);for (unsigned int i = 0; i < numFaces; ++i){QVector3D a = QVector3D(vertices[indices[i * 3 + 0]]);QVector3D b = QVector3D(vertices[indices[i * 3 + 1]]);QVector3D c = QVector3D(vertices[indices[i * 3 + 2]]);QVector3D faceNormal = QVector3D::crossProduct((b - a), (c - b));// Accumulates face normals on the verticesnormals[indices[i * 3 + 0]] += faceNormal;normals[indices[i * 3 + 1]] += faceNormal;normals[indices[i * 3 + 2]] += faceNormal;}for (unsigned int i = 0; i < numVertices; ++i){normals[i].normalize();}
}void Model::createTexCoords()
{texCoords = std::make_unique<QVector2D[]>(numVertices);// Compute minimum and maximum valuesauto minz = std::numeric_limits<float>::max();auto maxz = std::numeric_limits<float>::lowest();for (unsigned int i = 0; i < numVertices; ++i){minz = std::min(vertices[i].z(), minz);maxz = std::max(vertices[i].z(), maxz);}for (unsigned int i = 0; i < numVertices; ++i){auto s = (std::atan2(vertices[i].y(), vertices[i].x()) + M_PI) / (2 * M_PI);auto t = 1.0f - (vertices[i].z() - minz) / (maxz - minz);texCoords[i] = QVector2D(s, t);}
}void Model::loadTexture(const QImage &image)
{if (textureID){glDeleteTextures(1, &textureID);}glGenTextures(1, &textureID);glBindTexture(GL_TEXTURE_2D, textureID);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits());glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);glGenerateMipmap(GL_TEXTURE_2D);
}void Model::createTangents()
{tangents = std::make_unique<QVector4D[]>(numVertices);std::unique_ptr<QVector3D[]> bitangents;bitangents = std::make_unique<QVector3D[]>(numVertices);for (unsigned int i = 0; i < numFaces ; ++i){unsigned int i1 = indices[i * 3 + 0];unsigned int i2 = indices[i * 3 + 1];unsigned int i3 = indices[i * 3 + 2];QVector3D E = vertices[i1].toVector3D();QVector3D F = vertices[i2].toVector3D();QVector3D G = vertices[i3].toVector3D();QVector2D stE = texCoords[i1];QVector2D stF = texCoords[i2];QVector2D stG = texCoords[i3];QVector3D P = F - E;QVector3D Q = G - E;QVector2D st1 = stF - stE;QVector2D st2 = stG - stE;QMatrix2x2 M;M(0, 0) =  st2.y();M(0, 1) = -st1.y();M(1, 0) = -st2.x();M(1, 1) =  st1.x();M *= (1.0 / (st1.x() * st2.y() - st2.x() * st1.y()));QVector4D T = QVector4D (M(0, 0) * P.x() + M(0, 1) * Q.x(),M(0, 0) * P.y() + M(0, 1) * Q.y(),M(0, 0) * P.z() + M(0, 1) * Q.z(), 0.0);QVector3D B = QVector3D (M(1, 0) * P.x() + M(1, 1) * Q.x(),M(1, 0) * P.y() + M(1, 1) * Q.y(),M(1, 0) * P.z() + M(1, 1) * Q.z());tangents[i1] += T;tangents[i2] += T;tangents[i3] += T;bitangents[i1] += B;bitangents[i2] += B;bitangents[i3] += B;}for (unsigned int i = 0; i < numVertices; ++i){const QVector3D &n = normals[i];const QVector4D &t = tangents[i];tangents[i] = (t - n * QVector3D::dotProduct(n, t.toVector3D())).normalized();QVector3D b = QVector3D::crossProduct(n, t.toVector3D());double hand = QVector3D::dotProduct(b, bitangents[i]);tangents[i].setW((hand < 0.0) ? -1.0 : 1.0);}
}

三、下载链接

https://download.csdn.net/download/u013083044/88861312。

相关文章:

qt-OPENGL-星系仿真

qt-OPENGL-星系仿真 一、演示效果二、核心程序三、下载链接 一、演示效果 二、核心程序 #include "model.h"Model::Model(QOpenGLWidget *_glWidget) { glWidget _glWidget;glWidget->makeCurrent();initializeOpenGLFunctions(); }Model::~Model() {destroyV…...

Java实战:Spring Boot实现AOP记录操作日志

本文将详细介绍如何在Spring Boot应用程序中使用Aspect Oriented Programming&#xff08;AOP&#xff09;来实现记录操作日志的功能。我们将探讨Spring Boot集成AOP的基本概念&#xff0c;以及如何使用Spring Boot实现AOP记录操作日志。最后&#xff0c;我们将通过一个具体示例…...

C++ //练习 7.38 有些情况下我们希望提供cin作为接受istream参数的构造函数的默认实参,请声明这样的构造函数。

C Primer&#xff08;第5版&#xff09; 练习 7.38 练习 7.38 有些情况下我们希望提供cin作为接受istream&参数的构造函数的默认实参&#xff0c;请声明这样的构造函数。 环境&#xff1a;Linux Ubuntu&#xff08;云服务器&#xff09; 工具&#xff1a;vim 代码块 Sa…...

算法:两数之和

算法&#xff1a;两数之和 方法一&#xff1a;暴力法 function twoSum(nums, target) {for (let i 0; i < nums.length; i) {for (let j i 1; j < nums.length; j) {if (nums[i] nums[j] target) {return [i, j];}}}return null; }方法二&#xff1a;哈希表 func…...

pytorch: ground truth similarity matrix

按照真实标签排序pair-wise相似度矩阵的Pytorch代码 本文仅作留档&#xff0c;用于输出可视化 Inputs: Ground-truths Y ∈ R n 1 \mathbf{Y}\in\mathbb R^{n\times 1} Y∈Rn1, Similarity matrix A ∈ R n n \mathbf{A}\in\mathbb R^{n\times n} A∈RnnOutputs: Block dia…...

鸿蒙 gnss 开关使能流程

先WiFi&#xff0c;后 定位&#xff0c;再从蓝牙到NFC&#xff0c;这个就是我大致熟悉开源鸿蒙代码的一个顺序流程&#xff0c;WiFi 的年前差不多基本流程熟悉了&#xff0c;当然还有很多细节和内容没有写到&#xff0c;后续都会慢慢的丰富起来&#xff0c;这一篇将开启GNSS的篇…...

设计模式-创建型模式-抽象工厂模式

抽象工厂模式&#xff08;Abstract Factory Pattern&#xff09;&#xff1a;提供一个创建一系列相关或相互依赖对象的接口&#xff0c;而无须指定它们具体的类。抽象工厂模式又称为Kit模式&#xff0c;它是一种对象创建型模式。 由于工厂方法模式中的每个工厂只生产一类产品&…...

【Go】五、Grpc 的入门使用

grpc 与 protobuf grpc 使用的是 protobuf 协议&#xff0c;其是一个通用的 rpc 框架&#xff0c;基本支持主流的所有语言、其底层使用 http/2 进行网络通信&#xff0c;具有较高的效率 protobuf 是一种序列化格式&#xff0c;这种格式具有 序列化以及解码速度快&#xff08;…...

PDF加粗内容重复读取解决方案

文章目录 前言发现问题解决方案问题分析大致逻辑 show my code 前言 在使用pdfplumber读取PDF的过程中&#xff0c;由于加黑的内容会被莫名其妙的读取两次&#xff0c;带来了很大的困扰。这篇文章将给出解决方案。 发现问题 在在使用pdfplumber读取PDF的过程中&#xff0c;读…...

Golang 并发 Channel的用法

目录 Golang 并发 Channel的用法1. channel 的创建2. nil channel读写阻塞示例close示例 3. channel 的读写4. channel 只读只写5. 关闭channelchannel关闭后&#xff0c;剩余的数据能否取到读取关闭的channel&#xff0c;将获取零值使用ok判断&#xff0c;是否关闭使用for-ran…...

cfa复习资料介绍之二:notes(SchweserNotes)

什么是CFA notes? CFA资料Study Notes都是外国一些出版机构针对CFA考试提供的复习资料&#xff0c;而其中Schweser在国内的名气最大&#xff0c;用的人也最多。内容详尽并且突出重点&#xff0c;并且CFA Notes的内容相比于官方curriculum教材更加符合中国CFA考生的心态&#x…...

FITC Palmitate Conjugate,FITC-棕榈酸酯缀合物,可以用标准 FITC 滤光片组进行成像

FITC Palmitate Conjugate&#xff0c;FITC-棕榈酸酯缀合物&#xff0c;可以用标准 FITC 滤光片组进行成像 您好&#xff0c;欢迎来到新研之家 文章关键词&#xff1a;FITC Palmitate Conjugate&#xff0c;FITC-棕榈酸酯缀合物&#xff0c;FITC 棕榈酸酯缀合物&#xff0c;F…...

本机防攻击简介

定义 在网络中&#xff0c;存在着大量针对CPU&#xff08;Central Processing Unit&#xff09;的恶意攻击报文以及需要正常上送CPU的各类报文。针对CPU的恶意攻击报文会导致CPU长时间繁忙的处理攻击报文&#xff0c;从而引发其他业务的中断甚至系统的中断&#xff1b;大量正常…...

Python 进阶语法:JSON

1 什么是 JSON&#xff1f; 1.1 JSON 的定义 JSON 是 JavaScript Object Notation 的简写&#xff0c;字面上的意思是 JavaScript 对象标记。本质上&#xff0c;JSON 是轻量级的文本数据交换格式。轻量级&#xff0c;是拿它与另一种数据交换格式XML进行比较&#xff0c;相当轻…...

mescroll 在uni-app 运行的下拉刷新和上拉加载的组件

官网传送门: https://www.mescroll.com/uni.html 最近使用到了mescroll 但是一直都是整个页面的滚动, 最近需求有需要局部滚动, 收藏了一个博主的文章觉得写的还挺好, 传送门: https://blog.csdn.net/Minions_Fatman/article/details/134754926?spm1001.2014.3001.5506 使用…...

netty的TCP服务端和客户端实现

第一步&#xff1a;引入依赖 <dependencies><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.90.Final</version></dependency></dependencies> 第二步&#xff1a;实…...

合纵连横 – 以 Flink 和 Amazon MSK 构建 Amazon DocumentDB 之间的实时数据同步

在大数据时代&#xff0c;实时数据同步已经有很多地方应用&#xff0c;包括从在线数据库构建实时数据仓库&#xff0c;跨区域数据复制。行业落地场景众多&#xff0c;例如&#xff0c;电商 GMV 数据实时统计&#xff0c;用户行为分析&#xff0c;广告投放效果实时追踪&#xff…...

HBase 进阶

参考来源: B站尚硅谷HBase2.x 目录 Master 架构RegionServer 架构写流程MemStore Flush读流程HFile 结构读流程合并读取数据优化 StoreFile CompactionRegion Split预分区&#xff08;自定义分区&#xff09;系统拆分 Master 架构 Master详细架构 1&#xff09;Meta 表格介…...

一周学会Django5 Python Web开发-Django5路由命名与反向解析reverse与resolve

锋哥原创的Python Web开发 Django5视频教程&#xff1a; 2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 Django5 Python web开发 视频教程(无废话版) 玩命更新中~共计25条视频&#xff0c;包括&#xff1a;2024版 Django5 Python we…...

好奇!为什么gateway和springMVC之间依赖冲突?

Gateway和SpringMVC之间存在冲突&#xff0c;可能是因为它们分别基于不同的技术栈。具体来说&#xff1a; 技术栈差异&#xff1a;Spring Cloud Gateway 是建立在 Spring Boot 2.x 和 Spring WebFlux 基础之上的&#xff0c;它使用的是非阻塞式的 Netty 服务器。而 Spring MVC…...

KubeSphere 容器平台高可用:环境搭建与可视化操作指南

Linux_k8s篇 欢迎来到Linux的世界&#xff0c;看笔记好好学多敲多打&#xff0c;每个人都是大神&#xff01; 题目&#xff1a;KubeSphere 容器平台高可用&#xff1a;环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...

Spring Boot面试题精选汇总

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...

第 86 场周赛:矩阵中的幻方、钥匙和房间、将数组拆分成斐波那契序列、猜猜这个单词

Q1、[中等] 矩阵中的幻方 1、题目描述 3 x 3 的幻方是一个填充有 从 1 到 9 的不同数字的 3 x 3 矩阵&#xff0c;其中每行&#xff0c;每列以及两条对角线上的各数之和都相等。 给定一个由整数组成的row x col 的 grid&#xff0c;其中有多少个 3 3 的 “幻方” 子矩阵&am…...

快刀集(1): 一刀斩断视频片头广告

一刀流&#xff1a;用一个简单脚本&#xff0c;秒杀视频片头广告&#xff0c;还你清爽观影体验。 1. 引子 作为一个爱生活、爱学习、爱收藏高清资源的老码农&#xff0c;平时写代码之余看看电影、补补片&#xff0c;是再正常不过的事。 电影嘛&#xff0c;要沉浸&#xff0c;…...

Linux系统部署KES

1、安装准备 1.版本说明V008R006C009B0014 V008&#xff1a;是version产品的大版本。 R006&#xff1a;是release产品特性版本。 C009&#xff1a;是通用版 B0014&#xff1a;是build开发过程中的构建版本2.硬件要求 #安全版和企业版 内存&#xff1a;1GB 以上 硬盘&#xf…...

WEB3全栈开发——面试专业技能点P7前端与链上集成

一、Next.js技术栈 ✅ 概念介绍 Next.js 是一个基于 React 的 服务端渲染&#xff08;SSR&#xff09;与静态网站生成&#xff08;SSG&#xff09; 框架&#xff0c;由 Vercel 开发。它简化了构建生产级 React 应用的过程&#xff0c;并内置了很多特性&#xff1a; ✅ 文件系…...

《信号与系统》第 6 章 信号与系统的时域和频域特性

目录 6.0 引言 6.1 傅里叶变换的模和相位表示 6.2 线性时不变系统频率响应的模和相位表示 6.2.1 线性与非线性相位 6.2.2 群时延 6.2.3 对数模和相位图 6.3 理想频率选择性滤波器的时域特性 6.4 非理想滤波器的时域和频域特性讨论 6.5 一阶与二阶连续时间系统 6.5.1 …...

leetcode73-矩阵置零

leetcode 73 思路 记录 0 元素的位置&#xff1a;遍历整个矩阵&#xff0c;找出所有值为 0 的元素&#xff0c;并将它们的坐标记录在数组zeroPosition中置零操作&#xff1a;遍历记录的所有 0 元素位置&#xff0c;将每个位置对应的行和列的所有元素置为 0 具体步骤 初始化…...

【大模型】RankRAG:基于大模型的上下文排序与检索增强生成的统一框架

文章目录 A 论文出处B 背景B.1 背景介绍B.2 问题提出B.3 创新点 C 模型结构C.1 指令微调阶段C.2 排名与生成的总和指令微调阶段C.3 RankRAG推理&#xff1a;检索-重排-生成 D 实验设计E 个人总结 A 论文出处 论文题目&#xff1a;RankRAG&#xff1a;Unifying Context Ranking…...

从零手写Java版本的LSM Tree (一):LSM Tree 概述

&#x1f525; 推荐一个高质量的Java LSM Tree开源项目&#xff01; https://github.com/brianxiadong/java-lsm-tree java-lsm-tree 是一个从零实现的Log-Structured Merge Tree&#xff0c;专为高并发写入场景设计。 核心亮点&#xff1a; ⚡ 极致性能&#xff1a;写入速度超…...