Advertisement
Guest User

Qt text fail on QSurface with QPainter

a guest
Sep 29th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.43 KB | None | 0 0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the documentation of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** You may use this file under the terms of the BSD license as follows:
  10. **
  11. ** "Redistribution and use in source and binary forms, with or without
  12. ** modification, are permitted provided that the following conditions are
  13. ** met:
  14. **   * Redistributions of source code must retain the above copyright
  15. **     notice, this list of conditions and the following disclaimer.
  16. **   * Redistributions in binary form must reproduce the above copyright
  17. **     notice, this list of conditions and the following disclaimer in
  18. **     the documentation and/or other materials provided with the
  19. **     distribution.
  20. **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  21. **     of its contributors may be used to endorse or promote products derived
  22. **     from this software without specific prior written permission.
  23. **
  24. **
  25. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40.  
  41. #include <QtGui/QWindow>
  42. #include <QtGui/QOpenGLFunctions>
  43.  
  44. class QPainter;
  45. class QOpenGLContext;
  46. class QOpenGLPaintDevice;
  47.  
  48. class OpenGLWindow : public QWindow, protected QOpenGLFunctions
  49. {
  50.     Q_OBJECT
  51. public:
  52.     explicit OpenGLWindow(QWindow *parent = 0);
  53.     ~OpenGLWindow();
  54.  
  55.     virtual void render(QPainter *painter);
  56.     virtual void render();
  57.     virtual void initialize();
  58.  
  59.     void setAnimating(bool animating);
  60. public slots:
  61.     void renderLater();
  62.     void renderNow();
  63.  
  64. protected:
  65.     bool event(QEvent *event);
  66.  
  67.     void exposeEvent(QExposeEvent *event);
  68.  
  69. public:
  70.     bool m_update_pending;
  71.     bool m_animating;
  72.  
  73.     QOpenGLContext *m_context;
  74.     QOpenGLPaintDevice *m_device;
  75. };
  76. /****************************************************************************
  77. **
  78. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  79. ** Contact: http://www.qt-project.org/legal
  80. **
  81. ** This file is part of the documentation of the Qt Toolkit.
  82. **
  83. ** $QT_BEGIN_LICENSE:BSD$
  84. ** You may use this file under the terms of the BSD license as follows:
  85. **
  86. ** "Redistribution and use in source and binary forms, with or without
  87. ** modification, are permitted provided that the following conditions are
  88. ** met:
  89. **   * Redistributions of source code must retain the above copyright
  90. **     notice, this list of conditions and the following disclaimer.
  91. **   * Redistributions in binary form must reproduce the above copyright
  92. **     notice, this list of conditions and the following disclaimer in
  93. **     the documentation and/or other materials provided with the
  94. **     distribution.
  95. **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  96. **     of its contributors may be used to endorse or promote products derived
  97. **     from this software without specific prior written permission.
  98. **
  99. **
  100. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  101. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  102. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  103. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  104. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  105. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  106. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  107. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  108. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  109. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  110. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  111. **
  112. ** $QT_END_LICENSE$
  113. **
  114. ****************************************************************************/
  115.  
  116. #include "openglwindow.h"
  117.  
  118. #include <QtGui/QGuiApplication>
  119. #include <QtGui/QMatrix4x4>
  120. #include <QtGui/QOpenGLShaderProgram>
  121. #include <QtGui/QScreen>
  122.  
  123. #include <QtCore/qmath.h>
  124. #include <QDebug>
  125. #include <QTime>
  126. #include <QtGui/QPainter>
  127. #include <QOpenGLPaintDevice>
  128. class TriangleWindow : public OpenGLWindow
  129. {
  130. public:
  131.     TriangleWindow();
  132.  
  133.     void initialize();
  134.     void render();
  135.  
  136. private:
  137.     GLuint loadShader(GLenum type, const char *source);
  138.  
  139.     GLuint m_posAttr;
  140.     GLuint m_colAttr;
  141.     GLuint m_matrixUniform;
  142.  
  143.     QOpenGLShaderProgram *m_program;
  144.     int m_frame;
  145. };
  146.  
  147. TriangleWindow::TriangleWindow()
  148.     : m_program(0)
  149.     , m_frame(0)
  150. {
  151. }
  152.  
  153. int main(int argc, char **argv)
  154. {
  155.     QGuiApplication app(argc, argv);
  156.  
  157.     QSurfaceFormat format;
  158.     format.setSamples(16);
  159.  
  160.     TriangleWindow window;
  161.     window.setFormat(format);
  162.     window.resize(640, 480);
  163.     window.show();
  164.  
  165.     window.setAnimating(true);
  166.  
  167.     return app.exec();
  168. }
  169.  
  170. static const char *vertexShaderSource =
  171.     "attribute highp vec4 posAttr;\n"
  172.     "attribute lowp vec4 colAttr;\n"
  173.     "varying lowp vec4 col;\n"
  174.     "uniform highp mat4 matrix;\n"
  175.     "void main() {\n"
  176.     "   col = colAttr;\n"
  177.     "   gl_Position = matrix * posAttr;\n"
  178.     "}\n";
  179.  
  180. static const char *fragmentShaderSource =
  181.     "varying lowp vec4 col;\n"
  182.     "void main() {\n"
  183.     "   gl_FragColor = col;\n"
  184.     "}\n";
  185.  
  186. GLuint TriangleWindow::loadShader(GLenum type, const char *source)
  187. {
  188.     GLuint shader = glCreateShader(type);
  189.     glShaderSource(shader, 1, &source, 0);
  190.     glCompileShader(shader);
  191.     return shader;
  192. }
  193. QTime time;
  194. void TriangleWindow::initialize()
  195. {
  196.     m_program = new QOpenGLShaderProgram(this);
  197.     m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
  198.     m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
  199.     m_program->link();
  200.     m_posAttr = m_program->attributeLocation("posAttr");
  201.     m_colAttr = m_program->attributeLocation("colAttr");
  202.     m_matrixUniform = m_program->uniformLocation("matrix");
  203.     time.start();
  204. }
  205. int frames = 0;
  206. void TriangleWindow::render()
  207. {
  208.     const qreal retinaScale = devicePixelRatio();
  209.     glViewport(0, 0, width() * retinaScale, height() * retinaScale);
  210.  
  211.     glClear(GL_COLOR_BUFFER_BIT);
  212.  
  213.     m_program->bind();
  214.  
  215.     QMatrix4x4 matrix;
  216.     matrix.perspective(60, 4.0/3.0, 0.1, 100.0);
  217.     matrix.translate(0, 0, -2);
  218.     matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0);
  219.  
  220.     m_program->setUniformValue(m_matrixUniform, matrix);
  221.  
  222.     GLfloat vertices[] = {
  223.         0.0f, 0.707f,
  224.         -0.5f, -0.5f,
  225.         0.5f, -0.5f
  226.     };
  227.  
  228.     GLfloat colors[] = {
  229.         1.0f, 0.0f, 0.0f,
  230.         0.0f, 1.0f, 0.0f,
  231.         0.0f, 0.0f, 1.0f
  232.     };
  233.  
  234.     glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  235.     glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
  236.  
  237.     glEnableVertexAttribArray(0);
  238.     glEnableVertexAttribArray(1);
  239.  
  240.     glDrawArrays(GL_TRIANGLES, 0, 3);
  241.  
  242.     glDisableVertexAttribArray(1);
  243.     glDisableVertexAttribArray(0);
  244.  
  245.     m_program->release();
  246.  
  247.     ++m_frame;
  248.     frames++;
  249.     if(frames == 100)
  250.     {
  251.       this->setTitle(QString::number((1.0f*time.msec())/100*1000));
  252.       frames = 0;
  253.     }
  254.     if (!m_device)
  255.         m_device = new QOpenGLPaintDevice;
  256.     m_device->setSize(size());
  257.     QPainter painter(m_device);
  258.     QPen paint;
  259.     paint.setColor(QColor::fromRgb(255,255,255));
  260.     QFont font("Comic Sans MS");
  261.     font.setPointSize(14);
  262.     painter.setPen(paint);
  263.     painter.setFont(font);
  264.     painter.drawText(0,0,100,100,0,QString("fps:")+QString::number((1.0f*time.msec())/100*1000));
  265. }
  266. /****************************************************************************
  267. **
  268. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  269. ** Contact: http://www.qt-project.org/legal
  270. **
  271. ** This file is part of the documentation of the Qt Toolkit.
  272. **
  273. ** $QT_BEGIN_LICENSE:BSD$
  274. ** You may use this file under the terms of the BSD license as follows:
  275. **
  276. ** "Redistribution and use in source and binary forms, with or without
  277. ** modification, are permitted provided that the following conditions are
  278. ** met:
  279. **   * Redistributions of source code must retain the above copyright
  280. **     notice, this list of conditions and the following disclaimer.
  281. **   * Redistributions in binary form must reproduce the above copyright
  282. **     notice, this list of conditions and the following disclaimer in
  283. **     the documentation and/or other materials provided with the
  284. **     distribution.
  285. **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  286. **     of its contributors may be used to endorse or promote products derived
  287. **     from this software without specific prior written permission.
  288. **
  289. **
  290. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  291. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  292. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  293. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  294. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  295. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  296. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  297. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  298. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  299. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  300. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  301. **
  302. ** $QT_END_LICENSE$
  303. **
  304. ****************************************************************************/
  305.  
  306. #include "openglwindow.h"
  307.  
  308. #include <QtCore/QCoreApplication>
  309.  
  310. #include <QtGui/QOpenGLContext>
  311. #include <QtGui/QOpenGLPaintDevice>
  312. #include <QtGui/QPainter>
  313.  
  314. OpenGLWindow::OpenGLWindow(QWindow *parent)
  315.     : QWindow(parent)
  316.     , m_update_pending(false)
  317.     , m_animating(false)
  318.     , m_context(0)
  319.     , m_device(0)
  320. {
  321.     setSurfaceType(QWindow::OpenGLSurface);
  322. }
  323.  
  324. OpenGLWindow::~OpenGLWindow()
  325. {
  326.     delete m_device;
  327. }
  328. void OpenGLWindow::render(QPainter *painter)
  329. {
  330.     Q_UNUSED(painter);
  331. }
  332.  
  333. void OpenGLWindow::initialize()
  334. {
  335. }
  336.  
  337. void OpenGLWindow::render()
  338. {
  339.     if (!m_device)
  340.         m_device = new QOpenGLPaintDevice;
  341.  
  342.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  343.  
  344.     m_device->setSize(size());
  345.  
  346.     QPainter painter(m_device);
  347.     render(&painter);
  348. }
  349.  
  350. void OpenGLWindow::renderLater()
  351. {
  352.     if (!m_update_pending) {
  353.         m_update_pending = true;
  354.         QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
  355.     }
  356. }
  357.  
  358. bool OpenGLWindow::event(QEvent *event)
  359. {
  360.     switch (event->type()) {
  361.     case QEvent::UpdateRequest:
  362.         m_update_pending = false;
  363.         renderNow();
  364.         return true;
  365.     default:
  366.         return QWindow::event(event);
  367.     }
  368. }
  369.  
  370. void OpenGLWindow::exposeEvent(QExposeEvent *event)
  371. {
  372.     Q_UNUSED(event);
  373.  
  374.     if (isExposed())
  375.         renderNow();
  376. }
  377.  
  378. void OpenGLWindow::renderNow()
  379. {
  380.     if (!isExposed())
  381.         return;
  382.  
  383.     bool needsInitialize = false;
  384.  
  385.     if (!m_context) {
  386.         m_context = new QOpenGLContext(this);
  387.         m_context->setFormat(requestedFormat());
  388.         m_context->create();
  389.  
  390.         needsInitialize = true;
  391.     }
  392.  
  393.     m_context->makeCurrent(this);
  394.  
  395.     if (needsInitialize) {
  396.         initializeOpenGLFunctions();
  397.         initialize();
  398.     }
  399.  
  400.     render();
  401.     m_context->swapBuffers(this);
  402.  
  403.     if (m_animating)
  404.         renderLater();
  405. }
  406.  
  407. void OpenGLWindow::setAnimating(bool animating)
  408. {
  409.     m_animating = animating;
  410.  
  411.     if (animating)
  412.         renderLater();
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement