arjtilley
arjtilley

Reputation: 41

How do I get PyOpenGL to work with a wxPython context, based on this C++ Modern OpenGL tutorial?

I've been following this tutorial for drawing a simple triangle using shaders and modern OpenGL features such as Vertex Array Objects andVertex Buffer Objects. The tutorial code is in C++, but I figured that as OpenGL is the same whichever bindings you use, it would be easy to transpose into Python. The main difference is I am using the wxPython glCanvas context to create a window to draw in. This is what I have so far:

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()

When I run the code there are no python errors or OpenGL errors, and the shaders appear to compile correctly. However, it just draws a black window, with no triangle. I don't think this is a problem with wxPython's glcanvas.GLContext, as I have sucessfully used it to draw a triangle before using the deprecated glBegin() and glEnd() commands.

Incidentally, someone has converted the same tutorial to use Python and a pyglet context, which works perfectly, however I want to use wxPython for my GUI. Has anyone got this to work before?

Upvotes: 4

Views: 3047

Answers (1)

CodeSurgeon
CodeSurgeon

Reputation: 2465

If you are still having this problem, I think the issue is in this following line and the way PyOpenGL works. I found just making this following fix got your demo to work.

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

Apparently 0 != None in the bindings!

Upvotes: 3

Related Questions