The OpenGL interface — pyglet v1.5.26
文章推薦指數: 80 %
PyOpenGL provides similar functionality with a more “Pythonic” interface, and will work with pyglet without any modification. Using OpenGL¶. Documentation of ... pyglet latest ProgrammingGuide Installation Writingapygletapplication Windowing Workingwiththekeyboard Workingwiththemouse Workingwithotherinputdevices Graphics Shapes Displayingtext Images Soundandvideo Applicationresources Thepygleteventframework Keepingtrackoftime CreatinganOpenGLcontext TheOpenGLinterface UsingOpenGL Resizingthewindow Errorchecking Usingextensionfunctions Usingmultiplewindows AGL,GLXandWGL Theapplicationeventloop pygletoptions Debuggingtools Advancedtopics In-depthgameexample APIReference pyglet pyglet.app pyglet.canvas pyglet.clock pyglet.event pyglet.font pyglet.gl pyglet.graphics pyglet.gui pyglet.image pyglet.info pyglet.input pyglet.media pyglet.resource pyglet.sprite pyglet.shapes pyglet.text pyglet.window ExternalResources RelatedDocumentation Thirdpartylibraries Projectsusingpyglet DevelopmentGuide Contributing Developmentenvironment Testingpyglet Documentation Makingapygletrelease OpenGLInterfaceImplementation ctypesWrapperGeneration wraptypes Mediamanual Medialoggingmanual pyglet Docs» TheOpenGLinterface EditonGitHub TheOpenGLinterface¶ pygletprovidesaninterfacetoOpenGLandGLU.Theinterfaceisusedbyall ofpyglet’shigher-levelAPI’s,sothatallrenderingisdoneefficientlyby thegraphicscard,ratherthantheoperatingsystem.Youcanaccessthis interfacedirectly;usingitismuchlikeusingOpenGLfromC. Theinterfaceisa“thin-wrapper”aroundlibGL.soonLinux, opengl32.dllonWindowsandOpenGL.frameworkonOSX.Thepyglet maintainersregeneratetheinterfacefromthelatestspecifications,soitis alwaysup-to-datewiththelatestversionandalmostallextensions. Theinterfaceisprovidedbythepyglet.glpackage.Touseityouwill needagoodknowledgeofOpenGL,Candctypes.YoumayprefertouseOpenGL withoutusingctypes,inwhichcaseyoushouldinvestigatePyOpenGL. PyOpenGLprovidessimilarfunctionalitywithamore“Pythonic”interface, andwillworkwithpygletwithoutanymodification. UsingOpenGL¶ DocumentationofOpenGLandGLUareprovidedattheOpenGLwebsiteand (morecomprehensively)intheOpenGLProgrammingSDK. ImportingthepackagegivesaccesstoOpenGL,GLU,andallOpenGLregistered extensions.Thisissufficientforallbutthemostadvancedusesof OpenGL: frompyglet.glimport* AllfunctionnamesandconstantsareidenticaltotheCcounterparts.For example,thefollowingprogramdrawsatriangleonthescreen: frompyglet.glimport* #DirectOpenGLcommandstothiswindow. window=pyglet.window.Window() @window.event defon_draw(): glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() glBegin(GL_TRIANGLES) glVertex2f(0,0) glVertex2f(window.width,0) glVertex2f(window.width,window.height) glEnd() pyglet.app.run() SomeOpenGLfunctionsrequireanarrayofdata.Thesearraysmustbe constructedasctypesarraysofthecorrecttype.Thefollowingexample drawthesametriangleasabove,butusesavertexarrayinsteadofthe immediate-modefunctions.Notetheconstructionofthevertexarrayusinga one-dimensionalctypesarrayofGLfloat: frompyglet.glimport* window=pyglet.window.Window() vertices=[0,0, window.width,0, window.width,window.height] vertices_gl_array=(GLfloat*len(vertices))(*vertices) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(2,GL_FLOAT,0,vertices_gl_array) @window.event defon_draw(): glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() glDrawArrays(GL_TRIANGLES,0,len(vertices)//2) pyglet.app.run() Similararrayconstructionscanbeusedtocreatedataforvertexbuffer objects,texturedata,polygonstippledataandthemapfunctions. Resizingthewindow¶ pygletsetsuptheviewportandanorthographicprojectiononeachwindow automatically.Itdoesthisinadefault on_resize()handlerdefinedon Window: @window.event defon_resize(width,height): glViewport(0,0,width,height) glMatrixMode(gl.GL_PROJECTION) glLoadIdentity() glOrtho(0,width,0,height,-1,1) glMatrixMode(gl.GL_MODELVIEW) Ifyouneedtodefineyourownprojection(forexample,touse a3-dimensionalperspectiveprojection),youshouldoverridethis eventwithyourown;forexample: @window.event defon_resize(width,height): glViewport(0,0,width,height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(65,width/float(height),.1,1000) glMatrixMode(GL_MODELVIEW) returnpyglet.event.EVENT_HANDLED Notethattheon_resize()handleriscalledfor awindowthefirsttimeitisdisplayed,aswellasanytimeitislater resized. Errorchecking¶ Bydefault,pygletcallsglGetErroraftereveryGLfunctioncall(except wheresuchacheckwouldbeinvalid).Ifanerrorisreported,pygletraises GLExceptionwiththeresultofgluErrorStringasthemessage. Thisisveryhandyduringdevelopment,asitcatchescommoncodingerrors earlyon.However,ithasasignificantimpactonperformance,andis disabledwhenpythonisrunwiththe-Ooption. Youcanalsodisablethiserrorcheckbysettingthefollowingoptionbefore importingpyglet.glorpyglet.window: #Disableerrorcheckingforincreasedperformance pyglet.options['debug_gl']=False frompyglet.glimport* Settingtheoptionafterimportingpyglet.glwillhavenoeffect.Once disabled,thereisnoerror-checkingoverheadineachGLcall. Usingextensionfunctions¶ Beforeusinganextensionfunction,youshouldcheckthattheextensionis implementedbythecurrentdriver.Typicallythisisdoneusing glGetString(GL_EXTENSIONS),butpyglethasaconveniencemodule, pyglet.gl.gl_infothatdoesthisforyou: ifpyglet.gl.gl_info.have_extension('GL_ARB_shadow'): #...doshadow-relatedcode. else: #...raiseanexception,oruseafallbackmethod YoucanalsoeasilychecktheversionofOpenGL: ifpyglet.gl.gl_info.have_version(1,5): #WecanassumeallOpenGL1.5functionsareimplemented. Remembertoonlycallthegl_infofunctionsaftercreatingawindow. Thereisacorrespondingglu_infomoduleforcheckingtheversionand extensionsofGLU. nVidiaoftenreleasehardwarewithextensionsbeforehavingthemregistered officially.Whenyouimport*frompyglet.glyouimportonlythe registeredextensions.YoucanimportthelatestnVidiaextensions with: frompyglet.gl.glext_nvimport* Usingmultiplewindows¶ pygletallowsyoutocreateanddisplayanynumberofwindowssimultaneously. EachwillbecreatedwithitsownOpenGLcontext,howeverallcontextswill sharethesametextureobjects,displaylists,shaderprograms,andsoon, bydefault[1].Eachcontexthasitsownstateandframebuffers. Thereisalwaysanactivecontext(unlesstherearenowindows).Whenusing pyglet.app.run()fortheapplicationeventloop,pygletensuresthat thecorrectwindowistheactivecontextbeforedispatchingthe on_draw()or on_resize()events. Inothercases,youcanexplicitlysettheactivecontextwith pyglet.window.Window.switch_to. [1]Sometimesobjectsandlistscannotbesharedbetweencontexts;for example,whenthecontextsareprovidedbydifferentvideo devices.Thiswillusuallyonlyoccurifyouexplicitlyselect differentscreensdrivenbydifferentdevices. AGL,GLXandWGL¶ TheOpenGLcontextitselfismanagedbyanoperating-systemspecificlibrary: AGLonOSX,GLXunderX11andWGLonWindows.pyglethandlesthesedetails whenawindowiscreated,butyoumayneedtousethefunctionsdirectly(for example,tousepbuffers)oranextensionfunction. Themodulesarenamedpyglet.gl.agl,pyglet.gl.glxand pyglet.gl.wgl.Youmustonlyimportthecorrectmodulefortherunning operatingsystem: ifsys.platform.startswith('linux'): frompyglet.gl.glximport* glxCreatePbuffer(...) elifsys.platform=='darwin': frompyglet.gl.aglimport* aglCreatePbuffer(...) Alternativallyyoucanusepyglet.compat_platformtosupport platformsthatarecompatiblewithplatformsnotofficiallysupported bypyglet.ForexampleFreeBSDsystemswillappearaslinux-compat inpyglet.compat_platform. ThereareconveniencemodulesforqueryingtheversionandextensionsofWGL andGLXnamedpyglet.gl.wgl_infoandpyglet.gl.glx_info,respectively. AGLdoesnothavesuchamodule,justquerytheversionofOSXinstead. IfusingGLXextensions,youcanimportpyglet.gl.glxext_arbforthe registeredextensionsorpyglet.gl.glxext_nvforthelatestnVidia extensions. Similarly,ifusingWGLextensions,importpyglet.gl.wglext_arbor pyglet.gl.wglext_nv. ReadtheDocs v:latest Versions master latest Downloads html epub OnReadtheDocs ProjectHome Builds FreedocumenthostingprovidedbyReadtheDocs.
延伸文章資訊
- 1Advanced OpenGL in Python with PyGame and PyOpenGL
PyOpenGL is a standardized bridge between OpenGL and Python. PyGame is a standardized library for...
- 23d - Will I have an easier time learning OpenGL in Pygame or ...
If you just want to learn OpenGL, pyglet or PyOpenGL with the SuperBible will be fine. If you wan...
- 3Dependencies: Pyglet vs PyOpenGL - Google Groups
We basically have two choices: Pyglet or PyOpenGL. The former being a bit basic, the latter being...
- 4PYTHON : PyOpenGl or pyglet? - YouTube
- 5I'd like to learn graphics programming. Should I go with pyglet ...
PyOpenGL is nothing but a wrapper for OpenGL itself - no sound, no window management, just 3D gra...