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 程序设计与综合应用>张德丰等著 ...
随机推荐
- 【技术累积】【点】【编程】【13】XX式编程
(原)函数式编程 核心概念 函数式一等公民(输入输出啥的都可以是函数): 纯函数,固定输入带来固定输出: 阅读性良好,无并发问题,但效率偏低: 大历史背景 旨在描述问题如何计算: 有两位巨擘对问题的可 ...
- tee
功能说明:把数据重定向到给定文件和屏幕上. 参数选项: -a 向文件追加内容,而不是覆盖. tee命令允许标准输出同时把内容写入(覆盖)到文件中的实践. tee命令允许标准输出同时把内容 ...
- esp32(M5STACK)在线体验(Ubuntu)
我们往m5stack烧录的固件是可以在线编程的 具体使用方法可以参考 https://github.com/m5stack/M5Cloud/blob/master/README_CN.md ...
- 【剑指Offer】29、最小的K个数
题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 解题思路: 本题最直观的解法就是将输入的n个整数排 ...
- 在微信小程序里使用 watch 和 computed
在开发 vue 的时候,我们可以使用 watch 和 computed 很方便的检测数据的变化,从而做出相应的改变,但是在小程序里,只能在数据改变时手动触发 this.setData(),那么如何给小 ...
- c#打包时 Could not find file "I:\VS2012\myWork\SmartCam\SmartCam\bin\Debug\Emgu.CV.DebuggerVisualizers.VS2012.dll" ISEXP : error : -6103: Could not find file "I:\VS2012\myWork\SmartCam\SmartCam\bin
1.错误:C#打包时发生如下错误: 错误 1 -6103: Could not find file "I:\VS2012\myWork\SmartCam\SmartCam\bin\Debug ...
- Travel Card
Travel Card time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- eclipse jvm调优
1.初始参数 -Xms256m-Xmx1024m 2.在eclipse.ini中加入,注意一点的是D:/soft/eclipse-jee,这个目录必须存在,启动时并不会自动目录 -verbose:gc ...
- EF--code first数据迁移命令
原文推荐!点我点我! 添加Migrations文件夹,并生成类文件Configuration.cs PM> Enable-Migrations -EnableAutomaticMigration ...
- 南洋理工大学 ACM 在线评测系统 矩形嵌套
矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a& ...