]> Pileus Git - ~andy/cs275-proj/blob - src/test.py
Add PlantGL test code
[~andy/cs275-proj] / src / test.py
1 #!/bin/env python
2
3 # Dependencies:
4 #   PyQt4
5
6 import sys
7
8 from OpenGL.GL import *
9
10 from PyQt4.Qt    import QApplication, QMainWindow, QLabel
11 from PyQGLViewer import QGLViewer, Vec
12
13 from openalea.lpy         import Lsystem
14 from openalea.plantgl.all import Discretizer, GLRenderer
15
16 # Qt Plant Viewer
17 class Viewer(QGLViewer):
18     def __init__(self, lfile):
19         QGLViewer.__init__(self)
20
21         center   = Vec(0.16104, 0.00831, 2.53128)
22         position = Vec(4.74832, 4.66647, 8.79487)
23
24         self.discretizer = Discretizer()
25         self.renderer    = GLRenderer(self.discretizer)
26         self.lsystem     = Lsystem(lfile)
27         self.scene       = self.lsystem.sceneInterpretation(self.lsystem.iterate())
28
29         camera = self.camera()
30         camera.setRevolveAroundPoint(center)
31         camera.setPosition(position)
32         camera.setUpVector(Vec(0,0,1))
33         camera.lookAt(center)
34         camera.setSceneRadius(7)
35
36     def draw(self):
37         light_pos = list(self.camera().position())+[1.0]
38         light_dir = list(self.camera().viewDirection())+[1.0]
39
40         glEnable(GL_LIGHTING)
41         glEnable(GL_LIGHT0)
42
43         glLightfv(GL_LIGHT0, GL_POSITION,       light_pos)
44         glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_dir)
45         glLightfv(GL_LIGHT0, GL_AMBIENT,  (0.6,0.6,0.6,1.0))
46         glLightfv(GL_LIGHT0, GL_DIFFUSE,  (1.0,1.0,1.0,1.0))
47         glLightfv(GL_LIGHT0, GL_SPECULAR, (1.0,1.0,1.0,1.0))
48
49         if self.renderer.beginSceneList():
50             glEnable(GL_RESCALE_NORMAL)
51             self.scene.apply(self.renderer)
52             self.renderer.endSceneList()
53
54 # Test Qt
55 class TestQt:
56     def __init__(self, args):
57         self.args = args;
58
59     def run(self):
60         app  = QApplication(self.args)
61         win  = QMainWindow()
62         btn  = QLabel("Hello, World");
63
64         win.setCentralWidget(btn)
65         win.resize(800,600)
66         win.show()
67
68         app.exec_()
69
70 # Test PlantGL
71 class TestPlantGL:
72     def __init__(self, args):
73         self.args = args;
74
75     def run(self):
76         app  = QApplication(self.args)
77         win  = QMainWindow()
78         view = Viewer("plant.lpy");
79
80         win.setCentralWidget(view)
81         win.resize(800,600)
82         win.show()
83
84         app.exec_()
85
86 # Main
87 for test in sys.argv[1:]:
88
89     if test == 'qt':
90         qt = TestQt(sys.argv)
91         qt.run()
92
93     if test == 'plantgl' or test == 'pgl':
94         pgl = TestPlantGL(sys.argv)
95         pgl.run()