merge with/from trunk at r35190
authorJoseph Eagar <joeedh@gmail.com>
Sun, 27 Feb 2011 06:19:40 +0000 (06:19 +0000)
committerJoseph Eagar <joeedh@gmail.com>
Sun, 27 Feb 2011 06:19:40 +0000 (06:19 +0000)
353 files changed:
1  2 
SConstruct
extern/libopenjpeg/CMakeLists.txt
intern/decimation/SConscript
intern/ghost/GHOST_C-api.h
intern/ghost/GHOST_IEvent.h
intern/ghost/GHOST_ISystem.h
intern/ghost/GHOST_IWindow.h
intern/ghost/intern/GHOST_C-api.cpp
intern/ghost/intern/GHOST_DisplayManagerCocoa.h
intern/ghost/intern/GHOST_Event.h
intern/ghost/intern/GHOST_EventButton.h
intern/ghost/intern/GHOST_EventCursor.h
intern/ghost/intern/GHOST_EventKey.h
intern/ghost/intern/GHOST_EventManager.cpp
intern/ghost/intern/GHOST_EventManager.h
intern/ghost/intern/GHOST_EventTrackpad.h
intern/ghost/intern/GHOST_EventWheel.h
intern/ghost/intern/GHOST_System.cpp
intern/ghost/intern/GHOST_System.h
intern/ghost/intern/GHOST_SystemCocoa.h
intern/ghost/intern/GHOST_SystemCocoa.mm
intern/ghost/intern/GHOST_SystemX11.cpp
intern/ghost/intern/GHOST_WindowCocoa.h
intern/ghost/intern/GHOST_WindowCocoa.mm
intern/guardedalloc/MEM_guardedalloc.h
intern/guardedalloc/SConscript
intern/guardedalloc/intern/mallocn.c
intern/guardedalloc/intern/mmap_win.c
intern/string/SConscript
source/blender/CMakeLists.txt
source/blender/blenkernel/BKE_DerivedMesh.h
source/blender/blenkernel/BKE_blender.h
source/blender/blenkernel/BKE_bmesh.h
source/blender/blenkernel/BKE_cdderivedmesh.h
source/blender/blenkernel/BKE_context.h
source/blender/blenkernel/BKE_customdata.h
source/blender/blenkernel/BKE_idcode.h
source/blender/blenkernel/BKE_key.h
source/blender/blenkernel/BKE_mesh.h
source/blender/blenkernel/BKE_modifier.h
source/blender/blenkernel/BKE_paint.h
source/blender/blenkernel/BKE_subsurf.h
source/blender/blenkernel/BKE_utildefines.h
source/blender/blenkernel/CMakeLists.txt
source/blender/blenkernel/SConscript
source/blender/blenkernel/intern/BME_Customdata.c
source/blender/blenkernel/intern/BME_conversions.c
source/blender/blenkernel/intern/BME_eulers.c
source/blender/blenkernel/intern/BME_mesh.c
source/blender/blenkernel/intern/BME_structure.c
source/blender/blenkernel/intern/BME_tools.c
source/blender/blenkernel/intern/DerivedMesh.c
source/blender/blenkernel/intern/anim.c
source/blender/blenkernel/intern/armature.c
source/blender/blenkernel/intern/blender.c
source/blender/blenkernel/intern/bvhutils.c
source/blender/blenkernel/intern/cdderivedmesh.c
source/blender/blenkernel/intern/cloth.c
source/blender/blenkernel/intern/constraint.c
source/blender/blenkernel/intern/context.c
source/blender/blenkernel/intern/curve.c
source/blender/blenkernel/intern/customdata.c
source/blender/blenkernel/intern/deform.c
source/blender/blenkernel/intern/displist.c
source/blender/blenkernel/intern/editderivedbmesh.c
source/blender/blenkernel/intern/effect.c
source/blender/blenkernel/intern/exotic.c
source/blender/blenkernel/intern/fluidsim.c
source/blender/blenkernel/intern/fmodifier.c
source/blender/blenkernel/intern/font.c
source/blender/blenkernel/intern/idcode.c
source/blender/blenkernel/intern/key.c
source/blender/blenkernel/intern/lattice.c
source/blender/blenkernel/intern/library.c
source/blender/blenkernel/intern/mesh.c
source/blender/blenkernel/intern/mesh_validate.c
source/blender/blenkernel/intern/multires.c
source/blender/blenkernel/intern/node.c
source/blender/blenkernel/intern/object.c
source/blender/blenkernel/intern/particle.c
source/blender/blenkernel/intern/particle_system.c
source/blender/blenkernel/intern/sca.c
source/blender/blenkernel/intern/seqcache.c
source/blender/blenkernel/intern/shrinkwrap.c
source/blender/blenkernel/intern/smoke.c
source/blender/blenkernel/intern/softbody.c
source/blender/blenkernel/intern/sound.c
source/blender/blenkernel/intern/subsurf_ccg.c
source/blender/blenkernel/nla_private.h
source/blender/blenlib/BLI_cellalloc.h
source/blender/blenlib/BLI_edgehash.h
source/blender/blenlib/BLI_editVert.h
source/blender/blenlib/BLI_ghash.h
source/blender/blenlib/BLI_math_color.h
source/blender/blenlib/BLI_math_vector.h
source/blender/blenlib/BLI_mempool.h
source/blender/blenlib/BLI_smallhash.h
source/blender/blenlib/CMakeLists.txt
source/blender/blenlib/SConscript
source/blender/blenlib/intern/BLI_cellalloc.c
source/blender/blenlib/intern/BLI_ghash.c
source/blender/blenlib/intern/BLI_mempool.c
source/blender/blenlib/intern/edgehash.c
source/blender/blenlib/intern/math_geom.c
source/blender/blenlib/intern/math_vector_inline.c
source/blender/blenlib/intern/pbvh.c
source/blender/blenlib/intern/rct.c
source/blender/blenlib/intern/scanfill.c
source/blender/blenloader/SConscript
source/blender/blenloader/intern/readfile.c
source/blender/blenloader/intern/writefile.c
source/blender/blenpluginapi/SConscript
source/blender/bmesh/CMakeLists.txt
source/blender/bmesh/bmesh_class.h
source/blender/bmesh/bmesh_error.h
source/blender/bmesh/bmesh_iterators.h
source/blender/bmesh/bmesh_operator_api.h
source/blender/bmesh/intern/bmesh_construct.c
source/blender/bmesh/intern/bmesh_mesh.c
source/blender/bmesh/intern/bmesh_mods.c
source/blender/bmesh/intern/bmesh_newcore.c
source/blender/bmesh/intern/bmesh_operators.c
source/blender/bmesh/intern/bmesh_polygon.c
source/blender/bmesh/intern/bmesh_queries.c
source/blender/bmesh/operators/createops.c
source/blender/bmesh/operators/edgesplitop.c
source/blender/bmesh/operators/extrudeops.c
source/blender/bmesh/operators/mesh_conv.c
source/blender/bmesh/operators/mirror.c
source/blender/bmesh/operators/removedoubles.c
source/blender/bmesh/operators/subdivideop.c
source/blender/bmesh/operators/utils.c
source/blender/collada/CMakeLists.txt
source/blender/collada/DocumentExporter.cpp
source/blender/editors/animation/SConscript
source/blender/editors/armature/SConscript
source/blender/editors/armature/editarmature.c
source/blender/editors/armature/meshlaplacian.c
source/blender/editors/armature/poseobject.c
source/blender/editors/armature/reeb.c
source/blender/editors/curve/SConscript
source/blender/editors/curve/editcurve.c
source/blender/editors/curve/editfont.c
source/blender/editors/datafiles/SConscript
source/blender/editors/gpencil/SConscript
source/blender/editors/gpencil/gpencil_paint.c
source/blender/editors/include/ED_mesh.h
source/blender/editors/include/ED_util.h
source/blender/editors/include/ED_uvedit.h
source/blender/editors/include/ED_view3d.h
source/blender/editors/include/UI_interface.h
source/blender/editors/include/UI_resources.h
source/blender/editors/interface/SConscript
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/resources.c
source/blender/editors/mesh/CMakeLists.txt
source/blender/editors/mesh/SConscript
source/blender/editors/mesh/bmesh_select.c
source/blender/editors/mesh/bmesh_tools.c
source/blender/editors/mesh/bmeshutils.c
source/blender/editors/mesh/editbmesh_add.c
source/blender/editors/mesh/editbmesh_bvh.c
source/blender/editors/mesh/editface.c
source/blender/editors/mesh/knifetool.c
source/blender/editors/mesh/loopcut.c
source/blender/editors/mesh/mesh_data.c
source/blender/editors/mesh/mesh_intern.h
source/blender/editors/mesh/mesh_ops.c
source/blender/editors/mesh/meshtools.c
source/blender/editors/metaball/mball_edit.c
source/blender/editors/object/CMakeLists.txt
source/blender/editors/object/SConscript
source/blender/editors/object/object_add.c
source/blender/editors/object/object_constraint.c
source/blender/editors/object/object_edit.c
source/blender/editors/object/object_hook.c
source/blender/editors/object/object_lattice.c
source/blender/editors/object/object_modifier.c
source/blender/editors/object/object_relations.c
source/blender/editors/object/object_shapekey.c
source/blender/editors/object/object_transform.c
source/blender/editors/object/object_vgroup.c
source/blender/editors/physics/SConscript
source/blender/editors/physics/particle_edit.c
source/blender/editors/physics/particle_object.c
source/blender/editors/render/CMakeLists.txt
source/blender/editors/render/render_shading.c
source/blender/editors/screen/CMakeLists.txt
source/blender/editors/screen/SConscript
source/blender/editors/screen/screen_edit.c
source/blender/editors/screen/screen_ops.c
source/blender/editors/sculpt_paint/SConscript
source/blender/editors/sculpt_paint/paint_image.c
source/blender/editors/sculpt_paint/paint_utils.c
source/blender/editors/sculpt_paint/paint_vertex.c
source/blender/editors/sculpt_paint/sculpt.c
source/blender/editors/space_api/CMakeLists.txt
source/blender/editors/space_api/SConscript
source/blender/editors/space_api/spacetypes.c
source/blender/editors/space_buttons/SConscript
source/blender/editors/space_image/CMakeLists.txt
source/blender/editors/space_image/SConscript
source/blender/editors/space_image/image_buttons.c
source/blender/editors/space_image/image_draw.c
source/blender/editors/space_image/image_header.c
source/blender/editors/space_image/image_ops.c
source/blender/editors/space_image/space_image.c
source/blender/editors/space_info/CMakeLists.txt
source/blender/editors/space_info/SConscript
source/blender/editors/space_info/info_stats.c
source/blender/editors/space_logic/logic_buttons.c
source/blender/editors/space_node/SConscript
source/blender/editors/space_outliner/outliner.c
source/blender/editors/space_view3d/CMakeLists.txt
source/blender/editors/space_view3d/SConscript
source/blender/editors/space_view3d/drawarmature.c
source/blender/editors/space_view3d/drawmesh.c
source/blender/editors/space_view3d/drawobject.c
source/blender/editors/space_view3d/space_view3d.c
source/blender/editors/space_view3d/view3d_buttons.c
source/blender/editors/space_view3d/view3d_edit.c
source/blender/editors/space_view3d/view3d_header.c
source/blender/editors/space_view3d/view3d_intern.h
source/blender/editors/space_view3d/view3d_ops.c
source/blender/editors/space_view3d/view3d_select.c
source/blender/editors/space_view3d/view3d_snap.c
source/blender/editors/space_view3d/view3d_toolbar.c
source/blender/editors/space_view3d/view3d_view.c
source/blender/editors/transform/CMakeLists.txt
source/blender/editors/transform/SConscript
source/blender/editors/transform/transform.c
source/blender/editors/transform/transform.h
source/blender/editors/transform/transform_constraints.c
source/blender/editors/transform/transform_conversions.c
source/blender/editors/transform/transform_generics.c
source/blender/editors/transform/transform_manipulator.c
source/blender/editors/transform/transform_orientations.c
source/blender/editors/transform/transform_snap.c
source/blender/editors/util/CMakeLists.txt
source/blender/editors/util/SConscript
source/blender/editors/util/crazyspace.c
source/blender/editors/util/ed_util.c
source/blender/editors/util/editmode_undo.c
source/blender/editors/uvedit/CMakeLists.txt
source/blender/editors/uvedit/SConscript
source/blender/editors/uvedit/uvedit_draw.c
source/blender/editors/uvedit/uvedit_intern.h
source/blender/editors/uvedit/uvedit_ops.c
source/blender/editors/uvedit/uvedit_parametrizer.c
source/blender/editors/uvedit/uvedit_parametrizer.h
source/blender/editors/uvedit/uvedit_unwrap_ops.c
source/blender/gpu/GPU_material.h
source/blender/gpu/intern/gpu_buffers.c
source/blender/gpu/intern/gpu_draw.c
source/blender/gpu/intern/gpu_material.c
source/blender/gpu/intern/gpu_shader_material.glsl
source/blender/ikplugin/intern/itasc_plugin.cpp
source/blender/makesdna/DNA_color_types.h
source/blender/makesdna/DNA_customdata_types.h
source/blender/makesdna/DNA_key_types.h
source/blender/makesdna/DNA_mesh_types.h
source/blender/makesdna/DNA_meshdata_types.h
source/blender/makesdna/DNA_object_types.h
source/blender/makesdna/DNA_userdef_types.h
source/blender/makesdna/DNA_windowmanager_types.h
source/blender/makesdna/intern/SConscript
source/blender/makesdna/intern/makesdna.c
source/blender/makesrna/SConscript
source/blender/makesrna/intern/CMakeLists.txt
source/blender/makesrna/intern/SConscript
source/blender/makesrna/intern/makesrna.c
source/blender/makesrna/intern/rna_animviz.c
source/blender/makesrna/intern/rna_constraint.c
source/blender/makesrna/intern/rna_fcurve.c
source/blender/makesrna/intern/rna_image_api.c
source/blender/makesrna/intern/rna_main_api.c
source/blender/makesrna/intern/rna_mesh.c
source/blender/makesrna/intern/rna_mesh_api.c
source/blender/makesrna/intern/rna_modifier.c
source/blender/makesrna/intern/rna_nla.c
source/blender/makesrna/intern/rna_object.c
source/blender/makesrna/intern/rna_pose_api.c
source/blender/makesrna/intern/rna_scene.c
source/blender/makesrna/intern/rna_userdef.c
source/blender/makesrna/intern/rna_wm_api.c
source/blender/makesrna/rna_cleanup/rna_properties.txt
source/blender/makesrna/rna_cleanup/rna_update.sh
source/blender/modifiers/CMakeLists.txt
source/blender/modifiers/SConscript
source/blender/modifiers/intern/MOD_array.c
source/blender/modifiers/intern/MOD_bevel.c
source/blender/modifiers/intern/MOD_boolean_util.c
source/blender/modifiers/intern/MOD_build.c
source/blender/modifiers/intern/MOD_collision.c
source/blender/modifiers/intern/MOD_decimate.c
source/blender/modifiers/intern/MOD_displace.c
source/blender/modifiers/intern/MOD_edgesplit.c
source/blender/modifiers/intern/MOD_explode.c
source/blender/modifiers/intern/MOD_fluidsim_util.c
source/blender/modifiers/intern/MOD_mask.c
source/blender/modifiers/intern/MOD_meshdeform.c
source/blender/modifiers/intern/MOD_mirror.c
source/blender/modifiers/intern/MOD_multires.c
source/blender/modifiers/intern/MOD_particleinstance.c
source/blender/modifiers/intern/MOD_particlesystem.c
source/blender/modifiers/intern/MOD_screw.c
source/blender/modifiers/intern/MOD_solidify.c
source/blender/modifiers/intern/MOD_subsurf.c
source/blender/modifiers/intern/MOD_surface.c
source/blender/modifiers/intern/MOD_util.c
source/blender/modifiers/intern/MOD_uvproject.c
source/blender/modifiers/intern/MOD_wave.c
source/blender/nodes/intern/SHD_nodes/SHD_output.c
source/blender/python/generic/bpy_internal_import.h
source/blender/python/intern/bpy.c
source/blender/python/intern/bpy_interface.c
source/blender/python/intern/bpy_operator_wrap.c
source/blender/python/intern/bpy_rna.c
source/blender/readblenfile/intern/BLO_readblenfile.c
source/blender/render/SConscript
source/blender/render/intern/include/raycounter.h
source/blender/render/intern/include/rayobject.h
source/blender/render/intern/raytrace/reorganize.h
source/blender/render/intern/source/convertblender.c
source/blender/render/intern/source/pipeline.c
source/blender/render/intern/source/pointdensity.c
source/blender/render/intern/source/shadeoutput.c
source/blender/render/intern/source/strand.c
source/blender/windowmanager/CMakeLists.txt
source/blender/windowmanager/SConscript
source/blender/windowmanager/WM_types.h
source/blender/windowmanager/intern/wm_event_system.c
source/blender/windowmanager/intern/wm_init_exit.c
source/blender/windowmanager/intern/wm_operators.c
source/blender/windowmanager/intern/wm_window.c
source/blenderplayer/CMakeLists.txt
source/creator/CMakeLists.txt
source/creator/creator.c
source/gameengine/BlenderRoutines/SConscript
source/gameengine/Converter/BL_BlenderDataConversion.cpp
source/gameengine/Converter/SConscript
source/gameengine/Expressions/SConscript
source/gameengine/GameLogic/SConscript
source/gameengine/GamePlayer/common/SConscript
source/gameengine/GamePlayer/ghost/SConscript
source/gameengine/Ketsji/KXNetwork/SConscript
source/gameengine/Ketsji/KX_PythonInit.cpp
source/gameengine/Ketsji/SConscript
source/gameengine/Physics/Bullet/CcdPhysicsController.cpp
source/gameengine/Physics/Bullet/SConscript
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
source/gameengine/Rasterizer/SConscript
source/gameengine/VideoTexture/SConscript

diff --cc SConstruct
@@@ -408,10 -413,11 +413,18 @@@ thesyslibs = B.setup_syslibs(env
  if 'blender' in B.targets or not env['WITH_BF_NOBLENDER']:
      env.BlenderProg(B.root_build_dir, "blender", mainlist + thestatlibs + dobj, thesyslibs, [B.root_build_dir+'/lib'] + thelibincs, 'blender')
  if env['WITH_BF_PLAYER']:
++<<<<<<< .working
 +      playerlist = B.create_blender_liblist(env, 'player')
 +      playerlist += B.create_blender_liblist(env, 'intern')
 +      playerlist += B.create_blender_liblist(env, 'extern')
 +      env.BlenderProg(B.root_build_dir, "blenderplayer",  playerlist, thestatlibs + dobj + thesyslibs, [B.root_build_dir+'/lib'] + thelibincs, 'blenderplayer')
++=======
+     playerlist = B.create_blender_liblist(env, 'player')
+     playerlist += B.create_blender_liblist(env, 'player2')
+     playerlist += B.create_blender_liblist(env, 'intern')
+     playerlist += B.create_blender_liblist(env, 'extern')
+     env.BlenderProg(B.root_build_dir, "blenderplayer",  playerlist + thestatlibs + dobj, thesyslibs, [B.root_build_dir+'/lib'] + thelibincs, 'blenderplayer')
++>>>>>>> .merge-right.r35190
  
  ##### Now define some targets
  
@@@ -572,66 -583,66 +590,74 @@@ for tp, tn, tf in os.walk('release/text
  textinstall = env.Install(dir=env['BF_INSTALLDIR'], source=textlist)
  
  if  env['OURPLATFORM']=='darwin':
-               allinstall = [blenderinstall, plugininstall, textinstall]
+         allinstall = [blenderinstall, plugininstall, textinstall]
  elif env['OURPLATFORM']=='linux2':
-               allinstall = [blenderinstall, dotblenderinstall, scriptinstall, plugininstall, textinstall, iconinstall]
+         allinstall = [blenderinstall, dotblenderinstall, scriptinstall, plugininstall, textinstall, iconinstall]
  else:
-               allinstall = [blenderinstall, dotblenderinstall, scriptinstall, plugininstall, textinstall]
+         allinstall = [blenderinstall, dotblenderinstall, scriptinstall, plugininstall, textinstall]
  
  if env['OURPLATFORM'] in ('win32-vc', 'win32-mingw', 'win64-vc', 'linuxcross'):
-       dllsources = []
+     dllsources = []
  
-       if not env['OURPLATFORM'] in ('win32-mingw', 'win64-vc', 'linuxcross'):
-               # For MinGW and linuxcross static linking will be used
-               dllsources += ['${LCGDIR}/gettext/lib/gnu_gettext.dll']
+     if not env['OURPLATFORM'] in ('win32-mingw', 'win64-vc', 'linuxcross'):
+         # For MinGW and linuxcross static linking will be used
+         dllsources += ['${LCGDIR}/gettext/lib/gnu_gettext.dll']
  
-       #currently win64-vc doesn't appear to have libpng.dll
-       if env['OURPLATFORM'] != 'win64-vc':
-               dllsources += ['${BF_PNG_LIBPATH}/libpng.dll']
+     #currently win64-vc doesn't appear to have libpng.dll
+     if env['OURPLATFORM'] != 'win64-vc':
+         dllsources += ['${BF_PNG_LIBPATH}/libpng.dll']
  
-       dllsources += ['${BF_ZLIB_LIBPATH}/zlib.dll']
-       # Used when linking to libtiff was dynamic
-       # keep it here until compilation on all platform would be ok
-       # dllsources += ['${BF_TIFF_LIBPATH}/${BF_TIFF_LIB}.dll']
+     dllsources += ['${BF_ZLIB_LIBPATH}/zlib.dll']
+     # Used when linking to libtiff was dynamic
+     # keep it here until compilation on all platform would be ok
+     # dllsources += ['${BF_TIFF_LIBPATH}/${BF_TIFF_LIB}.dll']
  
-       if env['OURPLATFORM'] != 'linuxcross':
-               # pthreads library is already added
-               dllsources += ['${BF_PTHREADS_LIBPATH}/${BF_PTHREADS_LIB}.dll']
+     if env['OURPLATFORM'] != 'linuxcross':
+         # pthreads library is already added
+         dllsources += ['${BF_PTHREADS_LIBPATH}/${BF_PTHREADS_LIB}.dll']
  
-       if env['WITH_BF_SDL']:
-               if env['OURPLATFORM'] == 'win64-vc':
-                       pass # we link statically already to SDL on win64
-               else:
-                       dllsources.append('${BF_SDL_LIBPATH}/SDL.dll')
+     if env['WITH_BF_SDL']:
+         if env['OURPLATFORM'] == 'win64-vc':
+             pass # we link statically already to SDL on win64
+         else:
+             dllsources.append('${BF_SDL_LIBPATH}/SDL.dll')
  
-       if env['WITH_BF_ICONV']:
-               if env['OURPLATFORM'] == 'win64-vc':
-                       pass # we link statically to iconv on win64
-               elif not env['OURPLATFORM'] in ('win32-mingw', 'linuxcross'):
-                       #gettext for MinGW and cross-compilation is compiled staticly
-                       dllsources += ['${BF_ICONV_LIBPATH}/iconv.dll']
-       if env['WITH_BF_OPENAL']:
-               dllsources.append('${LCGDIR}/openal/lib/OpenAL32.dll')
-               dllsources.append('${LCGDIR}/openal/lib/wrap_oal.dll')
-       if env['WITH_BF_SNDFILE']:
-               dllsources.append('${LCGDIR}/sndfile/lib/libsndfile-1.dll')
-       if env['WITH_BF_FFMPEG']:
-               dllsources += ['${BF_FFMPEG_LIBPATH}/avcodec-52.dll',
-                                       '${BF_FFMPEG_LIBPATH}/avformat-52.dll',
-                                       '${BF_FFMPEG_LIBPATH}/avdevice-52.dll',
-                                       '${BF_FFMPEG_LIBPATH}/avutil-50.dll',
-                                       '${BF_FFMPEG_LIBPATH}/swscale-0.dll']
-       windlls = env.Install(dir=env['BF_INSTALLDIR'], source = dllsources)
-       allinstall += windlls
++<<<<<<< .working
 +      if env['WITH_BF_PYTHON']:
 +              if env['BF_DEBUG'] and not env["BF_NO_PYDEBUG"]:
 +                      dllsources.append('${BF_PYTHON_LIBPATH}/${BF_PYTHON_DLL}_d.dll')
 +              else:
 +                      dllsources.append('${BF_PYTHON_LIBPATH}/${BF_PYTHON_DLL}.dll')
++=======
+     if env['WITH_BF_PYTHON']:
+         if env['BF_DEBUG']:
+             dllsources.append('${BF_PYTHON_LIBPATH}/${BF_PYTHON_DLL}_d.dll')
+         else:
+             dllsources.append('${BF_PYTHON_LIBPATH}/${BF_PYTHON_DLL}.dll')
++>>>>>>> .merge-right.r35190
+     if env['WITH_BF_ICONV']:
+         if env['OURPLATFORM'] == 'win64-vc':
+             pass # we link statically to iconv on win64
+         elif not env['OURPLATFORM'] in ('win32-mingw', 'linuxcross'):
+             #gettext for MinGW and cross-compilation is compiled staticly
+             dllsources += ['${BF_ICONV_LIBPATH}/iconv.dll']
+     if env['WITH_BF_OPENAL']:
+         dllsources.append('${LCGDIR}/openal/lib/OpenAL32.dll')
+         dllsources.append('${LCGDIR}/openal/lib/wrap_oal.dll')
+     if env['WITH_BF_SNDFILE']:
+         dllsources.append('${LCGDIR}/sndfile/lib/libsndfile-1.dll')
+     if env['WITH_BF_FFMPEG']:
+         dllsources += ['${BF_FFMPEG_LIBPATH}/avcodec-52.dll',
+                     '${BF_FFMPEG_LIBPATH}/avformat-52.dll',
+                     '${BF_FFMPEG_LIBPATH}/avdevice-52.dll',
+                     '${BF_FFMPEG_LIBPATH}/avutil-50.dll',
+                     '${BF_FFMPEG_LIBPATH}/swscale-0.dll']
+     windlls = env.Install(dir=env['BF_INSTALLDIR'], source = dllsources)
+     allinstall += windlls
  
  installtarget = env.Alias('install', allinstall)
  bininstalltarget = env.Alias('install-bin', blenderinstall)
  #
  # ***** END GPL LICENSE BLOCK *****
  
- SET(INC . src)
+ set(INC
+       .
+ )
  
++<<<<<<< .working
 +FILE(GLOB SRC *.c except t1_generate_luts.c)
 +ADD_DEFINITIONS(-DWITH_OPENJPEG)
 +BLENDERLIB(extern_openjpeg "${SRC}" "${INC}")
 +#, libtype=['international','player'], priority=[5, 210])
++=======
+ set(SRC
+       bio.c
+       cio.c
+       dwt.c
+       event.c
+       image.c
+       j2k.c
+       j2k_lib.c
+       jp2.c
+       jpt.c
+       mct.c
+       mqc.c
+       openjpeg.c
+       pi.c
+       raw.c
+       t1.c
+       t2.c
+       tcd.c
+       tgt.c
+       bio.h
+       cio.h
+       dwt.h
+       event.h
+       fix.h
+       image.h
+       int.h
+       j2k.h
+       j2k_lib.h
+       jp2.h
+       jpt.h
+       mct.h
+       mqc.h
+       openjpeg.h
+       opj_includes.h
+       opj_malloc.h
+       pi.h
+       raw.h
+       t1.h
+       t1_luts.h
+       t2.h
+       tcd.h
+       tgt.h
+ )
+ blender_add_lib(extern_openjpeg "${SRC}" "${INC}")
++>>>>>>> .merge-right.r35190
@@@ -5,4 -5,4 +5,8 @@@ sources = env.Glob('intern/*.cpp'
  
  incs = '. ../moto/include ../container ../memutil'
  
++<<<<<<< .working
 +env.BlenderLib ('bf_intern_decimate', sources, Split(incs) , [], libtype=['core'], priority = [200] )
++=======
+ env.BlenderLib ('bf_intern_decimate', sources, Split(incs) , [], libtype=['core', 'player'], priority = [200, 100] )
++>>>>>>> .merge-right.r35190
Simple merge
@@@ -90,17 -84,12 +91,23 @@@ public
         * @return The event data.
         */
        virtual GHOST_TEventDataPtr getData() = 0;
 +
 +      virtual int serialize(char buf[256])
 +      {
 +              return 0;
 +      }
 +
 +      int writeheader(char buf[256])
 +      {
 +              sprintf(buf, "%lf %d %d", ((double)this->getTime())*0.001, this->getType(), this->getWindow()->getID());
 +              return 0;
 +      }
+       
+ #ifdef WITH_CXX_GUARDEDALLOC
+ public:
+       void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GHOST:GHOST_IEvent"); }
+       void operator delete( void *mem ) { MEM_freeN(mem); }
+ #endif
  };
  
  #endif // _GHOST_IEVENT_H_
Simple merge
@@@ -316,8 -306,11 +317,13 @@@ public
         */
        virtual GHOST_TSuccess setCursorGrab(GHOST_TGrabCursorMode mode, GHOST_Rect *bounds) { return GHOST_kSuccess; };
  
 +private:
 +      int m_id;
+ #ifdef WITH_CXX_GUARDEDALLOC
+ public:
+       void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GHOST:GHOST_IWindow"); }
+       void operator delete( void *mem ) { MEM_freeN(mem); }
+ #endif
  };
  
  #endif // _GHOST_IWINDOW_H_
Simple merge
@@@ -1,5 -1,5 +1,5 @@@
- /**
+ /*
 - * $Id$
 + * $Id: GHOST_DisplayManagerCocoa.h 26841 2010-02-12 13:34:04Z campbellbarton $
   * ***** BEGIN GPL LICENSE BLOCK *****
   *
   * This program is free software; you can redistribute it and/or
Simple merge
Simple merge
@@@ -133,39 -100,6 +138,38 @@@ GHOST_TSuccess GHOST_EventManager::push
        if (m_events.size() < m_events.max_size()) {
                m_events.push_front(event);
                success = GHOST_kSuccess;
-                               (int)keys.get(GHOST_kModifierKeyRightControl), 
-                               (int)keys.get(GHOST_kModifierKeyCommand));
 +              if (m_recfile) {
 +                      GHOST_System *sys;
 +                      GHOST_ModifierKeys keys;
 +                      GHOST_TInt32 x, y;
 +                      char buf[256];
 +
 +                      sys = reinterpret_cast<GHOST_System*>(GHOST_ISystem::getSystem());
 +                      
 +                      /*write event parent class data*/
 +                      event->writeheader(buf);
 +                      fprintf(m_recfile, "%s\n", buf);
 +                      
 +                      /*write child class data*/
 +                      event->serialize(buf);
 +                      fprintf(m_recfile, "%s\n", buf);
 +                      
 +                      /*write modifier key states*/
 +                      sys->getModifierKeys(keys);
 +                      fprintf(m_recfile, "lshift: %d rshift: %d lalt: %d ralt: %d lctrl: %d rctrl: %d command: %d\n", 
 +                              (int)keys.get(GHOST_kModifierKeyLeftShift),
 +                              (int)keys.get(GHOST_kModifierKeyRightShift), 
 +                              (int)keys.get(GHOST_kModifierKeyLeftAlt), 
 +                              (int)keys.get(GHOST_kModifierKeyRightAlt), 
 +                              (int)keys.get(GHOST_kModifierKeyLeftControl), 
++                              (int)keys.get(GHOST_kModifierKeyRightControl));
 +                      fflush(m_recfile);
 +                      
 +                      sys->getCursorPosition(x, y);
 +
 +                      /*write mouse cursor state*/
 +                      fprintf(m_recfile, "mcursorstate: %d %d\n", x, y);
 +              }
        }
        else {
                success = GHOST_kFailure;
@@@ -222,200 -141,18 +226,199 @@@ bool GHOST_EventManager::dispatchEvent(
  
  bool GHOST_EventManager::dispatchEvents()
  {
 -      bool handled;
 -      if (getNumEvents()) {
 -              handled = true;
 -              while (getNumEvents()) {
 -                      if (!dispatchEvent()) {
 -                              handled = false;
 +      bool handled = false;
 +      
 +      if (m_recfile && getNumEvents()) {
 +              fprintf(m_recfile, "break\n");
 +      }
 +      
 +      if (m_playfile) {
 +              GHOST_IEvent *event = NULL;
 +              GHOST_System *sys;
 +              GHOST_WindowManager *wm;
 +              GHOST_TInt32 x, y;
 +              GHOST_ModifierKeys modkeys;
 +              std::vector<GHOST_IWindow *> windows;
 +              double lasttime = -1.0;
 +              char buf[256], *str;
 +                              
 +              sys = reinterpret_cast<GHOST_System*>(GHOST_ISystem::getSystem());
 +              wm = sys->getWindowManager();
 +              windows = wm->getWindows();
 +
 +              while (str = fgets(buf, 256, m_playfile)) {
 +                      GHOST_IWindow *iwin = NULL;
 +                      GHOST_TEventType type;
 +                      double time;
 +                      int winid, i;
 +                      int ctype;
 +                      
 +                      event = NULL;
 +                      
 +                      if (strcmp(str, "break\n")==0) {
 +                              event = new GHOST_Event(0, GHOST_kEventUnknown, NULL);
 +                              pushEvent(event);
 +                              continue;
 +                      }
 +                      
 +                      sscanf(str, "%lf %d %d", &time, &ctype, &winid);
 +                      type = (GHOST_TEventType)(ctype);
 +                      
 +                      if (lasttime > 0.0) {
 +                              double t = time;
 +                              
 +                              time -= lasttime;
 +                              lasttime = t;
 +                      } else lasttime = time;
 +                      
 +                      for (i=0; i<windows.size(); i++) {
 +                              if (windows[i]->getID() == winid)
 +                                      break;
 +                      }
 +                      
 +                      if (i == windows.size()) {
 +                              printf("Eek! Could not find window %d!\n", winid);
 +                              str = fgets(buf, 256, m_playfile);
 +                              continue;
 +                      }
 +                      
 +                      iwin = windows[i];
 +                      
 +                      str = fgets(buf, 256, m_playfile);
 +                      if (!str)
 +                              break;
 +                              
 +                      switch (type) {
 +                              case GHOST_kEventCursorMove:
 +                                      event = new GHOST_EventCursor(time*1000, type, iwin, str);
 +                                      break;
 +                              case GHOST_kEventButtonDown:
 +                                      event = new GHOST_EventButton(time*1000, type, iwin, str);
 +                                      break;
 +                              case GHOST_kEventButtonUp:
 +                                      event = new GHOST_EventButton(time*1000, type, iwin, str);
 +                                      break;
 +                              case GHOST_kEventWheel:
 +                                      event = new GHOST_EventWheel(time*1000, type, iwin, str);
 +                                      break;
 +                              case GHOST_kEventTrackpad:
 +                                      event = new GHOST_EventTrackpad(time*1000, type, iwin, str);
 +                                      break;
 +                      
 +                              case GHOST_kEventNDOFMotion:
 +                                      break;
 +                              case GHOST_kEventNDOFButton:
 +                                      break;
 +                      
 +                              case GHOST_kEventKeyDown:
 +                                      event = new GHOST_EventKey(time*1000, type, iwin, str);
 +                                      break;
 +                              case GHOST_kEventKeyUp:
 +                                      event = new GHOST_EventKey(time*1000, type, iwin, str);
 +                                      break;
 +                      //      case GHOST_kEventKeyAuto:
 +                      
 +                              case GHOST_kEventQuit:
 +                                      break;
 +                      
 +                              case GHOST_kEventWindowClose:
 +                                      break;
 +                              case GHOST_kEventWindowActivate:
 +                                      break;
 +                              case GHOST_kEventWindowDeactivate:
 +                                      break;
 +                              case GHOST_kEventWindowUpdate:
 +                                      break;
 +                              case GHOST_kEventWindowSize:
 +                                      break;
 +                              case GHOST_kEventWindowMove:
 +                                      break;
 +                              
 +                              case GHOST_kEventDraggingEntered:
 +                                      break;
 +                              case GHOST_kEventDraggingUpdated:
 +                                      break;
 +                              case GHOST_kEventDraggingExited:
 +                                      break;
 +                              case GHOST_kEventDraggingDropDone:
 +                                      break;
 +                              
 +                              case GHOST_kEventOpenMainFile:
 +                                      break;
 +                      
 +                              case GHOST_kEventTimer:
 +                                      break;                          
 +                      }
 +                      
 +                      str = fgets(buf, 256, m_playfile);
 +                      if (str) {
 +                              int lshift, rshift, lalt, ralt, lctrl, rctrl, command;
 +                              sscanf(str, "lshift: %d rshift: %d lalt: %d ralt: %d lctrl: %d rctrl: %d command: %d",
 +                                          &lshift, &rshift, &lalt, &ralt, &lctrl, &rctrl, &command);
 +                              modkeys.set(GHOST_kModifierKeyLeftShift, lshift);
 +                              modkeys.set(GHOST_kModifierKeyRightShift, rshift);
 +                              modkeys.set(GHOST_kModifierKeyLeftAlt, lalt);
 +                              modkeys.set(GHOST_kModifierKeyRightAlt, ralt);
 +                              modkeys.set(GHOST_kModifierKeyLeftControl, lctrl);
 +                              modkeys.set(GHOST_kModifierKeyRightControl, rctrl);
-                               modkeys.set(GHOST_kModifierKeyCommand, command);
 +                      }           
 +                      
 +                      str = fgets(buf, 256, m_playfile);
 +                      if (str) {
 +                              /*read mouse cursor state*/
 +                              sscanf(str, "mcursorstate: %d %d", &x, &y);
 +                      }
 +                      
 +                      if (event) {
 +                              event->setPlaybackCursor(x, y);
 +                              event->setPlaybackModifierKeys(modkeys);
 +                              pushEvent(event);
                        }
                }
 +              
 +              if (getNumEvents()) {
 +                      handled = true;
 +                      while (getNumEvents()) {
 +                              event = m_events[m_events.size()-1];
 +                              //event->geTime() stores delay between last event and this one
 +                              if (event->getType() == 0 || sys->getMilliSeconds()-m_lasttime < event->getTime()) {
 +                                      handled = false;
 +                                      
 +                                      if (event->getType() == 0) 
 +                                              popEvent();             
 +                                      break;
 +                              }
 +                              
 +                              //change event->time from delay-since-last-event to 
 +                              //current system timevoid
 +                              m_lasttime = sys->getMilliSeconds();
 +                              event->setTime(m_lasttime);
 +                              
 +                              event->getPlaybackModifierKeys(m_playmods);
 +                              event->getPlaybackCursor(m_x, m_y);
 +                              
 +                              if (!dispatchEvent()) {
 +                                      handled = false;
 +                              }
 +                      }
 +              } else {
 +                      handled = false;
 +                      m_playfile = NULL;
 +              }
 +      } else {        
 +              if (getNumEvents()) {
 +                      handled = true;
 +                      while (getNumEvents()) {
 +                              if (!dispatchEvent()) {
 +                                      handled = false;
 +                              }
 +                      }
 +              }
 +              else {
 +                      handled = false;
 +              }
        }
 -      else {
 -              handled = false;
 -      }
 +      
        return handled;
  }
  
@@@ -1,5 -1,5 +1,5 @@@
- /**
+ /*
 - * $Id$
 + * $Id: GHOST_EventTrackpad.h 26841 2010-02-12 13:34:04Z campbellbarton $
   * ***** BEGIN GPL LICENSE BLOCK *****
   *
   * This program is free software; you can redistribute it and/or
Simple merge
Simple merge
@@@ -1,5 -1,5 +1,5 @@@
- /**
+ /*
 - * $Id$
 + * $Id: GHOST_SystemCocoa.h 30526 2010-07-20 10:41:08Z campbellbarton $
   * ***** BEGIN GPL LICENSE BLOCK *****
   *
   * This program is free software; you can redistribute it and/or
@@@ -1,5 -1,5 +1,5 @@@
- /**
+ /*
 - * $Id$
 + * $Id: GHOST_WindowCocoa.h 29143 2010-06-01 20:21:40Z damien78 $
   * ***** BEGIN GPL LICENSE BLOCK *****
   *
   * This program is free software; you can redistribute it and/or
@@@ -72,11 -85,7 +85,11 @@@ extern "C" 
         * Release memory previously allocatred by this module. 
         */
        short MEM_freeN(void *vmemh);
 -
 +      short WMEM_freeN(void *vmemh);
 +      
-       short _MEM_freeN(void *vmemh, char *file, int line);
++      short _MEM_freeN(void *vmemh, const char *file, int line);
 +      #define MEM_freeN(vmemh)        _MEM_freeN(vmemh, __FILE__, __LINE__)
 +      
  
        /**
         * Return zero if memory is not in allocated list
         * - MEM_get_memory_in_use is all memory
         * - MEM_get_mapped_memory_in_use is a subset of all memory */
        uintptr_t MEM_get_memory_in_use(void);
+       /** Get mapped memory usage. */
        uintptr_t MEM_get_mapped_memory_in_use(void);
+       /** Get amount of memory blocks in use. */
        int MEM_get_memory_blocks_in_use(void);
  
- /********* Internal structs.   They're only here for the MEM_OVERHEAD macro.*********/
+       /** Reset the peak memory statistic to zero. */
+       void MEM_reset_peak_memory(void);
  
- /*BMESH_TODO/XXX: note to self, don't merge this into trunk*/
+       /** Get the peak memory usage in bytes, including mmap allocations. */
+       uintptr_t MEM_get_peak_memory(void) WARN_UNUSED;
  
 +/* all memory chunks are put in linked lists */
 +typedef struct localLink
 +{
 +      struct localLink *next,*prev;
 +} localLink;
 +
 +typedef struct localListBase
 +{
 +      void *first, *last;
 +} localListBase;
 +
 +      /* note: keep this struct aligned (e.g., irix/gcc) - Hos */
 +typedef struct MemHead {
 +      int tag1;
 +      size_t len;
 +      struct MemHead *next,*prev;
 +      const char * name;
 +      const char * nextname;
 +      int tag2;
 +      int mmap;       /* if true, memory was mmapped */
 +#ifdef DEBUG_MEMCOUNTER
 +      int _count;
 +#endif
 +} MemHead;
 +
 +typedef struct MemTail {
 +      int tag3, tag4;
 +} MemTail;
 +
 +/*memory bias to hopefully account 
 +  for allocation overhead from
 +  the system allocator.*/
 +#define MEM_OVERHEADBIAS      32
 +
 +#define MEM_OVERHEAD  (sizeof(MemHead) + sizeof(MemTail) + MEM_OVERHEADBIAS)
 +
 +/*reset the peak memory statistic to zero*/
 +void MEM_reset_peak_memory(void);
 +
 +/*get the peak memory usage in bytes, including mmap allocations*/
 +uintptr_t MEM_get_peak_memory(void);
 +
+ #ifndef NDEBUG
+ const char *MEM_name_ptr(void *vmemh);
+ #endif
+       
  #ifdef __cplusplus
  }
  #endif
@@@ -2,7 -2,14 +2,18 @@@
  
  Import('env')
  
- sources = env.Glob('intern/*.c')
+ defs = []
+ sources = ['intern/mallocn.c', 'intern/mmap_win.c']
+ if env['WITH_BF_CXX_GUARDEDALLOC']:
+     sources.append('cpp/mallocn.cpp')
+     defs.append('WITH_CXX_GUARDEDALLOC')
  incs = '.'
  
++<<<<<<< .working
 +env.BlenderLib ('bf_intern_guardedalloc', sources, Split(incs), defines=[], libtype=['intern','player'], priority = [5,150] )
++=======
+ env.BlenderLib ('bf_intern_guardedalloc', sources, Split(incs), defs, libtype=['intern','player'], priority = [5,150] )
++>>>>>>> .merge-right.r35190
@@@ -525,22 -593,7 +548,22 @@@ void MEM_printmemlist_pydict( void ) 
        MEM_printmemlist_internal(1);
  }
  
 -short MEM_freeN(void *vmemh)          /* anders compileertie niet meer */
 +#ifdef MEM_freeN
 +#undef MEM_freeN
 +#endif
 +
 +short MEM_freeN(void *vmemh)
 +{
 +      return _MEM_freeN(vmemh, "(called through C stub function)", -1);
 +}
 +
 +short WMEM_freeN(void *vmemh)
 +{
 +      return _MEM_freeN(vmemh, "(called through C stub function)", -1);
 +}
 +
 +/*special macro-wrapped MEM_freeN that keeps track of where MEM_freeN is called.*/
- short _MEM_freeN(void *vmemh, char *file, int line)           /* anders compileertie niet meer */
++short _MEM_freeN(void *vmemh, const char *file, int line)             /* anders compileertie niet meer */
  {
        short error = 0;
        MemTail *memt;
@@@ -1,5 -1,5 +1,5 @@@
- /**
+ /*
 - * $Id$
 + * $Id$
   *
   * ***** BEGIN GPL LICENSE BLOCK *****
   *
@@@ -4,4 -4,10 +4,14 @@@ Import ('env'
  sources = env.Glob('intern/*.cpp')
  incs = '.'
  
++<<<<<<< .working
 +env.BlenderLib ('bf_intern_string', sources, Split(incs), [], libtype=['intern','player'], priority = [50,10] )
++=======
+ defs = []
+ if env['WITH_BF_CXX_GUARDEDALLOC']:
+     defs.append('WITH_CXX_GUARDEDALLOC')
+     incs += ' #intern/guardedalloc'
+ env.BlenderLib ('bf_intern_string', sources, Split(incs), defs, libtype=['intern','player'], priority = [50,10] )
++>>>>>>> .merge-right.r35190
  #
  # ***** END GPL LICENSE BLOCK *****
  
- ADD_SUBDIRECTORY(windowmanager)
- ADD_SUBDIRECTORY(editors)
- ADD_SUBDIRECTORY(avi)
- ADD_SUBDIRECTORY(nodes)
- ADD_SUBDIRECTORY(blenkernel)
- ADD_SUBDIRECTORY(modifiers)
- ADD_SUBDIRECTORY(blenlib)
- ADD_SUBDIRECTORY(bmesh)
- ADD_SUBDIRECTORY(blenloader)
- ADD_SUBDIRECTORY(blenpluginapi)
- ADD_SUBDIRECTORY(imbuf)
- ADD_SUBDIRECTORY(gpu)
- ADD_SUBDIRECTORY(makesdna)
- ADD_SUBDIRECTORY(makesrna)
- ADD_SUBDIRECTORY(readblenfile)
- ADD_SUBDIRECTORY(render)
- ADD_SUBDIRECTORY(blenfont)
- ADD_SUBDIRECTORY(ikplugin)
+ set(SRC_DNA_INC
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_ID.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_action_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_actuator_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_anim_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_armature_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_boid_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_brush_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_camera_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_cloth_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_color_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_constraint_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_controller_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_curve_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_customdata_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_documentation.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_effect_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_fileglobal_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_genfile.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_gpencil_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_group_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_image_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_ipo_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_key_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_lamp_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_lattice_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_listBase.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_material_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_mesh_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_meshdata_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_meta_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_modifier_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_nla_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_node_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_object_fluidsim.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_object_force.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_object_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_outliner_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_packedFile_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_particle_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_property_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_scene_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_screen_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_sdna_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_sensor_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_sequence_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_smoke_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_sound_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_space_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_text_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_texture_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_userdef_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_vec_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_vfont_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_view2d_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_view3d_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_windowmanager_types.h
+       ${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_world_types.h
+ )
  
- IF(WITH_IMAGE_OPENEXR)
-       ADD_SUBDIRECTORY(imbuf/intern/openexr)
- ENDIF(WITH_IMAGE_OPENEXR)
+ add_subdirectory(editors)
+ add_subdirectory(windowmanager)
+ add_subdirectory(blenkernel)
+ add_subdirectory(blenlib)
++add_subdirectory(bmesh)
+ add_subdirectory(render)
+ add_subdirectory(blenfont)
+ add_subdirectory(blenloader)
+ add_subdirectory(readblenfile)
+ add_subdirectory(blenpluginapi)
+ add_subdirectory(ikplugin)
+ add_subdirectory(gpu)
+ add_subdirectory(imbuf)
+ add_subdirectory(avi)
+ add_subdirectory(nodes)
+ add_subdirectory(modifiers)
+ add_subdirectory(makesdna)
+ add_subdirectory(makesrna)
  
IF(WITH_IMAGE_DDS)
-       ADD_SUBDIRECTORY(imbuf/intern/dds)
ENDIF(WITH_IMAGE_DDS)
if(WITH_IMAGE_OPENEXR)
+       add_subdirectory(imbuf/intern/openexr)
endif()
  
IF(WITH_IMAGE_CINEON)
-       ADD_SUBDIRECTORY(imbuf/intern/cineon)
ENDIF(WITH_IMAGE_CINEON)
if(WITH_IMAGE_DDS)
+       add_subdirectory(imbuf/intern/dds)
endif()
  
IF(WITH_QUICKTIME)
-       ADD_SUBDIRECTORY(quicktime)
ENDIF(WITH_QUICKTIME)
if(WITH_IMAGE_CINEON)
+       add_subdirectory(imbuf/intern/cineon)
endif()
  
IF(WITH_PYTHON)
-       ADD_SUBDIRECTORY(python)
ENDIF(WITH_PYTHON)
if(WITH_CODEC_QUICKTIME)
+       add_subdirectory(quicktime)
endif()
  
- IF(WITH_OPENCOLLADA)
-   ADD_SUBDIRECTORY(collada)
- ENDIF(WITH_OPENCOLLADA)
+ if(WITH_PYTHON)
+       add_subdirectory(python)
+ endif()
+ if(WITH_OPENCOLLADA)
+   add_subdirectory(collada)
+ endif()
  
  #ifndef BKE_DERIVEDMESH_H
  #define BKE_DERIVEDMESH_H
  
 -/** \file BKE_DerivedMesh.h
 - *  \ingroup bke
 - *
 - *  \todo
 - *  - Make drawMapped* functions take a predicate function that
 - *    determines whether to draw the edge (this predicate can
 - *    also set color, etc). This will be slightly more general 
 - *    and allow some of the functions to be collapsed.
 - *  - Once accessor functions are added then single element draw
 - *    functions can be implemented using primitive accessors.
 - *  - Add function to dispatch to renderer instead of using
 - *    conversion to DLM.
 +/*
 +  Basic design of the DerivedMesh system:
 +
 +  DerivedMesh is a common set of interfaces for mesh systems.
 +
 +  There are three main mesh data structures in Blender: Mesh, CDDM, and BMesh.
 +  These, and a few others, all implement DerivedMesh interfaces, 
 +  which contains unified drawing interfaces, a few utility interfaces, 
 +  and a bunch of read-only interfaces intended mostly for conversion from 
 +  one format to another.
 +
 +  All Mesh structures in blender make use of CustomData, which is used to store
 +  per-element attributes and interpolate them (e.g. uvs, vcols, vgroups, etc).
 +  
 +  Mesh is the "serialized" structure, used for storing object-mode mesh data
 +  and also for saving stuff to disk.  It's interfaces are also what DerivedMesh
 +  uses to communicate with.
 +  
 +  CDDM is a little mesh library, that uses Mesh data structures in the backend.
 +  It's mostly used for modifiers, and has the advantages of not taking much
 +  resources.
 +
 +  BMesh is a full-on brep, used for editmode, some modifiers, etc.  It's much
 +  more capable (if memory-intensive) then CDDM.
 +
 +  DerivedMesh is somewhat hackish.  Many places assumes that a DerivedMesh is
 +  a CDDM (most of the time by simply copying it and converting it to one).
 +  CDDM is the original structure for modifiers, but has since been superseded
 +  by BMesh, at least for the foreseeable future.
 +*/
 +
 +/* 
 + * Note: This sturcture is read-only, for all practical purposes.
 + *       At some point in the future, we may want to consider
 + *       creating a replacement structure that implements a proper
 + *       abstract mesh kernel interface.  Or, we can leave this
 + *       as it is and stick with using BMesh and CDDM.
   */
  
- /* TODO (Probably)
-  *
-  *  o Make drawMapped* functions take a predicate function that
-  *    determines whether to draw the edge (this predicate can
-  *    also set color, etc). This will be slightly more general 
-  *    and allow some of the functions to be collapsed.
-  *  o Once accessor functions are added then single element draw
-  *    functions can be implemented using primitive accessors.
-  *  o Add function to dispatch to renderer instead of using
-  *    conversion to DLM.
-  */
 +
  #include "DNA_customdata_types.h"
 +#include "DNA_meshdata_types.h"
 +
  #include "BKE_customdata.h"
  #include "BKE_bvhutils.h"
  
@@@ -617,20 -495,30 +608,30 @@@ DerivedMesh *mesh_create_derived_no_def
  /* for gameengine */
  DerivedMesh *mesh_create_derived_no_virtual(struct Scene *scene, struct Object *ob, float (*vertCos)[3],
                                                                                        CustomDataMask dataMask);
+ DerivedMesh *mesh_create_derived_physics(struct Scene *scene, struct Object *ob, float (*vertCos)[3],
+                                                                                       CustomDataMask dataMask);
  
 -DerivedMesh *editmesh_get_derived(struct EditMesh *em, float (*vertexCos)[3]);
 -DerivedMesh *editmesh_get_derived_base(struct Object *, struct EditMesh *em);
 -DerivedMesh *editmesh_get_derived_cage(struct Scene *scene, struct Object *, 
 -                                                                         struct EditMesh *em, CustomDataMask dataMask);
 -DerivedMesh *editmesh_get_derived_cage_and_final(struct Scene *scene, struct Object *, 
 -                                                                                               struct EditMesh *em, DerivedMesh **final_r,
++DerivedMesh *editbmesh_get_derived(struct BMEditMesh *em, float (*vertexCos)[3]);
 +DerivedMesh *editbmesh_get_derived_base(struct Object *, struct BMEditMesh *em);
 +DerivedMesh *editbmesh_get_derived_cage(struct Scene *scene, struct Object *, 
 +                                                                         struct BMEditMesh *em, CustomDataMask dataMask);
 +DerivedMesh *editbmesh_get_derived_cage_and_final(struct Scene *scene, struct Object *, 
 +                                               struct BMEditMesh *em, DerivedMesh **final_r,
                                                                                                 CustomDataMask dataMask);
 -float (*editmesh_get_vertex_cos(struct EditMesh *em, int *numVerts_r))[3];
 -int editmesh_modifier_is_enabled(struct Scene *scene, struct ModifierData *md, DerivedMesh *dm);
 -void makeDerivedMesh(struct Scene *scene, struct Object *ob, struct EditMesh *em, CustomDataMask dataMask);
++float (*editbmesh_get_vertex_cos(struct BMEditMesh *em, int *numVerts_r))[3];
++int editbmesh_modifier_is_enabled(struct Scene *scene, struct ModifierData *md, DerivedMesh *dm);
 +void makeDerivedMesh(struct Scene *scene, struct Object *ob, struct BMEditMesh *em, CustomDataMask dataMask);
  
  /* returns an array of deform matrices for crazyspace correction, and the
     number of modifiers left */
 -int editmesh_get_first_deform_matrices(struct Scene *, struct Object *, struct EditMesh *em,
 +int editbmesh_get_first_deform_matrices(struct Scene *, struct Object *, struct BMEditMesh *em,
                                                                           float (**deformmats)[3][3], float (**deformcos)[3]);
  
+ /* returns an array of deform matrices for crazyspace correction when sculpting,
+    and the number of modifiers left */
+ int sculpt_get_deform_matrices(struct Scene *scene, struct Object *ob,
+                                                               float (**deformmats)[3][3], float (**deformcos)[3]);
  void weight_to_rgb(float input, float *fr, float *fg, float *fb);
  
  /* convert layers requested by a GLSL material to actually available layers in
Simple merge
@@@ -110,10 -91,6 +112,13 @@@ void CDDM_calc_normals(struct DerivedMe
   */
  void CDDM_calc_edges(struct DerivedMesh *dm);
  
 +/* same as CDDM_calc_edges only makes edges from ngon faces instead of tesselation
 +   faces*/
 +void CDDM_calc_edges_poly(struct DerivedMesh *dm);
 +
++/*reconstitute face triangulation*/
++void CDDM_recalc_tesselation(struct DerivedMesh *dm);
++
  /* lowers the number of vertices/edges/faces in a CDDerivedMesh
   * the layer data stays the same size
   */
@@@ -138,18 -113,6 +143,18 @@@ struct MPoly *CDDM_get_face(struct Deri
   */
  struct MVert *CDDM_get_verts(struct DerivedMesh *dm);
  struct MEdge *CDDM_get_edges(struct DerivedMesh *dm);
 -struct MFace *CDDM_get_faces(struct DerivedMesh *dm);
 +struct MFace *CDDM_get_tessfaces(struct DerivedMesh *dm);
 +struct MLoop *CDDM_get_loops(struct DerivedMesh *dm);
- struct MPoly *CDDM_get_faces(struct DerivedMesh *dm);
++struct MPoly *CDDM_get_polys(struct DerivedMesh *dm);
 +
 +/*Assigns news m*** layers to the cddm.  Note that you must handle
 +  freeing the old ones yourself.  Also you must ensure dm->num****Data
 +  is correct.*/
 +void CDDM_set_mvert(struct DerivedMesh *dm, struct MVert *mvert);
 +void CDDM_set_medge(struct DerivedMesh *dm, struct MEdge *medge);
 +void CDDM_set_mface(struct DerivedMesh *dm, struct MFace *mface);
 +void CDDM_set_mloop(struct DerivedMesh *dm, struct MLoop *mloop);
 +void CDDM_set_mpoly(struct DerivedMesh *dm, struct MPoly *mpoly);
 +
  #endif
  
  #ifndef BKE_CUSTOMDATA_H
  #define BKE_CUSTOMDATA_H
  
+ #ifdef __cplusplus
+ extern "C" {
+ #endif
 +struct BMesh;
  struct ID;
  struct CustomData;
  struct CustomDataLayer;
@@@ -168,8 -146,9 +176,9 @@@ void CustomData_em_copy_data(const stru
                                                        struct CustomData *dest, void *src_block,
                                                        void **dest_block);
  void CustomData_bmesh_copy_data(const struct CustomData *source, 
 -                                                      struct CustomData *dest,void *src_block, 
 -                                                      void **dest_block);
 +                              struct CustomData *dest, void *src_block, 
 +                              void **dest_block);
+ void CustomData_em_validate_data(struct CustomData *data, void *block, int sub_elements);
  
  /* frees data in a CustomData object
   * return 1 on success, 0 on failure
@@@ -226,10 -200,10 +235,10 @@@ void *CustomData_bmesh_get_layer_n(cons
  void *CustomData_get_layer(const struct CustomData *data, int type);
  void *CustomData_get_layer_n(const struct CustomData *data, int type, int n);
  void *CustomData_get_layer_named(const struct CustomData *data, int type,
-                                                                char *name);
+                                                                const char *name);
 -
  int CustomData_get_layer_index(const struct CustomData *data, int type);
- int CustomData_get_named_layer_index(const struct CustomData *data, int type, char *name);
 +int CustomData_get_layer_index_n(const struct CustomData *data, int type, int n);
+ int CustomData_get_named_layer_index(const struct CustomData *data, int type, const char *name);
  int CustomData_get_active_layer_index(const struct CustomData *data, int type);
  int CustomData_get_render_layer_index(const struct CustomData *data, int type);
  int CustomData_get_clone_layer_index(const struct CustomData *data, int type);
Simple merge
@@@ -57,39 -60,12 +63,39 @@@ extern "C" 
  
  struct EditMesh *BKE_mesh_get_editmesh(struct Mesh *me);
  void BKE_mesh_end_editmesh(struct Mesh *me, struct EditMesh *em);
 +struct BMesh *BKE_mesh_to_bmesh(struct Mesh *me, struct Object *ob);
 +
 +/*
 +  this function recreates a tesselation.
 +  returns number of tesselation faces.
 +
 +  use_poly_origindex sets whether or not the tesselation faces' origindex
 +  layer should point to original poly indices or real poly indices.
 +
 +  use_face_origindex sets the tesselation faces' origindex layer
 +  to point to the tesselation faces themselves, not the polys.
 +
 +  if both of the above are 0, it'll use the indices of the mpolys of the MPoly
 +  data in pdata, and ignore the origindex layer altogether.
 + */
 +int mesh_recalcTesselation(struct CustomData *fdata, struct CustomData *ldata, 
 +      struct CustomData *pdata, struct MVert *mvert, int totface, 
 +      int totloop, int totpoly, int use_poly_origindex, int use_face_origindex);
 +
 +/*calculates a face normal.*/
 +void mesh_calc_poly_normal(struct MPoly *mpoly, struct MLoop *loopstart, 
 +                           struct MVert *mvarray, float *no);
  
  void unlink_mesh(struct Mesh *me);
 -void free_mesh(struct Mesh *me);
 +void free_mesh(struct Mesh *me, int unlink);
- struct Mesh *add_mesh(char *name);
+ struct Mesh *add_mesh(const char *name);
  struct Mesh *copy_mesh(struct Mesh *me);
  void mesh_update_customdata_pointers(struct Mesh *me);
 +
 +/*this ensures grouped customdata (e.g. mtexpoly and mloopuv and mtface, or
 +  mloopcol and mcol) have the same relative active/render/clone/mask indices.*/
 +void mesh_update_linked_customdata(struct Mesh *me);
 +
  void make_local_tface(struct Mesh *me);
  void make_local_mesh(struct Mesh *me);
  void boundbox_mesh(struct Mesh *me, float *loc, float *size);
Simple merge
@@@ -75,10 -75,7 +79,9 @@@ typedef struct CCGDerivedMesh 
        short *edgeFlags;
        char *faceFlags;
  
 +      int *reverseFaceMap;
 +
        struct PBVH *pbvh;
-       int pbvh_draw;
        struct ListBase *fmap;
        struct IndexNode *fmap_mem;
  
  #ifndef BKE_UTILDEFINES_H
  #define BKE_UTILDEFINES_H
  
- #ifndef FALSE
- #define FALSE 0
- #endif
- #ifndef TRUE
- #define TRUE 1
- #endif
- /* Macro to convert a value to string in the preprocessor */
- #define QUOTE(x) #x
 +#ifdef __cplusplus
 +extern "C" {
 +#endif
 +
  /* these values need to be hardcoded in structs, dna does not recognize defines */
  /* also defined in DNA_space_types.h */
  #ifndef FILE_MAXDIR
  /* bit-row */
  #define BROW(min, max)        (((max)>=31? 0xFFFFFFFF: (1<<(max+1))-1) - ((min)? ((1<<(min))-1):0) )
  
- #ifdef GS
- #undef GS
- #endif
- #define GS(a) (*((short *)(a)))
- /* Warning-free macros for storing ints in pointers. Use these _only_
-  * for storing an int in a pointer, not a pointer in an int (64bit)! */
- #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
- #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
+ #define BMEMSET(mem, val, size) {unsigned int _i; char *_c = (char*) mem; for (_i=0; _i<size; _i++) *_c++ = val;}
 +/*little macro so inline keyword works*/
 +#if defined(_MSC_VER)
 +#define BM_INLINE static __forceinline
 +#else
 +#define BM_INLINE static inline __attribute((always_inline))
 +#endif
 +
- #define BMEMSET(mem, val, size) {unsigned int _i; char *_c = (char*) mem; for (_i=0; _i<size; _i++) *_c++ = val;}
 +#ifdef __cplusplus
 +}
 +#endif
- #endif
+ #endif // BKE_UTILDEFINES_H
++
  #
  # ***** END GPL LICENSE BLOCK *****
  
- FILE(GLOB SRC intern/*.c)
- SET(INC 
-       . ../../../intern/guardedalloc ../../../intern/memutil ../editors/include ../blenlib ../makesdna ../modifiers
-       ../render/extern/include ../../../intern/decimation/extern
-       ../imbuf ../avi ../../../intern/elbeem/extern ../../../intern/opennl/extern
-       ../../../intern/iksolver/extern ../blenloader ../ikplugin ../bmesh
-       ../nodes ../../../extern/glew/include ../gpu ../makesrna ../../../intern/smoke/extern
-       ../../../intern/bsp/extern ../blenfont
+ set(INC 
+       .
+       ../avi
+       ../blenlib
+       ../blenloader
+       ../gpu
+       ../ikplugin
+       ../imbuf
+       ../makesdna
+       ../makesrna
++      ../bmesh
+       ../modifiers
+       ../nodes
+       ../editors/include
+       ../render/extern/include
+       ../../../extern/glew/include
        ../../../intern/audaspace/intern
+       ../../../intern/bsp/extern ../blenfont
+       ../../../intern/decimation/extern
+       ../../../intern/elbeem/extern
+       ../../../intern/guardedalloc
+       ../../../intern/iksolver/extern
+       ../../../intern/memutil
+       ../../../intern/opennl/extern
+       ../../../intern/smoke/extern
+       ../../../intern/mikktspace
      ../../../source/blender/windowmanager # XXX - BAD LEVEL CALL WM_api.h
-       ${ZLIB_INC}
+       ${ZLIB_INCLUDE_DIRS}
  )
  
+ set(SRC
+       intern/BME_Customdata.c
+       intern/BME_conversions.c
+       intern/BME_eulers.c
+       intern/BME_mesh.c
+       intern/BME_structure.c
+       intern/BME_tools.c
+       intern/CCGSubSurf.c
+       intern/DerivedMesh.c
+       intern/action.c
+       intern/anim.c
+       intern/anim_sys.c
+       intern/armature.c
+       intern/blender.c
+       intern/bmfont.c
+       intern/boids.c
+       intern/booleanops_mesh.c
+       intern/brush.c
+       intern/bullet.c
+       intern/bvhutils.c
+       intern/cdderivedmesh.c
+       intern/cloth.c
+       intern/collision.c
+       intern/colortools.c
+       intern/constraint.c
+       intern/context.c
+       intern/curve.c
+       intern/customdata.c
+       intern/customdata_file.c
+       intern/deform.c
+       intern/depsgraph.c
+       intern/displist.c
+       intern/effect.c
++      intern/editderivedbmesh.c
+       intern/exotic.c
+       intern/fcurve.c
+       intern/fluidsim.c
+       intern/fmodifier.c
+       intern/font.c
+       intern/gpencil.c
+       intern/group.c
+       intern/icons.c
+       intern/idcode.c
+       intern/idprop.c
+       intern/image.c
+       intern/image_gen.c
+       intern/implicit.c
+       intern/ipo.c
+       intern/key.c
+       intern/lattice.c
+       intern/library.c
+       intern/material.c
+       intern/mball.c
+       intern/mesh.c
+       intern/mesh_validate.c
+       intern/modifier.c
++      intern/modifiers_bmesh.c
+       intern/multires.c
+       intern/nla.c
+       intern/node.c
+       intern/object.c
+       intern/packedFile.c
+       intern/paint.c
+       intern/particle.c
+       intern/particle_system.c
+       intern/pointcache.c
+       intern/property.c
+       intern/report.c
+       intern/sca.c
+       intern/scene.c
+       intern/screen.c
+       intern/script.c
+       intern/seqcache.c
+       intern/seqeffects.c
+       intern/sequencer.c
+       intern/shrinkwrap.c
+       intern/sketch.c
+       intern/smoke.c
+       intern/softbody.c
+       intern/sound.c
+       intern/subsurf_ccg.c
+       intern/suggestions.c
+       intern/text.c
+       intern/texture.c
+       intern/unit.c
+       intern/world.c
+       intern/writeavi.c
+       intern/writeffmpeg.c
+       intern/writeframeserver.c
+       BKE_DerivedMesh.h
+       BKE_action.h
+       BKE_anim.h
+       BKE_animsys.h
+       BKE_armature.h
+       BKE_array_mallocn.h
+       BKE_blender.h
+       BKE_bmesh.h
+       BKE_bmeshCustomData.h
+       BKE_bmfont.h
+       BKE_bmfont_types.h
+       BKE_boids.h
+       BKE_booleanops_mesh.h
+       BKE_brush.h
+       BKE_bullet.h
+       BKE_bvhutils.h
+       BKE_cdderivedmesh.h
+       BKE_cloth.h
+       BKE_collision.h
+       BKE_colortools.h
+       BKE_constraint.h
+       BKE_context.h
+       BKE_curve.h
+       BKE_customdata.h
+       BKE_customdata_file.h
+       BKE_deform.h
+       BKE_depsgraph.h
+       BKE_displist.h
+       BKE_effect.h
+       BKE_endian.h
+       BKE_exotic.h
+       BKE_fcurve.h
+       BKE_fluidsim.h
+       BKE_font.h
+       BKE_global.h
+       BKE_gpencil.h
+       BKE_group.h
+       BKE_icons.h
+       BKE_idcode.h
+       BKE_idprop.h
+       BKE_image.h
+       BKE_ipo.h
+       BKE_key.h
+       BKE_lattice.h
+       BKE_library.h
+       BKE_main.h
+       BKE_material.h
+       BKE_mball.h
+       BKE_mesh.h
+       BKE_modifier.h
+       BKE_multires.h
+       BKE_nla.h
+       BKE_node.h
+       BKE_object.h
+       BKE_packedFile.h
+       BKE_paint.h
+       BKE_particle.h
+       BKE_plugin_types.h
+       BKE_pointcache.h
+       BKE_property.h
+       BKE_report.h
+       BKE_sca.h
+       BKE_scene.h
+       BKE_screen.h
+       BKE_script.h
+       BKE_sequencer.h
+       BKE_shrinkwrap.h
+       BKE_sketch.h
+       BKE_smoke.h
+       BKE_softbody.h
+       BKE_sound.h
+       BKE_subsurf.h
+       BKE_suggestions.h
+       BKE_text.h
+       BKE_texture.h
+       BKE_unit.h
+       BKE_utildefines.h
+       BKE_world.h
+       BKE_writeavi.h
+       BKE_writeffmpeg.h
+       BKE_writeframeserver.h
+       depsgraph_private.h
+       intern/CCGSubSurf.h
+       intern/bmesh_private.h
+       nla_private.h
+ )
  
ADD_DEFINITIONS(-DGLEW_STATIC)
add_definitions(-DGLEW_STATIC)
  
IF(WITH_BULLET)
-       SET(INC ${INC} ../../../extern/bullet2/src)
-       ADD_DEFINITIONS(-DUSE_BULLET)
ENDIF(WITH_BULLET)
if(WITH_BULLET)
+       list(APPEND INC ../../../extern/bullet2/src)
+       add_definitions(-DUSE_BULLET)
endif()
  
IF(WITH_IMAGE_OPENEXR)
-       ADD_DEFINITIONS(-DWITH_OPENEXR)
ENDIF(WITH_IMAGE_OPENEXR)
if(WITH_IMAGE_OPENEXR)
+       add_definitions(-DWITH_OPENEXR)
endif()
  
IF(WITH_IMAGE_TIFF)
-       ADD_DEFINITIONS(-DWITH_TIFF)
ENDIF(WITH_IMAGE_TIFF)
if(WITH_IMAGE_TIFF)
+       add_definitions(-DWITH_TIFF)
endif()
  
IF(WITH_IMAGE_OPENJPEG)
-       ADD_DEFINITIONS(-DWITH_OPENJPEG)
ENDIF(WITH_IMAGE_OPENJPEG)
if(WITH_IMAGE_OPENJPEG)
+       add_definitions(-DWITH_OPENJPEG)
endif()
  
IF(WITH_IMAGE_DDS)
-       ADD_DEFINITIONS(-DWITH_DDS)
ENDIF(WITH_IMAGE_DDS)
if(WITH_IMAGE_DDS)
+       add_definitions(-DWITH_DDS)
endif()
  
IF(WITH_IMAGE_CINEON)
-       ADD_DEFINITIONS(-DWITH_CINEON)
ENDIF(WITH_IMAGE_CINEON)
if(WITH_IMAGE_CINEON)
+       add_definitions(-DWITH_CINEON)
endif()
  
IF(WITH_IMAGE_HDR)
-       ADD_DEFINITIONS(-DWITH_HDR)
ENDIF(WITH_IMAGE_HDR)
if(WITH_IMAGE_HDR)
+       add_definitions(-DWITH_HDR)
endif()
  
IF(WITH_QUICKTIME)
-       SET(INC ${INC} ../quicktime ${QUICKTIME_INC})
-       ADD_DEFINITIONS(-DWITH_QUICKTIME)
ENDIF(WITH_QUICKTIME)
if(WITH_CODEC_QUICKTIME)
+       list(APPEND INC ../quicktime ${QUICKTIME_INC})
+       add_definitions(-DWITH_QUICKTIME)
endif()
  
IF(WITH_FFMPEG)
-       SET(INC ${INC} ${FFMPEG_INC})
-       ADD_DEFINITIONS(-DWITH_FFMPEG)
ENDIF(WITH_FFMPEG)
if(WITH_CODEC_FFMPEG)
+       list(APPEND INC ${FFMPEG_INC})
+       add_definitions(-DWITH_FFMPEG)
endif()
  
IF(WITH_LCMS)
-       SET(INC ${INC} ${LCMS_INCLUDE_DIR})
-       ADD_DEFINITIONS(-DWITH_LCMS)
ENDIF(WITH_LCMS)
if(WITH_LCMS)
+       list(APPEND INC ${LCMS_INCLUDE_DIR})
+       add_definitions(-DWITH_LCMS)
endif()
  
- IF(WITH_PYTHON)
-       SET(INC ${INC} ../python ${PYTHON_INC})
- ELSE(WITH_PYTHON)
-       ADD_DEFINITIONS(-DDISABLE_PYTHON)
- ENDIF(WITH_PYTHON)
+ if(WITH_PYTHON)
+       list(APPEND INC ../python ${PYTHON_INCLUDE_DIRS})
+       add_definitions(-DWITH_PYTHON)
+ endif()
  
IF(WITH_OPENMP)
-               ADD_DEFINITIONS(-DPARALLEL=1)
ENDIF(WITH_OPENMP)
if(WITH_OPENMP)
+       add_definitions(-DPARALLEL=1)
endif()
  
IF(NOT WITH_FLUID)
-       ADD_DEFINITIONS(-DDISABLE_ELBEEM)
ENDIF(NOT WITH_FLUID)
if(NOT WITH_MOD_FLUID)
+       add_definitions(-DDISABLE_ELBEEM)
endif()
  
- IF(WITH_LZO)
-       SET(INC ${INC} ../../../extern/lzo/minilzo)
-       ADD_DEFINITIONS(-DWITH_LZO)
- ENDIF(WITH_LZO)
+ if(WITH_JACK)
+       add_definitions(-DWITH_JACK)
+ endif()
  
IF(WITH_LZMA)
-       SET(INC ${INC} ../../../extern/lzma)
-       ADD_DEFINITIONS(-DWITH_LZMA)
ENDIF(WITH_LZMA)
if(WITH_LZO)
+       list(APPEND INC ../../../extern/lzo/minilzo)
+       add_definitions(-DWITH_LZO)
endif()
  
- IF(WIN32)
-       SET(INC ${INC} ${PTHREADS_INC})
- ENDIF(WIN32)
+ if(WITH_LZMA)
+       list(APPEND INC ../../../extern/lzma)
+       add_definitions(-DWITH_LZMA)
+ endif()
  
- BLENDERLIB(bf_blenkernel "${SRC}" "${INC}")
+ if(MSVC)
+     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
+ endif()
  
+ blender_add_lib(bf_blenkernel "${SRC}" "${INC}")
@@@ -11,8 -11,8 +11,9 @@@ incs += ' #/intern/iksolver/extern ../b
  incs += ' #/extern/bullet2/src'
  incs += ' #/intern/opennl/extern #/intern/bsp/extern'
  incs += ' ../gpu #/extern/glew/include'
 +incs += ' ../bmesh'
  incs += ' #/intern/smoke/extern'
+ incs += ' #/intern/mikktspace'
  incs += ' #/intern/audaspace/intern'
  
  incs += ' ' + env['BF_OPENGL_INC']
@@@ -1,5 -1,4 +1,6 @@@
 +#if 0
 +/**
+ /*
   * BME_customdata.c    jan 2007
   *
   *    Custom Data functions for Bmesh
@@@ -1,5 -1,4 +1,6 @@@
 +#if 0
 +/**
+ /*
   * BME_mesh.c    jan 2007
   *
   *    BMesh mesh level functions.
@@@ -1,5 -1,4 +1,6 @@@
 +#if 0
 +/**
+ /*
   * BME_eulers.c    jan 2007
   *
   *    BMesh Euler construction API.
@@@ -1,5 -1,4 +1,6 @@@
 +#if 0
 +/**
+ /*
   * BME_mesh.c    jan 2007
   *
   *    BMesh mesh level functions.
@@@ -1,5 -1,4 +1,6 @@@
 +#if 0
 +/**
+ /*
   * BME_structure.c    jan 2007
   *
   *    Low level routines for manipulating the BMesh structure.
   *
   * ***** END GPL LICENSE BLOCK *****
   */
 -
 +#if 0
  #include <limits.h>
- #include "MEM_guardedalloc.h"
  
+ #include "MEM_guardedalloc.h"
+ #include "BLI_utildefines.h"
  #include "BKE_bmesh.h"
  /**
   *    MISC utility functions.
@@@ -1,5 -1,4 +1,6 @@@
 +#if 0
 +/**
+ /*
   * BME_tools.c    jan 2007
   *
   *    Functions for changing the topology of a mesh.
  #include "DNA_meshdata_types.h"
  #include "DNA_object_types.h"
  
- #include "BKE_bmesh.h"
  #include "BLI_math.h"
++<<<<<<< .working
 +#include "BLI_cellalloc.h"
++=======
+ #include "BLI_utildefines.h"
++>>>>>>> .merge-right.r35190
+ #include "BKE_bmesh.h"
  
  /*split this all into a seperate bevel.c file in src*/
  
@@@ -584,11 -584,11 +590,11 @@@ static float BME_bevel_set_max(BME_Ver
        return max;
  }
  
- static BME_Vert *BME_bevel_wire(BME_Mesh *bm, BME_Vert *v, float value, int res, int options, BME_TransData_Head *td) {
+ static BME_Vert *BME_bevel_wire(BME_Mesh *bm, BME_Vert *v, float value, int res, int UNUSED(options), BME_TransData_Head *td) {
        BME_Vert *ov1, *ov2, *v1, *v2;
  
 -      ov1 = BME_edge_getothervert(v->edge, v);
 -      ov2 = BME_edge_getothervert(BME_disk_nextedge(v->edge, v), v);
 +      ov1 = BME_edge_getothervert(v->e, v);
 +      ov2 = BME_edge_getothervert(BME_disk_nextedge(v->e, v), v);
  
        /* split the edges */
        v1 = BME_bevel_split_edge(bm,v,ov1,NULL,NULL,value,td);
@@@ -1158,13 -1158,13 +1164,13 @@@ static void bmesh_dissolve_disk(BME_Mes
                                        break;
                                }
                                e = BME_disk_nextedge(e,v);
 -                      }while(e != v->edge);
 +                      }while(e != v->e);
                }
 -              BME_collapse_vert(bm, v->edge, v, 1.0);
 -              //BME_JEKV(bm,v->edge,v);
 +              BME_collapse_vert(bm, v->e, v, 1.0);
 +              //BME_JEKV(bm,v->e,v);
        }
  }
- static BME_Mesh *BME_bevel_mesh(BME_Mesh *bm, float value, int res, int options, int defgrp_index, BME_TransData_Head *td) {
+ static BME_Mesh *BME_bevel_mesh(BME_Mesh *bm, float value, int res, int options, int UNUSED(defgrp_index), BME_TransData_Head *td) {
        BME_Vert *v, *nv;
        BME_Edge *e, *oe;
        BME_Loop *l, *l2;
@@@ -42,8 -42,8 +42,9 @@@
  #include "BLI_editVert.h"
  #include "BLI_math.h"
  #include "BLI_memarena.h"
 +#include "BLI_array.h"
  #include "BLI_pbvh.h"
+ #include "BLI_utildefines.h"
  
  #include "BKE_cdderivedmesh.h"
  #include "BKE_displist.h"
  #include "BKE_object.h"
  #include "BKE_paint.h"
  #include "BKE_texture.h"
- #include "BKE_utildefines.h"
+ #include "BKE_multires.h"
 +#include "BKE_particle.h"
 +#include "BKE_tessmesh.h"
 +#include "BKE_bvhutils.h"
  
  #include "BLO_sys_types.h" // for intptr_t support
  
  #include "BIF_gl.h"
@@@ -394,10 -241,17 +398,19 @@@ void DM_to_mesh(DerivedMesh *dm, Mesh *
        if(!CustomData_has_layer(&tmp.edata, CD_MEDGE))
                CustomData_add_layer(&tmp.edata, CD_MEDGE, CD_ASSIGN, dm->dupEdgeArray(dm), totedge);
        if(!CustomData_has_layer(&tmp.fdata, CD_MFACE))
 -              CustomData_add_layer(&tmp.fdata, CD_MFACE, CD_ASSIGN, dm->dupFaceArray(dm), totface);
 +              CustomData_add_layer(&tmp.fdata, CD_MFACE, CD_ASSIGN, dm->dupTessFaceArray(dm), totface);
 +      if(!CustomData_has_layer(&tmp.pdata, CD_MPOLY))
 +              dm_add_polys_from_iter(&tmp.ldata, &tmp.pdata, dm, totloop);
  
+       /* object had got displacement layer, should copy this layer to save sculpted data */
+       /* NOTE: maybe some other layers should be copied? nazgul */
+       if(CustomData_has_layer(&me->fdata, CD_MDISPS)) {
+               if (totface == me->totface) {
+                       MDisps *mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS);
+                       CustomData_add_layer(&tmp.fdata, CD_MDISPS, CD_DUPLICATE, mdisps, totface);
+               }
+       }
        mesh_update_customdata_pointers(&tmp);
  
        CustomData_free(&me->vdata, me->totvert);
@@@ -620,24 -430,9 +633,24 @@@ void DM_swap_tessface_data(DerivedMesh 
        CustomData_swap(&dm->faceData, index, corner_indices);
  }
  
 -///
 +void DM_interp_loop_data(DerivedMesh *source, DerivedMesh *dest,
 +                         int *src_indices,
 +                         float *weights, int count, int dest_index)
 +{
 +      CustomData_interp(&source->loopData, &dest->loopData, src_indices,
 +                        weights, NULL, count, dest_index);
 +}
  
- static DerivedMesh *getMeshDerivedMesh(Mesh *me, Object *ob, float (*vertCos)[3])
 +void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
 +                         int *src_indices,
 +                         float *weights, int count, int dest_index)
 +{
 +      CustomData_interp(&source->polyData, &dest->polyData, src_indices,
 +                        weights, NULL, count, dest_index);
 +}
 +
 +///
+ DerivedMesh *mesh_create_derived(Mesh *me, Object *ob, float (*vertCos)[3])
  {
        DerivedMesh *dm = CDDM_from_mesh(me, ob);
        
@@@ -1062,20 -1853,22 +1111,27 @@@ static void mesh_calc_modifiers(Scene *
                                if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
                                        add_weight_mcol_dm(ob, dm);
  
-                               /* constructive modifiers need to have an origindex
-                                * otherwise they wont have anywhere to copy the data from */
-                               if(needMapping) {
-                                       int *index, i;
+                               /* Constructive modifiers need to have an origindex
+                                * otherwise they wont have anywhere to copy the data from.
+                                *
+                                * Also create ORIGINDEX data if any of the following modifiers
+                                * requests it, this way Mirror, Solidify etc will keep ORIGINDEX
+                                * data by using generic DM_copy_vert_data() functions.
+                                */
+                               if(needMapping || (nextmask & CD_MASK_ORIGINDEX)) {
++                                      int i, *orig;
++                                      
+                                       /* calc */
                                        DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
                                        DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
                                        DM_add_face_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
  
-                                       index = DM_get_vert_data_layer(dm, CD_ORIGINDEX);
-                                       for(i=0; i<dm->numVertData; i++) *index++= i;
-                                       index = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
-                                       for(i=0; i<dm->numEdgeData; i++) *index++= i;
-                                       index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
-                                       for(i=0; i<dm->numPolyData; i++) *index++= i;
 -                                      range_vni(DM_get_vert_data_layer(dm, CD_ORIGINDEX), dm->numVertData, 0);
 -                                      range_vni(DM_get_edge_data_layer(dm, CD_ORIGINDEX), dm->numEdgeData, 0);
 -                                      range_vni(DM_get_face_data_layer(dm, CD_ORIGINDEX), dm->numFaceData, 0);
++                                      orig = DM_get_vert_data_layer(dm, CD_ORIGINDEX);
++                                      for(i=0; i<dm->numVertData; i++) *orig++= i;
++                                      orig = DM_get_edge_data_layer(dm, CD_ORIGINDEX);
++                                      for(i=0; i<dm->numEdgeData; i++) *orig++= i;
++                                      orig = DM_get_face_data_layer(dm, CD_ORIGINDEX);
++                                      for(i=0; i<dm->numPolyData; i++) *orig++= i;
                                }
                        }
  
        BLI_linklist_free(datamasks, NULL);
  }
  
- static float (*editbmesh_getVertexCos(BMEditMesh *em, int *numVerts_r))[3]
 -float (*editmesh_get_vertex_cos(EditMesh *em, int *numVerts_r))[3]
++float (*editbmesh_get_vertex_cos(BMEditMesh *em, int *numVerts_r))[3]
  {
 -      int i, numVerts = *numVerts_r = BLI_countlist(&em->verts);
 +      int i, numVerts = *numVerts_r = em->bm->totvert;
        float (*cos)[3];
 -      EditVert *eve;
 +      BMIter iter;
 +      BMVert *eve;
  
        cos = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos");
 -      for (i=0,eve=em->verts.first; i<numVerts; i++,eve=eve->next) {
 +
 +      eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
 +      for (i=0; eve; eve=BMIter_Step(&iter), i++) {
                VECCOPY(cos[i], eve->co);
        }
  
        return cos;
  }
  
- static int editbmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm)
 -int editmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm)
++int editbmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm)
  {
        ModifierTypeInfo *mti = modifierType_getInfo(md->type);
        int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
@@@ -1295,7 -2082,7 +1348,7 @@@ static void editbmesh_calc_modifiers(Sc
                                                MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
                                        dm->getVertCos(dm, deformedVerts);
                                } else {
-                                       deformedVerts = editbmesh_getVertexCos(em, &numVerts);
 -                                      deformedVerts = editmesh_get_vertex_cos(em, &numVerts);
++                                      deformedVerts = editbmesh_get_vertex_cos(em, &numVerts);
                                }
                        }
  
                        }
  
                        /* set the DerivedMesh to only copy needed data */
-                       DM_set_only_copy(dm, (CustomDataMask)GET_INT_FROM_POINTER(curr->link));
+                       mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link); /* CD_MASK_ORCO may have been cleared above */
  
-                       if(((CustomDataMask)GET_INT_FROM_POINTER(curr->link)) & CD_MASK_ORIGSPACE)
+                       DM_set_only_copy(dm, mask | CD_MASK_ORIGINDEX);
+                       if(mask & CD_MASK_ORIGSPACE)
                                if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
 -                                      DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
 +                                      DM_add_tessface_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
                        
                        if (mti->applyModifierEM)
                                ndm = mti->applyModifierEM(md, ob, em, dm);
@@@ -1757,13 -2603,15 +1869,15 @@@ void DM_add_tangent_layer(DerivedMesh *
        if(CustomData_get_layer_index(&dm->faceData, CD_TANGENT) != -1)
                return;
  
 -      nors = dm->getFaceDataArray(dm, CD_NORMAL);
++      nors = dm->getTessFaceDataArray(dm, CD_NORMAL);
        /* check we have all the needed layers */
        totvert= dm->getNumVerts(dm);
 -      totface= dm->getNumFaces(dm);
 +      totface= dm->getNumTessFaces(dm);
  
        mvert= dm->getVertArray(dm);
 -      mface= dm->getFaceArray(dm);
 -      mtface= dm->getFaceDataArray(dm, CD_MTFACE);
 +      mface= dm->getTessFaceArray(dm);
 +      mtface= dm->getTessFaceDataArray(dm, CD_MTFACE);
  
        if(!mtface) {
                orco= dm->getVertDataArray(dm, CD_ORCO);
  #include "DNA_key_types.h"
  #include "DNA_meshdata_types.h"
  #include "DNA_scene_types.h"
 +#include "DNA_windowmanager_types.h"
 +#include "DNA_view3d_types.h"
  #include "DNA_vfont_types.h"
  
+ #include "BKE_animsys.h"
  #include "BKE_curve.h"
  #include "BKE_DerivedMesh.h"
  #include "BKE_depsgraph.h"
@@@ -64,8 -64,8 +66,9 @@@
  #include "BKE_particle.h"
  #include "BKE_scene.h"
  #include "BKE_utildefines.h"
 +#include "BKE_tessmesh.h"
  #include "BKE_depsgraph.h"
+ #include "BKE_anim.h"
  
  
  // XXX bad level call...
@@@ -854,9 -880,10 +883,10 @@@ static void vertex_duplilist(ListBase *
        Scene *sce = NULL;
        Group *group = NULL;
        GroupObject * go = NULL;
 -      EditMesh *em;
 +      BMEditMesh *em;
        float vec[3], no[3], pmat[4][4];
-       int lay, totvert, a, oblay;
+       int totvert, a, oblay;
+       unsigned int lay;
        
        copy_m4_m4(pmat, par->obmat);
        
@@@ -36,7 -36,7 +36,8 @@@
  
  #include "BLI_math.h"
  #include "BLI_blenlib.h"
 +#include "BLI_cellalloc.h"
+ #include "BLI_utildefines.h"
  
  #include "DNA_anim_types.h"
  #include "DNA_armature_types.h"
@@@ -466,10 -445,9 +445,11 @@@ static int read_undosave(bContext *C, U
        
        /* This is needed so undoing/redoing doesnt crash with threaded previews going */
        WM_jobs_stop_all(CTX_wm_manager(C));
-       
-       strcpy(scestr, G.sce);  /* temporal store */
+       BLI_strncpy(mainstr, G.main->name, sizeof(mainstr));    /* temporal store */
 +      strcpy(mainstr, G.main->name);  /* temporal store */
 +
        fileflags= G.fileflags;
        G.fileflags |= G_FILE_NO_UI;
  
  #include "BLI_editVert.h"
  #include "BLI_math.h"
  #include "BLI_pbvh.h"
 +#include "BLI_array.h"
 +#include "BLI_smallhash.h"
+ #include "BLI_utildefines.h"
+ #include "BKE_cdderivedmesh.h"
+ #include "BKE_global.h"
+ #include "BKE_mesh.h"
+ #include "BKE_paint.h"
  
  #include "DNA_meshdata_types.h"
  #include "DNA_object_types.h"
@@@ -622,16 -637,18 +658,18 @@@ static void cdDM_drawFacesTex_common(De
  {
        CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
        MVert *mv = cddm->mvert;
 -      MFace *mf = DM_get_face_data_layer(dm, CD_MFACE);
 -      MCol *realcol = dm->getFaceDataArray(dm, CD_TEXTURE_MCOL);
 -      float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
 -      MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
 -      int i, j, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
 +      MFace *mf = DM_get_tessface_data_layer(dm, CD_MFACE);
 +      MCol *realcol = dm->getTessFaceDataArray(dm, CD_TEXTURE_MCOL);
 +      float *nors= dm->getTessFaceDataArray(dm, CD_NORMAL);
 +      MTFace *tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
 +      int i, j, orig, *index = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
        int startFace = 0, lastFlag = 0xdeadbeef;
 -      MCol *mcol = dm->getFaceDataArray(dm, CD_WEIGHT_MCOL);
 +      MCol *mcol = dm->getTessFaceDataArray(dm, CD_WEIGHT_MCOL);
        if(!mcol)
 -              mcol = dm->getFaceDataArray(dm, CD_MCOL);
 +              mcol = dm->getTessFaceDataArray(dm, CD_MCOL);
  
+       cdDM_update_normals_from_pbvh(dm);
        if( GPU_buffer_legacy(dm) ) {
                DEBUG_VBO( "Using legacy code. cdDM_drawFacesTex_common\n" );
                for(i = 0; i < dm->numFaceData; i++, mf++) {
@@@ -798,15 -817,17 +838,17 @@@ static void cdDM_drawMappedFaces(Derive
        MVert *mv = cddm->mvert;
        MFace *mf = cddm->mface;
        MCol *mc;
 -      float *nors= dm->getFaceDataArray(dm, CD_NORMAL);
 -      int i, orig, *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
 +      float *nors= dm->getTessFaceDataArray(dm, CD_NORMAL);
 +      int i, orig, *index = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
  
 -      mc = DM_get_face_data_layer(dm, CD_ID_MCOL);
 +      mc = DM_get_tessface_data_layer(dm, CD_ID_MCOL);
        if(!mc)
 -              mc = DM_get_face_data_layer(dm, CD_WEIGHT_MCOL);
 +              mc = DM_get_tessface_data_layer(dm, CD_WEIGHT_MCOL);
        if(!mc)
 -              mc = DM_get_face_data_layer(dm, CD_MCOL);
 +              mc = DM_get_tessface_data_layer(dm, CD_MCOL);
  
+       cdDM_update_normals_from_pbvh(dm);
        /* back-buffer always uses legacy since VBO's would need the
         * color array temporarily overwritten for drawing, then reset. */
        if( GPU_buffer_legacy(dm) || G.f & G_BACKBUFSEL) {
@@@ -955,14 -968,15 +989,15 @@@ static void cdDM_drawMappedFacesGLSL(De
        DMVertexAttribs attribs;
        MVert *mvert = cddm->mvert;
        MFace *mface = cddm->mface;
 -      MTFace *tf = dm->getFaceDataArray(dm, CD_MTFACE);
 -      float (*nors)[3] = dm->getFaceDataArray(dm, CD_NORMAL);
 +      MTFace *tf = dm->getTessFaceDataArray(dm, CD_MTFACE);
 +      float (*nors)[3] = dm->getTessFaceDataArray(dm, CD_NORMAL);
-       int a, b, dodraw, smoothnormal, matnr, new_matnr;
+       int a, b, dodraw, matnr, new_matnr;
        int transp, new_transp, orig_transp;
 -      int orig, *index = dm->getFaceDataArray(dm, CD_ORIGINDEX);
 +      int orig, *index = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
  
+       cdDM_update_normals_from_pbvh(dm);
        matnr = -1;
-       smoothnormal = 0;
        dodraw = 0;
        transp = GPU_get_material_blend_mode();
        orig_transp = transp;
                        else
                                PASSVERT(mface->v3, 2)
  
--#undef PASSVERT
                }
                glEnd();
        }
@@@ -1392,30 -1426,6 +1451,35 @@@ static void cdDM_foreachMappedFaceCente
  
                func(userData, orig, cent, no);
        }
 +
 +}
 +
 +static void cdDM_recalcTesselation(DerivedMesh *dm)
 +{
 +      CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
 +
 +      dm->numFaceData = mesh_recalcTesselation(&dm->faceData, &dm->loopData, 
 +              &dm->polyData, cddm->mvert, dm->numFaceData, dm->numLoopData, 
 +              dm->numPolyData, 1, 0);
 +      
 +      cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
 +}
 +
++void CDDM_recalc_tesselation(DerivedMesh *dm)
++{
++      cdDM_recalcTesselation(dm);
++}
++
 +/*ignores original poly origindex layer*/
 +static void cdDM_recalcTesselation2(DerivedMesh *dm)
 +{
 +      CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
 +
 +      dm->numFaceData = mesh_recalcTesselation(&dm->faceData, &dm->loopData, 
 +              &dm->polyData, cddm->mvert, dm->numFaceData, dm->numLoopData, 
 +              dm->numPolyData, 0, 0);
 +      
 +      cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
  }
  
  static void cdDM_free_internal(CDDerivedMesh *cddm)
@@@ -1570,11 -1554,11 +1634,11 @@@ DerivedMesh *CDDM_from_mesh(Mesh *mesh
        return dm;
  }
  
- DerivedMesh *CDDM_from_editmesh(EditMesh *em, Mesh *me)
+ DerivedMesh *CDDM_from_editmesh(EditMesh *em, Mesh *UNUSED(me))
  {
        DerivedMesh *dm = CDDM_new(BLI_countlist(&em->verts),
 -                                                         BLI_countlist(&em->edges),
 -                                                         BLI_countlist(&em->faces));
 +                                 BLI_countlist(&em->edges),
 +                                 BLI_countlist(&em->faces), 0, 0);
        CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
        EditVert *eve;
        EditEdge *eed;
@@@ -1707,332 -1688,7 +1770,331 @@@ DerivedMesh *CDDM_from_curve_customDB(O
        return dm;
  }
  
 -DerivedMesh *CDDM_copy(DerivedMesh *source)
 +static void loops_to_customdata_corners(BMesh *bm, CustomData *facedata,
 +                                        int cdindex, BMLoop *l3[3],
 +                                        int numCol, int numTex)
 +{
 +      BMLoop *l;
 +      BMFace *f = l3[0]->f;
 +      MTFace *texface;
 +      MTexPoly *texpoly;
 +      MCol *mcol;
 +      MLoopCol *mloopcol;
 +      MLoopUV *mloopuv;
 +      int i, j, hasWCol = CustomData_has_layer(&bm->ldata, CD_WEIGHT_MLOOPCOL);
 +
 +      for(i=0; i < numTex; i++){
 +              texface = CustomData_get_n(facedata, CD_MTFACE, cdindex, i);
 +              texpoly = CustomData_bmesh_get_n(&bm->pdata, f->head.data, CD_MTEXPOLY, i);
 +              
 +              texface->tpage = texpoly->tpage;
 +              texface->flag = texpoly->flag;
 +              texface->transp = texpoly->transp;
 +              texface->mode = texpoly->mode;
 +              texface->tile = texpoly->tile;
 +              texface->unwrap = texpoly->unwrap;
 +      
 +              for (j=0; j<3; j++) {
 +                      l = l3[j];
 +                      mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
 +                      texface->uv[j][0] = mloopuv->uv[0];
 +                      texface->uv[j][1] = mloopuv->uv[1];
 +              }
 +      }
 +
 +      for(i=0; i < numCol; i++){
 +              mcol = CustomData_get_n(facedata, CD_MCOL, cdindex, i);
 +              
 +              for (j=0; j<3; j++) {
 +                      l = l3[j];
 +                      mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPCOL, i);
 +                      mcol[j].r = mloopcol->r;
 +                      mcol[j].g = mloopcol->g;
 +                      mcol[j].b = mloopcol->b;
 +                      mcol[j].a = mloopcol->a;
 +              }
 +      }
 +
 +      if (hasWCol) {
 +              mcol = CustomData_get(facedata, cdindex, CD_WEIGHT_MCOL);
 +
 +              for (j=0; j<3; j++) {
 +                      l = l3[j];
 +                      mloopcol = CustomData_bmesh_get(&bm->ldata, l->head.data, CD_WEIGHT_MLOOPCOL);
 +                      mcol[j].r = mloopcol->r;
 +                      mcol[j].g = mloopcol->g;
 +                      mcol[j].b = mloopcol->b;
 +                      mcol[j].a = mloopcol->a;
 +              }
 +      }
 +}
 +
 +DerivedMesh *CDDM_from_BMEditMesh(BMEditMesh *em, Mesh *me)
 +{
 +      DerivedMesh *dm = CDDM_new(em->bm->totvert, em->bm->totedge, 
 +                             em->tottri, em->bm->totloop, em->bm->totface);
 +      CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
 +      BMesh *bm = em->bm;
 +      BMIter iter, liter;
 +      BMVert *eve;
 +      BMEdge *eed;
 +      BMFace *efa;
 +      MVert *mvert = cddm->mvert;
 +      MEdge *medge = cddm->medge;
 +      MFace *mface = cddm->mface;
 +      MLoop *mloop = cddm->mloop;
 +      MPoly *mpoly = cddm->mpoly;
 +      int numCol = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPCOL);
 +      int numTex = CustomData_number_of_layers(&em->bm->pdata, CD_MTEXPOLY);
 +      int i, j, *index, add_orig;
 +
 +      dm->deformedOnly = 1;
 +      
 +      /*don't add origindex layer if one already exists*/
 +      add_orig = !CustomData_has_layer(&em->bm->pdata, CD_ORIGINDEX);
 +
 +      CustomData_merge(&em->bm->vdata, &dm->vertData, CD_MASK_DERIVEDMESH,
 +                       CD_CALLOC, dm->numVertData);
 +      CustomData_merge(&em->bm->edata, &dm->edgeData, CD_MASK_DERIVEDMESH,
 +                       CD_CALLOC, dm->numEdgeData);
 +      CustomData_merge(&em->bm->ldata, &dm->loopData, CD_MASK_DERIVEDMESH,
 +                       CD_CALLOC, dm->numLoopData);
 +      CustomData_merge(&em->bm->pdata, &dm->polyData, CD_MASK_DERIVEDMESH,
 +                       CD_CALLOC, dm->numPolyData);
 +      
 +      /*add tesselation mface layers*/
 +      CustomData_from_bmeshpoly(&dm->faceData, &dm->polyData, &dm->loopData, em->tottri);
 +
 +      /* set vert index */
 +      eve = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
 +      for (i=0; eve; eve=BMIter_Step(&iter), i++)
 +              BMINDEX_SET(eve, i);
 +
 +      index = dm->getVertDataArray(dm, CD_ORIGINDEX);
 +
 +      eve = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
 +      for (i=0; eve; eve=BMIter_Step(&iter), i++, index++) {
 +              MVert *mv = &mvert[i];
 +
 +              VECCOPY(mv->co, eve->co);
 +
 +              BMINDEX_SET(eve, i);
 +
 +              mv->no[0] = eve->no[0] * 32767.0;
 +              mv->no[1] = eve->no[1] * 32767.0;
 +              mv->no[2] = eve->no[2] * 32767.0;
 +
-               mv->mat_nr = 0;
 +              mv->flag = BMFlags_To_MEFlags(eve);
 +
 +              if (add_orig) *index = i;
 +
 +              CustomData_from_bmesh_block(&bm->vdata, &dm->vertData, eve->head.data, i);
 +      }
 +
 +      index = dm->getEdgeDataArray(dm, CD_ORIGINDEX);
 +      eed = BMIter_New(&iter, bm, BM_EDGES_OF_MESH, NULL);
 +      for (i=0; eed; eed=BMIter_Step(&iter), i++, index++) {
 +              MEdge *med = &medge[i];
 +
 +              BMINDEX_SET(eed, i);
 +
 +              med->v1 = BMINDEX_GET(eed->v1);
 +              med->v2 = BMINDEX_GET(eed->v2);
 +              med->flag = ME_EDGEDRAW|ME_EDGERENDER;
 +              
 +              med->flag = BMFlags_To_MEFlags(eed);
 +
 +              CustomData_from_bmesh_block(&bm->edata, &dm->edgeData, eed->head.data, i);
 +              if (add_orig) *index = i;
 +      }
 +
 +      efa = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
 +      for (i=0; efa; i++, efa=BMIter_Step(&iter)) {
 +              BMINDEX_SET(efa, i);
 +      }
 +
 +      index = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
 +      for(i = 0; i < dm->numFaceData; i++, index++) {
 +              MFace *mf = &mface[i];
 +              BMLoop **l = em->looptris[i];
 +              efa = l[0]->f;
 +
 +              mf->v1 = BMINDEX_GET(l[0]->v);
 +              mf->v2 = BMINDEX_GET(l[1]->v);
 +              mf->v3 = BMINDEX_GET(l[2]->v);
 +              mf->v4 = 0;
 +              mf->mat_nr = efa->mat_nr;
 +              mf->flag = BMFlags_To_MEFlags(efa);
 +              
 +              *index = add_orig ? BMINDEX_GET(efa) : *(int*)CustomData_bmesh_get(&bm->pdata, efa->head.data, CD_ORIGINDEX);
 +
 +              loops_to_customdata_corners(bm, &dm->faceData, i, l, numCol, numTex);
 +              test_index_face(mf, &dm->faceData, i, 3);
 +      }
 +      
 +      index = CustomData_get_layer(&dm->polyData, CD_ORIGINDEX);
 +      j = 0;
 +      efa = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
 +      for (i=0; efa; i++, efa=BMIter_Step(&iter), index++) {
 +              BMLoop *l;
 +              MPoly *mp = &mpoly[i];
 +
 +              mp->totloop = efa->len;
 +              mp->flag = BMFlags_To_MEFlags(efa);
 +              mp->loopstart = j;
 +              mp->mat_nr = efa->mat_nr;
 +              
 +              BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, efa) {
 +                      mloop->v = BMINDEX_GET(l->v);
 +                      mloop->e = BMINDEX_GET(l->e);
 +                      CustomData_from_bmesh_block(&bm->ldata, &dm->loopData, l->head.data, j);
 +
 +                      j++;
 +                      mloop++;
 +              }
 +
 +              CustomData_from_bmesh_block(&bm->pdata, &dm->polyData, efa->head.data, i);
 +
 +              if (add_orig) *index = i;
 +      }
 +
 +      return dm;
 +}
 +
 +typedef struct CDDM_LoopIter {
 +      DMLoopIter head;
 +      CDDerivedMesh *cddm;
 +      int len, i;
 +} CDDM_LoopIter;
 +
 +typedef struct CDDM_FaceIter {
 +      DMFaceIter head;
 +      CDDerivedMesh *cddm;
 +      CDDM_LoopIter liter;
 +} CDDM_FaceIter;
 +
 +void cddm_freeiter(void *self)
 +{
 +      MEM_freeN(self);
 +}
 +
 +void cddm_stepiter(void *self)
 +{
 +      CDDM_FaceIter *iter = self;
 +      MPoly *mp;
 +      
 +      mp = iter->cddm->mpoly + iter->head.index;
 +      mp->flag = iter->head.flags;
 +      mp->mat_nr = iter->head.mat_nr;
 +
 +      iter->head.index++;
 +      if (iter->head.index >= iter->cddm->dm.numPolyData) {
 +              iter->head.done = 1;
 +              return;
 +      }
 +
 +      mp = iter->cddm->mpoly + iter->head.index;
 +
 +      iter->head.flags = mp->flag;
 +      iter->head.mat_nr = mp->mat_nr;
 +      iter->head.len = mp->totloop;
 +}
 +
 +void *cddm_faceiter_getcddata(void *self, int type, int layer)
 +{
 +      CDDM_FaceIter *iter = self;
 +
 +      if (layer == -1) return CustomData_get(&iter->cddm->dm.polyData, 
 +                                             iter->head.index, type);
 +      else return CustomData_get_n(&iter->cddm->dm.polyData, type, 
 +                                  iter->head.index, layer);
 +}
 +
 +void *cddm_loopiter_getcddata(void *self, int type, int layer)
 +{
 +      CDDM_LoopIter *iter = self;
 +
 +      if (layer == -1) return CustomData_get(&iter->cddm->dm.loopData, 
 +                                             iter->head.index, type);
 +      else return CustomData_get_n(&iter->cddm->dm.loopData, type, 
 +                                   iter->head.index, layer);
 +}
 +
 +void *cddm_loopiter_getvertcddata(void *self, int type, int layer)
 +{
 +      CDDM_LoopIter *iter = self;
 +
 +      if (layer == -1) return CustomData_get(&iter->cddm->dm.vertData, 
 +                                             iter->cddm->mloop[iter->head.vindex].v,
 +                                             type);
 +      else return CustomData_get_n(&iter->cddm->dm.vertData, type, 
 +                                   iter->cddm->mloop[iter->head.vindex].v, layer);
 +}
 +
 +DMLoopIter *cddmiter_get_loopiter(void *self)
 +{
 +      CDDM_FaceIter *iter = self;
 +      CDDM_LoopIter *liter = &iter->liter;
 +      MPoly *mp = iter->cddm->mpoly + iter->head.index;
 +
 +      liter->i = -1;
 +      liter->len = iter->head.len;
 +      liter->head.index = mp->loopstart-1;
 +      liter->head.done = 0;
 +
 +      liter->head.step(liter);
 +
 +      return (DMLoopIter*) liter;
 +}
 +
 +void cddm_loopiter_step(void *self)
 +{
 +      CDDM_LoopIter *liter = self;
 +      MLoop *ml;
 +
 +      liter->i++;
 +      liter->head.index++;
 +
 +      if (liter->i == liter->len) {
 +              liter->head.done = 1;
 +              return;
 +      }
 +
 +      ml = liter->cddm->mloop + liter->head.index;
 +
 +      liter->head.eindex = ml->e;
 +      liter->head.v = liter->cddm->mvert[ml->v];
 +      liter->head.vindex = ml->v;
 +}
 +
 +DMFaceIter *cdDM_newFaceIter(DerivedMesh *source)
 +{
 +      CDDerivedMesh *cddm = (CDDerivedMesh*) source;
 +      CDDM_FaceIter *iter = MEM_callocN(sizeof(CDDM_FaceIter), "DMFaceIter from cddm");
 +
 +      iter->head.free = cddm_freeiter;
 +      iter->head.step = cddm_stepiter;
 +      iter->head.getCDData = cddm_faceiter_getcddata;
 +      iter->head.getLoopsIter = cddmiter_get_loopiter;
 +
 +      iter->liter.head.step = cddm_loopiter_step;
 +      iter->liter.head.getLoopCDData = cddm_loopiter_getcddata;
 +      iter->liter.head.getVertCDData = cddm_loopiter_getvertcddata;
 +      iter->liter.cddm = cddm;
 +
 +      iter->cddm = cddm;
 +
 +      if (source->numFaceData) {
 +              iter->head.index = -1;
 +              iter->head.step(iter);
 +      } else {
 +              iter->head.done = 1;
 +      }
 +
 +      return (DMFaceIter*) iter;
 +}
 +
 +DerivedMesh *CDDM_copy(DerivedMesh *source, int faces_from_tessfaces)
  {
        CDDerivedMesh *cddm = cdDM_create("CDDM_copy cddm");
        DerivedMesh *dm = &cddm->dm;
        return dm;
  }
  
+ /* note, the CD_ORIGINDEX layers are all 0, so if there is a direct
+  * relationship betwen mesh data this needs to be set by the caller. */
  DerivedMesh *CDDM_from_template(DerivedMesh *source,
 -                                                              int numVerts, int numEdges, int numFaces)
 +                                int numVerts, int numEdges, int numFaces,
 +                                                              int numLoops, int numPolys)
  {
        CDDerivedMesh *cddm = cdDM_create("CDDM_from_template dest");
        DerivedMesh *dm = &cddm->dm;
@@@ -2679,101 -1951,3 +2743,106 @@@ MFace *CDDM_get_tessfaces(DerivedMesh *
        return ((CDDerivedMesh*)dm)->mface;
  }
  
++MPoly *CDDM_get_polys(DerivedMesh *dm)
++{
++      return ((CDDerivedMesh*)dm)->mpoly;
++}
++
 +void CDDM_tessfaces_to_faces(DerivedMesh *dm)
 +{
 +      /*converts mfaces to mpolys/mloops*/
 +      CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
 +      MFace *mf;
 +      MEdge *me;
 +      MLoop *ml;
 +      MPoly *mp;
 +      EdgeHash *eh = BLI_edgehash_new();
 +      int i, l, totloop, *index1, *index2;
 +      
 +      /*ensure we have all the edges we need*/
 +      CDDM_calc_edges(dm);
 +
 +      /*build edge hash*/
 +      me = cddm->medge;
 +      for (i=0; i<cddm->dm.numEdgeData; i++, me++) {
 +              BLI_edgehash_insert(eh, me->v1, me->v2, SET_INT_IN_POINTER(i));
 +      }
 +
 +      mf = cddm->mface;
 +      totloop = 0;
 +      for (i=0; i<cddm->dm.numFaceData; i++, mf++) {
 +              totloop += mf->v4 ? 4 : 3;
 +      }
 +
 +      CustomData_free(&cddm->dm.polyData, cddm->dm.numPolyData);
 +      CustomData_free(&cddm->dm.loopData, cddm->dm.numLoopData);
 +      
 +      cddm->dm.numLoopData = totloop;
 +      cddm->dm.numPolyData = cddm->dm.numFaceData;
 +
 +      if (!totloop) return;
 +
 +      cddm->mloop = MEM_callocN(sizeof(MLoop)*totloop, "cddm->mloop in CDDM_tessfaces_to_faces");
 +      cddm->mpoly = MEM_callocN(sizeof(MPoly)*cddm->dm.numFaceData, "cddm->mpoly in CDDM_tessfaces_to_faces");
 +      
 +      CustomData_add_layer(&cddm->dm.loopData, CD_MLOOP, CD_ASSIGN, cddm->mloop, totloop);
 +      CustomData_add_layer(&cddm->dm.polyData, CD_MPOLY, CD_ASSIGN, cddm->mpoly, cddm->dm.numPolyData);
 +      CustomData_merge(&cddm->dm.faceData, &cddm->dm.polyData, 
 +              CD_MASK_ORIGINDEX, CD_DUPLICATE, cddm->dm.numFaceData);
 +
 +      index1 = CustomData_get_layer(&cddm->dm.faceData, CD_ORIGINDEX);
 +      index2 = CustomData_get_layer(&cddm->dm.polyData, CD_ORIGINDEX);
 +
 +      mf = cddm->mface;
 +      mp = cddm->mpoly;
 +      ml = cddm->mloop;
 +      l = 0;
 +      for (i=0; i<cddm->dm.numFaceData; i++, mf++, mp++) {
 +              mp->flag = mf->flag;
 +              mp->loopstart = l;
 +              mp->mat_nr = mf->mat_nr;
 +              mp->totloop = mf->v4 ? 4 : 3;
 +              
 +              ml->v = mf->v1;
 +              ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(eh, mf->v1, mf->v2));
 +              ml++, l++;
 +
 +              ml->v = mf->v2;
 +              ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(eh, mf->v2, mf->v3));
 +              ml++, l++;
 +
 +              ml->v = mf->v3;
 +              ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(eh, mf->v3, mf->v4?mf->v4:mf->v1));
 +              ml++, l++;
 +
 +              if (mf->v4) {
 +                      ml->v = mf->v4;
 +                      ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(eh, mf->v4, mf->v1));
 +                      ml++, l++;
 +              }
 +
 +      }
 +
 +      BLI_edgehash_free(eh, NULL);
 +}
 +
 +void CDDM_set_mvert(DerivedMesh *dm, MVert *mvert)
 +{
 +      CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
 +
 +      cddm->mvert = mvert;
 +}
 +
 +void CDDM_set_medge(DerivedMesh *dm, MEdge *medge)
 +{
 +      CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
 +
 +      cddm->medge = medge;
 +}
 +
 +void CDDM_set_mface(DerivedMesh *dm, MFace *mface)
 +{
 +      CDDerivedMesh *cddm = (CDDerivedMesh*)dm;
 +
 +      cddm->mface = mface;
 +}
  #include "BKE_global.h"
  #include "BKE_modifier.h"
  #include "BKE_pointcache.h"
- #include "BKE_utildefines.h"
- #include "BKE_pointcache.h"
- #include "BLI_kdopbvh.h"
- #include "BLI_cellalloc.h"
  
 -
  #ifdef _WIN32
  void tstart ( void )
  {}
@@@ -945,11 -926,11 +925,11 @@@ static int cloth_from_object(Object *ob
        return 1;
  }
  
- static void cloth_from_mesh ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm )
+ static void cloth_from_mesh ( ClothModifierData *clmd, DerivedMesh *dm )
  {
        unsigned int numverts = dm->getNumVerts ( dm );
 -      unsigned int numfaces = dm->getNumFaces ( dm );
 -      MFace *mface = dm->getFaceArray( dm );
 +      unsigned int numfaces = dm->getNumTessFaces ( dm );
-       MFace *mface = CDDM_get_tessfaces(dm);
++      MFace *mface = dm->getTessFaceArray( dm );
        unsigned int i = 0;
  
        /* Allocate our vertices. */
@@@ -1058,12 -1039,12 +1038,12 @@@ static int cloth_build_springs ( ClothM
        Cloth *cloth = clmd->clothObject;
        ClothSpring *spring = NULL, *tspring = NULL, *tspring2 = NULL;
        unsigned int struct_springs = 0, shear_springs=0, bend_springs = 0;
-       int i = 0;
-       int numverts = dm->getNumVerts ( dm );
-       int numedges = dm->getNumEdges ( dm );
-       int numfaces = dm->getNumTessFaces ( dm );
-       MEdge *medge = CDDM_get_edges ( dm );
-       MFace *mface = CDDM_get_tessfaces ( dm );
+       unsigned int i = 0;
+       unsigned int numverts = (unsigned int)dm->getNumVerts ( dm );
+       unsigned int numedges = (unsigned int)dm->getNumEdges ( dm );
 -      unsigned int numfaces = (unsigned int)dm->getNumFaces ( dm );
++      unsigned int numfaces = (unsigned int)dm->getNumTessFaces ( dm );
+       MEdge *medge = dm->getEdgeArray ( dm );
 -      MFace *mface = dm->getFaceArray ( dm );
++      MFace *mface = dm->getTessFaceArray ( dm );
        int index2 = 0; // our second vertex index
        LinkNode **edgelist = NULL;
        EdgeHash *edgehash = NULL;
@@@ -39,7 -39,7 +39,8 @@@
  #include "BLI_listbase.h"
  #include "BLI_math.h"
  #include "BLI_editVert.h"
 +#include "BLI_cellalloc.h"
+ #include "BLI_utildefines.h"
  
  #include "DNA_armature_types.h"
  #include "DNA_constraint_types.h"
@@@ -52,9 -52,8 +53,9 @@@
  #include "DNA_lattice_types.h"
  #include "DNA_scene_types.h"
  #include "DNA_text_types.h"
 +#include "DNA_windowmanager_types.h"
  
- #include "BKE_utildefines.h"
  #include "BKE_action.h"
  #include "BKE_anim.h" /* for the curve calculation part */
  #include "BKE_armature.h"
  #include "BKE_global.h"
  #include "BKE_library.h"
  #include "BKE_idprop.h"
 +#include "BKE_mesh.h"
  #include "BKE_shrinkwrap.h"
  #include "BKE_mesh.h"
 +#include "BKE_tessmesh.h"
  
- #ifndef DISABLE_PYTHON
+ #ifdef WITH_PYTHON
  #include "BPY_extern.h"
  #endif
  
  #include "BKE_main.h"
  #include "BKE_screen.h"
  
 +#ifdef EVENT_RECORDER
 +#include "../../../../intern/ghost/GHOST_C-api.h"
 +#endif
 +
 +#ifndef DISABLE_PYTHON
+ #ifdef WITH_PYTHON
  #include "BPY_extern.h"
  #endif
++#endif
  
  /* struct */
  
@@@ -34,7 -34,7 +34,8 @@@
  
  #include <math.h>
  #include <string.h>
- #include "BLI_cellalloc.h"
++
+ #include <assert.h>
  
  #include "MEM_guardedalloc.h"
  
  #include "DNA_ID.h"
  
  #include "BLI_blenlib.h"
+ #include "BLI_path_util.h"
  #include "BLI_linklist.h"
+ #include "BLI_math.h"
  #include "BLI_mempool.h"
++#include "BLI_cellalloc.h"
+ #include "BLI_utildefines.h"
  
  #include "BKE_customdata.h"
  #include "BKE_customdata_file.h"
  #include "BKE_global.h"
+ #include "BKE_main.h"
  #include "BKE_utildefines.h"
+ #include "BKE_multires.h"
  
 +#include "bmesh.h"
 +
 +#include <math.h>
 +#include <string.h>
 +
  /* number of layers to add when growing a CustomData object */
  #define CUSTOMDATA_GROW 5
  
@@@ -896,47 -801,9 +941,47 @@@ static void layerDefault_mcol(void *dat
                mcol[i] = default_mcol;
  }
  
 +static void layerInterp_bweight(void **sources, float *weights,
 +                             float *sub_weights, int count, void *dest)
 +{
 +      float *f = dest, *src;
 +      float **in = sources;
 +      int i;
 +      
 +      if(count <= 0) return;
 +
 +      *f = 0.0f;
 +      
 +      for(i = 0; i < count; ++i) {
 +              float weight = weights ? weights[i] : 1.0f;
 +              
 +              src = in[i];
 +              *f += *src * weight;
 +      }
 +}
 +
 +static void layerInterp_shapekey(void **sources, float *weights,
 +                             float *sub_weights, int count, void *dest)
 +{
 +      float *co = dest, *src;
 +      float **in = sources;
 +      int i, j, k;
  
 +      if(count <= 0) return;
 +
 +      memset(co, 0, sizeof(float)*3);
 +      
 +      for(i = 0; i < count; ++i) {
 +              float weight = weights ? weights[i] : 1.0f;
 +              
 +              src = in[i];
 +              co[0] += src[0] * weight;
 +              co[1] += src[1] * weight;
 +              co[2] += src[2] * weight;
 +      }
 +}
  
- const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
        {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
        {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
         NULL},
         layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
        {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
        {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL},
 -      {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL},
 -      {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL, layerDefault_mloopcol},
 +      {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL,
 +       layerEqual_mloopuv, layerMultiply_mloopuv, layerInitMinMax_mloopuv, 
 +       layerAdd_mloopuv, layerDoMinMax_mloopuv, layerCopyValue_mloopuv},
 +      {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL, 
 +       layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol, 
 +       layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
-       {sizeof(float)*3*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
+       {sizeof(float)*4*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
        {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps,
 -       layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL, layerRead_mdisps, layerWrite_mdisps,
 -       layerFilesize_mdisps, layerValidate_mdisps},
 +       layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL, 
 +       NULL, NULL, NULL, NULL, NULL, NULL, 
-        layerRead_mdisps, layerWrite_mdisps, layerFilesize_mdisps},
++       layerRead_mdisps, layerWrite_mdisps, layerFilesize_mdisps, layerValidate_mdisps},
        {sizeof(MCol)*4, "MCol", 4, "WeightCol", NULL, NULL, layerInterp_mcol,
         layerSwap_mcol, layerDefault_mcol},
 -       {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol,
 +      {sizeof(MPoly), "MPoly", 1, "NGon Face", NULL, NULL, NULL, NULL, NULL},
 +      {sizeof(MLoop), "MLoop", 1, "NGon Face-Vertex", NULL, NULL, NULL, NULL, NULL},
 +      {sizeof(float)*3, "", 0, "ClothOrco", NULL, NULL, layerInterp_shapekey},
 +      {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol,
         layerSwap_mcol, layerDefault_mcol},
 -       {sizeof(MCol)*4, "MCol", 4, "TexturedCol", NULL, NULL, layerInterp_mcol,
 +      {sizeof(MCol)*4, "MCol", 4, "TextureCol", NULL, NULL, layerInterp_mcol,
         layerSwap_mcol, layerDefault_mcol},
 -      {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL}
 +      {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
 +      {sizeof(float)*3, "", 0, "ShapeKey", NULL, NULL, layerInterp_shapekey},
 +      {sizeof(float), "", 0, "BevelWeight", NULL, NULL, layerInterp_bweight},
 +      {sizeof(float), "", 0, "SubSurfCrease", NULL, NULL, layerInterp_bweight},
 +      {sizeof(MLoopCol), "MLoopCol", 1, "WeightLoopCol", NULL, NULL, layerInterp_mloopcol, NULL,
 +       layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
 +       layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
  };
  
- const char *LAYERTYPENAMES[CD_NUMTYPES] = {
static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
 -      /*   0-4 */ "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace",
 -      /*   5-9 */ "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags",
 -      /* 10-14 */ "CDMFloatProperty", "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco",
 -      /* 15-19 */ "CDMTexPoly", "CDMLoopUV", "CDMloopCol", "CDTangent", "CDMDisps",
 -      /* 20-23 */"CDWeightMCol", "CDIDMCol", "CDTextureMCol", "CDClothOrco"
 +      "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace", "CDMTFace",
 +      "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags","CDMFloatProperty",
 +      "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco", "CDMTexPoly", "CDMLoopUV",
 +      "CDMloopCol", "CDTangent", "CDMDisps", "CDWeightMCol", "CDMPoly", 
 +      "CDMLoop", "CDMClothOrco", "CDMLoopCol", "CDIDCol", "CDTextureCol",
 +      "CDShapeKeyIndex", "CDShapeKey", "CDBevelWeight", "CDSubSurfCrease"
  };
  
 +
  const CustomDataMask CD_MASK_BAREMESH =
 -      CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE;
 +      CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE | CD_MASK_MLOOP | CD_MASK_MPOLY | CD_MASK_BWEIGHT;
  const CustomDataMask CD_MASK_MESH =
        CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
        CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
@@@ -1064,13 -893,13 +1109,13 @@@ void customData_update_typemap(CustomDa
  void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
                                          CustomDataMask mask, int alloctype, int totelem)
  {
-       const LayerTypeInfo *typeInfo;
+       /*const LayerTypeInfo *typeInfo;*/
        CustomDataLayer *layer, *newlayer;
        int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0, lastclone = 0, lastmask = 0, lastflag = 0;
 -
 +      
        for(i = 0; i < source->totlayer; ++i) {
                layer = &source->layers[i];
-               typeInfo = layerType_getInfo(layer->type);
+               /*typeInfo = layerType_getInfo(layer->type);*/ /*UNUSED*/
  
                type = layer->type;
  
@@@ -1184,18 -1011,7 +1229,18 @@@ int CustomData_get_layer_index(const Cu
        return -1;
  }
  
- int CustomData_get_named_layer_index(const CustomData *data, int type, char *name)
 +int CustomData_get_layer_index_n(const struct CustomData *data, int type, int n)
 +{
 +      int i; 
 +
 +      for(i=0; i < data->totlayer; ++i)
 +              if(data->layers[i].type == type)
 +                      return i + n;
 +
 +      return -1;      
 +}
 +
+ int CustomData_get_named_layer_index(const CustomData *data, int type, const char *name)
  {
        int i;
  
@@@ -1442,9 -1257,8 +1487,9 @@@ static CustomDataLayer *customData_add_
        data->layers[index].type = type;
        data->layers[index].flag = flag;
        data->layers[index].data = newlayerdata;
 +
        if(name || (name=typeInfo->defaultname)) {
-               strcpy(data->layers[index].name, name);
+               BLI_strncpy(data->layers[index].name, name, 32);
                CustomData_set_layer_unique_name(data, index);
        }
        else
index 168e0e5,0000000..b38521b
mode 100644,000000..100644
--- /dev/null
@@@ -1,1709 -1,0 +1,1711 @@@
-               BLI_edgefill(0, 0);
 +/**
 + * $Id: editderivedbmesh.c 18571 2009-01-19 06:04:57Z joeedh $
 + *
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software Foundation,
 + * Inc., 59 Tbmple Place - Suite 330, Boston, MA  02111-1307, USA.
 + *
 + * The Original Code is Copyright (C) 2005 Blender Foundation.
 + * All rights reserved.
 + *
 + * The Original Code is: all of this file.
 + *
 + * Contributor(s): none yet.
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +
 +#include <string.h>
 +
 +#ifdef HAVE_CONFIG_H
 +#include <config.h>
 +#endif
 +
 +#include "PIL_time.h"
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "DNA_effect_types.h"
 +#include "DNA_mesh_types.h"
 +#include "DNA_key_types.h"
 +#include "DNA_meshdata_types.h"
 +#include "DNA_modifier_types.h"
 +#include "DNA_object_types.h"
 +#include "DNA_object_force.h"
 +#include "DNA_object_fluidsim.h" // N_T
 +#include "DNA_scene_types.h" // N_T
 +#include "DNA_texture_types.h"
 +#include "DNA_view3d_types.h"
 +#include "DNA_screen_types.h"
 +#include "DNA_space_types.h"
 +#include "DNA_particle_types.h"
 +
 +#include "BLI_math.h"
 +#include "BLI_blenlib.h"
 +#include "BLI_editVert.h"
 +#include "BLI_edgehash.h"
 +#include "BLI_linklist.h"
 +#include "BLI_memarena.h"
 +#include "BLI_scanfill.h"
 +#include "BLI_ghash.h"
 +#include "BLI_array.h"
++#include "BLI_utildefines.h"
 +
 +#include "BKE_cdderivedmesh.h"
 +#include "BKE_customdata.h"
 +#include "BKE_DerivedMesh.h"
 +#include "BKE_deform.h"
 +#include "BKE_displist.h"
 +#include "BKE_effect.h"
 +#include "BKE_fluidsim.h"
 +#include "BKE_global.h"
 +#include "BKE_key.h"
 +#include "BKE_material.h"
 +#include "BKE_modifier.h"
 +#include "BKE_mesh.h"
 +#include "BKE_object.h"
 +#include "BKE_subsurf.h"
 +#include "BKE_texture.h"
 +#include "BKE_utildefines.h"
 +#include "BKE_particle.h"
 +#include "BKE_tessmesh.h"
 +
 +#include "BLO_sys_types.h" // for intptr_t support
 +
 +#include "BIF_gl.h"
 +#include "BIF_glutil.h"
 +
 +#include "GPU_draw.h"
 +#include "GPU_extensions.h"
 +#include "GPU_material.h"
 +
 +#include "bmesh.h"
 +
 +BMEditMesh *BMEdit_Create(BMesh *bm)
 +{
 +      BMEditMesh *tm = MEM_callocN(sizeof(BMEditMesh), "tm");
 +      
 +      tm->bm = bm;
 +
 +      BMEdit_RecalcTesselation(tm);
 +
 +      return tm;
 +}
 +
 +BMEditMesh *BMEdit_Copy(BMEditMesh *tm)
 +{
 +      BMEditMesh *tm2 = MEM_callocN(sizeof(BMEditMesh), "tm2");
 +      *tm2 = *tm;
 +      
 +      tm2->derivedCage = tm2->derivedFinal = NULL;
 +      
 +      tm2->looptris = NULL;
 +      tm2->bm = BM_Copy_Mesh(tm->bm);
 +      BMEdit_RecalcTesselation(tm2);
 +
 +      tm2->vert_index = NULL;
 +      tm2->edge_index = NULL;
 +      tm2->face_index = NULL;
 +
 +      return tm2;
 +}
 +
 +static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
 +{
 +      BMesh *bm = tm->bm;
 +      BMLoop **looptris = NULL;
 +      BLI_array_declare(looptris);
 +      BMIter iter, liter;
 +      BMFace *f;
 +      BMLoop *l;
 +      int i = 0, j;
 +      
 +      if (tm->looptris) MEM_freeN(tm->looptris);
 +
 +#if 0 //simple quad/triangle code for performance testing purposes
 +      looptris = MEM_callocN(sizeof(void*)*bm->totface*8, "looptris");
 +
 +      f = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
 +      for ( ; f; f=BMIter_Step(&iter)) {
 +              EditVert *v, *lastv=NULL, *firstv=NULL;
 +              EditEdge *e;
 +              EditFace *efa;
 +
 +              /*don't consider two-edged faces*/
 +              if (f->len < 3) continue;
 +              
 +              //BLI_array_growone(looptris);
 +              //BLI_array_growone(looptris);
 +              //BLI_array_growone(looptris);
 +
 +              looptris[i*3] = f->loopbase;
 +              looptris[i*3+1] = f->loopbase->head.next;
 +              looptris[i*3+2] = f->loopbase->head.next->next;
 +              i++;
 +
 +              if (f->len > 3) {
 +                      //BLI_array_growone(looptris);
 +                      //BLI_array_growone(looptris);
 +                      //BLI_array_growone(looptris);
 +
 +                      looptris[i*3] = f->loopbase;
 +                      looptris[i*3+1] = f->loopbase->head.next->next;
 +                      looptris[i*3+2] = f->loopbase->head.next->next->next;
 +                      i++;
 +              }
 +
 +      }
 +
 +      tm->tottri = i;
 +      tm->looptris = looptris;
 +      return;
 +#endif
 +
 +      f = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
 +      for ( ; f; f=BMIter_Step(&iter)) {
 +              EditVert *v, *lastv=NULL, *firstv=NULL;
 +              EditEdge *e;
 +              EditFace *efa;
 +
 +              /*don't consider two-edged faces*/
 +              if (f->len < 3) continue;
 +              
 +              /*scanfill time*/
 +              l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
 +              for (j=0; l; l=BMIter_Step(&liter), j++) {
 +                      /*mark order*/
 +                      l->_index = j;
 +
 +                      v = BLI_addfillvert(l->v->co);
 +                      v->tmp.p = l;
 +                      
 +                      if (lastv) {
 +                              e = BLI_addfilledge(lastv, v);
 +                      }
 +
 +                      lastv = v;
 +                      if (firstv==NULL) firstv = v;
 +              }
 +
 +              /*complete the loop*/
 +              BLI_addfilledge(firstv, v);
 +
- #if 0
++              BLI_edgefill(0);
 +              
 +              for (efa = fillfacebase.first; efa; efa=efa->next) {
 +                      BMLoop *l1, *l2, *l3;
 +
 +                      BLI_array_growone(looptris);
 +                      BLI_array_growone(looptris);
 +                      BLI_array_growone(looptris);
 +                      
 +                      looptris[i*3] = l1 = efa->v1->tmp.p;
 +                      looptris[i*3+1] = l2 = efa->v2->tmp.p;
 +                      looptris[i*3+2] = l3 = efa->v3->tmp.p;
 +                      
 +                      if (l1->_index > l2->_index) {
 +                              SWAP(BMLoop*, l1, l2);
 +                      }
 +                      if (l2->_index > l3->_index) {
 +                              SWAP(BMLoop*, l2, l3);
 +                      }
 +                      if (l1->_index > l2->_index) {
 +                              SWAP(BMLoop*, l1, l2);
 +                      }
 +                      
 +                      looptris[i*3] = l1;
 +                      looptris[i*3+1] = l2;
 +                      looptris[i*3+2] = l3;
 +
 +                      i += 1;
 +              }
 +
 +              BLI_end_edgefill();
 +      }
 +
 +      tm->tottri = i;
 +      tm->looptris = looptris;
 +}
 +
 +void BMEdit_RecalcTesselation(BMEditMesh *tm)
 +{
 +      BMEdit_RecalcTesselation_intern(tm);
 +
 +      if (tm->derivedFinal && tm->derivedFinal == tm->derivedCage) {
 +              if (tm->derivedFinal->recalcTesselation) 
 +                      tm->derivedFinal->recalcTesselation(tm->derivedFinal);
 +      } else if (tm->derivedFinal) {
 +              if (tm->derivedCage->recalcTesselation) 
 +                      tm->derivedCage->recalcTesselation(tm->derivedCage);
 +              if (tm->derivedFinal->recalcTesselation) 
 +                      tm->derivedFinal->recalcTesselation(tm->derivedFinal);
 +      }
 +}
 +
 +void BMEdit_UpdateLinkedCustomData(BMEditMesh *em)
 +{
 +      BMesh *bm = em->bm;
 +      int act;
 +
 +      if (CustomData_has_layer(&bm->pdata, CD_MTEXPOLY)) {
 +              act = CustomData_get_active_layer(&bm->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_active(&bm->ldata, CD_MLOOPUV, act);
 +
 +              act = CustomData_get_render_layer(&bm->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_render(&bm->ldata, CD_MLOOPUV, act);
 +
 +              act = CustomData_get_clone_layer(&bm->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_clone(&bm->ldata, CD_MLOOPUV, act);
 +
 +              act = CustomData_get_stencil_layer(&bm->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_stencil(&bm->ldata, CD_MLOOPUV, act);
 +      }
 +}
 +
 +/*does not free the BMEditMesh struct itself*/
 +void BMEdit_Free(BMEditMesh *em)
 +{
 +      if(em->derivedFinal) {
 +              if (em->derivedFinal!=em->derivedCage) {
 +                      em->derivedFinal->needsFree= 1;
 +                      em->derivedFinal->release(em->derivedFinal);
 +              }
 +              em->derivedFinal= NULL;
 +      }
 +      if(em->derivedCage) {
 +              em->derivedCage->needsFree= 1;
 +              em->derivedCage->release(em->derivedCage);
 +              em->derivedCage= NULL;
 +      }
 +
 +      em->retopo_paint_data= NULL;
 +
 +      if (em->looptris) MEM_freeN(em->looptris);
 +
 +      if (em->vert_index) MEM_freeN(em->vert_index);
 +      if (em->edge_index) MEM_freeN(em->edge_index);
 +      if (em->face_index) MEM_freeN(em->face_index);
 +
 +      BM_Free_Mesh(em->bm);
 +}
 +
 +
 +/*
 +ok, basic design:
 +
 +the bmesh derivedmesh exposes the mesh as triangles.  it stores pointers
 +to three loops per triangle.  the derivedmesh stores a cache of tesselations
 +for each face.  this cache will smartly update as needed (though at first
 +it'll simply be more brute force).  keeping track of face/edge counts may
 +be a small problbm.
 +
 +this won't be the most efficient thing, considering that internal edges and
 +faces of tesselations are exposed.  looking up an edge by index in particular
 +is likely to be a little slow.
 +*/
 +
 +typedef struct EditDerivedBMesh {
 +      DerivedMesh dm;
 +
 +      Object *ob;
 +      BMEditMesh *tc;
 +
 +      float (*vertexCos)[3];
 +      float (*vertexNos)[3];
 +      float (*faceNos)[3];
 +
 +      /*lookup caches; these are rebuilt on dm->RecalcTesselation()
 +        (or when the derivedmesh is created, of course)*/
 +      GHash *vhash, *ehash, *fhash;
 +      BMVert **vtable;
 +      BMEdge **etable;
 +      BMFace **ftable;
 +
 +      /*private variables, for number of verts/edges/faces
 +        within the above hash/table members*/
 +      int tv, te, tf;
 +
 +      /*customdata layout of the tesselated faces*/
 +      CustomData tessface_layout;
 +} EditDerivedBMesh;
 +
 +static void bmdm_recalc_lookups(EditDerivedBMesh *bmdm)
 +{
 +      BMIter iter;
 +      BMHeader *h;
 +      int a, i, iters[3] = {BM_VERTS_OF_MESH, BM_EDGES_OF_MESH, BM_FACES_OF_MESH};
 +      
 +      bmdm->tv = bmdm->tc->bm->totvert;
 +      bmdm->te = bmdm->tc->bm->totedge;
 +      bmdm->tf = bmdm->tc->bm->totface;
 +
 +      if (bmdm->vhash) BLI_ghash_free(bmdm->vhash, NULL, NULL);
 +      if (bmdm->ehash) BLI_ghash_free(bmdm->ehash, NULL, NULL);
 +      if (bmdm->fhash) BLI_ghash_free(bmdm->fhash, NULL, NULL);
 +
 +      bmdm->vhash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
 +      bmdm->ehash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
 +      bmdm->fhash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
 +      
 +      if (bmdm->vtable) MEM_freeN(bmdm->vtable);
 +      if (bmdm->etable) MEM_freeN(bmdm->etable);
 +      if (bmdm->ftable) MEM_freeN(bmdm->ftable);
 +      
 +      if (bmdm->tc->bm->totvert)
 +              bmdm->vtable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totvert, "bmdm->vtable");
 +      else bmdm->vtable = NULL;
 +
 +      if (bmdm->tc->bm->totedge)
 +              bmdm->etable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totedge, "bmdm->etable");
 +      else bmdm->etable = NULL;
 +      
 +      if (bmdm->tc->bm->totface)
 +              bmdm->ftable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totface, "bmdm->ftable");
 +      else bmdm->ftable = NULL;
 +      
 +      for (a=0; a<3; a++) {
 +              h = BMIter_New(&iter, bmdm->tc->bm, iters[a], NULL);
 +              for (i=0; h; h=BMIter_Step(&iter), i++) {
 +                      switch (a) {
 +                              case 0:
 +                                      bmdm->vtable[i] = (BMVert*) h;
 +                                      BLI_ghash_insert(bmdm->vhash, h, SET_INT_IN_POINTER(i));
 +                                      break;
 +                              case 1:
 +                                      bmdm->etable[i] = (BMEdge*) h;
 +                                      BLI_ghash_insert(bmdm->ehash, h, SET_INT_IN_POINTER(i));
 +                                      break;
 +                              case 2:
 +                                      bmdm->ftable[i] = (BMFace*) h;
 +                                      BLI_ghash_insert(bmdm->fhash, h, SET_INT_IN_POINTER(i));
 +                                      break;
 +
 +                      }
 +              }
 +      }
 +}
 +
 +static void bmDM_recalcTesselation(DerivedMesh *dm)
 +{
 +      //EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +
 +      //bmdm_recalc_lookups(bmdm);
 +}
 +
 +static void bmDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData)
 +{
 +      EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +      BMVert *eve;
 +      BMIter iter;
 +      int i;
 +      
 +      eve = BMIter_New(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
 +      for (i=0; eve; i++, eve=BMIter_Step(&iter)) {
 +              if (bmdm->vertexCos) {
 +                      func(userData, i, bmdm->vertexCos[i], bmdm->vertexNos[i], NULL);
 +              } else {
 +                      func(userData, i, eve->co, eve->no, NULL);
 +              }
 +      }
 +}
 +static void bmDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, int index, float *v0co, float *v1co), void *userData)
 +{
 +      EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +      BMEdge *eed;
 +      BMIter iter;
 +      int i;
 +      
 +      if (bmdm->vertexCos) {
 +              BMVert *eve;
 +              BMIter viter;
 +
 +              eve = BMIter_New(&viter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
 +              for (i=0; eve; eve=BMIter_Step(&viter), i++) {
 +                      BMINDEX_SET(eve, i);
 +              }
 +
 +              eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
 +              for(i=0; eed; i++,eed=BMIter_Step(&iter))
 +                      func(userData, i, 
 +                           bmdm->vertexCos[BMINDEX_GET(eve)], 
 +                           bmdm->vertexCos[BMINDEX_GET(eve)]);
 +      } else {
 +              eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
 +              for(i=0; eed; i++,eed=BMIter_Step(&iter))
 +                      func(userData, i, eed->v1->co, eed->v2->co);
 +      }
 +
 +}
 +
 +static void bmDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) 
 +{
 +      EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +      BMEdge *eed;
 +      BMIter iter;
 +      int i;
 +      
 +      if (bmdm->vertexCos) {
 +              BMVert *eve;
 +              BMIter viter;
 +
 +              eve = BMIter_New(&viter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
 +              for (i=0; eve; eve=BMIter_Step(&viter), i++) {
 +                      BMINDEX_SET(eve, i);
 +              }
 +
 +              glBegin(GL_LINES);
 +              eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
 +              for(i=0; eed; i++,eed=BMIter_Step(&iter)) {
 +                      if(!setDrawOptions || setDrawOptions(userData, i)) {
 +                              glVertex3fv(bmdm->vertexCos[BMINDEX_GET(eed->v1)]);
 +                              glVertex3fv(bmdm->vertexCos[BMINDEX_GET(eed->v2)]);
 +                      }
 +              }
 +              glEnd();
 +
 +      } else {
 +              glBegin(GL_LINES);
 +              eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
 +              for(i=0; eed; i++,eed=BMIter_Step(&iter)) {
 +                      if(!setDrawOptions || setDrawOptions(userData, i)) {
 +                              glVertex3fv(eed->v1->co);
 +                              glVertex3fv(eed->v2->co);
 +                      }
 +              }
 +              glEnd();
 +      }
 +}
 +
 +static void bmDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
 +{
 +      bmDM_drawMappedEdges(dm, NULL, NULL);
 +}
 +
 +static void bmDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void (*setDrawInterpOptions)(void *userData, int index, float t), void *userData) 
 +{
 +      EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +      BMEdge *eed;
 +      BMIter iter;
 +      int i;
 +
 +      if (bmdm->vertexCos) {
 +              BMVert *eve;
 +
 +              eve = BMIter_New(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
 +              for (i=0; eve; eve=BMIter_Step(&iter), i++)
 +                      BMINDEX_SET(eve, i);
 +
 +              glBegin(GL_LINES);
 +              eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
 +              for(i=0; eed; i++,eed=BMIter_Step(&iter)) {
 +                      if(!setDrawOptions || setDrawOptions(userData, i)) {
 +                              setDrawInterpOptions(userData, i, 0.0);
 +                              glVertex3fv(bmdm->vertexCos[(int) BMINDEX_GET(eed->v1)]);
 +                              setDrawInterpOptions(userData, i, 1.0);
 +                              glVertex3fv(bmdm->vertexCos[(int) BMINDEX_GET(eed->v2)]);
 +                      }
 +              }
 +              glEnd();
 +      } else {
 +              glBegin(GL_LINES);
 +              eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
 +              for(i=0; eed; i++,eed=BMIter_Step(&iter)) {
 +                      if(!setDrawOptions || setDrawOptions(userData, i)) {
 +                              setDrawInterpOptions(userData, i, 0.0);
 +                              glVertex3fv(eed->v1->co);
 +                              setDrawInterpOptions(userData, i, 1.0);
 +                              glVertex3fv(eed->v2->co);
 +                      }
 +              }
 +              glEnd();
 +      }
 +}
 +
 +static void bmDM_drawUVEdges(DerivedMesh *dm)
 +{
-       MTFace *tf;
 +      EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
++      BMEditMesh *em = bmdm->tc;
 +      BMFace *efa;
-       for(efa= bmdm->tc->bm->faces.first; efa; efa= efa->next) {
-               tf = CustomData_bm_get(&bmdm->tc->bm->pdata, efa->data, CD_MTFACE);
-               if(tf && !(efa->h)) {
-                       glVertex2fv(tf->uv[0]);
-                       glVertex2fv(tf->uv[1]);
-                       glVertex2fv(tf->uv[1]);
-                       glVertex2fv(tf->uv[2]);
-                       if (!efa->v4) {
-                               glVertex2fv(tf->uv[2]);
-                               glVertex2fv(tf->uv[0]);
-                       } else {
-                               glVertex2fv(tf->uv[2]);
-                               glVertex2fv(tf->uv[3]);
-                               glVertex2fv(tf->uv[3]);
-                               glVertex2fv(tf->uv[0]);
++      BMIter iter;
 +
 +      glBegin(GL_LINES);
- #endif
++      BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
++              BMIter liter;
++              BMLoop *l;
++              MLoopUV *lastluv = NULL, *firstluv = NULL;
++              
++              if (BM_TestHFlag(efa, BM_HIDDEN))
++                      continue;
++              
++              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, efa) {
++                      MLoopUV *luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
++                      
++                      if (luv) {
++                              if (lastluv) 
++                                      glVertex2fv(luv->uv);
++                              glVertex2fv(luv->uv);
++                              
++                              lastluv = luv;
++                              if (!firstluv) 
++                                      firstluv = luv;
 +                      }
 +              }
++              
++              if (lastluv) {
++                      glVertex2fv(lastluv->uv);
++                      glVertex2fv(firstluv->uv);
++              }
 +      }
 +      glEnd();
- static void bmDM__calcFaceCent(BMesh *bm, BMFace *efa, float cent[3],
-                                float (*vertexCos)[3])
 +}
 +
-       VECMUL(cent, 1.0f/(float)tot);
++static void bmDM__calcFaceCent(BMesh *bm, BMFace *efa, float cent[3], 
++                                                         float (*vertexCos)[3])
 +{
 +      BMIter iter;
 +      BMLoop *l;
 +      int tot = 0;
 +      
 +      cent[0] = cent[1] = cent[2] = 0.0f;
 +      
 +      /*simple (and stupid) median (average) based method :/ */
 +      
 +      if (vertexCos) {
 +              l = BMIter_New(&iter, bm, BM_LOOPS_OF_FACE, efa);
 +              for (; l; l=BMIter_Step(&iter)) {
 +                      VECADD(cent, cent, vertexCos[BMINDEX_GET(l->v)]);
 +                      tot++;
 +              }
 +      } else {
 +              l = BMIter_New(&iter, bm, BM_LOOPS_OF_FACE, efa);
 +              for (; l; l=BMIter_Step(&iter)) {
 +                      VECADD(cent, cent, l->v->co);
 +                      tot++;
 +              }
 +      }
 +
 +      if (tot==0) return;
-       /* TODO what to do with vert_r->flag and vert_r->mat_nr? */
++      mul_v3_fl(cent, 1.0f/(float)tot);
 +}
 +
 +static void bmDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData)
 +{
 +      EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +      BMVert *eve;
 +      BMFace *efa;
 +      BMIter iter;
 +      float cent[3];
 +      int i;
 +
 +      if (bmdm->vertexCos) {
 +              eve = BMIter_New(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
 +              for (i=0; eve; eve=BMIter_Step(&iter), i++)
 +                      BMINDEX_SET(eve, i);
 +      }
 +
 +      efa = BMIter_New(&iter, bmdm->tc->bm, BM_FACES_OF_MESH, NULL);
 +      for (i=0; efa; efa=BMIter_Step(&iter), i++) {
 +              bmDM__calcFaceCent(bmdm->tc->bm, efa, cent, bmdm->vertexCos);
 +              func(userData, i, cent, bmdm->vertexCos?bmdm->faceNos[i]:efa->no);
 +      }
 +}
 +
 +static void bmDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors)
 +{
 +      EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +      BMFace *efa;
 +      BMIter iter;
 +      int i, draw;
 +
 +      if (bmdm->vertexCos) {
 +              BMVert *eve;
 +              
 +              eve = BMIter_New(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
 +              for (i=0; eve; eve=BMIter_Step(&iter), i++)
 +                      BMINDEX_SET(eve, i);
 +
 +              efa = BMIter_New(&iter, bmdm->tc->bm, BM_FACES_OF_MESH, NULL);
 +              for (i=0; efa; efa=BMIter_Step(&iter), i++)
 +                      BMINDEX_SET(efa, i);
 +
 +              for (i=0; i<bmdm->tc->tottri; i++) {
 +                      BMLoop **l = bmdm->tc->looptris[i];
 +                      int drawSmooth;
 +                      
 +                      efa = l[0]->f;
 +                      drawSmooth = (efa->head.flag & BM_SMOOTH);
 +
 +                      draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, BMINDEX_GET(efa), &drawSmooth);
 +                      if(draw) {
 +                              if (draw==2) { /* enabled with stipple */
 +                                      glEnable(GL_POLYGON_STIPPLE);
 +                                      glPolygonStipple(stipple_quarttone);
 +                              }
 +                              
 +                              glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
 +
 +                              glBegin(GL_TRIANGLES);
 +
 +                              if (!drawSmooth) {
 +                                      glNormal3fv(bmdm->faceNos[i]);
 +                                      glVertex3fv(bmdm->vertexCos[(int) BMINDEX_GET(l[0]->v)]);
 +                                      glVertex3fv(bmdm->vertexCos[(int) BMINDEX_GET(l[1]->v)]);
 +                                      glVertex3fv(bmdm->vertexCos[(int) BMINDEX_GET(l[2]->v)]);
 +                              } else {
 +                                      glNormal3fv(bmdm->vertexNos[(int) BMINDEX_GET(l[0]->v)]);
 +                                      glVertex3fv(bmdm->vertexCos[(int) BMINDEX_GET(l[0]->v)]);
 +                                      glNormal3fv(bmdm->vertexNos[(int) BMINDEX_GET(l[1]->v)]);
 +                                      glVertex3fv(bmdm->vertexCos[(int) BMINDEX_GET(l[1]->v)]);
 +                                      glNormal3fv(bmdm->vertexNos[(int) BMINDEX_GET(l[2]->v)]);
 +                                      glVertex3fv(bmdm->vertexCos[(int) BMINDEX_GET(l[2]->v)]);
 +                              }
 +                              glEnd();
 +
 +                              if (draw==2)
 +                                      glDisable(GL_POLYGON_STIPPLE);
 +                      }
 +              }
 +      } else {
 +              efa = BMIter_New(&iter, bmdm->tc->bm, BM_FACES_OF_MESH, NULL);
 +              for (i=0; efa; efa=BMIter_Step(&iter), i++)
 +                      BMINDEX_SET(efa, i);
 +
 +              for (i=0; i<bmdm->tc->tottri; i++) {
 +                      BMLoop **l = bmdm->tc->looptris[i];
 +                      int drawSmooth;
 +
 +                      efa = l[0]->f;
 +                      drawSmooth = (efa->head.flag & BM_SMOOTH);
 +                      
 +                      draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, BMINDEX_GET(efa), &drawSmooth);
 +                      if(draw) {
 +                              if (draw==2) { /* enabled with stipple */
 +                                      glEnable(GL_POLYGON_STIPPLE);
 +                                      glPolygonStipple(stipple_quarttone);
 +                              }
 +                              glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
 +                              
 +                              glBegin(GL_TRIANGLES);
 +                              if (!drawSmooth) {
 +                                      glNormal3fv(efa->no);
 +                                      glVertex3fv(l[0]->v->co);
 +                                      glVertex3fv(l[1]->v->co);
 +                                      glVertex3fv(l[2]->v->co);
 +                              } else {
 +                                      glNormal3fv(l[0]->v->no);
 +                                      glVertex3fv(l[0]->v->co);
 +                                      glNormal3fv(l[1]->v->no);
 +                                      glVertex3fv(l[1]->v->co);
 +                                      glNormal3fv(l[2]->v->no);
 +                                      glVertex3fv(l[2]->v->co);
 +                              }
 +                              glEnd();
 +                              
 +                              if (draw==2)
 +                                      glDisable(GL_POLYGON_STIPPLE);
 +                      }
 +              }
 +      }
 +}
 +
 +static void bmdm_get_tri_tex(BMesh *bm, BMLoop **ls, MLoopUV *luv[3], MLoopCol *lcol[3], 
 +                           int has_uv, int has_col)
 +{
 +      if (has_uv) { 
 +              luv[0] = CustomData_bmesh_get(&bm->ldata, ls[0]->head.data, CD_MLOOPUV);
 +              luv[1] = CustomData_bmesh_get(&bm->ldata, ls[1]->head.data, CD_MLOOPUV);
 +              luv[2] = CustomData_bmesh_get(&bm->ldata, ls[2]->head.data, CD_MLOOPUV);
 +      }
 +
 +      if (has_col) {
 +              lcol[0] = CustomData_bmesh_get(&bm->ldata, ls[0]->head.data, CD_MLOOPCOL);
 +              lcol[1] = CustomData_bmesh_get(&bm->ldata, ls[1]->head.data, CD_MLOOPCOL);
 +              lcol[2] = CustomData_bmesh_get(&bm->ldata, ls[2]->head.data, CD_MLOOPCOL);
 +      }
 +
 +
 +}
 +
 +static void bmDM_drawFacesTex_common(DerivedMesh *dm,
 +               int (*drawParams)(MTFace *tface, int has_vcol, int matnr),
 +               int (*drawParamsMapped)(void *userData, int index),
 +               void *userData) 
 +{
 +      EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +      BMEditMesh *em = bmdm->tc;
 +      BMesh *bm= bmdm->tc->bm;
 +      float (*vertexCos)[3]= bmdm->vertexCos;
 +      float (*vertexNos)[3]= bmdm->vertexNos;
 +      BMFace *efa;
 +      BMVert *eve;
 +      BMIter iter;
 +      MLoopUV *luv[3], dummyluv = {0};
 +      MLoopCol *lcol[3], dummylcol = {0};
 +      int i, has_vcol = CustomData_has_layer(&bm->ldata, CD_MLOOPCOL);
 +      int has_uv = CustomData_has_layer(&bm->pdata, CD_MTEXPOLY);
 +      
 +      luv[0] = luv[1] = luv[2] = &dummyluv;
 +      lcol[0] = lcol[1] = lcol[2] = &dummylcol;
 +
 +      dummylcol.a = dummylcol.r = dummylcol.g = dummylcol.b = 255;
 +
 +      /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
 +      glShadeModel(GL_SMOOTH);
 +      
 +      i = 0;
 +      BM_ITER(efa, &iter, bm, BM_FACES_OF_MESH, NULL)
 +              BMINDEX_SET(efa, i++);
 +
 +      if (vertexCos) {
 +              i = 0;
 +              BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL)
 +                      BMINDEX_SET(eve, i++);
 +                              
 +              glBegin(GL_TRIANGLES);
 +              for (i=0; i<em->tottri; i++) {
 +                      BMLoop **ls = em->looptris[i];
 +                      MTexPoly *tp= CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY);
 +                      MTFace mtf = {0};
 +                      unsigned char *cp= NULL;
 +                      int drawSmooth= BM_TestHFlag(ls[0]->f, BM_SMOOTH);
 +                      int flag;
 +
 +                      efa = ls[0]->f;
 +                      
 +                      if (has_uv) {
 +                              mtf.flag = tp->flag;
 +                              mtf.tpage = tp->tpage;
 +                              mtf.transp = tp->transp;
 +                              mtf.mode = tp->mode;
 +                              mtf.tile = tp->tile;
 +                              mtf.unwrap = tp->unwrap;
 +                      }
 +
 +                      if(drawParams)
 +                              flag= drawParams(&mtf, has_vcol, efa->mat_nr);
 +                      else if(drawParamsMapped)
 +                              flag= drawParamsMapped(userData, BMINDEX_GET(efa));
 +                      else
 +                              flag= 1;
 +
 +                      if(flag != 0) { /* flag 0 == the face is hidden or invisible */
 +                              
 +                              /* we always want smooth here since otherwise vertex colors dont interpolate */
 +                              if (!has_vcol) {
 +                                      glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
 +                              } 
 +                              
 +                              if (!drawSmooth) {
 +                                      glNormal3fv(bmdm->faceNos[i]);
 +                                      
 +                                      bmdm_get_tri_tex(bm, ls, luv, lcol, has_uv, has_vcol);
 +                                      
 +                                      glTexCoord2fv(luv[0]->uv);
 +                                      glColor3ub(lcol[0]->b, lcol[0]->g, lcol[0]->r);
 +                                      glVertex3fv(vertexCos[BMINDEX_GET(ls[0]->v)]);
 +
 +                                      glTexCoord2fv(luv[1]->uv);
 +                                      glColor3ub(lcol[1]->b, lcol[1]->g, lcol[1]->r);
 +                                      glVertex3fv(vertexCos[BMINDEX_GET(ls[1]->v)]);
 +
 +                                      glTexCoord2fv(luv[2]->uv);
 +                                      glColor3ub(lcol[2]->b, lcol[2]->g, lcol[2]->r);
 +                                      glVertex3fv(vertexCos[BMINDEX_GET(ls[2]->v)]);
 +                              } else {
 +                                      bmdm_get_tri_tex(bm, ls, luv, lcol, has_uv, has_vcol);
 +                                      
 +                                      glTexCoord2fv(luv[0]->uv);
 +                                      glColor3ub(lcol[0]->b, lcol[0]->g, lcol[0]->r);
 +                                      glNormal3fv(vertexNos[BMINDEX_GET(ls[0]->v)]);
 +                                      glVertex3fv(vertexCos[BMINDEX_GET(ls[0]->v)]);
 +
 +                                      glTexCoord2fv(luv[1]->uv);
 +                                      glColor3ub(lcol[1]->b, lcol[1]->g, lcol[1]->r);
 +                                      glNormal3fv(vertexNos[BMINDEX_GET(ls[1]->v)]);
 +                                      glVertex3fv(vertexCos[BMINDEX_GET(ls[1]->v)]);
 +
 +                                      glTexCoord2fv(luv[2]->uv);
 +                                      glColor3ub(lcol[2]->b, lcol[2]->g, lcol[2]->r);
 +                                      glNormal3fv(vertexNos[BMINDEX_GET(ls[2]->v)]);
 +                                      glVertex3fv(vertexCos[BMINDEX_GET(ls[2]->v)]);
 +                              }
 +                      }
 +              }
 +              glEnd();
 +      } else {
 +              i = 0;
 +              BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL)
 +                      BMINDEX_SET(eve, i++);
 +                              
 +              for (i=0; i<em->tottri; i++) {
 +                      BMLoop **ls = em->looptris[i];
 +                      MTexPoly *tp= CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY);
 +                      MTFace mtf = {0};
 +                      unsigned char *cp= NULL;
 +                      int drawSmooth= BM_TestHFlag(ls[0]->f, BM_SMOOTH);
 +                      int flag;
 +
 +                      efa = ls[0]->f;
 +                      
 +                      if (has_uv) {
 +                              mtf.flag = tp->flag;
 +                              mtf.tpage = tp->tpage;
 +                              mtf.transp = tp->transp;
 +                              mtf.mode = tp->mode;
 +                              mtf.tile = tp->tile;
 +                              mtf.unwrap = tp->unwrap;
 +                      }
 +
 +                      if(drawParams)
 +                              flag= drawParams(&mtf, has_vcol, efa->mat_nr);
 +                      else if(drawParamsMapped)
 +                              flag= drawParamsMapped(userData, BMINDEX_GET(efa));
 +                      else
 +                              flag= 1;
 +
 +                      if(flag != 0) { /* flag 0 == the face is hidden or invisible */
 +                              
 +                              /* we always want smooth here since otherwise vertex colors dont interpolate */
 +                              if (!has_vcol) {
 +                                      glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
 +                              } 
 +                              
 +                              glBegin(GL_TRIANGLES);
 +                              if (!drawSmooth) {
 +                                      glNormal3fv(efa->no);
 +                                      
 +                                      bmdm_get_tri_tex(bm, ls, luv, lcol, has_uv, has_vcol);
 +                                      
 +                                      if (luv[0])
 +                                              glTexCoord2fv(luv[0]->uv);
 +                                      if (lcol[0])
 +                                              glColor3ub(lcol[0]->b, lcol[0]->g, lcol[0]->r);
 +                                      else glColor3ub(0, 0, 0);
 +                                      glVertex3fv(ls[0]->v->co);
 +
 +                                      if (luv[1])
 +                                              glTexCoord2fv(luv[1]->uv);
 +                                      if (lcol[1])
 +                                              glColor3ub(lcol[1]->b, lcol[1]->g, lcol[1]->r);
 +                                      else glColor3ub(0, 0, 0);
 +                                      glVertex3fv(ls[1]->v->co);
 +
 +                                      if (luv[2])
 +                                              glTexCoord2fv(luv[2]->uv);
 +                                      if (lcol[2])
 +                                              glColor3ub(lcol[2]->b, lcol[2]->g, lcol[2]->r);
 +                                      else glColor3ub(0, 0, 0);
 +                                      glVertex3fv(ls[2]->v->co);
 +                              } else&