python下学习opengl之矩形框
Posted wndf6122
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python下学习opengl之矩形框相关的知识,希望对你有一定的参考价值。
https://learnopengl.com/Getting-started/Hello-Triangle 实现了最简单的着色器,其翻译版请见对应的中文教程
以下是对上述教程中c++代码的python实现:
#! /usr/bin/env python # -*- coding: utf-8 -*- import sys, os import OpenGL.GL as gl import glfw import numpy as np WIN_WIDTH = 800 WIN_HEIGHT = 600 vertexShaderSource = """ #version 330 core layout (location = 0) in vec3 aPos; void main() { gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); } """ fragmentShaderSource = """ #version 330 core out 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) def framebuffer_size_callback(window, width, height): gl.glViewport(0, 0, width, height) def processInput(window): if glfw.glfwGetKey(window, glfw.GLFW_KEY_ESCAPE) == glfw.GLFW_PRESS: glfw.glfwSetWindowShouldClose() if glfw.glfwGetKey(window, glfw.GLFW_KEY_LEFT) == glfw.GLFW_PRESS: gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL) if glfw.glfwGetKey(window, glfw.GLFW_KEY_RIGHT) == glfw.GLFW_PRESS: gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE) def main(): 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, "学习OpenGL".encode(), 0, 0) if window == 0: print("failed to create window") glfw.glfwTerminate() glfw.glfwMakeContextCurrent(window) glfw.glfwSetFramebufferSizeCallback(window, framebuffer_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, fragmentShader) gl.glLinkProgram(shaderProgram) gl.glDeleteShader(vertexShader) gl.glDeleteShader(fragmentShader) VAO = 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 not glfw.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()
代码执行效果如下:
键盘的左右键可以控制GL_FILL和GL_LINE模式之间的切换
以上是关于python下学习opengl之矩形框的主要内容,如果未能解决你的问题,请参考以下文章
openGL之API学习(一六七)默认着色器 顶点属性索引 别名索引