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
6 Posts 3 Posters 507 Views
  • 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.
  • K Offline
    K Offline
    ktechhydle
    wrote on last edited by ktechhydle
    #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)

    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

      K 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

        K 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.

          K Offline
          K Offline
          ktechhydle
          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

            K 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.

              K Offline
              K Offline
              ktechhydle
              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
              • K ktechhydle has marked this topic as solved on
              • K ktechhydle

                @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

                • Login

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