GLCanvas always display black background

Hi guys, I’m a beginner with wxPython and pyOpenGL. Here is a problem I met: when I run the code below, the GLCanvas always shows a black background, I tried to change glClearColor(), but not works. Please point out where my code is wrong, thank you.

"""
Hello World, but with more meat.
"""

import wx
from wx.glcanvas import GLCanvas, GLContext
from OpenGL.GL import *


class GLView(GLCanvas):
    def __init__(self, parent):
        GLCanvas.__init__(self, parent, size=(200, 200))
        # self.Bind(wx.EVT_ERASE_BACKGROUND, self.processEraseBackgroundEvent)
        # self.Bind(wx.EVT_SIZE, self.processSizeEvent)
        # self.OnInitGL()
        self.context = GLContext(self)
        self.SetCurrent(self.context)
        # self.Bind(wx.EVT_PAINT, self.processPaintEvent)
        self.OnDraw()

    # def OnInitGL(self):
    #     glClearColor(0, 0, 1, 1)
    #     glClear(GL_COLOR_BUFFER_BIT)
    #
    # def processPaintEvent(self, event):
    #     self.SetCurrent(self.context)
    #     self.OnDraw()
    #     event.Skip()

    def OnDraw(self):
        # glClear(GL_COLOR_BUFFER_BIT)
        # # glPointSize(3.0)
        #
        glClearColor(0, 0, 1, 1)
        glColor3f(1.0, 1.0, 0.0)
        glBegin(GL_LINES)
        glVertex2f(-5.0, 0.0)
        glVertex2f(5.0, 0.0)
        glVertex2f(0.0, 5.0)
        glVertex2f(0.0, -5.0)
        glEnd()

        glColor3f(1.0, 1.0, 1.0)
        glBegin(GL_LINES)
        # for x in arange(-5.0, 5.0, 0.1):
        for x in (i * 0.1 for i in range(-50, 50)):
            y = x * x
            glVertex2f(x, y)
        glEnd()

        self.SwapBuffers()
        self.Refresh()

class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        # self.panel = ExamplePanel(self)

        glview = GLView(self)
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
        # main_sizer.Add(self.panel)
        main_sizer.Add(glview)
        self.SetSizerAndFit(main_sizer)
        # self.Show()


if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame(None)
    app.MainLoop()

It looks like you’re setting the clear color but not actually doing a clear operation? You probably want to do: glClear(GL_COLOR_BUFFER_BIT). You probably want to setup a projection matrix as well.

Oh, you have some bits commented out… Maybe I can make a minimal example for you if you’re still having trouble with this. I see you posted a while ago.

My current project is using OpenGL as well and I’d be happy to help. – Are you still working on this?

I was able to do a little work on your example code and it seems to be working for me now (see below). Note that I commented out my “glOrtho()” call so it keeps your current coordinate systems. In reality, you’d probably want to setup the orthogonal projection matrix to be something standard like I have, and work within that coordinate system. If you need to, you can use “glScalef()” and “glTranslatef()” to move around.

import wx
from wx.glcanvas import GLCanvas, GLContext
from OpenGL.GL import *

class GLView(GLCanvas):
    def __init__(self, parent):
        GLCanvas.__init__(self, parent, size=(200, 200))
        self.context = GLContext(self)
        self.SetCurrent(self.context)
        self.InitGL()
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Refresh()
        return
    def InitGL(self):
        glViewport(0, 0, self.Size[0], self.Size[1])
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        #glOrtho(0.0, self.Size[0],                                                                      
        #        0.0, self.Size[1],                                                                      
        #        -0.01, 10.0)                                                                            
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glClearColor(0, 0, 1, 1)
        return
    def OnSize(self, event):
        self.InitGL()
        return
    def OnPaint(self, event):
        self.SetCurrent(self.context)
        self.OnDraw()
        event.Skip()
        return
    def OnDraw(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glClearColor(0, 0, 1, 1)
        glColor3f(1.0, 1.0, 0.0)
        glBegin(GL_LINES)
        glVertex2f(-5.0, 0.0)
        glVertex2f(5.0, 0.0)
        glVertex2f(0.0, 5.0)
        glVertex2f(0.0, -5.0)
        glEnd()
        glColor3f(1.0, 1.0, 1.0)
        glBegin(GL_LINES)
        # for x in arange(-5.0, 5.0, 0.1):                                                               
        for x in (i * 0.1 for i in range(-50, 50)):
            y = x * x
            glVertex2f(x, y)
        glEnd()
        self.SwapBuffers()
        self.Refresh()
        return

class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        self.glview = GLView(self)
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
        main_sizer.Add(self.glview)
        self.SetSizerAndFit(main_sizer)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Show()
        return
    def OnSize(self, event):
        self.glview.SetSize(0, 0, self.Size[0], self.Size[1])
        return

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame(None)
    app.MainLoop()

Hi avose,

Thank you very much for your response. The problem was indeed caused by glClear(GL_COLOR_BUFFER_BIT). Making such a basic mistake is truly amusing. Thank you again for providing me with the example code.

No worries. Let me know if you have any further OpenGL questions, happy to help. :sunglasses: