Forked from olymk2/gist:b156f7490e3f5662e2fa0cf42b8c7c63
Last active
March 12, 2017 13:13
-
-
Save matti-kariluoma/7786a620abe87deb54c6012b07ded884 to your computer and use it in GitHub Desktop.
glutexample.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import os | |
import sys | |
import OpenGL | |
# OpenGL.USE_ACCELERATE = False | |
# OpenGL.ERROR_CHECKING = False | |
from OpenGL.GLUT import * | |
from OpenGL.GLU import * | |
from OpenGL import GL as GL | |
from ctypes import * | |
import gi | |
from OpenGL.arrays import vbo | |
from OpenGL.GL import shaders | |
from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, \ | |
glBindVertexArray | |
from numpy import array | |
import numpy as np | |
VERTEX_SHADER = """ | |
#version 330 | |
in vec4 position; | |
void main() | |
{ | |
gl_Position = position; | |
}""" | |
FRAGMENT_SHADER = """ | |
#version 330 | |
out vec4 fragColor; | |
void main() | |
{ | |
fragColor = vec4(1.0, 0.0, 0.0, 1.0); | |
} | |
""" | |
class application_gui(object): | |
def __init__(self): | |
self.OnInit() | |
def OnInit(self): | |
self.vertices = [ | |
0.6, 0.6, 0.0, 1.0, | |
-0.6, 0.6, 0.0, 1.0, | |
0.0, -0.6, 0.0, 1.0] | |
self.vertices = np.array(self.vertices, dtype=np.float32) | |
try: | |
glutInit([]) | |
except: | |
print('freeglut missing, try "sudo apt-get install freeglut3" or similar', file=sys.stderr) | |
sys.exit(1) | |
# make a window | |
glutInitContextVersion(4, 0) | |
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE) | |
# glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE) | |
glutInitContextProfile(GLUT_CORE_PROFILE) | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) | |
glutInitWindowSize(640, 480) | |
glutCreateWindow("pyopengl with glut") | |
self.initialize() | |
self.setup_data() | |
glutDisplayFunc(self.Render) | |
print([(p.name, p.loaded) for p in OpenGL.PlatformPlugin.all()], file=sys.stderr) | |
glutMainLoop() | |
def initialize(self): | |
vs = shaders.compileShader(VERTEX_SHADER, GL.GL_VERTEX_SHADER) | |
fs = shaders.compileShader(FRAGMENT_SHADER, GL.GL_FRAGMENT_SHADER) | |
self.shader = shaders.compileProgram(vs, fs) | |
def setup_data(self): | |
try: | |
self.vertex_array_object = glGenVertexArrays(1) | |
except: | |
array = (c_int * 1)() | |
array[0] = 0 | |
glGenVertexArrays(1, array) | |
self.vertex_array_object = array[0] | |
GL.glBindVertexArray( self.vertex_array_object ) | |
# Generate buffers to hold our vertices | |
self.vertex_buffer = GL.glGenBuffers(1) | |
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vertex_buffer) | |
# Get the position of the 'position' in parameter of our shader and bind it. | |
self.position = GL.glGetAttribLocation(self.shader, 'position') | |
GL.glEnableVertexAttribArray(self.position) | |
# Describe the position data layout in the buffer | |
GL.glVertexAttribPointer(self.position, 4, GL.GL_FLOAT, False, 0, ctypes.c_void_p(0)) | |
# Send the data over to the buffer | |
GL.glBufferData(GL.GL_ARRAY_BUFFER, 48, self.vertices, GL.GL_STATIC_DRAW) | |
# Unbind the VAO first (Important) | |
GL.glBindVertexArray( 0 ) | |
# Unbind other stuff | |
GL.glDisableVertexAttribArray(self.position) | |
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) | |
return True | |
def Render(self): | |
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) | |
GL.glUseProgram(self.shader) | |
self.setup_data() | |
GL.glBindVertexArray( self.vertex_array_object ) | |
GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3) | |
GL.glBindVertexArray( 0 ) | |
GL.glUseProgram(0) | |
try: | |
glFlush() | |
except: | |
pass | |
glutSwapBuffers() | |
app = application_gui() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist improves on some error handling, and demonstrates how to inspect which PlatformPlugin is loaded in PyOpenGL.
Sample runs (Ubuntu 16.10, $ sudo apt-get install python-gobject freeglut3; pip install pyopengl):
$ PYOPENGL_PLATFORM="posix" python glutexample.py
[(nt, False), (linux2, False), (darwin, False), (posix, True), (osmesa, False), (egl, False)]
$ PYOPENGL_PLATFORM="linux2" python glutexample.py
[(nt, False), (linux2, True), (darwin, False), (posix, False), (osmesa, False), (egl, False)]
With more effort, one could attempt to switch the platform on-the-fly by using the reload() function and setting os.environ:
http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module
https://docs.python.org/2/library/os.html#os.environ