No valid context

physkets via PyOpenGL-Users <[email protected]> Sun, 19 Apr 2020 16:02:24 +0200 (CEST)
Newsgroups gmane.comp.python.opengl.user
Message-ID <[email protected]>
------=_Part_151399_936686012.1587304944928
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Hi!

I'm trying to use pyGLFW to make a simple coloured quad, but I fail with the following error message:

Traceback (most recent call last):  File "first.py", line 121, in <module>    gl.glVertexAttribPointer(LOCATION, 2, gl.GL_FLOAT, False, STRIDE, OFFSET)  File "/usr/lib/python3.8/site-packages/OpenGL/latebind.py", line 63, in __call__    return self.wrapperFunction( self.baseFunction, *args, **named )  File "/usr/lib/python3.8/site-packages/OpenGL/GL/VERSION/GL_2_0.py", line 469, in glVertexAttribPointer    contextdata.setValue( key, array )  File "/usr/lib/python3.8/site-packages/OpenGL/contextdata.py", line 58, in setValue    context = getContext( context )  File "/usr/lib/python3.8/site-packages/OpenGL/contextdata.py", line 40, in getContext    raise error.Error(OpenGL.error.Error: Attempt to retrieve context when no valid context
I am attaching the program that gives me that error along with the shaders.

Am I doing something wrong?

This is information from glxinfo:

OpenGL vendor string: Intel
OpenGL renderer string: Mesa Intel(R) HD Graphics 620 (KBL GT2)
OpenGL core profile version string: 4.6 (Core Profile) Mesa 20.0.4
OpenGL core profile shading language version string: 4.60
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 4.6 (Compatibility Profile) Mesa 20.0.4
OpenGL shading language version string: 4.60
OpenGL context flags: (none)
OpenGL profile mask: compatibility profile
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.2 Mesa 20.0.4
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20
OpenGL ES profile extensions:
Also, I'm on a Wayland-based compositor, and am using GLFW compiled for wayland. Might that be an issue?

------=_Part_151399_936686012.1587304944928
Content-Type: application/octet-stream; name=first.frag
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=first.frag

I3ZlcnNpb24gNDYwCm91dCB2ZWM0IEZyYWdDb2xvcjsKdm9pZCBtYWluKCkKewogICAgRnJhZ0Nv
bG9yID0gdmVjNCgxLjAsIDAuMCwgMC4wLCAxLjApOwoKICAgIC8vIG9yIEZyYWdDb2xvci5yZ2Jh
ID0gdmVjNCgxLjAsIDAuMCwgMC4wLCAxLjApOwoKICAgIC8vIG9yIEZyYWdDb2xvci5yZ2IgPSB2
ZWMzKDEuMCwgMC4wLCAwLjApOwogICAgLy8gICAgRnJhZ0NvbG9yLmEgPSAxLjA7Cn0K
------=_Part_151399_936686012.1587304944928
Content-Type: application/octet-stream; name=first.vert
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=first.vert

I3ZlcnNpb24gNDYwCmluIHZlYzIgcG9zaXRpb247CnZvaWQgbWFpbigpCnsKICAgIGdsX1Bvc2l0
aW9uID0gdmVjNChwb3NpdGlvbiwgMC4wLCAxLjApOwoKICAgIC8vIG9yIGdsX1Bvc2l0aW9uLnh5
encgPSB2ZWM0KHBvc2l0aW9uLCAwLjAsIDEuMCk7CgogICAgLy8gb3IgZ2xfUG9zaXRpb24ueHkg
PSBwb3NpdGlvbjsKICAgIC8vICAgIGdsX1Bvc2l0aW9uLnp3ID0gdmVjMigwLjAsIDEuMCk7Cgog
ICAgLy8gb3IgZ2xfUG9zaXRpb24ueCA9IHBvc2l0aW9uLng7CiAgICAvLyAgICBnbF9Qb3NpdGlv
bi55ID0gcG9zaXRpb24ueTsKICAgIC8vICAgIGdsX1Bvc2l0aW9uLnogPSAwLjA7CiAgICAvLyAg
ICBnbF9Qb3NpdGlvbi53ID0gMS4wOwp9Cg==
------=_Part_151399_936686012.1587304944928
Content-Type: text/x-python; charset=us-ascii; name=first.py
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=first.py

"""
My first OpenGL program
"""
from contextlib import contextmanager
from ctypes import c_void_p
import glfw
import OpenGL.GL as gl
from numpy import zeros, float32

def kill(pane, key, scancode, action, mods):
    """
    Set ESCAPE as the kill key
    """
    assert isinstance(scancode, int) and isinstance(mods, int), "Oops!"

    if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
        glfw.set_window_should_close(pane, True)

def reshape(pane, width, height):
    """
    Reshape incase of framebuffer resize
    """
    del pane
    gl.glViewport(0, 0, width, height)

@contextmanager
def create_main_window():
    """
    Creates the window
    """
    if not glfw.init():
        raise RuntimeError("GLFW initialisation failed")
    try:
        # present GLFWErrors as Warnings and not Exceptions
        glfw.ERROR_REPORTING = 'warn'

        # prevent Wayland focusing warning
        glfw.window_hint(glfw.FOCUSED, False)
        #glfw.window_hint(glfw.VISIBLE, False)

        ## use core profile
        #glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
        #glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6)
        #glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True)
        #glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

        title = "Hello World"
        pane = glfw.create_window(512, 512, title, None, None)
        if not pane:
            raise RuntimeError("GLFW window creation failed")

        glfw.set_key_callback(pane, kill)
        glfw.make_context_current(pane)

        glfw.swap_interval(1)

        glfw.set_framebuffer_size_callback(pane, reshape)

        yield pane

    finally:
        glfw.terminate()


with create_main_window() as window:

    ########################################
    ################ OpenGL ################
    ########################################

    PROGRAM = gl.glCreateProgram()
    VERTEX = gl.glCreateShader(gl.GL_VERTEX_SHADER)
    FRAGMENT = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)

    with open('first.vert', 'r') as file:
        VERTEX_CODE = file.read()
    with open('first.frag', 'r') as file:
        FRAGMENT_CODE = file.read()

    # Set shaders source
    gl.glShaderSource(VERTEX, VERTEX_CODE)
    gl.glShaderSource(FRAGMENT, FRAGMENT_CODE)

    # Compile shaders
    gl.glCompileShader(VERTEX)
    if not gl.glGetShaderiv(VERTEX, gl.GL_COMPILE_STATUS):
        print(gl.glGetShaderInfoLog(VERTEX).decode())
        raise RuntimeError("Vertex shader compilation error")

    gl.glCompileShader(FRAGMENT)
    if not gl.glGetShaderiv(FRAGMENT, gl.GL_COMPILE_STATUS):
        print(gl.glGetShaderInfoLog(FRAGMENT).decode())
        raise RuntimeError("Fragment shader compilation error")

    gl.glAttachShader(PROGRAM, VERTEX)
    gl.glAttachShader(PROGRAM, FRAGMENT)
    gl.glLinkProgram(PROGRAM)

    if not gl.glGetProgramiv(PROGRAM, gl.GL_LINK_STATUS):
        print(gl.glGetProgramInfoLog(PROGRAM))
        raise RuntimeError('Linking error')

    gl.glDetachShader(PROGRAM, VERTEX)
    gl.glDetachShader(PROGRAM, FRAGMENT)

    gl.glUseProgram(PROGRAM)

    ########################################
    ################# Main #################
    ########################################

    # Display one colour
    #gl.glClearColor(0, 0, 0.3, 0)

    # Build data
    DATA = zeros((4, 2), dtype=float32)
    # Request a buffer slot from GPU
    GPU_BUFFER = gl.glGenBuffers(1)
    # Make this buffer the default one
    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, GPU_BUFFER)

    STRIDE = DATA.strides[0]
    OFFSET = c_void_p(0)
    LOCATION = gl.glGetAttribLocation(PROGRAM, "position")
    gl.glEnableVertexAttribArray(LOCATION)
    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, GPU_BUFFER)
    gl.glVertexAttribPointer(LOCATION, 2, gl.GL_FLOAT, False, STRIDE, OFFSET)

    # Assign CPU data
    DATA[...] = (-1, +1), (+1, +1), (-1, -1), (+1, -1)
    # Upload CPU data to GPU buffer
    gl.glBufferData(gl.GL_ARRAY_BUFFER, DATA.nbytes, DATA, gl.GL_DYNAMIC_DRAW)

    ########################################
    ############### Display ################
    ########################################

    while not glfw.window_should_close(window):
        # Render here, e.g. using pyOpenGL
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4)

        # Swap front and back buffers
        glfw.swap_buffers(window)

        # Poll for and process events
        glfw.poll_events()

------=_Part_151399_936686012.1587304944928
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


------=_Part_151399_936686012.1587304944928
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
PyOpenGL Homepage
http://pyopengl.sourceforge.net
_______________________________________________
PyOpenGL-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyopengl-users

------=_Part_151399_936686012.1587304944928--