#include "SCENE.h" #include "Wininet.h" #ifdef SMALLSCENE_MAX //#pragma optimize("",off) Mesh *WORLD::SmallSceneGetMesh(char *NameSmSC,char *ObjectName) { SmallScene *SmSc; _3D_Object *pObj; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return NULL; UnicID SmScID; SmScID = SmSc->ObjectNameDico.IsExist(ObjectName); if (SmScID == -1) return NULL; SmScID = SmSc->ObjectNameDico.mpst_Dico[SmScID].AssociatedValue; pObj = SmSc->ObjectsPtrs[SmScID]; if (pObj->MeshIndex > SmSc->ulNumberOfMeshes) return NULL; return SmSc->MeshesPtrs[pObj->MeshIndex]; } int WORLD::SmallSceneNew(char *NameSmSC) { START_RASTER(Rendering); UnicID SmScID; SmScID = SmallSceneDico.IsExist(NameSmSC); if (SmScID != -1) return 0; SmallScene *Ret; Ret = (SmallScene*)CC_malloc(sizeof(SmallScene)); memset(Ret,0,sizeof(SmallScene)); Ret->CameraPosition_Save = Ret->CameraPosition; Ret->ObjectNameDico.Init(); Ret->MaterialNameDico.Init(); Ret->MultiMaterialNameDico.Init(); Ret->RootMatrix.Identity(); SmallSceneDico.GetUnic(&SmScID,NameSmSC); pSmallScenes[SmScCreateID] = Ret; SmallSceneDico.mpst_Dico[SmScID].AssociatedValue = SmScCreateID; SmScCreateID = 0; while (pSmallScenes[SmScCreateID] && (SmScCreateID < MAX_SmallSCenes)) {SmScCreateID++;}; if (SmScCreateID == MAX_SmallSCenes) *((u32 *)0) = *((u32 *)4); //<- MUST CRASH !! Ret->CameraPosition.Identity(); Ret->CameraPosition_Save.Identity(); Ret->RootMatrix.Identity(); STOP_RASTER(Rendering); return 1; } void WORLD::SmallSceneNewObject(char *NameSmSC,char *ObjectName, char *ObjectFather, MATRIX *objPos) { START_RASTER(Rendering); SmallScene *SmSc; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return; UnicID SmScID; SmScID = SmSc->ObjectNameDico.IsExist(ObjectName); if (SmScID != -1) return; if (strlen(ObjectName)>128) return; SmSc->ulNumberOfObjects++; SmSc->ObjectsPtrs = (_3D_Object**)CC_realloc(SmSc->ObjectsPtrs,4*SmSc->ulNumberOfObjects); if (!SmSc->ObjectsBuffer) SmSc->ObjectsBuffer = (_3D_Object*)CC_malloc(sizeof(_3D_Object)*SmSc->ulNumberOfObjects); else SmSc->ObjectsBuffer = (_3D_Object*)CC_realloc(SmSc->ObjectsBuffer,sizeof(_3D_Object)*SmSc->ulNumberOfObjects); SmSc->ObjectsIDs = (u32*)CC_realloc(SmSc->ObjectsIDs,4*SmSc->ulNumberOfObjects); SmSc->ObjectsIDs[SmSc->ulNumberOfObjects - 1] = -1; for (int i=0; iulNumberOfObjects; i++) SmSc->ObjectsPtrs[i] = SmSc->ObjectsBuffer + i; // SmSc->ObjectsPtrs[SmSc->ulNumberOfObjects - 1] = (_3D_Object*)CC_malloc(sizeof(_3D_Object)); memset(SmSc->ObjectsPtrs[SmSc->ulNumberOfObjects - 1],0,sizeof(_3D_Object)); (SmSc->ObjectsPtrs[SmSc->ulNumberOfObjects - 1])->pObjectName = (u8 *)CC_malloc(strlen(ObjectName) + 1); strcpy((char*)(SmSc->ObjectsPtrs[SmSc->ulNumberOfObjects - 1])->pObjectName, ObjectName); SmSc->ObjectsPtrs[SmSc->ulNumberOfObjects - 1]->MeshIndex = -1; SmSc->ObjectNameDico.GetUnic(&SmScID,ObjectName); SmSc->ObjectNameDico.mpst_Dico[SmScID].AssociatedValue = SmSc->ulNumberOfObjects - 1; char ObjectNameOBJ[2048]; sprintf(ObjectNameOBJ,"%s.OBJ",ObjectName); SmSc->ObjectNameDico.GetUnic(&SmScID,ObjectNameOBJ); SmSc->ObjectNameDico.mpst_Dico[SmScID].AssociatedValue = SmSc->ulNumberOfObjects - 1; _3D_Object *pObj; pObj = SmSc->ObjectsPtrs[SmSc->ulNumberOfObjects - 1]; if (ObjectFather && *ObjectFather) { char ObjectFatherOBJ[2048]; UnicID ObjectFatherREF; sprintf(ObjectFatherOBJ,"%s.OBJ",ObjectFather); ObjectFatherREF = SmSc->ObjectNameDico.IsExist(ObjectFatherOBJ); if (ObjectFatherREF != -1) { ObjectFatherREF = SmSc->ObjectNameDico.mpst_Dico[ObjectFatherREF].AssociatedValue; pObj->FatherIndex = ObjectFatherREF; } } else pObj->FatherIndex = -1; if (objPos) { pObj->LocalMatrix = *objPos; pObj->OriginalLocalMatrix = *objPos; ComputeSmallSceneHierarchie(SmSc); pObj->OriginalGlobalMatrix = pObj->GlobalMatrix; } else { pObj->LocalMatrix.Identity(); pObj->GlobalMatrix.Identity(); pObj->OriginalLocalMatrix.Identity(); pObj->OriginalGlobalMatrix.Identity(); } pObj->DrawOrder = 0; pObj->MeshIndex = pObj->MaterialIndex = pObj->MultiMaterialIndex = -1; ComputeLastSon(SmSc); STOP_RASTER(Rendering); return; } int WORLD::SmallSceneNewMesh(char *NameSmSC,char *ObjectName) { START_RASTER(Rendering); SmallScene *SmSc; _3D_Object *pObj; UnicID SmScID; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return -1; SmScID = SmSc->ObjectNameDico.IsExist(ObjectName); if (SmScID == -1) return -1; SmScID = SmSc->ObjectNameDico.mpst_Dico[SmScID].AssociatedValue; pObj = SmSc->ObjectsPtrs[SmScID]; if (pObj->MeshIndex != -1) return -1; SmSc->ulNumberOfMeshes++; SmSc->MeshesPtrs = (Mesh**)CC_realloc(SmSc->MeshesPtrs,4*SmSc->ulNumberOfMeshes); SmSc->MeshesIDs = (u32*)CC_realloc(SmSc->MeshesIDs,4*SmSc->ulNumberOfMeshes); SmSc->MeshesIDs[SmSc->ulNumberOfMeshes - 1] = -1; SmSc->MeshesPtrs[SmSc->ulNumberOfMeshes - 1] = new Mesh(); pObj->MeshIndex = SmSc->ulNumberOfMeshes - 1; SmSc->MeshesPtrs[SmSc->ulNumberOfMeshes - 1]->UVAreUnic = 1; STOP_RASTER(Rendering); return SmScID; } int WORLD::SmallSceneNewMaterial(char *NameSmSC,char *MaterialName,u32 MediaIndex ) { START_RASTER(Rendering); SmallScene *SmSc; char MaterialNameMAT[2048]; UnicID SmScID; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return -1; SmScID = SmSc->MaterialNameDico.IsExist(MaterialName); if (SmScID != -1) return -1; u32 matIndex = SmSc->ulNumberOfMaterials++; strcpy(MaterialNameMAT, MaterialName); if (GetFileNameExtend(MaterialNameMAT) != '.MAT') strcat(MaterialNameMAT, ".MAT"); SmSc->MaterialNameDico.GetUnic(&SmScID,MaterialNameMAT); SmSc->MaterialNameDico.mpst_Dico[SmScID].AssociatedValue = matIndex; SmSc->MaterialsPtrs = (_3D_Material**)CC_realloc(SmSc->MaterialsPtrs,4*SmSc->ulNumberOfMaterials); SmSc->MaterialsIDs = (u32*)CC_realloc(SmSc->MaterialsIDs,4*SmSc->ulNumberOfMaterials); SmSc->MaterialsRedirs = (u32*)CC_realloc(SmSc->MaterialsRedirs,4*SmSc->ulNumberOfMaterials); SmSc->MaterialsIDs[matIndex] = -1; SmSc->MaterialsPtrs[matIndex] = (_3D_Material*)CC_malloc(sizeof(_3D_Material)); SmSc->MaterialsRedirs[matIndex] = matIndex; memset(SmSc->MaterialsPtrs[matIndex],0,sizeof(_3D_Material)); SmSc->MaterialsPtrs[matIndex]->NumberOfLayers = 1; SmSc->MaterialsPtrs[matIndex]->Layers[0].TextureID = -1; if ((MediaIndex != -1) && (ID_MEDIA[MediaIndex])) { ID_MEDIA[MediaIndex]->nbReferences++; MediaIndex = MediaDico.IsExist((char*)ID_MEDIA[MediaIndex]->MediaName); } SmSc->MaterialsPtrs[matIndex]->Layers[0].TextureMediaIndex = MediaIndex; GLOB_SetDefaultRS(&SmSc->MaterialsPtrs[matIndex]->Layers[0].RenderState); SmSc->MaterialsPtrs[matIndex]->Layers[0].ColorMultiplier = -1; sprintf((char *)SmSc->MaterialsPtrs[matIndex]->Layers[0].LayerName, "Layer %d" , 0); STOP_RASTER(Rendering); return SmScID; } int WORLD::SmallSceneLinkObjectMaterial(char *NameSmSC,char *ObjectName,char *MaterialName) { START_RASTER(Rendering); char MaterialNameMAT[2048]; SmallScene *SmSc; _3D_Object *pObj; UnicID SmScID; strcpy(MaterialNameMAT, MaterialName); if (GetFileNameExtend(MaterialNameMAT) != '.MAT') strcat(MaterialNameMAT, ".MAT"); SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return -1; SmScID = SmSc->ObjectNameDico.IsExist(ObjectName); if (SmScID == -1) return -1; SmScID = SmSc->ObjectNameDico.mpst_Dico[SmScID].AssociatedValue; pObj = SmSc->ObjectsPtrs[SmScID]; SmScID = SmSc->MaterialNameDico.IsExist(MaterialNameMAT); if (SmScID == -1) return -1; SmScID = SmSc->MaterialNameDico.mpst_Dico[SmScID].AssociatedValue; pObj->MaterialIndex = SmScID; STOP_RASTER(Rendering); return 0; } int WORLD::SmallSceneNewMultiMaterial(lua_State *L) { // PARAM_STRING(NameSmSC) // PARAM_STRING(MultiMatName) // PARAM_LIST_OF_MATS() char *NameSmSC = (char *)lua_tostring(L, 1); char *MultiMatName = (char *)lua_tostring(L, 2); char *ObjectName = NULL; int tabIndex = 3; if (lua_isstring(L, 3)) { ObjectName = (char *)lua_tostring(L, 3); tabIndex = 4; } if (!NameSmSC || !MultiMatName || !lua_istable(L, tabIndex)) return -1; SmallScene *SmSc; UnicID SmScID; char MaterialNameMMT[2048]; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return -1; SmScID = SmSc->MultiMaterialNameDico.IsExist(MultiMatName); if (SmScID != -1) return -1; u32 mmtIndex = SmSc->ulNumberOfMultiMats++; SmSc->MultiMaterialNameDico.GetUnic(&SmScID,MultiMatName); SmSc->MultiMaterialNameDico.mpst_Dico[SmScID].AssociatedValue = mmtIndex; sprintf(MaterialNameMMT,"%s.MMT",MultiMatName); SmSc->MultiMaterialNameDico.GetUnic(&SmScID,MaterialNameMMT); SmSc->MultiMaterialNameDico.mpst_Dico[SmScID].AssociatedValue = mmtIndex; SmSc->MultiMaterialsPtrs = (_3D_MultiMaterial**)CC_realloc(SmSc->MultiMaterialsPtrs,4*SmSc->ulNumberOfMultiMats); SmSc->MultiMaterialsIDs = (u32*)CC_realloc(SmSc->MultiMaterialsIDs,4*SmSc->ulNumberOfMultiMats); SmSc->MultiMaterialsIDs[mmtIndex] = -1; SmSc->MultiMaterialsPtrs[mmtIndex] = (_3D_MultiMaterial*)CC_malloc(sizeof(_3D_MultiMaterial)); memset(SmSc->MultiMaterialsPtrs[mmtIndex],0,sizeof(_3D_MultiMaterial)); SmSc->MultiMaterialsPtrs[mmtIndex]->ulNumberOfMaterials = lua_objlen(L, tabIndex); SmSc->MultiMaterialsPtrs[mmtIndex]->pMaterialsIDS = (UnicID *)CC_malloc(SmSc->MultiMaterialsPtrs[mmtIndex]->ulNumberOfMaterials*sizeof(UnicID)); SmSc->MultiMaterialsPtrs[mmtIndex]->pMaterialsIndexes = (u32 *)CC_malloc(SmSc->MultiMaterialsPtrs[mmtIndex]->ulNumberOfMaterials*sizeof(u32)); for (int matIdx=0; matIdxMultiMaterialsPtrs[mmtIndex]->ulNumberOfMaterials; matIdx++) { char *matName; UnicID matID; char MaterialNameMAT[2048]; lua_pushnumber(L, matIdx + 1); lua_gettable(L, -2); matName = (char *)lua_tostring(L, -1); lua_pop(L, 1); if (!matName) return -1; strcpy(MaterialNameMAT, matName); if (GetFileNameExtend(MaterialNameMAT) != '.MAT') strcat(MaterialNameMAT, ".MAT"); matID = SmSc->MaterialNameDico.IsExist(MaterialNameMAT); if (matID == -1) return -1; matID = SmSc->MaterialNameDico.mpst_Dico[matID].AssociatedValue; SmSc->MultiMaterialsPtrs[mmtIndex]->pMaterialsIDS[matIdx] = matID; SmSc->MultiMaterialsPtrs[mmtIndex]->pMaterialsIndexes[matIdx] = matID; } if (ObjectName) SmallSceneLinkObjectMultiMat(NameSmSC, ObjectName, MultiMatName); return mmtIndex; } int WORLD::SmallSceneLinkObjectMultiMat(char *NameSmSC,char *ObjectName,char *MultiMatName) { START_RASTER(Rendering); SmallScene *SmSc; _3D_Object *pObj; UnicID SmScID; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return -1; SmScID = SmSc->ObjectNameDico.IsExist(ObjectName); if (SmScID == -1) return -1; SmScID = SmSc->ObjectNameDico.mpst_Dico[SmScID].AssociatedValue; pObj = SmSc->ObjectsPtrs[SmScID]; SmScID = SmSc->MultiMaterialNameDico.IsExist(MultiMatName); if (SmScID == -1) return -1; SmScID = SmSc->MultiMaterialNameDico.mpst_Dico[SmScID].AssociatedValue; pObj->MultiMaterialIndex = SmScID; pObj->MaterialIndex = -1; STOP_RASTER(Rendering); return 0; } int WORLD::SmallSceneNewLight(char *NameSmSC, char *LightName, char *ObjectName) { START_RASTER(Rendering); SmallScene *SmSc; _3D_Object *pObj; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return -1; UnicID SmScID; int SmScIndex; SmScID = SmSc->ObjectNameDico.IsExist(ObjectName); if (SmScID == -1) return -1; SmScIndex = SmSc->ObjectNameDico.mpst_Dico[SmScID].AssociatedValue; if (SmScIndex == -1) return -1; //u32 ulNumberOfLights; //UnicID *LightsIDs; //_3D_Light **LightsPtrs; if (SmSc->ulNumberOfLights == 0) { SmSc->LightsIDs = (UnicID*)CC_malloc(sizeof(UnicID*)); SmSc->LightsPtrs = (_3D_Light**)CC_malloc(sizeof(_3D_Light*)); } else { SmSc->LightsIDs = (UnicID*)CC_realloc(SmSc->LightsIDs,sizeof(UnicID*)*(SmSc->ulNumberOfLights+1)); SmSc->LightsPtrs = (_3D_Light**)CC_realloc(SmSc->LightsPtrs,sizeof(_3D_Light*)*(SmSc->ulNumberOfLights+1)); } SmSc->LightsIDs[SmSc->ulNumberOfLights] = -1; SmSc->LightsPtrs[SmSc->ulNumberOfLights] = (_3D_Light*)CC_malloc(sizeof(_3D_Light)); memset(SmSc->LightsPtrs[SmSc->ulNumberOfLights],0,sizeof(_3D_Light)); SmSc->LightsPtrs[SmSc->ulNumberOfLights]->ObjectIndex = SmScIndex; SmSc->LightsPtrs[SmSc->ulNumberOfLights]->ObjectID = SmScID; SmSc->LightsPtrs[SmSc->ulNumberOfLights]->LightColor.x = 1.0f; SmSc->LightsPtrs[SmSc->ulNumberOfLights]->LightColor.y = 1.0f; SmSc->LightsPtrs[SmSc->ulNumberOfLights]->LightColor.z = 1.0f; SmSc->LightsPtrs[SmSc->ulNumberOfLights]->IsOn = 1; SmSc->LightsPtrs[SmSc->ulNumberOfLights]->Near = 1.0f; SmSc->LightsPtrs[SmSc->ulNumberOfLights]->Far = 10.0f; STOP_RASTER(Rendering); return SmSc->ulNumberOfLights++; } int WORLD::SmallSceneIKReset(char *NameSmSC) { SmallScene *SmSc; _3D_Object *pObj; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return -1; if (SmSc->IK) { if (SmSc->IK->ulNumberOfPatchs && SmSc->IK->pObjPatchs) CC_free(SmSc->IK->pObjPatchs); if (SmSc->IK->ulNumberOfCollider && SmSc->IK->pColliders) CC_free(SmSc->IK->pColliders); if (SmSc->IK->ulNumberOfInstance && SmSc->IK->pPI) CC_free(SmSc->IK->pPI); } else { SmSc->IK = (SmallScene_IK *)CC_malloc(sizeof(SmallScene_IK)); } memset(SmSc->IK, 0, sizeof(SmallScene_IK)); SmSc->IODico.Init(); return 0; } void WORLD::SmallSceneIKInstancesReset(char *NameSmSC) { SmallScene *SmSc; _3D_Object *pObj; SmSc = SmallNameToScene(NameSmSC); if (SmSc == NULL) return; SmallSceneIKInstancesReset(SmSc); } void WORLD::SmallSceneIKInstancesReset(SmallScene *SmSc) { if (!SmSc->IK || !SmSc->IK->pPI) return; for (int K = 0 ; K < SmSc->IK->ulNumberOfInstance ; K++) { if (SmSc->IK->pPI[K]) CC_free(SmSc->IK->pPI[K]); SmSc->IK->pPI[K] = NULL; } if (SmSc->IK->pPI) CC_free(SmSc->IK->pPI); SmSc->IK->pPI = NULL; SmSc->IK->ulNumberOfInstance = 0; } void SetStrFieldFloat(lua_State *L, const char *index, float value) ; void SetStrFieldString(lua_State *L, const char *index, char * value) ; void SetStrFieldFunction(lua_State *L, const char *index, lua_CFunction function) ; void SetStrFieldBoolean(lua_State *L, const char *index, bool function) ; void SetStrFieldInteger(lua_State *L, const char *index, int value); void lua_pushinteger (lua_State *L, lua_Integer n) ; void SetIntFieldInt(lua_State *L, const int index, float value) { lua_pushnumber(L, index); lua_pushnumber(L, (double)value); //lua_settable(L, -3); lua_rawset(L, -3); } #pragma optimize("",off) int PushMeshLua(WORLD *pScene,lua_State *L, char *NameSmSC,char *ObjectName) { Mesh *ToPush = pScene->SmallSceneGetMesh(NameSmSC,ObjectName); if (ToPush) { lua_newtable(L); SetStrFieldString(L, "ObjectName", ObjectName) ; lua_pushstring(L,"Faces"); lua_createtable(L,0,ToPush->NumberOfFaces); for(unsigned i=0; iNumberOfFaces; i++) { lua_pushinteger(L,i); lua_createtable(L,0,6); SetIntFieldInt(L,0,ToPush->Indexes[i].I[0]); SetIntFieldInt(L,1,ToPush->Indexes[i].I[1]); SetIntFieldInt(L,2,ToPush->Indexes[i].I[2]); SetIntFieldInt(L,3,ToPush->Indexes_UV[i].I[0]); SetIntFieldInt(L,4,ToPush->Indexes_UV[i].I[1]); SetIntFieldInt(L,5,ToPush->Indexes_UV[i].I[2]); lua_settable(L,-3); } lua_settable(L,-3); lua_pushstring(L,"Targets"); lua_createtable(L,0,ToPush->ulNumberOfTarget); for(unsigned i=0; iulNumberOfTarget; i++) { lua_pushinteger(L,i); lua_createtable(L,0,ToPush->MorphTargets[i].MorphNumber); for(unsigned j=0; jMorphTargets[i].MorphNumber; j++) { lua_pushinteger(L,j); lua_createtable(L,0,4); SetStrFieldFloat(L, "PIndex", ToPush->MorphTargets[i].MorphList[j].VIndex); SetStrFieldFloat(L, "x", ToPush->MorphTargets[i].MorphList[j].Move.x); SetStrFieldFloat(L, "y", ToPush->MorphTargets[i].MorphList[j].Move.y); SetStrFieldFloat(L, "z", ToPush->MorphTargets[i].MorphList[j].Move.z); lua_settable(L,-3); } lua_settable(L,-3); } lua_settable(L,-3); lua_pushstring(L,"Points"); lua_createtable(L,0,ToPush->NumberOfPoints); for(unsigned i=0; iNumberOfPoints; i++) { lua_pushinteger(L,i); lua_createtable(L,0,3); SetStrFieldFloat(L, "x", ToPush->Points[i].x); SetStrFieldFloat(L, "y", ToPush->Points[i].y); SetStrFieldFloat(L, "z", ToPush->Points[i].z); lua_settable(L,-3); //lua_pushstring(L,"\n"); } lua_settable(L,-3); lua_pushstring(L,"Normals"); lua_createtable(L,0,ToPush->NumberOfPointsN); for(unsigned i=0; iNumberOfPointsN; i++) { lua_pushinteger(L,i); lua_createtable(L,0,3); SetStrFieldFloat(L, "x", ToPush->PointsN[i].x); SetStrFieldFloat(L, "y", ToPush->PointsN[i].y); SetStrFieldFloat(L, "z", ToPush->PointsN[i].z); lua_settable(L,-3); //lua_pushstring(L,"\n"); } lua_settable(L,-3); lua_pushstring(L,"UV1"); lua_createtable(L,0,ToPush->NumberOfPointsUV); for(unsigned i=0; iNumberOfPointsUV; i++) { lua_pushinteger(L,i); lua_createtable(L,0,3); SetStrFieldFloat(L, "u", ToPush->PointsUV[i].x); SetStrFieldFloat(L, "v", ToPush->PointsUV[i].y); SetStrFieldFloat(L, "w", ToPush->PointsUV[i].z); lua_settable(L,-3); //lua_pushstring(L,"\n"); } lua_settable(L,-3); lua_pushstring(L,"UV2"); lua_createtable(L,0,ToPush->NumberOfPointsUV2); for(unsigned i=0; iNumberOfPointsUV2; i++) { lua_pushinteger(L,i); lua_createtable(L,0,3); SetStrFieldFloat(L, "u", ToPush->PointsUV2[i].x); SetStrFieldFloat(L, "v", ToPush->PointsUV2[i].y); SetStrFieldFloat(L, "w", ToPush->PointsUV2[i].z); lua_settable(L,-3); //lua_pushstring(L,"\n"); } lua_settable(L,-3);//*/ return 1; } return 0; } #endif