Attempt to retrieve context when no valid context
See original GitHub issueIm following a very simple code example to draw something on screen using glfw and opengl but I get met with
Traceback (most recent call last):
File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/src/__unused/test.py", line 75, in <module>
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, ctypes.c_void_p(0))
File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/lib/python3.10/site-packages/OpenGL/latebind.py", line 63, in __call__
return self.wrapperFunction( self.baseFunction, *args, **named )
File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/lib/python3.10/site-packages/OpenGL/GL/VERSION/GL_2_0.py", line 469, in glVertexAttribPointer
contextdata.setValue( key, array )
File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/lib/python3.10/site-packages/OpenGL/contextdata.py", line 58, in setValue
context = getContext( context )
File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/lib/python3.10/site-packages/OpenGL/contextdata.py", line 40, in getContext
raise error.Error(
OpenGL.error.Error: Attempt to retrieve context when no valid context
I have reduced my code as much as possible so I can post it here, I am doing glfw.make_context_current(window)
to make the context current so I am not sure what is going wrong:
import glfw
from OpenGL.GL import *
import numpy as np
glfw.init()
glfw.window_hint(glfw.FOCUSED, glfw.FALSE)
window = glfw.create_window(640, 400, "Window", None, None)
glfw.make_context_current(window)
vertices = [
0.5, 0.5, 0.0,
0.5, -0.5, 0.0,
-0.5, -0.5, 0.0,
-0.5, 0.5, 0.0
]
indices = [
0, 1, 3,
1, 2, 3
]
vertices = np.array(vertices, dtype=np.float32)
indices = np.array(indices, dtype=np.int32)
vertex_shader_source = """
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
"""
frag_shader_source = """
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
"""
vertex_shader = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vertex_shader, [vertex_shader_source])
glCompileShader(vertex_shader)
print(glGetShaderiv(vertex_shader, GL_COMPILE_STATUS))
frag_shader = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(frag_shader, [frag_shader_source])
glCompileShader(frag_shader)
print(glGetShaderiv(frag_shader, GL_COMPILE_STATUS))
shader_program = glCreateProgram()
glAttachShader(shader_program, vertex_shader)
glAttachShader(shader_program, frag_shader)
glLinkProgram(shader_program)
print(glGetProgramiv(shader_program, GL_LINK_STATUS))
glDeleteShader(vertex_shader)
glDeleteShader(frag_shader)
VAO = glGenVertexArrays(1)
VBO = glGenBuffers(1)
EBO = glGenBuffers(1)
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)
glClearColor(1, 0, 0, 1)
while not glfw.window_should_close(window):
glClear(GL_COLOR_BUFFER_BIT)
glUseProgram(shader_program)
glBindVertexArray(VAO)
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
glfw.swap_buffers(window)
glfw.poll_events()
glfw.terminate()
Putting:
print(glfw.get_current_context())
just before:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, ctypes.c_void_p(0))
prints <glfw.LP__GLFWwindow object at 0x7f822c987340>
meaning that some context was created for that window, as far as I understand the glfw docs.
EDIT: might be worth mentioning that I compiled glfw from source and replaced the wheel package with this newly compiled version as glfw 3.3.x does not work with ubuntu 21 and its wayland version
EDIT2: I am currently investigating if this has potentially something to do with my nvidia drivers and my gpu not showing up on some places as it should. Although I have an igpu capable of OpenGL 4.6 support
Issue Analytics
- State:
- Created 2 years ago
- Comments:20 (7 by maintainers)
Top GitHub Comments
I can’t tell you what the future will hold for OpenGL and I don’t know your use cases, but take a look at a Vulkan tutorial for drawing a single triangle. Vulkan is a wee bit more verbose and complex, the learning curve isn’t any less steep than that of OpenGL. I think even if you don’t end up using OpenGL all that much, it can be a useful tool to learn and understand.
There are plenty of tutorials for OpenGL in general, but for pitfalls such as this, I think suffering is the way to go. 😂 Though it’s not as bad as my statement might’ve made it seem, it’s just that issues like this one that can be pretty disheartening/annoying, when you don’t even manage to draw something.
Also: note that you actually got it right first try with
glVertexAttribPointer
, you’re passing a null pointer there.