Qt5使用openGL,进行3d绘图

https://blog.csdn.net/qq_35703848/article/details/79819854 

 1、

 1 #ifndef WIDGET_H
 2 #define WIDGET_H
 3 
 4 #include <QWidget>
 5 #include <QOpenGLWindow>
 6 #include <QOpenGLFunctions>
 7 
 8 class Widget : public QOpenGLWindow,protected QOpenGLFunctions
 9 {
10     Q_OBJECT
11 
12 public:
13   //  Widget(QWidget *parent = 0);
14   //使用时,直接Widget w;即可。
15     ~Widget(){};
16 
17     void initializeGL()
18     {
19         initializeOpenGLFunctions();
20         glClearColor(0.0f,0.0f,0.0f,0.0f);
21     }
22     void resizeGL(int width, int height){} //若无需对高、宽进行处理,此函数可省
23     void paintGL()
24     {
25         glClear(GL_COLOR_BUFFER_BIT);
26     }
27 };
28 
29 #endif // WIDGET_H

 

2、使用QGLWidget

 1 #ifndef OPENGLWIDGET_H
 2 #define OPENGLWIDGET_H
 3 #include <QtOpenGL>
 4 
 5 class OpenglWidget : public QGLWidget
 6 {
 7 public:
 8     OpenglWidget(QWidget *parent=0);
 9 protected:
10     void initializeGL();
11     void initWidget();
12     void paintGL();
13     void resizeGL(int width, int height);
14 private:
15     GLfloat angle;
16     GLfloat angle1;
17 };
18 #endif // OPENGLWIDGET_H

 

  1 #include "widget.h"
  2 
  3 OpenglWidget::OpenglWidget(QWidget *parent):QGLWidget(parent)
  4 {
  5     initWidget();
  6     initializeGL();
  7 }
  8 void OpenglWidget::initializeGL()
  9 {
 10     //设置着色模式,平滑的
 11     glShadeModel(GL_SMOOTH);
 12     //清除掉之前的所有颜色
 13     glClearColor(0.0,0.0,0.0,0.0);
 14     //深度缓存,设置初始值为1.0,小于1.0的部分是可见的
 15     glClearDepth(1.0);
 16     //启动OPenGL的相关功能,由参数决定,这里指
 17     //(启用了之后,OpenGL在绘制的时候就会检查,当前像素前面是否有别的像素,如果别的像素挡道了它,那它就不会绘制,也就是说,OpenGL就只绘制最前面的一层)
 18     glEnable(GL_DEPTH_TEST);
 19     //制定深度缓存比较值
 20     //这里参数指的是如果输入的深度值小于或者等于参考值则通过
 21     glDepthFunc(GL_LEQUAL);
 22 
 23     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 24 
 25 }
 26 
 27 void OpenglWidget::initWidget()
 28 {
 29     //从屏幕上(400,400)为起始点,显示一个640*400的界面
 30     setGeometry(400,200,640,480);
 31     setWindowTitle("My OPenGL");
 32 }
 33 
 34 void OpenglWidget::paintGL()
 35 {
 36     //清除颜色缓冲和深度缓冲
 37     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 38     //将当前点移置屏幕中心,相当于复位的操作
 39     glLoadIdentity();
 40     //平移函数,参数指的是分别从X轴,Y轴,Z轴平移
 41     glTranslatef(-1.5,0.0,-6.0);
 42 
 43 
 44     glRotated(angle,0.0,1.0,0.0);
 45     //创建图元,是绘制什么图形的接口,参数是绘制多边形的意思
 46     glBegin(GL_QUADS);
 47     //设置顶点值(f代表浮点),三维空间坐标
 48     glColor3f( 1.0, 0.0, 0.0 );
 49     glBegin( GL_QUADS );
 50 
 51        glColor3f(1, 0, 0);
 52        glVertex3f(-1,1,1);
 53        glVertex3f(-1,1,-1);
 54        glVertex3f(-1,-1,-1);
 55        glVertex3f(-1,-1,1);
 56 
 57        glColor3f(0, 0, 1);
 58        glVertex3f(-1,1,-1);
 59        glVertex3f(-1,-1,-1);
 60        glVertex3f(1,-1,-1);
 61        glVertex3f(1,1,-1);
 62 
 63        glColor3f(0, 1, 0);
 64        glVertex3f(1,1,-1);
 65        glVertex3f(1,-1,-1);
 66        glVertex3f(1,-1,1);
 67        glVertex3f(1,1,1);
 68 
 69        glColor3f(0, 0, 1);
 70        glVertex3f(1,-1,1);
 71        glVertex3f(1,1,1);
 72        glVertex3f(-1,1,1);
 73        glVertex3f(-1,-1,1);
 74 
 75        glColor3f(0, 1, 0);
 76        glVertex3f(-1,1,1);
 77        glVertex3f(1,1,1);
 78        glVertex3f(1,1,-1);
 79        glVertex3f(-1,1,-1);
 80 
 81        glColor3f(0, 0, 1);
 82        glVertex3f(-1,-1,1);
 83        glVertex3f(-1,-1,-1);
 84        glVertex3f(1,-1,-1);
 85        glVertex3f(1,-1,1);
 86        glEnd();
 87 
 88        glLoadIdentity();
 89 
 90       glTranslatef(1.5,0.0,-6.0);
 91       glRotatef( angle1,0.0,1.0,0.0);
 92 
 93       glBegin( GL_TRIANGLES );
 94 
 95       glColor3f( 1.0, 0.0, 0.0 );
 96       glVertex3f(  0.0,  1.0,  0.0 );
 97       glColor3f( 0.0, 0.0, 1.0 );
 98       glVertex3f( -1.0, -1.0, -1.0 );
 99       glColor3f( 0.0, 1.0, 0.0 );
100       glVertex3f( -1.0, -1.0,  1.0 );
101 
102       glColor3f( 1.0, 0.0, 0.0 );
103       glVertex3f(  0.0,  1.0,  0.0 );
104       glColor3f( 0.0, 0.0, 1.0 );
105       glVertex3f(  1.0, -1.0,  1.0 );
106       glColor3f( 0.0, 1.0, 0.0 );
107       glVertex3f(  1.0, -1.0, -1.0 );
108 
109       glColor3f( 1.0, 0.0, 0.0 );
110       glVertex3f(  0.0,  1.0,  0.0 );
111       glColor3f( 0.0, 1.0, 0.0 );
112       glVertex3f( -1.0, -1.0,  1.0 );
113       glColor3f( 0.0, 0.0, 1.0 );
114       glVertex3f(  1.0, -1.0,  1.0 );
115 
116 
117       glColor3f( 1.0, 0.0, 0.0 );
118       glVertex3f(  0.0,  1.0,  0.0 );
119       glColor3f( 0.0, 1.0, 0.0 );
120       glVertex3f(  1.0, -1.0, -1.0 );
121       glColor3f( 0.0, 0.0, 1.0 );
122       glVertex3f( -1.0, -1.0, -1.0 );
123 
124       glEnd();
125 
126     angle+=70;
127     angle1+=10;
128 
129 }
130 
131 void OpenglWidget::resizeGL(int width, int height)
132 {
133     if(0==height)
134         height=1;
135     //告诉绘制到窗体的哪个位置
136     glViewport(0,0,width,height);
137     // 设置矩阵模式,参数是设置为投影矩阵
138     glMatrixMode(GL_PROJECTION);
139     //复位操作
140     glLoadIdentity();
141 
142     GLdouble aspectRatio=(GLfloat)width/(GLfloat)height;
143     GLdouble rFov=45.0*3.14159265/180.0;
144     GLdouble zNear=0.1;
145     GLdouble zFar=100.0;
146     //调用glFrustum,生成矩阵与当前矩阵相乘,生成透视效果
147     glFrustum(-zNear*tan(rFov/2.0)*aspectRatio,
148               zNear*tan(rFov/2.0)*aspectRatio,
149               -zNear*tan(rFov/2.0),
150               zNear*tan(rFov/2.0),
151               zNear,zFar);
152     //切回模型视图矩阵
153     glMatrixMode(GL_MODELVIEW);
154     //复位
155     glLoadIdentity();
156 }

 

转载于:https://www.cnblogs.com/wangbin-heng/p/10182007.html

This directory contains the Qt3D project for Qt5: * Qt3D QML bindings and * Qt3D C++ APIs Building Qt3D ================== Qt5 is a rapidly changing bleeding edge environment. This branch is our initial support for it and thus is also rapidly changing and bleeding edge. This branch is experimental, and unsupported. This information is provided for advanced use only. No guarantees about API stability or even if this works at all are supplied, use at your own risk. First fetch the Qt5 source tree and Qt3D master branch: cd ~/depot git clone ssh://codereview.qt-project.org:29418/qt/qt5.git cd qt5 ./init-repository --codereview-username \ --module-subset=qtbase,qtsvg,qtdeclarative,qttools,qtxmlpatterns,qtdoc,qlalr,qtrepotools,qtqa,qtlocation,qt3d git submodule foreach "git fetch gerrit && git reset --hard gerrit/master" cd qt3d scp -p -P 29418 codereview.qt-project.org:hooks/commit-msg .git/hooks/ git fetch gerrit git checkout --track -b master gerrit/master If you are reading this file then somehow you probably already got this far anyway. Now build Qt5, which will also build Qt3D as a module: cd ~/build mkdir qt5 cd qt5 ~/depot/qt5/configure -developer-build -opensource -confirm-license -no-webkit -no-phonon -nomake tests \ -nomake examples -declarative -opengl -svg && make -j 4 What's in Qt3D ================== Directory structure: src/threed/ This is the main library of the Qt3D project, containing abstractions for cross-platform GL, shaders, lighting models, and so on. src/plugins/ Scene format loading plugins. src/imports/ QML import plugins. util/ Various utilities that are useful when working with Qt3D. examples/ Some examples of using Qt3D QML bindings and Qt3D C++ API. demos/ Some more complex demos of using Qt3D QML bindings and Qt3D C++ API. tests/auto/qml3d/ Unit tests for the QML bindings. tests/auto/threed/ Unit tests for the C++ API doc/ Documentation. devices/symbian/ Symbian deployment file Documentation ============= The documentation can be generated with "make docs". It will be placed into "doc/html" in the build directory. Packages ======== This section is only for those developing Qt3D. Read on to discover how the building of packages works. This section is also important if you want to change how the structure of the Qt3D pro files work. To build Qt3D, run: qmake && make The .pro files will cause the toolchain to place the libraries, QML files and meshes of Qt3D directly into place, as part of the compile process. The files go into the bin/ directory, and the executables can be run directly from there. If you are doing a developer build, plugins will be installed in such a way that Qt will find them. After building the tree the install step is invoked using the INSTALL_ROOT environment export to cause the installation rules to place all the files into a sandboxed install tree, ready for packaging: INSTALL_ROOT=tmp make install Examples ======== Some examples require assimp library to parse the content. Go to http://assimp.sourceforge.net/ and build and install the assimp library. Then configure Qt3D to include assimp and run qmake && make.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值