summaryrefslogtreecommitdiff
-rw-r--r--examples/gears.py495
-rw-r--r--examples/gles_demo.py169
-rw-r--r--examples/simplecube.py230
3 files changed, 894 insertions, 0 deletions
diff --git a/examples/gears.py b/examples/gears.py
new file mode 100644
index 0000000..9d6098f
--- a/dev/null
+++ b/examples/gears.py
@@ -0,0 +1,495 @@
+#!/usr/bin/env python2.3
+# * 3-D gear wheels. This program is in the public domain.
+# * Brian Paul
+# * Conversion to GLUT by Mark J. Kilgard
+# conversion to Python using PyOpenGL with frame rates ala glxgears
+# Peter Barth
+# Converted to Python for S60/OpenGL ES 1.0 by Teemu Haapoja
+
+import pygame
+from pygame.locals import *
+import sys
+import egl
+from gles import *
+import time
+from math import sin,cos,sqrt,pi
+
+def float2fixed(values):
+ ret = tuple([int(v*pow(2,16)) for v in values])
+ return ret
+
+class Gears:
+ def __init__(self):
+ # It's best to set these before creating the GL Canvas
+ self.frames = 0
+ self.exitflag = False
+ self.render=0
+ self.canvas = None
+ self.angle = 0.0
+ self.tStart = self.t0 = time.time()
+ self.view_rotx=20.0
+ self.view_roty=30.0
+ self.view_rotz=0.0
+
+ # Attributes for an antialiased canvas
+ #egl_attrs = {EGL_SAMPLE_BUFFERS:1, EGL_SAMPLES:4}
+
+ #appuifw.app.screen = 'full'
+ try:
+ pygame.init()
+ self.screen = pygame.display.set_mode( (800,480), FULLSCREEN )
+ egl.create(pygame.display.get_wm_info()['window'])
+ except Exception,e:
+ self.set_exit()
+ return
+
+ # The canvas is created, we are now ready to use GL functions
+ print "GL_RENDERER = %s" % (glGetString(GL_RENDERER))
+ print "GL_VERSION = %s" % (glGetString(GL_VERSION))
+ print "GL_VENDOR = %s" % (glGetString(GL_VENDOR))
+ print "GL_EXTENSIONS = %s" % (glGetString(GL_EXTENSIONS))
+
+
+ try:
+ self.initgl()
+ self.resize()
+ except Exception,e:
+ self.set_exit()
+
+ def event(self, ev):
+ # Change view angle
+ if ev['type'] is not appuifw.EEventKey:
+ return
+ k = ev['scancode']
+ if k == EScancode1:
+ self.view_rotz += 5.0
+ elif k == EScancode3:
+ self.view_rotz -= 5.0
+ elif k == EScancodeUpArrow:
+ self.view_rotx += 5.0
+ elif k == EScancodeDownArrow:
+ self.view_rotx -= 5.0
+ elif k == EScancodeRightArrow:
+ self.view_roty += 5.0
+ elif k == EScancodeLeftArrow:
+ self.view_roty -= 5.0
+
+ def resize(self):
+ glViewport(0, 0, 800, 480)
+ aspect = float(800) / float(480)
+ glMatrixMode( GL_PROJECTION )
+ glLoadIdentity()
+ glFrustumf( -1.0*aspect, 1.0*aspect, -1.0, 1.0, 5.0, 60.0 )
+
+ glMatrixMode(GL_MODELVIEW)
+ glLoadIdentity()
+ glTranslatef(0.0, 0.0, -40.0)
+
+ def initgl(self):
+ """Initializes OpenGL ES and the vertex/normal/triangle data."""
+ pos = (5.0, 5.0, 10.0, 0.0)
+ red = (0.8, 0.1, 0.0, 1.0)
+ green = (0.0, 0.8, 0.2, 1.0)
+ blue = (0.2, 0.2, 1.0, 1.0)
+
+ glLightfv(GL_LIGHT0, GL_POSITION, pos)
+ glEnable(GL_CULL_FACE)
+ glEnable(GL_LIGHTING)
+ glEnable(GL_LIGHT0)
+ glEnable(GL_DEPTH_TEST)
+ glEnableClientState(GL_VERTEX_ARRAY)
+ glEnableClientState(GL_NORMAL_ARRAY)
+
+ # Generate the gears
+ self.gear1 = self.gear( 1.0, 4.0, 1.0, 20, 0.7)
+ self.gear2 = self.gear( 0.5, 2.0, 2.0, 10, 0.7)
+ self.gear3 = self.gear( 1.3, 2.0, 0.5, 10, 0.7)
+
+ # Convert the data
+ self.gear1['vertices'] = array(GL_FIXED, 3, self.gear1['vertices'])
+ self.gear1['normals'] = array(GL_FIXED, 3, self.gear1['normals'])
+ self.gear1['indices'] = array(GL_UNSIGNED_SHORT, 3, self.gear1['indices'])
+
+ self.gear2['vertices'] = array(GL_FIXED, 3, self.gear2['vertices'])
+ self.gear2['normals'] = array(GL_FIXED, 3, self.gear2['normals'])
+ self.gear2['indices'] = array(GL_UNSIGNED_SHORT, 3, self.gear2['indices'])
+
+ self.gear3['vertices'] = array(GL_FIXED, 3, self.gear3['vertices'])
+ self.gear3['normals'] = array(GL_FIXED, 3, self.gear3['normals'])
+ self.gear3['indices'] = array(GL_UNSIGNED_SHORT, 3, self.gear3['indices'])
+
+ glEnable(GL_NORMALIZE)
+ glShadeModel(GL_SMOOTH)
+ self.render=1
+
+ def gear(self, inner_radius, outer_radius, width, teeth, tooth_depth):
+ r0 = float(inner_radius)
+ r1 = float(outer_radius) - tooth_depth/2.0
+ r2 = float(outer_radius) + tooth_depth/2.0
+ da = 2.0*pi / teeth / 4.0
+
+ icount = 0
+ vertices = []
+ indices = []
+ normals = []
+
+ normal = float2fixed((0.0, 0.0, 1.0))
+ # Front face
+ for i in range(teeth):
+ angle = i * 2.0 * pi / teeth
+ angle_next = (i-1) * 2.0 * pi / teeth
+ # Triangle 1
+ v1 = (r0*cos(angle), r0*sin(angle), width*0.5)
+ v2 = (r1*cos(angle), r1*sin(angle), width*0.5)
+ v3 = (r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [int(icount), icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ # Triangle 2
+ v1 = (r0*cos(angle), r0*sin(angle), width*0.5)
+ v2 = (r1*cos(angle_next+3*da), r1*sin(angle_next+3*da), width*0.5)
+ v3 = (r1*cos(angle), r1*sin(angle), width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [int(icount), icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ # Triangle 3
+ v1 = (r0*cos(angle), r0*sin(angle), width*0.5)
+ v2 = (r0*cos(angle_next), r0*sin(angle_next), width*0.5)
+ v3 = (r1*cos(angle_next+3*da), r1*sin(angle_next+3*da), width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [int(icount), icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ # Front sides of teeth
+ da = 2.0*pi / teeth / 4.0
+ normal = float2fixed((0.0, 0.0, 1.0))
+ for i in range(teeth):
+ angle = i * 2.0*pi / teeth
+
+ v1 = (r1*cos(angle), r1*sin(angle), width*0.5)
+ v2 = (r2*cos(angle+da), r2*sin(angle+da), width*0.5)
+ v3 = (r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [int(icount), icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ v1 = (r1*cos(angle), r1*sin(angle), width*0.5)
+ v2 = (r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5)
+ v3 = (r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [int(icount), icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+ normal = float2fixed((0.0, 0.0, -1.0))
+
+ # Back face
+ for i in range(teeth + 1):
+ angle = i * 2.0*pi / teeth
+ angle_next = (i+1) * 2.0*pi / teeth
+ v1 = (r1*cos(angle), r1*sin(angle), -width*0.5)
+ v2 = (r0*cos(angle), r0*sin(angle), -width*0.5)
+ v3 = (r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ v1 = (r0*cos(angle), r0*sin(angle), -width*0.5)
+ v2 = (r0*cos(angle_next), r0*sin(angle_next), -width*0.5)
+ v3 = (r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ v1 = (r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)
+ v2 = (r0*cos(angle_next), r0*sin(angle_next), -width*0.5)
+ v3 = (r1*cos(angle_next), r1*sin(angle_next), -width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ # Back sides of teeth
+ da = 2.0*pi / teeth / 4.0
+ for i in range(teeth):
+ angle = i * 2.0*pi / teeth
+
+ v1 = (r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)
+ v2 = (r2*cos(angle+2*da), r2*sin(angle+2*da),-width*0.5)
+ v3 = (r2*cos(angle+da), r2*sin(angle+da), -width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ v1 = (r1*cos(angle), r1*sin(angle), -width*0.5)
+ v2 = (r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5)
+ v3 = (r2*cos(angle+da), r2*sin(angle+da), -width*0.5)
+ vertices.append( float2fixed(v1) )
+ vertices.append( float2fixed(v2) )
+ vertices.append( float2fixed(v3) )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ # Outward faces of teeth
+ angle = 0 * 2.0*pi / teeth
+ normal = float2fixed((cos(angle), sin(angle), 0.0))
+ for i in range(teeth):
+ angle = i * 2.0*pi / teeth
+ angle_next = (i-1) * 2.0*pi / teeth
+
+ # # # Right side of a teeth
+ v1 = float2fixed((r1*cos(angle), r1*sin(angle), width*0.5))
+ v2 = float2fixed((r1*cos(angle), r1*sin(angle), -width*0.5))
+ u = r2*cos(angle+da) - r1*cos(angle)
+ v = r2*sin(angle+da) - r1*sin(angle)
+ len = sqrt(u*u + v*v)
+ u = u / len
+ v = v / len
+
+ v3 = float2fixed((r2*cos(angle+da), r2*sin(angle+da), width*0.5))
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ v1 = float2fixed((r2*cos(angle+da), r2*sin(angle+da), width*0.5))
+ v2 = float2fixed((r1*cos(angle), r1*sin(angle), -width*0.5))
+ v3 = float2fixed((r2*cos(angle+da), r2*sin(angle+da), -width*0.5))
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+ normal = float2fixed((v, -u, 0.0))
+
+ # # # Left side of a teeth
+ normal = float2fixed((cos(angle), sin(angle), 0.0))
+ v1 = float2fixed((r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5))
+ v2 = float2fixed((r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5))
+ v3 = float2fixed((r2*cos(angle+2*da), r2*sin(angle+2*da),-width*0.5))
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+ u = r1*cos(angle+3*da) - r2*cos(angle+2*da)
+ v = r1*sin(angle+3*da) - r2*sin(angle+2*da)
+
+ v1 = float2fixed((r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5))
+ v2 = float2fixed((r2*cos(angle+2*da), r2*sin(angle+2*da),-width*0.5))
+ v3 = float2fixed((r1*cos(angle+3*da), r1*sin(angle+3*da),-width*0.5))
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+ normal = float2fixed((v, -u, 0.0))
+
+ # # # Top of a teeth
+ v1 = float2fixed((r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5))
+ v2 = float2fixed((r2*cos(angle+da), r2*sin(angle+da), width*0.5))
+ v3 = float2fixed((r2*cos(angle+da), r2*sin(angle+da), -width*0.5))
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ v1 = float2fixed((r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5))
+ v2 = float2fixed((r2*cos(angle+da), r2*sin(angle+da), -width*0.5))
+ v3 = float2fixed((r2*cos(angle+2*da), r2*sin(angle+2*da),-width*0.5))
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ # # # Bottom of a teeth
+ v1 = float2fixed((r1*cos(angle), r1*sin(angle), width*0.5))
+ v2 = float2fixed((r1*cos(angle_next+3*da), r1*sin(angle_next+3*da), width*0.5))
+ v3 = float2fixed((r1*cos(angle_next+3*da), r1*sin(angle_next+3*da), -width*0.5))
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+
+ v1 = float2fixed((r1*cos(angle), r1*sin(angle), width*0.5))
+ v2 = float2fixed((r1*cos(angle_next+3*da), r1*sin(angle_next+3*da), -width*0.5))
+ v3 = float2fixed((r1*cos(angle), r1*sin(angle), -width*0.5))
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal) )
+ icount += 3
+ normal = float2fixed((cos(angle), sin(angle), 0.0))
+
+ # Inside radius cylinder
+ for i in range(teeth + 1):
+ angle = i * 2.0*pi / teeth;
+ angle_next = (i+1) * 2.0*pi / teeth
+ normal = float2fixed((-cos(angle), -sin(angle), 0.0))
+ normal_next = float2fixed((-cos(angle_next), -sin(angle_next), 0.0))
+ v1 = float2fixed((r0*cos(angle), r0*sin(angle), -width*0.5)) # 1
+ v2 = float2fixed((r0*cos(angle), r0*sin(angle), width*0.5)) # 2
+ v3 = float2fixed((r0*cos(angle_next), r0*sin(angle_next), width*0.5)) # 2b
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal,normal_next) )
+ icount += 3
+
+ v1 = float2fixed((r0*cos(angle), r0*sin(angle), -width*0.5)) # 1
+ v2 = float2fixed((r0*cos(angle_next), r0*sin(angle_next), width*0.5)) # 2b
+ v3 = float2fixed((r0*cos(angle_next), r0*sin(angle_next), -width*0.5)) # 1b
+ vertices.append( v1 )
+ vertices.append( v2 )
+ vertices.append( v3 )
+
+ indices.append( [icount, icount+1, icount+2] )
+ normals.append( (normal,normal_next,normal_next) )
+ icount += 3
+
+ return {'vertices':vertices, 'indices':indices, 'normals':normals}
+
+ def framerate(self):
+ t = time.time()
+ self.frames += 1
+ if t - self.t0 >= 5.0:
+ seconds = t - self.t0
+ self.fps = self.frames/seconds
+ print "%.0f frames in %3.1f seconds = %6.3f FPS" % (self.frames,seconds,self.fps)
+ self.t0 = t
+ self.frames = 0
+
+ def redraw(self, frame):
+
+ red = (0.8, 0.1, 0.0, 1.0)
+ green = (0.0, 0.8, 0.2, 1.0)
+ blue = (0.2, 0.2, 1.0, 1.0)
+ self.angle += 2.0
+
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
+
+ #glLoadIdentity()
+ #glTranslatef( 0.0, 0.0, -5.0 )
+ #glScalef( 0.2, 0.2, 0.2)
+
+ glPushMatrix()
+ glRotatef(self.view_rotx, 1.0, 0.0, 0.0)
+ glRotatef(self.view_roty, 0.0, 1.0, 0.0)
+ glRotatef(self.view_rotz, 0.0, 0.0, 1.0)
+
+ # Draw gear 1
+ glPushMatrix()
+ glTranslatef(-3.0, -2.0, 0.0)
+ glRotatef(self.angle, 0.0, 0.0, 1.0)
+
+ glVertexPointerx( self.gear1['vertices'] )
+ glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red)
+
+ glNormalPointerx( self.gear1['normals'] )
+ glDrawElementsus(GL_TRIANGLES, self.gear1['indices'])
+ glPopMatrix()
+
+ # Draw gear 2
+ glPushMatrix()
+ glTranslatef(3.1, -2.0, 0.0)
+ glRotatef(-2.0*self.angle-9.0, 0.0, 0.0, 1.0)
+ glVertexPointerx( self.gear2['vertices'] )
+ glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green)
+
+ glNormalPointerx( self.gear2['normals'] )
+ glDrawElementsus(GL_TRIANGLES, self.gear2['indices'])
+ glPopMatrix()
+
+ # Draw gear 3
+ glPushMatrix()
+ glTranslatef(-3.1, 4.2, 0.0)
+ glRotatef(-2.0*self.angle-25.0, 0.0, 0.0, 1.0)
+ glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue)
+ glVertexPointerx( self.gear3['vertices'] )
+
+ glNormalPointerx( self.gear3['normals'] )
+ glDrawElementsus(GL_TRIANGLES, self.gear3['indices'])
+
+ glPopMatrix()
+
+ glPopMatrix()
+
+ self.framerate()
+
+ def set_exit(self):
+ self.exitflag = True
+
+ def run(self):
+ #appuifw.app.exit_key_handler=self.set_exit
+ frame = 0
+ while not self.exitflag:
+ event = pygame.event.poll()
+ if event:
+ if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
+ self.set_exit()
+ self.redraw(frame)
+ egl.swapbuffers()
+
+if __name__ == '__main__':
+ app=Gears()
+ app.run()
+ del app
diff --git a/examples/gles_demo.py b/examples/gles_demo.py
new file mode 100644
index 0000000..0a9e3fc
--- a/dev/null
+++ b/examples/gles_demo.py
@@ -0,0 +1,169 @@
+#
+# gles_demo.py
+#
+# Copyright (c) 2006 Nokia Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import sys
+import pygame
+from pygame.locals import *
+from gles import *
+import egl
+import time
+
+class GLESDemo:
+ varray = array(GL_BYTE, 3, [
+ -1,1,1, 1,1,1, 1,-1,1, -1,-1,1,
+ -1,1,-1, 1,1,-1, 1,-1,-1, -1,-1,-1
+ ])
+
+ indices = array(GL_UNSIGNED_BYTE, 3, [
+ 1,0,3, 1,3,2, 2,6,5, 2,5,1, 7,4,5, 7,5,6,
+ 0,4,7, 0,7,3, 5,4,0, 5,0,1, 3,7,6, 3,6,2
+ ])
+
+ colors = array(GL_UNSIGNED_BYTE, 4, [
+ 0,255,0,255, 0,0,255,255, 0,255,0,255, 255,0,0,255,
+ 0,0,255,255, 255,0,0,255, 0,0,255,255, 0,255,0,255
+ ])
+
+ texcoords = array(GL_BYTE, 2, [
+ 0,0, 0,1, 1,0, 1,1, 0,0, 0,1, 1,0, 1,1
+ ] )
+
+ # initialize texture array (just used for passing texture data to glTexImage2D or glTexSubImage2D...)
+ texture = array(GL_UNSIGNED_BYTE, 4, [
+ 255,0,0,255, 255,0,0,255, 0,255,0,255, 0,255,0,255,
+ 255,0,0,255, 255,0,0,255, 0,255,0,255, 0,255,0,255,
+ 0,0,255,255, 0,0,255,255, 0,255,255,255, 0,255,255,255,
+ 0,0,255,255, 0,0,255,255, 0,255,255,255, 0,255,255,255,
+ ] )
+
+ def __init__(self):
+ """Initializes OpenGL ES, sets the vertex and color arrays and pointers,
+and selects the shading mode."""
+ # It's best to set these before creating the GL Canvas
+ self.iFrame=0
+ self.exitflag = False
+
+ self.render=0
+
+ pygame.init()
+ self.screen = pygame.display.set_mode( (800,480), FULLSCREEN )
+ egl.create(pygame.display.get_wm_info()['window'])
+ self.initgl()
+ self.render=1
+
+ def initgl(self):
+ # Initialize texture stuff
+ self.texhandle = glGenTextures( 1 )
+ glBindTexture(GL_TEXTURE_2D, self.texhandle)
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, self.texture)
+ glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
+
+ # Disable mip mapping
+ glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR )
+ glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR )
+
+ # Initialize array pointers
+ glVertexPointerb(self.varray)
+
+ glColorPointerub(self.colors)
+ glTexCoordPointerb(self.texcoords)
+ glEnableClientState(GL_VERTEX_ARRAY)
+ glEnableClientState(GL_COLOR_ARRAY)
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY)
+
+ # Set up state
+ glEnable(GL_CULL_FACE)
+ glEnable(GL_TEXTURE_2D)
+ glDisable(GL_DEPTH_TEST)
+ glClearColorx(0,0,0,65536)
+ glClear(GL_COLOR_BUFFER_BIT)
+
+ glViewport(0, 0, 800, 480)
+ glMatrixMode( GL_PROJECTION )
+ aspect = float(800) / float(480)
+ #glFrustumf( -1.0, 1.0, -1.0, 1.0, 3.0, 1000.0 )
+ glFrustumf( -1.0*aspect, 1.0*aspect, -1.0, 1.0, 3.0, 1000.0 )
+ glMatrixMode( GL_MODELVIEW )
+ glLoadIdentity()
+ glTranslatef(0,0,-100.0)
+ glScalef(15,15,15)
+
+ glLoadIdentity()
+ glTranslatef(0,0,-100.0)
+ glScalef(15,15,15)
+
+ def redraw(self,frame=None):
+ """Draws and animates the objects.
+The frame number determines the amount of rotation."""
+ if self.render != 1:
+ return
+ self.iFrame += 1
+
+ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
+
+ glPushMatrix()
+ glTranslatef(-2,-2,-2)
+ glRotatef(self.iFrame/1.1, 5,2,3)
+ glMatrixMode( GL_TEXTURE )
+ glLoadIdentity()
+ glRotatef(self.iFrame/0.7, 0.5, 0.7, 0.2)
+ glScalef(10,10,10)
+ glMatrixMode( GL_MODELVIEW )
+ glDrawElementsub(GL_TRIANGLES, self.indices)
+ glPopMatrix()
+
+ glPushMatrix()
+ glTranslatef(2,3,-3)
+ glRotatef(self.iFrame/1.8, 3,2,3)
+ glMatrixMode( GL_TEXTURE )
+ glLoadIdentity()
+ glRotatef(self.iFrame/0.7, 0.1, 0.2, 0.3)
+ glScalef(10,10,10)
+ glMatrixMode( GL_MODELVIEW )
+ glDrawElementsub(GL_TRIANGLES, self.indices)
+ glPopMatrix()
+
+ glPushMatrix()
+ glRotatef(self.iFrame/1.5, 1,2,3)
+ glMatrixMode( GL_TEXTURE )
+ glLoadIdentity()
+ glRotatef(self.iFrame/0.5, 0.5, 0.3, 0.2)
+ glScalef(10,10,10)
+ glMatrixMode( GL_MODELVIEW )
+ glDrawElementsub(GL_TRIANGLES, self.indices)
+ glPopMatrix()
+
+ def set_exit(self):
+ self.exitflag = True
+ self.render = 0
+
+ def run(self):
+ frame = 0
+ while not self.exitflag:
+ event = pygame.event.poll()
+ if event:
+ if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
+ self.set_exit()
+ self.redraw(frame)
+ egl.swapbuffers()
+ #time.sleep(0.0001)
+ glDeleteTextures([self.texhandle])
+
+app=GLESDemo()
+app.run()
+del app
diff --git a/examples/simplecube.py b/examples/simplecube.py
new file mode 100644
index 0000000..3b053b5
--- a/dev/null
+++ b/examples/simplecube.py
@@ -0,0 +1,230 @@
+#
+# simplecube.py
+#
+# Copyright (c) 2006 Nokia Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import sys
+import egl
+from gles import *
+import time
+import pygame
+from pygame.locals import *
+
+class SimpleCube:
+ vertices = array( GL_BYTE, 3, (
+ [-1, 1, 1],
+ [ 1, 1, 1],
+ [ 1, -1, 1],
+ [-1, -1, 1],
+
+ [-1, 1, -1],
+ [ 1, 1, -1],
+ [ 1, -1, -1],
+ [-1, -1, -1]
+ ) )
+
+ triangles = array( GL_UNSIGNED_BYTE, 3, (
+ # front
+ [1,0,3],
+ [1,3,2],
+
+ # right
+ [2,6,5],
+ [2,5,1],
+
+ # back
+ [7,4,5],
+ [7,5,6],
+
+ # left
+ [0,4,7],
+ [0,7,3],
+
+ # top
+ [5,4,0],
+ [5,0,1],
+
+ # bottom
+ [3,7,6],
+ [3,6,2]
+ ) )
+
+
+ fanOne = array( GL_UNSIGNED_BYTE, 3,(
+ [1,0,3],
+ [1,3,2],
+ [1,2,6],
+ [1,6,5],
+ [1,5,4],
+ [1,4,0]
+ ) )
+
+ fanTwo = array( GL_UNSIGNED_BYTE, 3, (
+ [7,4,5],
+ [7,5,6],
+ [7,6,2],
+ [7,2,3],
+ [7,3,0],
+ [7,0,4]
+ ) )
+
+ colors = array( GL_UNSIGNED_BYTE, 4, (
+ [0 ,255, 0,255],
+ [0 , 0,255,255],
+ [0 ,255, 0,255],
+ [255, 0, 0,255],
+
+ [0 , 0,255,255],
+ [255, 0, 0,255],
+ [0 , 0,255,255],
+ [0 ,255, 0,255]
+ ) )
+
+ ETriangles=0
+ ETriangleFans=1
+
+ def __init__(self):
+ """Initializes OpenGL ES, sets the vertex and color arrays and pointers,
+and selects the shading mode."""
+
+ # It's best to set these before creating the GL Canvas
+ self.iDrawingMode=self.ETriangles
+ self.iFrame=0
+ self.exitflag = False
+ self.render=0
+ self.canvas = None
+
+ pygame.init()
+ self.screen = pygame.display.set_mode( (800,480), FULLSCREEN )
+ egl.create(pygame.display.get_wm_info()['window'])
+
+ self.initgl()
+ self.SmoothShading()
+
+ def event(self, ev):
+ """Event handler"""
+ pass
+
+ def resize(self):
+ """Resize handler"""
+ # This may get called before the canvas is created, so check that the canvas exists
+ glViewport(0, 0, 800, 480)
+ aspect = float(800) / float(480)
+ glMatrixMode( GL_PROJECTION )
+ glLoadIdentity()
+ glFrustumf( -1.0*aspect, 1.0*aspect, -1.0, 1.0, 3.0, 1000.0 )
+
+ def initgl(self):
+ """Initializes OpenGL and sets up the rendering environment"""
+ # Set the screen background color.
+ glClearColor( 0.0, 0.0, 0.0, 1.0 )
+
+ # Enable back face culling.
+ glEnable( GL_CULL_FACE )
+
+ # Initialize viewport and projection.
+ self.resize()
+
+ glMatrixMode( GL_MODELVIEW )
+
+ # Enable vertex arrays.
+ glEnableClientState( GL_VERTEX_ARRAY )
+
+ # Set array pointers.
+ glVertexPointerb(self.vertices)
+
+ # Enable color arrays.
+ glEnableClientState( GL_COLOR_ARRAY )
+
+ # Set color pointers.
+ glColorPointerub(self.colors )
+
+ # Set the initial shading mode
+ glShadeModel( GL_FLAT )
+
+ # Do not use perspective correction
+ glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST )
+ self.render=1
+
+ def FlatShading(self):
+ """Sets the GL shading model to flat."""
+ glShadeModel( GL_FLAT )
+
+ def SmoothShading(self):
+ """Sets the GL shading model to smooth."""
+ glShadeModel( GL_SMOOTH )
+
+ def TriangleMode(self):
+ """Sets the rendering mode to triangles."""
+ self.iDrawingMode = self.ETriangles
+
+ def TriangleFanMode(self):
+ """Sets the rendering mode to triangle fans."""
+ self.iDrawingMode = self.ETriangleFans
+
+ def drawbox(self, aSizeX, aSizeY, aSizeZ):
+ """Draws a box with triangles or triangle fans depending on the current rendering mode.
+Scales the box to the given size using glScalef."""
+ glScalef( aSizeX, aSizeY, aSizeZ )
+
+ if self.iDrawingMode == self.ETriangles:
+ glDrawElementsub( GL_TRIANGLES, self.triangles )
+ elif self.iDrawingMode == self.ETriangleFans:
+ glDrawElementsub( GL_TRIANGLE_FAN, self.fanOne )
+ glDrawElementsub( GL_TRIANGLE_FAN, self.fanTwo )
+
+ def redraw(self,frame):
+ """Draws and animates the objects.
+The frame number determines the amount of rotation."""
+ self.iFrame = frame
+
+ if self.render == 0:
+ return
+ glMatrixMode( GL_MODELVIEW )
+
+ cameraDistance = 100
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
+
+ # Animate and draw box
+ glLoadIdentity()
+ glTranslatex( 0 , 0 , -cameraDistance << 16 )
+ glRotatex( self.iFrame << 16, 1 << 16, 0 , 0 )
+ glRotatex( self.iFrame << 15, 0 , 1 << 16, 0 )
+ glRotatex( self.iFrame << 14, 0 , 0 , 1 << 16 )
+ self.drawbox( 15.0, 15.0, 15.0 )
+
+ def set_exit(self):
+ self.exitflag = True
+
+ def run(self):
+ #appuifw.app.exit_key_handler=self.set_exit
+ frame=0
+ while not self.exitflag:
+ event = pygame.event.poll()
+ if event:
+ if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
+ self.set_exit()
+
+ self.redraw(frame)
+ egl.swapbuffers()
+
+ time.sleep(0.0001)
+ frame += 1
+ #self.close_canvas()
+
+app=SimpleCube()
+app.run()
+del app