Deutsch English Français Italiano |
<vd2s8j$2ohs$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Lawrence D'Oliveiro <ldo@nz.invalid> Newsgroups: comp.lang.python Subject: Python OpenGL The Proper Way (Posting On Python-List Prohibited) Date: Thu, 26 Sep 2024 05:42:44 -0000 (UTC) Organization: A noiseless patient Spider Lines: 60 Message-ID: <vd2s8j$2ohs$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 26 Sep 2024 07:42:44 +0200 (CEST) Injection-Info: dont-email.me; posting-host="bdf142e3b9878dfa03dc2566703c0971"; logging-data="90684"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Km2jPtExkt+PSDX2TEOVo" User-Agent: Pan/0.160 (Toresk; ) Cancel-Lock: sha1:6jzinrljZ6IqDvks7lqR+J2eZxk= Bytes: 2940 Funny thing: the OpenGL specs document all routines and constants *without* “gl” and “GL_” prefixes. It makes sense to add these in a language that does not have namespaces, like C. In Python, it does not. So this routine takes apart the contents of the PyOpenGL modules into separate pieces, with those unnecessary prefixes stripped. So instead of calling OpenGL.GL.glClear(), say, you can call gl.Clear(). And instead of referring to OpenGL.GL.GL_ACCUM, you can use GL.ACCUM instead. Much more concise all round. import types import OpenGL import OpenGL.GL import OpenGL.GLU def gen_gl() : "generates versions of the PyOpenGL modules without spurious prefixes on names." gl = types.ModuleType("gl", doc = "OpenGL routines") GL = types.ModuleType("GL", doc = "OpenGL constants") glu = types.ModuleType("glu", doc = "OpenGL utility routines") GLU = types.ModuleType("GLU", doc = "OpenGL utility constants") for prefix, src, dest in \ ( ("gl", OpenGL.GL, gl), ("GL_", OpenGL.GL, GL), ("glu", OpenGL.GLU, glu), ("GLU_", OpenGL.GLU, GLU), ) \ : for name in dir(src) : if name.startswith(prefix) : setattr(dest, name[len(prefix):], getattr(src, name)) #end if #end for #end for return \ gl, GL, glu, GLU #end gen_gl gl, GL, glu, GLU = gen_gl() Now you can write code like gl.Clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT) gl.MatrixMode(GL.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 640, 0, 480, 0, 1) gl.Color3f(0, 0.5, 0.5) gl.Begin(GL.QUADS) for v in \ ( (100, 100), (100, 200), (200, 200), (200, 100), ) \ : gl.Vertex2f(*v) #end for gl.End() etc. Much cleaner, don’t you think?