Learning OpenGL shaders under Python

Source: Internet
Author: User

Https://learnopengl.com/Getting-started/Shaders adds the color attribute of the triangle vertex in the vertex shader, and the Chinese translation of this section is shown in the Chinese course

The corresponding Python code is as follows:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 Importsys, OS5 ImportOpengl.gl as GL6 ImportGLFW7 ImportNumPy as NP8  fromcTYPESImportc_void_p9 TenWin_width = 800 OneWin_height = 600 A  -Vertexshadersource =""" - #version Core the Layout (location = 0) in vec3 aPos; - Layout (location = 1) in vec3 Acolor; - Out vec3 ourcolor; - void Main () + { - gl_position = VEC4 (apos.x, Apos.y, Apos.z, 1.0); + ourcolor = Acolor; A } at """ -  -Fragmentshadersource =""" - #version Core - Out vec4 fragcolor; - In vec3 Ourcolor; in void Main () - { to Fragcolor = VEC4 (Ourcolor, 1.0); + } - """ thevertices = Np.array ([0.5,-0.5, 0, 1.0, 0, 0, *-0.5,-0.5, 0, 0, 1.0, 0, $0.0, 0.5, 0, 0, 0, 1.0], Dtype =Np.float32)Panax Notoginseng  -  the defframebuffer_size_callback (window, width, height): + gl.glviewport (0, 0, width, height) A  the 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) -  the     ifGlfw.glfwgetkey (window, GLFW. glfw_key_right) = =GLFW. Glfw_press: - Gl.glpolygonmode (Gl.gl_front_and_back, Gl.gl_line)Wuyi  the defMain (): - Glfw.glfwinit () WuGlfw.glfwwindowhint (GLFW. Glfw_context_version_major, 3) -Glfw.glfwwindowhint (GLFW. Glfw_context_version_minor, 3) About 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") A glfw.glfwterminate () +  the glfw.glfwmakecontextcurrent (window) - glfw.glfwsetframebuffersizecallback (window, framebuffer_size_callback) $  theVertexShader =Gl.glcreateshader (Gl.gl_vertex_shader) the Gl.glshadersource (vertexshader, Vertexshadersource) the Gl.glcompileshader (vertexshader) the  -Fragmentshader =Gl.glcreateshader (Gl.gl_fragment_shader) in Gl.glshadersource (Fragmentshader, Fragmentshadersource) the Gl.glcompileshader (Fragmentshader) the  AboutShaderprogram =Gl.glcreateprogram () the Gl.glattachshader (Shaderprogram, vertexshader) the Gl.glattachshader (Shaderprogram, Fragmentshader) the Gl.gllinkprogram (Shaderprogram) +  - Gl.gldeleteshader (vertexshader) the Gl.gldeleteshader (Fragmentshader)Bayi  theVAO = gl.glgenvertexarrays (1) theVBO, EBO = Gl.glgenbuffers (2) -  - Gl.glbindvertexarray (VAO) the Gl.glbindbuffer (Gl.gl_array_buffer, VBO) the Gl.glbufferdata (Gl.gl_array_buffer, sys.getsizeof (vertices), vertices, Gl.gl_static_draw) the  theGl.glvertexattribpointer (0, 3, gl.gl_float, Gl.gl_false, 24, c_void_p (0)) - Gl.glenablevertexattribarray (0) theGl.glvertexattribpointer (1, 3, Gl.gl_float, Gl.gl_false, C_void_p (12)) theGl.glenablevertexattribarray (1) the Gl.glbindbuffer (gl.gl_array_buffer, 0)94 Gl.glbindvertexarray (0) the  the      while  notglfw.glfwwindowshouldclose (window): the processinput (window)98Gl.glclearcolor (0.2, 0.3, 0.3, 1.0) About gl.glclear (gl.gl_color_buffer_bit) - 101 Gl.gluseprogram (Shaderprogram)102 103 Gl.glbindvertexarray (VAO)104Gl.gldrawarrays (gl.gl_triangles, 0, 3) the 106 glfw.glfwswapbuffers (window)107 glfw.glfwpollevents ()108 109 glfw.glfwterminate () the 111 if __name__=="__main__": theMain ()

Code Execution Effect:

Learning OpenGL shaders 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.