python下学习opengl之着色器
Posted wndf6122
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python下学习opengl之着色器相关的知识,希望对你有一定的参考价值。
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之着色器的主要内容,如果未能解决你的问题,请参考以下文章