~matobet/pyopengl/Python3

« back to all changes in this revision

Viewing changes to GL/selection.py.bak

  • Committer: matobet at gmail
  • Date: 2010-06-26 14:11:04 UTC
  • Revision ID: matobet@gmail.com-20100626141104-k011ofmltgiiu60g
Initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Selection-buffer handling code
 
2
 
 
3
This code is resonsible for turning gluint *
 
4
arrays into structured representations for use
 
5
by Python-level code.
 
6
"""
 
7
def uintToLong( value ):
 
8
    if value < 0:
 
9
        # array type without a uint, so represented as an int 
 
10
        value = (value & 0x7fffffff) + 0x80000000
 
11
    return value
 
12
 
 
13
 
 
14
class GLSelectRecord( object ):
 
15
    """Minimalist object for storing an OpenGL selection-buffer record
 
16
    
 
17
    Provides near and far as *float* values by dividing by 
 
18
    self.DISTANCE_DIVISOR (2**32-1)
 
19
    From the spec:
 
20
        Depth values (which are in the range [0,1]) are multiplied by 
 
21
        2^32 - 1, before being placed in the hit record.
 
22
    
 
23
    Names are unmodified, so normally are slices of the array passed in 
 
24
    to GLSelectRecord.fromArray( array )
 
25
    """
 
26
    DISTANCE_DIVISOR = float((2L**32)-1)
 
27
    __slots__ = ('near','far','names')
 
28
    def fromArray( cls, array, total ):
 
29
        """Produce list with all records from the array"""
 
30
        result = []
 
31
        index = 0
 
32
        arrayLength = len(array)
 
33
        for item in xrange( total ):
 
34
            if index + 2 >= arrayLength:
 
35
                break
 
36
            count = array[index]
 
37
            near = array[index+1]
 
38
            far = array[index+2]
 
39
            names = map(uintToLong, array[index+3:index+3+count])
 
40
            result.append(  cls( near, far, names ) )
 
41
            index += 3+count
 
42
        return result
 
43
    fromArray = classmethod( fromArray )
 
44
    
 
45
    def __init__( self, near, far, names ):
 
46
        """Initialise/store the values"""
 
47
        self.near = self.convertDistance( near )
 
48
        self.far = self.convertDistance( far )
 
49
        self.names = names 
 
50
    def convertDistance( self, value ):
 
51
        """Convert a distance value from array uint to 0.0-1.0 range float"""
 
52
        return uintToLong( value ) / self.DISTANCE_DIVISOR
 
53
    def __getitem__( self, key ):
 
54
        """Allow for treating the record as a three-tuple"""
 
55
        if isinstance( key, (int,long)):
 
56
            return (self.near,self.far,self.names)[key]
 
57
        elif key in self.__slots__:
 
58
            try:
 
59
                return getattr( self, key )
 
60
            except AttributeError, err:
 
61
                raise KeyError( """Don't have an index/key %r for %s instant"""%(
 
62
                    key, self.__class__,
 
63
                ))
 
 
b'\\ No newline at end of file'