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

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