1

I am trying to create Python OpenGL code based on the following example: https://www.opengl.org/wiki/Tutorial2:_VAOs,_VBOs,_Vertex_and_Fragment_Shaders_(C_/_SDL)

I am facing an application crash when invoking glBufferData.

The issue I face is the same as the ones mentioned here: Crash on glBufferData and http://www.cplusplus.com/forum/beginner/47978/

However, the solutions mentioned in these pages aren't still working. I am using PyQt5.5, Python 3.4.0 with PyOpenGL 3.1. Also, GPU Caps Viewer reports that Open GL 4.5 is available on my machine.

Any help or pointers to get moving is much appreciated.

Note: When launching the PyDev debugger at the time of the crash, the check

self.context().isValid()

returned True

The initialization code is below:

from PyQt5 import QtGui, QtCore
import sys, array
from OpenGL import GL

class PulleysWithWeights3D( QtGui.QOpenGLWindow ):

    def __init__(self):
        super().__init__()

        # These are the 4D coordinates of a triangle that needs to be drawn.
        _vertexPos = [ 0.075, 0.075, 0.075, 0.075, \
                      0.275, 0.275, 0.275, 0.075, \
                      0.550, 0.550, 0.550, 0.075 ]

        # These are the RGBA colours for each vertex.
        _vertexCols = [ 0.875, 0.525, 0.075, 0.500, \
                       0.875, 0.525, 0.075, 0.500, \
                       0.875, 0.525, 0.075, 0.500 ]

        self._deltaPositions = array.array( 'f', _vertexPos )
        self._deltaColours = array.array( 'f', _vertexCols )

        self._appSurfaceFormat = QtGui.QSurfaceFormat()

        self._appSurfaceFormat.setProfile( QtGui.QSurfaceFormat.CoreProfile )
        self._appSurfaceFormat.setMajorVersion( 4 )
        self._appSurfaceFormat.setMinorVersion( 2 )
        self._appSurfaceFormat.setRenderableType( QtGui.QSurfaceFormat.OpenGL )
        self._appSurfaceFormat.setSamples( 16 )          
        self._appSurfaceFormat.setSwapBehavior( QtGui.QSurfaceFormat.DoubleBuffer )
        self.setSurfaceType( QtGui.QSurface.OpenGLSurface )

        self.setFormat( self._appSurfaceFormat )

        self.setIcon( QtGui.QIcon('OpenGL.png') )
        self.setTitle( 'Pulleys3D' )
        self.setMinimumSize( QtCore.QSize( 1280, 640 ) )
        self.show()


    def initializeGL(self):

        GL.glClearColor( 0.0, 0.0, 0.0, 0.500 ) # RGBA
        GL.glClearDepthf(1.0)
        GL.glClearStencil(0)
        GL.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT )

        self._vaoColouredDelta = GL.glGenVertexArrays(1) 
        GL.glBindVertexArray( self._vaoColouredDelta )

        self._vboPositions = GL.glGenBuffers(1)
        GL.glBindBuffer( GL.GL_ARRAY_BUFFER, self._vboPositions )

        GL.glBufferData( GL.GL_ARRAY_BUFFER, \
                         self._deltaPositions.buffer_info()[1] * self._deltaPositions.itemsize, \
                         self._deltaPositions.tolist(), GL.GL_STATIC_DRAW )

        # PS: Code has been stopped here.

    def resizeGL(self, wd, ht ):
        GL.glViewport( 0, 0, wd, ht )


    def paintGL(self):
        pass


if __name__ == '__main__':
    _pww3dApp = QtGui.QGuiApplication( sys.argv )
    _pww3d = PulleysWithWeights3D()
    sys.exit( _pww3dApp.exec_() )
Community
  • 1
  • 1
  • The code in the following example seems to use glBufferData with 3 parameters whereas PyOpenGL specifies 4. [link](http://stackoverflow.com/questions/11125827/how-to-use-glbufferdata-in-pyopengl?rq=1). I am trying this out. – Pralines And Cream Apr 07 '16 at 18:44
  • Are there any registered OpenGL errors before glBufferData? – Andreas Apr 07 '16 at 18:54
  • @Andreas No, there are no errors listed in the debug console when single-stepping through the code. – Pralines And Cream Apr 07 '16 at 19:46
  • AFAIK opengl errors are not automatically thrown to debug console. You need to retrieve them manually through glGetErrors and parse them according to the code recieved. – Andreas Apr 07 '16 at 19:49
  • Thank you @Andreas - this is new information. I'll try this. – Pralines And Cream Apr 08 '16 at 03:25
  • @Andreas The glGetError() function always returns `GL.GL_NO_ERROR`. I put this check in multiple places: When creating the VAO binding, On creating the VBO bindings, and after calling `glBindVertexArray` and `glEnableVertexAttribArray`. – Pralines And Cream Apr 08 '16 at 06:24
  • Good. Once before buffer swap is recommended for a release version. I also put one after initialization. That way performance doesn't suffer much and you still have a good idea if the program is working as expected. During development have more checks or find a debugger (gDEBugger) that may break on OpenGL error. – Andreas Apr 09 '16 at 09:27
  • Thank you very much for your helpful pointers, @Andreas. :). – Pralines And Cream Apr 13 '16 at 02:24

1 Answers1

0

The issue has been solved. The call to

self._deltaPositions.tolist()

was causing the problem. I replaced it with

self._deltaPositions.tostring()

and the crash goes away.