`
schi
  • 浏览: 205872 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Modern OpenGL with python (freeglut, glfw, PySide/PyQt)

阅读更多
Base on this tutorial
基于这个教程
http://www.arcsynthesis.org/gltut/Basics/Tut02%20Vertex%20Attributes.html

Required packages:
  • pyopengl
  • numpy

pyopengl with freeglut.
To make freeglut works you need to download a pre-compiled binary from http://www.transmissionzero.co.uk/software/freeglut-devel/ or compile it from source http://freeglut.sourceforge.net/.
Copy the freeglut.dll or freeglut64.dll to site-packages/OpenGL/DLLS/
你需要去下载编译好的freeglut,下载地址http://www.transmissionzero.co.uk/software/freeglut-devel/,把freeglut.dll 或 freeglut64.dll放到site-packages/OpenGL/DLLS/里
# --------------------------------------------------------------------------------
# Copyright (c) 2013 Mack Stone. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# --------------------------------------------------------------------------------

"""
Modern OpenGL with python.
render a color triangle with pyopengl using freeglut.

@author: Mack Stone
"""

import ctypes

import numpy
from OpenGL.GL import *
from OpenGL.GL import shaders
from OpenGL.GLUT import *

VERTEX_SHADER = """
#version 330

layout (location=0) in vec4 position;
layout (location=1) in vec4 color;

smooth out vec4 theColor;

void main()
{
    gl_Position = position;
    theColor = color;
}
"""

FRAGMENT_SHADER = """
#version 330

smooth in vec4 theColor;
out vec4 outputColor;

void main()
{
    outputColor = theColor;
}
"""

shaderProgram = None
VAO = None

def initialize():
    global VERTEX_SHADER
    global FRAGMENT_SHADER
    global shaderProgram
    global VAO
    # compile shaders and program
    vertexShader = shaders.compileShader(VERTEX_SHADER, GL_VERTEX_SHADER)
    fragmentShader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)
    shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

    # triangle position and color
    vertexData = numpy.array([0.0, 0.5, 0.0, 1.0,
                            0.5, -0.366, 0.0, 1.0,
                            -0.5, -0.366, 0.0, 1.0,
                            1.0, 0.0, 0.0, 1.0,
                            0.0, 1.0, 0.0, 1.0,
                            0.0, 0.0, 1.0, 1.0, ],
                            dtype=numpy.float32)

    # create VAO
    VAO = glGenVertexArrays(1)
    glBindVertexArray(VAO)

    # create VBO
    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, vertexData.nbytes, vertexData, GL_STATIC_DRAW)

    # enable array and set up data
    glEnableVertexAttribArray(0)
    glEnableVertexAttribArray(1)
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, None)
    # the last parameter is a pointer
    # python donot have pointer, have to using ctypes
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(48))

    glBindBuffer(GL_ARRAY_BUFFER, 0)
    glBindVertexArray(0)

def render():
    global shaderProgram
    global VAO
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # active shader program
    glUseProgram(shaderProgram)

    glBindVertexArray(VAO)

    # draw triangle
    glDrawArrays(GL_TRIANGLES, 0, 3)

    glBindVertexArray(0)
    glUseProgram(0)

    glutSwapBuffers()

def main():
    # init freeglut
    glutInit([])

    # make a window
    glutInitContextVersion(3, 3)
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE)
    glutInitContextProfile(GLUT_CORE_PROFILE)

    glutInitWindowSize(640, 480)
    glutCreateWindow("pyopengl with glut")

    initialize()

    glutDisplayFunc(render)

    glutMainLoop()

if __name__ == '__main__':
    main()


pyopengl with glfw.
Get pyglfw from https://github.com/nightcracker/pyglfw
There was a glfw.dll included in pyglfw. I think it was a 32bit version. You can replace it to the 64bit version for using python 64bit. The pyglfw only support GLFW 2.x.x version. You can download it from http://sourceforge.net/projects/glfw/. I think it had a small bug that it only works with python2.7. It's easy to fix if you want to using python2.6.
安装pyglfw,下载地址https://github.com/nightcracker/pyglfw
它已经附带了一个glfw.dll,但这版本可能不是你想要或能用的,你可以直接替换成你需要的版本,但pyglfw只支持GLFW 2.x.x的版本,下载地址http://sourceforge.net/projects/glfw/。它有个小bug,使得它只支持python2.7,这个bug不难修复。
# --------------------------------------------------------------------------------
# Copyright (c) 2013 Mack Stone. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# --------------------------------------------------------------------------------

"""
Modern OpenGL with python.
render a color triangle with pyopengl using glfw.

@author: Mack Stone
"""

import ctypes

import numpy
from OpenGL.GL import *
from OpenGL.GL import shaders
import glfw

VERTEX_SHADER = """
#version 330

layout (location=0) in vec4 position;
layout (location=1) in vec4 color;

smooth out vec4 theColor;

void main()
{
    gl_Position = position;
    theColor = color;
}
"""

FRAGMENT_SHADER = """
#version 330

smooth in vec4 theColor;
out vec4 outputColor;

void main()
{
    outputColor = theColor;
}
"""

shaderProgram = None
VAO = None

def initialize():
    global VERTEX_SHADER
    global FRAGMENT_SHADER
    global shaderProgram
    global VAO
    # compile shaders and program
    vertexShader = shaders.compileShader(VERTEX_SHADER, GL_VERTEX_SHADER)
    fragmentShader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)
    shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

    # triangle position and color
    vertexData = numpy.array([0.0, 0.5, 0.0, 1.0,
                            0.5, -0.366, 0.0, 1.0,
                            -0.5, -0.366, 0.0, 1.0,
                            1.0, 0.0, 0.0, 1.0,
                            0.0, 1.0, 0.0, 1.0,
                            0.0, 0.0, 1.0, 1.0, ],
                            dtype=numpy.float32)

    # create VAO
    VAO = glGenVertexArrays(1)
    glBindVertexArray(VAO)

    # create VBO
    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, vertexData.nbytes, vertexData, GL_STATIC_DRAW)

    # enable array and set up data
    glEnableVertexAttribArray(0)
    glEnableVertexAttribArray(1)
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, None)
    # the last parameter is a pointer
    # python donot have pointer, have to using ctypes
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(48))

    glBindBuffer(GL_ARRAY_BUFFER, 0)
    glBindVertexArray(0)

def render():
    global shaderProgram
    global VAO
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # active shader program
    glUseProgram(shaderProgram)

    glBindVertexArray(VAO)

    # draw triangle
    glDrawArrays(GL_TRIANGLES, 0, 3)

    glBindVertexArray(0)
    glUseProgram(0)

    glfw.SwapBuffers()

def main():
    # init glfw
    glfw.Init()

    # make a window
    glfw.OpenWindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.OpenWindowHint(glfw.OPENGL_VERSION_MAJOR, 3)
    glfw.OpenWindowHint(glfw.OPENGL_VERSION_MINOR, 3)

    glfw.OpenWindow(640, 480, 8, 8, 8, 8, 0, 0, glfw.WINDOW)

    initialize()

    while glfw.GetWindowParam(glfw.OPENED):
        render()

    glfw.Terminate()

if __name__ == '__main__':
    main()

pyopengl with PySide/PyQt.
Install PySide/PyQt4. I only tested with PySide. It should works fine with PyQt4.
安装PySide或PyQt4。我只测试了PySide。
# --------------------------------------------------------------------------------
# Copyright (c) 2013 Mack Stone. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# --------------------------------------------------------------------------------

"""
Modern OpenGL with python.
render a color triangle with pyopengl using PySide/PyQt4.

@author: Mack Stone
"""

import ctypes

import numpy
from OpenGL.GL import *
from OpenGL.GL import shaders
from PySide import QtGui, QtOpenGL
#from PyQt4 import QtGui, QtOpenGL

VERTEX_SHADER = """
#version 330

layout (location=0) in vec4 position;
layout (location=1) in vec4 color;

smooth out vec4 theColor;

void main()
{
    gl_Position = position;
    theColor = color;
}
"""

FRAGMENT_SHADER = """
#version 330

smooth in vec4 theColor;
out vec4 outputColor;

void main()
{
    outputColor = theColor;
}
"""

class MyWidget(QtOpenGL.QGLWidget):
    def initializeGL(self):
        glViewport(0, 0, self.width(), self.height())

        # compile shaders and program
        vertexShader = shaders.compileShader(VERTEX_SHADER, GL_VERTEX_SHADER)
        fragmentShader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)
        self.shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

        # triangle position and color
        vertexData = numpy.array([0.0, 0.5, 0.0, 1.0,
                                0.5, -0.366, 0.0, 1.0,
                                -0.5, -0.366, 0.0, 1.0,
                                1.0, 0.0, 0.0, 1.0,
                                0.0, 1.0, 0.0, 1.0,
                                0.0, 0.0, 1.0, 1.0, ],
                                dtype=numpy.float32)

        # create VAO
        self.VAO = glGenVertexArrays(1)
        glBindVertexArray(self.VAO)

        # create VBO
        VBO = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, VBO)
        glBufferData(GL_ARRAY_BUFFER, vertexData.nbytes, vertexData, GL_STATIC_DRAW)

        # enable array and set up data
        glEnableVertexAttribArray(0)
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, None)
        # the last parameter is a pointer
        # python donot have pointer, have to using ctypes
        glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(48))

        glBindBuffer(GL_ARRAY_BUFFER, 0)
        glBindVertexArray(0)

    def paintGL(self):
        glClearColor(0, 0, 0, 1)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # active shader program
        glUseProgram(self.shaderProgram)

        glBindVertexArray(self.VAO)

        # draw triangle
        glDrawArrays(GL_TRIANGLES, 0, 3)

        glBindVertexArray(0)
        glUseProgram(0)


def main():
    import sys

    app = QtGui.QApplication(sys.argv)

    glformat = QtOpenGL.QGLFormat()
    glformat.setVersion(3, 3)
    glformat.setProfile(QtOpenGL.QGLFormat.CoreProfile)
    w = MyWidget(glformat)
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
分享到:
评论

相关推荐

    OpenGL的软件包freeglut、glfw、glew、SDK,都在这里

    例如,使用glfw创建窗口和处理输入,通过glew访问高级OpenGL功能,而freeglut则提供了一些基础的窗口和事件管理。在开发过程中,OpenGL SDK提供的工具和文档可以帮助理解API的使用方法和最佳实践。同时,对于不同的...

    opengl中的freeglut 64位版本

    FreeGLUT是一个开源的OpenGL实用程序工具包,它是GLUT(OpenGL Utility Toolkit)的一个替代品,旨在提供更自由的授权条件和更广泛的平台支持。 标题提到的“opengl中的freeglut 64位版本”意味着这是一个专为64位...

    OpenGL所有库(OpenGL,freeglut、glu、glut、glew、glfw等)的头文件、库文件和dll文件

    头文件是`GLFW/glfw3.h`。 为了在Visual Studio项目中使用这些库,你需要将对应的头文件路径添加到项目的“包含目录”中,库文件路径添加到“库目录”,并链接相应的库。例如,对于freeglut,你可能需要链接`...

    opengl示例。freeglut库

    在本示例中,我们关注的是OpenGL结合FreeGLUT库的使用,这对于初学者来说是一个很好的起点。 FreeGLUT是一个开源的、跨平台的OpenGL实用程序工具包,它提供了窗口管理和输入设备处理的功能,如鼠标和键盘。它是...

    opengl开源库 freeglut2.8

    FreeGLUT是一个开源的库,它是OpenGL Utility Toolkit (GLUT) 的替代品,为开发人员提供了一个跨平台的框架,用于编写OpenGL程序。本文将深入探讨FreeGLUT 2.8版本以及如何在不同IDE中集成和使用它。 **FreeGLUT ...

    openGL glut GLAux freeglut glew opengl开发库

    然而,随着OpenGL版本的发展,许多GLAux的功能已被内建到OpenGL标准中,而GLUT和freeglut也逐渐被更现代的窗口系统API(如SDL或SFML)取代。 在实际开发中,你可能需要结合使用这些库,或者根据项目需求选择更适合...

    openGL开发教程(入门教程/上手教程+freeglut包)

    openGL开发教程(入门教程+freeglut包) freeglut类同glut,freeglut是开源的,

    opengl可用插件freeglut-3.2.1.tar.gz

    FreeGLUT就是这样一种库,它是OpenGL开发中的一个重要辅助工具。 FreeGLUT是GLUT(OpenGL Utility Toolkit)的一个开源替代品,由Bill Hollings创建。GLUT最初是由Mark Kilgard在SGI公司开发的,用于简化跨平台的...

    Opengl(1.1库+freeglut+glew+glfw库)

    在这个压缩包中,我们看到了与OpenGL相关的几个关键组件:FreeGLUT、GLEW和GLFW。 **FreeGLUT**是一个开源的OpenGL实用程序工具包,它提供了窗口管理和输入设备处理的功能。FreeGLUT是最初GLUT(OpenGL Utility ...

    "winform+opengl+freeglut"实现导入两关节机械臂的obj模型,并进行旋转、平移等操作

    工具库:freeglut 窗口:winform 主要实现:将OpenGL绘图窗口嵌入Winform窗口,在OpenGL窗口中导入事先准备好的obj模型,并通过winform窗口上的控件对导入的两关节机械臂进行简单操作并输出结果。

    OPENGL glew2.1 glfw3.2 freeglut3

    本函数库包含glew2.1、glfw3.2、及freeglut3的库函数及lib文件,还有一个配置方法的网址https://blog.csdn.net/ideallic/article/details/52836425。

    QtCreator+minGW+OpenGL第三方库freeglut

    QtCreator开发环境下(minGW编译器),进行OpenGL编程时使用的第三方库,使用时只需将压缩包内文件夹内容复制到Qt安装目录下的minGW目录下的对应文件夹内,如:D:\Qt5.4\5.4\mingw491_32\bin;并在新建工程的.pro...

    opengl工具包 freeglut glut glew gl opengl32 .h文件 .dll文件 .lib文件

    1. **freeglut**: freeglut是OpenGL的一个辅助库,它提供了窗口管理和输入设备处理等功能,作为旧版的GLUT(通用OpenGL界面工具包)的替代品。freeglut不仅兼容GLUT的大部分功能,还增加了一些新的特性,如多窗口...

    vs用opengl环境搭建freeglut+glew 64位 包(自编译)

    在Visual Studio(VS)环境下,开发者通常需要利用特定的库来辅助开发OpenGL程序,比如freeglut和GLEW。这两个库是OpenGL开发中的重要组件,它们提供了额外的功能和支持,使得开发工作更加便利。 首先,我们来详细...

    VS2015下OpenGL开发库(glew、glfw、soil、freeglut)

    在Visual Studio 2015(VS2015)环境下进行OpenGL开发,通常需要几个关键的库来支持,这些库包括GLEW、GLFW、SOIL和FreeGLUT。下面将详细介绍这些库的功能和使用方法。 1. GLEW (OpenGL Extension Wrangler Library...

    OpenGL配置freeglut3.0.0

    OpenGL配置freeglut3.0.0是一个关键的步骤,对于使用OpenGL进行图形编程的开发者来说,这是一个必不可少的过程。freeglut是一个开源的、跨平台的OpenGL实用程序工具包,它为开发人员提供了窗口管理、输入设备处理...

    OpenGL + freeglut + glew + glm 環境配置

    在本文中,我们将探讨如何配置一个基于OpenGL的开发环境,包括freeglut、glew和glm这三个重要的库。 首先,让我们了解这些库的作用: 1. **OpenGL**:作为基础图形库,它定义了一系列接口,允许程序员直接与硬件...

    opengl-freeglut3.2.1-vs2019-useful.rar

    这个“opengl-freeglut3.2.1-vs2019-useful.rar”压缩包包含的是FreeGLUT 3.2.1版本的编译结果,适用于Visual Studio 2019开发环境,使得开发者能够方便地在Windows上配置和使用OpenGL。 在Windows上使用OpenGL通常...

    最新版freeglut3.0.0(.h、.lib、.dll)opengl开发库+vs2015编译

    FreeGLUT是OpenGL的辅助库,它为开发者提供了一个跨平台的API,用于创建OpenGL上下文、窗口系统以及输入设备的管理。最新版FreeGLUT 3.0.0是这个库的一个重要更新,它包含了许多改进和新特性,使得OpenGL编程更加...

    opengl环境配置(freeglut+glew+project)

    FreeGLUT是OpenGL的一个开源替代品,它提供了窗口管理、输入设备处理和基本的OpenGL上下文创建等功能。在没有Microsoft的OpenGL辅助库GLUT的情况下,FreeGLUT是开发者首选的工具。配置FreeGLUT主要包括以下几个步骤...

Global site tag (gtag.js) - Google Analytics