summaryrefslogtreecommitdiff
path: root/cad/src/experimental/LearningOpenGL/Cruft.py
blob: 922d698c527b7b84cbdecebc5a28bdf8ae9c46e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python

# Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
"""
>... anything I'm doing wrong?

Not overriding QGLWidget's necessary methods that draw things,
paintGL, resizeGL, maybe an init method. GLPane shows the way,
so does the smaller ThumbView.

But it's easier and more relevant to NE1 to forget about learning (for now)
to make your own GL context aka QGLWidget, and just use NE1's,
like we did to demo brad's code in selectMode.py,
where if some debug flag is set,
we replace mode.Draw with his own drawing code.

I doubt you need to override anything higher up than mode.Draw,
but if you do, you can add debug flags to GLPane to call your own code
instead of what it now does inside paintGL. Or patch into it at runtime,
like this:

# in your test code
glpane = assy.w.glpane

def mypaintGL(): bla

glpane.paintGL = mypaintGL # this does *not* get the GLPane (self) as arg 1
# but it gets whatever other args paintGL usually gets
"""


import CruftDialog
from qt import *
from qtcanvas import *
from qtgl import *
from OpenGL.GL import *
import Numeric
import sys
import random
import time
import foo

class MyGLWidget(QGLWidget):
    def __init__(self, parent, name, shareWidget=None):
        """  """
        if shareWidget:
            self.shareWidget = shareWidget #bruce 051212
            format = shareWidget.format()
            QGLWidget.__init__(self, format, parent, name, shareWidget)
            if not self.isSharing():
                print "Request of display list sharing is failed."
                return
        else:  
            QGLWidget.__init__(self, parent, name)  
        
        # point of view, and half-height of window in Angstroms
        self.pov = Numeric.array((0.0, 0.0, 0.0))
        self.scale = 10.0
        #self.quat = Q(1, 0, 0, 0)

        self.selectedObj = None
        
        # clipping planes, as percentage of distance from the eye
        self.near = 0.66
        self.far = 2.0  
        # start in perspective mode
        self.ortho = False #True
        self.initialised = False
        self.backgroundColor = (0.5, 0.5, 0.5)  # gray
    
    def initializeGL(self):
        glShadeModel(GL_SMOOTH)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        return
    
    def resetView(self):
        '''Subclass can override this method with different <scale>, so call this version in the overridden
           version. '''
        self.pov = Numeric.array((0.0, 0.0, 0.0))
        #self.quat = Q(1, 0, 0, 0)
        
    def resizeGL(self, width, height):
        """Called by QtGL when the drawing window is resized.
        """
        self.width = width
        self.height = height
           
        glViewport(0, 0, self.width, self.height)
        
        if not self.initialised:
            self.initialised = True


    def _setup_projection(self, glselect = False): #bruce 050608 split this out; 050615 revised docstring
        """Set up standard projection matrix contents using aspect, vdist, and some attributes of self.
        (Warning: leaves matrixmode as GL_PROJECTION.)
        Optional arg glselect should be False (default) or a 4-tuple (to prepare for GL_SELECT picking).
        """
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        scale = self.scale #bruce 050608 used this to clarify following code
        near, far = self.near, self.far

        if glselect:
            x,y,w,h = glselect
            gluPickMatrix(
                    x,y,
                    w,h,
                    glGetIntegerv( GL_VIEWPORT ) #k is this arg needed? it might be the default...
            )
         
        if self.ortho:
            glOrtho( - scale * self.aspect, scale * self.aspect,
                     - scale,          scale,
                       self.vdist * near, self.vdist * far )
        else:
            glFrustum( - scale * near * self.aspect, scale * near * self.aspect,
                       - scale * near,          scale * near,
                         self.vdist * near, self.vdist * far)
        return
    
    
    def paintGL(self):        
        """Called by QtGL when redrawing is needed.
            For every redraw, color & depth butter are cleared, view projection are reset, view location & orientation are also reset. 
        """
        if not self.initialised: return

        c = self.backgroundColor
        glClearColor(c[0], c[1], c[2], 0.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        
        self.aspect = (self.width + 0.0)/(self.height + 0.0)
        self.vdist = 6.0 * self.scale
        self._setup_projection()
        
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()    
        glTranslatef(0.0, 0.0, -self.vdist)
       
        #q = self.quat
        
        #glRotatef(q.angle*180.0/pi, q.x, q.y, q.z)
        glTranslatef(self.pov[0], self.pov[1], self.pov[2])
        
        #self.drawModel()
        foo.foo()




class Cruft(CruftDialog.CruftDialog):

    ANIMATION_DELAY = 50   # milliseconds
    COLOR_CHOICES = (
        # Wonder Bread builds strong bodies twelve ways.
        # Oh wait, now these are the eBay colors.
        QColor(Qt.red), QColor(Qt.yellow),
        QColor(Qt.green), QColor(Qt.blue)
        )

    def __init__(self, parent=None, name=None, modal=0, fl=0):
        CruftDialog.CruftDialog.__init__(self,parent,name,modal,fl)
        foo.init()
        glformat = QGLFormat()
        glformat.setStencil(True)
        # self.qglwidget = QGLWidget(glformat, self.frame1, "glpane")
        self.qglwidget = MyGLWidget(glformat, self.frame1)
        self.qglwidget.resizeGL(400, 400)

    def pushButton1_clicked(self):
        self.app.quit()

    def __paintEvent(self, e):
        """Draw a colorful collection of lines and circles.
        """
        foo.foo()
        if False:
            print 'paintEvent',
            sys.stdout.flush()
        if False:
            # Here is how to draw stuff using PyQt
            p = QPainter()
            size = self.frame1.size()
            w, h = size.width(), size.height()
            p.begin(self.frame1)
            p.eraseRect(0, 0, w, h)
            for i in range(100):
                color = random.choice(self.COLOR_CHOICES)
                p.setPen(QPen(color))
                p.setBrush(QBrush(color))
                x1 = w * random.random()
                y1 = h * random.random()
                if random.random() < 0.5:
                    x2 = w * random.random()
                    y2 = h * random.random()
                    p.drawLine(x1, y1, x2, y2)
                else:
                    x2 = 0.05 * w * random.random()
                    y2 = x2
                    p.drawEllipse(x1, y1, x2, y2)
            p.flush()
            p.end()

def main():
    app = QApplication(sys.argv)
    cr = Cruft()
    cr.app = app
    app.setMainWidget(cr)
    cr.show()
    cr.update()
    app.exec_loop()

if __name__ == "__main__":
    main()