python-opengl/glut-cube.py at master - GitHub
文章推薦指數: 80 %
An open access book on Python, OpenGL and Scientific Visualization, Nicolas P. Rougier, 2018 - python-opengl/glut-cube.py at master · rougier/python-opengl. 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-cube.py / Jumpto rotate Function translate Function frustum Function perspective Function display Function reshape Function keyboard Function timer Function Gotofile Gotofile T Gotoline L Gotodefinition R Copypath Copypermalink Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository. Cannotretrievecontributorsatthistime 219lines(181sloc) 7.49KB Raw Blame Editthisfile E OpeninGitHubDesktop OpenwithDesktop Viewraw Viewblame ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters. LearnmoreaboutbidirectionalUnicodecharacters Showhiddencharacters #----------------------------------------------------------------------------- #Copyright(c)2016NicolasP.Rougier.Allrightsreserved. #Distributedunderthe(new)BSDLicense. #----------------------------------------------------------------------------- importsys importmath importctypes importnumpyasnp importOpenGL.GLasgl importOpenGL.GLUTasglut vertex_code=""" uniformmat4u_model;//Modelmatrix uniformmat4u_view;//Viewmatrix uniformmat4u_projection;//Projectionmatrix uniformvec4u_color;//Globalcolor attributevec4a_color;//Vertexcolor attributevec3a_position;//Vertexposition varyingvec4v_color;//Interpolatedfragmentcolor(out) voidmain() { v_color=u_color*a_color; gl_Position=u_projection*u_view*u_model*vec4(a_position,1.0); } """ fragment_code=""" varyingvec4v_color;//Interpolatedfragmentcolor(in) voidmain() { gl_FragColor=v_color; } """ defrotate(M,angle,x,y,z): angle=math.pi*angle/180 c,s=math.cos(angle),math.sin(angle) n=math.sqrt(x*x+y*y+z*z) x,y,z=x/n,y/n,z/n cx,cy,cz=(1-c)*x,(1-c)*y,(1-c)*z R=np.array([[cx*x+c,cy*x-z*s,cz*x+y*s,0], [cx*y+z*s,cy*y+c,cz*y-x*s,0], [cx*z-y*s,cy*z+x*s,cz*z+c,0], [0,0,0,1]],dtype=M.dtype).T M[...]=np.dot(M,R) returnM deftranslate(M,x,y=None,z=None): y=xifyisNoneelsey z=xifzisNoneelsez T=np.array([[1.0,0.0,0.0,x], [0.0,1.0,0.0,y], [0.0,0.0,1.0,z], [0.0,0.0,0.0,1.0]],dtype=M.dtype).T M[...]=np.dot(M,T) returnM deffrustum(left,right,bottom,top,znear,zfar): M=np.zeros((4,4),dtype=np.float32) M[0,0]=+2.0*znear/(right-left) M[2,0]=(right+left)/(right-left) M[1,1]=+2.0*znear/(top-bottom) M[3,1]=(top+bottom)/(top-bottom) M[2,2]=-(zfar+znear)/(zfar-znear) M[3,2]=-2.0*znear*zfar/(zfar-znear) M[2,3]=-1.0 returnM defperspective(fovy,aspect,znear,zfar): h=math.tan(fovy/360.0*math.pi)*znear w=h*aspect returnfrustum(-w,w,-h,h,znear,zfar) defdisplay(): globalphi,theta gl.glDepthMask(gl.GL_TRUE) gl.glClear(gl.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT) #Filledcube gl.glDisable(gl.GL_BLEND) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_POLYGON_OFFSET_FILL) gl.glUniform4f(gpu["uniform"]["u_color"],1,1,1,1) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER,gpu["buffer"]["filled"]) gl.glDrawElements(gl.GL_TRIANGLES,len(f_indices),gl.GL_UNSIGNED_INT,None) #Outlinedcube gl.glDisable(gl.GL_POLYGON_OFFSET_FILL) gl.glEnable(gl.GL_BLEND) gl.glDepthMask(gl.GL_FALSE) gl.glUniform4f(gpu["uniform"]["u_color"],0,0,0,.5) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER,gpu["buffer"]["outline"]) gl.glDrawElements(gl.GL_LINES,len(o_indices),gl.GL_UNSIGNED_INT,None) gl.glDepthMask(gl.GL_TRUE) #Rotatecube theta+=0.5#degrees phi+=0.5#degrees model=np.eye(4,dtype=np.float32) rotate(model,theta,0,0,1) rotate(model,phi,0,1,0) gl.glUniformMatrix4fv(gpu["uniform"]["u_model"],1,False,model) glut.glutSwapBuffers() defreshape(width,height): gl.glViewport(0,0,width,height) projection=perspective(45.0,width/float(height),2.0,100.0) gl.glUniformMatrix4fv(gpu["uniform"]["u_projection"],1,False,projection) defkeyboard(key,x,y): ifkey==b'\033':sys.exit() deftimer(fps): glut.glutTimerFunc(1000//fps,timer,fps) glut.glutPostRedisplay() #GLUTinit #-------------------------------------- glut.glutInit() glut.glutInitDisplayMode(glut.GLUT_DOUBLE|glut.GLUT_RGBA|glut.GLUT_DEPTH) glut.glutCreateWindow('Helloworld!') glut.glutReshapeWindow(512,512) glut.glutReshapeFunc(reshape) glut.glutDisplayFunc(display) glut.glutKeyboardFunc(keyboard) glut.glutTimerFunc(1000//60,timer,60) #Buildcube #-------------------------------------- vertices=np.zeros(8,[("a_position",np.float32,3), ("a_color",np.float32,4)]) vertices["a_position"]=[[1,1,1],[-1,1,1],[-1,-1,1],[1,-1,1], [1,-1,-1],[1,1,-1],[-1,1,-1],[-1,-1,-1]] vertices["a_color"]=[[0,1,1,1],[0,0,1,1],[0,0,0,1],[0,1,0,1], [1,1,0,1],[1,1,1,1],[1,0,1,1],[1,0,0,1]] f_indices=np.array([0,1,2,0,2,3,0,3,4,0,4,5,0,5,6,0,6,1, 1,6,7,1,7,2,7,4,3,7,3,2,4,7,6,4,6,5],dtype=np.uint32) o_indices=np.array([0,1,1,2,2,3,3,0,4,7,7,6, 6,5,5,4,0,5,1,6,2,7,3,4],dtype=np.uint32) #Build&activateprogram #-------------------------------------- program=gl.glCreateProgram() vertex=gl.glCreateShader(gl.GL_VERTEX_SHADER) fragment=gl.glCreateShader(gl.GL_FRAGMENT_SHADER) gl.glShaderSource(vertex,vertex_code) gl.glCompileShader(vertex) gl.glAttachShader(program,vertex) gl.glShaderSource(fragment,fragment_code) gl.glCompileShader(fragment) gl.glAttachShader(program,fragment) gl.glLinkProgram(program) gl.glDetachShader(program,vertex) gl.glDetachShader(program,fragment) gl.glUseProgram(program) #BuildGPUobjects #-------------------------------------- gpu={"buffer":{},"uniform":{}} gpu["buffer"]["vertices"]=gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ARRAY_BUFFER,gpu["buffer"]["vertices"]) gl.glBufferData(gl.GL_ARRAY_BUFFER,vertices.nbytes,vertices,gl.GL_DYNAMIC_DRAW) stride=vertices.strides[0] offset=ctypes.c_void_p(0) loc=gl.glGetAttribLocation(program,"a_position") gl.glEnableVertexAttribArray(loc) gl.glVertexAttribPointer(loc,3,gl.GL_FLOAT,False,stride,offset) offset=ctypes.c_void_p(vertices.dtype["a_position"].itemsize) loc=gl.glGetAttribLocation(program,"a_color") gl.glEnableVertexAttribArray(loc) gl.glVertexAttribPointer(loc,4,gl.GL_FLOAT,False,stride,offset) gpu["buffer"]["filled"]=gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER,gpu["buffer"]["filled"]) gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER,f_indices.nbytes,f_indices,gl.GL_STATIC_DRAW) gpu["buffer"]["outline"]=gl.glGenBuffers(1) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER,gpu["buffer"]["outline"]) gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER,o_indices.nbytes,o_indices,gl.GL_STATIC_DRAW) #Binduniforms #-------------------------------------- gpu["uniform"]["u_model"]=gl.glGetUniformLocation(program,"u_model") gl.glUniformMatrix4fv(gpu["uniform"]["u_model"],1,False,np.eye(4)) gpu["uniform"]["u_view"]=gl.glGetUniformLocation(program,"u_view") view=translate(np.eye(4),0,0,-5) gl.glUniformMatrix4fv(gpu["uniform"]["u_view"],1,False,view) gpu["uniform"]["u_projection"]=gl.glGetUniformLocation(program,"u_projection") gl.glUniformMatrix4fv(gpu["uniform"]["u_projection"],1,False,np.eye(4)) gpu["uniform"]["u_color"]=gl.glGetUniformLocation(program,"u_color") gl.glUniform4f(gpu["uniform"]["u_color"],1,1,1,1) phi,theta=40,30 #Entermainloop #-------------------------------------- gl.glClearColor(1,1,1,1) gl.glPolygonOffset(1,1) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA) gl.glHint(gl.GL_LINE_SMOOTH_HINT,gl.GL_NICEST) gl.glLineWidth(1.0) glut.glutMainLoop() Copylines Copypermalink Viewgitblame Referenceinnewissue Go Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.
延伸文章資訊
- 1pyOpenGL GLUT window function doesn't close properly
PyOpenGL uses freeglut. You have to give freeglut the chance to close the window. glutDestroyWind...
- 2python-opengl/glut-cube.py at master - GitHub
An open access book on Python, OpenGL and Scientific Visualization, Nicolas P. Rougier, 2018 - py...
- 3PyOpenGL - Python Wiki
GLUT import * # The display() method does all the work; it has to call the appropriate # OpenGL f...
- 4PyOpenGL:是一個呼叫OpenGL的2D/3D的python圖形庫
1 說明1.1 PyOpenGL:是一個呼叫OpenGL的2D/3D的python圖形庫。1.2 OpenGL:1.2.1 Open Graphics ... GLU import * #調出g...
- 5Python GLUT.glutInit方法代碼示例- 純淨天空
本文整理匯總了Python中OpenGL.GLUT.glutInit方法的典型用法代碼示例。如果您正苦於以下問題:Python GLUT.glutInit方法的具體用法?Python GLUT....