I am using Mac OS X Mavericks, iMac with NVIDIA GeForce GTX 660M, so it must support OpenGL version 4.1 and GLSL version 4.1.0. But when I use this:
print("OpenGL: " + str(glGetString(GL_VERSION)))
print("GLSL: " + str(glGetString(GL_SHADING_LANGUAGE_VERSION)))
I get this:
OpenGL: b'2.1 NVIDIA-8.26.26 310.40.45f01'
GLSL: b'1.20'
I found out that I have to enable the main profile
class TestWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
super(TestWindow, self).__init__(parent)
self.data = np.array([
[ -0.90, -0.90 ], [ 0.85, -0.90 ], [ -0.90, 0.85 ],
[ 0.90, -0.85 ], [ 0.90, 0.90 ], [ -0.85, 0.90 ]
], dtype = np.float32)
glformat = QtOpenGL.QGLFormat()
glformat.setVersion(4, 1)
glformat.setProfile(QtOpenGL.QGLFormat.CoreProfile)
glformat.setSampleBuffers( True )
self.widget = GLPlotWidget(glformat)
self.widget.set_data(self.data)
self.setGeometry(100, 100, self.widget.width, self.widget.height)
self.setCentralWidget(self.widget)
self.show()
But this did not solve the problem. Now I found this question where this problem is solved for C ++:
// Add this in initializeGL before m_shader.setAttributeBuffer:
uint vao;
typedef void (APIENTRY *_glGenVertexArrays) (GLsizei, GLuint*);
typedef void (APIENTRY *_glBindVertexArray) (GLuint);
_glGenVertexArrays glGenVertexArrays;
_glBindVertexArray glBindVertexArray;
glGenVertexArrays = (_glGenVertexArrays) QGLWidget::context()->getProcAddress("glGenVertexArrays");
glBindVertexArray = (_glBindVertexArray) QGLWidget::context()->getProcAddress("glBindVertexArray");
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
but how can i do the same in python3?
Here is my full source code:
from PyQt4 import QtGui, QtCore, QtOpenGL
from PyQt4.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GL.shaders import *
class GLPlotWidget(QGLWidget):
width, height = 600, 600
def __init__(self, format = None):
super(GLPlotWidget, self).__init__(format, None)
def set_data(self, data):
self.data = data
self.count = data.shape[0]
self.numVAOs = 2
self.VAOs = [0] * self.numVAOs
self.numVBOs = 2
self.VBOs = [0] * self.numVBOs
self.shader = None
self.vPositionLocation = 0
def initializeGL(self):
glGenVertexArrays(self.numVAOs, self.VAOs)
glBindVertexArray(self.VAOs[0])
glGenBuffers(self.numVBOs, self.VBOs)
glBindBuffer(GL_ARRAY_BUFFER, self.VBOs[0])
glBufferData(GL_ARRAY_BUFFER, self.count, self.data, GL_STATIC_DRAW)
VERTEX_SHADER = compileShader("""
#version 410 core
layout(location = 0) in vec4 vPosition;
void main() {
gl_Position = vPosition;
}
""", GL_VERTEX_SHADER)
FRAGMENT_SHADER = compileShader("""
#version 410 core
out vec4 fColor;
void main() {
fColor = vec4(0.0, 0.0, 1.0, 1.0);
}
""", GL_FRAGMENT_SHADER)
self.shader = compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
glUseProgram(self.shader)
glVertexAttribPointer(self.vPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0)
glEnableVertexAttribArray(self.vPositionLocation)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT)
glBindVertexArray(self.VAOs[0])
glDrawArrays(GL_TRIANGLES, 0, self.count)
glFlush()
def resizeGL(self, width, height):
self.width, self.height = width, height
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-1, 1, -1, 1, -1, 1)
if __name__ == '__main__':
import sys
import numpy as np
import numpy.random as rnd
class TestWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
super(TestWindow, self).__init__(parent)
self.data = np.array([
[ -0.90, -0.90 ], [ 0.85, -0.90 ], [ -0.90, 0.85 ],
[ 0.90, -0.85 ], [ 0.90, 0.90 ], [ -0.85, 0.90 ]
], dtype = np.float32)
glformat = QtOpenGL.QGLFormat()
glformat.setVersion(4, 1)
glformat.setProfile(QtOpenGL.QGLFormat.CoreProfile)
glformat.setSampleBuffers( True )
self.widget = GLPlotWidget(glformat)
self.widget.set_data(self.data)
self.setGeometry(100, 100, self.widget.width, self.widget.height)
self.setCentralWidget(self.widget)
self.show()
app = QtGui.QApplication(sys.argv)
window = TestWindow()
window.show()
app.exec_()