0
|
1 |
#
|
|
2 |
# gles_demo.py
|
|
3 |
#
|
|
4 |
# Copyright (c) 2006-2007 Nokia Corporation
|
|
5 |
#
|
|
6 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7 |
# you may not use this file except in compliance with the License.
|
|
8 |
# You may obtain a copy of the License at
|
|
9 |
#
|
|
10 |
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11 |
#
|
|
12 |
# Unless required by applicable law or agreed to in writing, software
|
|
13 |
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
14 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15 |
# See the License for the specific language governing permissions and
|
|
16 |
# limitations under the License.
|
|
17 |
#
|
|
18 |
|
|
19 |
import appuifw, sys, e32, time
|
|
20 |
from glcanvas import *
|
|
21 |
from gles import *
|
|
22 |
from key_codes import *
|
|
23 |
|
|
24 |
class GLESDemo:
|
|
25 |
varray = array(GL_BYTE, 3, [
|
|
26 |
-1,1,1, 1,1,1, 1,-1,1, -1,-1,1,
|
|
27 |
-1,1,-1, 1,1,-1, 1,-1,-1, -1,-1,-1
|
|
28 |
])
|
|
29 |
|
|
30 |
indices = array(GL_UNSIGNED_BYTE, 3, [
|
|
31 |
1,0,3, 1,3,2, 2,6,5, 2,5,1, 7,4,5, 7,5,6,
|
|
32 |
0,4,7, 0,7,3, 5,4,0, 5,0,1, 3,7,6, 3,6,2
|
|
33 |
])
|
|
34 |
|
|
35 |
colors = array(GL_UNSIGNED_BYTE, 4, [
|
|
36 |
0,255,0,255, 0,0,255,255, 0,255,0,255, 255,0,0,255,
|
|
37 |
0,0,255,255, 255,0,0,255, 0,0,255,255, 0,255,0,255
|
|
38 |
])
|
|
39 |
|
|
40 |
texcoords = array(GL_BYTE, 2, [
|
|
41 |
0,0, 0,1, 1,0, 1,1, 0,0, 0,1, 1,0, 1,1
|
|
42 |
] )
|
|
43 |
|
|
44 |
# initialize texture array (just used for passing texture data to glTexImage2D or glTexSubImage2D...)
|
|
45 |
texture = array(GL_UNSIGNED_BYTE, 4, [
|
|
46 |
255,0,0,255, 255,0,0,255, 0,255,0,255, 0,255,0,255,
|
|
47 |
255,0,0,255, 255,0,0,255, 0,255,0,255, 0,255,0,255,
|
|
48 |
0,0,255,255, 0,0,255,255, 0,255,255,255, 0,255,255,255,
|
|
49 |
0,0,255,255, 0,0,255,255, 0,255,255,255, 0,255,255,255,
|
|
50 |
] )
|
|
51 |
|
|
52 |
def __init__(self):
|
|
53 |
"""Initializes OpenGL ES, sets the vertex and color arrays and pointers,
|
|
54 |
and selects the shading mode."""
|
|
55 |
# It's best to set these before creating the GL Canvas
|
|
56 |
self.iFrame=0
|
|
57 |
self.exitflag = False
|
|
58 |
|
|
59 |
self.render=0
|
|
60 |
|
|
61 |
self.old_body=appuifw.app.body
|
|
62 |
try:
|
|
63 |
self.canvas=GLCanvas(redraw_callback=self.redraw)
|
|
64 |
appuifw.app.body=self.canvas
|
|
65 |
except Exception,e:
|
|
66 |
appuifw.note(u"Exception: %s" % (e))
|
|
67 |
self.set_exit()
|
|
68 |
return
|
|
69 |
|
|
70 |
appuifw.app.menu = [
|
|
71 |
(u"Exit", self.set_exit)
|
|
72 |
]
|
|
73 |
|
|
74 |
self.initgl()
|
|
75 |
self.render=1
|
|
76 |
|
|
77 |
def initgl(self):
|
|
78 |
# Initialize texture stuff
|
|
79 |
self.texhandle = glGenTextures( 1 )
|
|
80 |
glBindTexture(GL_TEXTURE_2D, self.texhandle)
|
|
81 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, self.texture)
|
|
82 |
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
|
|
83 |
|
|
84 |
# Disable mip mapping
|
|
85 |
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR )
|
|
86 |
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR )
|
|
87 |
|
|
88 |
# Initialize array pointers
|
|
89 |
glVertexPointerb(self.varray)
|
|
90 |
|
|
91 |
glColorPointerub(self.colors)
|
|
92 |
glTexCoordPointerb(self.texcoords)
|
|
93 |
glEnableClientState(GL_VERTEX_ARRAY)
|
|
94 |
glEnableClientState(GL_COLOR_ARRAY)
|
|
95 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
|
|
96 |
|
|
97 |
# Set up state
|
|
98 |
glEnable(GL_CULL_FACE)
|
|
99 |
glEnable(GL_TEXTURE_2D)
|
|
100 |
glDisable(GL_DEPTH_TEST)
|
|
101 |
glClearColorx(0,0,0,65536)
|
|
102 |
glClear(GL_COLOR_BUFFER_BIT)
|
|
103 |
|
|
104 |
glViewport(0, 0, self.canvas.size[0], self.canvas.size[1])
|
|
105 |
glMatrixMode( GL_PROJECTION )
|
|
106 |
glFrustumf( -1.0, 1.0, -1.0, 1.0, 3.0, 1000.0 )
|
|
107 |
glMatrixMode( GL_MODELVIEW )
|
|
108 |
glLoadIdentity()
|
|
109 |
glTranslatef(0,0,-100.0)
|
|
110 |
glScalef(15,15,15)
|
|
111 |
|
|
112 |
glLoadIdentity()
|
|
113 |
glTranslatef(0,0,-100.0)
|
|
114 |
glScalef(15,15,15)
|
|
115 |
|
|
116 |
def redraw(self,frame=None):
|
|
117 |
"""Draws and animates the objects.
|
|
118 |
The frame number determines the amount of rotation."""
|
|
119 |
if self.render != 1:
|
|
120 |
return
|
|
121 |
self.iFrame += 1
|
|
122 |
|
|
123 |
glClear(GL_COLOR_BUFFER_BIT)
|
|
124 |
|
|
125 |
glPushMatrix()
|
|
126 |
glTranslatef(-2,-2,-2)
|
|
127 |
glRotatef(self.iFrame/1.1, 5,2,3)
|
|
128 |
glMatrixMode( GL_TEXTURE )
|
|
129 |
glLoadIdentity()
|
|
130 |
glRotatef(self.iFrame/0.7, 0.5, 0.7, 0.2)
|
|
131 |
glScalef(10,10,10)
|
|
132 |
glMatrixMode( GL_MODELVIEW )
|
|
133 |
glDrawElementsub(GL_TRIANGLES, self.indices)
|
|
134 |
glPopMatrix()
|
|
135 |
|
|
136 |
glPushMatrix()
|
|
137 |
glTranslatef(2,3,-3)
|
|
138 |
glRotatef(self.iFrame/1.8, 3,2,3)
|
|
139 |
glMatrixMode( GL_TEXTURE )
|
|
140 |
glLoadIdentity()
|
|
141 |
glRotatef(self.iFrame/0.7, 0.1, 0.2, 0.3)
|
|
142 |
glScalef(10,10,10)
|
|
143 |
glMatrixMode( GL_MODELVIEW )
|
|
144 |
glDrawElementsub(GL_TRIANGLES, self.indices)
|
|
145 |
glPopMatrix()
|
|
146 |
|
|
147 |
glPushMatrix()
|
|
148 |
glRotatef(self.iFrame/1.5, 1,2,3)
|
|
149 |
glMatrixMode( GL_TEXTURE )
|
|
150 |
glLoadIdentity()
|
|
151 |
glRotatef(self.iFrame/0.5, 0.5, 0.3, 0.2)
|
|
152 |
glScalef(10,10,10)
|
|
153 |
glMatrixMode( GL_MODELVIEW )
|
|
154 |
glDrawElementsub(GL_TRIANGLES, self.indices)
|
|
155 |
glPopMatrix()
|
|
156 |
|
|
157 |
def close_canvas(self): # break reference cycles
|
|
158 |
# Uninitializing OpenGL calls should be made before the GLCanvas is deleted
|
|
159 |
glDeleteTextures([self.texhandle])
|
|
160 |
appuifw.app.body=self.old_body
|
|
161 |
self.canvas=None
|
|
162 |
|
|
163 |
appuifw.app.exit_key_handler=None
|
|
164 |
|
|
165 |
def set_exit(self):
|
|
166 |
self.exitflag = True
|
|
167 |
self.render = 0
|
|
168 |
|
|
169 |
def run(self):
|
|
170 |
appuifw.app.exit_key_handler=self.set_exit
|
|
171 |
while not self.exitflag:
|
|
172 |
self.canvas.drawNow()
|
|
173 |
e32.ao_sleep(0.0001)
|
|
174 |
self.close_canvas()
|
|
175 |
|
|
176 |
appuifw.app.screen='full'
|
|
177 |
try:
|
|
178 |
app=GLESDemo()
|
|
179 |
except Exception,e:
|
|
180 |
appuifw.note(u"Cannot start: %s" % (e))
|
|
181 |
else:
|
|
182 |
app.run()
|
|
183 |
del app
|