Learn OpenGL's rectangular box under Python

Source: Internet
Author: User

Https://learnopengl.com/Getting-started/Hello-Triangle implements the simplest shader, the translated version of which is shown in the corresponding Chinese tutorial

The following is a python implementation of the C + + code in the tutorial above:

#!/usr/bin/env python#-*-coding:utf-8-*-Importsys, OSImportOpengl.gl as GLImportGLFWImportNumPy as Npwin_width= 800Win_height= 600Vertexshadersource="""#version corelayout (location = 0) in vec3 apos;void main () {gl_position = Vec4 (apos.x, Apos.y, Apos.z, 1.0);} """Fragmentshadersource="""#version coreout vec4 fragcolor;void Main () {Fragcolor = VEC4 (1.0, 0.5, 0.2, 1.0);}"""vertices= Np.array ([0.5, 0.5, 0,0.5,-0.5, 0,-0.5,-0.5, 0,-0.5, 0.5, 0], Dtype =np.float32) Indices= Np.array ([0.0, 1, 3,                    1, 2, 3], Dtype =Np.uint32)defframebuffer_size_callback (window, width, height): gl.glviewport (0, 0, width, height)defprocessinput (window):ifGlfw.glfwgetkey (window, GLFW. Glfw_key_escape) = =GLFW. GLFW_PRESS:glfw.glfwSetWindowShouldClose ()ifGlfw.glfwgetkey (window, GLFW. Glfw_key_left) = =GLFW. GLFW_PRESS:gl.glPolygonMode (Gl.gl_front_and_back, Gl.gl_fill)ifGlfw.glfwgetkey (window, GLFW. glfw_key_right) = =GLFW. GLFW_PRESS:gl.glPolygonMode (Gl.gl_front_and_back, Gl.gl_line)defMain (): Glfw.glfwinit () Glfw.glfwwindowhint (GLFW. Glfw_context_version_major,3) Glfw.glfwwindowhint (GLFW. Glfw_context_version_minor,3) Glfw.glfwwindowhint (GLFW. Glfw_opengl_profile, GLFW. glfw_opengl_core_profile) window= Glfw.glfwcreatewindow (Win_width, Win_height,"Learning OpenGL". Encode (), 0, 0)ifwindow = =0:Print("failed to create window") glfw.glfwterminate () glfw.glfwmakecontextcurrent (window) glfw.glfwsetframebuffersizecallback (window, FR Amebuffer_size_callback) VertexShader=Gl.glcreateshader (Gl.gl_vertex_shader) Gl.glshadersource (vertexshader, Vertexshadersource) gl.glCompileShader (vertexshader) Fragmentshader=Gl.glcreateshader (Gl.gl_fragment_shader) Gl.glshadersource (Fragmentshader, Fragmentshadersource) Gl.glcompileshader (fragmentshader) Shaderprogram=Gl.glcreateprogram () gl.glattachshader (Shaderprogram, VertexShader) Gl.glattachshader (ShaderProgram, FragmentS Hader) Gl.gllinkprogram (Shaderprogram) gl.gldeleteshader (vertexshader) gl.gldeleteshader (fragmentShader) VAO /c3>= Gl.glgenvertexarrays (1) VBO, EBO= Gl.glgenbuffers (2) Gl.glbindvertexarray (VAO) gl.glbindbuffer (Gl.gl_array_buffer, VBO) gl.glbufferdata (Gl.gl_array_buffer, sys. Getsizeof (vertices), vertices, Gl.gl_static_draw) gl.glbindbuffer (Gl.gl_element_array_buffer, EBO) Gl.glbufferdata ( Gl.gl_element_array_buffer, sys.getsizeof (indices), indices, Gl.gl_static_draw) Gl.glvertexattribpointer ( Gl.glgetattriblocation (Shaderprogram,'APos'), 3, Gl.gl_float, Gl.gl_false, 12, None) gl.glenablevertexattribarray (0) gl.glbindbuffer (gl.gl_array_buffer, 0) gl.glbindvertexarray (0)  while  notglfw.glfwwindowshouldclose (window): processinput (window) Gl.glclearcolor (0.2, 0.3, 0.3, 1.0) gl.glclear (gl.gl_color_buffer_bit) Gl.gluseprogram (Shaderprogram) Gl.glbindvertexarray (VAO) Gl.gldrawelements (Gl.gl_triangles, Len (indices), Gl.gl_unsigned_int, None)#gl.gldrawarrays (gl.gl_triangles, 0, 3)glfw.glfwswapbuffers (window) glfw.glfwpollevents () glfw.glfwterminate ()if __name__=="__main__": Main ()

The code performs the following effects:

Keyboard keys to control the switch between Gl_fill and Gl_line modes

Learn OpenGL's rectangular box under Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.