| 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 |
import Numeric |
|---|
| 27 |
from OpenGL import GL |
|---|
| 28 |
from RBRapier.Renderer.AttributeMgr import AttributeChangeElement |
|---|
| 29 |
from RBRapier.Renderer.Geometry.VertexArrays import VertexArray |
|---|
| 30 |
from RBRapier.Renderer.Geometry.ArrayTraversal import IndexedCollectionTraversal |
|---|
| 31 |
|
|---|
| 32 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 33 |
#~ Definitions |
|---|
| 34 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 35 |
|
|---|
| 36 |
class ProjectionBox(object): |
|---|
| 37 |
""" |
|---|
| 38 |
>>> ProjectionBox()._UpdateBoxVertices() |
|---|
| 39 |
array([[-1., -1., -1.], |
|---|
| 40 |
[ 1., -1., -1.], |
|---|
| 41 |
[ 1., 1., -1.], |
|---|
| 42 |
[-1., 1., -1.], |
|---|
| 43 |
[-1., -1., 1.], |
|---|
| 44 |
[ 1., -1., 1.], |
|---|
| 45 |
[ 1., 1., 1.], |
|---|
| 46 |
[-1., 1., 1.]],'f') |
|---|
| 47 |
""" |
|---|
| 48 |
|
|---|
| 49 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 50 |
#~ Constants / Variables / Etc. |
|---|
| 51 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 52 |
|
|---|
| 53 |
Projection = None |
|---|
| 54 |
LineWidth = 4. |
|---|
| 55 |
AttributeChange = AttributeChangeElement(GL.GL_LINE_BIT) |
|---|
| 56 |
_BoxTraversal = IndexedCollectionTraversal(GL.GL_LINES, [[0,1, 1,2, 2,3, 3,0, 0,4, 1,5, 2,6, 3,7, 4,5, 5,6, 6,7, 7,4]], Numeric.UInt8) |
|---|
| 57 |
|
|---|
| 58 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 59 |
#~ Public Methods |
|---|
| 60 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 61 |
|
|---|
| 62 |
def __init__(self, projection=None): |
|---|
| 63 |
self.Projection = projection |
|---|
| 64 |
self._Vertices= VertexArray(Numeric.zeros((8,3), Numeric.Float32)) |
|---|
| 65 |
|
|---|
| 66 |
def GLExecute(self, context): |
|---|
| 67 |
GL.glLineWidth(self.LineWidth) |
|---|
| 68 |
GL.glColor4f(*self.color) |
|---|
| 69 |
self._UpdateBoxVertices() |
|---|
| 70 |
self._Vertices.GLSelect(context) |
|---|
| 71 |
self._BoxTraversal.GLExecute(context) |
|---|
| 72 |
self._Vertices.GLDeselect(context) |
|---|
| 73 |
GL.glLineWidth(1.) |
|---|
| 74 |
|
|---|
| 75 |
def _UpdateBoxVertices(self): |
|---|
| 76 |
l,r,b,t,n,f = (-1.,1.,-1.,1.,-1.,1.) |
|---|
| 77 |
self._Vertices.data[:4] = [(l,b,n), (r,b,n), (r,t,n), (l,t,n)] |
|---|
| 78 |
self._Vertices.data[4:] = [(l,b,f), (r,b,f), (r,t,f), (l,t,f)] |
|---|
| 79 |
return self._Vertices.data |
|---|
| 80 |
|
|---|
| 81 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 82 |
|
|---|
| 83 |
class OrthographicBox(ProjectionBox): |
|---|
| 84 |
""" |
|---|
| 85 |
>>> from RBRapier.Tools.Projections import Orthographic |
|---|
| 86 |
>>> OrthographicBox(Orthographic(-1, 1, -1, 1, -1, 1))._UpdateBoxVertices() |
|---|
| 87 |
array([[-1., -1., -1.], |
|---|
| 88 |
[ 1., -1., -1.], |
|---|
| 89 |
[ 1., 1., -1.], |
|---|
| 90 |
[-1., 1., -1.], |
|---|
| 91 |
[-1., -1., 1.], |
|---|
| 92 |
[ 1., -1., 1.], |
|---|
| 93 |
[ 1., 1., 1.], |
|---|
| 94 |
[-1., 1., 1.]],'f') |
|---|
| 95 |
""" |
|---|
| 96 |
|
|---|
| 97 |
def _UpdateBoxVertices(self): |
|---|
| 98 |
l,r,b,t,n,f = [float(e) for e in self.Projection.asTuple] |
|---|
| 99 |
self._Vertices.data[:4] = [(l,b,n), (r,b,n), (r,t,n), (l,t,n)] |
|---|
| 100 |
self._Vertices.data[4:] = [(l,b,f), (r,b,f), (r,t,f), (l,t,f)] |
|---|
| 101 |
return self._Vertices.data |
|---|
| 102 |
|
|---|
| 103 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 104 |
|
|---|
| 105 |
class FrustumBox(ProjectionBox): |
|---|
| 106 |
""" |
|---|
| 107 |
>>> from RBRapier.Tools.Projections import Frustum |
|---|
| 108 |
>>> FrustumBox(Frustum(-1, 1, -1, 1, 1, 2))._UpdateBoxVertices() |
|---|
| 109 |
array([[-1., -1., 1.], |
|---|
| 110 |
[ 1., -1., 1.], |
|---|
| 111 |
[ 1., 1., 1.], |
|---|
| 112 |
[-1., 1., 1.], |
|---|
| 113 |
[-2., -2., 2.], |
|---|
| 114 |
[ 2., -2., 2.], |
|---|
| 115 |
[ 2., 2., 2.], |
|---|
| 116 |
[-2., 2., 2.]],'f') |
|---|
| 117 |
>>> FrustumBox(Frustum(1, 2, 1, 2, 2, 4))._UpdateBoxVertices() |
|---|
| 118 |
array([[ 1., 1., 2.], |
|---|
| 119 |
[ 2., 1., 2.], |
|---|
| 120 |
[ 2., 2., 2.], |
|---|
| 121 |
[ 1., 2., 2.], |
|---|
| 122 |
[ 2., 2., 4.], |
|---|
| 123 |
[ 4., 2., 4.], |
|---|
| 124 |
[ 4., 4., 4.], |
|---|
| 125 |
[ 2., 4., 4.]],'f') |
|---|
| 126 |
""" |
|---|
| 127 |
|
|---|
| 128 |
def _UpdateBoxVertices(self): |
|---|
| 129 |
l,r,b,t,n,f = [float(e) for e in self.Projection.asTuple] |
|---|
| 130 |
self._Vertices.data[:4] = [(l,b,n), (r,b,n), (r,t,n), (l,t,n)] |
|---|
| 131 |
scale = f/n |
|---|
| 132 |
l = l*scale |
|---|
| 133 |
r = r*scale |
|---|
| 134 |
b = b*scale |
|---|
| 135 |
t = t*scale |
|---|
| 136 |
self._Vertices.data[4:] = [(l,b,f), (r,b,f), (r,t,f), (l,t,f)] |
|---|
| 137 |
return self._Vertices.data |
|---|
| 138 |
|
|---|
| 139 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 140 |
#~ Testing |
|---|
| 141 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 142 |
|
|---|
| 143 |
if __name__=='__main__': |
|---|
| 144 |
print "Testing..." |
|---|
| 145 |
import doctest |
|---|
| 146 |
import ProjectionBoxes as _testmod |
|---|
| 147 |
doctest.testmod(_testmod) |
|---|
| 148 |
print "Test complete." |
|---|
| 149 |
|
|---|
| 150 |
|
|---|