python下學習opengl之著色器

來源:互聯網
上載者:User

標籤:back   esc   src   ted   span   pos   version   proc   pen   

https://learnopengl.com/Getting-started/Shaders在vertex著色器中增加了三角形頂點的顏色屬性, 該章節的中文翻譯版見中文教程

對應的python代碼如下:

  1 #! /usr/bin/env python  2 # -*- coding: utf-8 -*-  3   4 import sys, os  5 import OpenGL.GL as gl  6 import glfw  7 import numpy as np  8 from ctypes import c_void_p  9  10 WIN_WIDTH = 800 11 WIN_HEIGHT = 600 12  13 vertexShaderSource = """ 14 #version 330 core 15 layout (location = 0) in vec3 aPos; 16 layout (location = 1) in vec3 aColor; 17 out vec3 ourColor; 18 void main() 19 { 20     gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); 21     ourColor = aColor; 22 } 23 """ 24  25 fragmentShaderSource = """ 26 #version 330 core 27 out vec4 FragColor; 28 in vec3 ourColor; 29 void main() 30 { 31     FragColor = vec4(ourColor, 1.0); 32 } 33 """ 34 vertices = np.array([0.5,-0.5, 0, 1.0, 0, 0, 35                     -0.5,-0.5, 0, 0, 1.0, 0, 36                      0.0, 0.5, 0, 0, 0, 1.0], dtype = np.float32) 37  38  39 def framebuffer_size_callback(window, width, height): 40     gl.glViewport(0, 0, width, height) 41  42 def processInput(window): 43     if glfw.glfwGetKey(window, glfw.GLFW_KEY_ESCAPE) == glfw.GLFW_PRESS: 44         glfw.glfwSetWindowShouldClose() 45  46     if glfw.glfwGetKey(window, glfw.GLFW_KEY_LEFT) == glfw.GLFW_PRESS: 47         gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL) 48  49     if glfw.glfwGetKey(window, glfw.GLFW_KEY_RIGHT) == glfw.GLFW_PRESS: 50         gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE) 51  52 def main(): 53     glfw.glfwInit() 54     glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 3) 55     glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 3) 56     glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE, glfw.GLFW_OPENGL_CORE_PROFILE) 57  58     window = glfw.glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, "學習OpenGL".encode(), 0, 0) 59     if window == 0: 60         print("failed to create window") 61         glfw.glfwTerminate() 62  63     glfw.glfwMakeContextCurrent(window) 64     glfw.glfwSetFramebufferSizeCallback(window, framebuffer_size_callback) 65  66     vertexShader = gl.glCreateShader(gl.GL_VERTEX_SHADER) 67     gl.glShaderSource(vertexShader, vertexShaderSource) 68     gl.glCompileShader(vertexShader) 69  70     fragmentShader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER) 71     gl.glShaderSource(fragmentShader, fragmentShaderSource) 72     gl.glCompileShader(fragmentShader) 73  74     shaderProgram = gl.glCreateProgram() 75     gl.glAttachShader(shaderProgram, vertexShader) 76     gl.glAttachShader(shaderProgram, fragmentShader) 77     gl.glLinkProgram(shaderProgram) 78  79     gl.glDeleteShader(vertexShader) 80     gl.glDeleteShader(fragmentShader) 81  82     VAO = gl.glGenVertexArrays(1) 83     VBO, EBO = gl.glGenBuffers(2) 84  85     gl.glBindVertexArray(VAO) 86     gl.glBindBuffer(gl.GL_ARRAY_BUFFER, VBO) 87     gl.glBufferData(gl.GL_ARRAY_BUFFER, sys.getsizeof(vertices), vertices, gl.GL_STATIC_DRAW) 88  89     gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 24, c_void_p(0)) 90     gl.glEnableVertexAttribArray(0) 91     gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 24, c_void_p(12)) 92     gl.glEnableVertexAttribArray(1) 93     gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) 94     gl.glBindVertexArray(0) 95  96     while not glfw.glfwWindowShouldClose(window): 97         processInput(window) 98         gl.glClearColor(0.2, 0.3, 0.3, 1.0) 99         gl.glClear(gl.GL_COLOR_BUFFER_BIT)100 101         gl.glUseProgram(shaderProgram)102 103         gl.glBindVertexArray(VAO)104         gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3)105 106         glfw.glfwSwapBuffers(window)107         glfw.glfwPollEvents()108 109     glfw.glfwTerminate()110 111 if __name__ == "__main__":112     main()

代碼執行效果:

python下學習opengl之著色器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.