Changeset 1be8a643f08780c1a77c7db8ff813e238714af3b


Ignore:
Timestamp:
02/20/06 06:16:56 (6 years ago)
Author:
Administrateur <Administrateur@…>
Children:
7f4f0d8a9b76e4598fb2384c0c4ea43de6827161
Parents:
ef76b4592a44ebf22ec09f600cf3bdb95f5b70b3
git-committer:
Administrateur <Administrateur@…> (02/20/06 06:16:56)
Message:

Refactored the shader xml : images, textures and shaders are now in separate sections, referencing each other by ID.
Also moved the parsers code in parsers submodule, with base classes for other parsers.

Note: still WIP, parser not working.

git-svn-id:  http://kawa.selfip.org/svn/projects@150 532e76a9-45ae-c241-9c6d-8309ac6440cd

Location:
lambert/graphics
Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • lambert/graphics/api/__init__.py

    ra0cf643 r1be8a64  
    3838    ) 
    3939 
     40 
    4041def stripExtensionSuffix(name): 
    4142  """ Remove the suffix for extensions tokens and methods. """ 
     
    5960def init(verbose=False): 
    6061  """ Initialize extensions and get important hardware information such as 
    61   number of texture units """ 
     62  number of texture units, etc... 
     63        """ 
    6264  global hardwareCaps, textureUnits 
    6365 
     
    6971  # Force this extension being marked as not supported, since PyOpenGL reports 
    7072  # that it is supported although it is not 
    71   hardwareCaps["extensions"]["vertex_buffer_object"] = 0 #OpenGL.GL.ARB.vertex_buffer_object.glInitVertexBufferObjectARB()   
     73  hardwareCaps["extensions"]["vertex_buffer_object"] = 0  
     74        #OpenGL.GL.ARB.vertex_buffer_object.glInitVertexBufferObjectARB()   
    7275 
    7376  # Get hardware capabilities 
     
    172175    "color": texture_env_color 
    173176    } 
     177 
     178 
     179def evalParameter(parameter, keywordsMap=None): 
     180  """ Evaluate a parameter. 
     181 
     182  If parameter is a string, try to get the value from the api module, then try 
     183  to take it from the given keywords map, and finally return it as is. 
     184  If parameter is not a string, return it as is. 
     185  """ 
     186  if type(parameter) in types.StringTypes: 
     187    try: 
     188      result = globals()[parameter] 
     189    except AttributeError: 
     190      try: 
     191        result = keywordsMap[parameter] 
     192      except: 
     193        result = parameter 
     194  else: 
     195    result = parameter 
     196  return result 
  • lambert/graphics/default_shaders.xml

    ra0cf643 r1be8a64  
    1 <shaders> 
     1<xml> 
     2        <images> 
     3                <image id="Default" src="" /> 
     4                <image id="earth_color" src="data/earth_color.png" /> 
     5                <image id="earth_clouds" src="data/earth_clouds.png" /> 
     6        </images> 
     7         
     8        <textures> 
     9                <!-- Textures section defines texture objects.  
     10                 
     11                Each texture object is the combination of an image and states. 
     12                States are themselves compound of environment and parameters. 
     13                --> 
    214 
    3   <!-- The default shader. 
    4    
    5   This is a special shader, that is set when initializing the shader module, 
    6   and used as the reference when a shader leaves bits in an undefined state. 
    7   It is also used to get default values for partial shaders definitions. For  
    8   example, when omitting the alpha component for a color state, the default  
    9   alpha value is taken here. 
    10    
    11   Hence the name "Default" is a reserved name and must not be used for other 
    12   purposes. This shader must also be defined *first* in the main definitions 
    13   file. 
     15                <texture id="Default" image="Default"> 
     16                        <parameters> 
     17                                <min_filter>linear</min_filter> 
     18                                <mag_filter>linear</mag_filter> 
     19                                <wrap s="repeat" t="repeat" r="repeat" /> 
     20                        </parameters> 
     21                        <environment> 
     22                                <color r="0" g="0" b="0" a="0" /> 
     23                                <mode>replace</mode> 
     24                        </environment>                           
     25                </texture> 
     26                 
     27                <texture id="Earth" image="earth_color"> 
     28                        <parameters> 
     29                                <min_filter>linear</min_filter> 
     30                                <mag_filter>linear</mag_filter> 
     31                                <wrap s="repeat" t="repeat" /> 
     32                        </parameters> 
     33                </texture> 
     34        </textures> 
     35         
     36        <shaders> 
    1437 
    15   If this shader is loaded before shader.init() has been called, it will also  
    16   be used to set the initial rendering context state. 
    17   --> 
    18   <shader name="Default"> 
    19     <states> 
    20       <color r="1.0" g="1.0" b="1.0" a="1.0" /> 
    21       <polygon_mode front="fill" back="fill" /> 
    22       <shade_model>smooth</shade_model> 
    23     </states> 
    24   </shader> 
    25    
    26   <!-- Flat and smooth polygons shader --> 
    27   <shader name="Flat"> 
    28     <states> 
    29       <polygon_mode>flat</polygon_mode> 
    30     </states> 
    31   </shader> 
     38                <!-- The default shader. 
     39                 
     40                This is a special shader, that is set when initializing the shader module, 
     41                and used as the reference when a shader leaves bits in an undefined state. 
     42                It is also used to get default values for partial shaders definitions. For  
     43                example, when omitting the alpha component for a color state, the default  
     44                alpha value is taken here. 
     45                 
     46                Hence the name "Default" is a reserved name and must not be used for other 
     47                purposes. This shader must also be defined *first* in the main definitions 
     48                file. 
    3249 
    33   <shader name="Smooth"> 
    34     <states> 
    35       <polygon_mode>smooth</polygon_mode> 
    36     </states> 
    37   </shader> 
     50                If this shader is loaded before shader.init() has been called, it will also  
     51                be used to set the initial rendering context state. 
     52                --> 
     53                <shader id="Default"> 
     54                        <states> 
     55                                <color r="1.0" g="1.0" b="1.0" a="1.0" /> 
     56                                <polygon_mode front="fill" back="fill" /> 
     57                                <shade_model>smooth</shade_model> 
     58                        </states> 
     59                </shader> 
     60                 
     61                <!-- Flat and smooth polygons shader --> 
     62                <shader id="Flat"> 
     63                        <states> 
     64                                <polygon_mode>flat</polygon_mode> 
     65                        </states> 
     66                </shader> 
    3867 
    39   <!-- Standard colors --> 
    40   <shader name="White"> 
    41     <states> 
    42       <color r="1.0" g="1.0" b="1.0" /> 
    43     </states> 
    44   </shader> 
     68                <shader id="Smooth"> 
     69                        <states> 
     70                                <polygon_mode>smooth</polygon_mode> 
     71                        </states> 
     72                </shader> 
    4573 
    46   <shader name="Gray"> 
    47     <states> 
    48       <color r="0.666" g="0.666" b="0.666" /> 
    49     </states> 
    50   </shader> 
     74                <!-- Standard colors --> 
     75                <shader id="White"> 
     76                        <states> 
     77                                <color r="1.0" g="1.0" b="1.0" /> 
     78                        </states> 
     79                </shader> 
    5180 
    52   <shader name="Grey" parents="Gray" /> 
     81                <shader id="Gray"> 
     82                        <states> 
     83                                <color r="0.666" g="0.666" b="0.666" /> 
     84                        </states> 
     85                </shader> 
    5386 
    54   <shader name="Red"> 
    55     <states> 
    56       <color r="1.0" g="0.0" b="0.0" />  
    57     </states> 
    58   </shader> 
     87                <shader id="Grey" parents="Gray" /> 
    5988 
    60   <shader name="Green"> 
    61     <states> 
    62       <color r="0.0" g="1.0" b="0.0" />  
    63     </states> 
    64   </shader> 
     89                <shader id="Red"> 
     90                        <states> 
     91                                <color r="1.0" g="0.0" b="0.0" />  
     92                        </states> 
     93                </shader> 
    6594 
    66   <shader name="Blue"> 
    67     <states> 
    68       <color r="0.0" g="0.0" b="1.0" />  
    69     </states> 
    70   </shader> 
     95                <shader id="Green"> 
     96                        <states> 
     97                                <color r="0.0" g="1.0" b="0.0" />  
     98                        </states> 
     99                </shader> 
    71100 
    72   <shader name="Black"> 
    73     <states> 
    74       <color r="0.0" g="0.0" b="0.0" />  
    75     </states> 
    76   </shader> 
     101                <shader id="Blue"> 
     102                        <states> 
     103                                <color r="0.0" g="0.0" b="1.0" />  
     104                        </states> 
     105                </shader> 
    77106 
    78   <!-- Wireframe white shader --> 
    79   <shader name="Wireframe" parents="White"> 
    80     <states> 
    81       <polygon_mode front="line" back="line" /> 
    82     </states> 
    83   </shader> 
     107                <shader id="Black"> 
     108                        <states> 
     109                                <color r="0.0" g="0.0" b="0.0" />  
     110                        </states> 
     111                </shader> 
    84112 
    85   <!-- Earth texture map --> 
    86   <shader name="Earth"> 
    87     <textures> 
    88       <texture file="data/earth_color.png" /> 
    89     </textures> 
    90   </shader>   
     113                <!-- Wireframe white shader --> 
     114                <shader id="Wireframe" parents="White"> 
     115                        <states> 
     116                                <polygon_mode front="line" back="line" /> 
     117                        </states> 
     118                </shader> 
    91119 
    92   <!-- Multiple inheritance test --> 
    93   <shader name="SharpEarth" parents="Earth, Flat, White"> 
    94     <textures> 
    95       <texture file="data/earth_color.png"> 
    96         <parameters> 
    97           <min_filter>nearest</min_filter> 
    98           <mag_filter>nearest</mag_filter> 
    99         </parameters> 
    100       </texture> 
    101     </textures> 
    102   </shader> 
     120                <!-- Earth texture map --> 
     121                <shader id="Earth"> 
     122                        <textures> 
     123                                Earth                            
     124                        </textures> 
     125                </shader>   
    103126 
    104 </shaders> 
     127                <!-- Multiple inheritance test --> 
     128                <shader id="FlatEarth" parents="Earth, Flat, White" /> 
     129 
     130        </shaders> 
     131</xml> 
  • lambert/graphics/mesh.py

    ra0cf643 r1be8a64  
    632632 
    633633  # Create a smooth red shader and a mapped shader 
    634   flatRed = shader.Shader(smooth=shader.flat, red=shader.red) 
     634  flatRed = shader.Shader(smooth=flat, red=shader.red) 
    635635  earthTexture = texture.Texture("data/earth_color.png",  
    636636      #environment={"mode": api.replace} 
    637637      )   
    638   cloudsTexture = texture.Texture("data/earth_clouds.png", format=api.luminance,  
     638  cloudsTexture = texture.Texture("data/earth_clouds.png", format=api.luminance, 
    639639      parameters={"mag_filter": api.nearest,  
    640640                  "min_filter": api.nearest}, 
  • lambert/graphics/shader.py

    ra0cf643 r1be8a64  
    11import api 
    2 import texture 
    3 import xml.parsers.expat 
    4 import types 
    52import os.path 
     3import parsers.shader 
    64 
    75 
    8 # This is set to True by module's init function 
    96_initDone = False 
    10  
    11 # Default shaders filename 
    127defaultShadersFilename = "default_shaders.xml" 
    138 
    149 
    15 def evalParameter(parameter, keywordsMap=None): 
    16   """ Evaluate a parameter. 
     10def init():       
     11  global _initDone, defaultShadersFilename, shaders, textures 
     12   
     13  # If the default shaders file is present, load it 
     14  if os.path.exists(defaultShadersFilename): 
     15    shaders, textures = parsers.shader.parse(open(defaultShadersFilename)) 
     16  else: 
     17    shaders["Default"] = Shader(**Shader.defaults) 
     18    textures = {} 
    1719 
    18   If parameter is a string, try to get the value from the api module, then try 
    19   to take it from the given keywords map, and finally return it as is. 
    20   If parameter is not a string, return it as is. 
    21   """ 
    22   if type(parameter) in types.StringTypes: 
    23     try: 
    24       result = getattr(api, parameter) 
    25     except AttributeError: 
    26       try: 
    27         result = keywordsMap[parameter] 
    28       except: 
    29         result = parameter 
    30   else: 
    31     result = parameter 
    32   return result 
     20  # Set the default shader 
     21  shaders["Default"].set() 
     22 
     23  # Mark the module as initialized 
     24  _initDone = True 
    3325 
    3426 
     
    5143  defaults = { 
    5244    "color": (1.0, 1.0, 1.0, 1.0), 
    53     "polygon_mode": {"front": "fill", "back": "fill"}, 
    54     "shade_model": "smooth" 
     45    "polygon_mode": {api.front: api.fill, api.back: api.fill}, 
     46    "shade_model": api.smooth 
    5547    } 
    5648 
     
    118110        # library calls to set them 
    119111        for subState, subData in data.iteritems(): 
    120           subState = evalParameter(subState) 
    121           subData = evalParameter(subData) 
    122112          Shader.setFunctions[state](subState, subData) 
    123113      else: 
    124114        # Data is not a dictionnary, hence we have to do one and only one  
    125115        # library call to set the state         
    126         Shader.setFunctions[state](evalParameter(data)) 
     116        Shader.setFunctions[state](data) 
    127117    Shader.stateCache[state] = data 
    128118 
     
    154144    self.texture.setup() 
    155145 
    156  
    157 def init():       
    158   global _initDone, defaultShadersFilename, shaders, textures 
    159    
    160   # If the default shaders file is present, load it 
    161   if os.path.exists(defaultShadersFilename): 
    162     shaders, textures = parseXML(open(defaultShadersFilename)) 
    163   else: 
    164     shaders["Default"] = Shader(**Shader.defaults) 
    165     textures = {} 
    166      
    167   # Set the default shader 
    168   shaders["Default"].set() 
    169  
    170   # Mark the module as initialized 
    171   _initDone = True 
    172  
    173  
    174 class ShadersParser:  
    175   # "Default" shader name 
    176   default_name = "Default" 
    177    
    178   # Source data constants 
    179   attributes = 0 
    180   data = 1 
    181    
    182   # Data types 
    183   sequence = 0 
    184   keywords = 1 
    185  
    186   # statesDescriptions indexing constants 
    187   data_source = 0 
    188   data_type = 1 
    189   data_type_args = 2 
    190  
    191   # States descriptions 
    192   statesDescriptions = { 
    193     "color": (attributes, sequence, ("r", "g", "b", "a")), 
    194     "polygon_mode": (attributes, keywords), 
    195     "shade_model": (data, keywords) 
    196     } 
    197  
    198   def __init__(self): 
    199     self.level = 0 
    200     self.shaders = {} 
    201     self.textures = {} 
    202     self.inShader = False 
    203     self.attributes = [] 
    204     self.data = [] 
    205      
    206   def StartElementHandler(self, name, attributes): 
    207     if name == "shader": 
    208       self.currentShader = self.shaders[attributes["name"]] = Shader() 
    209       self.currentShaderName = attributes["name"] 
    210     self.attributes.append(attributes) 
    211     self.data.append("") 
    212     self.level += 1 
    213  
    214   def CharacterDataHandler(self, data): 
    215     data = data.strip() 
    216     if data: 
    217       self.data[-1] += data 
    218  
    219   def EndElementHandler(self, name): 
    220     global _initDone 
    221  
    222     self.level -= 1 
    223     attributes = self.attributes.pop() 
    224     data = self.data.pop()     
    225     if name == "shader": 
    226       # Got a shader, eventually inherit parent properties 
    227       if attributes.has_key("parents"): 
    228         tempShader = Shader() 
    229         for parentName in attributes["parents"].split(","): 
    230           parentName = parentName.strip() 
    231           tempShader.injectShader(self.shaders[parentName]) 
    232         tempShader.injectShader(self.currentShader) 
    233         self.currentShader = self.shaders[self.currentShaderName] = tempShader 
    234       # If we got a shader named "Default", overwrite Shader.defaults and  
    235       # initial states in Shader 
    236       if self.currentShaderName == ShadersParser.default_name: 
    237         Shader.defaults = self.currentShader.states.copy() 
    238         if not _initDone: 
    239           Shader.stateCache = Shader.defaults.copy() 
    240       print self.currentShader 
    241     if name in ShadersParser.statesDescriptions: 
    242       # Got a state 
    243       self.parseState(name, attributes, data) 
    244  
    245   def parseState(self, name, attributes, data): 
    246     desc = ShadersParser.statesDescriptions[name] 
    247     if desc[ShadersParser.data_source] == ShadersParser.attributes: 
    248       if desc[ShadersParser.data_type] == ShadersParser.sequence: 
    249         # Constructing a sequence from attributes ; the order is given in the 
    250         # state description 
    251         seq = [] 
    252         for i, key in enumerate(desc[ShadersParser.data_type_args]): 
    253           if attributes.has_key(key): 
    254             seq.append(attributes[key]) 
    255           else: 
    256             seq.append(Shader.defaults[name][i]) 
    257         self.currentShader.states[name] = seq 
    258       elif desc[ShadersParser.data_type] == ShadersParser.keywords: 
    259         pass 
    260     elif desc[ShadersParser.data_source] == ShadersParser.data: 
    261       pass 
    262  
    263  
    264 def parseXML(file): 
    265   """ Parse an XML file containing shaders definitions. 
    266    
    267   Returns a tuple containing two dictionnaries : shaders and textures, both 
    268   indexed by name. 
    269   """ 
    270   getter = ShadersParser() 
    271   parser = xml.parsers.expat.ParserCreate() 
    272    
    273   for name in ("StartElementHandler", "EndElementHandler", 
    274       "CharacterDataHandler"): 
    275     setattr(parser, name, getattr(getter, name)) 
    276      
    277   parser.ParseFile(file) 
    278  
    279   return getter.shaders, getter.textures 
    280  
    281  
    282 if __name__ == "__main__": 
    283   defaultShaders, defaultTextures = parseXML(open("default_shaders.xml")) 
    284   # shaders, textures = parseXML(open("data/shaders.xml")) 
     146  def setDefaults(states): 
     147    Shader.defaults = states.copy() 
     148    if not _initDone: 
     149      Shader.stateCache = states.copy() 
     150  setDefaults = staticmethod(setDefaults) 
  • lambert/graphics/texture.py

    r85c9d5e r1be8a64  
    22import api 
    33import types 
     4 
     5 
     6def init(): 
     7  global disableTexturing 
     8 
     9  if api.hardwareCaps["extensions"]["multitexture"]: 
     10    disableTexturing = _disableTexturingMulti 
     11  else: 
     12    disableTexturing = _disableTexturing 
    413 
    514 
     
    167176 
    168177 
    169 def init(): 
    170   global disableTexturing 
    171  
    172   if api.hardwareCaps["extensions"]["multitexture"]: 
    173     disableTexturing = _disableTexturingMulti 
    174   else: 
    175     disableTexturing = _disableTexturing 
    176  
    177  
    178178if __name__ == "__main__": 
    179179  # Sample usage 
Note: See TracChangeset for help on using the changeset viewer.