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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#! /usr/bin/env python
from __future__ import print_function
import os
if not os.environ.get( 'PYOPENGL_PLATFORM' ):
os.environ['PYOPENGL_PLATFORM'] = 'osmesa'
from math import pi, sin, cos
import OpenGL
OpenGL.USE_ACCELERATE = False
from OpenGL.GL import *
from OpenGL.GL.ARB.fragment_program import glGenProgramsARB
from OpenGL.GLU import *
from OpenGL.osmesa import *
def sphere(radius, slices, stacks):
q = gluNewQuadric()
gluQuadricNormals(q, GLU_SMOOTH)
gluSphere(q, radius, slices, stacks)
gluDeleteQuadric(q)
def cone(base, height, slices, stacks):
q = gluNewQuadric()
gluQuadricDrawStyle(q, GLU_FILL)
gluQuadricNormals(q, GLU_SMOOTH)
gluCylinder(q, base, 0.0, height, slices, stacks)
gluDeleteQuadric(q)
def torus(innerRadius, outerRadius, sides, rings):
ringDelta = 2.0 * pi / rings
sideDelta = 2.0 * pi / sides
theta = 0.0
cosTheta = 1.0
sinTheta = 0.0
for i in range(rings - 1, -1, -1):
theta1 = theta + ringDelta
cosTheta1 = cos(theta1)
sinTheta1 = sin(theta1)
glBegin(GL_QUAD_STRIP)
phi = 0.0
for j in range(sides, -1, -1):
phi += sideDelta
cosPhi = cos(phi)
sinPhi = sin(phi)
dist = outerRadius + innerRadius * cosPhi
glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi)
glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, innerRadius * sinPhi)
glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi)
glVertex3f(cosTheta * dist, -sinTheta * dist, innerRadius * sinPhi)
glEnd()
theta = theta1
cosTheta = cosTheta1
sinTheta = sinTheta1
def render_image():
light_ambient = [0.0, 0.0, 0.0, 1.0]
light_diffuse = [1.0, 1.0, 1.0, 1.0]
light_specular = [1.0, 1.0, 1.0, 1.0]
light_position = [1.0, 1.0, 1.0, 0.0]
red_mat = [1.0, 0.2, 0.2, 1.0]
green_mat = [0.2, 1.0, 0.2, 1.0]
blue_mat = [0.2, 0.2, 1.0, 1.0]
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular)
glLightfv(GL_LIGHT0, GL_POSITION, light_position)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0)
glMatrixMode(GL_MODELVIEW)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glRotatef(20.0, 1.0, 0.0, 0.0)
glPushMatrix()
glTranslatef(-0.75, 0.5, 0.0)
glRotatef(90.0, 1.0, 0.0, 0.0)
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat)
torus(0.275, 0.85, 20, 20)
glPopMatrix()
glPushMatrix()
glTranslatef(-0.75, -0.5, 0.0)
glRotatef(270.0, 1.0, 0.0, 0.0)
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat)
cone(1.0, 2.0, 16, 1)
glPopMatrix()
glPushMatrix()
glTranslatef(0.75, 0.0, -1.0)
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat)
sphere(1.0, 20, 20)
glPopMatrix()
glPopMatrix()
# This is very important!!!
# Make sure buffered commands are finished!!!
glFinish()
def write_ppm(buf, filename):
f = open(filename, "w")
if f:
h, w, c = buf.shape
print( "P3", file=f)
print( "# ascii ppm file created by osmesa",file=f)
print( "%i %i" % (w, h),file=f)
print("255",file=f)
for y in range(h - 1, -1, -1):
for x in range(w):
pixel = buf[y, x]
l = " %3d %3d %3d" % (pixel[0], pixel[1], pixel[2])
f.write(l)
f.write("\n")
if __name__ == '__main__':
from OpenGL import arrays
ctx = OSMesaCreateContext(OSMESA_RGBA, None)
#ctx = OSMesaCreateContextExt(OSMESA_RGBA, 32, 0, 0, None)
width, height = 400, 400
buf = arrays.GLubyteArray.zeros((height, width, 4))
assert(OSMesaMakeCurrent(ctx, buf, GL_UNSIGNED_BYTE, width, height))
assert(OSMesaGetCurrentContext())
program = glGenProgramsARB(1)
assert(program)
z = glGetIntegerv(GL_DEPTH_BITS)
s = glGetIntegerv(GL_STENCIL_BITS)
a = glGetIntegerv(GL_ACCUM_RED_BITS)
print("OpenGL Version: %s"%(extensions.GLQuerier.getVersion()))
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)
render_image()
write_ppm(buf, 'output.ppm')
OSMesaDestroyContext(ctx)
|