[ad_1]
I have been making an attempt to be taught PyOpenGL, so I am nonetheless fairly clueless about what is going on on. I have been following a video sequence. I could not get it to cease throwing “Try to retrieve context when no legitimate context”, so I attempted copying the code immediately from the GitHub repo of the video sequence I used to be following together with, nevertheless it nonetheless throws the identical error! I’ve seen a number of individuals encounter the identical situation on Linux in my analysis however with not options to the difficulty. If it issues, I am operating Ubuntu 22.04.2 LTS.
Here is the code:
from OpenGL.GL import *
import numpy as np
import ctypes
from OpenGL.GL.shaders import compileProgram, compileShader
class App:
def __init__(self):
pygame.init()
pygame.show.set_mode((640, 480), pygame.OPENGL | pygame.DOUBLEBUF)
self.clock = pygame.time.Clock()
glClearColor(0.1, 0.2, 0.2, 0.1)
self.shader = self.createShader("shaders/vertex.txt", "shaders/fragment.txt")
glUseProgram(self.shader)
self.triangle = Triangle()
self.mainLoop()
def createShader(self, vertexFilepath, fragmentFilepath):
with open(vertexFilepath, "r") as f:
vertex_src = f.readlines()
with open(fragmentFilepath, "r") as f:
fragment_src = f.readlines()
shader = compileProgram(
compileShader(vertex_src, GL_VERTEX_SHADER),
compileShader(fragment_src, GL_FRAGMENT_SHADER),
)
return shader
def mainLoop(self):
operating = True
whereas operating:
# verify occasions
for occasion in pygame.occasion.get():
if occasion.kind == pygame.QUIT:
operating = False
# refresh display
glClear(GL_COLOR_BUFFER_BIT)
glUseProgram(self.shader)
glBindVertexArray(self.triangle.vao)
glDrawArrays(GL_TRIANGLES, 0, self.triangle.vertex_count)
pygame.show.flip()
# timing
self.clock.tick(60)
self.give up()
def give up(self):
self.triangle.destroy()
glDeleteProgram(self.shader)
pygame.give up()
class Triangle:
def __init__(self):
# x, y z, r, g, b
self.vertices = (
-0.5,
-0.5,
0.0,
1.0,
0.0,
0.0,
0.5,
-0.5,
0.0,
0.0,
1.0,
0.0,
0.0,
0.5,
0.0,
0.0,
0.0,
1.0,
)
self.vertices = np.array(self.vertices, dtype=np.float32)
self.vertex_count = 3
self.vao = glGenVertexArrays(1)
glBindVertexArray(self.vao)
self.vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glBufferData(
GL_ARRAY_BUFFER, self.vertices.nbytes, self.vertices, GL_STATIC_DRAW
)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0)) // OpenGL.error.Error: Try to retrieve context when no legitimate context
glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
def destroy(self):
glDeleteVertexArrays(1, (self.vao,))
glDeleteBuffers(1, (self.vbo,))
if __name__ == "__main__":
myApp = App()
I’ve marked the error, it is close to the tip of the code. The traceback begins at myApp = App()
.
[ad_2]