| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the demonstration applications of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:BSD$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** BSD License Usage | 
| 18 | ** Alternatively, you may use this file under the terms of the BSD license | 
| 19 | ** as follows: | 
| 20 | ** | 
| 21 | ** "Redistribution and use in source and binary forms, with or without | 
| 22 | ** modification, are permitted provided that the following conditions are | 
| 23 | ** met: | 
| 24 | **   * Redistributions of source code must retain the above copyright | 
| 25 | **     notice, this list of conditions and the following disclaimer. | 
| 26 | **   * Redistributions in binary form must reproduce the above copyright | 
| 27 | **     notice, this list of conditions and the following disclaimer in | 
| 28 | **     the documentation and/or other materials provided with the | 
| 29 | **     distribution. | 
| 30 | **   * Neither the name of The Qt Company Ltd nor the names of its | 
| 31 | **     contributors may be used to endorse or promote products derived | 
| 32 | **     from this software without specific prior written permission. | 
| 33 | ** | 
| 34 | ** | 
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
| 46 | ** | 
| 47 | ** $QT_END_LICENSE$ | 
| 48 | ** | 
| 49 | ****************************************************************************/ | 
| 50 |  | 
| 51 | #include "glextensions.h" | 
| 52 | #include "scene.h" | 
| 53 |  | 
| 54 | #include <QGLWidget> | 
| 55 | #include <QtWidgets> | 
| 56 |  | 
| 57 | class GraphicsView : public QGraphicsView | 
| 58 | { | 
| 59 | public: | 
| 60 |     GraphicsView() | 
| 61 |     { | 
| 62 |         setWindowTitle(tr(s: "Boxes" )); | 
| 63 |         setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); | 
| 64 |         //setRenderHints(QPainter::SmoothPixmapTransform); | 
| 65 |     } | 
| 66 |  | 
| 67 | protected: | 
| 68 |     void resizeEvent(QResizeEvent *event) override { | 
| 69 |         if (scene()) | 
| 70 |             scene()->setSceneRect(QRect(QPoint(0, 0), event->size())); | 
| 71 |         QGraphicsView::resizeEvent(event); | 
| 72 |     } | 
| 73 | }; | 
| 74 |  | 
| 75 | inline bool matchString(const char *extensionString, const char *subString) | 
| 76 | { | 
| 77 |     int subStringLength = strlen(s: subString); | 
| 78 |     return (strncmp(s1: extensionString, s2: subString, n: subStringLength) == 0) | 
| 79 |         && ((extensionString[subStringLength] == ' ') || (extensionString[subStringLength] == '\0')); | 
| 80 | } | 
| 81 |  | 
| 82 | bool necessaryExtensionsSupported() | 
| 83 | { | 
| 84 |     const char *extensionString = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)); | 
| 85 |     const char *p = extensionString; | 
| 86 |  | 
| 87 |     const int GL_EXT_FBO = 1; | 
| 88 |     const int GL_ARB_VS = 2; | 
| 89 |     const int GL_ARB_FS = 4; | 
| 90 |     const int GL_ARB_SO = 8; | 
| 91 |     int extensions = 0; | 
| 92 |  | 
| 93 |     while (*p) { | 
| 94 |         if (matchString(extensionString: p, subString: "GL_EXT_framebuffer_object" )) | 
| 95 |             extensions |= GL_EXT_FBO; | 
| 96 |         else if (matchString(extensionString: p, subString: "GL_ARB_vertex_shader" )) | 
| 97 |             extensions |= GL_ARB_VS; | 
| 98 |         else if (matchString(extensionString: p, subString: "GL_ARB_fragment_shader" )) | 
| 99 |             extensions |= GL_ARB_FS; | 
| 100 |         else if (matchString(extensionString: p, subString: "GL_ARB_shader_objects" )) | 
| 101 |             extensions |= GL_ARB_SO; | 
| 102 |         while ((*p != ' ') && (*p != '\0')) | 
| 103 |             ++p; | 
| 104 |         if (*p == ' ') | 
| 105 |             ++p; | 
| 106 |     } | 
| 107 |     return (extensions == 15); | 
| 108 | } | 
| 109 |  | 
| 110 | int main(int argc, char **argv) | 
| 111 | { | 
| 112 |     QApplication app(argc, argv); | 
| 113 |  | 
| 114 |     if ((QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_Version_1_5) == 0) { | 
| 115 |         QMessageBox::critical(parent: nullptr, title: "OpenGL features missing" , | 
| 116 |             text: "OpenGL version 1.5 or higher is required to run this demo.\n"  | 
| 117 |             "The program will now exit." ); | 
| 118 |         return -1; | 
| 119 |     } | 
| 120 |  | 
| 121 |     int maxTextureSize = 1024; | 
| 122 |     QGLWidget *widget = new QGLWidget(QGLFormat(QGL::SampleBuffers)); | 
| 123 |     widget->makeCurrent(); | 
| 124 |  | 
| 125 |     if (!necessaryExtensionsSupported()) { | 
| 126 |         QMessageBox::critical(parent: nullptr, title: "OpenGL features missing" , | 
| 127 |             text: "The OpenGL extensions required to run this demo are missing.\n"  | 
| 128 |             "The program will now exit." ); | 
| 129 |         delete widget; | 
| 130 |         return -2; | 
| 131 |     } | 
| 132 |  | 
| 133 |     // Check if all the necessary functions are resolved. | 
| 134 |     if (!getGLExtensionFunctions().resolve(context: widget->context())) { | 
| 135 |         QMessageBox::critical(parent: nullptr, title: "OpenGL features missing" , | 
| 136 |             text: "Failed to resolve OpenGL functions required to run this demo.\n"  | 
| 137 |             "The program will now exit." ); | 
| 138 |         delete widget; | 
| 139 |         return -3; | 
| 140 |     } | 
| 141 |  | 
| 142 |     // TODO: Make conditional for final release | 
| 143 |     QMessageBox::information(parent: nullptr, title: "For your information" , | 
| 144 |         text: "This demo can be GPU and CPU intensive and may\n"  | 
| 145 |         "work poorly or not at all on your system." ); | 
| 146 |  | 
| 147 |     widget->makeCurrent(); // The current context must be set before calling Scene's constructor | 
| 148 |     Scene scene(1024, 768, maxTextureSize); | 
| 149 |     GraphicsView view; | 
| 150 |     view.setViewport(widget); | 
| 151 |     view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate); | 
| 152 |     view.setScene(&scene); | 
| 153 |     view.show(); | 
| 154 |  | 
| 155 |     return app.exec(); | 
| 156 | } | 
| 157 |  | 
| 158 |  |