numbers.py :  » Game-2D-3D » PyOpenGL » PyOpenGL-3.0.1 » OpenGL » arrays » 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 » arrays » numbers.py
"""Numbers passed as array handling code for PyOpenGL
"""
REGISTRY_NAME = 'numbers'
from OpenGL import constants
from OpenGL.arrays import formathandler
import ctypes

class NumberHandler( formathandler.FormatHandler ):
    """Allows the user to pass a bald Python float,int, etceteras as an array-of-1"""
    HANDLED_TYPES = (
        int,long,float,
        constants.GLdouble,
        constants.GLfloat,
        constants.GLint,
        constants.GLshort,
        constants.GLuint,
        constants.GLushort, 
        constants.GLclampf,
        constants.GLclampd,
    )
    def from_param( self, value, typeCode=None  ):
        """If it's a ctypes value, pass on, otherwise do asArray"""
        try:
            return ctypes.byref(value)
        except TypeError, err:
            err.args += (' If you have ERROR_ON_COPY enabled, remember to pass in an array to array-requiring functions.', )
            raise
    dataPointer = from_param
    def zeros( self, dims, typeCode=None ):
        """Currently don't allow Number as output types!"""
        raise NotImplemented( """Number data-type not allowed as an output array format""" )
    def ones( self, dims, typeCode=None ):
        """Currently don't allow Number as output types!"""
        raise NotImplemented( """Number data-type not allowed as an output array format""" )
    def arrayToGLType( self, value ):
        """Given a value, guess OpenGL type of the corresponding pointer"""
        if value.__class__ in TARGET_TYPES:
            return TARGET_TYPES[ value.__class__ ]
        else:
            guess = DEFAULT_TYPES.get( value.__class__ )
            if guess is not None:
                return guess[1]
            raise TypeError( """Can't guess array data-type for %r types"""%(type(value)))
    def arraySize( self, value, typeCode = None ):
        """Given a data-value, calculate ravelled size for the array"""
        return 1
    def asArray( self, value, typeCode=None ):
        """Convert given value to an array value of given typeCode"""
        
        if value.__class__ in TARGET_TYPES:
            return value
        targetType = CONSTANT_TO_TYPE.get( typeCode )
        if targetType is not None:
            return targetType( value )
        raise TypeError( """Don't know how to convert %r to an array type"""%(
            typeCode,
        ))
    def unitSize( self, value, typeCode=None ):
        """Determine unit size of an array (if possible)"""
        return 1 # there's only 1 possible value in the set...
    def registerEquivalent( self, typ, base ):
        """Register a sub-class for handling as the base-type"""
        global TARGET_TYPE_TUPLE
        for source in (DEFAULT_TYPES, TARGET_TYPES, BYTE_SIZES):
            if source.has_key( base ):
                source[typ] = source[base]
        if TARGET_TYPES.has_key( base ):
            TARGET_TYPE_TUPLE = TARGET_TYPE_TUPLE + (base,)

DEFAULT_TYPES = {
    float: (constants.GLdouble,constants.GL_DOUBLE),
    int: (constants.GLint,constants.GL_INT),
    long: (constants.GLint,constants.GL_INT),
}
TARGET_TYPES = dict([
    (getattr( constants,n),c)
    for (n,c) in constants.ARRAY_TYPE_TO_CONSTANT
])
TARGET_TYPE_TUPLE = tuple([
    getattr(constants,n) 
    for (n,c) in constants.ARRAY_TYPE_TO_CONSTANT
])
CONSTANT_TO_TYPE = dict([
    (c,getattr( constants, n))
    for (n,c) in constants.ARRAY_TYPE_TO_CONSTANT
])

BYTE_SIZES = dict([
    ( c, ctypes.sizeof( getattr( constants, n) ) )
    for (n,c) in constants.ARRAY_TYPE_TO_CONSTANT
])

del n,c
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.