/**
*公司:杭州图华科技有限公司
*版权信息:图华所有
*任务:太阳系模拟构建实习作业
*描述:绘图界面实现
*
*版本:1.1
*作者:叶广平
*日期:2019/4/25
**/
#include "OpenglWidget.h"
#include "Mainwindow.h"
#include "MainWidget.h"
#include"constant.h"
#include<GL/glut.h>
#include"planet.h"
#include<ctime>
#include<Windows.h>
#include<iostream>
#include<cmath>
OpenglWidget::OpenglWidget(QWidget *parent)
:QOpenGLWidget(parent),current_planet(Sun),speed(60),pause(GL_FALSE),lightSwitch(GL_FALSE)
{
projM.setToIdentity();
projM.perspective(75,13.0/9.0, 0.2, 1000);
}
OpenglWidget::~OpenglWidget()
{
delete[] solarsystem;
delete[] camera;
}
void OpenglWidget::initializeGL()
{
solarsystem = new solarSystem();
camera=new Camera();
solarsystem->calculatePositions(times);
glShadeModel(GL_SMOOTH);
}
void OpenglWidget::paintGL()
{
solarsystem->calculatePositions(times);
check_current_planet();
solarsystem->render_system(QOpenGLContext::currentContext()->extraFunctions(),
projM,camera,lightSwitch);
calculate_frame_rate();
if (pause == GL_FALSE)
times += speed;
else if(pause == GL_TRUE)
times = times;
update();
}
void OpenglWidget::resizeGL(int w, int h)
{
projM.setToIdentity();
projM.perspective(75.0f, GLfloat(w) / (GLfloat)h, 1.0f, 1000.0f);
}
void OpenglWidget::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
}
void OpenglWidget::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->x() - lastPos.x();
int dy = event->y() - lastPos.y();
if (event->buttons() & Qt::LeftButton)
{
camera->angle_xy += dx * rate;
camera->angle_z += dy * rate;
if (camera->angle_xy > PI*5/4)
camera->angle_xy = PI*5/4;
if (camera->angle_xy < PI*3/4)
camera->angle_xy = PI*3/4;
if (camera->angle_z > PI / 6)
camera->angle_z = PI / 6 ;
if (camera->angle_z < PI/32)
camera->angle_z = PI/32;
}
else if (event->buttons() & Qt::RightButton)
{
camera->distance += dy*15*rate;
if (camera->distance > 100)
camera->distance = 100;
if (camera->distance < 2.5)
camera->distance = 2.5;
}
lastPos = event->pos();
}
void OpenglWidget::check_current_planet()
{
switch (current_planet)
{
case Sun:
{
camera->m_target[0] = 0.0f;
camera->m_target[1] = 0.0f;
camera->m_target[2] = 0.0f;
camera->m_eye[0] = camera->distance * cos(camera->angle_z)*cos(camera->angle_xy);
camera->m_eye[1] = camera->distance * cos(camera->angle_z)*sin(camera->angle_xy);
camera->m_eye[2] = camera->distance * sin(camera->angle_z);
camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));
break;
}
case Mercury:
{
camera->m_target[0] = solarsystem->mercury->position[0] * distanceScale;
camera->m_target[1] = solarsystem->mercury->position[1] * distanceScale;
camera->m_target[2] = solarsystem->mercury->position[2] * distanceScale;
camera->m_eye[0] = camera->m_target[0] + camera->distance * cos(camera->angle_z)*cos(camera->angle_xy);
camera->m_eye[1] = camera->m_target[1] + camera->distance * cos(camera->angle_z)*sin(camera->angle_xy);
camera->m_eye[2] = camera->m_target[2] + camera->distance * sin(camera->angle_z);
camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));
break;
}
case Venus:
{
camera->m_target[0] = solarsystem->venus->position[0] * distanceScale;
camera->m_target[1] = solarsystem->venus->position[1] * distanceScale;
camera->m_target[2] = solarsystem->venus->position[2] * distanceScale;
camera->m_eye[0] = camera->m_target[0] + camera->distance * cos(camera->angle_z)*cos(camera->angle_xy);
camera->m_eye[1] = camera->m_target[1] + camera->distance * cos(camera->angle_z)*sin(camera->angle_xy);
camera->m_eye[2] = camera->m_target[2] + camera->distance * sin(camera->angle_z);
camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));
break;
}
case Earth:
{
camera->m_target[0] = solarsystem->earth->position[0] * distanceScale;
camera->m_target[1] = solarsystem->earth->position[1] * distanceScale;
camera->m_target[2] = solarsystem->earth->position[2] * distanceScale;
camera->m_eye[0] = camera->m_target[0] + camera->distance * cos(camera->angle_z)*cos(camera->angle_xy);
camera->m_eye[1] = camera->m_target[1] + camera->distance * cos(camera->angle_z)*sin(camera->angle_xy);
camera->m_eye[2] = camera->m_target[2] + camera->distance * sin(camera->angle_z);
camera->m_up = QVector3D{ 0,0,1 };
/* camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));*/
break;
}
case Mars:
{
camera->m_target[0] = solarsystem->mars->position[0] * distanceScale;
camera->m_target[1] = solarsystem->mars->position[1] * distanceScale;
camera->m_target[2] = solarsystem->mars->position[2] * distanceScale;
camera->m_eye[0] = camera->m_target[0] + camera->distance * cos(camera->angle_z)*cos(camera->angle_xy);
camera->m_eye[1] = camera->m_target[1] + camera->distance * cos(camera->angle_z)*sin(camera->angle_xy);
camera->m_eye[2] = camera->m_target[2] + camera->distance * sin(camera->angle_z);
camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));
break;
}
case Jupiter:
{
camera->m_target[0] = solarsystem->jupiter->position[0] * distanceScale;
camera->m_target[1] = solarsystem->jupiter->position[1] * distanceScale;
camera->m_target[2] = solarsystem->jupiter->position[2] * distanceScale;
if (camera->distance < 10)
camera->distance = 10;
camera->m_eye[0] =(camera->m_target[0] + camera->distance * abs(cos(camera->angle_z))*cos(camera->angle_xy));
camera->m_eye[1] = (camera->m_target[1] + camera->distance * abs(cos(camera->angle_z))*sin(camera->angle_xy));
camera->m_eye[2] =(camera->m_target[2] + camera->distance * abs(sin(camera->angle_z)));
camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));
break;
} /*(solarsystem->jupiter->radius)/10000**/
case Saturn:
{
camera->m_target[0] = solarsystem->saturn->position[0] * distanceScale;
camera->m_target[1] = solarsystem->saturn->position[1] * distanceScale;
camera->m_target[2] = solarsystem->saturn->position[2] * distanceScale;
if (camera->distance < 8)
camera->distance = 8;
camera->m_eye[0] = camera->m_target[0] + camera->distance * cos(camera->angle_z)*cos(camera->angle_xy);
camera->m_eye[1] = camera->m_target[1] + camera->distance * cos(camera->angle_z)*sin(camera->angle_xy);
camera->m_eye[2] = camera->m_target[2] + camera->distance * sin(camera->angle_z);
camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));
break;
}
case Uranus:
{
camera->m_target[0] = solarsystem->uranus->position[0] * distanceScale;
camera->m_target[1] = solarsystem->uranus->position[1] * distanceScale;
camera->m_target[2] = solarsystem->uranus->position[2] * distanceScale;
camera->m_eye[0] = camera->m_target[0] + camera->distance * cos(camera->angle_z)*cos(camera->angle_xy);
camera->m_eye[1] = camera->m_target[1] + camera->distance * cos(camera->angle_z)*sin(camera->angle_xy);
camera->m_eye[2] = camera->m_target[2] + camera->distance * sin(camera->angle_z);
camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));
break;
}
case Neptune:
{
camera->m_target[0] = solarsystem->neptune->position[0] * distanceScale;
camera->m_target[1] = solarsystem->neptune->position[1] * distanceScale;
camera->m_target[2] = solarsystem->neptune->position[2] * distanceScale;
camera->m_eye[0] = camera->m_target[0] + camera->distance * cos(camera->angle_z)*cos(camera->angle_xy);
camera->m_eye[1] = camera->m_target[1] + camera->distance * cos(camera->angle_z)*sin(camera->angle_xy);
camera->m_eye[2] = camera->m_target[2] + camera->distance * sin(camera->angle_z);
camera->m_up = QVector3D::crossProduct(camera->m_eye - camera->m_target, QVector3D(0, -1, 0));
break;
}
}
}
void OpenglWidget::sun()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、IOS、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:数据集、包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备。
资源推荐
资源详情
资源评论
收起资源包目录
【C++项目】Qt+OpenGL模拟太阳系行星系统.zip (57个子文件)
cm
MainWidget.h 944B
Mainwindow.ui 776B
MainWidget.cpp 3KB
BallShader.cpp 1KB
OpenglWidget.cpp 9KB
QtSolarSystem.sln 1KB
CoreBallShader.h 763B
ball.cpp 213B
solarSystem.h 746B
.gitattributes 2KB
fsrc.glsl 149B
Camera.h 413B
planet.h 1016B
ball.h 279B
vsrc.glsl 241B
OpenglWidget.h 2KB
tga.cpp 6KB
main.cpp 395B
GeneratedFiles
qrc_Mainwindow.cpp 1KB
ui_Mainwindow.h 2KB
solarSystem.cpp 7KB
constant.h 541B
ControlLayer.h 618B
ControlLayer.cpp 4KB
QtSolarSystem.vcxproj 11KB
Mainwindow.cpp 2KB
CoreBallShader.cpp 3KB
Mainwindow.qrc 66B
Mainwindow.h 972B
Camera.cpp 725B
planet.cpp 2KB
Resources
include
GL
freeglut_ext.h 10KB
freeglut.h 681B
glut.h 639B
freeglut_std.h 26KB
lib
freeglutd.lib 36KB
glut.lib 78KB
glut32.lib 78KB
.gitignore 4KB
QtSolarSystem.vcxproj.filters 5KB
images
saturn.tga 37KB
help.tga 655KB
pluto.tga 46KB
earth.tga 47KB
stars2.tga 3MB
venus.tga 47KB
mars.tga 47KB
moon.tga 47KB
neptune.tga 32KB
test.tga 48KB
mercury.tga 48KB
stars.tga 3MB
sun.tga 48KB
uranus.tga 45KB
jupiter.tga 48KB
BallShader.h 635B
tga.h 557B
共 57 条
- 1
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计算机专业设计思路,个人学习整理教程,分析给需要的同学
- 大学生就业-JAVA-基于springBoot大学生就业信息管理系统设计与实现
- 计算机软件课程设计思路,个人学习整理教程,分析给需要的同学
- VMware安装教程,个人学习整理教程,分析给需要的同学
- X230安装Sonoma成功 博通BCM94352HMB网卡 扩展坞引线改屏1080P
- i686-4.8.2-release-posix-dwarf-rt-v3-rev3.7
- PHP的laravel5框架仿小米商城源码数据库 MySQL源码类型 WebForm
- (源码)基于Arduino和HTML的自动喂食系统.zip
- 智能健康饮食-JAVA-基于springBoot智能健康饮食系统设计与实现
- 在线项目管理-JAVA-基于springBoot在线项目管理与任务分配中的应用设计与实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功