Package VisionEgg :: Module GLTrace
[frames] | no frames]

Source Code for Module VisionEgg.GLTrace

  1  # The Vision Egg: GLTrace 
  2  # 
  3  # Copyright (C) 2001-2004 Andrew Straw 
  4  # Copyright (C) 2004,2008 California Institute of Technology 
  5  # 
  6  # URL: <http://www.visionegg.org/> 
  7  # 
  8  # Distributed under the terms of the GNU Lesser General Public License 
  9  # (LGPL). See LICENSE.TXT that came with this file. 
 10   
 11  """ 
 12  Trace calls to OpenGL 
 13   
 14  With this module, you can trace all calls made to OpenGL through PyOpenGL. 
 15  To do this, substitute 
 16   
 17  import OpenGL.GL as gl 
 18   
 19  with 
 20   
 21  import VisionEgg.GLTrace as gl 
 22   
 23  in your code. 
 24   
 25  Also, trace another module's use of OpenGL by changing its reference 
 26  to OpenGL.GL to a reference to VisionEgg.GLTrace. 
 27   
 28  """ 
 29   
 30  # Copyright (c) 2003 Andrew Straw.  Distributed under the terms of the 
 31  # GNU Lesser General Public License (LGPL). 
 32   
 33  #################################################################### 
 34  # 
 35  #        Import all the necessary packages 
 36  # 
 37  #################################################################### 
 38   
 39  import OpenGL.GL as gl 
 40   
 41  gl_constants = {} 
 42   
 43  # functions which we don't want to translate arguments to gl constant 
 44  raw_args_by_function = { 
 45      'glColor':[0,1,2,3], 
 46      'glColorf':[0,1,2,3], 
 47      'glDepthRange':[0,1], 
 48      'glGenTextures':[0], 
 49      'glGetTexLevelParameteriv':[1], 
 50      'glOrtho':[0,1,2,3,4,5], 
 51      'glPixelStorei':[1], 
 52      'glReadPixels':[0,1,2,3], 
 53      'glRotate':[0,1,2,3], 
 54      'glTexCoord2f':[0,1], 
 55      'glTexImage1D':[1,3,4], 
 56      'glTexImage2D':[1,3,4,5], 
 57      'glTexSubImage1D':[1,2,3], 
 58      'glTranslate':[0,1,2], 
 59      'glVertex2f':[0,1], 
 60      'glVertex3f':[0,1,2], 
 61      'glViewport':[0,1,2,3], 
 62      } 
 63   
 64  bitmasks_by_function = { 
 65      'glClear':[0], 
 66      } 
 67   
 68  bitmask_names_by_value = { 
 69      gl.GL_COLOR_BUFFER_BIT : 'GL_COLOR_BUFFER_BIT', 
 70      gl.GL_DEPTH_BUFFER_BIT : 'GL_DEPTH_BUFFER_BIT', 
 71      } 
 72   
73 -def arg_to_str( arg ):
74 if isinstance(arg, int): 75 if arg in gl_constants.keys(): 76 return gl_constants[arg] 77 elif type(arg) == str and len(arg) > 30: 78 return "'<string>'" 79 return repr(arg)
80
81 -class Wrapper:
82 - def __init__(self, function_name):
83 self.function_name = function_name 84 self.orig_func = getattr(gl,self.function_name)
85 - def run(self,*args,**kw):
86 if kw: kw_str = " AND KEYWORDS" 87 else: kw_str = "" 88 if 1: 89 ## if self.function_name in raw_args_by_function: 90 arg_str = [] 91 for i in range(len(args)): 92 if self.function_name in raw_args_by_function and i in raw_args_by_function[self.function_name]: 93 arg_str.append(str(args[i])) # don't convert to name of OpenGL constant 94 elif self.function_name in bitmasks_by_function and i in bitmasks_by_function[self.function_name]: 95 bitmask_strs = [] 96 value = args[i] 97 for bit_value in bitmask_names_by_value: 98 if value & bit_value: 99 value = value & ~bit_value 100 bitmask_strs.append( bitmask_names_by_value[bit_value] ) 101 if value != 0: 102 bitmask_strs.append( arg_to_str(args[i]) ) 103 arg_str.append( '|'.join(bitmask_strs) ) 104 else: 105 arg_str.append(arg_to_str(args[i])) # convert to name of OpenGL constant 106 arg_str = ','.join( arg_str ) 107 try: 108 result = self.orig_func(*args,**kw) 109 except: 110 print "%s(%s)%s"%(self.function_name,arg_str,kw_str) 111 raise 112 113 if result is not None: 114 result_str = "%s = "%(arg_to_str(result),) 115 else: 116 result_str = '' 117 print "%s%s(%s)%s"%(result_str,self.function_name,arg_str,kw_str) 118 return result
119
120 -def gl_trace_attach():
121 for attr_name in dir(gl): 122 if callable( getattr(gl,attr_name) ): 123 wrapper = Wrapper(attr_name) 124 globals()[attr_name] = wrapper.run 125 else: 126 attr = getattr(gl,attr_name) 127 globals()[attr_name] = attr 128 if not attr_name.startswith('__') and not attr_name.startswith('__'): 129 if type(getattr(gl,attr_name)) == int: 130 gl_constants[getattr(gl,attr_name)] = attr_name 131 if not hasattr(gl,'GL_CLAMP_TO_EDGE'): 132 # XXX This hack requires opengl >= 1.2. See Core.py for more info. 133 globals()['GL_CLAMP_TO_EDGE'] = 0x812F 134 if hasattr(gl,'glActiveTexture'): 135 # XXX Another, similar hack. 136 globals()['glActiveTextureARB'] = gl.glActiveTexture 137 globals()['glMultiTexCoord2fARB'] = gl.glMultiTexCoord2f 138 globals()['GL_TEXTURE0_ARB'] = gl.GL_TEXTURE0 139 globals()['GL_TEXTURE1_ARB'] = gl.GL_TEXTURE1
140 141 gl_trace_attach() # attach when imported 142