[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1991] trunk/py/scripts/addons/ io_scene_x3d: WIP H3D/X3D code.

Campbell Barton ideasman42 at gmail.com
Thu Jun 2 03:25:08 CEST 2011


Revision: 1991
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=1991
Author:   campbellbarton
Date:     2011-06-02 01:25:06 +0000 (Thu, 02 Jun 2011)
Log Message:
-----------
WIP H3D/X3D code.

Modified Paths:
--------------
    trunk/py/scripts/addons/io_scene_x3d/__init__.py
    trunk/py/scripts/addons/io_scene_x3d/export_x3d.py

Modified: trunk/py/scripts/addons/io_scene_x3d/__init__.py
===================================================================
--- trunk/py/scripts/addons/io_scene_x3d/__init__.py	2011-06-01 00:17:03 UTC (rev 1990)
+++ trunk/py/scripts/addons/io_scene_x3d/__init__.py	2011-06-02 01:25:06 UTC (rev 1991)
@@ -99,6 +99,7 @@
     use_triangulate = BoolProperty(name="Triangulate", description="Write quads into 'IndexedTriangleSet'", default=True)
     use_normals = BoolProperty(name="Normals", description="Write normals with geometry", default=False)
     use_compress = BoolProperty(name="Compress", description="GZip the resulting file, requires a full python install", default=False)
+    use_h3d = BoolProperty(name="H3D Extensions", description="Export shaders for H3D", default=False)
 
     axis_forward = EnumProperty(
             name="Forward",

Modified: trunk/py/scripts/addons/io_scene_x3d/export_x3d.py
===================================================================
--- trunk/py/scripts/addons/io_scene_x3d/export_x3d.py	2011-06-01 00:17:03 UTC (rev 1990)
+++ trunk/py/scripts/addons/io_scene_x3d/export_x3d.py	2011-06-02 01:25:06 UTC (rev 1991)
@@ -105,10 +105,20 @@
            use_selection=True,
            use_triangulate=False,
            use_normals=False,
+           use_h3d=False,
            ):
 
+    # globals
     fw = file.write
+    dirname = os.path.dirname(file.name)
+    gpu_shader_cache = {}
 
+    if use_h3d:
+        import gpu
+        gpu_shader_dummy_mat = bpy.data.materials.new("X3D_DYMMY_MAT")
+        gpu_shader_cache[None] = gpu.export_shader(scene, gpu_shader_dummy_mat)
+
+
 ##########################################################
 # Writing nodes routines
 ##########################################################
@@ -118,8 +128,12 @@
         #bfile = sys.expandpath( Blender.Get('filepath') ).replace('<', '&lt').replace('>', '&gt')
         bfile = repr(os.path.basename(filepath).replace('<', '&lt').replace('>', '&gt'))[1:-1]  # use outfile name
         fw("%s<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" % ident)
-        fw("%s<!DOCTYPE X3D PUBLIC \"ISO//Web3D//DTD X3D 3.0//EN\" \"http://www.web3d.org/specifications/x3d-3.0.dtd\">\n" % ident)
-        fw("%s<X3D version=\"3.0\" profile=\"Immersive\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema-instance\" xsd:noNamespaceSchemaLocation=\"http://www.web3d.org/specifications/x3d-3.0.xsd\">\n" % ident)
+        if use_h3d:
+            fw("%s<X3D profile=\"H3DAPI\" version=\"1.4\">\n" % ident)
+        else:
+            fw("%s<!DOCTYPE X3D PUBLIC \"ISO//Web3D//DTD X3D 3.0//EN\" \"http://www.web3d.org/specifications/x3d-3.0.dtd\">\n" % ident)
+            fw("%s<X3D version=\"3.0\" profile=\"Immersive\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema-instance\" xsd:noNamespaceSchemaLocation=\"http://www.web3d.org/specifications/x3d-3.0.xsd\">\n" % ident)
+
         ident += "\t"
         fw("%s<head>\n" % ident)
         ident += "\t"
@@ -391,10 +405,26 @@
                             is_smooth = True
                             break
 
+                    if use_h3d:
+                        gpu_shader = gpu_shader_cache.get(material)  # material can be 'None', uses dummy cache
+                        if gpu_shader is None:
+                            gpu_shader = gpu_shader_cache[material] = gpu.export_shader(scene, material)
+                            
+                            if 1:  # XXX DEBUG
+                                gpu_shader_tmp = gpu.export_shader(scene, material)
+                                import pprint
+                                print("\nWRITING MATERIAL:", material.name)
+                                del gpu_shader_tmp['fragment']
+                                del gpu_shader_tmp['vertex']
+                                pprint.pprint(gpu_shader_tmp, width=120)
+                                #pprint.pprint(val['vertex'])
+                                del gpu_shader_tmp
+                            
+
                     fw("%s<Appearance>\n" % ident)
                     ident += "\t"
 
-                    if image:
+                    if image and not use_h3d:
                         writeImageTexture(ident, image)
 
                         if mesh_materials_use_face_texture[material_index]:
@@ -424,7 +454,12 @@
                             fw("rotation=\"%.6g\" " % rot)
                             fw("/>\n")
 
-                    if material:
+                    if use_h3d:
+                        mat_tmp = material if material else gpu_shader_dummy_mat
+                        writeMaterialH3D(ident, mat_tmp, clean_str(mat_tmp.name, ""), world,
+                                         ob, gpu_shader)
+                        del mat_tmp
+                    else:
                         writeMaterial(ident, material, clean_str(material.name, ""), world)
 
                     ident = ident[:-1]
@@ -471,7 +506,7 @@
 
                             def vertex_key(fidx, f_cnr_idx):
                                 return (
-                                    getattr(mesh_faces_col[fidx], "color%d" % (f_cnr_idx))[:],
+                                    getattr(mesh_faces_col[fidx], "color%d" % (f_cnr_idx + 1))[:],
                                 )
                         else:
                             # ack, not especially efficient in this case
@@ -549,6 +584,30 @@
                                 fw("%.3g %.3g %.3g " % x3d_v[0][slot_col])
                             fw("\" />\n")
 
+
+                        if use_h3d:
+                            # write attributes
+                            for gpu_attr in gpu_shader['attributes']:
+                                
+                                # UVs
+                                if gpu_attr['type'] == gpu.CD_MTFACE:
+                                    if gpu_attr['datatype'] == gpu.GPU_DATA_2F:
+                                        fw("%s<FloatVertexAttribute " % ident)
+                                        fw('name="%s" ' % gpu_attr['varname'])
+                                        fw('numComponents="2" ')
+                                        fw('value="')
+                                        for x3d_v in vert_tri_list:
+                                            fw("%.4g %.4g " % x3d_v[0][slot_uv])
+                                        fw('" />\n')
+                                    else:
+                                        assert(0)
+
+                                elif gpu_attr['type'] == gpu.CD_MCOL:
+                                    if gpu_attr['datatype'] == gpu.GPU_DATA_4UB:
+                                        pass  # XXX, H3D can't do
+                                    else:
+                                        assert(0)
+
                         fw("%s</IndexedTriangleSet>\n" % ident)
 
                     else:
@@ -660,10 +719,10 @@
             ident = ident[:-1]
             fw("%s</Collision>\n" % ident)
 
-    def writeMaterial(ident, mat, matName, world):
+    def writeMaterial(ident, mat, material_id, world):
         # look up material name, use it if available
         if mat.tag:
-            fw("%s<Material USE=\"MA_%s\" />\n" % (ident, matName))
+            fw("%s<Material USE=\"MA_%s\" />\n" % (ident, material_id))
         else:
             mat.tag = True
 
@@ -685,14 +744,177 @@
                 shininess = 0.0
                 specColor = emitColor = diffuseColor
 
-            fw("%s<Material DEF=\"MA_%s\" " % (ident, matName))
+            fw("%s<Material DEF=\"MA_%s\" " % (ident, material_id))
             fw("diffuseColor=\"%.3g %.3g %.3g\" " % clamp_color(diffuseColor))
             fw("specularColor=\"%.3g %.3g %.3g\" " % clamp_color(specColor))
             fw("emissiveColor=\"%.3g %.3g %.3g\" " % clamp_color(emitColor))
             fw("ambientIntensity=\"%.3g\" " % ambient)
             fw("shininess=\"%.3g\" " % shininess)
-            fw("transparency=\"%s\" />\n" % transp)
+            fw("transparency=\"%s\" " % transp)
+            fw("/>\n")
 
+    def writeMaterialH3D(ident, mat, material_id, world,
+                         ob, gpu_shader):
+
+        fw("%s<Material />\n" % ident)
+        if mat.tag:
+            fw("%s<ComposedShader USE=\"MA_%s\" />\n" % (ident, material_id))
+        else:
+            mat.tag = True
+
+            #~ CD_MCOL 6
+            #~ CD_MTFACE 5
+            #~ CD_ORCO 14
+            #~ CD_TANGENT 18
+            #~ GPU_DATA_16F 7
+            #~ GPU_DATA_1F 2
+            #~ GPU_DATA_1I 1
+            #~ GPU_DATA_2F 3
+            #~ GPU_DATA_3F 4
+            #~ GPU_DATA_4F 5
+            #~ GPU_DATA_4UB 8
+            #~ GPU_DATA_9F 6
+            #~ GPU_DYNAMIC_LAMP_DYNCO 7
+            #~ GPU_DYNAMIC_LAMP_DYNCOL 11
+            #~ GPU_DYNAMIC_LAMP_DYNENERGY 10
+            #~ GPU_DYNAMIC_LAMP_DYNIMAT 8
+            #~ GPU_DYNAMIC_LAMP_DYNPERSMAT 9
+            #~ GPU_DYNAMIC_LAMP_DYNVEC 6
+            #~ GPU_DYNAMIC_OBJECT_COLOR 5
+            #~ GPU_DYNAMIC_OBJECT_IMAT 4
+            #~ GPU_DYNAMIC_OBJECT_MAT 2
+            #~ GPU_DYNAMIC_OBJECT_VIEWIMAT 3
+            #~ GPU_DYNAMIC_OBJECT_VIEWMAT 1
+            #~ GPU_DYNAMIC_SAMPLER_2DBUFFER 12
+            #~ GPU_DYNAMIC_SAMPLER_2DIMAGE 13
+            #~ GPU_DYNAMIC_SAMPLER_2DSHADOW 14
+
+
+            '''
+            inline const char* typeToString( X3DType t ) {
+              switch( t ) {
+              case     SFFLOAT: return "SFFloat";
+              case     MFFLOAT: return "MFFloat";
+              case    SFDOUBLE: return "SFDouble";
+              case    MFDOUBLE: return "MFDouble";
+              case      SFTIME: return "SFTime";
+              case      MFTIME: return "MFTime";
+              case     SFINT32: return "SFInt32";
+              case     MFINT32: return "MFInt32";
+              case     SFVEC2F: return "SFVec2f";
+              case     MFVEC2F: return "MFVec2f";
+              case     SFVEC2D: return "SFVec2d";
+              case     MFVEC2D: return "MFVec2d";
+              case     SFVEC3F: return "SFVec3f";
+              case     MFVEC3F: return "MFVec3f";
+              case     SFVEC3D: return "SFVec3d";
+              case     MFVEC3D: return "MFVec3d";
+              case     SFVEC4F: return "SFVec4f";
+              case     MFVEC4F: return "MFVec4f";
+              case     SFVEC4D: return "SFVec4d";

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list