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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
########################################################################
#
# Date: 2000 Authors: Michel Sanner, Kevin Chan
#
# vareille@scripps.edu
# sanner@scripps.edu
#
# The Scripps Research Institute (TSRI)
# Molecular Graphics Lab
# La Jolla, CA 92037, USA
#
# Copyright: Michel Sanner and TSRI 2000
#
# revision: Guillaume Vareille
#
#########################################################################
#
# $Header: /opt/cvs/python/packages/share1.5/DejaVu/quad_strip.py,v 1.29.8.1 2016/02/11 01:25:21 annao Exp $
#
# $Id: quad_strip.py,v 1.29.8.1 2016/02/11 01:25:21 annao Exp $
#
from opengltk.OpenGL import GL
from DejaVu.IndexedPolygons import IndexedPolygons
from DejaVu.viewerFns import checkKeywords
import DejaVu.datamodel, DejaVu.viewerConst
from DejaVu.colorTool import glMaterialWithCheck, resetMaterialMemory
class Quad_strip(IndexedPolygons):
""" Class to draw a quad strip or multiple quad strip geometries. """
keywords = IndexedPolygons.keywords + [
'stripBegin',
'fnormals',
]
def __init__(self, name=None, check=1, **kw):
""" Constructor:
Takes an array of vertices and splits it into separate quad strips
at specific vertices specified by stripBegin, which is and array of
indices. Generates faceSet. In this class, a face corresponds to a
quad strip. Calls IndexedPolygons constructor. Calls
MatBindingMode() to set the correct mode specific to a quad strip,
overwriting the binding mode set by MaterialBindingMode in the Geom
class. Initializes frontPolyMode to FILL. Initializes currentCol and
currentMat."""
apply( IndexedPolygons.__init__, (self, name, check), kw)
for face in self.materials.keys():
for j in range(5):
self.MatBindingMode(j, face)
self.frontPolyMode = GL.GL_FILL
#self.inheritFrontPolyMode = 0
self.currentCol = []
for i in range(4):
self.currentCol.append(-1.0)
self.currentMat = []
for i in range(2):
mat = []
for j in range(5):
rgba = []
for k in range(4):
rgba.append(-1.0)
mat.append(rgba)
self.currentMat.append(mat)
def Set(self, check=1, redo=1, updateOwnGui=True, **kw):
"""set data for this object: Set polygon's vertices, faces, normals or materials
check=1 : verify that all the keywords present can be handle by this func
redo=1 : append self to viewer.objectsNeedingRedo
updateOwnGui=True : allow to update owngui at the end this func
"""
v = kw['vertices']
self.stripBegin = kw.get( 'stripBegin')
if self.stripBegin is None:
self.stripBegin = [0, len(v)]
for j in self.stripBegin:
assert j%2==0
faces = []
for i in range(0, len(self.stripBegin)-1):
strip_ind = ()
for j in range(self.stripBegin[i], self.stripBegin[i+1]):
strip_ind = strip_ind + (j,)
faces.extend([strip_ind])
kw['faces'] = faces
self.fnormals = kw.get( 'fnormals')
redoFlags = apply( IndexedPolygons.Set, (self, check, 0), kw )
return self.redoNow(redo, updateOwnGui, redoFlags)
def MatBindingMode(self, propNum, face=GL.GL_FRONT):
""" For the given face and material property corresponding to propNum,
the method sets the binding mode. If there is one value, the mode is
OVERALL; if the number of values equals the number of vertices, strips,
or faces(quads), the mode is PER_VERTEX, PER_PART, or PER_FACE,
respectively. Else, mode is none. """
OVERALL, PER_VERTEX, PER_PART, PER_FACE = -1, 10, 11, 12, 13
#NONE, OVERALL, PER_VERTEX, PER_PART, PER_FACE = -1, 10, 11, 12, 13
num = propNum
f = face
nn = self.materials[f].prop[num].shape[0]
self.inheritMaterial = 0
if nn == 1:
self.materials[f].binding[num] = OVERALL
elif nn == len(self.vertexSet.vertices):
self.materials[f].binding[num] = PER_VERTEX
elif hasattr(self, 'faceSet') and nn == len(self.faceSet):
self.materials[f].binding[num] = PER_PART
elif hasattr(self, 'IndexedFaceSet') and nn==len(self.IndexedFaceSet):
self.materials[f].binding[num] = PER_FACE
self.shading = GL.GL_FLAT
self.GetNormals()
else:
self.materials[f].binding[num] = -1
self.inheritMaterial = 1
def buildIndexedFaceSet(self):
"""Build the set of indices describing the strips"""
f = []
for stripNum in range(1,len(self.stripBegin)):
for i in range(self.stripBegin[stripNum-1],
self.stripBegin[stripNum]-3, 2):
f.extend([(i, i+1, i+3, i+2)])
self.IndexedFaceSet = DejaVu.datamodel.FaceSet( f, (0,0) )
def GetNormals(self):
""" Gets the proper normals for smooth or flat shading. Calls
buildIndexedFaceSet(). Sets face normals or computes them if not given.
Sets object normals to the vertex normals for smooth shading or to the
face normals for flat shading. If shading none, normals are none."""
if self.shading==GL.GL_NONE:
self.normals = None
else:
if hasattr(self, 'faceSet'):
self.StripFaceSet = self.faceSet
self.buildIndexedFaceSet()
self.faceSet = self.IndexedFaceSet
if self.fnormals:
self.faceSet.normals.SetValues(self.fnormals)
else:
self.FaceNormalFunction( self.ComputeFaceNormals )
self.faceSet.normals.ComputeMode( DejaVu.viewerConst.AUTO )
if self.shading==GL.GL_FLAT:
if hasattr(self, 'faceSet'):
self.normals = self.faceSet.normals.GetProperty()
else: self.normals = None
elif self.shading==GL.GL_SMOOTH:
self.normals = self.vertexSet.normals.GetProperty()
self.faceSet = self.StripFaceSet
else: self.normals = None
def isNewColor(self, c=None):
""" Compares new color c to the current color. If the same, method
returns 0. If the new color is different, the current color gets the
values of the new color, and the method returns 1. """
if c is None:
for i in self.currentCol:
i = -1.0 # set an impossible color
return 0
elif abs(c[0]-self.currentCol[0]) < 0.0001 and \
abs(c[1]-self.currentCol[1]) < 0.0001 and \
abs(c[2]-self.currentCol[2]) < 0.0001 and \
abs(c[3]-self.currentCol[3]) < 0.0001:
return 0
else:
self.currentCol[0] = c[0]
self.currentCol[1] = c[1]
self.currentCol[2] = c[2]
self.currentCol[3] = c[3]
return 1
## def isNewMaterial(self, face, prop, c):
## """ For the given face (face) and property number (prop), the method
## compares the new material value c to the current material. If
## the same, method returns 0. If different, the current material gets
## the new material value, and the method returns 1. """
## f = not(face==GL.GL_FRONT)
## if not c:
## for i in range(2):
## for j in range(5):
## for k in self.currentMat[i][j]:
## k = -1.0
## return 0
## elif abs(c[0]-self.currentMat[f][prop][0]) < 0.0001 and \
## abs(c[1]-self.currentMat[f][prop][1]) < 0.0001 and \
## abs(c[2]-self.currentMat[f][prop][2]) < 0.0001 and \
## abs(c[3]-self.currentMat[f][prop][3]) < 0.0001:
## return 0
## else:
## self.currentMat[f][prop][0] = c[0]
## self.currentMat[f][prop][1] = c[1]
## self.currentMat[f][prop][2] = c[2]
## self.currentMat[f][prop][3] = c[3]
## return 1
def DisplayFunction(self):
""" Either executes the present display list or creates a display
list to display the quad strip. """
OVERALL, PER_VERTEX, PER_PART, PER_FACE = -1, 10, 11, 12, 13
#NONE, OVERALL, PER_VERTEX, PER_PART, PER_FACE = -1, 10, 11, 12, 13
propConst = DejaVu.viewerConst.propConst
noCol = 0
if self.viewer.currentCamera.renderMode == GL.GL_SELECT:
save = self.dpyList
temp = self.primitiveType
if self.frontPolyMode == GL.GL_FILL:
self.primitiveType = GL.GL_POLYGON
elif self.frontPolyMode == GL.GL_LINE:
self.primitiveType = GL.GL_LINE_LOOP
elif self.frontPolyMode == GL.GL_POINT:
self.primitiveType = GL.GL_POINTS
self.faceSet = self.IndexedFaceSet
IndexedPolygons.DisplayFunction(self)
self.faceSet = self.StripFaceSet
self.primitiveType = temp
self.dpyList = save
return
if self.dpyList:
IndexedPolygons.DisplayFunction(self)
def Draw(self):
OVERALL, PER_VERTEX, PER_PART, PER_FACE = -1, 10, 11, 12, 13
#NONE, OVERALL, PER_VERTEX, PER_PART, PER_FACE = -1, 10, 11, 12, 13
propConst = DejaVu.viewerConst.propConst
noCol = 1
vert = self.vertexSet.vertices.array
if len(vert)==0: return
if self.materials[GL.GL_FRONT] and not self.inheritMaterial:
mat = self.materials[GL.GL_FRONT]
frontMat = fpProp = []
frontMatBind = fpBind = []
for propInd in range(4):
b, p = mat.GetProperty(propInd)
fpProp.append(p)
fpBind.append(b)
fpProp.append(mat.prop[4])
fpBind.append(mat.binding[4])
# frontMat = self.materials[GL.GL_FRONT].prop
# frontMatBind = self.materials[GL.GL_FRONT].binding
else:
frontMat = None
frontMatBind = None
if self.materials[GL.GL_BACK] and not self.inheritMaterial:
mat = self.materials[GL.GL_BACK]
backMat = bpProp = []
backMatBind = bpBind = []
for propInd in range(4):
b, p = mat.GetProperty(propInd)
bpProp.append(p)
bpBind.append(b)
bpProp.append(mat.prop[4])
bpBind.append(mat.binding[4])
# backMat = self.materials[GL.GL_BACK].prop
# backMatBind = self.materials[GL.GL_BACK].binding
else:
backMat = None
backMatBind = None
## texCoords = None
## if hasattr(self.vertexSet, "texCoords"):
## if self.vertexSet.texCoords.status >= viewerConst.COMPUTED:
## texCoords = self.vertexSet.texCoords.array
if not self.frontAndBack is None:
face = GL.GL_FRONT
else:
face = GL.GL_FRONT_AND_BACK
if not self.normals: # overall color for no normals or lighting
if frontMat:
if frontMatBind[noCol] == OVERALL:
GL.glColor4fv( frontMat[noCol][0] )
else:
if len(self.normals)==1: # overall normal
n = self.normals
GL.glNormal3dv(n[0])
if frontMat:
for j in range(5): # overall materials
if frontMatBind[j] == OVERALL:
glMaterialWithCheck( face, propConst[j],
frontMat[j][0] )
if backMat and not self.frontAndBack:
for j in range(5):
if backMatBind[j] == OVERALL:
glMaterialWithCheck( GL.GL_BACK, propConst[j],
backMat[j][0] )
self.isNewColor()
#self.isNewMaterial(0,0,0)
n = self.normals
# loop over each strip
for stripNum in range(1,len(self.stripBegin)):
c = vert[self.stripBegin[stripNum-1]:self.stripBegin[stripNum]]
GL.glPushName(stripNum)
GL.glBegin(GL.GL_QUAD_STRIP)
# per part material properties
if frontMat:
if frontMatBind[noCol] == PER_PART:
if self.isNewColor(c=frontMat[noCol][stripNum-1]):
GL.glColor4fv(frontMat[noCol][stripNum-1])
if n:
if frontMat:
for j in range(5):
if frontMatBind[j]==PER_PART:
glMaterialWithCheck( face,
propConst[j],
frontMat[j][stripNum-1] )
if backMat and not self.frontAndBack:
for j in range(5):
if backMatBind[j] == PER_PART:
glMaterialWithCheck( GL.GL_BACK,
propConst[j],
backMat[j][stripNum-1] )
# loop over each vertex in a strip
i = 0
for v in c:
if n:
if self.shading==GL.GL_FLAT:
if i > 1 and i%2==0:
GL.glNormal3dv(n[(self.stripBegin[stripNum-1]+i-(2*stripNum))/2])
elif self.shading==GL.GL_SMOOTH:
GL.glNormal3dv(n[self.stripBegin[stripNum-1]+i])
else:
pass
# per face (per triangle) material properties
if not n:
if frontMat:
if frontMatBind[noCol] == PER_FACE:
if i > 1 and i%2==0:
if self.isNewColor(c=frontMat[noCol][(self.stripBegin[stripNum-1]+i-(2*stripNum))/2]):
GL.glColor4fv(frontMat[noCol][(self.stripBegin[stripNum-1]+i-(2*stripNum))/2])
else:
if frontMat:
for k in range(5):
if frontMatBind[k] == PER_FACE:
if i > 1 and i%2==0:
glMaterialWithCheck( face,
propConst[k],
frontMat[k][(self.stripBegin[stripNum-1]+i-(2*stripNum))/2] )
if backMat and not self.frontAndBack:
for k in range(5):
if backMatBind[k] == PER_FACE:
if i > 1 and i%2==0:
glMaterialWithCheck( GL.GL_BACK,
propConst[k],
backMat[k][(self.stripBegin[stripNum-1]+i-(2*stripNum))/2] )
# per vertex material properties
if not n:
if frontMat:
if frontMatBind[noCol] == PER_VERTEX:
if self.isNewColor(c=frontMat[noCol][self.stripBegin[stripNum-1]+i]):
GL.glColor4fv(frontMat[noCol][self.stripBegin[stripNum-1]+i])
else:
if frontMat:
for k in range(5):
if frontMatBind[k] == PER_VERTEX:
glMaterialWithCheck( face, propConst[k],
frontMat[k][self.stripBegin[stripNum-1]+i] )
if backMat and not self.frontAndBack:
for k in range(5):
if backMatBind[k] == PER_VERTEX:
glMaterialWithCheck( GL.GL_BACK,
propConst[k],
backMat[k][self.stripBegin[stripNum-1]+i] )
# draw vertex
GL.glVertex3dv(v)
i = i + 1
GL.glEnd()
GL.glPopName()
return 1
if __name__=='__main__':
import pdb, numpy
from DejaVu import Viewer
vi = Viewer()
vert = [(0, 1, 0), (1, 0, 1), (1, 1, 0), (2, 0, -1), (2, 1, 0), (3, 0, 1),
(3, 1, 0), (4, 0, -1), (4, 1, 0), (5, 0, 1), (5, 3, 0), (6, -2, 1)]
v1 = numpy.array(vert)
v2 = v1 + 3.0
v3 = numpy.concatenate( (v1,v2) )
colors = [(0, 0, 1), (1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 0, 0), (0, 1, 0),
(0, 0, 1), (1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 0, 0), (0, 1, 0)]
v4 = v2 + 3.0
v5 = numpy.concatenate( (v3,v4) )
strip3 = Quad_strip(vertices = v5, stripBegin=[0,12,24,36], materials = colors+colors[0:3])
#strip.Set(materials = [(1, 0, 0)])
vi.AddObject(strip3)
strip2 = Quad_strip(vertices = v3, stripBegin=[0,12,24], materials = colors[0:10])
# strip2.Set(materials = [(0, 1, 0), (0, 0, 1)])
vi.AddObject(strip2)
strip = Quad_strip(vertices = vert, materials = colors[0:5])
# strip.Set(materials = [(1, 0, 0), (0, 0, 1), (1, 0, 0), (0, 1, 0), (0, 0, 1) ])
vi.AddObject(strip)
|