Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. ModernGL with QOpenGLWidget isn't rendering

ModernGL with QOpenGLWidget isn't rendering

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
8 Posts 5 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Hi all. I am so stumped on why the triangle is not rendering. My shaders are correct, I set up the VBO and VAOs correctly, its just not rendering anything for some reason.

    import sys
    import moderngl as GL
    import numpy
    from PyQt6.QtGui import *
    from PyQt6.QtWidgets import *
    from PyQt6.QtCore import *
    from PyQt6.QtOpenGLWidgets import *
    
    
    class Main(QOpenGLWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
    
        def initializeGL(self):
            # Set up the OpenGL context using moderngl
            self.ctx = GL.create_context()
            self.ctx.clear(0, 0, 0)
    
            # Define shaders with error checking
            vertex_shader_code = '''
            #version 330 core
    
            in vec3 in_vert;
    
            void main() {
                gl_Position = vec4(in_vert, 1.0);
            }
            '''
            fragment_shader_code = '''
            #version 330 core
    
            out vec4 fragColor;
    
            uniform vec4 color;
    
            void main() {
                fragColor = color;
            }
            '''
    
            # Compile shaders and program
            self.program = self.ctx.program(
                vertex_shader=vertex_shader_code,
                fragment_shader=fragment_shader_code
            )
    
            # Check for any shader compilation issues
            if not self.program:
                print("Shader program failed to compile.")
                return
    
            # Define triangle vertices (in normalized device coordinates)
            self.vertices = numpy.array([
                0.0, 0.5, 0.0,  # Top vertex
                -0.5, -0.5, 0.0,  # Bottom-left vertex
                0.5, -0.5, 0.0  # Bottom-right vertex
            ], dtype='f4')
    
            # Create the buffer for the vertices
            self.vbo = self.ctx.buffer(self.vertices)
    
            # Create vertex array object (VAO)
            self.vao = self.ctx.simple_vertex_array(self.program, self.vbo, 'in_vert')
    
            # Set the uniform color (Red in this case)
            self.program['color'].value = (1.0, 1.0, 1.0, 1.0)  # Red
    
        def resizeGL(self, w, h):
            self.ctx.viewport = (0, 0, w, h)
            self.update()
    
        def paintGL(self):
            # Clear the screen
            self.ctx.clear(0.0, 0.0, 0.0)
    
            # Draw the triangle
            self.vao.render()
    
    
    if __name__ == '__main__':
        app = QApplication([])
    
        win = Main()
        win.resize(800, 600)
        win.show()
    
        sys.exit(app.exec())
    
    

    I would like to add that if I remove self.ctx.clear(0.0, 0.0, 0.0) from the paintGL() method, it renders the vertices correctly. I can't do this though (obviously because I need to clear each frame before rendering)

    W 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      From a quick look at the code of QOpenGLWidget, I don't think you can.

      Do you really need QOpenGLWidget ? Maybe QWindow might do what you want.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      ? 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        The thing that is strange here is your context creation. AFAIK, QOpenGLWidget already has a context that will be made current when paintGL is called.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        ? 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          The thing that is strange here is your context creation. AFAIK, QOpenGLWidget already has a context that will be made current when paintGL is called.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #3

          @SGaist How could I prevent QOpenGLWidget from creating its own context? Or is there another widget that can render an OpenGL context in PyQt6?

          All of this works in PyQt5 with QGLWidget.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            From a quick look at the code of QOpenGLWidget, I don't think you can.

            Do you really need QOpenGLWidget ? Maybe QWindow might do what you want.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            ? 1 Reply Last reply
            0
            • SGaistS SGaist

              From a quick look at the code of QOpenGLWidget, I don't think you can.

              Do you really need QOpenGLWidget ? Maybe QWindow might do what you want.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #5

              @SGaist said in ModernGL with QOpenGLWidget isn't rendering:

              Do you really need QOpenGLWidget ? Maybe QWindow might do what you want.

              Any examples for QWindow OpenGL rendering? I don’t absolutely need QOpenGLWidget

              jsulmJ 1 Reply Last reply
              0
              • System has marked this topic as solved on
              • ? A Former User

                @SGaist said in ModernGL with QOpenGLWidget isn't rendering:

                Do you really need QOpenGLWidget ? Maybe QWindow might do what you want.

                Any examples for QWindow OpenGL rendering? I don’t absolutely need QOpenGLWidget

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @ktechhydle If you enter "qt6 QWindow OpenGL" in Google first result is: https://doc.qt.io/qt-6/qtopengl-openglwindow-example.html

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • H Offline
                  H Offline
                  Hexplivyus
                  wrote on last edited by
                  #7

                  Hi,

                  For those looking for a functional answer:

                  import sys
                  import moderngl as GL
                  import numpy
                  from PyQt6.QtWidgets import QApplication
                  from PyQt6.QtOpenGLWidgets import QOpenGLWidget
                  
                  
                  class Main(QOpenGLWidget):
                      def __init__(self, parent=None):
                          super().__init__(parent)
                  
                      def initializeGL(self):
                          # Set up the OpenGL context using moderngl
                          self.ctx = GL.create_context()
                          self.ctx.clear(0, 0, 0)
                  
                          # Define shaders with error checking
                          vertex_shader_code = '''
                          #version 330 core
                  
                          in vec3 in_vert;
                  
                          void main() {
                              gl_Position = vec4(in_vert, 1.0);
                          }
                          '''
                          fragment_shader_code = '''
                          #version 330 core
                  
                          out vec4 fragColor;
                  
                          uniform vec4 color;
                  
                          void main() {
                              fragColor = color;
                          }
                          '''
                  
                          # Compile shaders and program
                          self.program = self.ctx.program(
                              vertex_shader=vertex_shader_code,
                              fragment_shader=fragment_shader_code
                          )
                  
                          # Check for any shader compilation issues
                          if not self.program:
                              print("Shader program failed to compile.")
                              return
                  
                          # Define triangle vertices (in normalized device coordinates)
                          self.vertices = numpy.array([
                              0.0, 0.5, 0.0,  # Top vertex
                              -0.5, -0.5, 0.0,  # Bottom-left vertex
                              0.5, -0.5, 0.0  # Bottom-right vertex
                          ], dtype='f4')
                  
                          # Create the buffer for the vertices
                          self.vbo = self.ctx.buffer(self.vertices)
                  
                          # Create vertex array object (VAO)
                          self.vao = self.ctx.simple_vertex_array(self.program, self.vbo, 'in_vert')
                  
                          # Set the uniform color (Red in this case)
                          self.program['color'].value = (1.0, 0.0, 0.0, 1.0)  # Red # EDITED: 1.0, 1.0, 1.0 is white.
                  
                      def resizeGL(self, w, h):
                          # EDITED: Detect framebuffer again after resize.
                          # To be fair: Claude AI gave me this solution.
                          fbo = self.ctx.detect_framebuffer()
                          fbo.use()
                          self.ctx.viewport = (0, 0, w, h)
                  
                      def paintGL(self):
                          # Clear the screen
                          self.ctx.clear(0.0, 0.0, 0.0)
                  
                          # Draw the triangle
                          self.vao.render()
                  
                  
                  if __name__ == '__main__':
                      app = QApplication([])
                  
                      win = Main()
                      win.resize(800, 600)
                      win.show()
                  
                      sys.exit(app.exec())
                  
                  1 Reply Last reply
                  0
                  • ? A Former User

                    Hi all. I am so stumped on why the triangle is not rendering. My shaders are correct, I set up the VBO and VAOs correctly, its just not rendering anything for some reason.

                    import sys
                    import moderngl as GL
                    import numpy
                    from PyQt6.QtGui import *
                    from PyQt6.QtWidgets import *
                    from PyQt6.QtCore import *
                    from PyQt6.QtOpenGLWidgets import *
                    
                    
                    class Main(QOpenGLWidget):
                        def __init__(self, parent=None):
                            super().__init__(parent)
                    
                        def initializeGL(self):
                            # Set up the OpenGL context using moderngl
                            self.ctx = GL.create_context()
                            self.ctx.clear(0, 0, 0)
                    
                            # Define shaders with error checking
                            vertex_shader_code = '''
                            #version 330 core
                    
                            in vec3 in_vert;
                    
                            void main() {
                                gl_Position = vec4(in_vert, 1.0);
                            }
                            '''
                            fragment_shader_code = '''
                            #version 330 core
                    
                            out vec4 fragColor;
                    
                            uniform vec4 color;
                    
                            void main() {
                                fragColor = color;
                            }
                            '''
                    
                            # Compile shaders and program
                            self.program = self.ctx.program(
                                vertex_shader=vertex_shader_code,
                                fragment_shader=fragment_shader_code
                            )
                    
                            # Check for any shader compilation issues
                            if not self.program:
                                print("Shader program failed to compile.")
                                return
                    
                            # Define triangle vertices (in normalized device coordinates)
                            self.vertices = numpy.array([
                                0.0, 0.5, 0.0,  # Top vertex
                                -0.5, -0.5, 0.0,  # Bottom-left vertex
                                0.5, -0.5, 0.0  # Bottom-right vertex
                            ], dtype='f4')
                    
                            # Create the buffer for the vertices
                            self.vbo = self.ctx.buffer(self.vertices)
                    
                            # Create vertex array object (VAO)
                            self.vao = self.ctx.simple_vertex_array(self.program, self.vbo, 'in_vert')
                    
                            # Set the uniform color (Red in this case)
                            self.program['color'].value = (1.0, 1.0, 1.0, 1.0)  # Red
                    
                        def resizeGL(self, w, h):
                            self.ctx.viewport = (0, 0, w, h)
                            self.update()
                    
                        def paintGL(self):
                            # Clear the screen
                            self.ctx.clear(0.0, 0.0, 0.0)
                    
                            # Draw the triangle
                            self.vao.render()
                    
                    
                    if __name__ == '__main__':
                        app = QApplication([])
                    
                        win = Main()
                        win.resize(800, 600)
                        win.show()
                    
                        sys.exit(app.exec())
                    
                    

                    I would like to add that if I remove self.ctx.clear(0.0, 0.0, 0.0) from the paintGL() method, it renders the vertices correctly. I can't do this though (obviously because I need to clear each frame before rendering)

                    W Offline
                    W Offline
                    webhan
                    wrote on last edited by
                    #8

                    dont use self.ctx.clear in paintGL because its can make your shader into black fill, so you just write ctx.clear in initializeGL

                    like this

                        def initializeGL(self):
                            self.ctx = moderngl.create_context()
                            self.ctx.clear(0,0,0,0)
                    
                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved