summaryrefslogtreecommitdiff
path: root/cad/src/graphics/drawing/CS_workers.py
blob: b879a51ca90d320e839fb72864e9bdee0b94f895 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# Copyright 2004-2009 Nanorex, Inc.  See LICENSE file for details.
"""
CS_workers.py - Drawing functions for primitives drawn by the ColorSorter.

@version: $Id$
@copyright: 2004-2009 Nanorex, Inc.  See LICENSE file for details.

History:

Originated by Josh in drawer.py .

Various developers extended it since then.

Brad G. added ColorSorter features.

080311 piotr Added a "drawpolycone_multicolor" function for drawing polycone
tubes with per-vertex colors (necessary for DNA display style)

080420 piotr Solved highlighting and selection problems for multi-colored
objects (e.g. rainbow colored DNA structures).

080519 russ pulled the globals into a drawing_globals module and broke drawer.py
into 10 smaller chunks: glprefs.py setup_draw.py shape_vertices.py
ColorSorter.py CS_workers.py c_renderer.py CS_draw_primitives.py drawers.py
gl_lighting.py gl_buffers.py
"""

_DRAW_BONDS = True # Debug/test switch. Similar constant in chunk.py.

# the imports from math vs. Numeric are as discovered in existing code
# as of 2007/06/25.  It's not clear why acos is coming from math...
from math import acos
import Numeric
from Numeric import pi

# russ 080519 No doubt many of the following imports are unused.
# When the dust settles, the unnecessary ones will be removed.
from OpenGL.GL import glBegin
from OpenGL.GL import glCallList
from OpenGL.GL import glColor3fv
from OpenGL.GL import GL_COLOR_MATERIAL
from OpenGL.GL import GL_CULL_FACE
from OpenGL.GL import GL_CURRENT_BIT
from OpenGL.GL import glDisable
from OpenGL.GL import glDisableClientState
from OpenGL.GL import glDrawArrays
from OpenGL.GL import glDrawElements
from OpenGL.GL import glEnable
from OpenGL.GL import glEnableClientState
from OpenGL.GL import glEnd
from OpenGL.GL import GL_FALSE
from OpenGL.GL import GL_FILL
from OpenGL.GL import GL_FLOAT
from OpenGL.GL import GL_FRONT
from OpenGL.GL import GL_LIGHTING
from OpenGL.GL import GL_LIGHT_MODEL_TWO_SIDE
from OpenGL.GL import glLightModelfv
from OpenGL.GL import GL_LINE
from OpenGL.GL import GL_LINES
from OpenGL.GL import GL_LINE_SMOOTH
from OpenGL.GL import glLineStipple
from OpenGL.GL import GL_LINE_STIPPLE
from OpenGL.GL import glLineWidth
from OpenGL.GL import glNormal3fv
from OpenGL.GL import glNormalPointer
from OpenGL.GL import GL_NORMAL_ARRAY
from OpenGL.GL import glPolygonMode
from OpenGL.GL import glPopAttrib
from OpenGL.GL import glPopMatrix
from OpenGL.GL import glPushAttrib
from OpenGL.GL import glPushMatrix
from OpenGL.GL import glRotate
from OpenGL.GL import glTranslatef
from OpenGL.GL import GL_TRIANGLE_STRIP
from OpenGL.GL import glVertex
from OpenGL.GL import glVertex3fv
from OpenGL.GL import GL_VERTEX_ARRAY
from OpenGL.GL import glVertexPointer
from OpenGL.GL import GL_TRUE

from geometry.VQT import norm, V

import graphics.drawing.drawing_globals as drawing_globals
    # used only for GL resources related to geometric primitives, as of 090304

from graphics.drawing.drawers import renderSurface
from graphics.drawing.gl_GLE import glePolyCone
from graphics.drawing.gl_Scale import glScale

### Substitute this for drawsphere_worker to test drawing a lot of spheres.
def drawsphere_worker_loop(params):
    (pos, radius, detailLevel, n) = params
    pos += V(-n/2, -n/2, 0)             # Centered on the origin.
    for x in range(n):
        for y in range(n):
            newpos = pos + (x+x/10+x/100) * V(1, 0, 0) + \
                   (y+y/10+y/100) * V(0, 1, 0)
            drawsphere_worker((newpos, radius, detailLevel, 1))
            continue
        continue
    return

def drawsphere_worker(params):
    """
    Draw a sphere.  Receive parameters through a sequence so that this function
    and its parameters can be passed to another function for deferment.  Right
    now this is only ColorSorter.schedule (see below)
    """

    (pos, radius, detailLevel, n) = params
    del n

    # KLUGE: the detailLevel can be a tuple, (vboLevel, detailLevel).
    # The only for reason for this is that drawsphere_worker is used
    # in ProteinChunks (I added a comment there saying why that's bad)
    # and I don't have time to clean that up now. Ideally, vboLevel
    # would just be another parameter, or we'd combine it with detailLevel
    # in some other way not constrained by backward compatibility of
    # this internal worker function's API. [bruce 090304]
    if type(detailLevel) == type(()):
        (vboLevel, detailLevel) = detailLevel

    ## vboLevel = drawing_globals.use_drawing_variant

    glPushMatrix()
    glTranslatef(pos[0], pos[1], pos[2])
    glScale(radius,radius,radius)

    if vboLevel == 0:  # OpenGL 1.0 - glBegin/glEnd tri-strips vertex-by-vertex.
        glCallList(drawing_globals.sphereList[detailLevel])

    else:                             # OpenGL 1.1/1.5 - Array/VBO/IBO variants.
        glEnableClientState(GL_VERTEX_ARRAY)
        glEnableClientState(GL_NORMAL_ARRAY)

        size = len(drawing_globals.sphereArrays[detailLevel])
        GLIndexType = drawing_globals.sphereGLIndexTypes[detailLevel]

        if vboLevel == 1:               # DrawArrays from CPU RAM.
            verts = drawing_globals.sphereCArrays[detailLevel]
            glVertexPointer(3, GL_FLOAT, 0, verts)
            glNormalPointer(GL_FLOAT, 0, verts)
            glDrawArrays(GL_TRIANGLE_STRIP, 0, size)

        elif vboLevel == 2:             # DrawElements from CPU RAM.
            verts = drawing_globals.sphereCElements[detailLevel][1]
            glVertexPointer(3, GL_FLOAT, 0, verts)
            glNormalPointer(GL_FLOAT, 0, verts)
            # Can't use the C index in sphereCElements yet, fatal PyOpenGL bug.
            index = drawing_globals.sphereElements[detailLevel][0]
            glDrawElements(GL_TRIANGLE_STRIP, size, GLIndexType, index)

        elif vboLevel == 3:             # DrawArrays from graphics RAM VBO.
            vbo = drawing_globals.sphereArrayVBOs[detailLevel]
            vbo.bind()
            glVertexPointer(3, GL_FLOAT, 0, None)
            glNormalPointer(GL_FLOAT, 0, None)
            glDrawArrays(GL_TRIANGLE_STRIP, 0, vbo.size)
            vbo.unbind()

        elif vboLevel == 4: # DrawElements from index in CPU RAM, verts in VBO.
            vbo = drawing_globals.sphereElementVBOs[detailLevel][1]
            vbo.bind()              # Vertex and normal data comes from the vbo.
            glVertexPointer(3, GL_FLOAT, 0, None)
            glNormalPointer(GL_FLOAT, 0, None)
            # Can't use the C index in sphereCElements yet, fatal PyOpenGL bug.
            index = drawing_globals.sphereElements[detailLevel][0]
            glDrawElements(GL_TRIANGLE_STRIP, size, GLIndexType, index)
            vbo.unbind()

        elif vboLevel == 5: # VBO/IBO buffered DrawElements from graphics RAM.
            (ibo, vbo) = drawing_globals.sphereElementVBOs[detailLevel]
            vbo.bind()              # Vertex and normal data comes from the vbo.
            glVertexPointer(3, GL_FLOAT, 0, None)
            glNormalPointer(GL_FLOAT, 0, None)
            ibo.bind()              # Index data comes from the ibo.
            glDrawElements(GL_TRIANGLE_STRIP, size, GLIndexType, None)
            vbo.unbind()
            ibo.unbind()
            pass

        glDisableClientState(GL_VERTEX_ARRAY)
        glDisableClientState(GL_NORMAL_ARRAY)
        pass

    glPopMatrix()
    return

def drawwiresphere_worker(params):
    """
    Draw a wireframe sphere.  Receive parameters through a sequence so that this
    function and its parameters can be passed to another function for
    deferment.  Right now this is only ColorSorter.schedule (see below)
    """

    (color, pos, radius, detailLevel) = params
    ## assert detailLevel == 1 # true, but leave out for speed
    from utilities.debug_prefs import debug_pref, Choice_boolean_True
    #bruce 060415 experiment, re iMac G4 wiresphere bug; fixes it!
    newway = debug_pref("new wirespheres?", Choice_boolean_True)
    oldway = not newway
    # These objects want a constant line color even if they are selected or
    # highlighted.
    glColor3fv(color)
    glDisable(GL_LIGHTING)
    if oldway:
        glPolygonMode(GL_FRONT, GL_LINE)
    glPushMatrix()
    glTranslatef(pos[0], pos[1], pos[2])
    glScale(radius,radius,radius)
    if oldway:
        glCallList(drawing_globals.sphereList[detailLevel])
    else:
        glCallList(drawing_globals.wiresphere1list)
    glEnable(GL_LIGHTING)
    glPopMatrix()
    if oldway:
        glPolygonMode(GL_FRONT, GL_FILL)
    return

def drawcylinder_worker(params):
    """
    Draw a cylinder.  Receive parameters through a sequence so that this
    function and its parameters can be passed to another function for
    deferment.  Right now this is only ColorSorter.schedule (see below)

    @warning: our circular cross-section is approximated by a 13-gon
    whose alignment around the axis is chosen arbitrary, in a way
    which depends on the direction of the axis; negating the axis usually
    causes a different alignment of that 13-gon. This effect can cause
    visual bugs when drawing one cylinder over an identical or slightly
    smaller one (e.g. when highlighting a bond), unless the axes are kept
    parallel as opposed to antiparallel.
    """
    if not _DRAW_BONDS:
        return

    (pos1, pos2, radius, capped) = params

    glPushMatrix()
    vec = pos2-pos1
    axis = norm(vec)
    glTranslatef(pos1[0], pos1[1], pos1[2])

    ##Huaicai 1/17/05: To avoid rotate around (0, 0, 0), which causes
    ## display problem on some platforms
    angle = -acos(axis[2])*180.0/pi
    if (axis[2]*axis[2] >= 1.0):
        glRotate(angle, 0.0, 1.0, 0.0)
    else:
        glRotate(angle, axis[1], -axis[0], 0.0)

    glScale(radius,radius,Numeric.dot(vec,vec)**.5)
    glCallList(drawing_globals.CylList)
    if capped:
        glCallList(drawing_globals.CapList)

    glPopMatrix()

    return

def drawpolycone_worker(params):
    """
    Draw a polycone.  Receive parameters through a sequence so that this
    function and its parameters can be passed to another function for
    deferment.  Right now this is only ColorSorter.schedule (see below)
    """
    if not _DRAW_BONDS:
        return
    (pos_array, rad_array) = params
    glePolyCone(pos_array, None, rad_array)
    return

def drawpolycone_multicolor_worker(params):
    """
    Draw a polycone.  Receive parameters through a sequence so that this
    function and its parameters can be passed to another function for
    deferment.  Right now this is only ColorSorter.schedule (see below)
    piotr 080311: this variant accepts a color array as an additional parameter
    """
    # Note: See the code in class ColorSorter for GL_COLOR_MATERIAL objects.

    (pos_array, color_array, rad_array) = params
    glEnable(GL_COLOR_MATERIAL) # have to enable GL_COLOR_MATERIAL for
                                # the GLE function
    glPushAttrib(GL_CURRENT_BIT) # store current attributes in case glePolyCone
                                    # modifies the (e.g. current color)
                                    # piotr 080411
    glePolyCone(pos_array, color_array, rad_array)
    glPopAttrib() # This fixes the 'disappearing cylinder' bug
                    # glPopAttrib doesn't take any arguments
                    # piotr 080415
    glDisable(GL_COLOR_MATERIAL)
    return

def drawsurface_worker(params):
    """Draw a surface.  Receive parameters through a sequence so that this
    function and its parameters can be passed to another function for
    deferment.  Right now this is only ColorSorter.schedule (see below)"""

    (pos, radius, tm, nm) = params
    glPushMatrix()
    glTranslatef(pos[0], pos[1], pos[2])
    glScale(radius,radius,radius)
    renderSurface(tm, nm)
    glPopMatrix()
    return

def drawline_worker(params):
    """
    Draw a line.  Receive parameters through a sequence so that this
    function and its parameters can be passed to another function for
    deferment.  Right now this is only ColorSorter.schedule (see below)
    """
    (endpt1, endpt2, dashEnabled, stipleFactor, width, isSmooth) = params

    ###glDisable(GL_LIGHTING)
    ###glColor3fv(color)
    if dashEnabled:
        glLineStipple(stipleFactor, 0xAAAA)
        glEnable(GL_LINE_STIPPLE)
    if width != 1:
        glLineWidth(width)
    if isSmooth:
        glEnable(GL_LINE_SMOOTH)
    glBegin(GL_LINES)
    glVertex(endpt1[0], endpt1[1], endpt1[2])
    glVertex(endpt2[0], endpt2[1], endpt2[2])
    glEnd()
    if isSmooth:
        glDisable(GL_LINE_SMOOTH)
    if width != 1:
        glLineWidth(1.0) # restore default state
    if dashEnabled:
        glDisable(GL_LINE_STIPPLE)
    ###glEnable(GL_LIGHTING)
    return

def drawtriangle_strip_worker(params):
    """
    Draw a triangle strip using a list of triangle vertices
    and (optional) normals.
    """
    # Note: See the code in class ColorSorter for GL_COLOR_MATERIAL objects.

    # piotr 080904 - This method could be optimized by using vertex
    # arrays or VBOs.

    (triangles, normals, colors) = params

    # It needs to support two-sided triangles, therefore we disable
    # culling and enable two-sided lighting. These settings have to be
    # turned back to default setting.

    glDisable(GL_CULL_FACE)
    glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE)

    # Use color material mode if colors are present.

    if colors:
        glEnable(GL_COLOR_MATERIAL)

    glBegin(GL_TRIANGLE_STRIP)
    if normals:
        if colors:
            for p in range(len(triangles)):
                n = normals[p]
                v = triangles[p]
                c = colors[p]
                glNormal3fv(n)
                glColor3fv(c[:3])
                glVertex3fv(v)
        else:
            for p in range(len(triangles)):
                n = normals[p]
                v = triangles[p]
                glNormal3fv(n)
                glVertex3fv(v)
    else:
        for v in triangles:
            glVertex3fv(v)
    glEnd()

    if colors:
        glDisable(GL_COLOR_MATERIAL)

    # piotr 080904 - are these settings really default?

    glEnable(GL_CULL_FACE)
    glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE)

    return