首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >基于此C++现代OpenGL教程,如何让PyOpenGL与wxPython上下文一起工作?

基于此C++现代OpenGL教程,如何让PyOpenGL与wxPython上下文一起工作?
EN

Stack Overflow用户
提问于 2016-09-28 04:59:40
回答 1查看 2.2K关注 0票数 3

我一直关注this tutorial,使用着色器和现代OpenGL功能(如顶点数组对象、andVertex缓冲区对象)绘制一个简单的三角形。教程代码是用Python语言编写的,但我认为,由于无论使用哪种绑定,OpenGL都是相同的,因此很容易转换为Python语言。主要区别是我使用wxPython glCanvas上下文来创建一个要在其中绘图的窗口。这就是我到目前为止所知道的:

import wx
from wx import glcanvas
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GL.ARB.shader_objects import *
from OpenGL.GL.ARB.fragment_shader import *
from OpenGL.GL.ARB.vertex_shader import *
import numpy as np

vertexSource = """
#version 130
in vec2 position;
void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
}
"""
fragmentSource = """
#version 130
out vec4 outColor;
void main()
{
    outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
"""

class OpenGLCanvas(glcanvas.GLCanvas):
    def __init__(self, parent):
        glcanvas.GLCanvas.__init__(self, parent, -1, size=(640, 480))
        self.init = False
        self.context = glcanvas.GLContext(self) 

    self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
    self.Bind(wx.EVT_PAINT, self.OnPaint)

def OnEraseBackground(self, event):
    pass # Do nothing, to avoid flashing on MSW.

def OnPaint(self, event):
    dc = wx.PaintDC(self)
    self.SetCurrent(self.context)
    if not self.init:
        self.InitGL()
        self.init = True
    self.OnDraw()

def InitGL(self):

    # Vertex Input
    ## Vertex Array Objects
    vao = glGenVertexArrays(1)
    glBindVertexArray(vao)

    ## Vertex Buffer Object
    vbo = glGenBuffers(1) # Generate 1 buffer

    vertices = np.array([0.0,  0.5, 0.5, -0.5, -0.5, -0.5], dtype=np.float32)

    ## Upload data to GPU
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)

    # Compile shaders and combining them into a program
    ## Create and compile the vertex shader
    vertexShader = glCreateShader(GL_VERTEX_SHADER)
    glShaderSource(vertexShader, vertexSource)
    glCompileShader(vertexShader)

    ## Create and compile the fragment shader
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER)
    glShaderSource(fragmentShader, fragmentSource)
    glCompileShader(fragmentShader)

    ## Link the vertex and fragment shader into a shader program
    shaderProgram = glCreateProgram()
    glAttachShader(shaderProgram, vertexShader)
    glAttachShader(shaderProgram, fragmentShader)
    glBindFragDataLocation(shaderProgram, 0, "outColor")
    glLinkProgram(shaderProgram)
    glUseProgram(shaderProgram)

    # Making the link between vertex data and attributes
    posAttrib = glGetAttribLocation(shaderProgram, "position")
    glEnableVertexAttribArray(posAttrib)
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0)

def OnDraw(self):
    # Set clear color
    glClearColor(0.0, 0.0, 0.0, 1.0)
#Clear the screen to black
    glClear(GL_COLOR_BUFFER_BIT)

    # draw six faces of a cube
    glDrawArrays(GL_TRIANGLES, 0, 3)

    self.SwapBuffers()

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Hello World", size=(640,480))
        canvas = OpenGLCanvas(self)

app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()

当我运行代码时,没有python错误或OpenGL错误,并且着色器似乎可以正确编译。但是,它只绘制了一个黑色窗口,没有三角形。我不认为这是wxPython的glcanvas.GLContext的问题,因为在使用不推荐使用的glBegin()和glEnd()命令之前,我已经成功地使用它绘制了一个三角形。

顺便说一句,我想在我的图形用户界面上使用wxPython,它工作得很好。以前有没有人用过这个?

EN

回答 1

Stack Overflow用户

发布于 2016-10-01 14:32:06

如果你仍然有这个问题,我认为问题出在下面这一行和PyOpenGL的工作方式上。我发现,仅仅通过下面的修复,你的演示就能正常工作。

glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, None)

显然0 !=绑定中没有!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39734211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档