4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * Convert blender data to ketsji
31 /** \file gameengine/Converter/BL_BlenderDataConversion.cpp
36 #if defined(WIN32) && !defined(FREE_WINDOWS)
37 #pragma warning (disable : 4786)
42 #include "BL_BlenderDataConversion.h"
43 #include "KX_BlenderGL.h"
44 #include "KX_BlenderScalarInterpolator.h"
46 #include "RAS_IPolygonMaterial.h"
47 #include "KX_PolygonMaterial.h"
50 #include "ListValue.h"
52 // Collision & Fuzzics LTD
58 #include "KX_GameObject.h"
59 #include "RAS_FramingManager.h"
60 #include "RAS_MeshObject.h"
62 #include "KX_ConvertActuators.h"
63 #include "KX_ConvertControllers.h"
64 #include "KX_ConvertSensors.h"
66 #include "SCA_LogicManager.h"
67 #include "SCA_EventManager.h"
68 #include "SCA_TimeEventManager.h"
70 #include "KX_Camera.h"
71 #include "KX_EmptyObject.h"
72 #include "KX_FontObject.h"
73 #include "MT_Point3.h"
74 #include "MT_Transform.h"
75 #include "MT_MinMax.h"
76 #include "SCA_IInputDevice.h"
77 #include "RAS_TexMatrix.h"
78 #include "RAS_ICanvas.h"
79 #include "RAS_MaterialBucket.h"
80 //#include "KX_BlenderPolyMaterial.h"
81 #include "RAS_Polygon.h"
82 #include "RAS_TexVert.h"
83 #include "RAS_BucketManager.h"
84 #include "RAS_IRenderTools.h"
85 #include "BL_Material.h"
86 #include "KX_BlenderMaterial.h"
87 #include "BL_Texture.h"
89 #include "DNA_action_types.h"
91 #include "BKE_global.h"
92 #include "BKE_object.h"
93 #include "BL_ModifierDeformer.h"
94 #include "BL_ShapeDeformer.h"
95 #include "BL_SkinDeformer.h"
96 #include "BL_MeshDeformer.h"
97 #include "KX_SoftBodyDeformer.h"
98 //#include "BL_ArmatureController.h"
99 #include "BLI_utildefines.h"
100 #include "BlenderWorldInfo.h"
102 #include "KX_KetsjiEngine.h"
103 #include "KX_BlenderSceneConverter.h"
105 /* This little block needed for linking to Blender... */
107 #include "BLI_winstuff.h"
110 /* This list includes only data type definitions */
111 #include "DNA_object_types.h"
112 #include "DNA_material_types.h"
113 #include "DNA_texture_types.h"
114 #include "DNA_image_types.h"
115 #include "DNA_lamp_types.h"
116 #include "DNA_group_types.h"
117 #include "DNA_scene_types.h"
118 #include "DNA_camera_types.h"
119 #include "DNA_property_types.h"
120 #include "DNA_text_types.h"
121 #include "DNA_sensor_types.h"
122 #include "DNA_controller_types.h"
123 #include "DNA_actuator_types.h"
124 #include "DNA_mesh_types.h"
125 #include "DNA_meshdata_types.h"
126 #include "DNA_view3d_types.h"
127 #include "DNA_world_types.h"
128 #include "DNA_sound_types.h"
129 #include "DNA_key_types.h"
130 #include "DNA_armature_types.h"
131 #include "DNA_object_force.h"
133 #include "MEM_guardedalloc.h"
136 #include "BKE_mesh.h"
137 #include "MT_Point3.h"
139 #include "BLI_math.h"
142 #include "BKE_scene.h"
143 #include "BKE_customdata.h"
144 #include "BKE_cdderivedmesh.h"
145 #include "BKE_DerivedMesh.h"
148 #include "BKE_material.h" /* give_current_material */
149 /* end of blender include block */
151 #include "KX_BlenderInputDevice.h"
152 #include "KX_ConvertProperties.h"
153 #include "KX_HashedPtr.h"
156 #include "KX_ScalarInterpolator.h"
158 #include "KX_IpoConvert.h"
159 #include "BL_System.h"
165 #include "KX_ConvertPhysicsObject.h"
167 #include "CcdPhysicsEnvironment.h"
168 #include "CcdGraphicController.h"
170 #include "KX_MotionState.h"
172 // This file defines relationships between parents and children
173 // in the game engine.
175 #include "KX_SG_NodeRelationships.h"
176 #include "KX_SG_BoneParentNodeRelationship.h"
178 #include "BL_ArmatureObject.h"
179 #include "BL_DeformableGameObject.h"
184 //XXX #include "BSE_headerbuttons.h"
185 //XXX void update_for_newframe();
186 //void scene_update_for_newframe(struct Scene *sce, unsigned int lay);
187 //#include "BKE_ipo.h"
188 //void do_all_data_ipos(void);
193 static int default_face_mode = TF_DYNAMIC;
195 static unsigned int KX_rgbaint2uint_new(unsigned int icol)
199 unsigned int integer;
201 } out_color, in_color;
203 in_color.integer = icol;
204 out_color.cp[0] = in_color.cp[3]; // red
205 out_color.cp[1] = in_color.cp[2]; // green
206 out_color.cp[2] = in_color.cp[1]; // blue
207 out_color.cp[3] = in_color.cp[0]; // alpha
209 return out_color.integer;
212 /* Now the real converting starts... */
213 static unsigned int KX_Mcol2uint_new(MCol col)
215 /* color has to be converted without endian sensitivity. So no shifting! */
219 unsigned int integer;
221 } out_color, in_color;
224 out_color.cp[0] = in_color.cp[3]; // red
225 out_color.cp[1] = in_color.cp[2]; // green
226 out_color.cp[2] = in_color.cp[1]; // blue
227 out_color.cp[3] = in_color.cp[0]; // alpha
229 return out_color.integer;
232 static void SetDefaultFaceType(Scene* scene)
234 default_face_mode = TF_DYNAMIC;
238 for(SETLOOPER(scene, sce_iter, base))
240 if (base->object->type == OB_LAMP)
242 default_face_mode = TF_DYNAMIC|TF_LIGHT;
250 static void GetRGB(short type,
259 unsigned int color = 0xFFFFFFFFL;
262 case 0: // vertex colors
265 c0 = KX_Mcol2uint_new(mmcol[0]);
266 c1 = KX_Mcol2uint_new(mmcol[1]);
267 c2 = KX_Mcol2uint_new(mmcol[2]);
269 c3 = KX_Mcol2uint_new(mmcol[3]);
270 }else // backup white
272 c0 = KX_rgbaint2uint_new(color);
273 c1 = KX_rgbaint2uint_new(color);
274 c2 = KX_rgbaint2uint_new(color);
276 c3 = KX_rgbaint2uint_new( color );
281 case 1: // material rgba
286 unsigned int integer;
288 col_converter.cp[3] = (unsigned char) (mat->r*255.0);
289 col_converter.cp[2] = (unsigned char) (mat->g*255.0);
290 col_converter.cp[1] = (unsigned char) (mat->b*255.0);
291 col_converter.cp[0] = (unsigned char) (mat->alpha*255.0);
292 color = col_converter.integer;
294 c0 = KX_rgbaint2uint_new(color);
295 c1 = KX_rgbaint2uint_new(color);
296 c2 = KX_rgbaint2uint_new(color);
298 c3 = KX_rgbaint2uint_new(color);
303 c0 = KX_rgbaint2uint_new(color);
304 c1 = KX_rgbaint2uint_new(color);
305 c2 = KX_rgbaint2uint_new(color);
307 c3 = KX_rgbaint2uint_new(color);
312 typedef struct MTF_localLayer
318 // ------------------------------------
319 bool ConvertMaterial(
320 BL_Material *material,
323 const char *tfaceName,
326 MTF_localLayer *layers,
329 material->Initialize();
330 int numchan = -1, texalpha = 0;
331 bool validmat = (mat!=0);
332 bool validface = (tface!=0);
336 type = 1; // material color
338 material->IdMode = DEFAULT_BLENDER;
339 material->glslmat = (validmat)? glslmat: false;
340 material->materialindex = mface->mat_nr;
342 // --------------------------------
345 // use vertex colors by explicitly setting
346 if(mat->mode &MA_VERTEXCOLP || glslmat)
350 material->ras_mode |= ( mat->mode & MA_SHLESS )?0:USE_LIGHT;
352 numchan = getNumTexChannels(mat);
355 // use the face texture if
356 // 1) it is set in the buttons
357 // 2) we have a face texture and a material but no valid texture in slot 1
358 bool facetex = false;
359 if(validface && mat->mode &MA_FACETEXTURE)
361 if(validface && !mat->mtex[0])
363 if(validface && mat->mtex[0]) {
364 MTex *tmp = mat->mtex[0];
365 if(!tmp->tex || (tmp->tex && !tmp->tex->ima))
368 numchan = numchan>MAXTEX?MAXTEX:numchan;
371 for(int i=0; i<numchan; i++) {
374 if(i==0 && facetex ) {
375 Image*tmp = (Image*)(tface->tpage);
378 material->img[i] = tmp;
379 material->texname[i] = material->img[i]->id.name;
380 material->flag[i] |= ( tface->transp &TF_ALPHA )?USEALPHA:0;
381 material->flag[i] |= ( tface->transp &TF_ADD )?CALCALPHA:0;
382 material->flag[i] |= MIPMAP;
384 if(material->img[i]->flag & IMA_REFLECT)
385 material->mapping[i].mapping |= USEREFL;
388 mttmp = getImageFromMaterial( mat, i );
389 if(mttmp && mttmp->texco &TEXCO_UV)
391 STR_String uvName = mttmp->uvname;
393 if (!uvName.IsEmpty())
394 material->mapping[i].uvCoName = mttmp->uvname;
396 material->mapping[i].uvCoName = "";
398 material->mapping[i].mapping |= USEUV;
401 if(material->ras_mode & USE_LIGHT)
402 material->ras_mode &= ~USE_LIGHT;
403 if(tface->mode & TF_LIGHT)
404 material->ras_mode |= USE_LIGHT;
409 material->img[i] = 0;
410 material->texname[i] = "";
415 mttmp = getImageFromMaterial( mat, i );
418 if( mttmp->tex->type == TEX_IMAGE ) {
419 material->mtexname[i] = mttmp->tex->id.name;
420 material->img[i] = mttmp->tex->ima;
421 if( material->img[i] ) {
423 material->texname[i] = material->img[i]->id.name;
424 material->flag[i] |= ( mttmp->tex->imaflag &TEX_MIPMAP )?MIPMAP:0;
425 // -----------------------
426 if( mttmp->tex->imaflag &TEX_USEALPHA ) {
427 material->flag[i] |= USEALPHA;
429 // -----------------------
430 else if( mttmp->tex->imaflag &TEX_CALCALPHA ) {
431 material->flag[i] |= CALCALPHA;
433 else if(mttmp->tex->flag &TEX_NEGALPHA) {
434 material->flag[i] |= USENEGALPHA;
437 material->color_blend[i] = mttmp->colfac;
438 material->flag[i] |= ( mttmp->mapto & MAP_ALPHA )?TEXALPHA:0;
439 material->flag[i] |= ( mttmp->texflag& MTEX_NEGATIVE )?TEXNEG:0;
441 if(!glslmat && (material->flag[i] & TEXALPHA))
445 else if(mttmp->tex->type == TEX_ENVMAP) {
446 if( mttmp->tex->env->stype == ENV_LOAD ) {
448 material->mtexname[i] = mttmp->tex->id.name;
449 EnvMap *env = mttmp->tex->env;
450 env->ima = mttmp->tex->ima;
451 material->cubemap[i] = env;
453 if (material->cubemap[i])
455 if (!material->cubemap[i]->cube[0])
456 BL_Texture::SplitEnvMap(material->cubemap[i]);
458 material->texname[i]= material->cubemap[i]->ima->id.name;
459 material->mapping[i].mapping |= USEENV;
463 material->flag[i] |= (mat->ipo!=0)?HASIPO:0;
464 /// --------------------------------
466 material->mapping[i].mapping |= ( mttmp->texco & TEXCO_REFL )?USEREFL:0;
468 if(mttmp->texco & TEXCO_OBJECT) {
469 material->mapping[i].mapping |= USEOBJ;
471 material->mapping[i].objconame = mttmp->object->id.name;
473 else if(mttmp->texco &TEXCO_REFL)
474 material->mapping[i].mapping |= USEREFL;
475 else if(mttmp->texco &(TEXCO_ORCO|TEXCO_GLOB))
476 material->mapping[i].mapping |= USEORCO;
477 else if(mttmp->texco &TEXCO_UV)
479 STR_String uvName = mttmp->uvname;
481 if (!uvName.IsEmpty())
482 material->mapping[i].uvCoName = mttmp->uvname;
484 material->mapping[i].uvCoName = "";
485 material->mapping[i].mapping |= USEUV;
487 else if(mttmp->texco &TEXCO_NORM)
488 material->mapping[i].mapping |= USENORM;
489 else if(mttmp->texco &TEXCO_TANGENT)
490 material->mapping[i].mapping |= USETANG;
492 material->mapping[i].mapping |= DISABLE;
494 material->mapping[i].scale[0] = mttmp->size[0];
495 material->mapping[i].scale[1] = mttmp->size[1];
496 material->mapping[i].scale[2] = mttmp->size[2];
497 material->mapping[i].offsets[0] = mttmp->ofs[0];
498 material->mapping[i].offsets[1] = mttmp->ofs[1];
499 material->mapping[i].offsets[2] = mttmp->ofs[2];
501 material->mapping[i].projplane[0] = mttmp->projx;
502 material->mapping[i].projplane[1] = mttmp->projy;
503 material->mapping[i].projplane[2] = mttmp->projz;
504 /// --------------------------------
506 switch( mttmp->blendtype ) {
508 material->blend_mode[i] = BLEND_MIX;
511 material->blend_mode[i] = BLEND_MUL;
514 material->blend_mode[i] = BLEND_ADD;
517 material->blend_mode[i] = BLEND_SUB;
520 material->blend_mode[i] = BLEND_SCR;
528 // above one tex the switches here
530 switch(valid_index) {
532 material->IdMode = DEFAULT_BLENDER;
535 material->IdMode = ONETEX;
538 material->IdMode = GREATERTHAN2;
541 material->SetUsers(mat->id.us);
543 material->num_enabled = valid_index;
545 material->speccolor[0] = mat->specr;
546 material->speccolor[1] = mat->specg;
547 material->speccolor[2] = mat->specb;
548 material->hard = (float)mat->har/4.0f;
549 material->matcolor[0] = mat->r;
550 material->matcolor[1] = mat->g;
551 material->matcolor[2] = mat->b;
552 material->matcolor[3] = mat->alpha;
553 material->alpha = mat->alpha;
554 material->emit = mat->emit;
555 material->spec_f = mat->spec;
556 material->ref = mat->ref;
557 material->amb = mat->amb;
559 material->ras_mode |= (mat->material_type == MA_TYPE_WIRE)? WIRE: 0;
564 // check for tface tex to fallback on
568 if(tface->mode) material->ras_mode |= USE_LIGHT;
570 material->img[0] = (Image*)(tface->tpage);
571 // ------------------------
572 if(material->img[0]) {
573 material->texname[0] = material->img[0]->id.name;
574 material->mapping[0].mapping |= ( (material->img[0]->flag & IMA_REFLECT)!=0 )?USEREFL:0;
575 material->flag[0] |= ( tface->transp &TF_ALPHA )?USEALPHA:0;
576 material->flag[0] |= ( tface->transp &TF_ADD )?CALCALPHA:0;
580 material->SetUsers(-1);
581 material->num_enabled = valid;
582 material->IdMode = TEXFACE;
583 material->speccolor[0] = 1.f;
584 material->speccolor[1] = 1.f;
585 material->speccolor[2] = 1.f;
586 material->hard = 35.f;
587 material->matcolor[0] = 0.5f;
588 material->matcolor[1] = 0.5f;
589 material->matcolor[2] = 0.5f;
590 material->spec_f = 0.5f;
591 material->ref = 0.8f;
595 const char *uvName = "", *uv2Name = "";
598 uv2[0]= uv2[1]= uv2[2]= uv2[3]= MT_Point2(0.0f, 0.0f);
602 material->ras_mode |= (tface->mode & TF_INVISIBLE)?0:POLY_VIS;
604 material->transp = tface->transp;
605 material->tile = tface->tile;
606 material->mode = tface->mode;
608 uv[0].setValue(tface->uv[0]);
609 uv[1].setValue(tface->uv[1]);
610 uv[2].setValue(tface->uv[2]);
613 uv[3].setValue(tface->uv[3]);
619 material->ras_mode |= (POLY_VIS| (validmat?0:USE_LIGHT));
620 material->mode = default_face_mode;
621 material->transp = TF_SOLID;
624 uv[0]= uv[1]= uv[2]= uv[3]= MT_Point2(0.0f, 0.0f);
627 // with ztransp enabled, enforce alpha blending mode
628 if(validmat && (mat->mode & MA_TRANSP) && (mat->mode & MA_ZTRANSP) && (material->transp == TF_SOLID))
629 material->transp = TF_ALPHA;
631 // always zsort alpha + add
632 if((material->transp == TF_ALPHA || material->transp == TF_ADD || texalpha) && (material->transp != TF_CLIP)) {
633 material->ras_mode |= ALPHA;
634 material->ras_mode |= (material->mode & TF_ALPHASORT)? ZSORT: 0;
638 material->ras_mode |= (material->mode & TF_DYNAMIC)? COLLIDER: 0;
640 // these flags are irrelevant at this point, remove so they
641 // don't hurt material bucketing
642 material->mode &= ~(TF_DYNAMIC|TF_ALPHASORT|TF_TEX);
647 bool isFirstSet = true;
649 // only two sets implemented, but any of the eight
650 // sets can make up the two layers
651 for (int vind = 0; vind<material->num_enabled; vind++)
653 BL_Mapping &map = material->mapping[vind];
655 if (map.uvCoName.IsEmpty())
659 for (int lay=0; lay<MAX_MTFACE; lay++)
661 MTF_localLayer& layer = layers[lay];
662 if (layer.face == 0) break;
664 if (strcmp(map.uvCoName.ReadPtr(), layer.name)==0)
668 uvSet[0].setValue(layer.face->uv[0]);
669 uvSet[1].setValue(layer.face->uv[1]);
670 uvSet[2].setValue(layer.face->uv[2]);
673 uvSet[3].setValue(layer.face->uv[3]);
675 uvSet[3].setValue(0.0f, 0.0f);
679 uv[0] = uvSet[0]; uv[1] = uvSet[1];
680 uv[2] = uvSet[2]; uv[3] = uvSet[3];
684 else if(strcmp(layer.name, uvName) != 0)
686 uv2[0] = uvSet[0]; uv2[1] = uvSet[1];
687 uv2[2] = uvSet[2]; uv2[3] = uvSet[3];
688 map.mapping |= USECUSTOMUV;
689 uv2Name = layer.name;
698 GetRGB(type,mface,mmcol,mat,rgb[0],rgb[1],rgb[2], rgb[3]);
700 // swap the material color, so MCol on TF_BMFONT works
701 if (validmat && type==1 && (tface && tface->mode & TF_BMFONT))
703 rgb[0] = KX_rgbaint2uint_new(rgb[0]);
704 rgb[1] = KX_rgbaint2uint_new(rgb[1]);
705 rgb[2] = KX_rgbaint2uint_new(rgb[2]);
706 rgb[3] = KX_rgbaint2uint_new(rgb[3]);
709 material->SetConversionRGB(rgb);
710 material->SetConversionUV(uvName, uv);
711 material->SetConversionUV2(uv2Name, uv2);
714 material->matname =(mat->id.name);
716 material->tface = tface;
717 material->material = mat;
721 /* blenderobj can be NULL, make sure its checked for */
722 RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, KX_Scene* scene, KX_BlenderSceneConverter *converter)
724 RAS_MeshObject *meshobj;
725 int lightlayer = blenderobj ? blenderobj->lay:(1<<20)-1; // all layers if no object.
727 if ((meshobj = converter->FindGameMesh(mesh/*, ob->lay*/)) != NULL)
729 // Get DerivedMesh data
730 DerivedMesh *dm = CDDM_from_mesh(mesh, blenderobj);
732 MVert *mvert = dm->getVertArray(dm);
733 int totvert = dm->getNumVerts(dm);
735 MFace *mface = dm->getFaceArray(dm);
736 MTFace *tface = static_cast<MTFace*>(dm->getFaceDataArray(dm, CD_MTFACE));
737 MCol *mcol = static_cast<MCol*>(dm->getFaceDataArray(dm, CD_MCOL));
738 float (*tangent)[4] = NULL;
739 int totface = dm->getNumFaces(dm);
740 const char *tfaceName = "";
743 DM_add_tangent_layer(dm);
744 tangent = (float(*)[4])dm->getFaceDataArray(dm, CD_TANGENT);
747 meshobj = new RAS_MeshObject(mesh);
749 // Extract avaiable layers
750 MTF_localLayer *layers = new MTF_localLayer[MAX_MTFACE];
751 for (int lay=0; lay<MAX_MTFACE; lay++) {
752 layers[lay].face = 0;
753 layers[lay].name = "";
757 for (int i=0; i<dm->faceData.totlayer; i++)
759 if (dm->faceData.layers[i].type == CD_MTFACE)
761 assert(validLayers <= 8);
763 layers[validLayers].face = (MTFace*)(dm->faceData.layers[i].data);
764 layers[validLayers].name = dm->faceData.layers[i].name;
765 if(tface == layers[validLayers].face)
766 tfaceName = layers[validLayers].name;
771 meshobj->SetName(mesh->id.name + 2);
772 meshobj->m_sharedvertex_map.resize(totvert);
773 RAS_IPolyMaterial* polymat = NULL;
775 // These pointers will hold persistent material structure during the conversion
776 // to avoid countless allocation/deallocation of memory.
777 BL_Material* bl_mat = NULL;
778 KX_BlenderMaterial* kx_blmat = NULL;
779 KX_PolygonMaterial* kx_polymat = NULL;
781 for (int f=0;f<totface;f++,mface++)
784 bool collider = true;
785 MT_Point2 uv0(0.0,0.0),uv1(0.0,0.0),uv2(0.0,0.0),uv3(0.0,0.0);
786 MT_Point2 uv20(0.0,0.0),uv21(0.0,0.0),uv22(0.0,0.0),uv23(0.0,0.0);
787 unsigned int rgb0,rgb1,rgb2,rgb3 = 0;
789 MT_Point3 pt0, pt1, pt2, pt3;
790 MT_Vector3 no0(0,0,0), no1(0,0,0), no2(0,0,0), no3(0,0,0);
791 MT_Vector4 tan0(0,0,0,0), tan1(0,0,0,0), tan2(0,0,0,0), tan3(0,0,0,0);
793 /* get coordinates, normals and tangents */
794 pt0.setValue(mvert[mface->v1].co);
795 pt1.setValue(mvert[mface->v2].co);
796 pt2.setValue(mvert[mface->v3].co);
797 if (mface->v4) pt3.setValue(mvert[mface->v4].co);
799 if(mface->flag & ME_SMOOTH) {
800 float n0[3], n1[3], n2[3], n3[3];
802 normal_short_to_float_v3(n0, mvert[mface->v1].no);
803 normal_short_to_float_v3(n1, mvert[mface->v2].no);
804 normal_short_to_float_v3(n2, mvert[mface->v3].no);
810 normal_short_to_float_v3(n3, mvert[mface->v4].no);
818 normal_quad_v3( fno,mvert[mface->v1].co, mvert[mface->v2].co, mvert[mface->v3].co, mvert[mface->v4].co);
820 normal_tri_v3( fno,mvert[mface->v1].co, mvert[mface->v2].co, mvert[mface->v3].co);
822 no0 = no1 = no2 = no3 = MT_Vector3(fno);
826 tan0 = tangent[f*4 + 0];
827 tan1 = tangent[f*4 + 1];
828 tan2 = tangent[f*4 + 2];
831 tan3 = tangent[f*4 + 3];
834 ma = give_current_material(blenderobj, mface->mat_nr+1);
836 ma = mesh->mat ? mesh->mat[mface->mat_nr]:NULL;
840 bool twoside = false;
842 if(converter->GetMaterials()) {
843 /* do Blender Multitexture and Blender GLSL materials */
847 /* first is the BL_Material */
849 bl_mat = new BL_Material();
850 ConvertMaterial(bl_mat, ma, tface, tfaceName, mface, mcol,
851 layers, converter->GetGLSLMaterials());
853 visible = ((bl_mat->ras_mode & POLY_VIS)!=0);
854 collider = ((bl_mat->ras_mode & COLLIDER)!=0);
855 twoside = ((bl_mat->mode & TF_TWOSIDE)!=0);
857 /* vertex colors and uv's were stored in bl_mat temporarily */
858 bl_mat->GetConversionRGB(rgb);
859 rgb0 = rgb[0]; rgb1 = rgb[1];
860 rgb2 = rgb[2]; rgb3 = rgb[3];
862 bl_mat->GetConversionUV(uv);
863 uv0 = uv[0]; uv1 = uv[1];
864 uv2 = uv[2]; uv3 = uv[3];
866 bl_mat->GetConversionUV2(uv);
867 uv20 = uv[0]; uv21 = uv[1];
868 uv22 = uv[2]; uv23 = uv[3];
870 /* then the KX_BlenderMaterial */
871 if (kx_blmat == NULL)
872 kx_blmat = new KX_BlenderMaterial();
874 kx_blmat->Initialize(scene, bl_mat);
875 polymat = static_cast<RAS_IPolyMaterial*>(kx_blmat);
878 /* do Texture Face materials */
879 Image* bima = (tface)? (Image*)tface->tpage: NULL;
880 imastr = (tface)? (bima? (bima)->id.name : "" ) : "";
883 short mode=0, tile=0;
884 int tilexrep=4,tileyrep = 4;
887 tilexrep = bima->xrep;
888 tileyrep = bima->yrep;
891 /* get tface properties if available */
893 /* TF_DYNAMIC means the polygon is a collision face */
894 collider = ((tface->mode & TF_DYNAMIC) != 0);
895 transp = tface->transp;
899 visible = !(tface->mode & TF_INVISIBLE);
900 twoside = ((tface->mode & TF_TWOSIDE)!=0);
902 uv0.setValue(tface->uv[0]);
903 uv1.setValue(tface->uv[1]);
904 uv2.setValue(tface->uv[2]);
907 uv3.setValue(tface->uv[3]);
910 /* no texfaces, set COLLSION true and everything else FALSE */
911 mode = default_face_mode;
916 /* get vertex colors */
918 /* we have vertex colors */
919 rgb0 = KX_Mcol2uint_new(mcol[0]);
920 rgb1 = KX_Mcol2uint_new(mcol[1]);
921 rgb2 = KX_Mcol2uint_new(mcol[2]);
924 rgb3 = KX_Mcol2uint_new(mcol[3]);
927 /* no vertex colors, take from material, otherwise white */
928 unsigned int color = 0xFFFFFFFFL;
935 unsigned int integer;
938 col_converter.cp[3] = (unsigned char) (ma->r*255.0);
939 col_converter.cp[2] = (unsigned char) (ma->g*255.0);
940 col_converter.cp[1] = (unsigned char) (ma->b*255.0);
941 col_converter.cp[0] = (unsigned char) (ma->alpha*255.0);
943 color = col_converter.integer;
946 rgb0 = KX_rgbaint2uint_new(color);
947 rgb1 = KX_rgbaint2uint_new(color);
948 rgb2 = KX_rgbaint2uint_new(color);
951 rgb3 = KX_rgbaint2uint_new(color);
954 // only zsort alpha + add
955 bool alpha = (transp == TF_ALPHA || transp == TF_ADD);
956 bool zsort = (mode & TF_ALPHASORT)? alpha: 0;
958 if (kx_polymat == NULL)
959 kx_polymat = new KX_PolygonMaterial();
960 kx_polymat->Initialize(imastr, ma, (int)mface->mat_nr,
961 tile, tilexrep, tileyrep,
962 mode, transp, alpha, zsort, lightlayer, tface, (unsigned int*)mcol);
963 polymat = static_cast<RAS_IPolyMaterial*>(kx_polymat);
966 polymat->m_specular = MT_Vector3(ma->specr, ma->specg, ma->specb)*ma->spec;
967 polymat->m_shininess = (float)ma->har/4.0f; // 0 < ma->har <= 512
968 polymat->m_diffuse = MT_Vector3(ma->r, ma->g, ma->b)*(ma->emit + ma->ref);
971 polymat->m_specular.setValue(0.0f,0.0f,0.0f);
972 polymat->m_shininess = 35.0;
976 /* mark face as flat, so vertices are split */
977 bool flat = (mface->flag & ME_SMOOTH) == 0;
979 // see if a bucket was reused or a new one was created
980 // this way only one KX_BlenderMaterial object has to exist per bucket
982 RAS_MaterialBucket* bucket = scene->FindBucket(polymat, bucketCreated);
984 // this is needed to free up memory afterwards
985 converter->RegisterPolyMaterial(polymat);
986 if(converter->GetMaterials()) {
987 converter->RegisterBlenderMaterial(bl_mat);
988 // the poly material has been stored in the bucket, next time we must create a new one
992 // the poly material has been stored in the bucket, next time we must create a new one
996 // from now on, use the polygon material from the material bucket
997 polymat = bucket->GetPolyMaterial();
998 // keep the material pointers, they will be reused for next face
1001 int nverts = (mface->v4)? 4: 3;
1002 RAS_Polygon *poly = meshobj->AddPolygon(bucket, nverts);
1004 poly->SetVisible(visible);
1005 poly->SetCollider(collider);
1006 poly->SetTwoside(twoside);
1007 //poly->SetEdgeCode(mface->edcode);
1009 meshobj->AddVertex(poly,0,pt0,uv0,uv20,tan0,rgb0,no0,flat,mface->v1);
1010 meshobj->AddVertex(poly,1,pt1,uv1,uv21,tan1,rgb1,no1,flat,mface->v2);
1011 meshobj->AddVertex(poly,2,pt2,uv2,uv22,tan2,rgb2,no2,flat,mface->v3);
1014 meshobj->AddVertex(poly,3,pt3,uv3,uv23,tan3,rgb3,no3,flat,mface->v4);
1022 for (int lay=0; lay<MAX_MTFACE; lay++)
1024 MTF_localLayer &layer = layers[lay];
1025 if (layer.face == 0) break;
1030 // keep meshobj->m_sharedvertex_map for reinstance phys mesh.
1031 // 2.49a and before it did: meshobj->m_sharedvertex_map.clear();
1032 // but this didnt save much ram. - Campbell
1033 meshobj->EndConversion();
1035 // pre calculate texture generation
1036 for(list<RAS_MeshMaterial>::iterator mit = meshobj->GetFirstMaterial();
1037 mit != meshobj->GetLastMaterial(); ++ mit) {
1038 mit->m_bucket->GetPolyMaterial()->OnConstruction(lightlayer);
1052 converter->RegisterGameMesh(meshobj, mesh);
1058 static PHY_MaterialProps *CreateMaterialFromBlenderObject(struct Object* blenderobject)
1060 PHY_MaterialProps *materialProps = new PHY_MaterialProps;
1062 MT_assert(materialProps && "Create physics material properties failed");
1064 Material* blendermat = give_current_material(blenderobject, 0);
1068 MT_assert(0.0f <= blendermat->reflect && blendermat->reflect <= 1.0f);
1070 materialProps->m_restitution = blendermat->reflect;
1071 materialProps->m_friction = blendermat->friction;
1072 materialProps->m_fh_spring = blendermat->fh;
1073 materialProps->m_fh_damping = blendermat->xyfrict;
1074 materialProps->m_fh_distance = blendermat->fhdist;
1075 materialProps->m_fh_normal = (blendermat->dynamode & MA_FH_NOR) != 0;
1078 //give some defaults
1079 materialProps->m_restitution = 0.f;
1080 materialProps->m_friction = 0.5;
1081 materialProps->m_fh_spring = 0.f;
1082 materialProps->m_fh_damping = 0.f;
1083 materialProps->m_fh_distance = 0.f;
1084 materialProps->m_fh_normal = false;
1088 return materialProps;
1091 static PHY_ShapeProps *CreateShapePropsFromBlenderObject(struct Object* blenderobject)
1093 PHY_ShapeProps *shapeProps = new PHY_ShapeProps;
1095 MT_assert(shapeProps);
1097 shapeProps->m_mass = blenderobject->mass;
1099 // This needs to be fixed in blender. For now, we use:
1101 // in Blender, inertia stands for the size value which is equivalent to
1102 // the sphere radius
1103 shapeProps->m_inertia = blenderobject->formfactor;
1105 MT_assert(0.0f <= blenderobject->damping && blenderobject->damping <= 1.0f);
1106 MT_assert(0.0f <= blenderobject->rdamping && blenderobject->rdamping <= 1.0f);
1108 shapeProps->m_lin_drag = 1.0 - blenderobject->damping;
1109 shapeProps->m_ang_drag = 1.0 - blenderobject->rdamping;
1111 shapeProps->m_friction_scaling[0] = blenderobject->anisotropicFriction[0];
1112 shapeProps->m_friction_scaling[1] = blenderobject->anisotropicFriction[1];
1113 shapeProps->m_friction_scaling[2] = blenderobject->anisotropicFriction[2];
1114 shapeProps->m_do_anisotropic = ((blenderobject->gameflag & OB_ANISOTROPIC_FRICTION) != 0);
1116 shapeProps->m_do_fh = (blenderobject->gameflag & OB_DO_FH) != 0;
1117 shapeProps->m_do_rot_fh = (blenderobject->gameflag & OB_ROT_FH) != 0;
1119 // velocity clamping XXX
1120 shapeProps->m_clamp_vel_min = blenderobject->min_vel;
1121 shapeProps->m_clamp_vel_max = blenderobject->max_vel;
1130 //////////////////////////////////////////////////////////
1134 static float my_boundbox_mesh(Mesh *me, float *loc, float *size)
1138 float min[3], max[3];
1139 float mloc[3], msize[3];
1140 float radius=0.0f, vert_radius, *co;
1143 if(me->bb==0) me->bb= (struct BoundBox *)MEM_callocN(sizeof(BoundBox), "boundbox");
1146 INIT_MINMAX(min, max);
1148 if (!loc) loc= mloc;
1149 if (!size) size= msize;
1152 for(a=0; a<me->totvert; a++, mvert++) {
1156 DO_MINMAX(co, min, max);
1159 vert_radius= co[0]*co[0] + co[1]*co[1] + co[2]*co[2];
1160 if (vert_radius > radius)
1161 radius= vert_radius;
1165 loc[0]= (min[0]+max[0])/2.0f;
1166 loc[1]= (min[1]+max[1])/2.0f;
1167 loc[2]= (min[2]+max[2])/2.0f;
1169 size[0]= (max[0]-min[0])/2.0f;
1170 size[1]= (max[1]-min[1])/2.0f;
1171 size[2]= (max[2]-min[2])/2.0f;
1174 loc[0]= loc[1]= loc[2]= 0.0f;
1175 size[0]= size[1]= size[2]= 0.0f;
1178 bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= loc[0]-size[0];
1179 bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= loc[0]+size[0];
1181 bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= loc[1]-size[1];
1182 bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= loc[1]+size[1];
1184 bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= loc[2]-size[2];
1185 bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= loc[2]+size[2];
1187 return sqrt(radius);
1193 static void my_tex_space_mesh(Mesh *me)
1196 float *fp, loc[3], size[3], min[3], max[3];
1199 my_boundbox_mesh(me, loc, size);
1201 if(me->texflag & AUTOSPACE) {
1203 kb= me->key->refkey;
1206 INIT_MINMAX(min, max);
1208 fp= (float *)kb->data;
1209 for(a=0; a<kb->totelem; a++, fp+=3) {
1210 DO_MINMAX(fp, min, max);
1213 loc[0]= (min[0]+max[0])/2.0f; loc[1]= (min[1]+max[1])/2.0f; loc[2]= (min[2]+max[2])/2.0f;
1214 size[0]= (max[0]-min[0])/2.0f; size[1]= (max[1]-min[1])/2.0f; size[2]= (max[2]-min[2])/2.0f;
1217 loc[0]= loc[1]= loc[2]= 0.0;
1218 size[0]= size[1]= size[2]= 0.0;
1224 VECCOPY(me->loc, loc);
1225 VECCOPY(me->size, size);
1226 me->rot[0]= me->rot[1]= me->rot[2]= 0.0f;
1228 if(me->size[0]==0.0) me->size[0]= 1.0f;
1229 else if(me->size[0]>0.0 && me->size[0]< 0.00001f) me->size[0]= 0.00001f;
1230 else if(me->size[0]<0.0 && me->size[0]> -0.00001f) me->size[0]= -0.00001f;
1232 if(me->size[1]==0.0) me->size[1]= 1.0f;
1233 else if(me->size[1]>0.0 && me->size[1]< 0.00001f) me->size[1]= 0.00001f;
1234 else if(me->size[1]<0.0 && me->size[1]> -0.00001f) me->size[1]= -0.00001f;
1236 if(me->size[2]==0.0) me->size[2]= 1.0f;
1237 else if(me->size[2]>0.0 && me->size[2]< 0.00001f) me->size[2]= 0.00001f;
1238 else if(me->size[2]<0.0 && me->size[2]> -0.00001f) me->size[2]= -0.00001f;
1243 static void my_get_local_bounds(Object *ob, DerivedMesh *dm, float *center, float *size)
1246 /* uses boundbox, function used by Ketsji */
1252 float min_r[3], max_r[3];
1253 INIT_MINMAX(min_r, max_r);
1254 dm->getMinMax(dm, min_r, max_r);
1255 size[0]= 0.5f*fabsf(max_r[0] - min_r[0]);
1256 size[1]= 0.5f*fabsf(max_r[1] - min_r[1]);
1257 size[2]= 0.5f*fabsf(max_r[2] - min_r[2]);
1259 center[0]= 0.5f*(max_r[0] + min_r[0]);
1260 center[1]= 0.5f*(max_r[1] + min_r[1]);
1261 center[2]= 0.5f*(max_r[2] + min_r[2]);
1265 bb= ( (Mesh *)ob->data )->bb;
1268 my_tex_space_mesh((struct Mesh *)ob->data);
1269 bb= ( (Mesh *)ob->data )->bb;
1275 center[0]= center[1]= center[2]= 0.0;
1276 size[0] = size[1]=size[2]=0.0;
1279 center[0]= center[1]= center[2]= 0.0;
1280 size[0] = size[1]=size[2]=1.0;
1289 center[0]= center[1]= center[2]= 0.0;
1290 size[0] = size[1]=size[2]=1.0;
1294 size[0]= 0.5f*fabs(bb->vec[0][0] - bb->vec[4][0]);
1295 size[1]= 0.5f*fabs(bb->vec[0][1] - bb->vec[2][1]);
1296 size[2]= 0.5f*fabs(bb->vec[0][2] - bb->vec[1][2]);
1298 center[0]= 0.5f*(bb->vec[0][0] + bb->vec[4][0]);
1299 center[1]= 0.5f*(bb->vec[0][1] + bb->vec[2][1]);
1300 center[2]= 0.5f*(bb->vec[0][2] + bb->vec[1][2]);
1307 //////////////////////////////////////////////////////
1310 void BL_CreateGraphicObjectNew(KX_GameObject* gameobj,
1311 const MT_Point3& localAabbMin,
1312 const MT_Point3& localAabbMax,
1315 e_PhysicsEngine physics_engine)
1317 if (gameobj->GetMeshCount() > 0)
1319 switch (physics_engine)
1324 CcdPhysicsEnvironment* env = (CcdPhysicsEnvironment*)kxscene->GetPhysicsEnvironment();
1326 PHY_IMotionState* motionstate = new KX_MotionState(gameobj->GetSGNode());
1327 CcdGraphicController* ctrl = new CcdGraphicController(env, motionstate);
1328 gameobj->SetGraphicController(ctrl);
1329 ctrl->setNewClientInfo(gameobj->getClientInfo());
1330 ctrl->setLocalAabb(localAabbMin, localAabbMax);
1332 // add first, this will create the proxy handle, only if the object is visible
1333 if (gameobj->GetVisible())
1334 env->addCcdGraphicController(ctrl);
1335 // update the mesh if there is a deformer, this will also update the bounding box for modifiers
1336 RAS_Deformer* deformer = gameobj->GetDeformer();
1338 deformer->UpdateBuckets();
1349 void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
1350 struct Object* blenderobject,
1351 RAS_MeshObject* meshobj,
1353 int activeLayerBitInfo,
1354 e_PhysicsEngine physics_engine,
1355 KX_BlenderSceneConverter *converter,
1356 bool processCompoundChildren
1360 //SYS_SystemHandle syshandle = SYS_GetSystem(); /*unused*/
1361 //int userigidbody = SYS_GetCommandLineInt(syshandle,"norigidbody",0);
1362 //bool bRigidBody = (userigidbody == 0);
1364 // object has physics representation?
1365 if (!(blenderobject->gameflag & OB_COLLISION))
1368 // get Root Parent of blenderobject
1369 struct Object* parent= blenderobject->parent;
1370 while(parent && parent->parent) {
1371 parent= parent->parent;
1374 bool isCompoundChild = false;
1375 bool hasCompoundChildren = !parent && (blenderobject->gameflag & OB_CHILD);
1377 /* When the parent is not OB_DYNAMIC and has no OB_COLLISION then it gets no bullet controller
1378 * and cant be apart of the parents compound shape */
1379 if (parent && (parent->gameflag & (OB_DYNAMIC | OB_COLLISION))) {
1381 if ((parent->gameflag & OB_CHILD) != 0 && (blenderobject->gameflag & OB_CHILD))
1383 isCompoundChild = true;
1386 if (processCompoundChildren != isCompoundChild)
1390 PHY_ShapeProps* shapeprops =
1391 CreateShapePropsFromBlenderObject(blenderobject);
1394 PHY_MaterialProps* smmaterial =
1395 CreateMaterialFromBlenderObject(blenderobject);
1397 KX_ObjectProperties objprop;
1398 objprop.m_lockXaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_X_AXIS) !=0;
1399 objprop.m_lockYaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Y_AXIS) !=0;
1400 objprop.m_lockZaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Z_AXIS) !=0;
1401 objprop.m_lockXRotaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_X_ROT_AXIS) !=0;
1402 objprop.m_lockYRotaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Y_ROT_AXIS) !=0;
1403 objprop.m_lockZRotaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Z_ROT_AXIS) !=0;
1405 objprop.m_isCompoundChild = isCompoundChild;
1406 objprop.m_hasCompoundChildren = hasCompoundChildren;
1407 objprop.m_margin = blenderobject->margin;
1409 // ACTOR is now a separate feature
1410 objprop.m_isactor = (blenderobject->gameflag & OB_ACTOR)!=0;
1411 objprop.m_dyna = (blenderobject->gameflag & OB_DYNAMIC) != 0;
1412 objprop.m_softbody = (blenderobject->gameflag & OB_SOFT_BODY) != 0;
1413 objprop.m_angular_rigidbody = (blenderobject->gameflag & OB_RIGID_BODY) != 0;
1415 ///contact processing threshold is only for rigid bodies and static geometry, not 'dynamic'
1416 if (objprop.m_angular_rigidbody || !objprop.m_dyna )
1418 objprop.m_contactProcessingThreshold = blenderobject->m_contactProcessingThreshold;
1421 objprop.m_contactProcessingThreshold = 0.f;
1424 objprop.m_sensor = (blenderobject->gameflag & OB_SENSOR) != 0;
1426 if (objprop.m_softbody)
1428 ///for game soft bodies
1429 if (blenderobject->bsoft)
1431 objprop.m_gamesoftFlag = blenderobject->bsoft->flag;
1433 objprop.m_soft_linStiff = blenderobject->bsoft->linStiff;
1434 objprop.m_soft_angStiff = blenderobject->bsoft->angStiff; /* angular stiffness 0..1 */
1435 objprop.m_soft_volume= blenderobject->bsoft->volume; /* volume preservation 0..1 */
1437 objprop.m_soft_viterations= blenderobject->bsoft->viterations; /* Velocities solver iterations */
1438 objprop.m_soft_piterations= blenderobject->bsoft->piterations; /* Positions solver iterations */
1439 objprop.m_soft_diterations= blenderobject->bsoft->diterations; /* Drift solver iterations */
1440 objprop.m_soft_citerations= blenderobject->bsoft->citerations; /* Cluster solver iterations */
1442 objprop.m_soft_kSRHR_CL= blenderobject->bsoft->kSRHR_CL; /* Soft vs rigid hardness [0,1] (cluster only) */
1443 objprop.m_soft_kSKHR_CL= blenderobject->bsoft->kSKHR_CL; /* Soft vs kinetic hardness [0,1] (cluster only) */
1444 objprop.m_soft_kSSHR_CL= blenderobject->bsoft->kSSHR_CL; /* Soft vs soft hardness [0,1] (cluster only) */
1445 objprop.m_soft_kSR_SPLT_CL= blenderobject->bsoft->kSR_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */
1447 objprop.m_soft_kSK_SPLT_CL= blenderobject->bsoft->kSK_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */
1448 objprop.m_soft_kSS_SPLT_CL= blenderobject->bsoft->kSS_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */
1449 objprop.m_soft_kVCF= blenderobject->bsoft->kVCF; /* Velocities correction factor (Baumgarte) */
1450 objprop.m_soft_kDP= blenderobject->bsoft->kDP; /* Damping coefficient [0,1] */
1452 objprop.m_soft_kDG= blenderobject->bsoft->kDG; /* Drag coefficient [0,+inf] */
1453 objprop.m_soft_kLF= blenderobject->bsoft->kLF; /* Lift coefficient [0,+inf] */
1454 objprop.m_soft_kPR= blenderobject->bsoft->kPR; /* Pressure coefficient [-inf,+inf] */
1455 objprop.m_soft_kVC= blenderobject->bsoft->kVC; /* Volume conversation coefficient [0,+inf] */
1457 objprop.m_soft_kDF= blenderobject->bsoft->kDF; /* Dynamic friction coefficient [0,1] */
1458 objprop.m_soft_kMT= blenderobject->bsoft->kMT; /* Pose matching coefficient [0,1] */
1459 objprop.m_soft_kCHR= blenderobject->bsoft->kCHR; /* Rigid contacts hardness [0,1] */
1460 objprop.m_soft_kKHR= blenderobject->bsoft->kKHR; /* Kinetic contacts hardness [0,1] */
1462 objprop.m_soft_kSHR= blenderobject->bsoft->kSHR; /* Soft contacts hardness [0,1] */
1463 objprop.m_soft_kAHR= blenderobject->bsoft->kAHR; /* Anchors hardness [0,1] */
1464 objprop.m_soft_collisionflags= blenderobject->bsoft->collisionflags; /* Vertex/Face or Signed Distance Field(SDF) or Clusters, Soft versus Soft or Rigid */
1465 objprop.m_soft_numclusteriterations= blenderobject->bsoft->numclusteriterations; /* number of iterations to refine collision clusters*/
1466 //objprop.m_soft_welding = blenderobject->bsoft->welding; /* welding */
1467 /* disable welding: it doesn't bring any additional stability and it breaks the relation between soft body collision shape and graphic mesh */
1468 objprop.m_soft_welding = 0.f;
1469 objprop.m_margin = blenderobject->bsoft->margin;
1470 objprop.m_contactProcessingThreshold = 0.f;
1473 objprop.m_gamesoftFlag = OB_BSB_BENDING_CONSTRAINTS | OB_BSB_SHAPE_MATCHING | OB_BSB_AERO_VPOINT;
1475 objprop.m_soft_linStiff = 0.5;;
1476 objprop.m_soft_angStiff = 1.f; /* angular stiffness 0..1 */
1477 objprop.m_soft_volume= 1.f; /* volume preservation 0..1 */
1480 objprop.m_soft_viterations= 0;
1481 objprop.m_soft_piterations= 1;
1482 objprop.m_soft_diterations= 0;
1483 objprop.m_soft_citerations= 4;
1485 objprop.m_soft_kSRHR_CL= 0.1f;
1486 objprop.m_soft_kSKHR_CL= 1.f;
1487 objprop.m_soft_kSSHR_CL= 0.5;
1488 objprop.m_soft_kSR_SPLT_CL= 0.5f;
1490 objprop.m_soft_kSK_SPLT_CL= 0.5f;
1491 objprop.m_soft_kSS_SPLT_CL= 0.5f;
1492 objprop.m_soft_kVCF= 1;
1493 objprop.m_soft_kDP= 0;
1495 objprop.m_soft_kDG= 0;
1496 objprop.m_soft_kLF= 0;
1497 objprop.m_soft_kPR= 0;
1498 objprop.m_soft_kVC= 0;
1500 objprop.m_soft_kDF= 0.2f;
1501 objprop.m_soft_kMT= 0.05f;
1502 objprop.m_soft_kCHR= 1.0f;
1503 objprop.m_soft_kKHR= 0.1f;
1505 objprop.m_soft_kSHR= 1.f;
1506 objprop.m_soft_kAHR= 0.7f;
1507 objprop.m_soft_collisionflags= OB_BSB_COL_SDF_RS + OB_BSB_COL_VF_SS;
1508 objprop.m_soft_numclusteriterations= 16;
1509 objprop.m_soft_welding = 0.f;
1510 objprop.m_margin = 0.f;
1511 objprop.m_contactProcessingThreshold = 0.f;
1515 objprop.m_ghost = (blenderobject->gameflag & OB_GHOST) != 0;
1516 objprop.m_disableSleeping = (blenderobject->gameflag & OB_COLLISION_RESPONSE) != 0;//abuse the OB_COLLISION_RESPONSE flag
1517 //mmm, for now, taks this for the size of the dynamicobject
1518 // Blender uses inertia for radius of dynamic object
1519 objprop.m_radius = blenderobject->inertia;
1520 objprop.m_in_active_layer = (blenderobject->lay & activeLayerBitInfo) != 0;
1521 objprop.m_dynamic_parent=NULL;
1522 objprop.m_isdeformable = ((blenderobject->gameflag2 & 2)) != 0;
1523 objprop.m_boundclass = objprop.m_dyna?KX_BOUNDSPHERE:KX_BOUNDMESH;
1525 if ((blenderobject->gameflag & OB_SOFT_BODY) && !(blenderobject->gameflag & OB_BOUNDS))
1527 objprop.m_boundclass = KX_BOUNDMESH;
1531 DerivedMesh* dm = NULL;
1532 if (gameobj->GetDeformer())
1533 dm = gameobj->GetDeformer()->GetPhysicsMesh();
1534 my_get_local_bounds(blenderobject,dm,objprop.m_boundobject.box.m_center,bb.m_extends);
1535 if (blenderobject->gameflag & OB_BOUNDS)
1537 switch (blenderobject->boundtype)
1540 objprop.m_boundclass = KX_BOUNDBOX;
1541 //mmm, has to be divided by 2 to be proper extends
1542 objprop.m_boundobject.box.m_extends[0]=2.f*bb.m_extends[0];
1543 objprop.m_boundobject.box.m_extends[1]=2.f*bb.m_extends[1];
1544 objprop.m_boundobject.box.m_extends[2]=2.f*bb.m_extends[2];
1546 case OB_BOUND_POLYT:
1547 if (blenderobject->type == OB_MESH)
1549 objprop.m_boundclass = KX_BOUNDPOLYTOPE;
1552 // Object is not a mesh... fall through OB_BOUND_POLYH to
1554 case OB_BOUND_POLYH:
1555 if (blenderobject->type == OB_MESH)
1557 objprop.m_boundclass = KX_BOUNDMESH;
1560 // Object is not a mesh... can't use polyhedron.
1561 // Fall through and become a sphere.
1562 case OB_BOUND_SPHERE:
1564 objprop.m_boundclass = KX_BOUNDSPHERE;
1565 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], MT_max(bb.m_extends[1], bb.m_extends[2]));
1568 case OB_BOUND_CYLINDER:
1570 objprop.m_boundclass = KX_BOUNDCYLINDER;
1571 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], bb.m_extends[1]);
1572 objprop.m_boundobject.c.m_height = 2.f*bb.m_extends[2];
1577 objprop.m_boundclass = KX_BOUNDCONE;
1578 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], bb.m_extends[1]);
1579 objprop.m_boundobject.c.m_height = 2.f*bb.m_extends[2];
1582 case OB_BOUND_CAPSULE:
1584 objprop.m_boundclass = KX_BOUNDCAPSULE;
1585 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], bb.m_extends[1]);
1586 objprop.m_boundobject.c.m_height = 2.f*(bb.m_extends[2]-objprop.m_boundobject.c.m_radius);
1587 if (objprop.m_boundobject.c.m_height < 0.f)
1588 objprop.m_boundobject.c.m_height = 0.f;
1595 if (parent/* && (parent->gameflag & OB_DYNAMIC)*/) {
1596 // parented object cannot be dynamic
1597 KX_GameObject *parentgameobject = converter->FindGameObject(parent);
1598 objprop.m_dynamic_parent = parentgameobject;
1599 //cannot be dynamic:
1600 objprop.m_dyna = false;
1601 objprop.m_softbody = false;
1602 shapeprops->m_mass = 0.f;
1606 objprop.m_concave = (blenderobject->boundtype & 4) != 0;
1608 switch (physics_engine)
1612 KX_ConvertBulletObject(gameobj, meshobj, dm, kxscene, shapeprops, smmaterial, &objprop);
1617 //KX_ConvertDynamoObject(gameobj,meshobj,kxscene,shapeprops, smmaterial, &objprop);
1636 static KX_LightObject *gamelight_from_blamp(Object *ob, Lamp *la, unsigned int layerflag, KX_Scene *kxscene, RAS_IRenderTools *rendertools, KX_BlenderSceneConverter *converter) {
1637 RAS_LightObject lightobj;
1638 KX_LightObject *gamelight;
1640 lightobj.m_att1 = la->att1;
1641 lightobj.m_att2 = (la->mode & LA_QUAD) ? la->att2 : 0.0f;
1642 lightobj.m_red = la->r;
1643 lightobj.m_green = la->g;
1644 lightobj.m_blue = la->b;
1645 lightobj.m_distance = la->dist;
1646 lightobj.m_energy = la->energy;
1647 lightobj.m_layer = layerflag;
1648 lightobj.m_spotblend = la->spotblend;
1649 lightobj.m_spotsize = la->spotsize;
1651 lightobj.m_nodiffuse = (la->mode & LA_NO_DIFF) != 0;
1652 lightobj.m_nospecular = (la->mode & LA_NO_SPEC) != 0;
1654 bool glslmat = converter->GetGLSLMaterials();
1656 // in GLSL NEGATIVE LAMP is handled inside the lamp update function
1658 if (la->mode & LA_NEG)
1660 lightobj.m_red = -lightobj.m_red;
1661 lightobj.m_green = -lightobj.m_green;
1662 lightobj.m_blue = -lightobj.m_blue;
1666 if (la->type==LA_SUN) {
1667 lightobj.m_type = RAS_LightObject::LIGHT_SUN;
1668 } else if (la->type==LA_SPOT) {
1669 lightobj.m_type = RAS_LightObject::LIGHT_SPOT;
1671 lightobj.m_type = RAS_LightObject::LIGHT_NORMAL;
1674 gamelight = new KX_LightObject(kxscene, KX_Scene::m_callbacks, rendertools,
1677 BL_ConvertLampIpos(la, gamelight, converter);
1682 static KX_Camera *gamecamera_from_bcamera(Object *ob, KX_Scene *kxscene, KX_BlenderSceneConverter *converter) {
1683 Camera* ca = static_cast<Camera*>(ob->data);
1684 RAS_CameraData camdata(ca->lens, ca->ortho_scale, ca->clipsta, ca->clipend, ca->type == CAM_PERSP, ca->YF_dofdist);
1685 KX_Camera *gamecamera;
1687 gamecamera= new KX_Camera(kxscene, KX_Scene::m_callbacks, camdata);
1688 gamecamera->SetName(ca->id.name + 2);
1690 BL_ConvertCameraIpos(ca, gamecamera, converter);
1695 static KX_GameObject *gameobject_from_blenderobject(
1698 RAS_IRenderTools *rendertools,
1699 KX_BlenderSceneConverter *converter)
1701 KX_GameObject *gameobj = NULL;
1707 KX_LightObject* gamelight= gamelight_from_blamp(ob, static_cast<Lamp*>(ob->data), ob->lay, kxscene, rendertools, converter);
1708 gameobj = gamelight;
1710 gamelight->AddRef();
1711 kxscene->GetLightList()->Add(gamelight);
1718 KX_Camera* gamecamera = gamecamera_from_bcamera(ob, kxscene, converter);
1719 gameobj = gamecamera;
1721 //don't add a reference: the camera list in kxscene->m_cameras is not released at the end
1722 //gamecamera->AddRef();
1723 kxscene->AddCamera(gamecamera);
1730 Mesh* mesh = static_cast<Mesh*>(ob->data);
1731 float center[3], extents[3];
1732 float radius = my_boundbox_mesh((Mesh*) ob->data, center, extents);
1733 RAS_MeshObject* meshobj = BL_ConvertMesh(mesh,ob,kxscene,converter);
1735 // needed for python scripting
1736 kxscene->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
1738 gameobj = new BL_DeformableGameObject(ob,kxscene,KX_Scene::m_callbacks);
1740 // set transformation
1741 gameobj->AddMesh(meshobj);
1743 // for all objects: check whether they want to
1744 // respond to updates
1745 bool ignoreActivityCulling =
1746 ((ob->gameflag2 & OB_NEVER_DO_ACTIVITY_CULLING)!=0);
1747 gameobj->SetIgnoreActivityCulling(ignoreActivityCulling);
1748 gameobj->SetOccluder((ob->gameflag & OB_OCCLUDER) != 0, false);
1750 // two options exists for deform: shape keys and armature
1751 // only support relative shape key
1752 bool bHasShapeKey = mesh->key != NULL && mesh->key->type==KEY_RELATIVE;
1753 bool bHasDvert = mesh->dvert != NULL && ob->defbase.first;
1754 bool bHasArmature = (BL_ModifierDeformer::HasArmatureDeformer(ob) && ob->parent && ob->parent->type == OB_ARMATURE && bHasDvert);
1755 bool bHasModifier = BL_ModifierDeformer::HasCompatibleDeformer(ob);
1757 bool bHasSoftBody = (!ob->parent && (ob->gameflag & OB_SOFT_BODY));
1760 BL_ModifierDeformer *dcont = new BL_ModifierDeformer((BL_DeformableGameObject *)gameobj,
1761 kxscene->GetBlenderScene(), ob, meshobj);
1762 ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1763 if (bHasShapeKey && bHasArmature)
1764 dcont->LoadShapeDrivers(ob->parent);
1765 } else if (bHasShapeKey) {
1766 // not that we can have shape keys without dvert!
1767 BL_ShapeDeformer *dcont = new BL_ShapeDeformer((BL_DeformableGameObject*)gameobj,
1769 ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1771 dcont->LoadShapeDrivers(ob->parent);
1772 } else if (bHasArmature) {
1773 BL_SkinDeformer *dcont = new BL_SkinDeformer((BL_DeformableGameObject*)gameobj,
1775 ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1776 } else if (bHasDvert) {
1777 // this case correspond to a mesh that can potentially deform but not with the
1778 // object to which it is attached for the moment. A skin mesh was created in
1779 // BL_ConvertMesh() so must create a deformer too!
1780 BL_MeshDeformer *dcont = new BL_MeshDeformer((BL_DeformableGameObject*)gameobj,
1782 ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1784 } else if (bHasSoftBody) {
1785 KX_SoftBodyDeformer *dcont = new KX_SoftBodyDeformer(meshobj, (BL_DeformableGameObject*)gameobj);
1786 ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1790 MT_Point3 min = MT_Point3(center) - MT_Vector3(extents);
1791 MT_Point3 max = MT_Point3(center) + MT_Vector3(extents);
1792 SG_BBox bbox = SG_BBox(min, max);
1793 gameobj->GetSGNode()->SetBBox(bbox);
1794 gameobj->GetSGNode()->SetRadius(radius);
1801 gameobj = new BL_ArmatureObject(
1803 KX_Scene::m_callbacks,
1805 kxscene->GetBlenderScene() // handle
1807 /* Get the current pose from the armature object and apply it as the rest pose */
1813 gameobj = new KX_EmptyObject(kxscene,KX_Scene::m_callbacks);
1814 // set transformation
1820 /* font objects have no bounding box */
1821 gameobj = new KX_FontObject(kxscene,KX_Scene::m_callbacks, rendertools, ob);
1823 /* add to the list only the visible fonts */
1824 if((ob->lay & kxscene->GetBlenderScene()->lay) != 0)
1825 kxscene->AddFont(static_cast<KX_FontObject*>(gameobj));
1832 gameobj->SetLayer(ob->lay);
1833 gameobj->SetBlenderObject(ob);
1834 gameobj->SetObjectColor(ob->col);
1835 /* set the visibility state based on the objects render option in the outliner */
1836 if(ob->restrictflag & OB_RESTRICT_RENDER) gameobj->SetVisible(0, 0);
1841 struct parentChildLink {
1842 struct Object* m_blenderchild;
1843 SG_Node* m_gamechildnode;
1846 #include "DNA_constraint_types.h"
1847 //XXX #include "BIF_editconstraint.h"
1849 bPoseChannel *get_active_posechannel2 (Object *ob)
1851 bArmature *arm= (bArmature*)ob->data;
1852 bPoseChannel *pchan;
1855 for(pchan= (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1856 if(pchan->bone && (pchan->bone == arm->act_bone) && (pchan->bone->layer & arm->layer))
1863 ListBase *get_active_constraints2(Object *ob)
1868 // XXX - shouldnt we care about the pose data and not the mode???
1869 if (ob->mode & OB_MODE_POSE) {
1870 bPoseChannel *pchan;
1872 pchan = get_active_posechannel2(ob);
1874 return &pchan->constraints;
1877 return &ob->constraints;
1883 void RBJconstraints(Object *ob)//not used
1886 bConstraint *curcon;
1888 conlist = get_active_constraints2(ob);
1891 for (curcon = (bConstraint *)conlist->first; curcon; curcon=(bConstraint *)curcon->next) {
1893 printf("%i\n",curcon->type);
1900 #include "PHY_IPhysicsEnvironment.h"
1901 #include "KX_IPhysicsController.h"
1902 #include "PHY_DynamicTypes.h"
1904 KX_IPhysicsController* getPhId(CListValue* sumolist,STR_String busc){//not used
1906 for (int j=0;j<sumolist->GetCount();j++)
1908 KX_GameObject* gameobje = (KX_GameObject*) sumolist->GetValue(j);
1909 if (gameobje->GetName()==busc)
1910 return gameobje->GetPhysicsController();
1917 KX_GameObject* getGameOb(STR_String busc,CListValue* sumolist){
1919 for (int j=0;j<sumolist->GetCount();j++)
1921 KX_GameObject* gameobje = (KX_GameObject*) sumolist->GetValue(j);
1922 if (gameobje->GetName()==busc)
1930 // convert blender objects into ketsji gameobjects
1931 void BL_ConvertBlenderObjects(struct Main* maggie,
1933 KX_KetsjiEngine* ketsjiEngine,
1934 e_PhysicsEngine physics_engine,
1935 RAS_IRenderTools* rendertools,
1936 RAS_ICanvas* canvas,
1937 KX_BlenderSceneConverter* converter,
1938 bool alwaysUseExpandFraming
1942 Scene *blenderscene = kxscene->GetBlenderScene();
1947 // Get the frame settings of the canvas.
1948 // Get the aspect ratio of the canvas as designed by the user.
1950 RAS_FrameSettings::RAS_FrameType frame_type;
1953 vector<MT_Vector3> inivel,iniang;
1954 set<Group*> grouplist; // list of groups to be converted
1955 set<Object*> allblobj; // all objects converted
1956 set<Object*> groupobj; // objects from groups (never in active layer)
1958 if (alwaysUseExpandFraming) {
1959 frame_type = RAS_FrameSettings::e_frame_extend;
1960 aspect_width = canvas->GetWidth();
1961 aspect_height = canvas->GetHeight();
1963 if (blenderscene->gm.framing.type == SCE_GAMEFRAMING_BARS) {
1964 frame_type = RAS_FrameSettings::e_frame_bars;
1965 } else if (blenderscene->gm.framing.type == SCE_GAMEFRAMING_EXTEND) {
1966 frame_type = RAS_FrameSettings::e_frame_extend;
1968 frame_type = RAS_FrameSettings::e_frame_scale;
1971 aspect_width = blenderscene->gm.xsch;
1972 aspect_height = blenderscene->gm.ysch;
1975 RAS_FrameSettings frame_settings(
1977 blenderscene->gm.framing.col[0],
1978 blenderscene->gm.framing.col[1],
1979 blenderscene->gm.framing.col[2],
1983 kxscene->SetFramingType(frame_settings);
1985 kxscene->SetGravity(MT_Vector3(0,0, -blenderscene->gm.gravity));
1987 /* set activity culling parameters */
1988 kxscene->SetActivityCulling( (blenderscene->gm.mode & WO_ACTIVITY_CULLING) != 0);
1989 kxscene->SetActivityCullingRadius(blenderscene->gm.activityBoxRadius);
1990 kxscene->SetDbvtCulling((blenderscene->gm.mode & WO_DBVT_CULLING) != 0);
1992 // no occlusion culling by default
1993 kxscene->SetDbvtOcclusionRes(0);
1995 int activeLayerBitInfo = blenderscene->lay;
1997 // list of all object converted, active and inactive
1998 CListValue* sumolist = new CListValue();
2000 vector<parentChildLink> vec_parent_child;
2002 CListValue* objectlist = kxscene->GetObjectList();
2003 CListValue* inactivelist = kxscene->GetInactiveList();
2004 CListValue* parentlist = kxscene->GetRootParentList();
2006 SCA_LogicManager* logicmgr = kxscene->GetLogicManager();
2007 SCA_TimeEventManager* timemgr = kxscene->GetTimeEventManager();
2009 CListValue* logicbrick_conversionlist = new CListValue();
2011 //SG_TreeFactory tf;
2013 // Convert actions to actionmap
2015 for (curAct = (bAction*)maggie->action.first; curAct; curAct=(bAction*)curAct->id.next)
2017 logicmgr->RegisterActionName(curAct->id.name + 2, curAct);
2020 SetDefaultFaceType(blenderscene);
2021 // Let's support scene set.
2022 // Beware of name conflict in linked data, it will not crash but will create confusion
2023 // in Python scripting and in certain actuators (replace mesh). Linked scene *should* have
2024 // no conflicting name for Object, Object data and Action.
2025 for (SETLOOPER(blenderscene, sce_iter, base))
2027 Object* blenderobject = base->object;
2028 allblobj.insert(blenderobject);
2030 KX_GameObject* gameobj = gameobject_from_blenderobject(
2036 bool isInActiveLayer = (blenderobject->lay & activeLayerBitInfo) !=0;
2039 if (converter->addInitFromFrame)
2040 if (!isInActiveLayer)
2043 if (gameobj&&addobj)
2047 if (converter->addInitFromFrame) blenderscene->r.cfra=blenderscene->r.sfra;
2051 blenderobject->loc[0]+blenderobject->dloc[0],
2052 blenderobject->loc[1]+blenderobject->dloc[1],
2053 blenderobject->loc[2]+blenderobject->dloc[2]
2055 MT_Vector3 eulxyz(blenderobject->rot);
2056 MT_Vector3 scale(blenderobject->size);
2057 if (converter->addInitFromFrame){//rcruiz
2058 float eulxyzPrev[3];
2059 blenderscene->r.cfra=blenderscene->r.sfra-1;
2060 //XXX update_for_newframe();
2061 MT_Vector3 tmp=pos-MT_Point3(blenderobject->loc[0]+blenderobject->dloc[0],
2062 blenderobject->loc[1]+blenderobject->dloc[1],
2063 blenderobject->loc[2]+blenderobject->dloc[2]
2065 eulxyzPrev[0]=blenderobject->rot[0];
2066 eulxyzPrev[1]=blenderobject->rot[1];
2067 eulxyzPrev[2]=blenderobject->rot[2];
2069 double fps = (double) blenderscene->r.frs_sec/
2070 (double) blenderscene->r.frs_sec_base;
2072 tmp.scale(fps, fps, fps);
2073 inivel.push_back(tmp);
2074 tmp=eulxyz-eulxyzPrev;
2075 tmp.scale(fps, fps, fps);
2076 iniang.push_back(tmp);
2077 blenderscene->r.cfra=blenderscene->r.sfra;
2078 //XXX update_for_newframe();
2081 gameobj->NodeSetLocalPosition(pos);
2082 gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
2083 gameobj->NodeSetLocalScale(scale);
2084 gameobj->NodeUpdateGS(0);
2086 BL_ConvertIpos(blenderobject,gameobj,converter);
2087 BL_ConvertMaterialIpos(blenderobject, gameobj, converter);
2089 sumolist->Add(gameobj->AddRef());
2091 BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene,isInActiveLayer);
2093 gameobj->SetName(blenderobject->id.name + 2);
2095 // update children/parent hierarchy
2096 if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
2098 // blender has an additional 'parentinverse' offset in each object
2099 SG_Callbacks callback(NULL,NULL,NULL,KX_Scene::KX_ScenegraphUpdateFunc,KX_Scene::KX_ScenegraphRescheduleFunc);
2100 SG_Node* parentinversenode = new SG_Node(NULL,kxscene,callback);
2102 // define a normal parent relationship for this node.
2103 KX_NormalParentRelation * parent_relation = KX_NormalParentRelation::New();
2104 parentinversenode->SetParentRelation(parent_relation);
2106 parentChildLink pclink;
2107 pclink.m_blenderchild = blenderobject;
2108 pclink.m_gamechildnode = parentinversenode;
2109 vec_parent_child.push_back(pclink);
2111 float* fl = (float*) blenderobject->parentinv;
2112 MT_Transform parinvtrans(fl);
2113 parentinversenode->SetLocalPosition(parinvtrans.getOrigin());
2114 // problem here: the parent inverse transform combines scaling and rotation
2115 // in the basis but the scenegraph needs separate rotation and scaling.
2116 // This is not important for OpenGL (it uses 4x4 matrix) but it is important
2117 // for the physic engine that needs a separate scaling
2118 //parentinversenode->SetLocalOrientation(parinvtrans.getBasis());
2120 // Extract the rotation and the scaling from the basis
2121 MT_Matrix3x3 ori(parinvtrans.getBasis());
2122 MT_Vector3 x(ori.getColumn(0));
2123 MT_Vector3 y(ori.getColumn(1));
2124 MT_Vector3 z(ori.getColumn(2));
2125 MT_Vector3 parscale(x.length(), y.length(), z.length());
2126 if (!MT_fuzzyZero(parscale[0]))
2128 if (!MT_fuzzyZero(parscale[1]))
2130 if (!MT_fuzzyZero(parscale[2]))
2132 ori.setColumn(0, x);
2133 ori.setColumn(1, y);
2134 ori.setColumn(2, z);
2135 parentinversenode->SetLocalOrientation(ori);
2136 parentinversenode->SetLocalScale(parscale);
2138 parentinversenode->AddChild(gameobj->GetSGNode());
2141 // needed for python scripting
2142 logicmgr->RegisterGameObjectName(gameobj->GetName(),gameobj);
2144 // needed for group duplication
2145 logicmgr->RegisterGameObj(blenderobject, gameobj);
2146 for (int i = 0; i < gameobj->GetMeshCount(); i++)
2147 logicmgr->RegisterGameMeshName(gameobj->GetMesh(i)->GetName(), blenderobject);
2149 converter->RegisterGameObject(gameobj, blenderobject);
2150 // this was put in rapidly, needs to be looked at more closely
2151 // only draw/use objects in active 'blender' layers
2153 logicbrick_conversionlist->Add(gameobj->AddRef());
2155 if (converter->addInitFromFrame){
2156 posPrev=gameobj->NodeGetWorldPosition();
2157 angor=gameobj->NodeGetWorldOrientation();
2159 if (isInActiveLayer)
2161 objectlist->Add(gameobj->AddRef());
2162 //tf.Add(gameobj->GetSGNode());
2164 gameobj->NodeUpdateGS(0);
2165 gameobj->AddMeshUser();
2170 //we must store this object otherwise it will be deleted
2171 //at the end of this function if it is not a root object
2172 inactivelist->Add(gameobj->AddRef());
2174 if (gameobj->IsDupliGroup())
2175 grouplist.insert(blenderobject->dup_group);
2176 if (converter->addInitFromFrame){
2177 gameobj->NodeSetLocalPosition(posPrev);
2178 gameobj->NodeSetLocalOrientation(angor);
2182 /* Note about memory leak issues:
2183 When a CValue derived class is created, m_refcount is initialized to 1
2184 so the class must be released after being used to make sure that it won't
2185 hang in memory. If the object needs to be stored for a long time,
2186 use AddRef() so that this Release() does not free the object.
2187 Make sure that for any AddRef() there is a Release()!!!!
2188 Do the same for any object derived from CValue, CExpression and NG_NetworkMessage
2195 if (!grouplist.empty())
2197 // now convert the group referenced by dupli group object
2198 // keep track of all groups already converted
2199 set<Group*> allgrouplist = grouplist;
2200 set<Group*> tempglist;
2202 while (!grouplist.empty())
2204 set<Group*>::iterator git;
2206 tempglist.swap(grouplist);
2207 for (git=tempglist.begin(); git!=tempglist.end(); git++)
2209 Group* group = *git;
2211 for(go=(GroupObject*)group->gobject.first; go; go=(GroupObject*)go->next)
2213 Object* blenderobject = go->ob;
2214 if (converter->FindGameObject(blenderobject) == NULL)
2216 allblobj.insert(blenderobject);
2217 groupobj.insert(blenderobject);
2218 KX_GameObject* gameobj = gameobject_from_blenderobject(
2224 // this code is copied from above except that
2225 // object from groups are never in active layer
2226 bool isInActiveLayer = false;
2229 if (converter->addInitFromFrame)
2230 if (!isInActiveLayer)
2233 if (gameobj&&addobj)
2237 if (converter->addInitFromFrame)
2238 blenderscene->r.cfra=blenderscene->r.sfra;
2241 blenderobject->loc[0]+blenderobject->dloc[0],
2242 blenderobject->loc[1]+blenderobject->dloc[1],
2243 blenderobject->loc[2]+blenderobject->dloc[2]
2245 MT_Vector3 eulxyz(blenderobject->rot);
2246 MT_Vector3 scale(blenderobject->size);
2247 if (converter->addInitFromFrame){//rcruiz
2248 float eulxyzPrev[3];
2249 blenderscene->r.cfra=blenderscene->r.sfra-1;
2250 //XXX update_for_newframe();
2251 MT_Vector3 tmp=pos-MT_Point3(blenderobject->loc[0]+blenderobject->dloc[0],
2252 blenderobject->loc[1]+blenderobject->dloc[1],
2253 blenderobject->loc[2]+blenderobject->dloc[2]
2255 eulxyzPrev[0]=blenderobject->rot[0];
2256 eulxyzPrev[1]=blenderobject->rot[1];
2257 eulxyzPrev[2]=blenderobject->rot[2];
2259 double fps = (double) blenderscene->r.frs_sec/
2260 (double) blenderscene->r.frs_sec_base;
2262 tmp.scale(fps, fps, fps);
2263 inivel.push_back(tmp);
2264 tmp=eulxyz-eulxyzPrev;
2265 tmp.scale(fps, fps, fps);
2266 iniang.push_back(tmp);
2267 blenderscene->r.cfra=blenderscene->r.sfra;
2268 //XXX update_for_newframe();
2271 gameobj->NodeSetLocalPosition(pos);
2272 gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
2273 gameobj->NodeSetLocalScale(scale);
2274 gameobj->NodeUpdateGS(0);
2276 BL_ConvertIpos(blenderobject,gameobj,converter);
2277 BL_ConvertMaterialIpos(blenderobject,gameobj, converter);
2279 sumolist->Add(gameobj->AddRef());
2281 BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene,isInActiveLayer);
2284 gameobj->SetName(blenderobject->id.name + 2);
2286 // update children/parent hierarchy
2287 if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
2289 // blender has an additional 'parentinverse' offset in each object
2290 SG_Callbacks callback(NULL,NULL,NULL,KX_Scene::KX_ScenegraphUpdateFunc,KX_Scene::KX_ScenegraphRescheduleFunc);
2291 SG_Node* parentinversenode = new SG_Node(NULL,kxscene,callback);
2293 // define a normal parent relationship for this node.
2294 KX_NormalParentRelation * parent_relation = KX_NormalParentRelation::New();
2295 parentinversenode->SetParentRelation(parent_relation);
2297 parentChildLink pclink;
2298 pclink.m_blenderchild = blenderobject;
2299 pclink.m_gamechildnode = parentinversenode;
2300 vec_parent_child.push_back(pclink);
2302 float* fl = (float*) blenderobject->parentinv;
2303 MT_Transform parinvtrans(fl);
2304 parentinversenode->SetLocalPosition(parinvtrans.getOrigin());
2306 // Extract the rotation and the scaling from the basis
2307 MT_Matrix3x3 ori(parinvtrans.getBasis());
2308 MT_Vector3 x(ori.getColumn(0));
2309 MT_Vector3 y(ori.getColumn(1));
2310 MT_Vector3 z(ori.getColumn(2));
2311 MT_Vector3 localscale(x.length(), y.length(), z.length());
2312 if (!MT_fuzzyZero(localscale[0]))
2314 if (!MT_fuzzyZero(localscale[1]))
2316 if (!MT_fuzzyZero(localscale[2]))
2318 ori.setColumn(0, x);
2319 ori.setColumn(1, y);
2320 ori.setColumn(2, z);
2321 parentinversenode->SetLocalOrientation(ori);
2322 parentinversenode->SetLocalScale(localscale);
2324 parentinversenode->AddChild(gameobj->GetSGNode());
2327 // needed for python scripting
2328 logicmgr->RegisterGameObjectName(gameobj->GetName(),gameobj);
2330 // needed for group duplication
2331 logicmgr->RegisterGameObj(blenderobject, gameobj);
2332 for (int i = 0; i < gameobj->GetMeshCount(); i++)
2333 logicmgr->RegisterGameMeshName(gameobj->GetMesh(i)->GetName(), blenderobject);
2335 converter->RegisterGameObject(gameobj, blenderobject);
2336 // this was put in rapidly, needs to be looked at more closely
2337 // only draw/use objects in active 'blender' layers
2339 logicbrick_conversionlist->Add(gameobj->AddRef());
2341 if (converter->addInitFromFrame){
2342 posPrev=gameobj->NodeGetWorldPosition();
2343 angor=gameobj->NodeGetWorldOrientation();
2345 if (isInActiveLayer)
2347 objectlist->Add(gameobj->AddRef());
2348 //tf.Add(gameobj->GetSGNode());
2350 gameobj->NodeUpdateGS(0);
2351 gameobj->AddMeshUser();
2355 //we must store this object otherwise it will be deleted
2356 //at the end of this function if it is not a root object
2357 inactivelist->Add(gameobj->AddRef());
2360 if (gameobj->IsDupliGroup())
2362 // check that the group is not already converted
2363 if (allgrouplist.insert(blenderobject->dup_group).second)
2364 grouplist.insert(blenderobject->dup_group);
2366 if (converter->addInitFromFrame){
2367 gameobj->NodeSetLocalPosition(posPrev);
2368 gameobj->NodeSetLocalOrientation(angor);
2380 // non-camera objects not supported as camera currently
2381 if (blenderscene->camera && blenderscene->camera->type == OB_CAMERA) {
2382 KX_Camera *gamecamera= (KX_Camera*) converter->FindGameObject(blenderscene->camera);
2385 kxscene->SetActiveCamera(gamecamera);
2389 set<Object*>::iterator oit;
2390 for(oit=allblobj.begin(); oit!=allblobj.end(); oit++)
2392 Object* blenderobj = *oit;
2393 if (blenderobj->type==OB_MESH) {
2394 Mesh *me = (Mesh*)blenderobj->data;
2397 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)converter->FindGameObject(blenderobj);
2399 if (obj && BL_ModifierDeformer::HasArmatureDeformer(blenderobj) && blenderobj->parent && blenderobj->parent->type==OB_ARMATURE){
2400 KX_GameObject *par = converter->FindGameObject(blenderobj->parent);
2401 if (par && obj->GetDeformer())
2402 ((BL_SkinDeformer*)obj->GetDeformer())->SetArmature((BL_ArmatureObject*) par);
2408 // create hierarchy information
2410 vector<parentChildLink>::iterator pcit;
2412 for (pcit = vec_parent_child.begin();!(pcit==vec_parent_child.end());++pcit)
2415 struct Object* blenderchild = pcit->m_blenderchild;
2416 struct Object* blenderparent = blenderchild->parent;
2417 KX_GameObject* parentobj = converter->FindGameObject(blenderparent);
2418 KX_GameObject* childobj = converter->FindGameObject(blenderchild);
2422 if (!parentobj || objectlist->SearchValue(childobj) != objectlist->SearchValue(parentobj))
2424 // special case: the parent and child object are not in the same layer.
2425 // This weird situation is used in Apricot for test purposes.
2426 // Resolve it by not converting the child
2427 childobj->GetSGNode()->DisconnectFromParent();
2428 delete pcit->m_gamechildnode;
2429 // Now destroy the child object but also all its descendent that may already be linked
2430 // Remove the child reference in the local list!
2431 // Note: there may be descendents already if the children of the child were processed
2432 // by this loop before the child. In that case, we must remove the children also
2433 CListValue* childrenlist = childobj->GetChildrenRecursive();
2434 childrenlist->Add(childobj->AddRef());
2435 for ( i=0;i<childrenlist->GetCount();i++)
2437 KX_GameObject* obj = static_cast<KX_GameObject*>(childrenlist->GetValue(i));
2438 if (sumolist->RemoveValue(obj))
2440 if (logicbrick_conversionlist->RemoveValue(obj))
2443 childrenlist->Release();
2445 // now destroy recursively
2446 converter->UnregisterGameObject(childobj); // removing objects during conversion make sure this runs too
2447 kxscene->RemoveObject(childobj);
2452 switch (blenderchild->partype)
2456 // creat a new vertex parent relationship for this node.
2457 KX_VertexParentRelation * vertex_parent_relation = KX_VertexParentRelation::New();
2458 pcit->m_gamechildnode->SetParentRelation(vertex_parent_relation);
2463 // creat a new slow parent relationship for this node.
2464 KX_SlowParentRelation * slow_parent_relation = KX_SlowParentRelation::New(blenderchild->sf);
2465 pcit->m_gamechildnode->SetParentRelation(slow_parent_relation);
2470 // parent this to a bone
2471 Bone *parent_bone = get_named_bone( (bArmature *)(blenderchild->parent)->data, blenderchild->parsubstr);
2474 KX_BoneParentRelation *bone_parent_relation = KX_BoneParentRelation::New(parent_bone);
2475 pcit->m_gamechildnode->SetParentRelation(bone_parent_relation);
2480 case PARSKEL: // skinned - ignore
2491 parentobj-> GetSGNode()->AddChild(pcit->m_gamechildnode);
2493 vec_parent_child.clear();
2495 // find 'root' parents (object that has not parents in SceneGraph)
2496 for (i=0;i<sumolist->GetCount();++i)
2498 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2499 if (gameobj->GetSGNode()->GetSGParent() == 0)
2501 parentlist->Add(gameobj->AddRef());
2502 gameobj->NodeUpdateGS(0);
2506 // create graphic controller for culling
2507 if (kxscene->GetDbvtCulling())
2509 bool occlusion = false;
2510 for (i=0; i<sumolist->GetCount();i++)
2512 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2513 if (gameobj->GetMeshCount() > 0)
2516 gameobj->GetSGNode()->BBox().getmm(box, MT_Transform::Identity());
2517 // box[0] is the min, box[1] is the max
2518 bool isactive = objectlist->SearchValue(gameobj);
2519 BL_CreateGraphicObjectNew(gameobj,box[0],box[1],kxscene,isactive,physics_engine);
2520 if (gameobj->GetOccluder())
2525 kxscene->SetDbvtOcclusionRes(blenderscene->gm.occlusionRes);
2527 if (blenderscene->world)
2528 kxscene->GetPhysicsEnvironment()->setNumTimeSubSteps(blenderscene->gm.physubstep);
2530 // now that the scenegraph is complete, let's instantiate the deformers.
2531 // We need that to create reusable derived mesh and physic shapes
2532 for (i=0;i<sumolist->GetCount();++i)
2534 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2535 if (gameobj->GetDeformer())
2536 gameobj->GetDeformer()->UpdateBuckets();
2539 // Set up armature constraints
2540 for (i=0;i<sumolist->GetCount();++i)
2542 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2543 if (gameobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
2544 ((BL_ArmatureObject*)gameobj)->LoadConstraints(converter);
2547 bool processCompoundChildren = false;
2549 // create physics information
2550 for (i=0;i<sumolist->GetCount();i++)
2552 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2553 struct Object* blenderobject = gameobj->GetBlenderObject();
2554 int nummeshes = gameobj->GetMeshCount();
2555 RAS_MeshObject* meshobj = 0;
2558 meshobj = gameobj->GetMesh(0);
2560 int layerMask = (groupobj.find(blenderobject) == groupobj.end()) ? activeLayerBitInfo : 0;
2561 BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,layerMask,physics_engine,converter,processCompoundChildren);
2564 processCompoundChildren = true;
2565 // create physics information
2566 for (i=0;i<sumolist->GetCount();i++)
2568 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2569 struct Object* blenderobject = gameobj->GetBlenderObject();
2570 int nummeshes = gameobj->GetMeshCount();
2571 RAS_MeshObject* meshobj = 0;
2574 meshobj = gameobj->GetMesh(0);
2576 int layerMask = (groupobj.find(blenderobject) == groupobj.end()) ? activeLayerBitInfo : 0;
2577 BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,layerMask,physics_engine,converter,processCompoundChildren);
2580 //set ini linearVel and int angularVel //rcruiz
2581 if (converter->addInitFromFrame)
2582 for (i=0;i<sumolist->GetCount();i++)
2584 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2585 if (gameobj->IsDynamic()){
2586 gameobj->setLinearVelocity(inivel[i],false);
2587 gameobj->setAngularVelocity(iniang[i],false);
2593 // create physics joints
2594 for (i=0;i<sumolist->GetCount();i++)
2596 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2597 struct Object* blenderobject = gameobj->GetBlenderObject();
2599 bConstraint *curcon;
2600 conlist = get_active_constraints2(blenderobject);
2603 for (curcon = (bConstraint *)conlist->first; curcon; curcon=(bConstraint *)curcon->next) {
2604 if (curcon->type==CONSTRAINT_TYPE_RIGIDBODYJOINT){
2606 bRigidBodyJointConstraint *dat=(bRigidBodyJointConstraint *)curcon->data;
2610 PHY_IPhysicsController* physctr2 = 0;
2614 KX_GameObject *gotar=getGameOb(dat->tar->id.name+2,sumolist);
2615 if (gotar && gotar->GetPhysicsController())
2616 physctr2 = (PHY_IPhysicsController*) gotar->GetPhysicsController()->GetUserData();
2619 if (gameobj->GetPhysicsController())
2621 PHY_IPhysicsController* physctrl = (PHY_IPhysicsController*) gameobj->GetPhysicsController()->GetUserData();
2622 //we need to pass a full constraint frame, not just axis
2624 //localConstraintFrameBasis
2625 MT_Matrix3x3 localCFrame(MT_Vector3(dat->axX,dat->axY,dat->axZ));
2626 MT_Vector3 axis0 = localCFrame.getColumn(0);
2627 MT_Vector3 axis1 = localCFrame.getColumn(1);