python-opengl/glut-quad-solid.py at master - GitHub

文章推薦指數: 80 %
投票人數:10人

An open access book on Python, OpenGL and Scientific Visualization, Nicolas P. Rougier, 2018 - python-opengl/glut-quad-solid.py at master ... Skiptocontent {{message}} rougier / python-opengl Public Notifications Fork 90 Star 545 Code Issues 21 Pullrequests 0 Actions Security Insights More Code Issues Pullrequests Actions Security Insights Permalink master Branches Tags Couldnotloadbranches Nothingtoshow {{refName}} default Couldnotloadtags Nothingtoshow {{refName}} default python-opengl/code/chapter-03/glut-quad-solid.py / Jumpto display Function reshape Function keyboard Function Gotofile Gotofile T Gotoline L Gotodefinition R Copypath Copypermalink Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository.     Cannotretrievecontributorsatthistime 115lines(91sloc) 3.27KB Raw Blame Editthisfile E OpeninGitHubDesktop OpenwithDesktop Viewraw Viewblame ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters. LearnmoreaboutbidirectionalUnicodecharacters Showhiddencharacters #----------------------------------------------------------------------------- #Python&OpenGLforScientificVisualization #www.labri.fr/perso/nrougier/python+opengl #Copyright(c)2017,NicolasP.Rougier #Distributedunderthe2-ClauseBSDLicense. #----------------------------------------------------------------------------- importsys importctypes importnumpyasnp importOpenGL.GLasgl importOpenGL.GLUTasglut vertex_code=""" attributevec2position; voidmain(){gl_Position=vec4(position,0.0,1.0);}""" fragment_code=""" voidmain(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}""" defdisplay(): gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP,0,4) glut.glutSwapBuffers() defreshape(width,height): gl.glViewport(0,0,width,height) defkeyboard(key,x,y): ifkey==b'\x1b': sys.exit() #GLUTinit #-------------------------------------- glut.glutInit() glut.glutInitDisplayMode(glut.GLUT_DOUBLE|glut.GLUT_RGBA) glut.glutCreateWindow('Helloworld!') glut.glutReshapeWindow(512,512) glut.glutReshapeFunc(reshape) glut.glutDisplayFunc(display) glut.glutKeyboardFunc(keyboard) #Builddata #-------------------------------------- data=np.zeros(4,[("position",np.float32,2)]) data['position']=[(-1,+1),(+1,+1),(-1,-1),(+1,-1)] #Build&activateprogram #-------------------------------------- #RequestaprogramandshaderslotsfromGPU program=gl.glCreateProgram() vertex=gl.glCreateShader(gl.GL_VERTEX_SHADER) fragment=gl.glCreateShader(gl.GL_FRAGMENT_SHADER) #Setshaderssource gl.glShaderSource(vertex,vertex_code) gl.glShaderSource(fragment,fragment_code) #Compileshaders gl.glCompileShader(vertex) ifnotgl.glGetShaderiv(vertex,gl.GL_COMPILE_STATUS): error=gl.glGetShaderInfoLog(vertex).decode() print(error) raiseRuntimeError("Shadercompilationerror") gl.glCompileShader(fragment) gl.glCompileShader(fragment) ifnotgl.glGetShaderiv(fragment,gl.GL_COMPILE_STATUS): error=gl.glGetShaderInfoLog(fragment).decode() print(error) raiseRuntimeError("Shadercompilationerror") #Attachshaderobjectstotheprogram gl.glAttachShader(program,vertex) gl.glAttachShader(program,fragment) #Buildprogram gl.glLinkProgram(program) ifnotgl.glGetProgramiv(program,gl.GL_LINK_STATUS): print(gl.glGetProgramInfoLog(program)) raiseRuntimeError('Linkingerror') #Getridofshaders(nomoreneeded) gl.glDetachShader(program,vertex) gl.glDetachShader(program,fragment) #Makeprogramthedefaultprogram gl.glUseProgram(program) #Buildbuffer #-------------------------------------- #RequestabufferslotfromGPU buffer=gl.glGenBuffers(1) #Makethisbufferthedefaultone gl.glBindBuffer(gl.GL_ARRAY_BUFFER,buffer) #Uploaddata gl.glBufferData(gl.GL_ARRAY_BUFFER,data.nbytes,data,gl.GL_DYNAMIC_DRAW) #Bindthepositionattribute #-------------------------------------- stride=data.strides[0] offset=ctypes.c_void_p(0) loc=gl.glGetAttribLocation(program,"position") gl.glEnableVertexAttribArray(loc) gl.glBindBuffer(gl.GL_ARRAY_BUFFER,buffer) gl.glVertexAttribPointer(loc,2,gl.GL_FLOAT,False,stride,offset) #Enterthemainloop #-------------------------------------- glut.glutMainLoop() Copylines Copypermalink Viewgitblame Referenceinnewissue Go Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?