Beispiel #1
0
def set_format() -> QSurfaceFormat:
    surface_format = QSurfaceFormat()
    surface_format.setVersion(4, 1)
    surface_format.setProfile(QSurfaceFormat.CoreProfile)
    surface_format.setSamples(4)
    QSurfaceFormat.setDefaultFormat(surface_format)

    return surface_format
Beispiel #2
0
def get_default_format(*, gles: bool):
    fmt = QSurfaceFormat()
    fmt.setVersion(2, 0)
    fmt.setProfile(QSurfaceFormat.CoreProfile)
    fmt.setRenderableType(
        QSurfaceFormat.OpenGLES if gles else QSurfaceFormat.OpenGL)
    fmt.setDepthBufferSize(24)
    fmt.setStencilBufferSize(8)
    fmt.setOption(QSurfaceFormat.DebugContext)
    return fmt
Beispiel #3
0
class GLWidget(QOpenGLWidget):
    def __init__(self, parent=None):
        QOpenGLWidget.__init__(self, parent)
        self.gl_format = QSurfaceFormat()
        self.gl_format.setRenderableType(QSurfaceFormat.OpenGL)
        self.gl_format.setProfile(QSurfaceFormat.CoreProfile)
        self.gl_format.setVersion(4, 1)
        self.setFormat(self.gl_format)
        self.mouse_x = 0
        self.mouse_y = 0
        self.mouse_init = False
        self.mouse_pressed = False

    def paintGL(self):
        display_loop(c_double(0.0), c_uint(self.defaultFramebufferObject()))

    def resizeGL(self, width, height):
        width = c_double(self.size().width())
        height = c_double(self.size().height())
        dpi_ratio = c_double(self.devicePixelRatio())
        resize_window(width, height, dpi_ratio)

    def initializeGL(self):
        width = c_double(self.size().width())
        height = c_double(self.size().height())
        dpi_ratio = c_double(self.devicePixelRatio())
        load_gl_symbol()
        init_gl(width, height, dpi_ratio)
        print_gl_info()
        init_scene(width, height, dpi_ratio)

    def mousePressEvent(self, ev):
        if ev.button() == Qt.LeftButton:
            self.mouse_pressed = True

    def mouseReleaseEvent(self, ev):
        if ev.button() == Qt.LeftButton:
            self.mouse_pressed = False
            self.mouse_init = False

    def mouseMoveEvent(self, ev):
        pos = ev.localPos()
        if self.mouse_pressed:
            if not self.mouse_init:
                self.mouse_x = pos.x()
                self.mouse_y = pos.y()
                self.mouse_init = True
            else:
                dx = self.mouse_x - pos.x()
                dy = self.mouse_y - pos.y()
                self.mouse_x = pos.x()
                self.mouse_y = pos.y()
                handle_mouse(c_float(dx), c_float(dy), c_float(0.001))
                self.update()
 def defaultFormat(stereo_capable):
     fmt = QSurfaceFormat()
     fmt.setRenderableType(QSurfaceFormat.OpenGL)
     fmt.setVersion(3, 2)
     fmt.setProfile(QSurfaceFormat.CoreProfile)
     fmt.setSwapBehavior(QSurfaceFormat.DoubleBuffer)
     fmt.setRedBufferSize(8)
     fmt.setGreenBufferSize(8)
     fmt.setBlueBufferSize(8)
     fmt.setDepthBufferSize(8)
     fmt.setAlphaBufferSize(8)
     fmt.setStencilBufferSize(0)
     fmt.setStereo(stereo_capable)
     fmt.setSamples(0)
     return fmt
Beispiel #5
0
def defaultVtkFormat(stereo_capable):
    """ Po prostu skopiowałem to z https://github.com/Kitware/VTK/blob/master/GUISupport/Qt/QVTKRenderWindowAdapter.cxx
     i działa poprawnie bufor głębokości
    """
    fmt = QSurfaceFormat()
    fmt.setRenderableType(QSurfaceFormat.OpenGL)
    fmt.setVersion(3, 2)
    fmt.setProfile(QSurfaceFormat.CoreProfile)
    fmt.setSwapBehavior(QSurfaceFormat.DoubleBuffer)
    fmt.setRedBufferSize(8)
    fmt.setGreenBufferSize(8)
    fmt.setBlueBufferSize(8)
    fmt.setDepthBufferSize(8)
    fmt.setAlphaBufferSize(8)
    fmt.setStencilBufferSize(0)
    fmt.setStereo(stereo_capable)
    fmt.setSamples(0)
    return fmt
    def defaultSurfaceFormat(stereo_capable):
        """ Ported from: https://github.com/Kitware/VTK/blob/master/GUISupport/Qt/QVTKRenderWindowAdapter.cxx
        """
        fmt = QSurfaceFormat()
        fmt.setRenderableType(QSurfaceFormat.OpenGL)
        fmt.setVersion(3, 2)
        fmt.setProfile(QSurfaceFormat.CoreProfile)
        fmt.setSwapBehavior(QSurfaceFormat.DoubleBuffer)
        fmt.setRedBufferSize(8)
        fmt.setGreenBufferSize(8)
        fmt.setBlueBufferSize(8)
        fmt.setDepthBufferSize(8)
        fmt.setAlphaBufferSize(8)
        fmt.setStencilBufferSize(0)
        fmt.setStereo(stereo_capable)
        fmt.setSamples(0)

        return fmt
        self.imageView.setPixmap(QPixmap(qimage[0]).scaledToWidth(400))
        self.glWidget.updateImage(qimage[0])

    def showSaveDialog(self):
        filename = QFileDialog.getSaveFileName(self, "Save file", "", ".STL")
        print(filename[0])
        self.glWidget.generateSTL(filename[0] + filename[1])


if __name__ == '__main__':
    app = QApplication(sys.argv)

    fmt = QSurfaceFormat()
    if "--multisample" in QCoreApplication.arguments():
        fmt.setSamples(4)
    if "--coreprofile" in QCoreApplication.arguments():
        fmt.setVersion(3, 2)
        fmt.setProfile(QSurfaceFormat.CoreProfile)
    QSurfaceFormat.setDefaultFormat(fmt)

    mainWindow = Window()
    if "--transparent" in QCoreApplication.arguments():
        mainWindow.setAttribute(Qt.WA_TranslucentBackground)
        mainWindow.setAttribute(Qt.WA_NoSystemBackground, False)

    mainWindow.resize(mainWindow.sizeHint())
    mainWindow.show()

    res = app.exec_()
    sys.exit(res)
Beispiel #8
0
            self.setXRotation(self.xRot + 8 * dy)
            self.setYRotation(self.yRot + 8 * dx)
        elif event.buttons() & Qt.RightButton:
            self.setXRotation(self.xRot + 8 * dy)
            self.setZRotation(self.zRot + 8 * dx)

        self.lastPos = QPoint(event.pos())

if __name__ == '__main__':
    app = QApplication(sys.argv)

    fmt = QSurfaceFormat()
    fmt.setDepthBufferSize(24)
    if "--multisample" in QCoreApplication.arguments():
        fmt.setSamples(4)
    if "--coreprofile" in QCoreApplication.arguments():
        fmt.setVersion(3, 2)
        fmt.setProfile(QSurfaceFormat.CoreProfile)
    QSurfaceFormat.setDefaultFormat(fmt)

    mainWindow = Window()
    if "--transparent" in QCoreApplication.arguments():
        mainWindow.setAttribute(Qt.WA_TranslucentBackground)
        mainWindow.setAttribute(Qt.WA_NoSystemBackground, False)

    mainWindow.resize(mainWindow.sizeHint())
    mainWindow.show()

    res = app.exec_()
    sys.exit(res)
Beispiel #9
0
        self.__slot_channels()

    def __slot_checkerboard(self):
        self._viewport.set_colors(True, None, None)

    def __slot_solid_color(self):
        color = QColorDialog.getColor(Qt.black, self,
                                      "Choose background color")
        if color.isValid():
            self._viewport.set_colors(False, color, color)


if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sys.argv)
    # must be called before the OpenGLWidget or its parent window gets shown
    # set default OpenGL surface format
    glformat = QSurfaceFormat()
    glformat.setDepthBufferSize(24)
    glformat.setStencilBufferSize(8)
    glformat.setVersion(3, 1)
    glformat.setProfile(QSurfaceFormat.CoreProfile)
    QSurfaceFormat.setDefaultFormat(glformat)
    # Create and show the form
    form = TViewerWindow()
    form.show()
    sample = join(dirname(realpath(__file__)), "rgba.tga")
    form.view(sample)
    # Run the main Qt loop
    sys.exit(app.exec_())
Beispiel #10
0
class GLWin(QWindow):
    requestRender = Signal()

    def __init__(self, parent=None):
        QWindow.__init__(self, parent)
        self.setSurfaceType(QSurface.OpenGLSurface)
        self.gl_format = QSurfaceFormat()
        self.gl_format.setRenderableType(QSurfaceFormat.OpenGL)
        self.gl_format.setProfile(QSurfaceFormat.CoreProfile)
        self.gl_format.setVersion(4, 1)
        self.setFormat(self.gl_format)
        if self.supportsOpenGL():
            print("OpenGL supported !")
        self.mouse_x = 0
        self.mouse_y = 0
        self.mouse_init = False
        self.animating = False
        self.requestRender.connect(self.requestUpdate)
        # self.setMouseGrabEnabled(True)
        self.mouse_pressed = False

    def mousePressEvent(self, ev):
        if ev.button() == Qt.LeftButton:
            self.mouse_pressed = True

    def mouseReleaseEvent(self, ev):
        if ev.button() == Qt.LeftButton:
            self.mouse_pressed = False
            self.mouse_init = False

    def mouseMoveEvent(self, ev):
        pos = ev.localPos()
        if self.mouse_pressed:
            if not self.mouse_init:
                self.mouse_x = pos.x()
                self.mouse_y = pos.y()
                self.mouse_init = True
            else:
                dx = self.mouse_x - pos.x()
                dy = self.mouse_y - pos.y()
                self.mouse_x = pos.x()
                self.mouse_y = pos.y()
                handle_mouse(c_float(dx), c_float(dy), c_float(0.001))

    def start(self):
        self.animating = True
        self.renderLater()

    def render(self):
        display_loop(c_double(0.0))

    def renderLater(self):
        self.requestRender.emit()

    def renderNow(self):
        if not self.isExposed():
            return
        self.render()
        self.gl_context.swapBuffers(self)
        if self.animating:
            self.renderLater()

    def init_context(self):
        self.gl_context = QOpenGLContext(self)
        self.gl_context.setFormat(self.gl_format)
        if self.gl_context.create():
            print("Context created !")
        if self.gl_context.makeCurrent(self):
            print("Context made current !")

    def init_scene(self):
        width = c_double(self.size().width())
        height = c_double(self.size().height())
        dpi_ratio = c_double(self.devicePixelRatio())

        load_gl_symbol()
        init_gl(width, height, dpi_ratio)
        print_gl_info()
        init_scene(width, height, dpi_ratio)

    def resize(self):
        if self.isExposed():
            width = c_double(self.size().width())
            height = c_double(self.size().height())
            dpi_ratio = c_double(self.devicePixelRatio())
            resize_window(width, height, dpi_ratio)

    def event(self, ev):
        if ev.type() == QEvent.UpdateRequest:
            self.renderNow()
            return True
        else:
            return super().event(ev)

    def exposeEvent(self, ev):
        self.renderLater()

    def resizeEvent(self, ev):
        self.resize()
        self.renderLater()