root/trunk/RBRapier/demo/Lightwave/scene.py

Revision 705, 9.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 library 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 library 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 import time
27
28 from wxPython import wx
29 from OpenGL import GL
30 import Numeric
31
32 from RBSkinning import SkinXML
33 from RBSkinning.wxTools.GLViewSetup import GLViewSetup
34
35 from RBRapier.Renderer import SequenceMgr
36 from RBRapier.Renderer.Environment import Buffers
37 from RBRapier.Renderer.Environment import FragmentTests
38 from RBRapier.Renderer.View import Viewport
39 from RBRapier.Renderer.View import Transformations
40 from RBRapier.Renderer.View import TransformationSettings
41 from RBRapier.Renderer.Appearance import PolygonRasterization
42 from RBRapier.Renderer.Appearance import Lighting
43
44 from RBRapier.Renderer.Geometry import ArrayTraversal
45
46 from RBRapier.Formats import GeoObject
47 from RBRapier.Formats.Lightwave import MeshedObject
48
49 from RBRapier.Tools.Visualizers import Grids
50 from RBRapier.Tools.Visualizers import AxisSets
51 #from RBRapier.Tools.Visualizers import ProjectionBoxes
52
53 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54 #~ Constants / Variables / Etc.
55 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56
57 skinxml = """<?xml version='1.0'?>
58 <skin:skin xmlns:skin='http://namespaces.runeblade.com/skin' xmlns:py='http://namespaces.runeblade.com/xmlPython' xmlns='http://namespaces.runeblade.com/wxPythonSkin'>
59     <frame ctxvar='scene.frame' title='Lightwave Scene' show='1' pos='0,0' size='800,600'>
60         <layout fit='0'>
61             <panel>
62                 <layout>
63                     <glcanvas ctxvar='scene.glcanvas' sizercfg='1, wxEXPAND' attribList='[WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_MIN_ALPHA, 8, WX_GL_DEPTH_SIZE, 8]'>
64                     </glcanvas>
65                 </layout>
66             </panel>
67         </layout>
68     </frame>
69
70     <!--
71     <frame title='Demo PyCrust' show='1' pos='850,0' size='400,800'>
72         <layout fit='0'>
73             <pycrust_shell sizercfg='1, wxEXPAND' locals='{"scene":context.scene}'/>
74         </layout>
75     </frame>
76     -->
77 </skin:skin>
78 """
79
80 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 #~ Definitions
82 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83
84 class Scene(object):
85     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86     #~ Constants / Variables / Etc.
87     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88
89     _fps_score = 0
90     frametitle = 'Lightwave Scene'
91
92     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93     #~ Public Methods
94     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95
96     def OnSkinInitialize(self):
97         pass
98
99     def OnSkinFinalize(self):
100         self.viewsetup = GLViewSetup(self.glcanvas)
101         self.viewsetup.OnRender.Add(self.Initialize)
102         self.viewsetup.OnRender.Add(self.Render)
103         self.viewsetup.StartRendering()
104
105     def Initialize(self, glviewsetup, canvas):
106         glviewsetup.OnRender.Remove(self.Initialize)
107
108         self.Sequence = SequenceMgr.RootSequence()
109
110         self.ClearColor = Buffers.ClearColor((0.,0.,0.,0.))
111         self.Sequence.AddSetupElement(self.ClearColor.GLExecute)
112         self.ClearDepth = Buffers.ClearDepth(1.)
113         self.Sequence.AddSetupElement(self.ClearDepth.GLExecute)
114         self.DepthTest = FragmentTests.DepthTest()
115         self.Sequence.AddSetupElement(self.DepthTest.GLSelect)
116         self.Normalization = TransformationSettings.Normalization()
117         self.Sequence.AddSetupElement(self.Normalization.GLSelect)
118         self.Culling = PolygonRasterization.FaceCulling()
119         self.Sequence.AddSetupElement(self.Culling.GLSelect)
120
121         self.Lighting = Lighting.LightingModel()
122         self.Lighting.Active = 1
123         self.Sequence.AddElement(self.Lighting.GLSelect)
124
125         self.Lights = []
126         self.Lights.append(Lighting.Light(0))
127         self.Lights[0].Active = 1
128         self.Lights.append(Lighting.Light(1))
129         self.Lights[1].Active = 1
130         self.Lights[1].Specular = 1., 0., 0.
131         self.Lights[1].Diffuse = .6, .6, .6
132         self.Lights.append(Lighting.Light(2))
133         self.Lights[2].Active = 1
134         self.Lights[2].Specular = 0., 0., 1.
135         self.Lights[2].Diffuse = .6, .6, .6
136         self.Lights.append(Lighting.SpotLight(3, SpotCutoff=10., SpotExponent=16.))
137         self.Lights[3].Active = 1
138         self.Lights[3].Position = (0., .4, 0., 1.)
139         self.Lights[3].SpotDirection = (0., -1., 0., 0.)
140
141         #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142
143         self.Viewport = Viewport.Viewport()
144         self.Sequence.AddElement(self.Viewport.GLExecute)
145
146         self.Projection = Transformations.ManagedComposite(GL.GL_PROJECTION)
147         self.Sequence.AddElement(self.Projection.GLSelect)
148         self.Sequence.AddPostElement(self.Projection.GLDeselect)
149
150         self.Projection.Add(Transformations.LoadIdentity())
151         self.Projection.Perspective = Transformations.Perspective(10, far=100)
152         self.Projection.Add(self.Projection.Perspective)
153         self.Projection.LookAt = Transformations.SphericalLookAt((2., 0., 70.))
154         self.Projection.Add(self.Projection.LookAt)
155
156         self.Sequence.AddElement(self.Lights[0].GLSelect)
157         self.Sequence.AddElement(self.Lights[3].GLSelect)
158
159         self.ModelView = Transformations.ManagedComposite()
160         self.ModelView.Add(Transformations.LoadIdentity())
161         self.ModelView.Rotate = Transformations.Rotate(0, (0, 1, 0))
162         self.ModelView.Rotate.AngleDelta = 1.
163         self.ModelView.Add(self.ModelView.Rotate)
164         self.Sequence.AddElement(self.ModelView.GLSelect)
165         self.Sequence.AddPostElement(self.ModelView.GLDeselect)
166
167         self.Sequence.AddElement(self.Lights[1].GLSelect)
168
169         self.LightXForm = Transformations.ManagedComposite(Save=1)
170         self.LightXForm.Rotate = Transformations.Rotate(60, (0, 1, 0))
171         self.LightXForm.Rotate.AngleDelta = -3.
172         self.LightXForm.Add(self.LightXForm.Rotate)
173         self.Sequence.AddElement(self.LightXForm.GLSelect)
174         self.Sequence.AddElement(self.Lights[2].GLSelect)
175         self.Sequence.AddElement(self.LightXForm.GLDeselect)
176
177         self.Grid = Grids.TextureGrid(GridExtent=1, TextureExtent=10)
178         try: self.Grid.LoadGridTexture('coolgrid.png')
179         except IOError: pass
180         self.Sequence.AddElement(self.Grid)
181
182         #self.Sequence.AddElement(AxisSets.AxisLineSet())
183
184         args = 1,3
185         #self.GeoObj = self.LigthwaveLWO('data/RBText.lwo', *args)
186         self.GeoObj = self.LigthwaveLWO('data/dodecahedron.lwo', *args)
187         #self.GeoObj = self.LigthwaveLWO('data/ki162a.lwo', *args)
188         #self.GeoObj = self.LigthwaveLWO('data/fi110a.lwo', *args)
189         #self.GeoObj = self.LigthwaveLWO('data/ga102a.lwo', *args)
190         #self.GeoObj = self.LigthwaveLWO('data/CHIP.LWO', *args)
191         #self.GeoObj = self.LigthwaveLWO('data/SIM.LWO', *args)
192
193         self.DLOptimize(self.GeoObj)
194         self.Sequence.AddElement(self.GeoObj.Vertices.GLSelect)
195         self.Sequence.AddElement(self.GeoObj.Normals.GLSelect)
196         self.Sequence.AddElement(self.GeoObj.GLExecute)
197         #print "BoundingBox", self.GeoObj.BoundingBox
198
199     def StripColoredLigthwaveLWO(self, name, *args, **kw):
200         GL.glEnable(GL.GL_COLOR_MATERIAL)
201         builder = MeshedObject.MeshedObjectBuilder()
202         builder.GeoObjectFactory = GeoObject.DefaultGeoObject
203         builder.IndexedTraversal = ArrayTraversal.ColoredIndexedCollectionTraversal
204         GeoObj = builder.Build(open(name, 'rb'), *args, **kw)
205         return GeoObj
206
207     def LigthwaveLWO(self, name, *args, **kw):
208         builder = MeshedObject.MeshedObjectBuilder()
209         builder.GeoObjectFactory = GeoObject.DefaultGeoObject
210         GeoObj = builder.Build(open(name, 'rb'), *args, **kw)
211         return GeoObj
212
213     def DLOptimize(self, obj):
214         from RBRapier.Renderer.DisplayList import DisplayList
215         DisplayList.InsertAspect(obj)
216
217     Animate = 1
218     def Render(self, glviewsetup, canvas):
219         # Recalculate the viewport
220         self.Viewport.SetRectangle(canvas.GetClientRect().asTuple())
221         self.Projection.Perspective.SetAspectRatio(self.Viewport.GetAspectRatio())
222         # Draw the stuffage!  =)
223         self.Sequence.GLExecute(None)
224         # Animation
225         if self.Animate:
226             self.ModelView.Rotate.Angle = Numeric.fmod(self.ModelView.Rotate.Angle + self.ModelView.Rotate.AngleDelta, 360.)
227             #self.LightXForm.Rotate.Angle = Numeric.fmod(self.LightXForm.Rotate.Angle + self.LightXForm.Rotate.AngleDelta, 360.)
228
229         if time.clock() - self._fps_score >= 1.:
230             self.frame.SetTitle(self.frametitle + ' [FPS: %1.1f]' % self.Sequence.Statistics['persecond'])
231             self._fps_score = time.clock()
232 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233 #~ Main
234 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235
236 def Run():
237     application = wx.wxPySimpleApp()
238     scene = Scene()
239     scene.OnSkinInitialize()
240     skin = SkinXML(skinxml, application=application, scene=scene)
241     scene.OnSkinFinalize()
242     application.MainLoop()
243
244 if __name__ == '__main__':
245     Run()
246
Note: See TracBrowser for help on using the browser.