2011-02-23 10:52:22 +00:00
/*
2008-01-07 19:13:47 +00:00
* * * * * * BEGIN GPL LICENSE BLOCK * * * * *
2002-10-12 11:37:38 +00:00
*
* 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
2008-01-07 19:13:47 +00:00
* of the License , or ( at your option ) any later version .
2002-10-12 11:37:38 +00:00
*
* 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 ,
2010-02-12 13:34:04 +00:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
2002-10-12 11:37:38 +00:00
*
* The Original Code is Copyright ( C ) 2001 - 2002 by NaN Holding BV .
* All rights reserved .
*
* The Original Code is : all of this file .
*
* Contributor ( s ) : none yet .
*
2008-01-07 19:13:47 +00:00
* * * * * * END GPL LICENSE BLOCK * * * * *
2002-10-12 11:37:38 +00:00
*/
2011-02-25 13:30:41 +00:00
/** \file gameengine/Converter/KX_BlenderSceneConverter.cpp
* \ ingroup bgeconv
*/
2012-10-15 02:15:07 +00:00
# ifdef _MSC_VER
# pragma warning (disable:4786) /* suppress stl-MSVC debug info warning */
2002-10-12 11:37:38 +00:00
# endif
# include "KX_Scene.h"
# include "KX_GameObject.h"
# include "KX_IpoConvert.h"
# include "RAS_MeshObject.h"
# include "KX_PhysicsEngineEnums.h"
2005-08-22 18:31:19 +00:00
# include "PHY_IPhysicsEnvironment.h"
# include "KX_KetsjiEngine.h"
2012-06-21 05:41:06 +00:00
# include "KX_PythonInit.h" // So we can handle adding new text datablocks for Python to import
2006-01-06 03:46:54 +00:00
# include "BL_Material.h"
2010-10-09 10:45:25 +00:00
# include "BL_ActionActuator.h"
2009-11-15 23:58:56 +00:00
# include "KX_BlenderMaterial.h"
2011-05-06 20:18:42 +00:00
# include "BL_System.h"
2002-10-12 11:37:38 +00:00
# include "DummyPhysicsEnvironment.h"
2002-10-18 14:36:34 +00:00
2013-01-23 07:26:39 +00:00
# ifdef WITH_BULLET
2005-07-16 21:47:54 +00:00
# include "CcdPhysicsEnvironment.h"
# endif
2012-12-22 05:38:32 +00:00
# include "KX_LibLoadStatus.h"
2002-10-12 11:37:38 +00:00
# include "KX_BlenderScalarInterpolator.h"
# include "BL_BlenderDataConversion.h"
2015-03-24 00:19:15 +01:00
# include "KX_WorldInfo.h"
2002-10-12 11:37:38 +00:00
/* This little block needed for linking to Blender... */
# ifdef WIN32
# include "BLI_winstuff.h"
# endif
/* This list includes only data type definitions */
# include "DNA_scene_types.h"
# include "DNA_world_types.h"
# include "BKE_main.h"
2015-01-09 14:59:39 +01:00
# include "BKE_fcurve.h"
2002-10-12 11:37:38 +00:00
2009-11-10 20:43:45 +00:00
# include "BLI_math.h"
2006-12-16 05:50:38 +00:00
2005-08-23 13:16:02 +00:00
extern " C "
{
2005-08-22 18:31:19 +00:00
# include "DNA_object_types.h"
# include "DNA_curve_types.h"
2009-11-15 23:58:56 +00:00
# include "DNA_mesh_types.h"
# include "DNA_material_types.h"
2005-08-23 13:16:02 +00:00
# include "BLI_blenlib.h"
# include "MEM_guardedalloc.h"
# include "BKE_global.h"
2010-02-22 12:25:58 +00:00
# include "BKE_animsys.h"
2009-11-15 23:58:56 +00:00
# include "BKE_library.h"
2012-05-05 14:03:12 +00:00
# include "BKE_material.h" // BKE_material_copy
# include "BKE_mesh.h" // BKE_mesh_copy
2005-08-23 13:16:02 +00:00
# include "DNA_space_types.h"
2010-02-22 12:25:58 +00:00
# include "DNA_anim_types.h"
2015-01-09 14:59:39 +01:00
# include "DNA_action_types.h"
2010-03-16 10:18:19 +00:00
# include "RNA_define.h"
2010-02-22 12:25:58 +00:00
# include "../../blender/editors/include/ED_keyframing.h"
2005-08-23 13:16:02 +00:00
}
2005-08-22 18:31:19 +00:00
2009-11-15 23:58:56 +00:00
/* Only for dynamic loading and merging */
# include "RAS_BucketManager.h" // XXX cant stay
# include "KX_BlenderSceneConverter.h"
# include "KX_MeshProxy.h"
extern " C " {
2012-11-10 05:42:50 +00:00
# include "PIL_time.h"
2009-11-15 23:58:56 +00:00
# include "BKE_context.h"
# include "BLO_readfile.h"
2010-08-10 15:14:19 +00:00
# include "BKE_idcode.h"
2009-11-15 23:58:56 +00:00
# include "BKE_report.h"
# include "DNA_space_types.h"
# include "DNA_windowmanager_types.h" /* report api */
# include "../../blender/blenlib/BLI_linklist.h"
}
2002-10-12 11:37:38 +00:00
2015-10-08 21:37:03 +02:00
# include "BLI_task.h"
2012-12-22 05:38:32 +00:00
2015-10-08 21:37:03 +02:00
// This is used to avoid including BLI_task.h in KX_BlenderSceneConverter.h
2012-12-22 05:38:32 +00:00
typedef struct ThreadInfo {
2015-10-08 21:37:03 +02:00
TaskPool * m_pool ;
ThreadMutex m_mutex ;
2012-12-22 05:38:32 +00:00
} ThreadInfo ;
2002-10-12 11:37:38 +00:00
KX_BlenderSceneConverter : : KX_BlenderSceneConverter (
2015-05-04 13:32:28 +02:00
Main * maggie ,
KX_KetsjiEngine * engine )
: m_maggie ( maggie ) ,
2002-10-12 11:37:38 +00:00
m_ketsjiEngine ( engine ) ,
2006-01-06 03:46:54 +00:00
m_alwaysUseExpandFraming ( false ) ,
Merge of apricot branch game engine changes into trunk, excluding GLSL.
GLEW
====
Added the GLEW opengl extension library into extern/, always compiled
into Blender now. This is much nicer than doing this kind of extension
management manually, and will be used in the game engine, for GLSL, and
other opengl extensions.
* According to the GLEW website it works on Windows, Linux, Mac OS X,
FreeBSD, Irix, and Solaris. There might still be platform specific
issues due to this commit, so let me know and I'll look into it.
* This means also that all extensions will now always be compiled in,
regardless of the glext.h on the platform where compilation happens.
Game Engine
===========
Refactoring of the use of opengl extensions and other drawing code
in the game engine, and cleaning up some hacks related to GLSL
integration. These changes will be merged into trunk too after this.
The game engine graphics demos & apricot level survived my tests,
but this could use some good testing of course.
For users: please test with the options "Generate Display Lists" and
"Vertex Arrays" enabled, these should be the fastest and are supposed
to be "unreliable", but if that's the case that's probably due to bugs
that can be fixed.
* The game engine now also uses GLEW for extensions, replacing the
custom opengl extensions code that was there. Removes a lot of
#ifdef's, but the runtime checks stay of course.
* Removed the WITHOUT_GLEXT environment variable. This was added to
work around a specific bug and only disabled multitexturing anyway.
It might also have caused a slowdown since it was retrieving the
environment variable for every vertex in immediate mode (bug #13680).
* Refactored the code to allow drawing skinned meshes with vertex
arrays too, removing some specific immediate mode drawing functions
for this that only did extra normal calculation. Now it always splits
vertices of flat faces instead.
* Refactored normal recalculation with some minor optimizations,
required for the above change.
* Removed some outdated code behind the __NLA_OLDDEFORM #ifdef.
* Fixed various bugs in setting of multitexture coordinates and vertex
attributes for vertex arrays. These were not being enabled/disabled
correct according to the opengl spec, leading to crashes. Also tangent
attributes used an immediate mode call for vertex arrays, which can't
work.
* Fixed use of uninitialized variable in RAS_TexVert.
* Exporting skinned meshes was doing O(n^2) lookups for vertices and
deform weights, now uses same trick as regular meshes.
2008-06-17 10:27:34 +00:00
m_usemat ( false ) ,
2012-12-24 03:13:53 +00:00
m_useglslmat ( false ) ,
m_use_mat_cache ( true )
2002-10-12 11:37:38 +00:00
{
2013-12-30 13:25:27 +11:00
BKE_main_id_tag_all ( maggie , false ) ; /* avoid re-tagging later on */
2002-10-12 11:37:38 +00:00
m_newfilename = " " ;
2012-12-22 05:38:32 +00:00
m_threadinfo = new ThreadInfo ( ) ;
2015-10-08 21:37:03 +02:00
m_threadinfo - > m_pool = BLI_task_pool_create ( engine - > GetTaskScheduler ( ) , NULL ) ;
BLI_mutex_init ( & m_threadinfo - > m_mutex ) ;
2002-10-12 11:37:38 +00:00
}
KX_BlenderSceneConverter : : ~ KX_BlenderSceneConverter ( )
{
// clears meshes, and hashmaps from blender to gameengine data
// delete sumoshapes
2015-05-04 13:32:28 +02:00
2012-12-22 05:38:32 +00:00
if ( m_threadinfo ) {
2015-10-08 21:37:03 +02:00
BLI_task_pool_work_and_wait ( m_threadinfo - > m_pool ) ;
BLI_task_pool_free ( m_threadinfo - > m_pool ) ;
2012-12-22 05:38:32 +00:00
2015-10-08 21:37:03 +02:00
BLI_mutex_end ( & m_threadinfo - > m_mutex ) ;
2012-12-22 05:38:32 +00:00
delete m_threadinfo ;
}
2002-10-12 11:37:38 +00:00
2009-06-15 10:11:08 +00:00
int numAdtLists = m_map_blender_to_gameAdtList . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( int i = 0 ; i < numAdtLists ; i + + ) {
BL_InterpolatorList * adtList = * m_map_blender_to_gameAdtList . at ( i ) ;
2002-10-12 11:37:38 +00:00
2009-06-15 10:11:08 +00:00
delete ( adtList ) ;
2002-10-12 11:37:38 +00:00
}
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , KX_WorldInfo * > > : : iterator itw = m_worldinfos . begin ( ) ;
2002-10-12 11:37:38 +00:00
while ( itw ! = m_worldinfos . end ( ) ) {
2015-07-09 09:45:21 +02:00
delete itw - > second ;
2002-10-12 11:37:38 +00:00
itw + + ;
}
2013-06-20 06:17:28 +00:00
m_worldinfos . clear ( ) ;
2002-10-12 11:37:38 +00:00
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_IPolyMaterial * > > : : iterator itp = m_polymaterials . begin ( ) ;
2002-10-12 11:37:38 +00:00
while ( itp ! = m_polymaterials . end ( ) ) {
2015-07-09 09:45:21 +02:00
delete itp - > second ;
2002-10-12 11:37:38 +00:00
itp + + ;
}
2013-06-20 06:17:28 +00:00
m_polymaterials . clear ( ) ;
2006-01-06 03:46:54 +00:00
// delete after RAS_IPolyMaterial
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , BL_Material * > > : : iterator itmat = m_materials . begin ( ) ;
2006-01-06 03:46:54 +00:00
while ( itmat ! = m_materials . end ( ) ) {
2015-07-09 09:45:21 +02:00
delete itmat - > second ;
2006-01-06 03:46:54 +00:00
itmat + + ;
2012-09-16 04:58:18 +00:00
}
2013-06-20 06:17:28 +00:00
m_materials . clear ( ) ;
2006-01-06 03:46:54 +00:00
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_MeshObject * > > : : iterator itm = m_meshobjects . begin ( ) ;
2002-10-12 11:37:38 +00:00
while ( itm ! = m_meshobjects . end ( ) ) {
2015-07-09 09:45:21 +02:00
delete itm - > second ;
2002-10-12 11:37:38 +00:00
itm + + ;
}
2013-06-20 06:17:28 +00:00
m_meshobjects . clear ( ) ;
2005-07-16 21:47:54 +00:00
2009-11-15 23:58:56 +00:00
/* free any data that was dynamically loaded */
2015-05-04 13:32:28 +02:00
while ( m_DynamicMaggie . size ( ) ! = 0 ) {
2012-06-21 05:41:06 +00:00
FreeBlendFile ( m_DynamicMaggie [ 0 ] ) ;
2009-11-15 23:58:56 +00:00
}
2002-10-12 11:37:38 +00:00
2009-11-15 23:58:56 +00:00
m_DynamicMaggie . clear ( ) ;
}
2002-10-12 11:37:38 +00:00
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : SetNewFileName ( const STR_String & filename )
2002-10-12 11:37:38 +00:00
{
m_newfilename = filename ;
}
bool KX_BlenderSceneConverter : : TryAndLoadNewFile ( )
{
bool result = false ;
return result ;
}
2015-05-04 13:32:28 +02:00
Scene * KX_BlenderSceneConverter : : GetBlenderSceneForName ( const STR_String & name )
Merge of first part of changes from the apricot branch, especially
the features that are needed to run the game. Compile tested with
scons, make, but not cmake, that seems to have an issue not related
to these changes. The changes include:
* GLSL support in the viewport and game engine, enable in the game
menu in textured draw mode.
* Synced and merged part of the duplicated blender and gameengine/
gameplayer drawing code.
* Further refactoring of game engine drawing code, especially mesh
storage changed a lot.
* Optimizations in game engine armatures to avoid recomputations.
* A python function to get the framerate estimate in game.
* An option take object color into account in materials.
* An option to restrict shadow casters to a lamp's layers.
* Increase from 10 to 18 texture slots for materials, lamps, word.
An extra texture slot shows up once the last slot is used.
* Memory limit for undo, not enabled by default yet because it
needs the .B.blend to be changed.
* Multiple undo for image painting.
* An offset for dupligroups, so not all objects in a group have to
be at the origin.
2008-09-04 20:51:28 +00:00
{
Scene * sce ;
2002-10-12 11:37:38 +00:00
/**
2014-03-24 17:57:02 -07:00
* Find the specified scene by name , or NULL if nothing matches .
2002-10-12 11:37:38 +00:00
*/
2015-05-04 13:32:28 +02:00
if ( ( sce = ( Scene * ) BLI_findstring ( & m_maggie - > scene , name . ReadPtr ( ) , offsetof ( ID , name ) + 2 ) ) )
2010-02-22 09:22:36 +00:00
return sce ;
2002-10-12 11:37:38 +00:00
2015-05-04 13:32:28 +02:00
for ( vector < Main * > : : iterator it = m_DynamicMaggie . begin ( ) ; ! ( it = = m_DynamicMaggie . end ( ) ) ; it + + ) {
Main * main = * it ;
2009-11-15 23:58:56 +00:00
2012-03-24 07:52:14 +00:00
if ( ( sce = ( Scene * ) BLI_findstring ( & main - > scene , name . ReadPtr ( ) , offsetof ( ID , name ) + 2 ) ) )
2010-02-22 09:22:36 +00:00
return sce ;
2009-11-15 23:58:56 +00:00
}
2014-03-24 17:57:02 -07:00
return NULL ;
2002-10-12 11:37:38 +00:00
}
2005-03-25 10:33:39 +00:00
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : ConvertScene ( KX_Scene * destinationscene , RAS_IRasterizer * rendertools ,
RAS_ICanvas * canvas , bool libloading )
2002-10-12 11:37:38 +00:00
{
2004-04-03 00:04:44 +00:00
//find out which physics engine
2009-07-30 21:42:29 +00:00
Scene * blenderscene = destinationscene - > GetBlenderScene ( ) ;
2002-10-12 11:37:38 +00:00
2014-04-23 17:53:25 -07:00
PHY_IPhysicsEnvironment * phy_env = NULL ;
2006-04-27 04:37:20 +00:00
e_PhysicsEngine physics_engine = UseBullet ;
2008-03-09 21:42:03 +00:00
// hook for registration function during conversion.
m_currentScene = destinationscene ;
destinationscene - > SetSceneConverter ( this ) ;
2012-12-22 05:38:32 +00:00
// This doesn't really seem to do anything except cause potential issues
// when doing threaded conversion, so it's disabled for now.
// SG_SetActiveStage(SG_STAGE_CONVERTER);
2002-10-12 11:37:38 +00:00
2015-05-04 13:32:28 +02:00
switch ( blenderscene - > gm . physicsEngine ) {
2014-04-23 17:53:25 -07:00
# ifdef WITH_BULLET
case WOPHY_BULLET :
2002-10-12 11:37:38 +00:00
{
2014-04-23 17:53:25 -07:00
SYS_SystemHandle syshandle = SYS_GetSystem ( ) ; /*unused*/
2015-05-04 13:32:28 +02:00
int visualizePhysics = SYS_GetCommandLineInt ( syshandle , " show_physics " , 0 ) ;
2005-03-25 10:33:39 +00:00
2014-04-23 17:53:25 -07:00
phy_env = CcdPhysicsEnvironment : : Create ( blenderscene , visualizePhysics ) ;
physics_engine = UseBullet ;
break ;
}
2012-04-23 08:05:02 +00:00
# endif
2014-04-23 17:53:25 -07:00
default :
case WOPHY_NONE :
{
// We should probably use some sort of factory here
phy_env = new DummyPhysicsEnvironment ( ) ;
2004-04-03 00:04:44 +00:00
physics_engine = UseNone ;
break ;
2014-04-23 17:53:25 -07:00
}
2002-10-12 11:37:38 +00:00
}
2014-04-23 17:53:25 -07:00
destinationscene - > SetPhysicsEnvironment ( phy_env ) ;
2015-05-04 13:32:28 +02:00
BL_ConvertBlenderObjects (
m_maggie ,
2004-04-03 00:04:44 +00:00
destinationscene ,
m_ketsjiEngine ,
physics_engine ,
rendertools ,
canvas ,
this ,
2012-12-18 20:56:25 +00:00
m_alwaysUseExpandFraming ,
2015-05-04 13:32:28 +02:00
libloading ) ;
2004-04-03 00:04:44 +00:00
2008-03-09 21:42:03 +00:00
//These lookup are not needed during game
2004-04-03 00:04:44 +00:00
m_map_blender_to_gameactuator . clear ( ) ;
m_map_blender_to_gamecontroller . clear ( ) ;
m_map_blender_to_gameobject . clear ( ) ;
2005-08-22 18:31:19 +00:00
2008-03-09 21:42:03 +00:00
//Clearing this lookup table has the effect of disabling the cache of meshes
//between scenes, even if they are shared in the blend file.
//This cache mecanism is buggy so I leave it disable and the memory leak
//that would result from this is fixed in RemoveScene()
m_map_mesh_to_gamemesh . clear ( ) ;
2002-10-12 11:37:38 +00:00
}
2008-03-09 21:42:03 +00:00
// This function removes all entities stored in the converter for that scene
// It should be used instead of direct delete scene
// Note that there was some provision for sharing entities (meshes...) between
// scenes but that is now disabled so all scene will have their own copy
// and we can delete them here. If the sharing is reactivated, change this code too..
// (see KX_BlenderSceneConverter::ConvertScene)
void KX_BlenderSceneConverter : : RemoveScene ( KX_Scene * scene )
{
int i , size ;
// delete the scene first as it will stop the use of entities
delete scene ;
// delete the entities of this scene
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , KX_WorldInfo * > > : : iterator worldit ;
2008-03-09 21:42:03 +00:00
size = m_worldinfos . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( i = 0 , worldit = m_worldinfos . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
if ( worldit - > first = = scene ) {
delete worldit - > second ;
2008-03-09 21:42:03 +00:00
* worldit = m_worldinfos . back ( ) ;
m_worldinfos . pop_back ( ) ;
size - - ;
2015-05-04 13:32:28 +02:00
}
else {
2008-03-09 21:42:03 +00:00
i + + ;
worldit + + ;
}
}
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_IPolyMaterial * > > : : iterator polymit ;
2008-03-09 21:42:03 +00:00
size = m_polymaterials . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( i = 0 , polymit = m_polymaterials . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
if ( polymit - > first = = scene ) {
m_polymat_cache [ scene ] . erase ( polymit - > second - > GetBlenderMaterial ( ) ) ;
delete polymit - > second ;
2008-03-09 21:42:03 +00:00
* polymit = m_polymaterials . back ( ) ;
m_polymaterials . pop_back ( ) ;
size - - ;
2015-05-04 13:32:28 +02:00
}
else {
2008-03-09 21:42:03 +00:00
i + + ;
polymit + + ;
}
}
2013-07-15 22:35:09 +00:00
m_polymat_cache . erase ( scene ) ;
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , BL_Material * > > : : iterator matit ;
2008-03-09 21:42:03 +00:00
size = m_materials . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( i = 0 , matit = m_materials . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
if ( matit - > first = = scene ) {
m_mat_cache [ scene ] . erase ( matit - > second - > material ) ;
delete matit - > second ;
2008-03-09 21:42:03 +00:00
* matit = m_materials . back ( ) ;
m_materials . pop_back ( ) ;
size - - ;
2015-05-04 13:32:28 +02:00
}
else {
2008-03-09 21:42:03 +00:00
i + + ;
matit + + ;
}
}
2013-07-15 22:35:09 +00:00
m_mat_cache . erase ( scene ) ;
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_MeshObject * > > : : iterator meshit ;
2008-03-09 21:42:03 +00:00
size = m_meshobjects . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( i = 0 , meshit = m_meshobjects . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
if ( meshit - > first = = scene ) {
delete meshit - > second ;
2008-03-09 21:42:03 +00:00
* meshit = m_meshobjects . back ( ) ;
m_meshobjects . pop_back ( ) ;
size - - ;
2015-05-04 13:32:28 +02:00
}
else {
2008-03-09 21:42:03 +00:00
i + + ;
meshit + + ;
}
}
}
2002-10-12 11:37:38 +00:00
2006-01-06 03:46:54 +00:00
// use blender materials
void KX_BlenderSceneConverter : : SetMaterials ( bool val )
{
m_usemat = val ;
Merge of apricot branch game engine changes into trunk, excluding GLSL.
GLEW
====
Added the GLEW opengl extension library into extern/, always compiled
into Blender now. This is much nicer than doing this kind of extension
management manually, and will be used in the game engine, for GLSL, and
other opengl extensions.
* According to the GLEW website it works on Windows, Linux, Mac OS X,
FreeBSD, Irix, and Solaris. There might still be platform specific
issues due to this commit, so let me know and I'll look into it.
* This means also that all extensions will now always be compiled in,
regardless of the glext.h on the platform where compilation happens.
Game Engine
===========
Refactoring of the use of opengl extensions and other drawing code
in the game engine, and cleaning up some hacks related to GLSL
integration. These changes will be merged into trunk too after this.
The game engine graphics demos & apricot level survived my tests,
but this could use some good testing of course.
For users: please test with the options "Generate Display Lists" and
"Vertex Arrays" enabled, these should be the fastest and are supposed
to be "unreliable", but if that's the case that's probably due to bugs
that can be fixed.
* The game engine now also uses GLEW for extensions, replacing the
custom opengl extensions code that was there. Removes a lot of
#ifdef's, but the runtime checks stay of course.
* Removed the WITHOUT_GLEXT environment variable. This was added to
work around a specific bug and only disabled multitexturing anyway.
It might also have caused a slowdown since it was retrieving the
environment variable for every vertex in immediate mode (bug #13680).
* Refactored the code to allow drawing skinned meshes with vertex
arrays too, removing some specific immediate mode drawing functions
for this that only did extra normal calculation. Now it always splits
vertices of flat faces instead.
* Refactored normal recalculation with some minor optimizations,
required for the above change.
* Removed some outdated code behind the __NLA_OLDDEFORM #ifdef.
* Fixed various bugs in setting of multitexture coordinates and vertex
attributes for vertex arrays. These were not being enabled/disabled
correct according to the opengl spec, leading to crashes. Also tangent
attributes used an immediate mode call for vertex arrays, which can't
work.
* Fixed use of uninitialized variable in RAS_TexVert.
* Exporting skinned meshes was doing O(n^2) lookups for vertices and
deform weights, now uses same trick as regular meshes.
2008-06-17 10:27:34 +00:00
m_useglslmat = false ;
}
void KX_BlenderSceneConverter : : SetGLSLMaterials ( bool val )
{
m_usemat = val ;
m_useglslmat = val ;
2006-01-06 03:46:54 +00:00
}
2012-12-24 03:13:53 +00:00
void KX_BlenderSceneConverter : : SetCacheMaterials ( bool val )
{
m_use_mat_cache = val ;
}
2006-01-06 03:46:54 +00:00
bool KX_BlenderSceneConverter : : GetMaterials ( )
{
return m_usemat ;
}
Merge of apricot branch game engine changes into trunk, excluding GLSL.
GLEW
====
Added the GLEW opengl extension library into extern/, always compiled
into Blender now. This is much nicer than doing this kind of extension
management manually, and will be used in the game engine, for GLSL, and
other opengl extensions.
* According to the GLEW website it works on Windows, Linux, Mac OS X,
FreeBSD, Irix, and Solaris. There might still be platform specific
issues due to this commit, so let me know and I'll look into it.
* This means also that all extensions will now always be compiled in,
regardless of the glext.h on the platform where compilation happens.
Game Engine
===========
Refactoring of the use of opengl extensions and other drawing code
in the game engine, and cleaning up some hacks related to GLSL
integration. These changes will be merged into trunk too after this.
The game engine graphics demos & apricot level survived my tests,
but this could use some good testing of course.
For users: please test with the options "Generate Display Lists" and
"Vertex Arrays" enabled, these should be the fastest and are supposed
to be "unreliable", but if that's the case that's probably due to bugs
that can be fixed.
* The game engine now also uses GLEW for extensions, replacing the
custom opengl extensions code that was there. Removes a lot of
#ifdef's, but the runtime checks stay of course.
* Removed the WITHOUT_GLEXT environment variable. This was added to
work around a specific bug and only disabled multitexturing anyway.
It might also have caused a slowdown since it was retrieving the
environment variable for every vertex in immediate mode (bug #13680).
* Refactored the code to allow drawing skinned meshes with vertex
arrays too, removing some specific immediate mode drawing functions
for this that only did extra normal calculation. Now it always splits
vertices of flat faces instead.
* Refactored normal recalculation with some minor optimizations,
required for the above change.
* Removed some outdated code behind the __NLA_OLDDEFORM #ifdef.
* Fixed various bugs in setting of multitexture coordinates and vertex
attributes for vertex arrays. These were not being enabled/disabled
correct according to the opengl spec, leading to crashes. Also tangent
attributes used an immediate mode call for vertex arrays, which can't
work.
* Fixed use of uninitialized variable in RAS_TexVert.
* Exporting skinned meshes was doing O(n^2) lookups for vertices and
deform weights, now uses same trick as regular meshes.
2008-06-17 10:27:34 +00:00
bool KX_BlenderSceneConverter : : GetGLSLMaterials ( )
{
return m_useglslmat ;
}
2006-01-06 03:46:54 +00:00
2012-12-24 03:13:53 +00:00
bool KX_BlenderSceneConverter : : GetCacheMaterials ( )
{
return m_use_mat_cache ;
}
2006-01-06 03:46:54 +00:00
void KX_BlenderSceneConverter : : RegisterBlenderMaterial ( BL_Material * mat )
{
2012-12-18 20:56:25 +00:00
// First make sure we don't register the material twice
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , BL_Material * > > : : iterator it ;
2012-12-18 20:56:25 +00:00
for ( it = m_materials . begin ( ) ; it ! = m_materials . end ( ) ; + + it )
if ( it - > second = = mat )
return ;
2015-05-04 13:32:28 +02:00
m_materials . push_back ( pair < KX_Scene * , BL_Material * > ( m_currentScene , mat ) ) ;
2006-01-06 03:46:54 +00:00
}
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : SetAlwaysUseExpandFraming ( bool to_what )
2002-10-12 11:37:38 +00:00
{
m_alwaysUseExpandFraming = to_what ;
}
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : RegisterGameObject ( KX_GameObject * gameobject , Object * for_blenderobject )
2002-10-12 11:37:38 +00:00
{
2009-08-07 03:51:28 +00:00
/* only maintained while converting, freed during game runtime */
2015-05-04 13:32:28 +02:00
m_map_blender_to_gameobject . insert ( CHashedPtr ( for_blenderobject ) , gameobject ) ;
2002-10-12 11:37:38 +00:00
}
2009-08-21 03:06:36 +00:00
/* only need to run this during conversion since
* m_map_blender_to_gameobject is freed after conversion */
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : UnregisterGameObject ( KX_GameObject * gameobject )
2008-03-09 21:42:03 +00:00
{
2015-05-04 13:32:28 +02:00
Object * bobp = gameobject - > GetBlenderObject ( ) ;
2008-09-07 10:47:33 +00:00
if ( bobp ) {
2009-08-07 03:51:28 +00:00
CHashedPtr bptr ( bobp ) ;
2015-05-04 13:32:28 +02:00
KX_GameObject * * gobp = m_map_blender_to_gameobject [ bptr ] ;
if ( gobp & & * gobp = = gameobject ) {
2008-09-07 10:47:33 +00:00
// also maintain m_map_blender_to_gameobject if the gameobject
// being removed is matching the blender object
m_map_blender_to_gameobject . remove ( bptr ) ;
2009-08-07 03:51:28 +00:00
}
2008-09-07 10:47:33 +00:00
}
2008-03-09 21:42:03 +00:00
}
2002-10-12 11:37:38 +00:00
2015-05-04 13:32:28 +02:00
KX_GameObject * KX_BlenderSceneConverter : : FindGameObject ( Object * for_blenderobject )
2002-10-12 11:37:38 +00:00
{
2015-05-04 13:32:28 +02:00
KX_GameObject * * obp = m_map_blender_to_gameobject [ CHashedPtr ( for_blenderobject ) ] ;
return obp ? * obp : NULL ;
2002-10-12 11:37:38 +00:00
}
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : RegisterGameMesh ( RAS_MeshObject * gamemesh , Mesh * for_blendermesh )
2002-10-12 11:37:38 +00:00
{
2012-03-24 07:52:14 +00:00
if ( for_blendermesh ) { /* dynamically loaded meshes we don't want to keep lookups for */
2009-11-15 23:58:56 +00:00
m_map_mesh_to_gamemesh . insert ( CHashedPtr ( for_blendermesh ) , gamemesh ) ;
}
2015-05-04 13:32:28 +02:00
m_meshobjects . push_back ( pair < KX_Scene * , RAS_MeshObject * > ( m_currentScene , gamemesh ) ) ;
2002-10-12 11:37:38 +00:00
}
2015-05-04 13:32:28 +02:00
RAS_MeshObject * KX_BlenderSceneConverter : : FindGameMesh ( Mesh * for_blendermesh )
2002-10-12 11:37:38 +00:00
{
2015-05-04 13:32:28 +02:00
RAS_MeshObject * * meshp = m_map_mesh_to_gamemesh [ CHashedPtr ( for_blendermesh ) ] ;
if ( meshp ) {
2002-10-12 11:37:38 +00:00
return * meshp ;
2015-05-04 13:32:28 +02:00
}
else {
2002-10-12 11:37:38 +00:00
return NULL ;
}
2015-05-04 13:32:28 +02:00
}
2002-10-12 11:37:38 +00:00
void KX_BlenderSceneConverter : : RegisterPolyMaterial ( RAS_IPolyMaterial * polymat )
{
2012-12-18 20:56:25 +00:00
// First make sure we don't register the material twice
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_IPolyMaterial * > > : : iterator it ;
2012-12-18 20:56:25 +00:00
for ( it = m_polymaterials . begin ( ) ; it ! = m_polymaterials . end ( ) ; + + it )
if ( it - > second = = polymat )
return ;
2015-05-04 13:32:28 +02:00
m_polymaterials . push_back ( pair < KX_Scene * , RAS_IPolyMaterial * > ( m_currentScene , polymat ) ) ;
2002-10-12 11:37:38 +00:00
}
2013-07-15 22:35:09 +00:00
void KX_BlenderSceneConverter : : CachePolyMaterial ( KX_Scene * scene , Material * mat , RAS_IPolyMaterial * polymat )
2012-12-18 20:56:25 +00:00
{
2013-05-11 03:20:19 +00:00
if ( m_use_mat_cache & & mat )
2013-07-15 22:35:09 +00:00
m_polymat_cache [ scene ] [ mat ] = polymat ;
2012-12-18 20:56:25 +00:00
}
2015-05-04 13:32:28 +02:00
RAS_IPolyMaterial * KX_BlenderSceneConverter : : FindCachedPolyMaterial ( KX_Scene * scene , Material * mat )
2012-12-18 20:56:25 +00:00
{
2013-07-15 22:35:09 +00:00
return ( m_use_mat_cache ) ? m_polymat_cache [ scene ] [ mat ] : NULL ;
2012-12-18 20:56:25 +00:00
}
2002-10-12 11:37:38 +00:00
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : CacheBlenderMaterial ( KX_Scene * scene , Material * mat , BL_Material * blmat )
2012-12-18 20:56:25 +00:00
{
2013-05-11 03:20:19 +00:00
if ( m_use_mat_cache & & mat )
2013-07-15 22:35:09 +00:00
m_mat_cache [ scene ] [ mat ] = blmat ;
2012-12-18 20:56:25 +00:00
}
2015-05-04 13:32:28 +02:00
BL_Material * KX_BlenderSceneConverter : : FindCachedBlenderMaterial ( KX_Scene * scene , Material * mat )
2012-12-18 20:56:25 +00:00
{
2013-07-15 22:35:09 +00:00
return ( m_use_mat_cache ) ? m_mat_cache [ scene ] [ mat ] : NULL ;
2012-12-18 20:56:25 +00:00
}
2002-10-12 11:37:38 +00:00
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : RegisterInterpolatorList ( BL_InterpolatorList * actList , bAction * for_act )
2002-10-12 11:37:38 +00:00
{
2011-08-07 04:57:23 +00:00
m_map_blender_to_gameAdtList . insert ( CHashedPtr ( for_act ) , actList ) ;
2002-10-12 11:37:38 +00:00
}
2015-05-04 13:32:28 +02:00
BL_InterpolatorList * KX_BlenderSceneConverter : : FindInterpolatorList ( bAction * for_act )
2002-10-12 11:37:38 +00:00
{
2011-08-07 04:57:23 +00:00
BL_InterpolatorList * * listp = m_map_blender_to_gameAdtList [ CHashedPtr ( for_act ) ] ;
2015-05-04 13:32:28 +02:00
return listp ? * listp : NULL ;
2002-10-12 11:37:38 +00:00
}
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : RegisterGameActuator ( SCA_IActuator * act , bActuator * for_actuator )
2002-10-12 11:37:38 +00:00
{
m_map_blender_to_gameactuator . insert ( CHashedPtr ( for_actuator ) , act ) ;
}
2015-05-04 13:32:28 +02:00
SCA_IActuator * KX_BlenderSceneConverter : : FindGameActuator ( bActuator * for_actuator )
2002-10-12 11:37:38 +00:00
{
SCA_IActuator * * actp = m_map_blender_to_gameactuator [ CHashedPtr ( for_actuator ) ] ;
2015-05-04 13:32:28 +02:00
return actp ? * actp : NULL ;
2002-10-12 11:37:38 +00:00
}
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : RegisterGameController ( SCA_IController * cont , bController * for_controller )
2002-10-12 11:37:38 +00:00
{
m_map_blender_to_gamecontroller . insert ( CHashedPtr ( for_controller ) , cont ) ;
}
2015-05-04 13:32:28 +02:00
SCA_IController * KX_BlenderSceneConverter : : FindGameController ( bController * for_controller )
2002-10-12 11:37:38 +00:00
{
SCA_IController * * contp = m_map_blender_to_gamecontroller [ CHashedPtr ( for_controller ) ] ;
2015-05-04 13:32:28 +02:00
return contp ? * contp : NULL ;
2002-10-12 11:37:38 +00:00
}
2014-08-03 19:13:06 +02:00
void KX_BlenderSceneConverter : : RegisterWorldInfo ( KX_WorldInfo * worldinfo )
2002-10-12 11:37:38 +00:00
{
2015-05-04 13:32:28 +02:00
m_worldinfos . push_back ( pair < KX_Scene * , KX_WorldInfo * > ( m_currentScene , worldinfo ) ) ;
2002-10-12 11:37:38 +00:00
}
2005-08-22 15:47:56 +00:00
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : ResetPhysicsObjectsAnimationIpo ( bool clearIpo )
2005-08-22 15:47:56 +00:00
{
2014-07-23 13:07:01 +01:00
//TODO this entire function is deprecated, written for 2.4x
//the functionality should be rewritten, currently it does nothing
2005-08-22 18:31:19 +00:00
2015-05-04 13:32:28 +02:00
KX_SceneList * scenes = m_ketsjiEngine - > CurrentScenes ( ) ;
2005-08-22 18:31:19 +00:00
int numScenes = scenes - > size ( ) ;
int i ;
2015-05-04 13:32:28 +02:00
for ( i = 0 ; i < numScenes ; i + + ) {
KX_Scene * scene = scenes - > at ( i ) ;
CListValue * parentList = scene - > GetRootParentList ( ) ;
2005-08-22 18:31:19 +00:00
int numObjects = parentList - > GetCount ( ) ;
int g ;
2015-05-04 13:32:28 +02:00
for ( g = 0 ; g < numObjects ; g + + ) {
KX_GameObject * gameObj = ( KX_GameObject * ) parentList - > GetValue ( g ) ;
2013-12-09 22:26:52 +11:00
if ( gameObj - > IsRecordAnimation ( ) ) {
2015-05-04 13:32:28 +02:00
Object * blenderObject = gameObj - > GetBlenderObject ( ) ;
if ( blenderObject ) {
2010-02-22 12:25:58 +00:00
#if 0
2005-08-22 18:31:19 +00:00
//erase existing ipo's
2006-12-16 05:50:38 +00:00
Ipo * ipo = blenderObject - > ipo ; //findIpoForName(blenderObject->id.name+2);
2015-05-04 13:32:28 +02:00
if ( ipo ) { //clear the curve data
2012-03-28 05:03:24 +00:00
if ( clearIpo ) { //rcruiz
2006-12-16 05:50:38 +00:00
IpoCurve * icu1 ;
2014-07-23 13:07:01 +01:00
2006-12-16 05:50:38 +00:00
int numCurves = 0 ;
2012-03-24 07:52:14 +00:00
for ( icu1 = ( IpoCurve * ) ipo - > curve . first ; icu1 ; ) {
2014-07-23 13:07:01 +01:00
2006-12-16 05:50:38 +00:00
IpoCurve * tmpicu = icu1 ;
2014-07-23 13:07:01 +01:00
2006-12-16 05:50:38 +00:00
/*int i;
BezTriple * bezt ;
2012-03-28 05:03:24 +00:00
for ( bezt = tmpicu - > bezt , i = 0 ; i < tmpicu - > totvert ; i + + , bezt + + ) {
2006-12-16 05:50:38 +00:00
printf ( " (%f,%f,%f),(%f,%f,%f),(%f,%f,%f) \n " , bezt - > vec [ 0 ] [ 0 ] , bezt - > vec [ 0 ] [ 1 ] , bezt - > vec [ 0 ] [ 2 ] , bezt - > vec [ 1 ] [ 0 ] , bezt - > vec [ 1 ] [ 1 ] , bezt - > vec [ 1 ] [ 2 ] , bezt - > vec [ 2 ] [ 0 ] , bezt - > vec [ 2 ] [ 1 ] , bezt - > vec [ 2 ] [ 2 ] ) ;
} */
2014-07-23 13:07:01 +01:00
2006-12-16 05:50:38 +00:00
icu1 = icu1 - > next ;
numCurves + + ;
2014-07-23 13:07:01 +01:00
2006-12-16 05:50:38 +00:00
BLI_remlink ( & ( blenderObject - > ipo - > curve ) , tmpicu ) ;
2012-03-24 07:52:14 +00:00
if ( tmpicu - > bezt )
2006-12-16 05:50:38 +00:00
MEM_freeN ( tmpicu - > bezt ) ;
MEM_freeN ( tmpicu ) ;
2009-06-13 13:02:01 +00:00
localDel_ipoCurve ( tmpicu ) ;
2006-12-16 05:50:38 +00:00
}
2011-09-03 02:15:49 +00:00
}
2015-05-04 13:32:28 +02:00
}
else {
ipo = NULL ; // XXX add_ipo(blenderObject->id.name+2, ID_OB);
2005-08-23 13:16:02 +00:00
blenderObject - > ipo = ipo ;
2005-08-22 18:31:19 +00:00
}
2010-02-22 12:25:58 +00:00
# endif
2005-08-22 18:31:19 +00:00
}
}
}
}
2005-08-22 15:47:56 +00:00
}
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : resetNoneDynamicObjectToIpo ( )
2011-12-30 07:55:15 +00:00
{
2014-07-23 13:07:01 +01:00
//TODO the functionality should be rewritten
2006-12-16 05:50:38 +00:00
}
2005-08-23 13:16:02 +00:00
2015-05-04 13:32:28 +02:00
// this generates ipo curves for position, rotation, allowing to use game physics in animation
void KX_BlenderSceneConverter : : WritePhysicsObjectToAnimationIpo ( int frameNumber )
2005-08-22 15:47:56 +00:00
{
2015-05-04 13:32:28 +02:00
KX_SceneList * scenes = m_ketsjiEngine - > CurrentScenes ( ) ;
2005-08-23 13:16:02 +00:00
int numScenes = scenes - > size ( ) ;
int i ;
2015-05-04 13:32:28 +02:00
for ( i = 0 ; i < numScenes ; i + + ) {
KX_Scene * scene = scenes - > at ( i ) ;
2005-08-23 13:16:02 +00:00
//PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
2015-05-04 13:32:28 +02:00
CListValue * parentList = scene - > GetObjectList ( ) ;
2005-08-23 13:16:02 +00:00
int numObjects = parentList - > GetCount ( ) ;
int g ;
2015-05-04 13:32:28 +02:00
for ( g = 0 ; g < numObjects ; g + + ) {
KX_GameObject * gameObj = ( KX_GameObject * ) parentList - > GetValue ( g ) ;
Object * blenderObject = gameObj - > GetBlenderObject ( ) ;
if ( blenderObject & & blenderObject - > parent = = NULL & & gameObj - > IsRecordAnimation ( ) ) {
if ( blenderObject - > adt = = NULL )
2015-04-04 18:58:49 +11:00
BKE_animdata_add_id ( & blenderObject - > id ) ;
2010-02-22 12:25:58 +00:00
2015-05-04 13:32:28 +02:00
if ( blenderObject - > adt ) {
const MT_Point3 & position = gameObj - > NodeGetWorldPosition ( ) ;
2010-02-22 12:25:58 +00:00
//const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
2015-05-04 13:32:28 +02:00
const MT_Matrix3x3 & orn = gameObj - > NodeGetWorldOrientation ( ) ;
2010-02-22 12:25:58 +00:00
position . getValue ( blenderObject - > loc ) ;
float tmat [ 3 ] [ 3 ] ;
2015-05-04 13:32:28 +02:00
for ( int r = 0 ; r < 3 ; r + + )
for ( int c = 0 ; c < 3 ; c + + )
2011-04-10 09:37:04 +00:00
tmat [ r ] [ c ] = ( float ) orn [ c ] [ r ] ;
2010-02-22 12:25:58 +00:00
mat3_to_compatible_eul ( blenderObject - > rot , blenderObject - > rot , tmat ) ;
2011-04-10 09:37:04 +00:00
insert_keyframe ( NULL , & blenderObject - > id , NULL , NULL , " location " , - 1 , ( float ) frameNumber , INSERTKEY_FAST ) ;
insert_keyframe ( NULL , & blenderObject - > id , NULL , NULL , " rotation_euler " , - 1 , ( float ) frameNumber , INSERTKEY_FAST ) ;
2010-02-22 12:25:58 +00:00
2009-08-19 10:26:43 +00:00
#if 0
2009-06-08 20:08:19 +00:00
const MT_Point3 & position = gameObj - > NodeGetWorldPosition ( ) ;
//const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
2005-08-23 13:16:02 +00:00
const MT_Matrix3x3 & orn = gameObj - > NodeGetWorldOrientation ( ) ;
2009-06-08 20:08:19 +00:00
2012-09-16 04:58:18 +00:00
float eulerAngles [ 3 ] ;
float eulerAnglesOld [ 3 ] = { 0.0f , 0.0f , 0.0f } ;
2005-08-23 13:16:02 +00:00
float tmat [ 3 ] [ 3 ] ;
2009-06-08 20:08:19 +00:00
// XXX animato
2005-08-23 13:16:02 +00:00
Ipo * ipo = blenderObject - > ipo ;
2009-06-08 20:08:19 +00:00
//create the curves, if not existing, set linear if new
2005-08-23 13:16:02 +00:00
2009-06-08 20:08:19 +00:00
IpoCurve * icu_lx = findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " LocX " ) ;
if ( ! icu_lx ) {
icu_lx = verify_ipocurve ( & blenderObject - > id , ipo - > blocktype , NULL , NULL , NULL , OB_LOC_X , 1 ) ;
2012-03-24 07:52:14 +00:00
if ( icu_lx ) icu_lx - > ipo = IPO_LIN ;
2009-06-08 20:08:19 +00:00
}
IpoCurve * icu_ly = findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " LocY " ) ;
if ( ! icu_ly ) {
icu_ly = verify_ipocurve ( & blenderObject - > id , ipo - > blocktype , NULL , NULL , NULL , OB_LOC_Y , 1 ) ;
2012-03-24 07:52:14 +00:00
if ( icu_ly ) icu_ly - > ipo = IPO_LIN ;
2009-06-08 20:08:19 +00:00
}
IpoCurve * icu_lz = findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " LocZ " ) ;
if ( ! icu_lz ) {
icu_lz = verify_ipocurve ( & blenderObject - > id , ipo - > blocktype , NULL , NULL , NULL , OB_LOC_Z , 1 ) ;
2012-03-24 07:52:14 +00:00
if ( icu_lz ) icu_lz - > ipo = IPO_LIN ;
2009-06-08 20:08:19 +00:00
}
IpoCurve * icu_rx = findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " RotX " ) ;
if ( ! icu_rx ) {
icu_rx = verify_ipocurve ( & blenderObject - > id , ipo - > blocktype , NULL , NULL , NULL , OB_ROT_X , 1 ) ;
2012-03-24 07:52:14 +00:00
if ( icu_rx ) icu_rx - > ipo = IPO_LIN ;
2009-06-08 20:08:19 +00:00
}
IpoCurve * icu_ry = findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " RotY " ) ;
if ( ! icu_ry ) {
icu_ry = verify_ipocurve ( & blenderObject - > id , ipo - > blocktype , NULL , NULL , NULL , OB_ROT_Y , 1 ) ;
2012-03-24 07:52:14 +00:00
if ( icu_ry ) icu_ry - > ipo = IPO_LIN ;
2009-06-08 20:08:19 +00:00
}
IpoCurve * icu_rz = findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " RotZ " ) ;
if ( ! icu_rz ) {
icu_rz = verify_ipocurve ( & blenderObject - > id , ipo - > blocktype , NULL , NULL , NULL , OB_ROT_Z , 1 ) ;
2012-03-24 07:52:14 +00:00
if ( icu_rz ) icu_rz - > ipo = IPO_LIN ;
2009-06-08 20:08:19 +00:00
}
2005-08-23 13:16:02 +00:00
2012-10-22 08:15:51 +00:00
if ( icu_rx ) eulerAnglesOld [ 0 ] = eval_icu ( icu_rx , frameNumber - 1 ) / ( ( 180 / 3.14159265f ) / 10 ) ;
if ( icu_ry ) eulerAnglesOld [ 1 ] = eval_icu ( icu_ry , frameNumber - 1 ) / ( ( 180 / 3.14159265f ) / 10 ) ;
if ( icu_rz ) eulerAnglesOld [ 2 ] = eval_icu ( icu_rz , frameNumber - 1 ) / ( ( 180 / 3.14159265f ) / 10 ) ;
2009-06-08 20:08:19 +00:00
// orn.getValue((float *)tmat); // uses the wrong ordering, cant use this
2012-10-22 08:15:51 +00:00
for ( int r = 0 ; r < 3 ; r + + )
for ( int c = 0 ; c < 3 ; c + + )
2009-06-08 20:08:19 +00:00
tmat [ r ] [ c ] = orn [ c ] [ r ] ;
2009-11-10 20:43:45 +00:00
// mat3_to_eul( eulerAngles,tmat); // better to use Mat3ToCompatibleEul
mat3_to_compatible_eul ( eulerAngles , eulerAnglesOld , tmat ) ;
2009-06-08 20:08:19 +00:00
//eval_icu
2012-03-24 07:52:14 +00:00
for ( int x = 0 ; x < 3 ; x + + )
2009-06-08 20:08:19 +00:00
eulerAngles [ x ] * = ( float ) ( ( 180 / 3.14159265f ) / 10.0 ) ;
2005-08-23 13:16:02 +00:00
//fill the curves with data
2009-06-08 20:08:19 +00:00
if ( icu_lx ) insert_vert_icu ( icu_lx , frameNumber , position . x ( ) , 1 ) ;
if ( icu_ly ) insert_vert_icu ( icu_ly , frameNumber , position . y ( ) , 1 ) ;
if ( icu_lz ) insert_vert_icu ( icu_lz , frameNumber , position . z ( ) , 1 ) ;
if ( icu_rx ) insert_vert_icu ( icu_rx , frameNumber , eulerAngles [ 0 ] , 1 ) ;
if ( icu_ry ) insert_vert_icu ( icu_ry , frameNumber , eulerAngles [ 1 ] , 1 ) ;
if ( icu_rz ) insert_vert_icu ( icu_rz , frameNumber , eulerAngles [ 2 ] , 1 ) ;
2012-03-18 07:38:51 +00:00
// Handles are corrected at the end, testhandles_ipocurve isn't needed yet
2005-11-06 16:50:22 +00:00
# endif
2005-08-23 13:16:02 +00:00
}
}
}
2009-06-08 20:08:19 +00:00
}
2005-11-06 16:50:22 +00:00
}
2005-08-22 15:47:56 +00:00
2015-05-04 13:32:28 +02:00
void KX_BlenderSceneConverter : : TestHandlesPhysicsObjectToAnimationIpo ( )
2005-11-06 16:50:22 +00:00
{
2015-05-04 13:32:28 +02:00
KX_SceneList * scenes = m_ketsjiEngine - > CurrentScenes ( ) ;
2005-11-06 16:50:22 +00:00
int numScenes = scenes - > size ( ) ;
int i ;
2015-05-04 13:32:28 +02:00
for ( i = 0 ; i < numScenes ; i + + ) {
KX_Scene * scene = scenes - > at ( i ) ;
2005-11-06 16:50:22 +00:00
//PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
2015-05-04 13:32:28 +02:00
CListValue * parentList = scene - > GetRootParentList ( ) ;
2005-11-06 16:50:22 +00:00
int numObjects = parentList - > GetCount ( ) ;
int g ;
2015-05-04 13:32:28 +02:00
for ( g = 0 ; g < numObjects ; g + + ) {
KX_GameObject * gameObj = ( KX_GameObject * ) parentList - > GetValue ( g ) ;
2013-12-09 22:26:52 +11:00
if ( gameObj - > IsRecordAnimation ( ) ) {
2015-05-04 13:32:28 +02:00
Object * blenderObject = gameObj - > GetBlenderObject ( ) ;
if ( blenderObject & & blenderObject - > adt ) {
2015-01-09 14:59:39 +01:00
bAction * act = verify_adt_action ( & blenderObject - > id , false ) ;
FCurve * fcu ;
if ( ! act ) {
continue ;
}
/* for now, not much choice but to run this on all curves... */
for ( fcu = ( FCurve * ) act - > curves . first ; fcu ; fcu = fcu - > next ) {
/* Note: calling `sort_time_fcurve()` here is not needed, since
* all keys have been added in ' right ' order . */
calchandles_fcurve ( fcu ) ;
}
#if 0
2009-06-08 20:08:19 +00:00
// XXX animato
2005-11-06 16:50:22 +00:00
Ipo * ipo = blenderObject - > ipo ;
2015-01-09 14:59:39 +01:00
2009-06-08 20:08:19 +00:00
//create the curves, if not existing
//testhandles_ipocurve checks for NULL
testhandles_ipocurve ( findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " LocX " ) ) ;
testhandles_ipocurve ( findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " LocY " ) ) ;
testhandles_ipocurve ( findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " LocZ " ) ) ;
testhandles_ipocurve ( findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " RotX " ) ) ;
testhandles_ipocurve ( findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " RotY " ) ) ;
testhandles_ipocurve ( findIpoCurve ( ( IpoCurve * ) ipo - > curve . first , " RotZ " ) ) ;
2012-01-16 05:27:11 +00:00
# endif
2015-01-09 14:59:39 +01:00
}
2005-11-06 16:50:22 +00:00
}
}
}
2005-08-22 15:47:56 +00:00
}
2009-09-29 22:49:33 +00:00
2010-10-31 04:11:39 +00:00
# ifdef WITH_PYTHON
2009-09-29 22:49:33 +00:00
PyObject * KX_BlenderSceneConverter : : GetPyNamespace ( )
{
return m_ketsjiEngine - > GetPyNamespace ( ) ;
}
# endif
2009-11-15 23:58:56 +00:00
2015-05-04 13:32:28 +02:00
vector < Main * > & KX_BlenderSceneConverter : : GetMainDynamic ( )
2009-11-15 23:58:56 +00:00
{
return m_DynamicMaggie ;
}
2015-05-04 13:32:28 +02:00
Main * KX_BlenderSceneConverter : : GetMainDynamicPath ( const char * path )
2009-11-15 23:58:56 +00:00
{
2015-05-04 13:32:28 +02:00
for ( vector < Main * > : : iterator it = m_DynamicMaggie . begin ( ) ; ! ( it = = m_DynamicMaggie . end ( ) ) ; it + + )
2012-03-24 07:52:14 +00:00
if ( BLI_path_cmp ( ( * it ) - > name , path ) = = 0 )
2009-11-15 23:58:56 +00:00
return * it ;
return NULL ;
}
2012-12-22 05:38:32 +00:00
void KX_BlenderSceneConverter : : MergeAsyncLoads ( )
{
2015-05-04 13:32:28 +02:00
vector < KX_Scene * > * merge_scenes ;
2012-12-22 05:38:32 +00:00
2015-05-04 13:32:28 +02:00
vector < KX_LibLoadStatus * > : : iterator mit ;
vector < KX_Scene * > : : iterator sit ;
2012-12-22 05:38:32 +00:00
2015-10-08 21:37:03 +02:00
BLI_mutex_lock ( & m_threadinfo - > m_mutex ) ;
2012-12-22 05:38:32 +00:00
2015-05-04 13:32:28 +02:00
for ( mit = m_mergequeue . begin ( ) ; mit ! = m_mergequeue . end ( ) ; + + mit ) {
merge_scenes = ( vector < KX_Scene * > * ) ( * mit ) - > GetData ( ) ;
2012-12-22 05:38:32 +00:00
for ( sit = merge_scenes - > begin ( ) ; sit ! = merge_scenes - > end ( ) ; + + sit ) {
( * mit ) - > GetMergeScene ( ) - > MergeScene ( * sit ) ;
delete ( * sit ) ;
}
delete merge_scenes ;
( * mit ) - > SetData ( NULL ) ;
( * mit ) - > Finish ( ) ;
}
m_mergequeue . clear ( ) ;
2015-10-08 21:37:03 +02:00
BLI_mutex_unlock ( & m_threadinfo - > m_mutex ) ;
2012-12-22 05:38:32 +00:00
}
void KX_BlenderSceneConverter : : AddScenesToMergeQueue ( KX_LibLoadStatus * status )
{
2015-10-08 21:37:03 +02:00
BLI_mutex_lock ( & m_threadinfo - > m_mutex ) ;
2012-12-22 05:38:32 +00:00
m_mergequeue . push_back ( status ) ;
2015-10-08 21:37:03 +02:00
BLI_mutex_unlock ( & m_threadinfo - > m_mutex ) ;
2012-12-22 05:38:32 +00:00
}
2015-10-08 21:37:03 +02:00
static void async_convert ( TaskPool * pool , void * ptr , int UNUSED ( threadid ) )
2012-12-22 05:38:32 +00:00
{
KX_Scene * new_scene = NULL ;
2015-05-04 13:32:28 +02:00
KX_LibLoadStatus * status = ( KX_LibLoadStatus * ) ptr ;
vector < Scene * > * scenes = ( vector < Scene * > * ) status - > GetData ( ) ;
vector < KX_Scene * > * merge_scenes = new vector < KX_Scene * > ( ) ; // Deleted in MergeAsyncLoads
2012-12-22 05:38:32 +00:00
2015-05-04 13:32:28 +02:00
for ( unsigned int i = 0 ; i < scenes - > size ( ) ; + + i ) {
2012-12-22 05:38:32 +00:00
new_scene = status - > GetEngine ( ) - > CreateScene ( ( * scenes ) [ i ] , true ) ;
if ( new_scene )
merge_scenes - > push_back ( new_scene ) ;
2015-05-04 13:32:28 +02:00
status - > AddProgress ( ( 1.0f / scenes - > size ( ) ) * 0.9f ) ; // We'll call conversion 90% and merging 10% for now
2012-12-22 05:38:32 +00:00
}
delete scenes ;
status - > SetData ( merge_scenes ) ;
status - > GetConverter ( ) - > AddScenesToMergeQueue ( status ) ;
}
KX_LibLoadStatus * KX_BlenderSceneConverter : : LinkBlendFileMemory ( void * data , int length , const char * path , char * group , KX_Scene * scene_merge , char * * err_str , short options )
2010-08-28 02:07:55 +00:00
{
BlendHandle * bpy_openlib = BLO_blendhandle_from_memory ( data , length ) ;
// Error checking is done in LinkBlendFile
2011-03-22 02:39:08 +00:00
return LinkBlendFile ( bpy_openlib , path , group , scene_merge , err_str , options ) ;
2010-08-28 02:07:55 +00:00
}
2013-03-17 19:13:04 +00:00
KX_LibLoadStatus * KX_BlenderSceneConverter : : LinkBlendFilePath ( const char * filepath , char * group , KX_Scene * scene_merge , char * * err_str , short options )
2010-08-28 02:07:55 +00:00
{
2013-03-17 19:13:04 +00:00
BlendHandle * bpy_openlib = BLO_blendhandle_from_file ( filepath , NULL ) ;
2010-08-28 02:07:55 +00:00
// Error checking is done in LinkBlendFile
2013-03-17 19:13:04 +00:00
return LinkBlendFile ( bpy_openlib , filepath , group , scene_merge , err_str , options ) ;
2010-08-28 02:07:55 +00:00
}
2015-05-02 18:02:19 +02:00
static void load_datablocks ( Main * main_tmp , BlendHandle * bpy_openlib , const char * path , int idcode )
2009-11-15 23:58:56 +00:00
{
LinkNode * names = NULL ;
2012-06-07 01:46:28 +00:00
int totnames_dummy ;
2015-05-04 13:32:28 +02:00
names = BLO_blendhandle_get_datablock_names ( bpy_openlib , idcode , & totnames_dummy ) ;
2012-06-07 01:46:28 +00:00
2015-05-04 13:32:28 +02:00
int i = 0 ;
LinkNode * n = names ;
2012-10-21 07:58:38 +00:00
while ( n ) {
2012-06-07 01:46:28 +00:00
BLO_library_append_named_part ( main_tmp , & bpy_openlib , ( char * ) n - > link , idcode ) ;
2015-05-04 13:32:28 +02:00
n = ( LinkNode * ) n - > next ;
2012-06-07 01:46:28 +00:00
i + + ;
}
BLI_linklist_free ( names , free ) ; /* free linklist *and* each node's data */
}
2012-12-22 05:38:32 +00:00
KX_LibLoadStatus * KX_BlenderSceneConverter : : LinkBlendFile ( BlendHandle * bpy_openlib , const char * path , char * group , KX_Scene * scene_merge , char * * err_str , short options )
2012-06-07 01:46:28 +00:00
{
Main * main_newlib ; /* stored as a dynamic 'main' until we free it */
2012-11-10 05:42:50 +00:00
const int idcode = BKE_idcode_from_name ( group ) ;
2009-11-15 23:58:56 +00:00
ReportList reports ;
static char err_local [ 255 ] ;
2012-11-10 05:42:50 +00:00
// TIMEIT_START(bge_link_blend_file);
2012-12-22 05:38:32 +00:00
KX_LibLoadStatus * status ;
2015-05-04 13:32:28 +02:00
2009-11-15 23:58:56 +00:00
/* only scene and mesh supported right now */
2015-05-04 13:32:28 +02:00
if ( idcode ! = ID_SCE & & idcode ! = ID_ME & & idcode ! = ID_AC ) {
2009-11-15 23:58:56 +00:00
snprintf ( err_local , sizeof ( err_local ) , " invalid ID type given \" %s \" \n " , group ) ;
2015-05-04 13:32:28 +02:00
* err_str = err_local ;
2011-03-21 09:32:54 +00:00
BLO_blendhandle_close ( bpy_openlib ) ;
2012-12-22 05:38:32 +00:00
return NULL ;
2009-11-15 23:58:56 +00:00
}
2010-05-29 21:31:57 +00:00
2012-03-24 07:52:14 +00:00
if ( GetMainDynamicPath ( path ) ) {
2010-05-29 21:31:57 +00:00
snprintf ( err_local , sizeof ( err_local ) , " blend file already open \" %s \" \n " , path ) ;
2015-05-04 13:32:28 +02:00
* err_str = err_local ;
2011-03-21 09:32:54 +00:00
BLO_blendhandle_close ( bpy_openlib ) ;
2012-12-22 05:38:32 +00:00
return NULL ;
2010-05-29 21:31:57 +00:00
}
2009-11-15 23:58:56 +00:00
2015-05-04 13:32:28 +02:00
if ( bpy_openlib = = NULL ) {
2010-05-29 21:31:57 +00:00
snprintf ( err_local , sizeof ( err_local ) , " could not open blendfile \" %s \" \n " , path ) ;
2015-05-04 13:32:28 +02:00
* err_str = err_local ;
2012-12-22 05:38:32 +00:00
return NULL ;
2010-05-29 21:22:24 +00:00
}
BGE: Fix T40555: LibLoad material caching issue
Previously we don't merge material cached list, it create dangling pointer and memory leak.
Now we merge material cache list during the scene merge, and remove material in this list during the library free.
Reviewers: agoose77, dfelinto, hg1, pgi, campbellbarton, moguri
Reviewed By: campbellbarton, moguri
Subscribers: campbellbarton, youle, kupoman
Projects: #game_engine
Differential Revision: https://developer.blender.org/D1278
2015-05-19 19:24:14 +02:00
2013-12-30 12:18:41 +11:00
main_newlib = BKE_main_new ( ) ;
2012-09-16 04:58:18 +00:00
BKE_reports_init ( & reports , RPT_STORE ) ;
2010-05-29 21:31:57 +00:00
2015-05-02 18:02:19 +02:00
short flag = 0 ; /* don't need any special options */
/* created only for linking, then freed */
Main * main_tmp = BLO_library_append_begin ( main_newlib , & bpy_openlib , ( char * ) path ) ;
load_datablocks ( main_tmp , bpy_openlib , path , idcode ) ;
2011-03-22 02:39:08 +00:00
2015-05-04 13:32:28 +02:00
if ( idcode = = ID_SCE & & options & LIB_LOAD_LOAD_SCRIPTS ) {
2015-05-02 18:02:19 +02:00
load_datablocks ( main_tmp , bpy_openlib , path , ID_TXT ) ;
2012-06-21 05:41:06 +00:00
}
2011-03-22 02:39:08 +00:00
/* now do another round of linking for Scenes so all actions are properly loaded */
2015-05-04 13:32:28 +02:00
if ( idcode = = ID_SCE & & options & LIB_LOAD_LOAD_ACTIONS ) {
2015-05-02 18:02:19 +02:00
load_datablocks ( main_tmp , bpy_openlib , path , ID_AC ) ;
2011-03-22 02:39:08 +00:00
}
2015-05-02 18:02:19 +02:00
BLO_library_append_end ( NULL , main_tmp , & bpy_openlib , idcode , flag ) ;
2010-05-29 21:31:57 +00:00
BLO_blendhandle_close ( bpy_openlib ) ;
2011-08-05 05:26:19 +00:00
2010-05-29 21:31:57 +00:00
BKE_reports_clear ( & reports ) ;
2012-09-16 04:58:18 +00:00
/* done linking */
2010-05-29 21:31:57 +00:00
/* needed for lookups*/
GetMainDynamic ( ) . push_back ( main_newlib ) ;
2013-08-04 03:45:30 +00:00
BLI_strncpy ( main_newlib - > name , path , sizeof ( main_newlib - > name ) ) ;
2010-05-29 21:31:57 +00:00
2012-12-22 05:38:32 +00:00
status = new KX_LibLoadStatus ( this , m_ketsjiEngine , scene_merge , path ) ;
2015-05-04 13:32:28 +02:00
if ( idcode = = ID_ME ) {
2009-11-15 23:58:56 +00:00
/* Convert all new meshes into BGE meshes */
2015-05-04 13:32:28 +02:00
ID * mesh ;
2009-11-15 23:58:56 +00:00
2015-05-04 13:32:28 +02:00
for ( mesh = ( ID * ) main_newlib - > mesh . first ; mesh ; mesh = ( ID * ) mesh - > next ) {
2011-03-22 02:39:08 +00:00
if ( options & LIB_LOAD_VERBOSE )
2015-05-04 13:32:28 +02:00
printf ( " MeshName: %s \n " , mesh - > name + 2 ) ;
2012-12-18 20:56:25 +00:00
RAS_MeshObject * meshobj = BL_ConvertMesh ( ( Mesh * ) mesh , NULL , scene_merge , this , false ) ; // For now only use the libloading option for scenes, which need to handle materials/shaders
2015-05-04 13:32:28 +02:00
scene_merge - > GetLogicManager ( ) - > RegisterMeshName ( meshobj - > GetName ( ) , meshobj ) ;
2009-11-15 23:58:56 +00:00
}
}
2015-05-04 13:32:28 +02:00
else if ( idcode = = ID_AC ) {
2010-10-09 10:45:25 +00:00
/* Convert all actions */
ID * action ;
2015-05-04 13:32:28 +02:00
for ( action = ( ID * ) main_newlib - > action . first ; action ; action = ( ID * ) action - > next ) {
2011-03-22 02:39:08 +00:00
if ( options & LIB_LOAD_VERBOSE )
2015-05-04 13:32:28 +02:00
printf ( " ActionName: %s \n " , action - > name + 2 ) ;
scene_merge - > GetLogicManager ( ) - > RegisterActionName ( action - > name + 2 , action ) ;
2010-10-09 10:45:25 +00:00
}
}
2015-05-04 13:32:28 +02:00
else if ( idcode = = ID_SCE ) {
2009-11-15 23:58:56 +00:00
/* Merge all new linked in scene into the existing one */
ID * scene ;
2012-12-22 05:38:32 +00:00
// scenes gets deleted by the thread when it's done using it (look in async_convert())
2015-05-04 13:32:28 +02:00
vector < Scene * > * scenes = ( options & LIB_LOAD_ASYNC ) ? new vector < Scene * > ( ) : NULL ;
2012-12-22 05:38:32 +00:00
2015-05-04 13:32:28 +02:00
for ( scene = ( ID * ) main_newlib - > scene . first ; scene ; scene = ( ID * ) scene - > next ) {
2011-03-22 02:39:08 +00:00
if ( options & LIB_LOAD_VERBOSE )
2015-05-04 13:32:28 +02:00
printf ( " SceneName: %s \n " , scene - > name + 2 ) ;
2010-05-29 21:31:57 +00:00
2012-12-22 05:38:32 +00:00
if ( options & LIB_LOAD_ASYNC ) {
2015-05-04 13:32:28 +02:00
scenes - > push_back ( ( Scene * ) scene ) ;
}
else {
2012-12-22 05:38:32 +00:00
/* merge into the base scene */
2015-05-04 13:32:28 +02:00
KX_Scene * other = m_ketsjiEngine - > CreateScene ( ( Scene * ) scene , true ) ;
2012-12-22 05:38:32 +00:00
scene_merge - > MergeScene ( other ) ;
2010-05-29 21:31:57 +00:00
2012-12-22 05:38:32 +00:00
// RemoveScene(other); // Don't run this, it frees the entire scene converter data, just delete the scene
delete other ;
}
}
if ( options & LIB_LOAD_ASYNC ) {
status - > SetData ( scenes ) ;
2015-10-08 21:37:03 +02:00
BLI_task_pool_push ( m_threadinfo - > m_pool , async_convert , ( void * ) status , false , TASK_PRIORITY_LOW ) ;
2009-11-15 23:58:56 +00:00
}
2011-03-22 02:39:08 +00:00
2012-07-29 17:49:14 +00:00
# ifdef WITH_PYTHON
2012-06-21 05:41:06 +00:00
/* Handle any text datablocks */
2012-07-22 22:58:12 +00:00
if ( options & LIB_LOAD_LOAD_SCRIPTS )
addImportMain ( main_newlib ) ;
2012-07-29 17:49:14 +00:00
# endif
2012-06-21 05:41:06 +00:00
2011-03-22 02:39:08 +00:00
/* Now handle all the actions */
if ( options & LIB_LOAD_LOAD_ACTIONS ) {
ID * action ;
2015-05-04 13:32:28 +02:00
for ( action = ( ID * ) main_newlib - > action . first ; action ; action = ( ID * ) action - > next ) {
2011-03-22 02:39:08 +00:00
if ( options & LIB_LOAD_VERBOSE )
2015-05-04 13:32:28 +02:00
printf ( " ActionName: %s \n " , action - > name + 2 ) ;
scene_merge - > GetLogicManager ( ) - > RegisterActionName ( action - > name + 2 , action ) ;
2011-03-22 02:39:08 +00:00
}
}
2009-11-15 23:58:56 +00:00
}
2012-11-10 05:42:50 +00:00
2012-12-22 05:38:32 +00:00
if ( ! ( options & LIB_LOAD_ASYNC ) )
status - > Finish ( ) ;
2012-11-10 05:42:50 +00:00
// TIMEIT_END(bge_link_blend_file);
2012-12-22 05:38:32 +00:00
m_status_map [ main_newlib - > name ] = status ;
return status ;
2009-11-15 23:58:56 +00:00
}
2012-03-18 07:38:51 +00:00
/* Note m_map_*** are all ok and don't need to be freed
2009-11-15 23:58:56 +00:00
* most are temp and NewRemoveObject frees m_map_gameobject_to_blender */
2015-05-04 13:32:28 +02:00
bool KX_BlenderSceneConverter : : FreeBlendFile ( Main * maggie )
2009-11-15 23:58:56 +00:00
{
2015-05-04 13:32:28 +02:00
int maggie_index = - 1 ;
int i = 0 ;
2009-11-15 23:58:56 +00:00
2015-05-04 13:32:28 +02:00
if ( maggie = = NULL )
2009-11-15 23:58:56 +00:00
return false ;
/* tag all false except the one we remove */
2015-05-04 13:32:28 +02:00
for ( vector < Main * > : : iterator it = m_DynamicMaggie . begin ( ) ; ! ( it = = m_DynamicMaggie . end ( ) ) ; it + + ) {
Main * main = * it ;
2012-03-24 07:52:14 +00:00
if ( main ! = maggie ) {
2013-12-30 13:25:27 +11:00
BKE_main_id_tag_all ( main , false ) ;
2009-11-15 23:58:56 +00:00
}
else {
2015-05-04 13:32:28 +02:00
maggie_index = i ;
2009-11-15 23:58:56 +00:00
}
i + + ;
}
2011-08-28 05:01:16 +00:00
/* should never happen but just to be safe */
2012-03-24 07:52:14 +00:00
if ( maggie_index = = - 1 )
2011-08-28 05:01:16 +00:00
return false ;
2009-11-15 23:58:56 +00:00
m_DynamicMaggie . erase ( m_DynamicMaggie . begin ( ) + maggie_index ) ;
2013-12-30 13:25:27 +11:00
BKE_main_id_tag_all ( maggie , true ) ;
2009-11-15 23:58:56 +00:00
/* free all tagged objects */
2015-05-04 13:32:28 +02:00
KX_SceneList * scenes = m_ketsjiEngine - > CurrentScenes ( ) ;
2009-11-15 23:58:56 +00:00
int numScenes = scenes - > size ( ) ;
2015-05-04 13:32:28 +02:00
for ( int scene_idx = 0 ; scene_idx < numScenes ; scene_idx + + ) {
KX_Scene * scene = scenes - > at ( scene_idx ) ;
2012-03-24 07:52:14 +00:00
if ( IS_TAGGED ( scene - > GetBlenderScene ( ) ) ) {
2013-06-21 06:56:43 +00:00
m_ketsjiEngine - > RemoveScene ( scene - > GetName ( ) ) ;
2013-07-15 22:35:09 +00:00
m_mat_cache . erase ( scene ) ;
m_polymat_cache . erase ( scene ) ;
2009-11-15 23:58:56 +00:00
scene_idx - - ;
numScenes - - ;
}
else {
2012-03-09 00:41:09 +00:00
/* in case the mesh might be refered to later */
2009-11-15 23:58:56 +00:00
{
2015-05-04 13:32:28 +02:00
CTR_Map < STR_HashedString , void * > & mapStringToMeshes = scene - > GetLogicManager ( ) - > GetMeshMap ( ) ;
2009-11-15 23:58:56 +00:00
2015-05-04 13:32:28 +02:00
for ( int i = 0 ; i < mapStringToMeshes . size ( ) ; i + + ) {
RAS_MeshObject * meshobj = ( RAS_MeshObject * ) * mapStringToMeshes . at ( i ) ;
if ( meshobj & & IS_TAGGED ( meshobj - > GetMesh ( ) ) ) {
2009-11-15 23:58:56 +00:00
STR_HashedString mn = meshobj - > GetName ( ) ;
mapStringToMeshes . remove ( mn ) ;
2011-03-19 22:37:27 +00:00
m_map_mesh_to_gamemesh . remove ( CHashedPtr ( meshobj - > GetMesh ( ) ) ) ;
2009-11-15 23:58:56 +00:00
i - - ;
}
}
}
2010-10-09 10:45:25 +00:00
/* Now unregister actions */
{
2015-05-04 13:32:28 +02:00
CTR_Map < STR_HashedString , void * > & mapStringToActions = scene - > GetLogicManager ( ) - > GetActionMap ( ) ;
2010-10-09 10:45:25 +00:00
2015-05-04 13:32:28 +02:00
for ( int i = 0 ; i < mapStringToActions . size ( ) ; i + + ) {
ID * action = ( ID * ) * mapStringToActions . at ( i ) ;
2010-10-09 10:45:25 +00:00
2015-05-04 13:32:28 +02:00
if ( IS_TAGGED ( action ) ) {
STR_HashedString an = action - > name + 2 ;
2010-10-09 10:45:25 +00:00
mapStringToActions . remove ( an ) ;
2015-07-01 16:38:31 +02:00
m_map_blender_to_gameAdtList . remove ( CHashedPtr ( action ) ) ;
2010-10-09 10:45:25 +00:00
i - - ;
}
}
}
2009-11-15 23:58:56 +00:00
//scene->FreeTagged(); /* removed tagged objects and meshes*/
CListValue * obj_lists [ ] = { scene - > GetObjectList ( ) , scene - > GetInactiveList ( ) , NULL } ;
2015-05-04 13:32:28 +02:00
for ( int ob_ls_idx = 0 ; obj_lists [ ob_ls_idx ] ; ob_ls_idx + + ) {
CListValue * obs = obj_lists [ ob_ls_idx ] ;
RAS_MeshObject * mesh ;
2009-11-15 23:58:56 +00:00
2015-05-04 13:32:28 +02:00
for ( int ob_idx = 0 ; ob_idx < obs - > GetCount ( ) ; ob_idx + + ) {
KX_GameObject * gameobj = ( KX_GameObject * ) obs - > GetValue ( ob_idx ) ;
2012-03-24 07:52:14 +00:00
if ( IS_TAGGED ( gameobj - > GetBlenderObject ( ) ) ) {
2009-11-15 23:58:56 +00:00
int size_before = obs - > GetCount ( ) ;
/* Eventually calls RemoveNodeDestructObject
* frees m_map_gameobject_to_blender from UnregisterGameObject */
scene - > RemoveObject ( gameobj ) ;
2012-03-24 07:52:14 +00:00
if ( size_before ! = obs - > GetCount ( ) )
2009-11-15 23:58:56 +00:00
ob_idx - - ;
else {
printf ( " ERROR COULD NOT REMOVE \" %s \" \n " , gameobj - > GetName ( ) . ReadPtr ( ) ) ;
}
}
else {
2015-07-01 16:38:31 +02:00
gameobj - > RemoveTaggedActions ( ) ;
2009-11-15 23:58:56 +00:00
/* free the mesh, we could be referecing a linked one! */
2015-05-04 13:32:28 +02:00
int mesh_index = gameobj - > GetMeshCount ( ) ;
2012-10-21 07:58:38 +00:00
while ( mesh_index - - ) {
2015-05-04 13:32:28 +02:00
mesh = gameobj - > GetMesh ( mesh_index ) ;
2012-03-24 07:52:14 +00:00
if ( IS_TAGGED ( mesh - > GetMesh ( ) ) ) {
2009-11-15 23:58:56 +00:00
gameobj - > RemoveMeshes ( ) ; /* XXX - slack, should only remove meshes that are library items but mostly objects only have 1 mesh */
break ;
}
2014-05-13 15:50:28 -07:00
else {
/* also free the mesh if it's using a tagged material */
int mat_index = mesh - > NumMaterials ( ) ;
while ( mat_index - - ) {
if ( IS_TAGGED ( mesh - > GetMeshMaterial ( mat_index ) - > m_bucket - > GetPolyMaterial ( ) - > GetBlenderMaterial ( ) ) ) {
gameobj - > RemoveMeshes ( ) ; /* XXX - slack, same as above */
break ;
}
}
}
2009-11-15 23:58:56 +00:00
}
2010-10-09 10:45:25 +00:00
/* make sure action actuators are not referencing tagged actions */
2015-05-04 13:32:28 +02:00
for ( unsigned int act_idx = 0 ; act_idx < gameobj - > GetActuators ( ) . size ( ) ; act_idx + + ) {
if ( gameobj - > GetActuators ( ) [ act_idx ] - > IsType ( SCA_IActuator : : KX_ACT_ACTION ) ) {
BL_ActionActuator * act = ( BL_ActionActuator * ) gameobj - > GetActuators ( ) [ act_idx ] ;
2012-03-24 07:52:14 +00:00
if ( IS_TAGGED ( act - > GetAction ( ) ) )
2010-10-09 10:45:25 +00:00
act - > SetAction ( NULL ) ;
}
}
2009-11-15 23:58:56 +00:00
}
}
}
}
}
int size ;
// delete the entities of this scene
/* TODO - */
2012-03-03 16:31:46 +00:00
#if 0
2009-11-15 23:58:56 +00:00
vector < pair < KX_Scene * , KX_WorldInfo * > > : : iterator worldit ;
size = m_worldinfos . size ( ) ;
for ( i = 0 , worldit = m_worldinfos . begin ( ) ; i < size ; ) {
if ( ( * worldit ) . second ) {
delete ( * worldit ) . second ;
* worldit = m_worldinfos . back ( ) ;
m_worldinfos . pop_back ( ) ;
size - - ;
} else {
i + + ;
worldit + + ;
}
2012-03-03 16:31:46 +00:00
}
# endif
2009-11-15 23:58:56 +00:00
2012-03-18 07:38:51 +00:00
/* Worlds don't reference original blender data so we need to make a set from them */
2015-05-04 13:32:28 +02:00
typedef std : : set < KX_WorldInfo * > KX_WorldInfoSet ;
2009-11-15 23:58:56 +00:00
KX_WorldInfoSet worldset ;
2015-05-04 13:32:28 +02:00
for ( int scene_idx = 0 ; scene_idx < numScenes ; scene_idx + + ) {
KX_Scene * scene = scenes - > at ( scene_idx ) ;
2012-03-24 07:52:14 +00:00
if ( scene - > GetWorldInfo ( ) )
2015-05-04 13:32:28 +02:00
worldset . insert ( scene - > GetWorldInfo ( ) ) ;
2009-11-15 23:58:56 +00:00
}
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , KX_WorldInfo * > > : : iterator worldit ;
2009-11-15 23:58:56 +00:00
size = m_worldinfos . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( i = 0 , worldit = m_worldinfos . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
if ( worldit - > second & & ( worldset . count ( worldit - > second ) ) = = 0 ) {
delete worldit - > second ;
2009-11-15 23:58:56 +00:00
* worldit = m_worldinfos . back ( ) ;
m_worldinfos . pop_back ( ) ;
size - - ;
2015-05-04 13:32:28 +02:00
}
else {
2009-11-15 23:58:56 +00:00
i + + ;
worldit + + ;
}
}
worldset . clear ( ) ;
/* done freeing the worlds */
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_IPolyMaterial * > > : : iterator polymit ;
2009-11-15 23:58:56 +00:00
size = m_polymaterials . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( i = 0 , polymit = m_polymaterials . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
RAS_IPolyMaterial * mat = polymit - > second ;
2015-05-04 13:32:28 +02:00
Material * bmat = NULL ;
2009-11-15 23:58:56 +00:00
2015-05-04 13:32:28 +02:00
KX_BlenderMaterial * bl_mat = static_cast < KX_BlenderMaterial * > ( mat ) ;
bmat = bl_mat - > GetBlenderMaterial ( ) ;
2009-11-15 23:58:56 +00:00
if ( IS_TAGGED ( bmat ) ) {
/* only remove from bucket */
2015-07-09 09:45:21 +02:00
polymit - > first - > GetBucketManager ( ) - > RemoveMaterial ( mat ) ;
2009-11-15 23:58:56 +00:00
}
i + + ;
polymit + + ;
}
2015-05-04 13:32:28 +02:00
for ( i = 0 , polymit = m_polymaterials . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
RAS_IPolyMaterial * mat = polymit - > second ;
2015-05-04 13:32:28 +02:00
Material * bmat = NULL ;
2009-11-15 23:58:56 +00:00
2013-11-04 19:21:50 +00:00
KX_BlenderMaterial * bl_mat = static_cast < KX_BlenderMaterial * > ( mat ) ;
2015-05-04 13:32:28 +02:00
bmat = bl_mat - > GetBlenderMaterial ( ) ;
2009-11-15 23:58:56 +00:00
if ( IS_TAGGED ( bmat ) ) {
BGE: Fix T40555: LibLoad material caching issue
Previously we don't merge material cached list, it create dangling pointer and memory leak.
Now we merge material cache list during the scene merge, and remove material in this list during the library free.
Reviewers: agoose77, dfelinto, hg1, pgi, campbellbarton, moguri
Reviewed By: campbellbarton, moguri
Subscribers: campbellbarton, youle, kupoman
Projects: #game_engine
Differential Revision: https://developer.blender.org/D1278
2015-05-19 19:24:14 +02:00
// Remove the poly material coresponding to this Blender Material.
m_polymat_cache [ polymit - > first ] . erase ( bmat ) ;
2015-07-09 09:45:21 +02:00
delete polymit - > second ;
2009-11-15 23:58:56 +00:00
* polymit = m_polymaterials . back ( ) ;
m_polymaterials . pop_back ( ) ;
size - - ;
} else {
i + + ;
polymit + + ;
}
}
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , BL_Material * > > : : iterator matit ;
2009-11-15 23:58:56 +00:00
size = m_materials . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( i = 0 , matit = m_materials . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
BL_Material * mat = matit - > second ;
2009-11-15 23:58:56 +00:00
if ( IS_TAGGED ( mat - > material ) ) {
BGE: Fix T40555: LibLoad material caching issue
Previously we don't merge material cached list, it create dangling pointer and memory leak.
Now we merge material cache list during the scene merge, and remove material in this list during the library free.
Reviewers: agoose77, dfelinto, hg1, pgi, campbellbarton, moguri
Reviewed By: campbellbarton, moguri
Subscribers: campbellbarton, youle, kupoman
Projects: #game_engine
Differential Revision: https://developer.blender.org/D1278
2015-05-19 19:24:14 +02:00
// Remove the bl material coresponding to this Blender Material.
m_mat_cache [ matit - > first ] . erase ( mat - > material ) ;
2015-07-09 09:45:21 +02:00
delete matit - > second ;
2009-11-15 23:58:56 +00:00
* matit = m_materials . back ( ) ;
m_materials . pop_back ( ) ;
size - - ;
2015-05-04 13:32:28 +02:00
}
else {
2009-11-15 23:58:56 +00:00
i + + ;
matit + + ;
}
}
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_MeshObject * > > : : iterator meshit ;
2014-05-14 19:11:08 -07:00
RAS_BucketManager : : BucketList : : iterator bit ;
list < RAS_MeshSlot > : : iterator msit ;
RAS_BucketManager : : BucketList buckets ;
2009-11-15 23:58:56 +00:00
size = m_meshobjects . size ( ) ;
2015-05-04 13:32:28 +02:00
for ( i = 0 , meshit = m_meshobjects . begin ( ) ; i < size ; ) {
2015-07-09 09:45:21 +02:00
RAS_MeshObject * me = meshit - > second ;
2009-11-15 23:58:56 +00:00
if ( IS_TAGGED ( me - > GetMesh ( ) ) ) {
2014-05-14 19:11:08 -07:00
// Before deleting the mesh object, make sure the rasterizer is
// no longer referencing it.
buckets = meshit - > first - > GetBucketManager ( ) - > GetSolidBuckets ( ) ;
2015-05-04 13:32:28 +02:00
for ( bit = buckets . begin ( ) ; bit ! = buckets . end ( ) ; bit + + ) {
2014-05-14 19:11:08 -07:00
msit = ( * bit ) - > msBegin ( ) ;
while ( msit ! = ( * bit ) - > msEnd ( ) ) {
if ( msit - > m_mesh = = meshit - > second )
( * bit ) - > RemoveMesh ( & ( * msit + + ) ) ;
else
msit + + ;
}
}
// And now the alpha buckets
buckets = meshit - > first - > GetBucketManager ( ) - > GetAlphaBuckets ( ) ;
2015-05-04 13:32:28 +02:00
for ( bit = buckets . begin ( ) ; bit ! = buckets . end ( ) ; bit + + ) {
2014-05-14 19:11:08 -07:00
msit = ( * bit ) - > msBegin ( ) ;
while ( msit ! = ( * bit ) - > msEnd ( ) ) {
if ( msit - > m_mesh = = meshit - > second )
( * bit ) - > RemoveMesh ( & ( * msit + + ) ) ;
else
msit + + ;
}
}
// Now it should be safe to delete
2015-07-09 09:45:21 +02:00
delete meshit - > second ;
2009-11-15 23:58:56 +00:00
* meshit = m_meshobjects . back ( ) ;
m_meshobjects . pop_back ( ) ;
size - - ;
2015-05-04 13:32:28 +02:00
}
else {
2009-11-15 23:58:56 +00:00
i + + ;
meshit + + ;
}
}
2012-07-29 17:49:14 +00:00
# ifdef WITH_PYTHON
/* make sure this maggie is removed from the import list if it's there
* ( this operation is safe if it isn ' t in the list ) */
2012-06-21 05:41:06 +00:00
removeImportMain ( maggie ) ;
2012-07-29 17:49:14 +00:00
# endif
2012-06-21 05:41:06 +00:00
2012-12-22 05:38:32 +00:00
delete m_status_map [ maggie - > name ] ;
m_status_map . erase ( maggie - > name ) ;
2013-12-30 13:25:27 +11:00
BKE_main_free ( maggie ) ;
2009-11-15 23:58:56 +00:00
return true ;
}
bool KX_BlenderSceneConverter : : FreeBlendFile ( const char * path )
{
return FreeBlendFile ( GetMainDynamicPath ( path ) ) ;
}
bool KX_BlenderSceneConverter : : MergeScene ( KX_Scene * to , KX_Scene * from )
{
{
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , KX_WorldInfo * > > : : iterator itp = m_worldinfos . begin ( ) ;
2009-11-15 23:58:56 +00:00
while ( itp ! = m_worldinfos . end ( ) ) {
2015-07-09 09:45:21 +02:00
if ( itp - > first = = from )
itp - > first = to ;
2009-11-15 23:58:56 +00:00
itp + + ;
}
}
{
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_IPolyMaterial * > > : : iterator itp = m_polymaterials . begin ( ) ;
2009-11-15 23:58:56 +00:00
while ( itp ! = m_polymaterials . end ( ) ) {
2015-07-09 09:45:21 +02:00
if ( itp - > first = = from ) {
itp - > first = to ;
2009-11-15 23:58:56 +00:00
/* also switch internal data */
2015-07-09 09:45:21 +02:00
RAS_IPolyMaterial * mat = itp - > second ;
2009-11-15 23:58:56 +00:00
mat - > Replace_IScene ( to ) ;
}
itp + + ;
}
}
{
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , RAS_MeshObject * > > : : iterator itp = m_meshobjects . begin ( ) ;
2009-11-15 23:58:56 +00:00
while ( itp ! = m_meshobjects . end ( ) ) {
2015-07-09 09:45:21 +02:00
if ( itp - > first = = from )
itp - > first = to ;
2009-11-15 23:58:56 +00:00
itp + + ;
}
}
{
2015-05-04 13:32:28 +02:00
vector < pair < KX_Scene * , BL_Material * > > : : iterator itp = m_materials . begin ( ) ;
2009-11-15 23:58:56 +00:00
while ( itp ! = m_materials . end ( ) ) {
2015-07-09 09:45:21 +02:00
if ( itp - > first = = from )
itp - > first = to ;
2009-11-15 23:58:56 +00:00
itp + + ;
}
}
2015-05-04 13:32:28 +02:00
BGE: Fix T40555: LibLoad material caching issue
Previously we don't merge material cached list, it create dangling pointer and memory leak.
Now we merge material cache list during the scene merge, and remove material in this list during the library free.
Reviewers: agoose77, dfelinto, hg1, pgi, campbellbarton, moguri
Reviewed By: campbellbarton, moguri
Subscribers: campbellbarton, youle, kupoman
Projects: #game_engine
Differential Revision: https://developer.blender.org/D1278
2015-05-19 19:24:14 +02:00
MaterialCache : : iterator matcacheit = m_mat_cache . find ( from ) ;
2015-07-02 12:20:13 +02:00
if ( matcacheit ! = m_mat_cache . end ( ) ) {
// Merge cached BL_Material map.
m_mat_cache [ to ] . insert ( matcacheit - > second . begin ( ) , matcacheit - > second . end ( ) ) ;
m_mat_cache . erase ( matcacheit ) ;
}
BGE: Fix T40555: LibLoad material caching issue
Previously we don't merge material cached list, it create dangling pointer and memory leak.
Now we merge material cache list during the scene merge, and remove material in this list during the library free.
Reviewers: agoose77, dfelinto, hg1, pgi, campbellbarton, moguri
Reviewed By: campbellbarton, moguri
Subscribers: campbellbarton, youle, kupoman
Projects: #game_engine
Differential Revision: https://developer.blender.org/D1278
2015-05-19 19:24:14 +02:00
PolyMaterialCache : : iterator polymatcacheit = m_polymat_cache . find ( from ) ;
2015-07-02 12:20:13 +02:00
if ( polymatcacheit ! = m_polymat_cache . end ( ) ) {
// Merge cached RAS_IPolyMaterial map.
m_polymat_cache [ to ] . insert ( polymatcacheit - > second . begin ( ) , polymatcacheit - > second . end ( ) ) ;
m_polymat_cache . erase ( polymatcacheit ) ;
}
BGE: Fix T40555: LibLoad material caching issue
Previously we don't merge material cached list, it create dangling pointer and memory leak.
Now we merge material cache list during the scene merge, and remove material in this list during the library free.
Reviewers: agoose77, dfelinto, hg1, pgi, campbellbarton, moguri
Reviewed By: campbellbarton, moguri
Subscribers: campbellbarton, youle, kupoman
Projects: #game_engine
Differential Revision: https://developer.blender.org/D1278
2015-05-19 19:24:14 +02:00
2009-11-15 23:58:56 +00:00
return true ;
}
/* This function merges a mesh from the current scene into another main
* it does not convert */
2015-05-04 13:32:28 +02:00
RAS_MeshObject * KX_BlenderSceneConverter : : ConvertMeshSpecial ( KX_Scene * kx_scene , Main * maggie , const char * name )
2009-11-15 23:58:56 +00:00
{
/* Find a mesh in the current main */
2009-12-29 15:40:26 +00:00
ID * me = static_cast < ID * > ( BLI_findstring ( & m_maggie - > mesh , name , offsetof ( ID , name ) + 2 ) ) ;
2014-05-01 14:49:42 -07:00
Main * from_maggie = m_maggie ;
if ( me = = NULL ) {
// The mesh wasn't in the current main, try any dynamic (i.e., LibLoaded) ones
2015-05-04 13:32:28 +02:00
vector < Main * > : : iterator it ;
2014-05-01 14:49:42 -07:00
for ( it = GetMainDynamic ( ) . begin ( ) ; it ! = GetMainDynamic ( ) . end ( ) ; it + + ) {
me = static_cast < ID * > ( BLI_findstring ( & ( * it ) - > mesh , name , offsetof ( ID , name ) + 2 ) ) ;
from_maggie = * it ;
if ( me )
break ;
}
}
2015-05-04 13:32:28 +02:00
if ( me = = NULL ) {
2009-11-15 23:58:56 +00:00
printf ( " Could not be found \" %s \" \n " , name ) ;
return NULL ;
}
2015-05-04 13:32:28 +02:00
2009-11-15 23:58:56 +00:00
/* Watch this!, if its used in the original scene can cause big troubles */
2012-03-24 07:52:14 +00:00
if ( me - > us > 0 ) {
2014-05-20 15:04:25 -07:00
# ifdef DEBUG
2009-11-15 23:58:56 +00:00
printf ( " Mesh has a user \" %s \" \n " , name ) ;
2014-05-20 15:04:25 -07:00
# endif
2014-05-01 14:49:42 -07:00
me = ( ID * ) BKE_mesh_copy_ex ( from_maggie , ( Mesh * ) me ) ;
2009-11-15 23:58:56 +00:00
me - > us - - ;
}
2014-05-01 14:49:42 -07:00
BLI_remlink ( & from_maggie - > mesh , me ) ; /* even if we made the copy it needs to be removed */
2009-11-15 23:58:56 +00:00
BLI_addtail ( & maggie - > mesh , me ) ;
/* Must copy the materials this uses else we cant free them */
{
2015-05-04 13:32:28 +02:00
Mesh * mesh = ( Mesh * ) me ;
2009-11-15 23:58:56 +00:00
/* ensure all materials are tagged */
2015-05-04 13:32:28 +02:00
for ( int i = 0 ; i < mesh - > totcol ; i + + ) {
2012-03-24 07:52:14 +00:00
if ( mesh - > mat [ i ] )
2009-11-15 23:58:56 +00:00
mesh - > mat [ i ] - > id . flag & = ~ LIB_DOIT ;
2015-05-04 13:32:28 +02:00
}
for ( int i = 0 ; i < mesh - > totcol ; i + + ) {
Material * mat_old = mesh - > mat [ i ] ;
2009-11-15 23:58:56 +00:00
/* if its tagged its a replaced material */
2015-05-04 13:32:28 +02:00
if ( mat_old & & ( mat_old - > id . flag & LIB_DOIT ) = = 0 ) {
Material * mat_old = mesh - > mat [ i ] ;
Material * mat_new = BKE_material_copy ( mat_old ) ;
2009-11-15 23:58:56 +00:00
mat_new - > id . flag | = LIB_DOIT ;
mat_old - > id . us - - ;
2015-05-04 13:32:28 +02:00
2014-05-01 14:49:42 -07:00
BLI_remlink ( & G . main - > mat , mat_new ) ; // BKE_material_copy uses G.main, and there is no BKE_material_copy_ex
2009-11-15 23:58:56 +00:00
BLI_addtail ( & maggie - > mat , mat_new ) ;
2015-05-04 13:32:28 +02:00
2012-10-22 08:15:51 +00:00
mesh - > mat [ i ] = mat_new ;
2015-05-04 13:32:28 +02:00
2009-11-15 23:58:56 +00:00
/* the same material may be used twice */
2012-10-22 08:15:51 +00:00
for ( int j = i + 1 ; j < mesh - > totcol ; j + + ) {
if ( mesh - > mat [ j ] = = mat_old ) {
mesh - > mat [ j ] = mat_new ;
2009-11-15 23:58:56 +00:00
mat_new - > id . us + + ;
mat_old - > id . us - - ;
}
}
}
}
}
2014-05-14 19:11:08 -07:00
m_currentScene = kx_scene ; // This needs to be set in case we LibLoaded earlier
2012-12-18 20:56:25 +00:00
RAS_MeshObject * meshobj = BL_ConvertMesh ( ( Mesh * ) me , NULL , kx_scene , this , false ) ;
2009-11-15 23:58:56 +00:00
kx_scene - > GetLogicManager ( ) - > RegisterMeshName ( meshobj - > GetName ( ) , meshobj ) ;
m_map_mesh_to_gamemesh . clear ( ) ; /* This is at runtime so no need to keep this, BL_ConvertMesh adds */
return meshobj ;
}