root/trunk/RBRapier/RBRapier/Renderer/View/Transformations.py

Revision 704, 7.6 kB (checked in by sholloway, 5 years ago)

*** empty log message ***

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ License
4 ##~
5 ##- The RuneBlade Foundation GL is intended to ease some
6 ##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.)
7 ##- applications, while providing the flexibility to modularly change the
8 ##- architecture. Enjoy.
9 ##~
10 ##~ Copyright (C) 2002  TechGame Networks, LLC.
11 ##~
12 ##~ This GL is free software; you can redistribute it and/or
13 ##~ modify it under the terms of the BSD style License as found in the
14 ##~ LICENSE file included with this distribution.
15 ##~
16 ##~ TechGame Networks, LLC can be reached at:
17 ##~ 3578 E. Hartsel Drive #211
18 ##~ Colorado Springs, Colorado, USA, 80920
19 ##~
20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #~ Imports
24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 from RBRapier.Tools import Transformations
27 from RBRapier.Tools import Projections
28 from RBRapier.Tools import Quaternion
29 from RBRapier.Tools import ViewBox
30
31 from Numeric import transpose
32 from OpenGL import GL, GLU
33
34 try:
35     GL.GL_COLOR_MATRIX
36 except AttributeError:
37     GL.GL_COLOR_MATRIX = 0x80B1
38
39 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 #~ Definitions
41 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
43 def GLExecuteAsMatrix(self, context):
44     GL.glMultMatrixd(transpose(self.asArray4x4()).tolist())
45 def GLExecuteLoadMatrix(self, context):
46     GL.glLoadMatrixd(transpose(self.asArray4x4()).tolist())
47
48 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
50 class ManagedTransformationMixin(object):
51     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52     #~ Constants / Variables / Etc.
53     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54
55     _ModeNameTable = {None:"< None >", GL.GL_MODELVIEW:"Model View", GL.GL_PROJECTION:"Projection", GL.GL_TEXTURE:"Texture", GL.GL_COLOR:"Color"}
56     _SaveMatrixLookup = {
57         None: GL.GL_MODELVIEW_MATRIX,
58         GL.GL_MODELVIEW: GL.GL_MODELVIEW_MATRIX,
59         GL.GL_PROJECTION: GL.GL_PROJECTION_MATRIX,
60         GL.GL_TEXTURE: GL.GL_TEXTURE_MATRIX,
61         GL.GL_COLOR: GL.GL_COLOR_MATRIX,
62         }
63     Mode = None
64     Save = 0
65     _save_matrix = None
66
67     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68     #~ Public Methods
69     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70
71     def __init__(self, Mode=None, Save=0, *args, **kw):
72         super(ManagedTransformationMixin, self).__init__(*args, **kw)
73         if Mode is not None:
74             self.Mode = Mode
75         if Save is not 0:
76             self.Save = Save
77
78     def GLSelect(self, context):
79         if self.Mode:
80             GL.glMatrixMode(self.Mode)
81             if self.Save:
82                 self._SaveMatrix()
83             self.GLExecute(context)
84             GL.glMatrixMode(GL.GL_MODELVIEW)
85         elif self.Save:
86             self._SaveMatrix()
87             self.GLExecute(context)
88         else:
89             self.GLExecute(context)
90
91     def GLDeselect(self, context):
92         if self.Save:
93             if self.Mode:
94                 GL.glMatrixMode(self.Mode)
95                 self._RestoreMatrix()
96                 GL.glMatrixMode(GL.GL_MODELVIEW)
97             else:
98                 self._RestoreMatrix()
99
100     def _SaveMatrix(self):
101         try:
102             GL.glPushMatrix()
103         except GL.GLerror, e:
104             self._save_matrix = GL.glGetDouble(self._SaveMatrixLookup[self.Mode])
105     #_SaveMatrix = staticmethod(GL.glPushMatrix)
106
107     def _RestoreMatrix(self):
108         oldmatrix = self._save_matrix
109         if oldmatrix is None:
110             GL.glPopMatrix()
111         else:
112             GL.glLoadMatrixd(oldmatrix)
113             del self._save_matrix
114     #_RestoreMatrix = staticmethod(GL.glPopMatrix)
115
116 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117
118 class Composite(Transformations.Composite):
119     def GLExecute(self, context):
120         for each in self.collection:
121             each.GLExecute(context)
122
123 class CompositeMgd(ManagedTransformationMixin, Composite):
124     pass
125 ManagedComposite = CompositeMgd
126
127 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128
129 class Matrix(Transformations.Matrix):
130     GLExecute = GLExecuteAsMatrix
131
132 class MatrixMgd(ManagedTransformationMixin, Matrix):
133     pass
134
135 class LoadMatrix(Transformations.Matrix):
136     GLExecute = GLExecuteLoadMatrix
137
138 class LoadMatrixMgd(ManagedTransformationMixin, LoadMatrix):
139     pass
140
141 class Identity(Transformations.Identity):
142     def GLExecute(self, context):
143         pass # Uh.... don't use this?
144
145 class IdentityMgd(ManagedTransformationMixin, Identity):
146     pass
147
148 class LoadIdentity(Transformations.Identity):
149     def GLExecute(self, context):
150         GL.glLoadIdentity()
151
152 class LoadIdentityMgd(ManagedTransformationMixin, LoadIdentity):
153     pass
154
155 class Translate(Transformations.Translate):
156     def GLExecute(self, context):
157         GL.glTranslated(*self.Direction[:3])
158
159 class TranslateMgd(ManagedTransformationMixin, Translate):
160     pass
161
162 class Scale(Transformations.Scale):
163     def GLExecute(self, context):
164         GL.glScaled(*self.Scale[:3])
165
166 class ScaleMgd(ManagedTransformationMixin, Scale):
167     pass
168
169 class Rotate(Transformations.Rotate):
170     def GLExecute(self, context):
171         GL.glRotated(self.Angle, *self.Axis[:3])
172
173 class RotateMgd(ManagedTransformationMixin, Rotate):
174     pass
175
176 class Quaternion(Quaternion.Quaternion):
177     GLExecute = GLExecuteAsMatrix
178
179 class QuaternionMgd(ManagedTransformationMixin, Quaternion):
180     pass
181
182 class LinearMappingMatrix(Transformations.LinearMappingMatrix):
183     GLExecute = GLExecuteAsMatrix
184
185 class LinearMappingMatrixMgd(ManagedTransformationMixin, LinearMappingMatrix):
186     pass
187
188 class ViewBoxMappingMatrix(ViewBox.ViewBoxMappingMatrix3dh):
189     GLExecute = GLExecuteAsMatrix
190
191 class ViewBoxMappingMatrixMgd(ManagedTransformationMixin, ViewBoxMappingMatrix):
192     pass
193
194 class LookAt(Transformations.LookAt):
195     def GLExecute(self, context):
196         args= self.Eye.tolist() + self.Center.tolist() + self.Up.tolist()
197         GLU.gluLookAt(*args)
198
199 class LookAtMgd(ManagedTransformationMixin, LookAt):
200     pass
201
202 class SphericalLookAt(Transformations.SphericalLookAt):
203     def GLExecute(self, context):
204         args= self.Eye.tolist() + self.Center.tolist() + self.Up.tolist()
205         GLU.gluLookAt(*args)
206
207 class SphericalLookAtMgd(ManagedTransformationMixin, SphericalLookAt):
208     pass
209
210 class Shear(Transformations.Shear):
211     GLExecute = GLExecuteAsMatrix
212
213 class ShearMgd(ManagedTransformationMixin, Shear):
214     pass
215
216 class Skew(Transformations.Skew):
217     GLExecute = GLExecuteAsMatrix
218
219 class SkewMgd(ManagedTransformationMixin, Skew):
220     pass
221
222 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
223 #~ Projections
224 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
225
226 class ProjectionModelMixin(object):
227     ProjectionModel = None
228
229     def ApplyProjectionModel(self):
230         projmodel = self.ProjectionModel
231         if projmodel is not None:
232             projmodel.UpdateProjection(self)
233
234 class Orthographic(Projections.Orthographic, ProjectionModelMixin):
235     def GLExecute(self, context):
236         self.ApplyProjectionModel()
237         GL.glOrtho(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far)
238
239 class OrthographicMgd(ManagedTransformationMixin, Orthographic):
240     pass
241
242 class Frustum(Projections.Frustum, ProjectionModelMixin):
243     def GLExecute(self, context):
244         self.ApplyProjectionModel()
245         GL.glFrustum(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far)
246
247 class FrustumMgd(ManagedTransformationMixin, Frustum):
248     pass
249
250 class Perspective(Projections.Perspective, ProjectionModelMixin):
251     def GLExecute(self, context):
252         self.ApplyProjectionModel()
253         GL.glFrustum(self.Left, self.Right, self.Bottom, self.Top, self.Near, self.Far)
254
255 class PerspectiveMgd(ManagedTransformationMixin, Perspective):
256     pass
257
Note: See TracBrowser for help on using the browser.