| 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 |
from OpenGL import GL |
|---|
| 27 |
import Numeric |
|---|
| 28 |
import NumericVertexArray |
|---|
| 29 |
from RBFoundation.Aspects import Aspect |
|---|
| 30 |
|
|---|
| 31 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 32 |
#~ Constants / Variables / Etc. |
|---|
| 33 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 34 |
|
|---|
| 35 |
_PrimitveMap = { |
|---|
| 36 |
'pointlist': GL.GL_POINTS, |
|---|
| 37 |
|
|---|
| 38 |
'linelist': GL.GL_LINES, |
|---|
| 39 |
'lineloop': GL.GL_LINE_LOOP, |
|---|
| 40 |
'linestrip': GL.GL_LINE_STRIP, |
|---|
| 41 |
|
|---|
| 42 |
'trilist': GL.GL_TRIANGLES, |
|---|
| 43 |
'trifan': GL.GL_TRIANGLE_FAN, |
|---|
| 44 |
'tristrip': GL.GL_TRIANGLE_STRIP, |
|---|
| 45 |
|
|---|
| 46 |
'quadlist': GL.GL_QUADS, |
|---|
| 47 |
'quadstrip': GL.GL_QUAD_STRIP, |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
_ReversePrimitveMap = dict([(y,x) for x,y in _PrimitveMap.iteritems()]) |
|---|
| 51 |
_PrimitveStatsMap = { |
|---|
| 52 |
GL.GL_POINTS: ('points', lambda count: count), |
|---|
| 53 |
|
|---|
| 54 |
GL.GL_LINES: ('lines', lambda count: count//2), |
|---|
| 55 |
GL.GL_LINE_STRIP: ('lines', lambda count: count-1), |
|---|
| 56 |
GL.GL_LINE_LOOP: ('lines', lambda count: count), |
|---|
| 57 |
|
|---|
| 58 |
GL.GL_TRIANGLES: ('triangles', lambda count: count//3), |
|---|
| 59 |
GL.GL_TRIANGLE_FAN: ('triangles', lambda count: count-2), |
|---|
| 60 |
GL.GL_TRIANGLE_STRIP: ('triangles', lambda count: count-2), |
|---|
| 61 |
|
|---|
| 62 |
GL.GL_QUADS: ('triangles', lambda count: count//2), |
|---|
| 63 |
GL.GL_QUAD_STRIP: ('triangles', lambda count: count-2), |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 67 |
#~ Definitions |
|---|
| 68 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 69 |
|
|---|
| 70 |
def GetPrimitiveCollection(primitive, count): |
|---|
| 71 |
if isinstance(primitive, (list, tuple)): |
|---|
| 72 |
assert len(primitive) == count |
|---|
| 73 |
return [_PrimitveMap.get(p, p) for p in primitive] |
|---|
| 74 |
else: |
|---|
| 75 |
return [_PrimitveMap.get(primitive, primitive)]*count |
|---|
| 76 |
|
|---|
| 77 |
def GenerateStatistics(self, context): |
|---|
| 78 |
if context.Statistics: |
|---|
| 79 |
for StatName, StatResults in self._StatsData.iteritems(): |
|---|
| 80 |
try: context.Statistics[StatName] += StatResults |
|---|
| 81 |
except KeyError: context.Statistics[StatName] = StatResults |
|---|
| 82 |
|
|---|
| 83 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 84 |
#~ Ranged Traversals |
|---|
| 85 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 86 |
|
|---|
| 87 |
class RangedTraversalRaw(object): |
|---|
| 88 |
"""Draws a collection of ranges using multiple primitives""" |
|---|
| 89 |
|
|---|
| 90 |
def __init__(self, primitives, starts, lengths): |
|---|
| 91 |
assert len(primitives) == len(starts) == len(lengths) |
|---|
| 92 |
self.primitives = primitives |
|---|
| 93 |
self.starts = starts |
|---|
| 94 |
self.lengths = lengths |
|---|
| 95 |
|
|---|
| 96 |
self._StatsData = {} |
|---|
| 97 |
for primitive, count in zip(primitives, lengths): |
|---|
| 98 |
StatName, StatCalc = _PrimitveStatsMap[primitive] |
|---|
| 99 |
self._StatsData[StatName] = self._StatsData.get(StatName, 0) + StatCalc(count) |
|---|
| 100 |
ReverseName = _ReversePrimitveMap[primitive] |
|---|
| 101 |
self._StatsData[ReverseName] = self._StatsData.get(ReverseName, 0) + 1 |
|---|
| 102 |
|
|---|
| 103 |
GenerateStatistics = GenerateStatistics |
|---|
| 104 |
|
|---|
| 105 |
def GLExecute(self, context): |
|---|
| 106 |
map(GL.glDrawArrays, self.primitives, self.starts, self.lengths) |
|---|
| 107 |
self.GenerateStatistics(context) |
|---|
| 108 |
|
|---|
| 109 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 110 |
|
|---|
| 111 |
class RangedTraversal(RangedTraversalRaw): |
|---|
| 112 |
"""Draws a collection of ranges using multiple primitives""" |
|---|
| 113 |
|
|---|
| 114 |
def __init__(self, primitive, rangecollection): |
|---|
| 115 |
primitives = GetPrimitiveCollection(primitive, len(rangecollection)) |
|---|
| 116 |
starts, lengths = zip(*rangecollection) |
|---|
| 117 |
RangedTraversalRaw.__init__(self, primitives, starts, lengths) |
|---|
| 118 |
|
|---|
| 119 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 120 |
#~ Indexed Collection Traversals |
|---|
| 121 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 122 |
|
|---|
| 123 |
class IndexedCollectionTraversalRaw(object): |
|---|
| 124 |
"""Draws a collection of index arrays using multiple primitives""" |
|---|
| 125 |
|
|---|
| 126 |
def __init__(self, primitives, data): |
|---|
| 127 |
assert len(primitives) == len(data) |
|---|
| 128 |
self.primitives = primitives |
|---|
| 129 |
self.data = data |
|---|
| 130 |
|
|---|
| 131 |
self._StatsData = {} |
|---|
| 132 |
for primitive, datacoll in zip(primitives, data): |
|---|
| 133 |
StatName, StatCalc = _PrimitveStatsMap[primitive] |
|---|
| 134 |
self._StatsData[StatName] = self._StatsData.get(StatName, 0) + StatCalc(len(datacoll)) |
|---|
| 135 |
ReverseName = _ReversePrimitveMap[primitive] |
|---|
| 136 |
self._StatsData[ReverseName] = self._StatsData.get(ReverseName, 0) + 1 |
|---|
| 137 |
|
|---|
| 138 |
GenerateStatistics = GenerateStatistics |
|---|
| 139 |
|
|---|
| 140 |
def GLExecute(self, context): |
|---|
| 141 |
map(NumericVertexArray.DrawElementsArray, self.primitives, self.data) |
|---|
| 142 |
self.GenerateStatistics(context) |
|---|
| 143 |
|
|---|
| 144 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 145 |
|
|---|
| 146 |
class IndexedCollectionTraversal(IndexedCollectionTraversalRaw): |
|---|
| 147 |
"""Draws a collection of index arrays using multiple primitives""" |
|---|
| 148 |
|
|---|
| 149 |
_DefaultFormat = Numeric.UInt16 |
|---|
| 150 |
|
|---|
| 151 |
def __init__(self, primitive, data, format=None): |
|---|
| 152 |
if format is None: format = self._DefaultFormat |
|---|
| 153 |
primitives = GetPrimitiveCollection(primitive, len(data)) |
|---|
| 154 |
data = [Numeric.asarray(item, format) for item in data] |
|---|
| 155 |
IndexedCollectionTraversalRaw.__init__(self, primitives, data) |
|---|
| 156 |
|
|---|
| 157 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 158 |
|
|---|
| 159 |
class ColoredIndexedCollectionTraversal(IndexedCollectionTraversal): |
|---|
| 160 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 161 |
#~ Constants / Variables / Etc. |
|---|
| 162 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 163 |
|
|---|
| 164 |
colors = [] |
|---|
| 165 |
|
|---|
| 166 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 167 |
#~ Public Methods |
|---|
| 168 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 169 |
|
|---|
| 170 |
def __init__(self, primitive, data, format=None): |
|---|
| 171 |
IndexedCollectionTraversal.__init__(self, primitive, data, format) |
|---|
| 172 |
self.GenerateColors() |
|---|
| 173 |
|
|---|
| 174 |
def GenerateColors(self): |
|---|
| 175 |
import random |
|---|
| 176 |
self.colors = [(.3 + .7*random.random(), .3 + .7*random.random(), .3 + .7*random.random()) for x in self.data] |
|---|
| 177 |
|
|---|
| 178 |
def GLExecute(self, context): |
|---|
| 179 |
_glDrawElements = NumericVertexArray.DrawElementsArray |
|---|
| 180 |
for color, primitive, indexes in zip(self.colors, self.primitives, self.data): |
|---|
| 181 |
GL.glColor3fv(color) |
|---|
| 182 |
_glDrawElements(primitive, indexes) |
|---|
| 183 |
|
|---|
| 184 |
self.GenerateStatistics(context) |
|---|
| 185 |
|
|---|
| 186 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 187 |
|
|---|
| 188 |
class ColoredIndexedCollectionAspect(Aspect, ColoredIndexedCollectionTraversal): |
|---|
| 189 |
pass |
|---|