I\'m trying to run glGenVertexArrays on PyOpenGL on my Mac (10.11.5). It is not finding it.
The problem seems to be the version of OpenGL supported by my Mac (?). I\'ve
First of all, I'd suggest each time you use an Opengl function you check which opengl version is required to run it with not problems, for instance, if we look at glGenVertexArrays we'll see you need Opengl >= 3.0. Now, the reason you're getting 2.1 version when doing glGetString(GL_VERSION) is either because you got a really old card (unlikely) or because you haven't enabled the Opengl core profile. Once you do that you should see the right Opengl version and running modern Opengl functions such as the one you're asking for.
In some cases like using pyqt opengl widgets, the context setup will be done behind the curtains for you... Or as you're mentioning if you're using glut there will be a feature to do so.
One way it'd be enabling the core profile manually, which can be tidious if you're a beginner with opengl, if you just want to use opengl without too much hazzle I'd recommend to use something on top of pyopengl, such as pyqt, pyglet, glut, pygame... There are tons of wrappers where you won't find yourself making the manual setup. Below you'll find a simple example which uses QGlWidget which comes with PyQt5 (pip install PyQt5
) which should work out of the box:
import textwrap
import sys
import time
import ctypes
from array import array
from PyQt5 import QtWidgets
from PyQt5.QtOpenGL import QGLWidget
from PyQt5.QtWidgets import QApplication
from OpenGL.GL import *
from OpenGL.GLU import *
class FooOpengl(QGLWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Test to show how at this point Opengl Context setup hasn't been done
# and therefore will crash
print('{:*^80}'.format('Opengl Context not ready'))
try:
print(self._opengl_info())
except Exception as e:
print(e)
self.start_time = time.clock()
self.startTimer(0)
def initializeGL(self):
# Test to show how at this point Opengl Context is ready to go
print('{:*^80}'.format('Opengl Context ready'))
print(self._opengl_info())
# Shaders: Trivial program
vs_source = textwrap.dedent("""
#version 330
in vec3 position;
void main()
{
gl_Position = vec4(position, 1.0);
}\
""")
fs_source = textwrap.dedent("""
#version 330
void main()
{
gl_FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}\
""")
vs = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vs, vs_source)
glCompileShader(vs)
fs = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(fs, fs_source)
glCompileShader(fs)
self.program = glCreateProgram()
glAttachShader(self.program, fs)
glAttachShader(self.program, vs)
glLinkProgram(self.program)
vertices = [
0.0, 0.5, 0.0,
0.5, -0.5, 0.0,
-0.5, -0.5, 0.0
]
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
self.vao = glGenVertexArrays(1)
glBindVertexArray(self.vao)
position = glGetAttribLocation(self.program, 'position')
glEnableVertexAttribArray(position)
glVertexAttribPointer(
position, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0))
glBufferData(
GL_ARRAY_BUFFER, array("f", vertices).tostring(), GL_STATIC_DRAW)
glBindVertexArray(0)
glDisableVertexAttribArray(position)
glBindBuffer(GL_ARRAY_BUFFER, 0)
def timerEvent(self, event):
elapsed = time.clock() - self.start_time
self.repaint()
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glUseProgram(self.program)
glBindVertexArray(self.vao)
glDrawArrays(GL_TRIANGLES, 0, 3)
glBindVertexArray(0)
glUseProgram(0)
def _opengl_info(self):
return textwrap.dedent("""\
Vendor: {0}
Renderer: {1}
OpenGL Version: {2}
Shader Version: {3}
Num Extensions: {4}
Extensions: {5}
""").format(
glGetString(GL_VENDOR).decode("utf-8"),
glGetString(GL_RENDERER).decode("utf-8"),
glGetString(GL_VERSION).decode("utf-8"),
glGetString(GL_SHADING_LANGUAGE_VERSION).decode("utf-8"),
glGetIntegerv(GL_NUM_EXTENSIONS),
glGetString(GL_EXTENSIONS)
)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = FooOpengl()
ex.show()
sys.exit(app.exec_())
It's just a simple test which shows you what happens when the context is ready and when is not, also it gives you some extra candies about the usage of vbo+vao+shaders, it should give you something similar to this:
This should be alright to get you started... As I've said, there are tons of wrappers on top of opengl, if not pyglet, pygame is a very famous one in the python community and it got tons of cool features.