Qt的Graphics-View框架和OpenGL结合详解
演示程序下载地址:这里
程序源代码下载地址:这里
这是一篇纯技术文,介绍了这一个月来我抽时间研究的成果。
Qt中有一个非常炫的例子:Boxes,它展示了Qt能够让其Graphics–View框架和Qt的OpenGL模块结合起来,渲染出非常出色的效果。其实我私自认为凭这个程序,已经有很多游戏开发者关注Qt了,因为游戏开发一个非常常见的模块就是UI,一般情况下游戏引擎提供的UI模块比较弱,基本上都是游戏引擎+第三方GUI库进行结合的。但是Qt以其Graphics–View框架能够非常轻松地将UI控件嵌入场景中,而且能够和OpenGL底层共存,更重要的是,凭借着Qt的qss,Qt可以定制许多GUI元素,这是非常具有吸引力的。所以说,如果大家对游戏开发感兴趣,那么不妨看一下Qt。
好了,下面介绍一下前几天我制作并发布的一个demo。这个demo是对Boxes这个例子进行模仿,结合学习《OpenGL超级宝典》,制作而成的,由于最近比较忙,所以总共花了将近一个月才完成,不得不说效率有点儿低。
首先从MainWindow.cpp这个文件说起吧,一开始是要初始化MainWindow类的,这个类是继承QMainWindow的,这里重点说说它的构造函数:
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">MainWindow</span><span style="color: rgb(0, 0, 0);">::MainWindow(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QWidget</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pParent</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">):</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">QMainWindow</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pParent</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">{</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">QGLWidget</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pWidget</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">new</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGLWidget</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGLFormat</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGL</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(128, 0, 128);">SampleBuffers</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">this</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pWidget->makeCurrent(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">scene</span></span></span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span style="color: rgb(0, 128, 0);">的内容</span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">GraphicsScene</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pScene</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">new</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">GraphicsScene</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">this</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">OpenGLView</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pView</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">new</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">OpenGLView</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">this</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pView->setViewport(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pWidget</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pView->setViewportUpdateMode(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGraphicsView</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(128, 0, 128);">FullViewportUpdate</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pView->setScene(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pScene</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(192, 192, 192);"> </span></span></span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span style="color: rgb(0, 128, 0);">选择不同的着色器的时候进行着色器连接</span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">connect(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pScene,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SIGNAL</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>SwitchShader<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QString</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pView,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SLOT</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>SwitchShader<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QString</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">connect(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pScene,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SIGNAL</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>SetLightPos<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QVector3D</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pView,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SLOT</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>SetLightPos<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QVector3D</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">setCentralWidget(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pView</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">setWindowTitle(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">tr(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">"Light</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">for</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">shader"</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">resize(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">640</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">360</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">}</span></span></span>
首先在我们创建了一个QWidget,然后调用makeCurrent()成员函数,其实意思是让它的rendercontext设为当前的rendercontext。随后建立的是OpenGLView,这个OpenGLView是来自于QGraphicsView的,它的初始化和其祖先的并无二致,随后一句非常重要:setViewport(),它的作用是将QGLWidget设置为OpenGLView的viewport,这样的话背景的rendercontext不再是rastercontext而是OpenGLcontext了,否则场景的背景还是需要用CPU渲染的,效率低下。接着是两段建立连接的代码。最后设置的是窗口大小和标题什么的,一开始还是非常简单的。
接下来我们看看OpenGLView是怎么定义的:
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">class</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">OpenGLView</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">public</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGraphicsView</span><span style="color: rgb(0, 0, 0);">,</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">protected</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QOpenGLFunctions</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">{</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 128);">Q_OBJECT</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">public</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">OpenGLView</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QWidget</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pParent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">0</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">virtual</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">~</span><span style="color: rgb(0, 0, 0);">OpenGLView</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">setScene(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">GraphicsScene</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pScene<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">public</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">slots</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">bool</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">SwitchShader(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QString</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span>shaderFileName<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">SetLightPos(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QVector3D</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span>lightPos<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QVector3D</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">protected</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">resizeEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QResizeEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">mousePressEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QMouseEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">mouseReleaseEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QMouseEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">mouseMoveEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QMouseEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">wheelEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QWheelEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">drawBackground</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QPainter</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pPainter<span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QRectF</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span>rect<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">private</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">InitGL(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">ResizeGL(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">int</span><span style="color: rgb(192, 192, 192);"> </span>width<span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">int</span><span style="color: rgb(192, 192, 192);"> </span>height<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">PaintGL(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">DrawAxis(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">bool</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">SetupShaders(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">Camera</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 0);">m_Camera</span><span style="color: rgb(0, 0, 0);">;</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">Format3DS</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 0);">m_3DS</span><span style="color: rgb(0, 0, 0);">;</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(192, 192, 192);"> </span></span></span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span style="color: rgb(0, 128, 0);">着色器</span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">QOpenGLShader</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 0);">m_pVertexShader</span><span style="color: rgb(0, 0, 0);">;</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">QOpenGLShaderProgram</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 0);">m_pShaderProgram</span><span style="color: rgb(0, 0, 0);">;</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">};</span></span></span>
这里我们在它的成员中添加了一个摄像机,一个3ds模型实例,还有一个顶点着色器和着色器程序类。在上次的博客中讲到了在这种情况最好使用类指针而不是类成员作为数据成员,这里我索性把着色器程序类也做成了指针成员了。
由于OpenGLView类实现比较长,这里我着重说一下其中的几个函数。下面是drawBackground()函数的实现:
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">OpenGLView</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">drawBackground</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QPainter</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pPainter,</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QRectF</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">{</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pPainter->beginNativePainting(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">glPushAttrib(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">GL_ALL_ATTRIB_BITS</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">InitGL(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">ResizeGL(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pPainter->device(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)->width(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pPainter->device(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)->height(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">PaintGL(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">glPopAttrib(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pPainter->endNativePainting(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">}</span></span></span>
为什么选择drawBackground()?因为我们想要得到一种效果,OpenGL在底层绘制,上面绘制控件,其实自从QPainter有了beginNativePainting()和endNativePainting()这两个函数,我们就可以进行纯OpenGL的绘制了。这里还要注意的是,因为绘制控件也是使用OpenGL的context,这样简单调用会让OpenGL的状态混乱,所以需要将各种状态通过glPushAttrib(GL_ALL_ATTRIB_BITS);保存起来,然后初始化我们的OpenGL状态,以及绘图,最后记得glPopAttrib();还原所有状态供2D绘制。这里就不像以往的GLWidget套路了,因为GLWidget里面的initializeGL()函数只调用一次,paintGL()函数调用多次,但是在这里,只要有刷新的消息(通过update()或repaint()触发),就必须调用InitGL()函数来进行OpenGL状态的设置,否则先前设置的所有状态都消失了。
接下来看看ResizeEvent()函数:
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">OpenGLView</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">resizeEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QResizeEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pEvent</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">{</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">scene(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)->setSceneRect(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">0.0</span><span style="color: rgb(0, 0, 0);">,</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 128);">0.0</span><span style="color: rgb(0, 0, 0);">,</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pEvent->size(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">).width(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pEvent->size(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">).height(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pEvent->accept(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">}</span></span></span>
这里由于我们已经设置了scene为GraphicsScene类的实例指针,因此scene()是非空的,我们将场景限制为view的大小,这样可以避免一些刷新的问题。
而paintGL()函数也是相当的简单。
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">OpenGLView</span><span style="color: rgb(0, 0, 0);">::PaintGL(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">{</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">glClear(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">GL_COLOR_BUFFER_BIT</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">|</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 128);">GL_DEPTH_BUFFER_BIT</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">glPushMatrix(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 0);">m_Camera</span><span style="color: rgb(0, 0, 0);">.Apply(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 0);">m_3DS</span><span style="color: rgb(0, 0, 0);">.RenderGL(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">if</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>g_ShowAxis<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">DrawAxis(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">glPopMatrix(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">}</span></span></span>
接着我向大家介绍一下GraphicsScene类:
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">class</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">GraphicsScene</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">public</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGraphicsScene</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">{</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 128);">Q_OBJECT</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">public</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">GraphicsScene</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QObject</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pParent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">0</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">SetCamera(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">Camera</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pCamera<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">signals</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">SwitchShader(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QString</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span>shaderFileName<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">SetLightPos(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QVector3D</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span>pos<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">protected</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">mousePressEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGraphicsSceneMouseEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">mouseMoveEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGraphicsSceneMouseEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">mouseReleaseEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGraphicsSceneMouseEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">wheelEvent</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QGraphicsSceneWheelEvent</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span>pEvent<span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">private</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">slots</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">Feedback(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">private</span><span style="color: rgb(0, 0, 0);">:</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(192, 192, 192);"> </span></span></span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span style="color: rgb(0, 128, 0);">鼠标事件需要</span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">QPointF</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 0);">m_LastPos</span><span style="color: rgb(0, 0, 0);">;</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">Camera</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 0);">m_pCamera</span><span style="color: rgb(0, 0, 0);">;</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">};</span></span></span>
这里的GraphicsScene类保存的是来自view的Camera和一些信号以及事件的处理。在实现上也说一下它的构造函数吧。
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">GraphicsScene</span><span style="color: rgb(0, 0, 0);">::GraphicsScene(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QObject</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pParent</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">):</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">QGraphicsScene</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pParent</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 0);">m_pCamera</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">Q_NULLPTR</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">{</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">ClickableTextItem</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pTextItem</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">new</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">ClickableTextItem</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">Q_NULLPTR</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pTextItem->setPos(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">10.0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">10.0</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pTextItem->setHtml(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">tr(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">"<font</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">color=white>"</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 128, 0);">"Made</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">By</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">Jiangcaiyang<br>"</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 128, 0);">"Created</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">in</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">September<br>"</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 128, 0);">"Click</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">for</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 128, 0);">feedback."</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 128, 0);">"</font>"</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">connect(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pTextItem,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SIGNAL</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>Clicked<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">this</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SLOT</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>Feedback<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">addItem(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pTextItem</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">ShaderOptionDialog</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pDialog</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">new</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">ShaderOptionDialog</span><span style="color: rgb(0, 0, 0);">;</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">connect(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pDialog,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SIGNAL</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>SwitchShader<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QString</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">this</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SIGNAL</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>SwitchShader<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QString</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">connect(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pDialog,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SIGNAL</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>SetLightPos<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QVector3D</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">),</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 128, 0);">this</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">SIGNAL</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span>SetLightPos<span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QVector3D</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(128, 0, 128);">QGraphicsProxyWidget</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pProxy</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">addWidget(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">pDialog,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">Qt</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(128, 0, 128);">Window</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">Qt</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(128, 0, 128);">WindowTitleHint</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="color: rgb(192, 192, 192);"> </span><span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">pProxy->setPos(</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">100</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 128);">200</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">);</span></span></span>
<span style="font-family: 'DejaVu Sans Mono', monospace;"><span lang="en-US"><span style="color: rgb(0, 0, 0);">}</span></span></span>
我们创建了一个ClickableTextItem类,它继承于QGraphicsTextItem,它被摆在左上角,显示三排可以点击的文字。随后添加了一个对话框,设置信号和槽完毕后就用代理放入场景中了。整个过程也是非常简单的。
我测试了下,整个程序在我的Ubuntu13.04下能够正常运行。只是由于显卡不同(Ubuntu下较难支持nvidia显卡,使用的是intel集显),specularOpt效果出不来。
FROM: http://blog.csdn.net/gamesdev/article/details/12073065?utm_source=tuicool&utm_medium=referral