selection.py :  » Game-2D-3D » PyOpenGL » PyOpenGL-3.0.1 » OpenGL » GL » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PyOpenGL 
PyOpenGL » PyOpenGL 3.0.1 » OpenGL » GL » selection.py
"""Selection-buffer handling code

This code is resonsible for turning gluint *
arrays into structured representations for use
by Python-level code.
"""
def uintToLong( value ):
    if value < 0:
        # array type without a uint, so represented as an int 
        value = (value & 0x7fffffff) + 0x80000000
    return value


class GLSelectRecord( object ):
    """Minimalist object for storing an OpenGL selection-buffer record
    
    Provides near and far as *float* values by dividing by 
    self.DISTANCE_DIVISOR (2**32-1)
    From the spec:
        Depth values (which are in the range [0,1]) are multiplied by 
        2^32 - 1, before being placed in the hit record.
    
    Names are unmodified, so normally are slices of the array passed in 
    to GLSelectRecord.fromArray( array )
    """
    DISTANCE_DIVISOR = float((2L**32)-1)
    __slots__ = ('near','far','names')
    def fromArray( cls, array, total ):
        """Produce list with all records from the array"""
        result = []
        index = 0
        arrayLength = len(array)
        for item in xrange( total ):
            if index + 2 >= arrayLength:
                break
            count = array[index]
            near = array[index+1]
            far = array[index+2]
            names = map(uintToLong, array[index+3:index+3+count])
            result.append(  cls( near, far, names ) )
            index += 3+count
        return result
    fromArray = classmethod( fromArray )
    
    def __init__( self, near, far, names ):
        """Initialise/store the values"""
        self.near = self.convertDistance( near )
        self.far = self.convertDistance( far )
        self.names = names 
    def convertDistance( self, value ):
        """Convert a distance value from array uint to 0.0-1.0 range float"""
        return uintToLong( value ) / self.DISTANCE_DIVISOR
    def __getitem__( self, key ):
        """Allow for treating the record as a three-tuple"""
        if isinstance( key, (int,long)):
            return (self.near,self.far,self.names)[key]
        elif key in self.__slots__:
            try:
                return getattr( self, key )
            except AttributeError, err:
                raise KeyError( """Don't have an index/key %r for %s instant"""%(
                    key, self.__class__,
                ))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.