Skip to content

Instantly share code, notes, and snippets.

@dedan
Created October 27, 2011 13:02
Show Gist options
  • Save dedan/1319484 to your computer and use it in GitHub Desktop.
Save dedan/1319484 to your computer and use it in GitHub Desktop.
openGL python
from OpenGL.GL import *
from OpenGL.GLUT import *
from PIL import Image
width, height = 640, 480
def DrawStuff():
poly1 = [(0,0), (640,0), (0,480)]
color = (0.5, 0.4, 0.3, 0.8)
glClear(GL_COLOR_BUFFER_BIT)
glPushMatrix()
glLineWidth(5.0)
glColor4f(*color)
glBegin(GL_POLYGON)
glVertex2f(poly1[0][0], poly1[0][1])
glVertex2f(poly1[1][0], poly1[1][1])
glVertex2f(poly1[2][0], poly1[2][1])
glVertex2f(poly1[0][0], poly1[0][1])
glEnd() # GL_POLYGON
glPopMatrix()
glPixelStorei(GL_PACK_ALIGNMENT, 1)
data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
image = Image.fromstring("RGBA", (width, height), data)
image.show()
image.save('out.png', 'PNG')
glutSwapBuffers()
# glut initialization
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowSize(width, height)
glutCreateWindow("Draw Polygons")
# set the function to draw
glutDisplayFunc(DrawStuff)
# enable the alpha blending
glEnable(GL_BLEND)
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
# prepare for 2D drawing
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width, height, 0, 0, 1)
glDisable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)
# start the mainloop
glutMainLoop ()
@szabolcsdombi
Copy link

Hello,

Try ModernGL, it is faster and much more programmer-friendly then PyOpenGL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment