45.Qt openGL实现三维绘图
main.cpp
#include <QApplication>
#include <iostream> #include "tetraheadron.h" int main(int argc, char *argv[])
{
QApplication app(argc, argv); if (!QGLFormat::hasOpenGL()) {
std::cerr << "This system has no OpenGL support" << std::endl;
return ;
} Tetrahedron tetrahedron;
tetrahedron.setWindowTitle(QObject::tr("Tetrahedron"));
tetrahedron.resize(, );
tetrahedron.show(); return app.exec();
}
tetraheaderon.h
#ifndef TETRAHEDRON_H
#define TETRAHEDRON_H #include <QGLWidget> class Tetrahedron : public QGLWidget
{
Q_OBJECT public:
Tetrahedron(QWidget *parent = ); protected:
//在QGLWidget中实现
void initializeGL();
void resizeGL(int width, int height);
void paintGL(); //鼠标事件处理在QWidget中实现
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event); private:
void draw();
int faceAtPosition(const QPoint &pos); GLfloat rotationX;
GLfloat rotationY;
GLfloat rotationZ;
QColor faceColors[];
QPoint lastPos;
}; #endif
tetraheadron.cpp
#include <QtGui>
#include <QtOpenGL>
#include <GL/glu.h>
#include "tetraheadron.h" Tetrahedron::Tetrahedron(QWidget *parent)
: QGLWidget(parent)
{
//指定OpenGL的显示描述表
setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer)); //初始化私有变量
rotationX = -21.0;
rotationY = -57.0;
rotationZ = 0.0;
faceColors[] = Qt::red;
faceColors[] = Qt::green;
faceColors[] = Qt::blue;
faceColors[] = Qt::yellow;
} //在调用paintGL()之前被调用一次.这里设置
//OpenGL的绘图描述表,定义显示列表,以及执行其他的初始化
void Tetrahedron::initializeGL()
{
qglClearColor(Qt::black);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
} //调用initializeGL之后调用resizeGL()函数,窗口部件改变大小时也调用resizeGL()
//函数,设置OpenGL视口,投影,以及其他与窗口部件尺寸相关的设置
void Tetrahedron::resizeGL(int width, int height)
{
glViewport(, , width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x = GLfloat(width) / height;
glFrustum(-x, +x, -1.0, +1.0, 4.0, 15.0);
glMatrixMode(GL_MODELVIEW);
} //需要重绘的时候调用
void Tetrahedron::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//实际的绘制由私有函数draw()实现
draw();
} //鼠标按下的事件
void Tetrahedron::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
} //鼠标移动的事件
void Tetrahedron::mouseMoveEvent(QMouseEvent *event)
{
GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
GLfloat dy = GLfloat(event->y() - lastPos.y()) / height(); //左键绕X,Y轴
if (event->buttons() & Qt::LeftButton) {
rotationX += * dy;
rotationY += * dx;
updateGL();
}
//右键绕X,Z轴
else if (event->buttons() & Qt::RightButton) {
rotationX += * dy;
rotationZ += * dx;
updateGL();
}
lastPos = event->pos();
} //双击事件
void Tetrahedron::mouseDoubleClickEvent(QMouseEvent *event)
{
int face = faceAtPosition(event->pos());
if (face != -) {
QColor color = QColorDialog::getColor(faceColors[face], this);
if (color.isValid()) {
faceColors[face] = color;
updateGL();
}
}
} //绘制
void Tetrahedron::draw()
{
static const GLfloat P1[] = { 0.0, -1.0, +2.0 };
static const GLfloat P2[] = { +1.73205081, -1.0, -1.0 };
static const GLfloat P3[] = { -1.73205081, -1.0, -1.0 };
static const GLfloat P4[] = { 0.0, +2.0, 0.0 }; //四个面
static const GLfloat * const coords[][] = {
{ P1, P2, P3 }, { P1, P3, P4 }, { P1, P4, P2 }, { P2, P4, P3 }
}; glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
//设置旋转
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0); //设置颜色
for (int i = ; i < ; ++i) {
glLoadName(i);
glBegin(GL_TRIANGLES);
qglColor(faceColors[i]);
for (int j = ; j < ; ++j) {
glVertex3f(coords[i][j][], coords[i][j][],
coords[i][j][]);
}
glEnd();
}
} //返回窗口部件某位置所在的面的编号
int Tetrahedron::faceAtPosition(const QPoint &pos)
{
const int MaxSize = ;
GLuint buffer[MaxSize];
GLint viewport[]; makeCurrent(); glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(MaxSize, buffer);
glRenderMode(GL_SELECT); glInitNames();
glPushName(); glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix(GLdouble(pos.x()),GLdouble(viewport[] - pos.y()),5.0,5.0,viewport);
GLfloat x = GLfloat(width()) / height();
glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
draw();
glMatrixMode(GL_PROJECTION);
glPopMatrix(); if (!glRenderMode(GL_RENDER))
return -;
return buffer[];
}
45.Qt openGL实现三维绘图的更多相关文章
- QT OpenGL绘制三维图形(立方体、圆柱体、圆锥、球体、圆环等等)
本文使用QGLWidget来绘制各种三维基本图形,包括立方体.圆柱体.圆锥.球体.圆环等等,涉及包括基本绘制以及上色.纹理.旋转等操作. 使用的软件版本:QT5.12 + QT Creater4.8. ...
- Qt OpenGL三维绘图
简介 OpenGL是为三维绘图提供的标准应用编程接口. OpenGL处理的仅仅是三维绘图方面,而很少或是根本不提供图形用户界面编程方面的支持.OpenGL*应用程序的用户界面必须由其它工具包创建,比 ...
- 使用C语言实现二维,三维绘图算法(3)-简单的二维分形
使用C语言实现二维,三维绘图算法(3)-简单的二维分形 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其 ...
- 使用C语言实现二维,三维绘图算法(1)-透视投影
使用C语言实现二维,三维绘图算法(1)-透视投影 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其实想想 ...
- 使用C语言实现二维,三维绘图算法(2)-解析曲面的显示
使用C语言实现二维,三维绘图算法(2)-解析曲面的显示 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其 ...
- matlab的三维绘图和四维绘图
一.三维绘图1.曲线图plot3(X1,Y1,Z1,...):以默认线性属性绘制三维点集(X1,Y1,Z1)确定的曲线plot3(X1,Y1,Z1,LineSpec):以参数LineSpec确定的线性 ...
- QT OpenGL中文教程在QT4版本后的错误代码更改(一)
由于教程中说的已经够可以了,这里就不对代码进行分析了,有兴趣可以自己去看看.这个教程来源于原来的NeHeOpenGL中文教程 (http://www.yakergong.net/nehe/) ,但其有 ...
- 基于OpenGL的三维曲面动态显示实现
在使用Visual C++的MFC AppWizard建立应用程序框架后,生成了多个类,与OpenGL编程相关的类是视图类,主要的显示任务都在其中完成. 1.基于OpenGL绘图的基本设置 1.1 设 ...
- matlab学习笔记8 基本绘图命令-三维绘图
一起来学matlab-matlab学习笔记8 基本绘图命令_6 三维绘图 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 ...
随机推荐
- linux ssh 经常断开 的解决方法
1.现象 在linux ,用ssh进行远程连接时,经常会发生长时间后断线,或者是无响应,就像卡住的感觉(键盘输入不进去). 2.解决方法 在ssh客户端的linux设置 # sudo vim /etc ...
- Oracle SQL 性能优化技巧
Select语句完整的执行顺序: SQL Select语句完整的执行顺序: 1. from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将 ...
- C++编译错误fatal error C1004: 发现意外的文件尾
出现这种情况就是类或者结构体的定义后面没有加“;”导致的. 而且这种问题好难排查.
- dubbo之线程模型
事件处理线程说明 如果事件处理的逻辑能迅速完成,并且不会发起新的IO请求,比如只是在内存中记个标识,则直接在IO线程上处理更快,因为减少了线程池调度. 但如果事件处理逻辑较慢,或者需要发起新的IO请求 ...
- SLAM: VSLAM扫盲之旅
在<机器人手册> 第2卷,这本书里面,第23章讲述了:三维视觉和基于视觉的实时定位于地图重建.在第37章里面,讲述了 同时定位与建图.指出了SLAM的三种基本方法. 一种是EKF的方法,但 ...
- Python 字符串常用方法 day2
1.去空格和换行符: s = ' a bc ' print(s.strip())#strip(),去掉字符串两边的空格和换行符,无法去除中间的空格 print(s.rstrip())#rstrip() ...
- eas之编码规则&单据转换规则
*当在企业建模中没有要显示的项目的话,则从包更新到系统树然后选择到规则定义,对申请单新增规则. 企业建模--业务规则-规则定义组织优先 多组织有先 集团优先固定值 显示格式PUR ..系统日期 2 ...
- 对 p 开 n 次方 (数学推论)
#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> us ...
- html第四节课
css CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/ 此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合 ...
- leetcode 188-maxProfit
public static int maxProfit(int k, int[] prices) { if (0 >= k || null == prices || 1 >= prices ...