-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender.py
96 lines (65 loc) · 2.01 KB
/
render.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'''
Offscreen rendering using OSMesa context for headless containerized environment
'''
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.osmesa import *
from PIL import Image
from PIL import ImageOps
import os
width, height = 300, 300
def init():
glClearColor(0.5, 0.5, 0.5, 1.0)
glColor(0.0, 1.0, 0.0)
gluOrtho2D(-1.0, 1.0, -1.0, 1.0)
glViewport(0, 0, width, height)
def render():
glClear(GL_COLOR_BUFFER_BIT)
# draw xy axis with arrows
glBegin(GL_LINES)
# x
glVertex2d(-1, 0)
glVertex2d(1, 0)
glVertex2d(1, 0)
glVertex2d(0.95, 0.05)
glVertex2d(1, 0)
glVertex2d(0.95, -0.05)
# y
glVertex2d(0, -1)
glVertex2d(0, 1)
glVertex2d(0, 1)
glVertex2d(0.05, 0.95)
glVertex2d(0, 1)
glVertex2d(-0.05, 0.95)
glEnd()
glFlush()
def draw():
render()
glutSwapBuffers()
def main():
if not os.environ.get('PYOPENGL_PLATFORM') == 'osmesa':
print 'Use with: PYOPENGL_PLATFORM=osmesa'
return 0
ctx = OSMesaCreateContext(OSMESA_RGBA, None)
#ctx = OSMesaCreateContextExt(OSMESA_RGBA, 32, 0, 0, None)
buf = arrays.GLubyteArray.zeros((height, width, 4))
assert(OSMesaMakeCurrent(ctx, buf, GL_UNSIGNED_BYTE, width, height))
assert(OSMesaGetCurrentContext())
z = glGetIntegerv(GL_DEPTH_BITS)
s = glGetIntegerv(GL_STENCIL_BITS)
a = glGetIntegerv(GL_ACCUM_RED_BITS)
print "Depth=%d Stencil=%d Accum=%d" % (z, s, a)
print "Width=%d Height=%d" % (OSMesaGetIntegerv(OSMESA_WIDTH), OSMesaGetIntegerv(OSMESA_HEIGHT))
#OSMesaPixelStore(OSMESA_Y_UP, 0)
print "Rendering..."
init()
render()
print "Saving osmesa.png..."
glPixelStorei(GL_PACK_ALIGNMENT, 1)
data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
image = Image.frombytes("RGBA", (width, height), data)
image = ImageOps.flip(image) # in my case image is flipped top-bottom for some reason
image.save('osmesa.png', 'PNG')
OSMesaDestroyContext(ctx)
print "Done!"
main()