Load.cpp 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498
  1. /*
  2. pomocne funkce
  3. */
  4. #include <string.h>
  5. #include <limits.h>
  6. #include "compat_mini.h"
  7. #include "Berusky_universal.h"
  8. #include "3d_all.h"
  9. void json_export_kont_single(EDIT_KONTEJNER *p_kont, EDIT_MATERIAL ** p_mat, int max_mat);
  10. int hlasit_kolize = 0;
  11. template < class T > void vyrob_pole(T ** p_dst, int num)
  12. {
  13. if (!(*p_dst = (T *) mmalloc(sizeof(*p_dst[0]) * num)))
  14. chyba("Pamet");
  15. memset((void *) *p_dst, 0, sizeof(*p_dst[0]) * num);
  16. }
  17. template < class T > void vyrob_pole_abs(T ** p_dst, int num)
  18. {
  19. if (!(*p_dst = (T *) mmalloc(sizeof(byte) * num)))
  20. chyba("Pamet");
  21. memset((void *) *p_dst, 0, sizeof(byte) * num);
  22. }
  23. /*
  24. Koncovky
  25. */
  26. /* Zameni koncovku, koncovka je zadana vcetne tecky
  27. */
  28. char *zamen_koncovku(char *p_file, const char *p_koncovka)
  29. {
  30. char *p_pom = p_file, *p_last = NULL;
  31. while ((p_pom = strchr(p_pom, '.'))) {
  32. p_last = p_pom++;
  33. }
  34. if (p_last)
  35. *p_last = 0;
  36. return ((p_koncovka) ? strcat(p_file, p_koncovka) : p_file);
  37. }
  38. /* Zameni koncovku, oddelovaci znak se zadava
  39. */
  40. char *zamen_koncovku_znak(char *p_file, char znak, const char *p_koncovka)
  41. {
  42. char *p_pom = strchr(p_file, znak);
  43. if (p_pom)
  44. *p_pom = 0;
  45. return ((p_koncovka) ? strcat(p_file, p_koncovka) : p_file);
  46. }
  47. /* Vrati koncovku souboru
  48. */
  49. char *cti_koncovku(char *p_file)
  50. {
  51. if (p_file) {
  52. return (strchr(p_file, '.'));
  53. }
  54. else {
  55. return (NULL);
  56. }
  57. }
  58. /* Vrati znak
  59. */
  60. char *cti_za_znak(char *p_string, char znak)
  61. {
  62. char *p_pom = p_string;
  63. while (*p_pom && *p_pom == znak)
  64. p_pom++;
  65. return (p_pom);
  66. }
  67. /* Mlzna kostka
  68. */
  69. MLZNA_KOSTKA *vyrob_mlhokostku(char *p_jmeno)
  70. {
  71. MLZNA_KOSTKA *p_mlha = (MLZNA_KOSTKA *) mmalloc(sizeof(p_mlha[0]));
  72. strcpy(p_mlha->jmeno, p_jmeno);
  73. p_mlha->poly = K_CHYBA;
  74. p_mlha->a = 1.0f;
  75. p_mlha->mod = GL_LINEAR;
  76. p_mlha->max.x = 10.0f;
  77. p_mlha->max.y = 10.0f;
  78. p_mlha->max.z = 10.0f;
  79. return (p_mlha);
  80. }
  81. MLZNA_KOSTKA *kopiruj_mlhokostku(MLZNA_KOSTKA * p_src, char *p_jmeno)
  82. {
  83. MLZNA_KOSTKA *p_mlha = (MLZNA_KOSTKA *) mmalloc(sizeof(p_mlha[0]));
  84. *p_mlha = *p_src;
  85. strcpy(p_mlha->jmeno, p_jmeno);
  86. p_mlha->p_kont = NULL;
  87. p_mlha->p_next = p_src->p_next;
  88. p_src->p_next = p_mlha;
  89. return (p_mlha);
  90. }
  91. void zrus_mlhokostku(MLZNA_KOSTKA ** p_first, MLZNA_KOSTKA * p_maz)
  92. {
  93. MLZNA_KOSTKA *p_tmp, *p_prev;
  94. if (!p_first) {
  95. p_maz->p_kont = NULL;
  96. }
  97. else {
  98. if ((*p_first) == p_maz) {
  99. (*p_first) = p_maz->p_next;
  100. }
  101. else {
  102. p_tmp = (*p_first)->p_next;
  103. p_prev = (*p_first);
  104. while (p_tmp) {
  105. if (p_tmp == p_maz) {
  106. p_prev->p_next = p_maz->p_next;
  107. break;
  108. }
  109. else {
  110. p_prev = p_tmp;
  111. p_tmp = p_tmp->p_next;
  112. }
  113. }
  114. }
  115. free(p_maz);
  116. }
  117. }
  118. MLZNA_KOSTKA *zrus_mlhokostku_all(MLZNA_KOSTKA * p_first)
  119. {
  120. MLZNA_KOSTKA *p_tmp;
  121. while (p_first) {
  122. p_tmp = p_first->p_next;
  123. free(p_first);
  124. p_first = p_tmp;
  125. }
  126. return (NULL);
  127. }
  128. /* **************************************************
  129. Objekty - vyrobeni / zruseni
  130. */
  131. EDIT_OBJEKT *vyrob_objekt(void)
  132. {
  133. EDIT_OBJEKT *p_obj;
  134. p_obj = (EDIT_OBJEKT *) mmalloc(sizeof(EDIT_OBJEKT));
  135. p_obj->kreslit = 1;
  136. p_obj->face_typ = GL_TRIANGLES;
  137. p_obj->line_typ = GL_LINES;
  138. init_matrix(&p_obj->m);
  139. // init_matrix(&p_obj->local_matrix);
  140. return (p_obj);
  141. }
  142. // f = facenum * 3 !!!
  143. EDIT_OBJEKT *vyrob_objekt_komplet(int v, int f)
  144. {
  145. EDIT_OBJEKT *p_obj;
  146. p_obj = (EDIT_OBJEKT *) mmalloc(sizeof(EDIT_OBJEKT));
  147. p_obj->p_vertex = (MUJ_BOD *) mmalloc(sizeof(MUJ_BOD) * v);
  148. if (f) {
  149. if ((p_obj->p_face = (FACE *) mmalloc(sizeof(FACE) * f)) == NULL) {
  150. chyba("pamet");
  151. }
  152. }
  153. p_obj->vertexnum = v;
  154. p_obj->facenum = f;
  155. p_obj->kreslit = 1;
  156. p_obj->face_typ = GL_TRIANGLES;
  157. p_obj->line_typ = GL_LINES;
  158. init_matrix(&p_obj->m);
  159. return (p_obj);
  160. }
  161. void zrus_kosti_key(JOINT_KEYS ** p_key)
  162. {
  163. null_free((void **) &(*p_key)->p_pkeys);
  164. null_free((void **) &(*p_key)->p_pos);
  165. null_free((void **) &(*p_key)->p_rkeys);
  166. null_free((void **) &(*p_key)->p_rot);
  167. null_free((void **) &(*p_key)->p_skeys);
  168. null_free((void **) &(*p_key)->p_scale);
  169. null_free((void **) p_key);
  170. *p_key = NULL;
  171. }
  172. void zrus_kosti_prs_rec(JOINT * p_joint)
  173. {
  174. if (p_joint) {
  175. if (p_joint->p_child)
  176. zrus_kosti_prs_rec(p_joint->p_child);
  177. if (p_joint->p_next)
  178. zrus_kosti_prs_rec(p_joint->p_next);
  179. if (p_joint->p_keys)
  180. zrus_kosti_key(&(p_joint->p_keys));
  181. p_joint->pos_keys = 0;
  182. p_joint->p.x = p_joint->p.y = p_joint->p.z = 0.0f;
  183. null_free((void **) &p_joint->p_pos);
  184. p_joint->rot_keys = 0;
  185. p_joint->r.x = p_joint->r.y = p_joint->r.z = 0.0f;
  186. p_joint->r.w = 1.0f;
  187. null_free((void **) &p_joint->p_rot);
  188. p_joint->scs_keys = 0;
  189. p_joint->s.x = p_joint->s.y = p_joint->s.z = 1.0f;
  190. null_free((void **) &p_joint->p_scs);
  191. }
  192. }
  193. void zrus_kosti_rec(JOINT ** p_joint)
  194. {
  195. if (*p_joint) {
  196. if ((*p_joint)->p_child)
  197. zrus_kosti_rec(&((*p_joint)->p_child));
  198. if ((*p_joint)->p_next)
  199. zrus_kosti_rec(&((*p_joint)->p_next));
  200. if ((*p_joint)->p_keys)
  201. zrus_kosti_key(&((*p_joint)->p_keys));
  202. null_free((void **) &(*p_joint)->p_vertexlist);
  203. null_free((void **) &(*p_joint)->p_pos);
  204. null_free((void **) &(*p_joint)->p_rot);
  205. null_free((void **) &(*p_joint)->p_scs);
  206. null_free((void **) &(*p_joint));
  207. p_joint = NULL;
  208. }
  209. }
  210. void zrus_kosti(EDIT_OBJEKT * p_obj)
  211. {
  212. JOINT_ANIMACE *p_tmp = p_obj->p_joit_animace, *p_next;
  213. while (p_tmp) {
  214. p_next = p_tmp->p_next;
  215. if (p_tmp->p_child)
  216. zrus_kosti_rec(&p_tmp->p_child);
  217. free(p_tmp);
  218. p_tmp = p_next;
  219. }
  220. }
  221. void zrus_objekt(EDIT_OBJEKT ** p_obj)
  222. {
  223. zrus_kosti(*p_obj);
  224. if ((*p_obj)->p_tmp_sim) {
  225. key_sim_zrus_vnitrek((*p_obj)->p_tmp_sim);
  226. null_free((void **) &(*p_obj)->p_tmp_sim);
  227. }
  228. null_free((void **) &(*p_obj)->p_face);
  229. null_free((void **) &(*p_obj)->p_opt);
  230. null_free((void **) &(*p_obj)->p_vertex);
  231. null_free((void **) &(*p_obj)->p_line);
  232. null_free((void **) p_obj);
  233. }
  234. int smaz_divoke_vertexy_objektu_dir(EDIT_OBJEKT * p_obj)
  235. {
  236. OBJ_VERTEX *p_vert = p_obj->p_vertex;
  237. byte *p_div;
  238. word *p_face = p_obj->p_face;
  239. int i, j, vn = p_obj->vertexnum, od, fn = p_obj->facenum;
  240. if (!vn || !fn)
  241. return (TRUE);
  242. if ((p_div = (byte *) mmalloc(sizeof(byte) * vn)) == NULL) {
  243. chyba("pamet");
  244. }
  245. memset(p_div, 1, sizeof(byte) * vn);
  246. for (i = 0; i < fn; i += 3) {
  247. p_div[p_face[i]] = p_div[p_face[i + 1]] = p_div[p_face[i + 2]] = 0;
  248. }
  249. for (i = 0, od = 0; i < vn + od; i++) {
  250. if (p_div[i]) {
  251. smaz_vertex_bez_facu(p_vert, vn, (i - od));
  252. for (j = 0; j < fn; j += 3) {
  253. if (p_face[j] > i - od)
  254. p_face[j]--;
  255. if (p_face[j + 1] > i - od)
  256. p_face[j + 1]--;
  257. if (p_face[j + 2] > i - od)
  258. p_face[j + 2]--;
  259. }
  260. od++;
  261. }
  262. }
  263. free(p_div);
  264. p_obj->vertexnum -= od;
  265. return (od);
  266. }
  267. EDIT_OBJEKT *kopiruj_objekt(EDIT_OBJEKT * p_src)
  268. {
  269. EDIT_OBJEKT *p_cil;
  270. if (!p_src)
  271. return (p_src);
  272. p_cil = (EDIT_OBJEKT *) kopiruj_pole(p_src, sizeof(p_src[0]));
  273. p_cil->p_vertex =
  274. (OBJ_VERTEX *) kopiruj_pole(p_src->p_vertex,
  275. sizeof(OBJ_VERTEX) * p_cil->vertexnum);
  276. p_cil->p_vertex_kosti =
  277. (BOD *) kopiruj_pole(p_src->p_vertex_kosti,
  278. sizeof(p_src->p_vertex_kosti[0]) * p_cil->vertexnum);
  279. p_cil->p_face =
  280. (FACE *) kopiruj_pole(p_src->p_face, sizeof(FACE) * p_cil->facenum);
  281. p_cil->p_opt =
  282. (int *) kopiruj_pole(p_src->p_opt,
  283. sizeof(p_src->p_opt[0]) * p_src->optnum);
  284. p_cil->p_line = NULL;
  285. p_cil->p_joit_animace = key_kosti_kopiruj_animace(p_src->p_joit_animace);
  286. return (p_cil);
  287. }
  288. /* kopiruje
  289. */
  290. void kopiruj_objekt_geometrie(EDIT_OBJEKT * p_src, EDIT_OBJEKT * p_cil)
  291. {
  292. if (!p_src)
  293. return;
  294. free(p_cil->p_vertex);
  295. p_cil->vertexnum = p_src->vertexnum;
  296. p_cil->p_vertex =
  297. (OBJ_VERTEX *) mmalloc(sizeof(OBJ_VERTEX) * p_cil->vertexnum);
  298. // mtest(p_cil->p_vertex);
  299. memcpy(p_cil->p_vertex, p_src->p_vertex,
  300. sizeof(OBJ_VERTEX) * p_cil->vertexnum);
  301. free(p_cil->p_face);
  302. p_cil->facenum = p_src->facenum;
  303. p_cil->p_face = (FACE *) mmalloc(sizeof(FACE) * p_cil->facenum);
  304. // mtest(p_cil->p_face);
  305. memcpy(p_cil->p_face, p_src->p_face, sizeof(FACE) * p_cil->facenum);
  306. }
  307. EDIT_OBJEKT *kopiruj_objekt_indir_vyber(EDIT_OBJEKT * p_src,
  308. EDIT_OBJEKT * p_cil, int flag)
  309. {
  310. int i;
  311. if (!p_src || !p_cil)
  312. return (NULL);
  313. for (i = 0; i < p_cil->vertexnum; i++) {
  314. if (flag & COPY_GEOMETRY) {
  315. p_cil->p_vertex[i].x = p_src->p_vertex[i].x;
  316. p_cil->p_vertex[i].y = p_src->p_vertex[i].y;
  317. p_cil->p_vertex[i].z = p_src->p_vertex[i].z;
  318. }
  319. if (flag & COPY_DIFFUSE) {
  320. p_cil->p_vertex[i].dr = p_src->p_vertex[i].dr;
  321. p_cil->p_vertex[i].dg = p_src->p_vertex[i].dg;
  322. p_cil->p_vertex[i].db = p_src->p_vertex[i].db;
  323. p_cil->p_vertex[i].da = p_src->p_vertex[i].da;
  324. p_cil->p_vertex[i].mdr = p_src->p_vertex[i].mdr;
  325. p_cil->p_vertex[i].mdg = p_src->p_vertex[i].mdg;
  326. p_cil->p_vertex[i].mdb = p_src->p_vertex[i].mdb;
  327. p_cil->p_vertex[i].mda = p_src->p_vertex[i].mda;
  328. }
  329. if (flag & COPY_SPECULAR) {
  330. p_cil->p_vertex[i].sr = p_src->p_vertex[i].sr;
  331. p_cil->p_vertex[i].sg = p_src->p_vertex[i].sg;
  332. p_cil->p_vertex[i].sb = p_src->p_vertex[i].sb;
  333. p_cil->p_vertex[i].msr = p_src->p_vertex[i].msr;
  334. p_cil->p_vertex[i].msg = p_src->p_vertex[i].msg;
  335. p_cil->p_vertex[i].msb = p_src->p_vertex[i].msb;
  336. }
  337. if (flag & COPY_MAP1) {
  338. p_cil->p_vertex[i].tu1 = p_src->p_vertex[i].tu1;
  339. p_cil->p_vertex[i].tv1 = p_src->p_vertex[i].tv1;
  340. }
  341. if (flag & COPY_MAP2) {
  342. p_cil->p_vertex[i].tu2 = p_src->p_vertex[i].tu2;
  343. p_cil->p_vertex[i].tv2 = p_src->p_vertex[i].tv2;
  344. }
  345. }
  346. if (flag & COPY_MATERIAL) {
  347. p_cil->material = p_src->material;
  348. }
  349. return (p_cil);
  350. }
  351. EDIT_OBJEKT *slep_objekty(EDIT_OBJEKT * p_obj1, EDIT_OBJEKT * p_obj2)
  352. {
  353. EDIT_OBJEKT *p_obj = vyrob_objekt();
  354. int i;
  355. int f1, f2;
  356. if (strlen(p_obj1->jmeno) + strlen(p_obj2->jmeno) + 1 >= MAX_JMENO) {
  357. strcpy(p_obj->jmeno, p_obj1->jmeno);
  358. }
  359. else {
  360. if (snprintf(p_obj->jmeno, sizeof(p_obj->jmeno),
  361. "%s_%s", p_obj1->jmeno, p_obj2->jmeno) >=
  362. (int) sizeof(p_obj->jmeno)) {
  363. zrus_objekt(&p_obj);
  364. return (NULL);
  365. }
  366. }
  367. assert(strlen(p_obj->jmeno) < MAX_JMENO);
  368. p_obj->vertexnum = p_obj1->vertexnum + p_obj2->vertexnum;
  369. if ((p_obj->p_vertex =
  370. (OBJ_VERTEX *) malloc(sizeof(OBJ_VERTEX) * p_obj->vertexnum)) == NULL) {
  371. zrus_objekt(&p_obj);
  372. return (NULL);
  373. }
  374. memcpy(p_obj->p_vertex, p_obj1->p_vertex,
  375. sizeof(OBJ_VERTEX) * p_obj1->vertexnum);
  376. memcpy(p_obj->p_vertex + p_obj1->vertexnum, p_obj2->p_vertex,
  377. sizeof(OBJ_VERTEX) * p_obj2->vertexnum);
  378. p_obj->facenum = p_obj1->facenum + p_obj2->facenum;
  379. if ((p_obj->p_face =
  380. (FACE *) mmalloc(sizeof(FACE) * p_obj->facenum)) == NULL) {
  381. zrus_objekt(&p_obj);
  382. return (NULL);
  383. }
  384. memcpy(p_obj->p_face, p_obj1->p_face, sizeof(FACE) * p_obj1->facenum);
  385. f1 = p_obj1->facenum;
  386. f2 = (p_obj2->facenum + f1);
  387. for (i = f1; i < f2; i++)
  388. p_obj->p_face[i] = p_obj1->vertexnum + p_obj2->p_face[i - f1];
  389. p_obj->material = p_obj1->material;
  390. p_obj->m1flag = p_obj1->m1flag;
  391. p_obj->m2flag = p_obj1->m2flag;
  392. p_obj->oflag = p_obj1->oflag;
  393. return (p_obj);
  394. }
  395. /* **************************************************
  396. Kontejner
  397. */
  398. EDIT_KONTEJNER *vyrob_kontejner(void)
  399. {
  400. EDIT_KONTEJNER *p_kont;
  401. if ((p_kont = (EDIT_KONTEJNER *) mmalloc(sizeof(EDIT_KONTEJNER))) == NULL)
  402. chyba("pamet!");
  403. memset((void *) p_kont, 0, sizeof(p_kont[0]));
  404. p_kont->max_objektu = MAX_KONT_OBJEKTU;
  405. p_kont->kreslit = 1;
  406. p_kont->prvek = K_CHYBA;
  407. p_kont->kflag |= KONT_STATIC;
  408. p_kont->kontejner_ID = K_CHYBA; // defaultni id kontejneru
  409. init_matrix(&p_kont->world); // globalni transformacni matice
  410. init_matrix(&p_kont->mpmatrix); // lokalni transformacni matice (mp matrix)
  411. return (p_kont);
  412. }
  413. void zrus_kontejner(EDIT_KONTEJNER ** pp_kont, MLZNA_KOSTKA ** p_first)
  414. {
  415. int i;
  416. EDIT_KONTEJNER *p_kont;
  417. p_kont = *pp_kont;
  418. for (i = 0; i < p_kont->max_objektu; i++) {
  419. if (p_kont->p_obj[i]) {
  420. zrus_objekt(&p_kont->p_obj[i]);
  421. }
  422. }
  423. if (p_kont->p_mlha)
  424. zrus_mlhokostku(p_first, p_kont->p_mlha);
  425. for (i = 0; i < KONT_MAX_ANIM; i++) {
  426. key_sim_root_zrus(p_kont->sim + i);
  427. }
  428. free(p_kont);
  429. *pp_kont = NULL;
  430. }
  431. void zrus_kontejner_rec(EDIT_KONTEJNER ** pp_kont, MLZNA_KOSTKA ** p_first)
  432. {
  433. int i;
  434. EDIT_KONTEJNER *p_kont, *p_next;
  435. p_kont = *pp_kont;
  436. while (p_kont) {
  437. for (i = 0; i < p_kont->max_objektu; i++) {
  438. if (p_kont->p_obj[i]) {
  439. zrus_objekt(&p_kont->p_obj[i]);
  440. }
  441. }
  442. if (p_kont->p_mlha)
  443. zrus_mlhokostku(p_first, p_kont->p_mlha);
  444. for (i = 0; i < KONT_MAX_ANIM; i++) {
  445. key_sim_root_zrus(p_kont->sim + i);
  446. }
  447. p_next = p_kont->p_next;
  448. free(p_kont);
  449. p_kont = p_next;
  450. }
  451. *pp_kont = NULL;
  452. }
  453. void vyprazdni_kontejner(EDIT_KONTEJNER * p_kont)
  454. {
  455. int i;
  456. if (!p_kont)
  457. return;
  458. for (i = 0; i < p_kont->max_objektu; i++) {
  459. if (p_kont->p_obj[i])
  460. zrus_objekt(&p_kont->p_obj[i]);
  461. }
  462. updatuj_kontejner_statistika(p_kont, TRUE);
  463. }
  464. void updatuj_kontejner_flag(EDIT_KONTEJNER * p_kont, EDIT_MATERIAL ** p_mat)
  465. {
  466. int o, m;
  467. while (p_kont) {
  468. p_kont->m1flag = p_kont->m2flag = 0;
  469. for (o = 0; o < p_kont->max_objektu; o++) {
  470. if (p_kont->p_obj[o]) {
  471. m = p_kont->p_obj[o]->material;
  472. if (p_mat[m]) {
  473. p_kont->p_obj[o]->m1flag |= p_mat[m]->flag;
  474. p_kont->p_obj[o]->m2flag |= p_mat[m]->flag2;
  475. }
  476. p_kont->m1flag |= p_kont->p_obj[o]->m1flag;
  477. p_kont->m2flag |= p_kont->p_obj[o]->m2flag;
  478. p_kont->kflag |= p_kont->p_obj[o]->oflag;
  479. }
  480. }
  481. p_kont = p_kont->p_next;
  482. }
  483. }
  484. void updatuj_obalku_kontejner(EDIT_KONTEJNER * p_kont)
  485. {
  486. int i;
  487. if (!p_kont)
  488. return;
  489. for (i = 0; i < p_kont->max_objektu; i++) {
  490. if (p_kont->p_obj[i]) {
  491. obb_calc_obj_fast(p_kont->p_obj[i]);
  492. }
  493. }
  494. kontejner_obalka(p_kont);
  495. }
  496. void updatuj_kontejner_statistika(EDIT_KONTEJNER * p_kont, int obalky)
  497. {
  498. int i, j, f, v;
  499. while (p_kont) {
  500. for (i = 0, j = 0, f = 0, v = 0; i < p_kont->max_objektu; i++) {
  501. if (p_kont->p_obj[i]) {
  502. f += p_kont->p_obj[i]->facenum;
  503. v += p_kont->p_obj[i]->vertexnum;
  504. p_kont->m1flag |= p_kont->p_obj[i]->m1flag;
  505. p_kont->m2flag |= p_kont->p_obj[i]->m2flag;
  506. p_kont->kflag |= p_kont->p_obj[i]->oflag;
  507. j++;
  508. }
  509. }
  510. p_kont->objektu = j;
  511. p_kont->facu = f;
  512. p_kont->bodu = v;
  513. vycisti_kontejner(p_kont);
  514. if (obalky)
  515. updatuj_obalku_kontejner(p_kont);
  516. p_kont = p_kont->p_next;
  517. }
  518. }
  519. void smaz_divoke_vertexy_kontejneru(EDIT_KONTEJNER * p_kont)
  520. {
  521. int i;
  522. if (p_kont) {
  523. for (i = 0; i < p_kont->max_objektu; i++) {
  524. if (p_kont->p_obj[i] && !p_kont->p_obj[i]->p_joit_animace)
  525. smaz_divoke_vertexy_objektu_dir(p_kont->p_obj[i]);
  526. }
  527. }
  528. }
  529. void vycisti_kontejner(EDIT_KONTEJNER * p_kont)
  530. {
  531. int i;
  532. if (!p_kont)
  533. return;
  534. smaz_divoke_vertexy_kontejneru(p_kont);
  535. for (i = 0; i < p_kont->max_objektu; i++) {
  536. if (p_kont->p_obj[i] && (!p_kont->p_obj[i]->vertexnum
  537. || !p_kont->p_obj[i]->facenum))
  538. zrus_objekt(&p_kont->p_obj[i]);
  539. }
  540. }
  541. // posune objekty v kontejneru k sobe a pojmenuje je jednotne
  542. void preindexuj_kontejner(EDIT_KONTEJNER * p_kont)
  543. {
  544. int i, j;
  545. for (i = 0, j = 0; i < p_kont->max_objektu; i++) {
  546. if (!p_kont->p_obj[i])
  547. continue;
  548. if (j != i) {
  549. p_kont->p_obj[j] = p_kont->p_obj[i];
  550. p_kont->p_obj[i] = NULL;
  551. }
  552. j++;
  553. }
  554. }
  555. int slep_kontejner(EDIT_KONTEJNER * p_kont)
  556. {
  557. EDIT_OBJEKT *p_obj;
  558. int i, j, ob = -1;
  559. // 1000 je MAX_EDIT_MATERIALU
  560. for (i = 0; i < 1000 /*MAX_EDIT_MATERIALU */ ; i++) {
  561. for (j = 0; j < p_kont->max_objektu; j++) {
  562. if (p_kont->p_obj[j] && p_kont->p_obj[j]->material == i) {
  563. ob = j;
  564. break;
  565. }
  566. }
  567. if (j == p_kont->max_objektu)
  568. continue;
  569. for (j = ob + 1; j < p_kont->max_objektu; j++) {
  570. if (p_kont->p_obj[j] && p_kont->p_obj[j]->material == i) {
  571. p_obj = slep_objekty(p_kont->p_obj[ob], p_kont->p_obj[j]);
  572. zrus_objekt(&p_kont->p_obj[ob]);
  573. zrus_objekt(&p_kont->p_obj[j]);
  574. p_kont->p_obj[ob] = p_obj;
  575. }
  576. }
  577. }
  578. updatuj_kontejner_statistika(p_kont, TRUE);
  579. preindexuj_kontejner(p_kont);
  580. return (TRUE);
  581. }
  582. /*
  583. Slepi 2 objekty, pokud jsou od sebe vzdaleny mene nez min_dist
  584. */
  585. #define SLEP_FACE_LIMIT 500
  586. int slep_kontejner_dist(EDIT_KONTEJNER * p_kont, float min_dist)
  587. {
  588. EDIT_OBJEKT *p_obj1, *p_obj2;
  589. int i1, i2;
  590. kont_world_matrix(p_kont);
  591. // vraci pocet unikatnich materialu
  592. lo_setrid_kontejner(p_kont);
  593. for (i1 = 0, i2 = 1; i2 < p_kont->max_objektu;) {
  594. p_obj1 = p_kont->p_obj[i1];
  595. p_obj2 = p_kont->p_obj[i2];
  596. if (!p_obj2 || p_obj2->p_joit_animace) {
  597. i2++;
  598. continue;
  599. }
  600. if (!p_obj1 || p_obj1->p_joit_animace
  601. || p_obj1->material != p_obj2->material) {
  602. i1++;
  603. i2++;
  604. continue;
  605. }
  606. if (p_obj1->facenum + p_obj2->facenum > SLEP_FACE_LIMIT) {
  607. i1++;
  608. i2++;
  609. continue;
  610. }
  611. if (vzdal_bodu_bod(&p_obj1->obb.obb_stred,
  612. &p_obj2->obb.obb_stred) < min_dist) {
  613. p_kont->p_obj[i1] = slep_objekty(p_obj1, p_obj2);
  614. p_kont->p_obj[i2] = NULL;
  615. obb_calc_obj_fast(p_kont->p_obj[i1]);
  616. zrus_objekt(&p_obj1);
  617. zrus_objekt(&p_obj2);
  618. i2++;
  619. }
  620. else {
  621. i1++;
  622. i2++;
  623. }
  624. }
  625. preindexuj_kontejner(p_kont);
  626. updatuj_kontejner_statistika(p_kont, TRUE);
  627. return (TRUE);
  628. }
  629. /* Kopie kontejneru */
  630. EDIT_KONTEJNER *kopiruj_kontejner(EDIT_KONTEJNER * p_src,
  631. EDIT_MATERIAL ** p_smat, EDIT_MATERIAL ** p_dmat, int dmatnum,
  632. STATIC_LIGHT * p_slight, int slightnum, DYN_LIGHT * p_dlight, int dlightnum,
  633. EXTRA_DYN_LIGHT * p_elight, int elightnum, LENS_FLARE * p_flare,
  634. int maxflares)
  635. {
  636. EDIT_KONTEJNER *p_dest = NULL;
  637. EDIT_KONTEJNER *p_dest_top = NULL;
  638. int i, mat;
  639. while (p_src) {
  640. if (!p_dest) {
  641. p_dest_top = p_dest = vyrob_kontejner();
  642. }
  643. else {
  644. p_dest = (p_dest->p_next = vyrob_kontejner());
  645. }
  646. *p_dest = *p_src;
  647. if (p_src->p_mlha) {
  648. p_dest->p_mlha = kopiruj_mlhokostku(p_src->p_mlha, p_dest->jmeno);
  649. p_dest->p_mlha->p_kont = p_dest;
  650. }
  651. // Prekopiruju svetla
  652. lo_kopiruj_svetla_do_sceny(p_dest, p_slight, slightnum, p_dlight,
  653. dlightnum, p_elight, elightnum, p_flare, maxflares);
  654. for (i = 0; i < MAX_KONT_OBJEKTU; i++) {
  655. if (p_src->p_obj[i]) {
  656. p_dest->p_obj[i] = kopiruj_objekt(p_src->p_obj[i]);
  657. if (p_smat) {
  658. mat =
  659. lo_najdi_material(p_dmat, dmatnum,
  660. p_smat[p_src->p_obj[i]->material]->jmeno);
  661. if (mat == K_CHYBA) {
  662. mat = lo_najdi_prazdny_material(p_dmat, dmatnum);
  663. if (mat == K_CHYBA)
  664. continue;
  665. p_dmat[mat] = kopiruj_material(p_smat[p_src->p_obj[i]->material]);
  666. }
  667. p_dest->p_obj[i]->material = mat;
  668. }
  669. }
  670. }
  671. // kopie stromu animacniho stromu ...
  672. if (p_src->kflag & KONT_KEYFRAME) {
  673. for (i = 0; i < KONT_MAX_ANIM; i++) {
  674. if (p_src->sim[i].keynum)
  675. key_sim_root_to_sim_root_indir(p_dest, p_dest->sim + i,
  676. p_src->sim + i);
  677. }
  678. }
  679. p_src = p_src->p_next;
  680. }
  681. return (p_dest_top);
  682. }
  683. EDIT_KONTEJNER *kopiruj_kontejner_indir_vyber(EDIT_KONTEJNER * p_src,
  684. EDIT_KONTEJNER * p_dest, int flag)
  685. {
  686. int i;
  687. for (i = 0; i < MAX_KONT_OBJEKTU; i++) {
  688. if (p_src->p_obj[i] && p_dest->p_obj[i]) {
  689. kopiruj_objekt_indir_vyber(p_src->p_obj[i], p_dest->p_obj[i], flag);
  690. }
  691. }
  692. return (p_dest);
  693. }
  694. int lo_setrid_kontejner_compare(const void *p_o1, const void *p_o2)
  695. {
  696. EDIT_OBJEKT **p_obj1 = (EDIT_OBJEKT **) p_o1;
  697. EDIT_OBJEKT **p_obj2 = (EDIT_OBJEKT **) p_o2;
  698. int o1 = INT_MAX, o2 = INT_MAX;
  699. float d1, d2;
  700. if (*p_obj1) {
  701. o1 = ((EDIT_OBJEKT *) * p_obj1)->material;
  702. }
  703. if (*p_obj2) {
  704. o2 = ((EDIT_OBJEKT *) * p_obj2)->material;
  705. }
  706. if (!(*p_obj1) || !(*p_obj2)) {
  707. if (o1 < o2)
  708. return (-1);
  709. if (o1 == o2)
  710. return (0);
  711. else
  712. return (1);
  713. }
  714. if (o1 < o2) // o1 je mensi nez o2
  715. return (-1);
  716. if (o1 == o2) {
  717. d1 = vektor_velikost(&(((EDIT_OBJEKT *) * p_obj1)->obb.obb_stred));
  718. d2 = vektor_velikost(&(((EDIT_OBJEKT *) * p_obj2)->obb.obb_stred));
  719. if (d1 < d2)
  720. return (-1);
  721. if (d1 > d2)
  722. return (1);
  723. else
  724. return (0);
  725. }
  726. else
  727. return (1); // o2 je vetsi nez o1
  728. }
  729. /* setridi staticky kontejner podle pouzitych materialu
  730. da stejne materily k sobe
  731. */
  732. int lo_setrid_kontejner(EDIT_KONTEJNER * p_kont)
  733. {
  734. int mat = 0, i, am = INT_MAX;
  735. if (p_kont->kflag & KONT_KEYFRAME)
  736. return (0);
  737. kont_world_matrix(p_kont);
  738. for (i = 0; i < p_kont->max_objektu; i++) {
  739. if (p_kont->p_obj[i])
  740. obb_calc_obj_fast(p_kont->p_obj[i]);
  741. }
  742. qsort((void *) p_kont->p_obj, (size_t) p_kont->max_objektu,
  743. (size_t) sizeof(EDIT_OBJEKT *), lo_setrid_kontejner_compare);
  744. for (i = 0; i < MAX_KONT_OBJEKTU; i++) {
  745. if (p_kont->p_obj[i] && p_kont->p_obj[i]->material != am) {
  746. am = p_kont->p_obj[i]->material;
  747. mat++;
  748. }
  749. }
  750. return (mat);
  751. }
  752. /* **************************************************************************
  753. Setrideni kontejneru podle materialu
  754. */
  755. int lo_setrid_kontejner_materialy_compare(const void *p_o1, const void *p_o2)
  756. {
  757. EDIT_OBJEKT **p_obj1 = (EDIT_OBJEKT **) p_o1;
  758. EDIT_OBJEKT **p_obj2 = (EDIT_OBJEKT **) p_o2;
  759. int o1 = INT_MAX, o2 = INT_MAX;
  760. if (*p_obj1) {
  761. o1 = ((EDIT_OBJEKT *) * p_obj1)->material;
  762. }
  763. if (*p_obj2) {
  764. o2 = ((EDIT_OBJEKT *) * p_obj2)->material;
  765. }
  766. if (!(*p_obj1) || !(*p_obj2)) {
  767. if (o1 < o2)
  768. return (-1);
  769. if (o1 == o2)
  770. return (0);
  771. else
  772. return (1);
  773. }
  774. return (o1 - o2);
  775. }
  776. /* setridi staticky kontejner podle pouzitych materialu
  777. da stejne materily k sobe
  778. */
  779. void lo_setrid_kontejner_materialy(EDIT_KONTEJNER * p_kont)
  780. {
  781. if (!(p_kont->kflag & KONT_KEYFRAME)) {
  782. qsort((void *) p_kont->p_obj, (size_t) p_kont->max_objektu,
  783. (size_t) sizeof(EDIT_OBJEKT *), lo_setrid_kontejner_materialy_compare);
  784. }
  785. }
  786. /* **************************************************
  787. Animace
  788. */
  789. void zrus_tri_vnitrek(HIERARCHY_TRACK_INFO * p_tri)
  790. {
  791. null_free((void **) &p_tri->p_pkeys);
  792. null_free((void **) &p_tri->p_pos);
  793. null_free((void **) &p_tri->p_rkeys);
  794. null_free((void **) &p_tri->p_at);
  795. null_free((void **) &p_tri->p_scale);
  796. null_free((void **) &p_tri->p_skeys);
  797. }
  798. void zrus_tri(HIERARCHY_TRACK_INFO ** p_trii)
  799. {
  800. zrus_tri_vnitrek(*p_trii);
  801. free(*p_trii);
  802. *p_trii = NULL;
  803. }
  804. void key_root_zrus_rec(HIERARCHY_TRACK_INFO * p_tri)
  805. {
  806. int i;
  807. for (i = 0; i < p_tri->childnum; i++) {
  808. assert(p_tri->p_child[i]);
  809. key_root_zrus_rec(p_tri->p_child[i]);
  810. }
  811. zrus_tri(&p_tri);
  812. }
  813. // zrusi animacni strom
  814. void key_root_zrus(HIERARCHY_ROOT * p_root)
  815. {
  816. int i;
  817. if (p_root->framenum) {
  818. for (i = 0; i < p_root->childnum; i++) {
  819. assert(p_root->p_child[i]);
  820. key_root_zrus_rec(p_root->p_child[i]);
  821. }
  822. memset(p_root, 0, sizeof(*p_root));
  823. }
  824. }
  825. /* *******************************************************
  826. Materialy
  827. */
  828. EDIT_MATERIAL *vyrob_material(void)
  829. {
  830. EDIT_MATERIAL *p_mat;
  831. int i;
  832. if ((p_mat = (EDIT_MATERIAL *) mmalloc(sizeof(EDIT_MATERIAL))) == NULL) {
  833. chyba("pamet");
  834. }
  835. memset((void *) p_mat, 0, sizeof(EDIT_MATERIAL));
  836. p_mat->dxmat.diffuse_r = 1.0f;
  837. p_mat->dxmat.diffuse_g = 1.0f;
  838. p_mat->dxmat.diffuse_b = 1.0f;
  839. p_mat->dxmat.diffuse_a = 1.0f;
  840. p_mat->dxmat.specular_r = 0.0f;
  841. p_mat->dxmat.specular_g = 0.0f;
  842. p_mat->dxmat.specular_b = 0.0f;
  843. p_mat->dxmat.faktor_r = 1.0f;
  844. p_mat->dxmat.faktor_g = 1.0f;
  845. p_mat->dxmat.faktor_b = 1.0f;
  846. p_mat->dxmat.faktor_a = 1.0f;
  847. p_mat->lesk = 1.0f;
  848. p_mat->flag2 |= (MAT2_DIFFUSE | MAT2_DIFFUSE_RAY | MAT2_MAP1);
  849. mat_default_stage_bloky(p_mat);
  850. for (i = 0; i < MAT_TEXTUR; i++) {
  851. init_matrix(p_mat->ttext + i);
  852. }
  853. p_mat->env_scale = 1.0f;
  854. return (p_mat);
  855. }
  856. void zrus_material(EDIT_MATERIAL ** p_mat)
  857. {
  858. int i, j;
  859. if ((*p_mat)->flag & MAT_ANIM_FRAME)
  860. lo_zrus_animaci(&(*p_mat)->anim);
  861. for (j = 0; j < MAT_TEXTUR; j++) {
  862. for (i = 0; i < MAX_TEXT_ANIM; i++) {
  863. if ((*p_mat)->p_atext[j][i])
  864. key_zrus_material_animace((*p_mat)->p_atext[j] + i);
  865. }
  866. }
  867. if ((*p_mat)->p_sub)
  868. zrus_material(&((*p_mat)->p_sub));
  869. null_free((void **) p_mat);
  870. *p_mat = NULL;
  871. }
  872. void lo_zrus_material_kont(EDIT_KONTEJNER * p_kont, EDIT_MATERIAL ** p_list,
  873. int num)
  874. {
  875. EDIT_OBJEKT *p_obj;
  876. int o;
  877. oe_olist_reset(&o);
  878. while ((p_obj = oe_olist_next(p_kont, &o))) {
  879. if (p_obj->material < num && p_list[p_obj->material])
  880. zrus_material(p_list + p_obj->material);
  881. }
  882. }
  883. EDIT_MATERIAL *kopiruj_material_indir(EDIT_MATERIAL * p_src,
  884. EDIT_MATERIAL * p_dest)
  885. {
  886. int i, j;
  887. if (!p_src || !p_dest)
  888. return (NULL);
  889. memcpy(p_dest, p_src, sizeof(EDIT_MATERIAL));
  890. // kopie animaci
  891. if (p_src->flag & MAT_ANIM_FRAME) {
  892. p_dest->anim.p_frame =
  893. (ANIM_FRAME *) mmalloc(sizeof(ANIM_FRAME) * p_dest->anim.framenum);
  894. memcpy(p_dest->anim.p_frame, p_src->anim.p_frame,
  895. sizeof(ANIM_FRAME) * p_dest->anim.framenum);
  896. }
  897. // kopie animaci 1
  898. for (j = 0; j < MAT_TEXTUR; j++) {
  899. for (i = 0; i < MAX_TEXT_ANIM; i++) {
  900. if (p_src->p_atext[j][i])
  901. p_dest->p_atext[j][i] =
  902. key_kopiruj_material_animace(p_src->p_atext[j][i]);
  903. }
  904. }
  905. p_dest->p_sub = NULL;
  906. return (p_dest);
  907. }
  908. EDIT_MATERIAL *kopiruj_material(EDIT_MATERIAL * p_src)
  909. {
  910. EDIT_MATERIAL *p_dest;
  911. if (!p_src)
  912. return (NULL);
  913. p_dest = (EDIT_MATERIAL *) mmalloc(sizeof(EDIT_MATERIAL));
  914. return (kopiruj_material_indir(p_src, p_dest));
  915. }
  916. /* ****************************************************
  917. Dalsi
  918. */
  919. #define miror(promena)\
  920. {\
  921. for(i = 0; i < p_dest->vertexnum; i++) {\
  922. stred += promena;\
  923. }\
  924. stred /= (float)p_dest->vertexnum;\
  925. for(i = 0; i < p_dest->vertexnum; i++) {\
  926. if(promena > stred)\
  927. promena -= (promena - stred);\
  928. else\
  929. promena += (stred - promena);\
  930. } \
  931. }\
  932. /* *******************************************************
  933. Mesh
  934. */
  935. GAME_MESH_OLD *kopiruj_mesh(GAME_MESH_OLD * p_src, GAME_MESH_DATA * p_data)
  936. {
  937. GAME_MESH_OLD *p_dest = vyrob_mesh(p_src->objektu, p_data);
  938. int i;
  939. if (!p_dest)
  940. chyba("Pamet!");
  941. if (!p_data)
  942. kopiruj_mesh_data(p_src->p_data, p_dest, p_dest->p_data);
  943. p_dest->vertexnum = p_src->vertexnum;
  944. p_dest->objektu = p_src->objektu;
  945. p_dest->facevel = p_src->facevel;
  946. p_dest->p_vertex_pos =
  947. (BOD *) kopiruj_pole(p_src->p_vertex_pos,
  948. sizeof(p_src->p_vertex_pos[0]) * p_src->vertexnum);
  949. p_dest->p_vertex_norm =
  950. (BOD *) kopiruj_pole(p_src->p_vertex_norm,
  951. sizeof(p_dest->p_vertex_norm[0]) * p_src->vertexnum);
  952. p_dest->p_vertex_diff =
  953. (BODRGBA *) kopiruj_pole(p_src->p_vertex_diff,
  954. sizeof(p_dest->p_vertex_diff[0]) * p_src->vertexnum);
  955. p_dest->p_vertex_diff_material =
  956. (BODRGBA *) kopiruj_pole(p_src->p_vertex_diff_material,
  957. sizeof(p_dest->p_vertex_diff_material[0]) * p_src->vertexnum);
  958. p_dest->p_vertex_spec =
  959. (BODRGB *) kopiruj_pole(p_src->p_vertex_spec,
  960. sizeof(p_dest->p_vertex_spec[0]) * p_src->vertexnum);
  961. p_dest->p_vertex_spec_material =
  962. (BODRGB *) kopiruj_pole(p_src->p_vertex_spec_material,
  963. sizeof(p_dest->p_vertex_spec_material[0]) * p_src->vertexnum);
  964. p_dest->p_vertex_uv1_material =
  965. (BODUV *) kopiruj_pole(p_src->p_vertex_uv1_material,
  966. sizeof(p_dest->p_vertex_uv1_material[0]) * p_src->vertexnum);
  967. p_dest->p_vertex_uv2_material =
  968. (BODUV *) kopiruj_pole(p_src->p_vertex_uv2_material,
  969. sizeof(p_dest->p_vertex_uv2_material[0]) * p_src->vertexnum);
  970. memcpy(p_dest->objektnum, p_src->objektnum,
  971. sizeof(p_src->objektnum[0]) * p_src->objektu);
  972. memcpy(p_dest->objektstart, p_src->objektstart,
  973. sizeof(p_src->objektstart[0]) * p_src->objektu);
  974. memcpy(p_dest->facenum, p_src->facenum,
  975. sizeof(p_src->facenum[0]) * p_src->objektu);
  976. memcpy(p_dest->facestart, p_src->facestart,
  977. sizeof(p_src->facestart[0]) * p_src->objektu);
  978. memcpy(p_dest->p_mat, p_src->p_mat,
  979. sizeof(p_src->p_mat[0]) * p_src->objektu);
  980. p_dest->p_face =
  981. (int *) mmalloc(sizeof(p_dest->p_face[0]) * p_src->facevel);
  982. memcpy(p_dest->p_face, p_src->p_face,
  983. sizeof(p_dest->p_face[0]) * p_src->facevel);
  984. p_dest->simnum = p_src->simnum;
  985. if (p_dest->simnum) {
  986. p_dest->p_sim =
  987. (SIMPLE_TRACK_INFO *) mmalloc(sizeof(p_dest->p_sim[0]) *
  988. p_dest->simnum);
  989. for (i = 0; i < p_dest->simnum; i++) {
  990. key_sim_to_sim_indir(p_dest->p_sim + i, p_src->p_sim + i);
  991. }
  992. }
  993. return (p_dest);
  994. }
  995. /* Kopie Mesh-Dat
  996. */
  997. GAME_MESH_DATA *kopiruj_mesh_data(GAME_MESH_DATA * p_src,
  998. GAME_MESH_OLD * p_mesh_top, GAME_MESH_DATA * p_desc)
  999. {
  1000. struct _STATIC_LIGHT *p_lss, *p_lsd;
  1001. struct _DYN_LIGHT *p_lds, *p_ldd;
  1002. struct _EXTRA_DYN_LIGHT *p_les, *p_led;
  1003. if (!p_src || !p_desc)
  1004. return (NULL);
  1005. p_desc->p_mesh = p_mesh_top;
  1006. p_desc->kflag = p_src->kflag;
  1007. p_desc->m1flag = p_src->m1flag;
  1008. p_desc->m2flag = p_src->m2flag;
  1009. p_desc->mesh_handle = p_src->mesh_handle;
  1010. p_desc->mesh_ID = p_src->mesh_ID;
  1011. p_desc->slightnum = p_src->slightnum;
  1012. p_desc->lightmax = p_src->lightmax;
  1013. p_desc->lightakt = p_src->lightakt;
  1014. if (p_src->lightmax && p_src->p_light) {
  1015. p_desc->p_light = (void **) mmalloc(sizeof(void *) * p_src->lightmax);
  1016. memcpy(p_desc->p_light, p_src->p_light, sizeof(void *) * p_src->lightmax);
  1017. }
  1018. p_desc->top_edlight = p_src->top_edlight;
  1019. p_desc->edlightnum = p_src->edlightnum;
  1020. p_desc->edlightmax = p_src->edlightmax;
  1021. if (p_src->edlightmax && p_src->p_edlight) {
  1022. p_desc->p_edlight =
  1023. (EXTRA_DYN_LIGHT **) mmalloc(sizeof(p_desc->p_edlight[0]) *
  1024. p_src->edlightnum);
  1025. memcpy(p_desc->p_edlight, p_src->p_edlight,
  1026. sizeof(p_desc->p_edlight[0]) * p_src->edlightnum);
  1027. }
  1028. // Kopie statickych svetel
  1029. if (p_src->p_lslight) {
  1030. p_desc->p_lslight = (STATIC_LIGHT *) mmalloc(sizeof(p_src->p_lslight[0]));
  1031. memcpy(p_desc->p_lslight, p_src->p_lslight, sizeof(p_src->p_lslight[0]));
  1032. p_lss = p_src->p_lslight->p_next;
  1033. p_lsd = p_desc->p_lslight;
  1034. while (p_lss) {
  1035. p_lsd->p_next = (STATIC_LIGHT *) mmalloc(sizeof(p_lsd->p_next[0]));
  1036. memcpy(p_lsd->p_next, p_lss, sizeof(p_lss[0]));
  1037. p_lss = p_lss->p_next;
  1038. p_lsd = p_lsd->p_next;
  1039. }
  1040. }
  1041. // Kopie dynamickych svetel
  1042. if (p_src->p_ldlight) {
  1043. p_desc->p_ldlight = (DYN_LIGHT *) mmalloc(sizeof(p_src->p_ldlight[0]));
  1044. memcpy(p_desc->p_ldlight, p_src->p_ldlight, sizeof(p_src->p_ldlight[0]));
  1045. p_lds = p_src->p_ldlight->p_next;
  1046. p_ldd = p_desc->p_ldlight;
  1047. while (p_lds) {
  1048. p_ldd->p_next = (DYN_LIGHT *) mmalloc(sizeof(p_ldd->p_next[0]));
  1049. memcpy((void *) p_ldd->p_next, p_lss, sizeof(p_lds[0]));
  1050. p_lds = p_lds->p_next;
  1051. p_ldd = p_ldd->p_next;
  1052. }
  1053. }
  1054. // Kopie extra-svetel
  1055. if (p_src->p_lelight) {
  1056. p_desc->p_lelight =
  1057. (EXTRA_DYN_LIGHT *) mmalloc(sizeof(p_src->p_lelight[0]));
  1058. memcpy(p_desc->p_lelight, p_src->p_lelight, sizeof(p_src->p_lelight[0]));
  1059. p_les = p_src->p_lelight->p_next;
  1060. p_led = p_desc->p_lelight;
  1061. while (p_les) {
  1062. p_led->p_next = (EXTRA_DYN_LIGHT *) mmalloc(sizeof(p_led->p_next[0]));
  1063. memcpy(p_led->p_next, p_les, sizeof(p_les[0]));
  1064. p_les = p_les->p_next;
  1065. p_led = p_led->p_next;
  1066. }
  1067. }
  1068. return (p_desc);
  1069. }
  1070. /*
  1071. word *facenum; // pocty facu v extra-facich - polich
  1072. word *facestart; // starty facu jednotlivych objektu
  1073. EXTRA_FACE **p_face;
  1074. int
  1075. */
  1076. void rozsir_objekt_light(EDIT_OBJEKT * p_obj)
  1077. {
  1078. MUJ_BOD v, *p_v1, *p_v2;
  1079. MUJ_BOD nov[5];
  1080. word face[3 * 4], f[6];
  1081. int i, j, p[6], n, num = 0;
  1082. if (p_obj->vertexnum != 4 || p_obj->facenum != 6)
  1083. return;
  1084. for (i = 0; i < 6; i++) {
  1085. for (j = i; j < 6; j++) {
  1086. if (i == j)
  1087. continue;
  1088. if (p_obj->p_face[i] == p_obj->p_face[j]) {
  1089. p[num] = p_obj->p_face[i];
  1090. num++;
  1091. }
  1092. }
  1093. }
  1094. if (num == 2
  1095. && vzdal_bodu(p_obj->p_vertex + p[0], p_obj->p_vertex + p[1]) > 2.0f) {
  1096. p_v1 = p_obj->p_vertex + p[0];
  1097. p_v2 = p_obj->p_vertex + p[1];
  1098. v.x = p_v1->x * 0.5f + p_v2->x * 0.5f;
  1099. v.y = p_v1->y * 0.5f + p_v2->y * 0.5f;
  1100. v.z = p_v1->z * 0.5f + p_v2->z * 0.5f;
  1101. v.nx = p_v1->nx * 0.5f + p_v2->nx * 0.5f;
  1102. v.ny = p_v1->ny * 0.5f + p_v2->ny * 0.5f;
  1103. v.nz = p_v1->nz * 0.5f + p_v2->nz * 0.5f;
  1104. v.tu1 = p_v1->tu1 * 0.5f + p_v2->tu1 * 0.5f;
  1105. v.tu2 = p_v1->tu2 * 0.5f + p_v2->tu2 * 0.5f;
  1106. v.tv1 = p_v1->tv1 * 0.5f + p_v2->tv1 * 0.5f;
  1107. v.tv2 = p_v1->tv2 * 0.5f + p_v2->tv2 * 0.5f;
  1108. v.dr = p_v1->dr * 0.5f + p_v2->dr * 0.5f;
  1109. v.dg = p_v1->dg * 0.5f + p_v2->dg * 0.5f;
  1110. v.db = p_v1->db * 0.5f + p_v2->db * 0.5f;
  1111. v.da = p_v1->da * 0.5f + p_v2->da * 0.5f;
  1112. v.mdr = p_v1->mdr * 0.5f + p_v2->mdr * 0.5f;
  1113. v.mdg = p_v1->mdg * 0.5f + p_v2->mdg * 0.5f;
  1114. v.mdb = p_v1->mdb * 0.5f + p_v2->mdb * 0.5f;
  1115. v.mda = p_v1->mda * 0.5f + p_v2->mda * 0.5f;
  1116. v.sr = p_v1->sr * 0.5f + p_v2->sr * 0.5f;
  1117. v.sg = p_v1->sg * 0.5f + p_v2->sg * 0.5f;
  1118. v.sb = p_v1->sb * 0.5f + p_v2->sb * 0.5f;
  1119. v.msr = p_v1->msr * 0.5f + p_v2->msr * 0.5f;
  1120. v.msg = p_v1->msg * 0.5f + p_v2->msg * 0.5f;
  1121. v.msb = p_v1->msb * 0.5f + p_v2->msb * 0.5f;
  1122. for (i = 0; i < 6; i += 3) {
  1123. n = 0;
  1124. f[0] = p_obj->p_face[i];
  1125. if (p_obj->p_face[i] == p[0] || p_obj->p_face[i] == p[1]) {
  1126. n++;
  1127. }
  1128. if (p_obj->p_face[i + 1] == p[0] || p_obj->p_face[i + 1] == p[1]) {
  1129. if (n) { // druhy spolecny
  1130. f[3] = f[2] = p_obj->p_face[i + 2];
  1131. f[4] = f[1] = 4;
  1132. f[5] = p_obj->p_face[i + 1];
  1133. }
  1134. else { // prvni spolecny
  1135. f[1] = p_obj->p_face[i + 1];
  1136. f[4] = f[2] = 4;
  1137. f[3] = f[0];
  1138. f[5] = p_obj->p_face[i + 2];
  1139. }
  1140. }
  1141. else {
  1142. assert(n);
  1143. f[3] = f[1] = p_obj->p_face[i + 1];
  1144. f[5] = f[2] = 4;
  1145. f[4] = p_obj->p_face[i + 2];
  1146. }
  1147. memcpy(face + (i * 2), f, sizeof(f));
  1148. }
  1149. free(p_obj->p_face);
  1150. p_obj->p_face = (word *) mmalloc(sizeof(f[0]) * 12);
  1151. memcpy(p_obj->p_face, face, sizeof(f[0]) * 12);
  1152. memcpy(nov, p_obj->p_vertex, sizeof(p_obj->p_vertex[0]) * 4);
  1153. free(p_obj->p_vertex);
  1154. p_obj->p_vertex = (OBJ_VERTEX *) mmalloc(sizeof(p_obj->p_vertex[0]) * 5);
  1155. memcpy(p_obj->p_vertex, nov, sizeof(p_obj->p_vertex[0]) * 4);
  1156. p_obj->p_vertex[4] = v;
  1157. p_obj->vertexnum = 5;
  1158. p_obj->facenum = 12;
  1159. }
  1160. }
  1161. // Prida extra-vertex
  1162. void kont_extra_light(EDIT_KONTEJNER * p_kont)
  1163. {
  1164. int i;
  1165. for (i = 0; i < p_kont->objektu; i++) {
  1166. if (p_kont->objektu)
  1167. rozsir_objekt_light(p_kont->p_obj[i]);
  1168. }
  1169. updatuj_kontejner_statistika(p_kont, FALSE);
  1170. }
  1171. void mesh_calc_varray(GAME_MESH_OLD * p_mesh)
  1172. {
  1173. VERTEX_ARRAYS *p_varray = &p_mesh->varray;
  1174. int vertexu = p_mesh->vertexnum;
  1175. int m2flag = p_mesh->p_data->m2flag;
  1176. int last, norm = glstav_pn_triangles
  1177. || p_mesh->
  1178. p_data->m2flag & (MAT2_CALC_MAP1 | MAT2_CALC_MAP2 | MAT2_CALC_MAP3 |
  1179. MAT2_CALC_MAP4);
  1180. if (gl_ext::extlist_vertex_array) {
  1181. p_varray->ati_start_pos = 0;
  1182. last = sizeof(p_mesh->p_vertex_pos[0]) * vertexu;
  1183. if (norm) {
  1184. p_varray->ati_start_norm = last;
  1185. last = last + sizeof(p_mesh->p_vertex_norm[0]) * vertexu;
  1186. }
  1187. if (m2flag & MAT2_DIFFUSE) {
  1188. p_varray->ati_start_diff = last;
  1189. last = last + sizeof(p_mesh->p_vertex_diff[0]) * vertexu;
  1190. }
  1191. p_varray->ati_start_spec = last;
  1192. last = last + sizeof(p_mesh->p_vertex_spec[0]) * vertexu;
  1193. if (m2flag & MAT2_MAP1) {
  1194. p_varray->ati_start_text[0] = last;
  1195. last = last + sizeof(p_mesh->p_vertex_uv2_material[0]) * vertexu;
  1196. }
  1197. if (m2flag & MAT2_MAP2) {
  1198. p_varray->ati_start_text[1] = last;
  1199. last = last + sizeof(p_mesh->p_vertex_uv3_material[0]) * vertexu;
  1200. }
  1201. if (m2flag & MAT2_MAP3) {
  1202. p_varray->ati_start_text[2] = last;
  1203. last = last + sizeof(p_mesh->p_vertex_uv4_material[0]) * vertexu;
  1204. }
  1205. if (m2flag & MAT2_MAP4) {
  1206. p_varray->ati_start_text[3] = last;
  1207. last = last + sizeof(p_mesh->p_vertex_uv4_material[0]) * vertexu;
  1208. }
  1209. //p_varray->ati_velikost = last+sizeof(p_mesh->p_face[0])*p_mesh->facevel;
  1210. p_varray->ati_velikost = last;
  1211. }
  1212. }
  1213. void mesh_preved_jointy_rec(JOINT * p_joint, int prvni_vertex)
  1214. {
  1215. int i;
  1216. for (i = 0; i < p_joint->vertexnum; i++) {
  1217. p_joint->p_vertexlist[i] += prvni_vertex;
  1218. }
  1219. if (p_joint->p_child)
  1220. mesh_preved_jointy_rec(p_joint->p_child, prvni_vertex);
  1221. if (p_joint->p_next)
  1222. mesh_preved_jointy_rec(p_joint->p_next, prvni_vertex);
  1223. if (p_joint->p_keys)
  1224. zrus_kosti_key(&p_joint->p_keys);
  1225. }
  1226. // prevede animace
  1227. JOINT_ANIMACE *mesh_preved_jointy(EDIT_OBJEKT * p_obj, int prvni_vertex,
  1228. int objnum, GAME_MESH_OLD * p_mesh)
  1229. {
  1230. JOINT_ANIMACE *p_ret = p_obj->p_joit_animace;
  1231. JOINT_ANIMACE *p_tmp = p_ret;
  1232. if (!p_ret) {
  1233. p_mesh->p_data->k2flag &= ~KONT2_JOINT_ANIM;
  1234. return (NULL);
  1235. }
  1236. key_kosti_interpoluj(p_ret);
  1237. while (p_tmp) {
  1238. if (p_tmp->p_child) {
  1239. mesh_preved_jointy_rec(p_tmp->p_child, prvni_vertex);
  1240. p_tmp->p_norm_in = p_mesh->p_vertex_norm_kosti;
  1241. p_tmp->p_norm_out = p_mesh->p_vertex_norm;
  1242. p_tmp->p_pos_in = p_mesh->p_vertex_pos_kosti;
  1243. p_tmp->p_pos_out = p_mesh->p_vertex_pos;
  1244. }
  1245. p_tmp = p_tmp->p_next;
  1246. }
  1247. p_ret->p_k2flag = (int *) &p_mesh->p_data->k2flag;
  1248. p_ret->p_kkflag = (int *) &p_mesh->p_data->kflag;
  1249. p_ret->p_koflag = (int *) p_mesh->p_kflag + objnum;
  1250. p_obj->p_joit_animace = NULL;
  1251. return (p_ret);
  1252. }
  1253. /*
  1254. #define O_INDEX_POCET 0
  1255. #define O_INDEX_TYP 1
  1256. #define O_INDEX_DATA 2
  1257. */
  1258. /* Slepi dohromady pole p_opt
  1259. p_mesh->p_face = opt_slep(p_opt, p_optnum, p_mesh->facestart, p_mesh->objektu, &p_mesh->facevel);
  1260. null_free((void **)&p_opt);
  1261. null_free((void **)&p_optnum);
  1262. */
  1263. /*
  1264. p_opt - pole poli p_opt
  1265. p_optnum - delky v jednotlivych pol i
  1266. p_start - pole zacatku poli facu jednotlivych objektu
  1267. objnum - pocet objektu
  1268. p_vysnum - vysledna delka slepeneho pole
  1269. - vraci slepene pole a jeho delku
  1270. */
  1271. int *opt_slep(int **p_opt, int *p_optnum, int *p_start, int objnum,
  1272. int *p_vysnum)
  1273. {
  1274. int start, i, vel, num;
  1275. int *p_tmp;
  1276. vel = 0;
  1277. for (i = 0; i < objnum; i++) {
  1278. vel += p_optnum[i];
  1279. }
  1280. *p_vysnum = vel;
  1281. p_tmp = (int *) mmalloc(sizeof(p_tmp[0]) * vel);
  1282. start = 0;
  1283. for (i = 0; i < objnum; i++) {
  1284. num = p_optnum[i];
  1285. memcpy(p_tmp + start, p_opt[i], sizeof(p_tmp[0]) * num);
  1286. free(p_opt[i]);
  1287. p_start[i] = start;
  1288. start += num;
  1289. }
  1290. return (p_tmp);
  1291. }
  1292. /*
  1293. #define O_INDEX_POCET 0
  1294. #define O_INDEX_TYP 1
  1295. #define O_INDEX_DATA 2
  1296. */
  1297. /* Pripocte k facum add
  1298. *p_opt - pointer na list optu
  1299. optnum - delka pole *p_opt (vcetne vsech indexu)
  1300. */
  1301. int opt_add(int *p_opt, int optnum, int add)
  1302. {
  1303. int i, j, num;
  1304. int sekci;
  1305. if (p_opt) {
  1306. j = 0;
  1307. sekci = 0;
  1308. while (j < optnum) {
  1309. num = p_opt[O_INDEX_POCET];
  1310. for (i = O_INDEX_DATA; i < num + O_INDEX_DATA; i++) {
  1311. p_opt[i] += add;
  1312. }
  1313. j += num + O_INDEX_DATA;
  1314. p_opt += num + O_INDEX_DATA;
  1315. sekci++;
  1316. }
  1317. return (sekci);
  1318. }
  1319. else {
  1320. return (0);
  1321. }
  1322. }
  1323. void opt_dopln(EDIT_OBJEKT * p_obj)
  1324. {
  1325. int optnum;
  1326. int *p_opt;
  1327. int i;
  1328. if (!p_obj->p_opt) {
  1329. kprintf(TRUE, "Stary objekt - calc strip %s...", p_obj->jmeno);
  1330. optnum = p_obj->facenum + 2;
  1331. p_opt = (int *) mmalloc(sizeof(p_opt[0]) * optnum);
  1332. p_opt[O_INDEX_POCET] = p_obj->facenum;
  1333. p_opt[O_INDEX_TYP] = p_obj->face_typ;
  1334. for (i = 0; i < p_obj->facenum; i++)
  1335. p_opt[i + O_INDEX_DATA] = p_obj->p_face[i];
  1336. p_obj->p_opt = p_opt;
  1337. p_obj->optnum = optnum;
  1338. }
  1339. }
  1340. GAME_MESH_OLD *edit_to_mesh(GAME_MESH_DATA * p_mesh_data,
  1341. EDIT_KONTEJNER * p_kont, EDIT_MATERIAL ** p_mat, int max_mat,
  1342. int shadow_volume)
  1343. {
  1344. GAME_MESH_OLD *p_mesh = vyrob_mesh(p_kont->objektu, p_mesh_data);
  1345. // VERTEX_ARRAYS *p_varray = &p_mesh->varray;
  1346. GLMATRIX tmp;
  1347. BOD *p_piv;
  1348. int **p_opt;
  1349. int *p_optnum;
  1350. int i, j, p, k, vertexnum, bodu = p_kont->bodu;
  1351. size_ptr vel;
  1352. //int shut = 0;
  1353. int pivot = 0;
  1354. kprintf(TRUE, "Edit-to-mesh %s (f:%d v:%d)", p_kont->jmeno, p_kont->facu,
  1355. p_kont->bodu);
  1356. strcpy(p_mesh->jmeno, p_kont->jmeno);
  1357. p_mesh->p_data->kflag |=
  1358. (p_kont->kflag & KONT_STATIC ? p_kont->
  1359. kflag : p_kont->kflag | KONT_POHYB);
  1360. p_mesh->p_data->m1flag |= p_kont->m1flag;
  1361. p_mesh->p_data->m2flag |= p_kont->m2flag;
  1362. p_mesh->objektu = p_kont->objektu;
  1363. p_mesh->vertexnum = p_kont->bodu;
  1364. p_mesh->world = p_kont->world; // world matrix meshe
  1365. p_mesh->m = p_kont->mpmatrix; // mp (mesh-pozition) matrix meshe/kontejneru
  1366. p_mesh->low_id = p_kont->low_id;
  1367. p_mesh->p_data->mesh_ID = p_kont->kontejner_ID;
  1368. p_mesh->obb_local = p_kont->obb;
  1369. p_mesh->obb_world = p_kont->obb;
  1370. p_mesh->p_kflag =
  1371. (dword *) mmalloc(sizeof(p_mesh->p_kflag[0]) * p_kont->objektu);
  1372. p_opt = (int **) mmalloc(sizeof(p_opt[0]) * p_kont->objektu);
  1373. p_optnum = (int *) mmalloc(sizeof(p_optnum[0]) * p_kont->objektu);
  1374. p_mesh->p_mlha = p_kont->p_mlha;
  1375. if (p_mesh->p_mlha) {
  1376. p_kont->p_mlha->p_kont = p_mesh;
  1377. p_kont->p_mlha = NULL;
  1378. }
  1379. // Obalkovy system
  1380. p_mesh->p_obb_local =
  1381. (OBB_OLD *) mmalloc(sizeof(p_mesh->p_obb_local[0]) * p_mesh->objektu);
  1382. p_mesh->p_obb_world =
  1383. (OBB_OLD *) mmalloc(sizeof(p_mesh->p_obb_world[0]) * p_mesh->objektu);
  1384. if (p_kont->kflag & KONT_KEYFRAME) {
  1385. p_mesh->p_key =
  1386. (GLMATRIX *) mmalloc(sizeof(p_mesh->p_key[0]) * p_mesh->objektu);
  1387. for (i = 0; i < p_mesh->objektu; i++) {
  1388. init_matrix(p_mesh->p_key + i);
  1389. }
  1390. }
  1391. // joint-animace
  1392. if (p_kont->k2flag & KONT2_JOINT_ANIM) {
  1393. p_mesh->p_joint_anim =
  1394. (JOINT_ANIMACE **) mmalloc(sizeof(p_mesh->p_joint_anim[0]) *
  1395. p_mesh->objektu);
  1396. }
  1397. if (p_kont->kflag & KONT_NORMAL_ANIM) {
  1398. vyrob_pole(&p_mesh->p_normal_scale, p_mesh->objektu);
  1399. vyrob_pole(&p_mesh->p_normal_scale_last, p_mesh->objektu);
  1400. }
  1401. vyrob_pole(&p_mesh->p_Objekt_ID, p_mesh->objektu);
  1402. vel = sizeof(p_mesh->p_vertex_pos[0]) + sizeof(p_mesh->p_vertex_norm[0]);
  1403. if (p_kont->m2flag & MAT2_DIFFUSE) {
  1404. vel += sizeof(p_mesh->p_vertex_diff[0]) +
  1405. sizeof(p_mesh->p_vertex_diff_material[0]);
  1406. }
  1407. if (p_kont->m2flag & MAT2_SPECULAR) {
  1408. vel += sizeof(p_mesh->p_vertex_spec[0]) +
  1409. sizeof(p_mesh->p_vertex_spec_material[0]);
  1410. }
  1411. else if (p_kont->m2flag & MAT2_ENV_SPEC) {
  1412. vel += sizeof(p_mesh->p_vertex_spec[0]);
  1413. }
  1414. if (p_kont->m2flag & MAT2_MAP1) {
  1415. vel += sizeof(p_mesh->p_vertex_uv1_material[0]);
  1416. }
  1417. if (p_kont->m2flag & MAT2_MAP2) {
  1418. vel += sizeof(p_mesh->p_vertex_uv2_material[0]);
  1419. }
  1420. if (p_kont->m2flag & MAT2_MAP3) {
  1421. vel += sizeof(p_mesh->p_vertex_uv3_material[0]);
  1422. }
  1423. if (p_kont->m2flag & MAT2_MAP4) {
  1424. vel += sizeof(p_mesh->p_vertex_uv4_material[0]);
  1425. }
  1426. vyrob_pole_abs(&p_mesh->p_vertex_pos, vel * bodu);
  1427. p_mesh->p_vertex_norm = p_mesh->p_vertex_pos + bodu;
  1428. vel = reinterpret_cast<size_ptr>(p_mesh->p_vertex_norm + bodu);
  1429. if (p_kont->m2flag & MAT2_DIFFUSE) {
  1430. p_mesh->p_vertex_diff = (BARVA_RGBA *) vel;
  1431. p_mesh->p_vertex_diff_material = p_mesh->p_vertex_diff + bodu;
  1432. vel = reinterpret_cast<size_ptr>(p_mesh->p_vertex_diff_material + bodu);
  1433. }
  1434. if (p_kont->m2flag & MAT2_SPECULAR) {
  1435. p_mesh->p_vertex_spec = (BODRGB *) vel;
  1436. p_mesh->p_vertex_spec_material = p_mesh->p_vertex_spec + bodu;
  1437. vel = reinterpret_cast<size_ptr>(p_mesh->p_vertex_spec_material + bodu);
  1438. }
  1439. else if (p_kont->m2flag & MAT2_ENV_SPEC) {
  1440. p_mesh->p_vertex_spec = (BODRGB *) vel;
  1441. vel = reinterpret_cast<size_ptr>(p_mesh->p_vertex_spec + bodu);
  1442. }
  1443. if (p_kont->m2flag & MAT2_MAP1) {
  1444. p_mesh->p_vertex_uv1_material = (BODUV *) vel;
  1445. vel = reinterpret_cast<size_ptr>(p_mesh->p_vertex_uv1_material + bodu);
  1446. }
  1447. if (p_kont->m2flag & MAT2_MAP2) {
  1448. p_mesh->p_vertex_uv2_material = (BODUV *) vel;
  1449. vel = reinterpret_cast<size_ptr>(p_mesh->p_vertex_uv2_material + bodu);
  1450. }
  1451. if (p_kont->m2flag & MAT2_MAP3) {
  1452. p_mesh->p_vertex_uv3_material = (BODUV *) vel;
  1453. vel = reinterpret_cast<size_ptr>(p_mesh->p_vertex_uv3_material + bodu);
  1454. }
  1455. if (p_kont->m2flag & MAT2_MAP4) {
  1456. p_mesh->p_vertex_uv4_material = (BODUV *) vel;
  1457. vel = reinterpret_cast<size_ptr>(p_mesh->p_vertex_uv4_material + bodu);
  1458. }
  1459. if (p_kont->k2flag & KONT2_JOINT_ANIM) {
  1460. p_mesh->p_vertex_pos_kosti =
  1461. (BOD *) mmalloc(sizeof(p_mesh->p_vertex_pos_kosti[0]) * bodu);
  1462. p_mesh->p_vertex_norm_kosti =
  1463. (BOD *) mmalloc(sizeof(p_mesh->p_vertex_norm_kosti[0]) * bodu);
  1464. }
  1465. for (i = 0, p = 0, j = 0; i < MAX_KONT_OBJEKTU; i++) {
  1466. if (!p_kont->p_obj[i])
  1467. continue;
  1468. opt_dopln(p_kont->p_obj[i]);
  1469. if (p_kont->kflag & KONT_KEYFRAME) {
  1470. p_mesh->p_key[j] = p_kont->p_obj[i]->m;
  1471. }
  1472. p_piv = &p_kont->p_obj[i]->pivot;
  1473. if (!pivot && (p_piv->x != 0.0f || p_piv->y != 0.0f || p_piv->z != 0.0f)) {
  1474. p_mesh->kofola_pivot = *p_piv;
  1475. pivot = TRUE;
  1476. }
  1477. p_mesh->p_obb_world[j] = p_mesh->p_obb_local[j] = p_kont->p_obj[i]->obb;
  1478. p_mesh->p_Objekt_ID[j] = p_kont->p_obj[i]->Objekt_ID;
  1479. vertexnum = p_kont->p_obj[i]->vertexnum;
  1480. for (k = 0; k < vertexnum; k++) {
  1481. p_mesh->p_vertex_pos[k + p].x = p_kont->p_obj[i]->p_vertex[k].x;
  1482. p_mesh->p_vertex_pos[k + p].y = p_kont->p_obj[i]->p_vertex[k].y;
  1483. p_mesh->p_vertex_pos[k + p].z = p_kont->p_obj[i]->p_vertex[k].z;
  1484. p_mesh->p_vertex_norm[k + p].x = p_kont->p_obj[i]->p_vertex[k].nx;
  1485. p_mesh->p_vertex_norm[k + p].y = p_kont->p_obj[i]->p_vertex[k].ny;
  1486. p_mesh->p_vertex_norm[k + p].z = p_kont->p_obj[i]->p_vertex[k].nz;
  1487. if (p_mesh->p_vertex_pos_kosti)
  1488. p_mesh->p_vertex_pos_kosti[k + p] = p_mesh->p_vertex_pos[k + p];
  1489. if (p_mesh->p_vertex_norm_kosti)
  1490. p_mesh->p_vertex_norm_kosti[k + p] = p_mesh->p_vertex_norm[k + p];
  1491. }
  1492. if (p_kont->m2flag & MAT2_DIFFUSE) {
  1493. for (k = 0; k < vertexnum; k++) {
  1494. p_mesh->p_vertex_diff[k + p].r = p_kont->p_obj[i]->p_vertex[k].dr;
  1495. p_mesh->p_vertex_diff[k + p].g = p_kont->p_obj[i]->p_vertex[k].dg;
  1496. p_mesh->p_vertex_diff[k + p].b = p_kont->p_obj[i]->p_vertex[k].db;
  1497. p_mesh->p_vertex_diff[k + p].a = p_kont->p_obj[i]->p_vertex[k].da;
  1498. p_mesh->p_vertex_diff_material[k + p].r =
  1499. p_kont->p_obj[i]->p_vertex[k].mdr;
  1500. p_mesh->p_vertex_diff_material[k + p].g =
  1501. p_kont->p_obj[i]->p_vertex[k].mdg;
  1502. p_mesh->p_vertex_diff_material[k + p].b =
  1503. p_kont->p_obj[i]->p_vertex[k].mdb;
  1504. p_mesh->p_vertex_diff_material[k + p].a =
  1505. p_kont->p_obj[i]->p_vertex[k].mda;
  1506. }
  1507. }
  1508. if (p_kont->m2flag & MAT2_SPECULAR) {
  1509. for (k = 0; k < vertexnum; k++) {
  1510. p_mesh->p_vertex_spec[k + p].r = p_kont->p_obj[i]->p_vertex[k].sr;
  1511. p_mesh->p_vertex_spec[k + p].g = p_kont->p_obj[i]->p_vertex[k].sg;
  1512. p_mesh->p_vertex_spec[k + p].b = p_kont->p_obj[i]->p_vertex[k].sb;
  1513. p_mesh->p_vertex_spec_material[k + p].r =
  1514. p_kont->p_obj[i]->p_vertex[k].msr;
  1515. p_mesh->p_vertex_spec_material[k + p].g =
  1516. p_kont->p_obj[i]->p_vertex[k].msg;
  1517. p_mesh->p_vertex_spec_material[k + p].b =
  1518. p_kont->p_obj[i]->p_vertex[k].msb;
  1519. }
  1520. }
  1521. if (p_kont->m2flag & MAT2_MAP1) {
  1522. for (k = 0; k < vertexnum; k++) {
  1523. p_mesh->p_vertex_uv1_material[k + p].u =
  1524. p_kont->p_obj[i]->p_vertex[k].tu1;
  1525. p_mesh->p_vertex_uv1_material[k + p].v =
  1526. p_kont->p_obj[i]->p_vertex[k].tv1;
  1527. }
  1528. }
  1529. if (p_kont->m2flag & MAT2_MAP2) {
  1530. for (k = 0; k < vertexnum; k++) {
  1531. p_mesh->p_vertex_uv2_material[k + p].u =
  1532. p_kont->p_obj[i]->p_vertex[k].tu2;
  1533. p_mesh->p_vertex_uv2_material[k + p].v =
  1534. p_kont->p_obj[i]->p_vertex[k].tv2;
  1535. }
  1536. }
  1537. if (p_kont->m2flag & MAT2_MAP3) {
  1538. for (k = 0; k < vertexnum; k++) {
  1539. p_mesh->p_vertex_uv3_material[k + p].u =
  1540. p_kont->p_obj[i]->p_vertex[k].tu3;
  1541. p_mesh->p_vertex_uv3_material[k + p].v =
  1542. p_kont->p_obj[i]->p_vertex[k].tv3;
  1543. }
  1544. }
  1545. if (p_kont->m2flag & MAT2_MAP4) {
  1546. for (k = 0; k < vertexnum; k++) {
  1547. p_mesh->p_vertex_uv4_material[k + p].u =
  1548. p_kont->p_obj[i]->p_vertex[k].tu4;
  1549. p_mesh->p_vertex_uv4_material[k + p].v =
  1550. p_kont->p_obj[i]->p_vertex[k].tv4;
  1551. }
  1552. }
  1553. p_opt[j] = p_kont->p_obj[i]->p_opt;
  1554. p_optnum[j] = p_kont->p_obj[i]->optnum;
  1555. p_kont->p_obj[i]->p_opt = NULL;
  1556. p_kont->p_obj[i]->optnum = 0;
  1557. p_mesh->facenum[j] = opt_add(p_opt[j], p_optnum[j], p);
  1558. p_mesh->objektnum[j] = p_kont->p_obj[i]->vertexnum;
  1559. p_mesh->objektstart[j] = p;
  1560. p_mesh->p_mat[j] = p_kont->p_obj[i]->material;
  1561. if (p_kont->k2flag & KONT2_JOINT_ANIM) {
  1562. p_mesh->p_joint_anim[j] =
  1563. mesh_preved_jointy(p_kont->p_obj[i], p, j, p_mesh);
  1564. }
  1565. p += p_kont->p_obj[i]->vertexnum;
  1566. j++;
  1567. }
  1568. p_mesh->objektu = j; // pocet skutecne prevedenych objektu
  1569. assert(j == p_kont->objektu);
  1570. p_mesh->p_face =
  1571. opt_slep(p_opt, p_optnum, p_mesh->facestart, p_mesh->objektu,
  1572. &p_mesh->facevel);
  1573. null_free((void **) &p_opt);
  1574. null_free((void **) &p_optnum);
  1575. //assert(!p_kont->p_slight);
  1576. lo_premapuj_svetla_kont_mesh(p_kont, p_mesh);
  1577. // kopie lokalnich animaci
  1578. if (p_kont->kflag & KONT_KEYFRAME) {
  1579. for (i = 0, p = K_CHYBA; i < KONT_MAX_ANIM; i++) {
  1580. if (p_kont->sim[i].keynum) {
  1581. p = i;
  1582. }
  1583. }
  1584. if (p == K_CHYBA) {
  1585. p_kont->kflag &= ~KONT_KEYFRAME;
  1586. }
  1587. else {
  1588. p++;
  1589. // p je posledni pouzity mesh
  1590. p_mesh->p_sim =
  1591. (SIMPLE_TRACK_INFO *) mmalloc(sizeof(p_mesh->p_sim[0]) * p);
  1592. p_mesh->simnum = p;
  1593. // kopirovani animacnich stromu
  1594. for (i = 0; i < p; i++) {
  1595. if (p_kont->sim[i].keynum) {
  1596. key_sim_root_to_sim_indir(p_kont->sim + i, p_mesh->p_sim + i);
  1597. key_sim_dopln_matrix_mesh(p_mesh, p_mesh->p_sim + i);
  1598. }
  1599. }
  1600. // ____OPRAVA____
  1601. p_mesh->p_sim_aktivni = p_mesh->p_sim;
  1602. p_mesh->sim_aktivni_time = 0;
  1603. mat_mult_dir(&p_mesh->world, &p_mesh->m, &tmp);
  1604. key_mesh_reanimuj(p_mesh, p_mesh->sim_aktivni_time, &tmp);
  1605. }
  1606. }
  1607. // Vertex-arrays inicializace
  1608. mesh_calc_varray(p_mesh);
  1609. mesh_pridej_vertex_array(p_mesh);
  1610. if (!(p_mesh->p_data->kflag & KONT_KEYFRAME)) {
  1611. mat_mult_dir(&p_mesh->world, &p_mesh->m, &tmp);
  1612. obb_transformuj(&p_mesh->obb_local, &tmp, &p_mesh->obb_world);
  1613. for (i = 0; i < p_mesh->objektu; i++)
  1614. obb_transformuj(p_mesh->p_obb_local + i, &tmp, p_mesh->p_obb_world + i);
  1615. }
  1616. return (p_mesh);
  1617. }
  1618. GAME_MESH_OLD *vyrob_mesh(int objektu, GAME_MESH_DATA * p_data)
  1619. {
  1620. GAME_MESH_OLD *p_mesh;
  1621. int i;
  1622. if ((p_mesh = (GAME_MESH_OLD *) mmalloc(sizeof(p_mesh[0]))) == NULL) {
  1623. chyba("pamet");
  1624. }
  1625. memset((void *) p_mesh, 0, sizeof(p_mesh[0]));
  1626. p_mesh->p_data = (p_data) ? p_data : vyrob_mesh_data();
  1627. p_mesh->objektu = objektu;
  1628. p_mesh->p_data->top_edlight = K_CHYBA;
  1629. p_mesh->objektnum =
  1630. (word *) mmalloc(sizeof(p_mesh->objektnum[0]) * objektu);
  1631. p_mesh->objektstart =
  1632. (word *) mmalloc(sizeof(p_mesh->objektstart[0]) * objektu);
  1633. p_mesh->facenum = (int *) mmalloc(sizeof(p_mesh->facenum[0]) * objektu);
  1634. p_mesh->facestart = (int *) mmalloc(sizeof(p_mesh->facestart[0]) * objektu);
  1635. p_mesh->p_mat = (int *) mmalloc(sizeof(p_mesh->p_mat[0]) * objektu);
  1636. p_mesh->siminfo.odkaz = K_CHYBA;
  1637. for (i = 0; i < LANI_FRONTA; i++)
  1638. p_mesh->siminfo.akt[i] = K_CHYBA;
  1639. return (p_mesh);
  1640. }
  1641. GAME_MESH_DATA *vyrob_mesh_data(void)
  1642. {
  1643. GAME_MESH_DATA *p_data;
  1644. p_data = (GAME_MESH_DATA *) mmalloc(sizeof(p_data[0]));
  1645. return (p_data);
  1646. }
  1647. void mesh_pridej_vodavertexy(GAME_MESH_OLD * p_mesh)
  1648. {
  1649. vyrob_pole(&p_mesh->p_vertex_diff_voda, p_mesh->vertexnum);
  1650. vyrob_pole(&p_mesh->p_vertex_spec_voda, p_mesh->vertexnum);
  1651. }
  1652. void zrus_fleky(FLEK ** p_flek)
  1653. {
  1654. if (*p_flek) {
  1655. zrus_fleky(&((*p_flek)->p_next));
  1656. free(*p_flek);
  1657. *p_flek = NULL;
  1658. }
  1659. }
  1660. void zrus_flare(LENS_FLARE ** p_flare)
  1661. {
  1662. if (*p_flare) {
  1663. zrus_flare(&((*p_flare)->p_next));
  1664. null_free((void **) &(*p_flare)->p_sloz);
  1665. free(*p_flare);
  1666. *p_flare = NULL;
  1667. }
  1668. }
  1669. void zrus_mesh_data(GAME_MESH_DATA ** p_tmp)
  1670. {
  1671. GAME_MESH_DATA *p_data = *p_tmp;
  1672. null_free((void **) &p_data->p_light);
  1673. null_free((void **) &p_data->p_edlight);
  1674. null_free((void **) p_tmp);
  1675. }
  1676. void zrus_mesh(GAME_MESH_OLD ** p_mesh_top)
  1677. {
  1678. GAME_MESH_OLD *p_mesh = *p_mesh_top;
  1679. GAME_MESH_OLD *p_tmp;
  1680. int i;
  1681. zrus_mesh_data(&p_mesh->p_data);
  1682. while (p_mesh) {
  1683. if (p_mesh->p_sim) {
  1684. for (i = 0; i < p_mesh->simnum; i++) {
  1685. key_sim_zrus(p_mesh->p_sim + i);
  1686. }
  1687. free(p_mesh->p_sim);
  1688. }
  1689. null_free((void **) &p_mesh->p_vertex_pos);
  1690. null_free((void **) &p_mesh->objektnum);
  1691. null_free((void **) &p_mesh->objektstart);
  1692. null_free((void **) &p_mesh->facenum);
  1693. null_free((void **) &p_mesh->facestart);
  1694. null_free((void **) &p_mesh->p_face);
  1695. null_free((void **) &p_mesh->p_mat);
  1696. null_free((void **) &p_mesh->p_key);
  1697. null_free((void **) &p_mesh->p_obb_local);
  1698. null_free((void **) &p_mesh->p_obb_world);
  1699. null_free((void **) &p_mesh->p_Objekt_ID);
  1700. null_free((void **) &p_mesh->p_kflag);
  1701. if (mesh_vertex_array_zrus)
  1702. mesh_vertex_array_zrus(p_mesh);
  1703. p_tmp = p_mesh;
  1704. p_mesh = p_mesh->p_next;
  1705. null_free((void **) &p_tmp);
  1706. }
  1707. *p_mesh_top = NULL;
  1708. }
  1709. /************************************************************************
  1710. Hledaci rutiny - co hledaji ruzne veci jako treba
  1711. volne textury, plne textury a podobne
  1712. ************************************************************************
  1713. */
  1714. // pocet textur - MAX_EDIT_TEXTUR
  1715. int lo_najdi_texturu(EDIT_TEXT * p_text, int max, char *p_file, int flag)
  1716. {
  1717. int i;
  1718. for (i = 0; i < max; i++) {
  1719. if (flag == p_text[i].flag && !strcasecmp(p_file, p_text[i].jmeno))
  1720. return (i);
  1721. }
  1722. return (K_CHYBA);
  1723. }
  1724. int lo_najdi_volnou_texturu(EDIT_TEXT * p_text, int max)
  1725. {
  1726. int i;
  1727. for (i = 0; i < max; i++) {
  1728. if (!p_text[i].jmeno[0]) {
  1729. return (i);
  1730. }
  1731. }
  1732. return (K_CHYBA);
  1733. }
  1734. // Pocet materialu - MAX_EDIT_MATERIALU
  1735. int lo_najdi_material(EDIT_MATERIAL ** p_mat, int max, char *p_text)
  1736. {
  1737. int i;
  1738. for (i = 0; i < max; i++) {
  1739. if (p_mat[i] && !strcasecmp(p_text, p_mat[i]->jmeno))
  1740. return (i);
  1741. }
  1742. return (K_CHYBA);
  1743. }
  1744. EDIT_MATERIAL *lo_najdi_material_text_point(EDIT_MATERIAL ** p_mat, int max,
  1745. EDIT_TEXT * p_text)
  1746. {
  1747. int i;
  1748. for (i = 0; i < max; i++) {
  1749. if (p_mat[i] && p_mat[i]->p_text[0] == p_text)
  1750. return (p_mat[i]);
  1751. }
  1752. return (NULL);
  1753. }
  1754. int lo_najdi_prazdny_material(EDIT_MATERIAL ** p_mat, int max)
  1755. {
  1756. int i;
  1757. for (i = 0; i < max; i++) {
  1758. if (!p_mat[i])
  1759. return (i);
  1760. }
  1761. return (K_CHYBA);
  1762. }
  1763. int lo_pocet_materialu(EDIT_MATERIAL ** p_mat, int max)
  1764. {
  1765. int i, vel = 0;
  1766. for (i = 0; i < max; i++) {
  1767. if (p_mat[i])
  1768. vel = i + 1;
  1769. }
  1770. return (vel);
  1771. }
  1772. int lo_najdi_kameru(KAMERA * p_kam, int max, char *p_jmeno)
  1773. {
  1774. int i;
  1775. for (i = 0; i < max; i++) {
  1776. if (p_kam[i].cislo != K_CHYBA && !strcasecmp(p_kam[i].jmeno, p_jmeno))
  1777. return (i);
  1778. }
  1779. return (K_CHYBA);
  1780. }
  1781. int lo_najdi_volnou_kameru(KAMERA * p_kam, int max)
  1782. {
  1783. int i;
  1784. for (i = 0; i < max; i++) {
  1785. if (p_kam[i].cislo == K_CHYBA) {
  1786. return (i);
  1787. }
  1788. }
  1789. return (K_CHYBA);
  1790. }
  1791. int lo_najdi_volny_kontejner(EDIT_KONTEJNER ** p_kont, int max)
  1792. {
  1793. int i;
  1794. for (i = 0; i < max; i++) {
  1795. if (!p_kont[i]) {
  1796. return (i);
  1797. }
  1798. }
  1799. return (K_CHYBA);
  1800. }
  1801. int lo_najdi_volny_kontejner_up(EDIT_KONTEJNER ** p_kont, int max, int up)
  1802. {
  1803. int i;
  1804. for (i = up; i < max; i++) {
  1805. if (!p_kont[i]) {
  1806. return (i);
  1807. }
  1808. }
  1809. return (K_CHYBA);
  1810. }
  1811. int lo_najdi_volny_kontejner_zpet(EDIT_KONTEJNER ** p_kont, int max)
  1812. {
  1813. int i;
  1814. for (i = max - 1; i > -1; i--) {
  1815. if (!p_kont[i]) {
  1816. return (i);
  1817. }
  1818. }
  1819. return (K_CHYBA);
  1820. }
  1821. int lo_najdi_kontejner(EDIT_KONTEJNER ** p_kont, int max, char *p_jmeno)
  1822. {
  1823. int i;
  1824. for (i = 0; i < max; i++) {
  1825. if (!p_kont[i])
  1826. continue;
  1827. if (!strcasecmp(p_kont[i]->jmeno, p_jmeno))
  1828. return (i);
  1829. }
  1830. return (K_CHYBA);
  1831. }
  1832. int lo_najdi_volny_objekt_kont(EDIT_KONTEJNER * p_kont)
  1833. {
  1834. int i;
  1835. if (!p_kont)
  1836. return (K_CHYBA);
  1837. for (i = 0; i < p_kont->max_objektu; i++) {
  1838. if (!p_kont->p_obj[i]) {
  1839. return (i);
  1840. }
  1841. }
  1842. return (K_CHYBA);
  1843. }
  1844. int lo_najdi_objekt_kont(EDIT_KONTEJNER * p_kont, char *p_jmeno)
  1845. {
  1846. int i;
  1847. if (!p_kont)
  1848. return (K_CHYBA);
  1849. for (i = 0; i < p_kont->max_objektu; i++) {
  1850. if (p_kont->p_obj[i]) {
  1851. if (!strcasecmp(p_kont->p_obj[i]->jmeno, p_jmeno))
  1852. return (i);
  1853. }
  1854. }
  1855. return (K_CHYBA);
  1856. }
  1857. int lo_najdi_objekt_kont_ID(EDIT_KONTEJNER * p_kont, int ID)
  1858. {
  1859. int i;
  1860. if (!p_kont)
  1861. return (K_CHYBA);
  1862. for (i = 0; i < p_kont->max_objektu; i++) {
  1863. if (p_kont->p_obj[i] && p_kont->p_obj[i]->Objekt_ID == ID)
  1864. return (i);
  1865. }
  1866. return (K_CHYBA);
  1867. }
  1868. int lo_najdi_objekt_kont_plny(EDIT_KONTEJNER * p_kont)
  1869. {
  1870. int i;
  1871. if (!p_kont)
  1872. return (K_CHYBA);
  1873. for (i = 0; i < p_kont->max_objektu; i++) {
  1874. if (p_kont->p_obj[i])
  1875. return (i);
  1876. }
  1877. return (K_CHYBA);
  1878. }
  1879. EDIT_OBJEKT *lo_najdi_objekt_kont_poiter(EDIT_KONTEJNER * p_kont,
  1880. char *p_jmeno)
  1881. {
  1882. int i;
  1883. if (!p_kont)
  1884. return (NULL);
  1885. for (i = 0; i < p_kont->max_objektu; i++) {
  1886. if (p_kont->p_obj[i] && !strcmp(p_kont->p_obj[i]->jmeno, p_jmeno))
  1887. return (p_kont->p_obj[i]);
  1888. }
  1889. return (NULL);
  1890. }
  1891. EDIT_OBJEKT *lo_najdi_objekt_kont_poiter_ID(EDIT_KONTEJNER * p_kont, int ID)
  1892. {
  1893. int i;
  1894. if (!p_kont)
  1895. return (NULL);
  1896. for (i = 0; i < p_kont->max_objektu; i++) {
  1897. if (p_kont->p_obj[i] && p_kont->p_obj[i]->Objekt_ID == ID)
  1898. return (p_kont->p_obj[i]);
  1899. }
  1900. return (NULL);
  1901. }
  1902. int lo_najdi_volny_mesh(GAME_MESH_OLD ** p_mesh, int max)
  1903. {
  1904. int i = -1;
  1905. while (p_mesh[++i] && i < max);
  1906. return ((i == max) ? K_CHYBA : i);
  1907. }
  1908. int lo_najdi_volny_flare(LENS_FLARE * p_flare, int max)
  1909. {
  1910. int i = -1;
  1911. while (p_flare[++i].akt && i < max);
  1912. return ((i == max) ? K_CHYBA : i);
  1913. }
  1914. // Prevede pole flaru na oboustrany linearni seznam
  1915. LENS_FLARE *lo_flare2linear(LENS_FLARE * p_flare_list, int max)
  1916. {
  1917. LENS_FLARE *p_flare, *p_flare_first = NULL;
  1918. LENS_FLARE *p_prev = NULL;
  1919. int i;
  1920. for (i = 0; i < max; i++) {
  1921. if (!p_flare_list[i].akt)
  1922. continue;
  1923. p_flare = (LENS_FLARE *) mmalloc(sizeof(p_flare[0]));
  1924. *p_flare = p_flare_list[i];
  1925. p_flare->p_next = NULL;
  1926. if (p_prev) {
  1927. p_flare->p_prev = p_prev;
  1928. p_prev->p_next = p_flare;
  1929. }
  1930. else {
  1931. p_flare->p_prev = NULL;
  1932. }
  1933. if (!i) {
  1934. p_flare_first = p_flare;
  1935. }
  1936. p_prev = p_flare;
  1937. }
  1938. return (p_flare_first);
  1939. }
  1940. int lo_posledni_svetlo(STATIC_LIGHT * p_light, int max)
  1941. {
  1942. int i = 0, mmx = 0;
  1943. while (i < max) {
  1944. if (p_light[i].akt)
  1945. mmx = i + 1;
  1946. i++;
  1947. }
  1948. return (mmx);
  1949. }
  1950. int lo_reload_textur_formaty(char *cDir, EDIT_TEXT * p_text,
  1951. int max, int save)
  1952. {
  1953. int i;
  1954. for (i = 0; i < max; i++) {
  1955. if (!p_text[i].jmeno[0] || p_text[i].load)
  1956. continue;
  1957. kprintf(TRUE, "Texture %s...", p_text[i].jmeno);
  1958. if (strcasestr(p_text[i].jmeno, ".png") ||
  1959. strcasestr(p_text[i].jmeno, ".btx"))
  1960. txt_nahraj_texturu_z_func(cDir, p_text[i].jmeno, p_text + i,
  1961. save, TRUE, NULL, bmp_nahraj);
  1962. else if (strstr(p_text[i].jmeno, ".dds"))
  1963. txt_nahraj_texturu_z_dds(cDir, p_text[i].jmeno, p_text + i, save);
  1964. else if (strstr(p_text[i].jmeno, ".tga"))
  1965. txt_nahraj_texturu_z_func(cDir, p_text[i].jmeno, p_text + i,
  1966. save, TRUE, NULL, bmp_nahraj);
  1967. else
  1968. assert(0);
  1969. }
  1970. return (0);
  1971. }
  1972. int lo_reload_textur_dir(EDIT_TEXT * p_text, int max, char *p_dir, int save)
  1973. {
  1974. return (lo_reload_textur_formaty(p_dir, p_text, max, save));
  1975. }
  1976. int lo_reload_textur(TEXT_DIR * p_dir, EDIT_TEXT * p_text, int num, int save)
  1977. {
  1978. int i;
  1979. for (i = 0; i < TEXT_DIRS; i++) {
  1980. if (p_dir->texture_dir[i][0]) {
  1981. txt_trida(p_dir->texture_dir_class[i]);
  1982. lo_reload_textur_dir(p_text, num, p_dir->texture_dir[i], save);
  1983. }
  1984. }
  1985. lo_reload_textur_chyby(p_text, num);
  1986. return (TRUE);
  1987. }
  1988. int lo_reload_textur_chyby(EDIT_TEXT * p_text, int num)
  1989. {
  1990. int i, ok, n;
  1991. for (i = 0, n = 0, ok = 0; i < num; i++) {
  1992. if (p_text[i].jmeno[0] && !p_text[i].load) {
  1993. kprintf(TRUE, "Chyba loadu textury %s...", p_text[i].jmeno);
  1994. n++;
  1995. }
  1996. else {
  1997. ok++;
  1998. }
  1999. }
  2000. kprintf(TRUE, "Nahrano %d textur", ok);
  2001. kprintf(TRUE, "Chybne nahrano %d textur", n);
  2002. return (TRUE);
  2003. }
  2004. int lo_reload_textur_vypis(EDIT_TEXT * p_text, int num)
  2005. {
  2006. int i;
  2007. for (i = 0; i < num; i++) {
  2008. if (p_text[i].jmeno[0]) {
  2009. kprintf(TRUE, "%d textura %s %d...", i, p_text[i].jmeno,
  2010. p_text[i].load);
  2011. }
  2012. }
  2013. return (TRUE);
  2014. }
  2015. int lo_smaz_textury(EDIT_TEXT * p_text, int max)
  2016. {
  2017. int i;
  2018. for (i = 0; i < max; i++) {
  2019. if (p_text[i].load) {
  2020. txt_zrus_texturu(p_text + i);
  2021. }
  2022. }
  2023. return (0);
  2024. }
  2025. int lo_smaz_textury_bmp(EDIT_TEXT * p_text, int max)
  2026. {
  2027. int i;
  2028. for (i = 0; i < max; i++) {
  2029. if (p_text[i].load && p_text[i].p_bmp) {
  2030. bmp_zrus(&p_text[i].p_bmp);
  2031. }
  2032. }
  2033. return (0);
  2034. }
  2035. // Vraci velikost pixelu v texturach
  2036. int lo_velikost_textur(EDIT_TEXT * p_text, int max)
  2037. {
  2038. int i, xres, yres, vel = 0;
  2039. int r, g, b, a, l, in;
  2040. for (i = 0; i < max; i++) {
  2041. if (p_text[i].load) {
  2042. glBindTexture(p_text[i].typ, p_text[i].text);
  2043. glGetTexLevelParameteriv(p_text[i].typ, 0, GL_TEXTURE_WIDTH, &xres);
  2044. glGetTexLevelParameteriv(p_text[i].typ, 0, GL_TEXTURE_HEIGHT, &yres);
  2045. glGetTexLevelParameteriv(p_text[i].typ, 0, GL_TEXTURE_RED_SIZE, &r);
  2046. glGetTexLevelParameteriv(p_text[i].typ, 0, GL_TEXTURE_GREEN_SIZE, &g);
  2047. glGetTexLevelParameteriv(p_text[i].typ, 0, GL_TEXTURE_BLUE_SIZE, &b);
  2048. glGetTexLevelParameteriv(p_text[i].typ, 0, GL_TEXTURE_ALPHA_SIZE, &a);
  2049. glGetTexLevelParameteriv(p_text[i].typ, 0, GL_TEXTURE_LUMINANCE_SIZE,
  2050. &l);
  2051. glGetTexLevelParameteriv(p_text[i].typ, 0, GL_TEXTURE_INTENSITY_SIZE,
  2052. &in);
  2053. vel += xres * yres * (r + b + g + a + l + in);
  2054. }
  2055. }
  2056. return (vel);
  2057. }
  2058. /* reloadne stage z textur do materialu
  2059. */
  2060. void lo_reload_stage(EDIT_MATERIAL ** p_mat, int num)
  2061. {
  2062. int i, t, as;
  2063. for (i = 0; i < num; i++) {
  2064. if (p_mat[i]) {
  2065. for (t = 0; t < MAT_TEXTUR; t++) {
  2066. if (p_mat[i]->p_text[t]) {
  2067. as = p_mat[i]->p_text[t]->alfa_stage;
  2068. if (as != K_CHYBA) {
  2069. p_mat[i]->alfa_state = as; //- alfa stage pouze od 1 textury
  2070. if (as == STAGE_ALFA_MASKA) {
  2071. p_mat[i]->flag &=
  2072. ~(MAT_PRUHLEDNY | MAT_NO_ZMASK | MAT_NO_ZTEST | MAT_NO_CULL);
  2073. }
  2074. }
  2075. as = p_mat[i]->p_text[t]->no_cull;
  2076. if (as) {
  2077. p_mat[i]->flag |= (MAT_NO_CULL);
  2078. }
  2079. }
  2080. }
  2081. }
  2082. }
  2083. }
  2084. int lo_pocet_textur(EDIT_TEXT * p_text, int max)
  2085. {
  2086. int i, vel = 0;
  2087. for (i = 0; i < max; i++) {
  2088. if (p_text[i].load) {
  2089. vel = i;
  2090. }
  2091. }
  2092. return (vel);
  2093. }
  2094. int lo_velikost_meshu(GAME_MESH_OLD ** p_mesh, int num, int *p_facu,
  2095. int *p_vertexu)
  2096. {
  2097. int i, vel = 0;
  2098. for (i = 0; i < num; i++) {
  2099. if (p_mesh[i]) {
  2100. vel += p_mesh[i]->varray.ati_velikost;
  2101. vel += p_mesh[i]->facevel * sizeof(p_mesh[0]->p_face[0]);
  2102. *p_facu += p_mesh[i]->facevel;
  2103. *p_vertexu += p_mesh[i]->vertexnum;
  2104. }
  2105. }
  2106. return (vel);
  2107. }
  2108. int lo_velikost_poly(EDIT_MESH_POLY * p_poly, int num, int *p_facu,
  2109. int *p_vertexu)
  2110. {
  2111. int i, vel = 0;
  2112. for (i = 0; i < num; i++) {
  2113. vel += p_poly[i].varray.ati_velikost;
  2114. *p_facu += p_poly[i].facenum;
  2115. *p_vertexu += p_poly[i].facenum;
  2116. }
  2117. return (vel);
  2118. }
  2119. /*
  2120. *.mnt - soubory s materialama
  2121. */
  2122. void lo_vymaz_materialy(EDIT_MATERIAL ** p_mat, int max_mat,
  2123. EDIT_TEXT * p_text, int max_text)
  2124. {
  2125. int i;
  2126. for (i = 0; i < max_mat; i++) {
  2127. if (p_mat[i])
  2128. zrus_material(p_mat + i);
  2129. }
  2130. for (i = 0; i < max_text; i++) {
  2131. if (p_text[i].load) {
  2132. txt_zrus_texturu(p_text + i);
  2133. }
  2134. }
  2135. }
  2136. /******************************************************************************
  2137. Materialy
  2138. ******************************************************************************
  2139. */
  2140. int lo_najdi_prazdnou_animaci(ANIM_MATERIAL * p_anim, int max)
  2141. {
  2142. int i;
  2143. for (i = 0; i < max; i++) {
  2144. if (!p_anim[i].pouzita)
  2145. return (i);
  2146. }
  2147. return (K_CHYBA);
  2148. }
  2149. /*
  2150. komplet = 0 - vsechno
  2151. --||--- = 1 - frame
  2152. --||--- = 2 - posun
  2153. #define ANIM_LOAD_ALL 0
  2154. #define ANIM_LOAD_FRAME 1
  2155. #define ANIM_LOAD_POSUN 2
  2156. */
  2157. #define FFF_FILE 'F' // |F |
  2158. #define FFF_TIME 'T'
  2159. #define FFF_EVENT 'E'
  2160. #define FFF_ALFA 'A'
  2161. #define FFF_POSUN 'P'
  2162. void lo_vyrob_animaci_list(EDIT_MATERIAL * p_mat, char *p_list,
  2163. EDIT_TEXT * p_text, int max, char komplet, char *p_dir)
  2164. {
  2165. ANIM_MATERIAL *p_amat = &p_mat->anim;
  2166. FILE *f;
  2167. char line[200]; // max delka radku
  2168. char *p_pom, *p_next;
  2169. int linenum;
  2170. int first_text = 1, i, t, akt, pos, alfa, a;
  2171. if (chdir(p_dir)) {
  2172. ddw("Cannot change directory to %s", p_dir);
  2173. assert(0);
  2174. return;
  2175. }
  2176. if ((f = fopen(p_list, "r")) == NULL) {
  2177. ddw("Nemuzu otevrit soubor %s", p_list);
  2178. assert(0);
  2179. return;
  2180. }
  2181. lo_zrus_animaci(p_amat);
  2182. // Prekopiruj list
  2183. strcpy(p_amat->jmeno, p_list);
  2184. if (fgets(line, 200, f) == NULL) {
  2185. ddw("Cannot read %s", p_list);
  2186. assert(0);
  2187. return;
  2188. }
  2189. // nahraj informace o frame animaci
  2190. if (komplet == ANIM_LOAD_ALL || komplet == ANIM_LOAD_FRAME) {
  2191. sscanf(line, "FRAME %d %d %d", &akt, &p_amat->frameakt, &alfa);
  2192. if (akt) {
  2193. p_mat->flag =
  2194. akt ? p_mat->flag | MAT_ANIM_FRAME : p_mat->flag & ~MAT_ANIM_FRAME;
  2195. //p_mat->flag = alfa ? p_mat->flag|MAT_ALFA_FAKTOR : p_mat->flag&~MAT_ALFA_FAKTOR;
  2196. }
  2197. }
  2198. if (fgets(line, 200, f) == NULL) {
  2199. ddw("Cannot read %s", p_list);
  2200. assert(0);
  2201. return;
  2202. }
  2203. // nahraj informace o posunu
  2204. if (komplet == ANIM_LOAD_ALL || komplet == ANIM_LOAD_POSUN)
  2205. sscanf(line, "POSUN %d\n", &pos);
  2206. // pokud je pozadavek na load frame animace
  2207. if (komplet == ANIM_LOAD_ALL || komplet == ANIM_LOAD_FRAME) {
  2208. linenum = 0;
  2209. while (fgets(line, 200, f))
  2210. linenum++;
  2211. // ddw("%d radek animace",linenum);
  2212. strcpy(p_amat->jmeno, p_list);
  2213. p_amat->framenum = linenum;
  2214. p_amat->frameakt = 0;
  2215. p_amat->p_frame =
  2216. (ANIM_FRAME *) mmalloc(sizeof(ANIM_FRAME) * p_amat->framenum);
  2217. fseek(f, 0, SEEK_SET);
  2218. if (fgets(line, 200, f) == NULL || fgets(line, 200, f) == NULL) {
  2219. ddw("Cannot read %s", p_list);
  2220. assert(0);
  2221. return;
  2222. }
  2223. for (i = 0; i < p_amat->framenum; i++) {
  2224. // nacti jmeno souboru
  2225. if (fgets(line, 200, f) == NULL) {
  2226. ddw("Cannot read %s", p_list);
  2227. assert(0);
  2228. return;
  2229. }
  2230. if ((p_pom = strchr(line, '\n')))
  2231. *p_pom = 0;
  2232. p_amat->p_frame[i].file[0] = 0;
  2233. p_amat->p_frame[i].flag &= ~FRAME_TEXTURA;
  2234. p_pom = line;
  2235. while (p_pom && p_pom[0]) {
  2236. if ((p_next = strchr(p_pom, '|'))) {
  2237. *p_next++ = 0;
  2238. }
  2239. /* Format p_pom:
  2240. A ' '
  2241. p_next -> dalsi zaznam
  2242. */
  2243. // Load textury
  2244. a = tolower((int) p_pom[0]);
  2245. switch (a) {
  2246. case 'f': // je to texture file
  2247. // pridej do texture listu
  2248. t = lo_najdi_texturu(p_text, max, p_pom + 2, FALSE);
  2249. if (t == K_CHYBA) {
  2250. t = lo_najdi_volnou_texturu(p_text, max);
  2251. if (t == K_CHYBA)
  2252. chyba("Neni volna textura !");
  2253. else {
  2254. memset(p_text + t, 0, sizeof(p_text[t]));
  2255. strcpy(p_text[t].jmeno, p_pom + 2);
  2256. }
  2257. }
  2258. p_amat->p_frame[i].p_text = p_text + t;
  2259. strcpy(p_amat->p_frame[i].file, p_pom + 2);
  2260. p_amat->p_frame[i].flag |= FRAME_TEXTURA;
  2261. if (first_text) {
  2262. strcpy(p_mat->textfile[0], p_text[t].jmeno);
  2263. p_mat->p_text[0] = p_text + t;
  2264. p_mat->textflag[0] = 0;
  2265. first_text = FALSE;
  2266. }
  2267. break;
  2268. case 't': // time flag
  2269. p_amat->p_frame[i].time = ftoi(atof(p_pom + 2) * 1000);
  2270. break;
  2271. case 'e': // event flag
  2272. p_amat->p_frame[i].flag |= FRAME_EVENT;
  2273. break;
  2274. case 'a':
  2275. sscanf(p_pom + 2, "%x", &p_amat->p_frame[i].alfa);
  2276. p_amat->p_frame[i].flag |= FRAME_ALFA_FAKTOR;
  2277. break;
  2278. case 'p':
  2279. sscanf(p_pom + 2, "%f %f", &p_amat->p_frame[i].u,
  2280. &p_amat->p_frame[i].v);
  2281. p_amat->p_frame[i].flag |= FRAME_POSUN;
  2282. p_mat->flag |= MAT_POSUN2D;
  2283. break;
  2284. default:
  2285. ddw("Neznamy flag ! (%s)", p_pom);
  2286. break;
  2287. }
  2288. p_pom = p_next;
  2289. }
  2290. }
  2291. }
  2292. fclose(f);
  2293. }
  2294. /*
  2295. #define FFF_FILE 'F' // |F |
  2296. #define FFF_TIME 'T'
  2297. #define FFF_EVENT 'E'
  2298. #define FFF_ALFA 'A'
  2299. #define FFF_POSUN 'P'
  2300. #define FRAME_EVENT 0x1 // timto framem se nepokracuje (ceka se na udalost)
  2301. #define FRAME_ALFA_FAKTOR 0x2 // tento frame ma nastavit alfa-stage
  2302. #define FRAME_TEXTURA 0x4 // tento frame ma nastavit texturu
  2303. #define FRAME_POSUN 0x8 // posun texturu oproti originalu
  2304. */
  2305. void lo_uloz_animaci_list(EDIT_MATERIAL * p_mat, char *p_list, char *p_dir)
  2306. {
  2307. ANIM_MATERIAL *p_amat = &p_mat->anim;
  2308. int i, akt;
  2309. FILE *f;
  2310. if ((f = fopen(p_list, "w")) == NULL)
  2311. chybat(STC, "File %s", p_list);
  2312. akt = p_mat->flag & MAT_ANIM_FRAME;
  2313. fprintf(f, "FRAME %d %d %d\n", akt, p_amat->frameakt, 0);
  2314. fprintf(f, "POSUN %d\n", 0);
  2315. for (i = 0; i < p_amat->framenum; i++) {
  2316. /* Soubor
  2317. */
  2318. fputc(FFF_FILE, f);
  2319. fputc(' ', f);
  2320. fputs(p_amat->p_frame[i].file, f);
  2321. fputc('|', f);
  2322. /* Cas
  2323. */
  2324. fprintf(f, "T %.3f|", (float) (p_amat->p_frame[i].time) * 0.001f);
  2325. /* Event flag
  2326. */
  2327. if (p_amat->p_frame[i].flag & FRAME_EVENT) {
  2328. fputc(FFF_EVENT, f);
  2329. fputc('|', f);
  2330. }
  2331. /* Alfa flag
  2332. */
  2333. if (p_amat->p_frame[i].flag & FRAME_ALFA_FAKTOR) {
  2334. fprintf(f, "%c %x|", FFF_ALFA, p_amat->p_frame[i].alfa);
  2335. }
  2336. /* Posunovaci flag
  2337. */
  2338. if (p_amat->p_frame[i].flag & FRAME_POSUN) {
  2339. fprintf(f, "%c %f %f |", FFF_POSUN, p_amat->p_frame[i].u,
  2340. p_amat->p_frame[i].v);
  2341. }
  2342. fputc('\n', f);
  2343. }
  2344. fclose(f);
  2345. }
  2346. void lo_zrus_animaci(ANIM_MATERIAL * p_amat)
  2347. {
  2348. if (p_amat->p_frame)
  2349. free(p_amat->p_frame);
  2350. memset(p_amat, 0, sizeof(ANIM_MATERIAL));
  2351. }
  2352. /*
  2353. Poly-listy
  2354. */
  2355. EDIT_MESH_POLY *vyrob_poly(void)
  2356. {
  2357. return ((EDIT_MESH_POLY *) mmalloc(sizeof(EDIT_MESH_POLY)));
  2358. }
  2359. void zrus_vnitrek_poly(EDIT_MESH_POLY * p_poly)
  2360. {
  2361. int i;
  2362. if (poly_vertex_array_zrus)
  2363. poly_vertex_array_zrus(p_poly);
  2364. if (p_poly->p_ind) {
  2365. for (i = 0; i < (p_poly->facenum / 3); i++) {
  2366. bmp_zrus(&p_poly->p_ind[i].p_bmp);
  2367. bmp_zrus(&p_poly->p_ind[i].p_zal);
  2368. }
  2369. null_free((void **) &p_poly->p_ind);
  2370. }
  2371. null_free((void **) &p_poly->p_koord);
  2372. null_free((void **) &p_poly->p_light);
  2373. null_free((void **) &p_poly->p_lightnum);
  2374. null_free((void **) &p_poly->p_edlight);
  2375. }
  2376. void zrus_poly(EDIT_MESH_POLY ** p_poly)
  2377. {
  2378. zrus_vnitrek_poly(*p_poly);
  2379. free((*p_poly));
  2380. *p_poly = NULL;
  2381. }
  2382. BOD *lo_edit_to_poly_najdi_vetex(BOD * p_pozice, int bodu, OBJ_VERTEX * p_bod,
  2383. int *p_index)
  2384. {
  2385. int v;
  2386. for (v = 0; v < bodu; v++) {
  2387. if (p_pozice[v].x == p_bod->x &&
  2388. p_pozice[v].y == p_bod->y && p_pozice[v].z == p_bod->z) {
  2389. *p_index = v;
  2390. return (p_pozice);
  2391. }
  2392. }
  2393. return (NULL);
  2394. }
  2395. /*
  2396. Frci pouze na kontejnery 1:1 (1 kont s 1 objektem - poly ma jeden material)
  2397. */
  2398. EDIT_MESH_POLY *edit_to_poly_indir(EDIT_KONTEJNER * p_kont,
  2399. EDIT_MESH_POLY * p_poly, int kID)
  2400. {
  2401. OBJ_VERTEX *p_bod;
  2402. EDIT_OBJEKT *p_obj;
  2403. TEXT_KOORD *p_koord;
  2404. int f;
  2405. int spec = p_kont->m2flag & MAT2_SPECULAR;
  2406. if (!p_poly)
  2407. return (NULL);
  2408. if (p_kont->objektu != 1) {
  2409. ddw("Poly s %d objektama !", p_kont->objektu);
  2410. assert(0);
  2411. }
  2412. p_obj = p_kont->p_obj[0];
  2413. assert(p_obj);
  2414. memset((void *) p_poly, 0, sizeof(*p_poly));
  2415. strcpy(p_poly->jmeno, p_kont->jmeno);
  2416. kont_norm_vect(p_kont);
  2417. updatuj_kontejner_statistika(p_kont, FALSE); // pocty objektu a pod.
  2418. // koordinaty pro vsecny facy kontejneru
  2419. p_koord = (TEXT_KOORD *) mmalloc(sizeof(TEXT_KOORD) * p_obj->facenum);
  2420. // vyrobim list sousednosti
  2421. if (!p_obj->p_fsous)
  2422. obj_vyrob_list_sousednosti(p_obj);
  2423. p_poly->p_fsous =
  2424. (dword *) kopiruj_pole(p_obj->p_fsous,
  2425. sizeof(p_obj->p_fsous[0]) * p_obj->facenum);
  2426. p_poly->material = p_obj->material;
  2427. // prekopiruj plosky
  2428. for (f = 0; f < p_obj->facenum; f++) {
  2429. p_bod = p_obj->p_vertex + p_obj->p_face[f];
  2430. p_koord[f].x = p_bod->x;
  2431. p_koord[f].y = p_bod->y;
  2432. p_koord[f].z = p_bod->z;
  2433. p_koord[f].tu1 = p_bod->tu1;
  2434. p_koord[f].tv1 = p_bod->tv1;
  2435. p_koord[f].tu2 = p_bod->tu2;
  2436. p_koord[f].tv2 = p_bod->tv2;
  2437. p_koord[f].tu3 = p_bod->tu3;
  2438. p_koord[f].tv3 = p_bod->tv3;
  2439. p_koord[f].tu4 = p_bod->tu4;
  2440. p_koord[f].tv4 = p_bod->tv4;
  2441. p_koord[f].nx = p_bod->nx;
  2442. p_koord[f].ny = p_bod->ny;
  2443. p_koord[f].nz = p_bod->nz;
  2444. p_koord[f].dr = p_bod->dr;
  2445. p_koord[f].dg = p_bod->dg;
  2446. p_koord[f].db = p_bod->db;
  2447. p_koord[f].da = p_bod->da;
  2448. p_koord[f].mdr = p_bod->mdr;
  2449. p_koord[f].mdg = p_bod->mdg;
  2450. p_koord[f].mdb = p_bod->mdb;
  2451. p_koord[f].mda = p_bod->mda;
  2452. if (spec) {
  2453. p_koord[f].sr = p_bod->sr;
  2454. p_koord[f].sg = p_bod->sg;
  2455. p_koord[f].sb = p_bod->sb;
  2456. p_koord[f].msr = p_bod->msr;
  2457. p_koord[f].msg = p_bod->msg;
  2458. p_koord[f].msb = p_bod->msb;
  2459. }
  2460. else {
  2461. p_koord[f].msr = p_koord[f].sr = 0.0f;
  2462. p_koord[f].msg = p_koord[f].sg = 0.0f;
  2463. p_koord[f].msb = p_koord[f].sb = 0.0f;
  2464. }
  2465. }
  2466. p_poly->p_koord = p_koord;
  2467. p_poly->facenum = p_kont->facu;
  2468. p_poly->kflag = p_kont->kflag;
  2469. p_poly->k2flag = p_kont->k2flag;
  2470. p_poly->m1flag = p_kont->m1flag;
  2471. p_poly->m2flag = p_kont->m2flag;
  2472. p_poly->p_mlha = p_kont->p_mlha;
  2473. if (p_kont->p_mlha) {
  2474. p_kont->p_mlha->poly = kID;
  2475. p_kont->p_mlha->p_kont = NULL;
  2476. p_kont->p_mlha = NULL;
  2477. }
  2478. p_poly->kreslit = TRUE;
  2479. obb_calc_poly(p_poly);
  2480. return (p_poly);
  2481. }
  2482. void lo_poly_flaguj_materialy(EDIT_MESH_POLY * p_poly, EDIT_MATERIAL ** p_mat)
  2483. {
  2484. int mat;
  2485. p_poly->m1flag |= p_mat[p_poly->material]->flag;
  2486. p_poly->m2flag |= p_mat[p_poly->material]->flag2;
  2487. mat = p_poly->material;
  2488. if (alfa_stage_edit_blok[p_mat[mat]->alfa_state].alfa_pruhledny) {
  2489. p_poly->m1flag |= MAT_PRUHLEDNY;
  2490. }
  2491. else {
  2492. p_poly->m1flag &= ~MAT_PRUHLEDNY;
  2493. }
  2494. if (p_mat[mat]->alfa_state == STAGE_ALFA_MASKA) {
  2495. p_poly->m2flag |= MAT2_MASKA;
  2496. }
  2497. else {
  2498. p_poly->m2flag &= ~MAT2_MASKA;
  2499. }
  2500. }
  2501. void lo_poly_oznac_zrcadlo(EDIT_MESH_POLY * p_poly)
  2502. {
  2503. p_poly->kflag |= KONT_ZRCADLO;
  2504. }
  2505. void edit_mesh_poly_from_disk(EDIT_MESH_POLY_DISK *src, EDIT_MESH_POLY *desc)
  2506. {
  2507. memcpy(desc->jmeno, src->jmeno, sizeof(desc->jmeno));
  2508. desc->facenum = src->facenum;
  2509. desc->m1flag = src->m1flag;
  2510. desc->m2flag = src->m2flag;
  2511. desc->kflag = src->kflag;
  2512. desc->k2flag = src->k2flag;
  2513. desc->material = src->material;
  2514. desc->kont = src->kont;
  2515. desc->poly = src->poly;
  2516. desc->lightnum = src->lightnum;
  2517. desc->kreslit = src->kreslit;
  2518. desc->mail = src->mail;
  2519. desc->obb = src->obb;
  2520. desc->lightmax = src->lightmax;
  2521. desc->lightakt = src->lightakt;
  2522. desc->top_edlight = src->top_edlight;
  2523. desc->edlightmax = src->edlightmax;
  2524. desc->edlightakt = src->edlightakt;
  2525. desc->varray = src->varray;
  2526. }
  2527. FFILE lo_poly_file_vyrob(char *p_file, int filenum, int velikost)
  2528. {
  2529. FFILE f;
  2530. if (!(f = ffopen(p_file, "wb"))) {
  2531. assert(0);
  2532. kerror(1,"Unable to open polyfile for writting (%s)",p_file);
  2533. }
  2534. ffwrite(&filenum, sizeof(int), 1, f);
  2535. ffwrite(&velikost, sizeof(int), 1, f);
  2536. return (f);
  2537. }
  2538. FFILE lo_poly_file_otevri(char *p_file, int *p_filenum, int velikost)
  2539. {
  2540. FFILE f;
  2541. int vel;
  2542. if (!(f = ffopen(p_file, "rb"))) {
  2543. pperror(1, "Unable to open file %s", p_file);
  2544. }
  2545. ffread(p_filenum, sizeof(int), 1, f);
  2546. ffread(&vel, sizeof(int), 1, f);
  2547. if (vel != velikost) {
  2548. pperror(1, "Wrong poly version (%s)! Save it again.", p_file);
  2549. ffclose(f);
  2550. return (NULL);
  2551. }
  2552. return (f);
  2553. }
  2554. void lo_poly_file_zavri(FFILE f)
  2555. {
  2556. ffclose(f);
  2557. }
  2558. void lo_poly_uloz(FFILE f, EDIT_MESH_POLY * p_poly, EDIT_TEXT * p_light)
  2559. {
  2560. int i;
  2561. ffwrite(p_poly, sizeof(p_poly[0]), 1, f);
  2562. ffwrite(p_poly->p_koord, sizeof(p_poly->p_koord[0]), p_poly->facenum, f);
  2563. for (i = 0; i < p_poly->lightnum; i++) {
  2564. p_poly->p_light[i] = (EDIT_TEXT *) (p_poly->p_light[i] - p_light);
  2565. }
  2566. // TODO - 64bit
  2567. ffwrite(p_poly->p_light, sizeof(p_poly->p_light[0]), p_poly->lightnum, f);
  2568. ffwrite(p_poly->p_lightnum, sizeof(p_poly->p_lightnum[0]), p_poly->lightnum, f);
  2569. for (i = 0; i < p_poly->lightnum; i++) {
  2570. p_poly->p_light[i] = p_light + reinterpret_cast<size_ptr>(p_poly->p_light[i]);
  2571. }
  2572. }
  2573. void lo_poly_nahraj_indir(FFILE f, EDIT_MESH_POLY * p_poly,
  2574. EDIT_TEXT * p_light)
  2575. {
  2576. EDIT_MESH_POLY_DISK poly_disk;
  2577. int i;
  2578. ffread(&poly_disk, sizeof(poly_disk), 1, f);
  2579. edit_mesh_poly_from_disk(&poly_disk, p_poly);
  2580. p_poly->mail = 0;
  2581. p_poly->p_koord = (TEXT_KOORD *) mmalloc(sizeof(p_poly->p_koord[0])*p_poly->facenum);
  2582. ffread(p_poly->p_koord, sizeof(p_poly->p_koord[0]), p_poly->facenum, f);
  2583. p_poly->p_light = (EDIT_TEXT **) mmalloc(sizeof(p_poly->p_light[0])*p_poly->lightnum);
  2584. p_poly->p_lightnum = (int *) mmalloc(sizeof(p_poly->p_lightnum[0])*p_poly->lightnum);
  2585. int *p_tmp = (int *)mmalloc(sizeof(int)*p_poly->lightnum);
  2586. ffread(p_tmp, sizeof(int), p_poly->lightnum, f);
  2587. ffread(p_poly->p_lightnum, sizeof(p_poly->p_lightnum[0]), p_poly->lightnum, f);
  2588. for (i = 0; i < p_poly->lightnum; i++) {
  2589. p_poly->p_light[i] = p_light + p_tmp[i];
  2590. }
  2591. ffree(p_tmp);
  2592. }
  2593. void load_poly_check(void)
  2594. {
  2595. assert(sizeof(EDIT_MESH_POLY_DISK) == 372);
  2596. assert(sizeof(TEXT_KOORD) == 120);
  2597. assert(sizeof(EDIT_TEXT_DISK) == 124);
  2598. }
  2599. EDIT_MESH_POLY *lo_nahraj_poly_list(char *p_file, int *p_polynum,
  2600. EDIT_TEXT * p_light, EDIT_MATERIAL ** p_mat,
  2601. int matnum)
  2602. {
  2603. EDIT_MESH_POLY *p_poly;
  2604. char material[50];
  2605. int i, filenum, mat, mn;
  2606. FFILE f;
  2607. load_poly_check();
  2608. if (!(f = lo_poly_file_otevri(p_file, &filenum, sizeof(EDIT_MESH_POLY_DISK)))) {
  2609. *p_polynum = 0;
  2610. return (NULL);
  2611. }
  2612. if (!filenum) {
  2613. *p_polynum = 0;
  2614. return (NULL);
  2615. }
  2616. if (!(p_poly = (EDIT_MESH_POLY *) mmalloc(sizeof(p_poly[0]) * filenum))) {
  2617. assert(0);
  2618. chyba("Pamet!");
  2619. }
  2620. for (i = 0; i < filenum; i++) {
  2621. kprintf(TRUE,"Loading poly %d...",i);
  2622. ffread(material, sizeof(char), 50, f);
  2623. lo_poly_nahraj_indir(f, p_poly + i, p_light);
  2624. if (p_poly[i].m1flag & MAT_ANIM_FRAME) {
  2625. int ret;
  2626. // animovany material u poly-listu skopiruju
  2627. mat = lo_najdi_material(p_mat, matnum, material);
  2628. assert(mat != K_CHYBA);
  2629. p_poly[i].material = mn = lo_najdi_prazdny_material(p_mat, matnum);
  2630. assert(mn != K_CHYBA);
  2631. p_mat[mn] = kopiruj_material(p_mat[mat]);
  2632. ret = snprintf(p_mat[mn]->jmeno, sizeof(p_mat[mn]->jmeno),
  2633. "%s_ak%d", p_mat[mat]->jmeno, mn);
  2634. assert(ret < (int) sizeof(p_mat[mn]->jmeno));
  2635. }
  2636. else {
  2637. p_poly[i].material = lo_najdi_material(p_mat, matnum, material);
  2638. }
  2639. assert(p_poly[i].material != K_CHYBA);
  2640. }
  2641. lo_poly_file_zavri(f);
  2642. *p_polynum = filenum;
  2643. for (i = 0; i < filenum; i++)
  2644. p_poly[i].kflag |= KONT_UPLOAD;
  2645. return (p_poly);
  2646. }
  2647. void lo_uloz_poly_list(char *p_file, EDIT_MESH_POLY * p_poly, int polynum,
  2648. EDIT_TEXT * p_light, EDIT_MATERIAL ** p_mat, int matnum)
  2649. {
  2650. char material[50];
  2651. FFILE f;
  2652. int i;
  2653. f = lo_poly_file_vyrob(p_file, polynum, sizeof(p_poly[0]));
  2654. for (i = 0; i < polynum; i++) {
  2655. assert(p_poly[i].material < matnum && p_mat[p_poly[i].material]);
  2656. strcpy(material, p_mat[p_poly[i].material]->jmeno);
  2657. ffwrite(material, sizeof(char), 50, f);
  2658. lo_poly_uloz(f, p_poly + i, p_light);
  2659. }
  2660. lo_poly_file_zavri(f);
  2661. }
  2662. void lo_poly_calc_lightmap_face(EDIT_MESH_POLY * p_poly)
  2663. {
  2664. LIGHTMAP_FACE *p_lf;
  2665. int facu = p_poly->facenum / 3;
  2666. int ind;
  2667. int l, i, last = 0;
  2668. int s, u, v, t;
  2669. BOD prus;
  2670. float bmp_u, bmp_v;
  2671. BOD vu, vv; // smerove vektory x,y
  2672. p_poly->p_ind = (LIGHTMAP_FACE *) mmalloc(sizeof(p_poly->p_ind[0]) * facu);
  2673. for (l = 0; l < p_poly->lightnum; l++) {
  2674. for (i = 0; i < p_poly->p_lightnum[l]; i += 3) {
  2675. ind = (last + i) / 3;
  2676. p_lf = p_poly->p_ind + ind;
  2677. s = last + i;
  2678. u = last + i + 1;
  2679. v = last + i + 2;
  2680. // Setrideni podle u
  2681. if (p_poly->p_koord[s].tul > p_poly->p_koord[u].tul) {
  2682. t = s;
  2683. s = u;
  2684. u = t;
  2685. if (p_poly->p_koord[s].tul > p_poly->p_koord[v].tul) {
  2686. t = s;
  2687. s = v;
  2688. v = t;
  2689. }
  2690. }
  2691. else {
  2692. if (p_poly->p_koord[v].tul > p_poly->p_koord[u].tul) {
  2693. t = u;
  2694. u = v;
  2695. v = t;
  2696. }
  2697. }
  2698. // Setrideni podle v
  2699. if (p_poly->p_koord[s].tvl > p_poly->p_koord[u].tvl) {
  2700. t = s;
  2701. s = u;
  2702. u = t;
  2703. if (p_poly->p_koord[s].tvl > p_poly->p_koord[v].tvl) {
  2704. t = s;
  2705. s = v;
  2706. v = t;
  2707. }
  2708. }
  2709. else {
  2710. if (p_poly->p_koord[v].tvl < p_poly->p_koord[u].tvl) {
  2711. t = u;
  2712. u = v;
  2713. v = t;
  2714. }
  2715. }
  2716. p_lf->last_u = 0;
  2717. // Ted to je OK, s-u-v
  2718. p_lf->s0.x = p_poly->p_koord[s].x;
  2719. p_lf->s0.y = p_poly->p_koord[s].y;
  2720. p_lf->s0.z = p_poly->p_koord[s].z;
  2721. vektor_sub((BOD *) (p_poly->p_koord + u), (BOD *) (p_poly->p_koord + s), &vu);
  2722. prusecik((BOD *) (p_poly->p_koord + s), (BOD *) (p_poly->p_koord + u), (BOD *) (p_poly->p_koord + v), &prus);
  2723. vektor_sub((BOD *) (p_poly->p_koord + v), &prus, &vv);
  2724. calc_rovinu_bod((BOD *) (p_poly->p_koord + s), (BOD *) (p_poly->p_koord + s + 2), (BOD *) (p_poly->p_koord + s + 1), &p_poly->p_ind[ind].n);
  2725. norm_rovinu(&p_poly->p_ind[ind].n);
  2726. calc_rovinu_bod_vektor((BOD *) (p_poly->p_koord + s), &vu, &p_poly->p_ind[ind].rv);
  2727. norm_rovinu(&p_poly->p_ind[ind].rv);
  2728. calc_rovinu_bod_vektor((BOD *) (p_poly->p_koord + s), &vv, &p_poly->p_ind[ind].ru);
  2729. norm_rovinu(&p_poly->p_ind[ind].ru);
  2730. p_poly->p_ind[ind].ddu = vektor_velikost(&vu);
  2731. p_poly->p_ind[ind].ddv = vektor_velikost(&vv);
  2732. vektor_add(&p_lf->s0, &vv, &p_lf->s1);
  2733. vektor_add(&p_lf->s0, &vu, &p_lf->s2);
  2734. vektor_add(&p_lf->s2, &vv, &p_lf->s3);
  2735. if (p_poly->p_light[l]->p_bmp) {
  2736. bmp_u = (float) p_poly->p_light[l]->p_bmp->x;
  2737. bmp_v = (float) p_poly->p_light[l]->p_bmp->y;
  2738. p_poly->p_ind[ind].u = ftoi((float) floor(p_poly->p_koord[s].tul * bmp_u));
  2739. p_poly->p_ind[ind].v = ftoi((float) floor(p_poly->p_koord[s].tvl * bmp_v));
  2740. p_poly->p_ind[ind].nu = ftoi((float) ceil(p_poly->p_koord[u].tul * bmp_u));
  2741. p_poly->p_ind[ind].nv = ftoi((float) ceil(p_poly->p_koord[v].tvl * bmp_v));
  2742. p_poly->p_ind[ind].nu -= p_poly->p_ind[ind].u;
  2743. p_poly->p_ind[ind].nv -= p_poly->p_ind[ind].v;
  2744. p_poly->p_ind[ind].p_bmp = bmp_vyrob(p_poly->p_ind[ind].nu + 2, p_poly->p_ind[ind].nv + 2);
  2745. p_poly->p_ind[ind].p_zal = bmp_vyrob(p_poly->p_ind[ind].nu + 2, p_poly->p_ind[ind].nv + 2);
  2746. // MSS_SET_BLOCK_LABEL(p_poly->p_ind[ind].p_bmp, "lo_poly_calc_lightmap_face1");
  2747. // MSS_SET_BLOCK_LABEL(p_poly->p_ind[ind].p_zal, "lo_poly_calc_lightmap_face2");
  2748. bmp_vyber_rec(p_poly->p_light[l]->p_bmp,
  2749. p_poly->p_ind[ind].p_bmp,
  2750. p_poly->p_ind[ind].u - 1,
  2751. p_poly->p_ind[ind].v - 1);
  2752. bmp_kopiruj(p_poly->p_ind[ind].p_bmp, p_poly->p_ind[ind].p_zal);
  2753. }
  2754. else {
  2755. kprintf(1, "lo_poly_calc_lightmap_face -> p_bmp = NULL!");
  2756. }
  2757. }
  2758. last += p_poly->p_lightnum[l];
  2759. }
  2760. }
  2761. /* **********************************************************************
  2762. Load/save kont/materialy
  2763. */
  2764. /* Uklada utomaticky do *.b2m
  2765. */
  2766. FFILE lo_uloz_kontejner(EDIT_MATERIAL ** p_mat, int max_mat,
  2767. EDIT_KONTEJNER * p_kont_top, char *p_jmeno, int file, FFILE f)
  2768. {
  2769. EDIT_KONTEJNER *p_kont;
  2770. if (!f || !file) {
  2771. if ((f = ffopen(p_jmeno, "wb")) == NULL) {
  2772. return (NULL);
  2773. }
  2774. }
  2775. lo_uloz_kontejner_chunk(f, p_mat, max_mat, p_kont_top, FALSE);
  2776. p_kont = p_kont_top->p_next;
  2777. while (p_kont) {
  2778. lo_uloz_kontejner_chunk(f, p_mat, max_mat, p_kont, TRUE);
  2779. p_kont = p_kont->p_next;
  2780. }
  2781. if (!file) {
  2782. ffclose(f);
  2783. return (NULL);
  2784. }
  2785. else {
  2786. return (f);
  2787. }
  2788. }
  2789. /*
  2790. Nacti b2m (OGL) a out meshe
  2791. */
  2792. EDIT_KONTEJNER *lo_nahraj_kontejner(EDIT_MATERIAL ** p_mat, int max_mat,
  2793. EDIT_TEXT * p_text, int max_text, char *p_jmeno, int mat)
  2794. {
  2795. if (!strcmp(cti_koncovku(p_jmeno), KONCOVKA_MESH_OLD)) {
  2796. return (lo_nahraj_kontejner_out(p_mat, max_mat, p_text, max_text, p_jmeno,
  2797. mat));
  2798. }
  2799. else {
  2800. return (lo_nahraj_kontejner_chunk(p_mat, max_mat, p_text, max_text,
  2801. p_jmeno, mat, FALSE));
  2802. }
  2803. }
  2804. /* Nahraje b2t nebo mnt materialy
  2805. */
  2806. int lo_nahraj_materialy(EDIT_MATERIAL ** p_mat, int max_mat,
  2807. EDIT_TEXT * p_text, int max_text, char *p_file)
  2808. {
  2809. if (!strcmp(cti_koncovku(p_file), KONCOVKA_MATERIAL_OLD)) {
  2810. return (lo_nahraj_materialy_out_jmeno(p_mat, max_mat, p_text, max_text,
  2811. p_file, TRUE));
  2812. }
  2813. else {
  2814. return (lo_nahraj_materialy_chunk(p_mat, max_mat, p_text, max_text,
  2815. p_file, TRUE));
  2816. }
  2817. }
  2818. /*
  2819. Ulozi vsechny materialy vcetne animaci - pouze jako chunk
  2820. */
  2821. int lo_uloz_materialy(EDIT_MATERIAL ** p_mat, int max_mat, char *p_file,
  2822. char *p_dir)
  2823. {
  2824. char file[500];
  2825. FFILE f;
  2826. int i;
  2827. strcpy(file, p_file);
  2828. zamen_koncovku(file, KONCOVKA_MATERIAL);
  2829. if (chdir(p_dir) || (f = ffopen(file, "wb")) == NULL)
  2830. return (FALSE);
  2831. for (i = 0; i < max_mat; i++) {
  2832. if (p_mat[i])
  2833. lo_uloz_material_chunk(f, p_mat[i]);
  2834. }
  2835. ffclose(f);
  2836. return (TRUE);
  2837. }
  2838. int lo_uloz_material(EDIT_MATERIAL * p_mat, char *p_file, char *p_dir)
  2839. {
  2840. char file[500];
  2841. FFILE f;
  2842. strcpy(file, p_file);
  2843. zamen_koncovku(file, KONCOVKA_MATERIAL);
  2844. if (chdir (p_dir) || (f = ffopen(file, "wb")) == NULL)
  2845. return (FALSE);
  2846. lo_uloz_material_chunk(f, p_mat);
  2847. ffclose(f);
  2848. return (TRUE);
  2849. }
  2850. /*
  2851. ulozi pouzite materily vcetne animaci
  2852. */
  2853. int lo_uloz_materialy_pouzite(EDIT_MATERIAL ** p_mat, int max_mat,
  2854. char *p_file, char *p_dir)
  2855. {
  2856. char file[200];
  2857. FFILE f;
  2858. int i;
  2859. strcpy(file, p_file);
  2860. zamen_koncovku(file, KONCOVKA_MATERIAL);
  2861. if (chdir(p_dir) || (f = ffopen(file, "wb")) == NULL)
  2862. return (FALSE);
  2863. for (i = 0; i < max_mat; i++) {
  2864. if (p_mat[i] && p_mat[i]->flag & (MAT_POUZITY | MAT_SYSTEM)) {
  2865. lo_uloz_material_chunk(f, p_mat[i]);
  2866. }
  2867. }
  2868. ffclose(f);
  2869. return (i);
  2870. }
  2871. /*
  2872. Mesi sekce
  2873. */
  2874. GAME_MESH_OLD *lo_nahraj_mesh(EDIT_MATERIAL ** p_mat, int max_mat,
  2875. EDIT_TEXT * p_text, int max_text,
  2876. char *p_file, int mat, int extra_light,
  2877. int json_export)
  2878. {
  2879. EDIT_KONTEJNER *p_kont_top, *p_kont;
  2880. GAME_MESH_OLD *p_mesh_top = NULL, *p_mesh = NULL;
  2881. p_kont = p_kont_top = lo_nahraj_kontejner(p_mat, max_mat, p_text, max_text, p_file, mat);
  2882. if(json_export) {
  2883. json_export_kont_single(p_kont, p_mat, max_mat);
  2884. }
  2885. if (p_kont && p_kont->bodu && p_kont->objektu) {
  2886. while (p_kont) {
  2887. if (extra_light)
  2888. kont_extra_light(p_kont);
  2889. lo_setrid_kontejner_materialy(p_kont);
  2890. if (p_mesh) {
  2891. p_mesh = (p_mesh->p_next = edit_to_mesh(p_mesh_top->p_data, p_kont, p_mat, max_mat, FALSE));
  2892. }
  2893. else {
  2894. p_mesh_top = p_mesh = edit_to_mesh(NULL, p_kont, p_mat, max_mat, FALSE);
  2895. p_mesh->p_data->p_mesh = p_mesh;
  2896. }
  2897. p_kont = p_kont->p_next;
  2898. }
  2899. }
  2900. else {
  2901. p_mesh_top = NULL;
  2902. }
  2903. zrus_kontejner_rec(&p_kont_top, NULL);
  2904. return (p_mesh_top);
  2905. }
  2906. /* Kontejner to mesh
  2907. */
  2908. GAME_MESH_OLD *lo_kontejner_to_mesh(EDIT_KONTEJNER ** p_kont_top,
  2909. EDIT_MATERIAL ** p_mat, int max_mat, int extra_light)
  2910. {
  2911. EDIT_KONTEJNER *p_kont;
  2912. GAME_MESH_OLD *p_mesh_top = NULL, *p_mesh = NULL;
  2913. p_kont = *p_kont_top;
  2914. if (p_kont->bodu && p_kont->objektu) {
  2915. while (p_kont) {
  2916. if (extra_light)
  2917. kont_extra_light(p_kont);
  2918. lo_setrid_kontejner_materialy(p_kont);
  2919. updatuj_kontejner_statistika(p_kont, FALSE);
  2920. if (p_mesh) {
  2921. p_mesh = (p_mesh->p_next =
  2922. edit_to_mesh(p_mesh_top->p_data, p_kont, p_mat, max_mat, FALSE));
  2923. }
  2924. else {
  2925. p_mesh_top = p_mesh =
  2926. edit_to_mesh(NULL, p_kont, p_mat, max_mat, FALSE);
  2927. p_mesh->p_data->p_mesh = p_mesh;
  2928. }
  2929. p_kont = p_kont->p_next;
  2930. }
  2931. }
  2932. else {
  2933. p_mesh_top = NULL;
  2934. }
  2935. zrus_kontejner_rec(p_kont_top, NULL);
  2936. return (p_mesh_top);
  2937. }
  2938. // Prevede kontejner tak, aby jeho objekty meli jedinecne jmena
  2939. void lo_kontejner_jedinecne_jmena(EDIT_KONTEJNER * p_kont, int ds3)
  2940. {
  2941. char pom[200];
  2942. int i, j, max = 0;
  2943. if (ds3) {
  2944. for (i = 0; i < p_kont->max_objektu; i++) {
  2945. if (p_kont->p_obj[i]) {
  2946. sprintf(p_kont->p_obj[i]->jmeno, "Obj_%d", i);
  2947. }
  2948. }
  2949. }
  2950. else {
  2951. for (i = 0; i < p_kont->max_objektu; i++) {
  2952. if (p_kont->p_obj[i]) {
  2953. max = i + 1;
  2954. }
  2955. }
  2956. for (i = 0; i < max; i++) {
  2957. if (!p_kont->p_obj[i])
  2958. continue;
  2959. for (j = i + 1; j < max; j++) {
  2960. if (!p_kont->p_obj[j])
  2961. continue;
  2962. if (!strcmp(p_kont->p_obj[i]->jmeno, p_kont->p_obj[j]->jmeno)) {
  2963. strcat(p_kont->p_obj[i]->jmeno, "#");
  2964. strcat(p_kont->p_obj[i]->jmeno, itoa(i, pom, 10));
  2965. assert(strlen(p_kont->p_obj[i]->jmeno) < MAX_JMENO);
  2966. break; // koncim a modlim se
  2967. }
  2968. }
  2969. }
  2970. }
  2971. }
  2972. void lo_kontlist_jedinecne_jmena(EDIT_KONTEJNER ** p_kont, int listnum)
  2973. {
  2974. char pom[200];
  2975. int i, j, max = 0;
  2976. // Zjistim posledni objekt
  2977. for (i = 0; i < listnum; i++) {
  2978. if (p_kont[i])
  2979. max = i + 1;
  2980. }
  2981. for (i = 0; i < max; i++) {
  2982. if (!p_kont[i])
  2983. continue;
  2984. for (j = i + 1; j < max; j++) {
  2985. if (!p_kont[j])
  2986. continue;
  2987. if (!strcmp(p_kont[i]->jmeno, p_kont[j]->jmeno)) {
  2988. strcat(p_kont[i]->jmeno, "#");
  2989. strcat(p_kont[i]->jmeno, itoa(i, pom, 10));
  2990. assert(strlen(p_kont[i]->jmeno) < MAX_JMENO);
  2991. break; // koncim a modlim se
  2992. }
  2993. }
  2994. }
  2995. }
  2996. int lo_najdi_volne_stat_svetlo(STATIC_LIGHT * p_light, int max)
  2997. {
  2998. int i;
  2999. for (i = 0; i < max; i++) {
  3000. if (!p_light[i].akt)
  3001. return (i);
  3002. }
  3003. return (K_CHYBA);
  3004. }
  3005. int lo_najdi_plne_stat_svetlo(STATIC_LIGHT * p_light, int max)
  3006. {
  3007. int i;
  3008. for (i = 0; i < max; i++) {
  3009. if (p_light[i].akt)
  3010. return (i);
  3011. }
  3012. return (K_CHYBA);
  3013. }
  3014. int lo_najdi_volne_dsvetlo(DYN_LIGHT * p_dlight, int max)
  3015. {
  3016. int i;
  3017. for (i = 0; i < max; i++) {
  3018. if (!p_dlight[i].akt)
  3019. return (i);
  3020. }
  3021. return (K_CHYBA);
  3022. }
  3023. int lo_najdi_plne_dsvetlo(DYN_LIGHT * p_dlight, int max)
  3024. {
  3025. int i;
  3026. for (i = 0; i < max; i++) {
  3027. if (p_dlight[i].akt)
  3028. return (i);
  3029. }
  3030. return (K_CHYBA);
  3031. }
  3032. DYN_LIGHT *lo_najdi_volne_dsvetlo_point(DYN_LIGHT * p_dlight, int max)
  3033. {
  3034. int i;
  3035. for (i = 0; i < max; i++) {
  3036. if (!p_dlight[i].akt)
  3037. return (p_dlight + i);
  3038. }
  3039. return (NULL);
  3040. }
  3041. int lo_najdi_prepocitej_dsvetlo(DYN_LIGHT * p_dlight, int max)
  3042. {
  3043. int i, dl_lightnum = -1;
  3044. for (i = 0; i < MAX_FLARE_SVETEL; i++) {
  3045. if (p_dlight[i].akt && i > dl_lightnum) {
  3046. dl_lightnum = i;
  3047. }
  3048. }
  3049. return (dl_lightnum + 1);
  3050. }
  3051. /*
  3052. Extra-svetla
  3053. */
  3054. int lo_najdi_prepocitej_extra_svetlo(EXTRA_DYN_LIGHT * p_dlight, int max)
  3055. {
  3056. int i, dl_lightnum = -1;
  3057. for (i = 0; i < MAX_FLARE_SVETEL; i++) {
  3058. if (p_dlight[i].akt && i > dl_lightnum) {
  3059. dl_lightnum = i;
  3060. }
  3061. }
  3062. return (dl_lightnum + 1);
  3063. }
  3064. int lo_najdi_volne_extra_svetlo(EXTRA_DYN_LIGHT * p_dlight, int max)
  3065. {
  3066. int i;
  3067. for (i = 0; i < max; i++) {
  3068. if (!p_dlight[i].akt)
  3069. return (i);
  3070. }
  3071. return (K_CHYBA);
  3072. }
  3073. int lo_najdi_plne_extra_svetlo(EXTRA_DYN_LIGHT * p_dlight, int max)
  3074. {
  3075. int i;
  3076. for (i = 0; i < max; i++) {
  3077. if (p_dlight[i].akt)
  3078. return (i);
  3079. }
  3080. return (K_CHYBA);
  3081. }
  3082. EXTRA_DYN_LIGHT *lo_najdi_volne_extra_svetlo_point(EXTRA_DYN_LIGHT * p_dlight,
  3083. int max)
  3084. {
  3085. int i;
  3086. for (i = 0; i < max; i++) {
  3087. if (!p_dlight[i].akt)
  3088. return (p_dlight + i);
  3089. }
  3090. return (NULL);
  3091. }
  3092. void lo_preved_flare_do_sceny(STATIC_LIGHT * p_light,
  3093. LENS_FLARE * p_flarelist, int flaremax)
  3094. {
  3095. LENS_FLARE *p_flare;
  3096. while (p_light) {
  3097. if (p_light->p_flare) {
  3098. p_flare =
  3099. lo_kopiruj_flare(p_flarelist, flaremax,
  3100. (LENS_FLARE *) p_light->p_flare);
  3101. free(p_light->p_flare);
  3102. p_light->p_flare = p_flare;
  3103. p_flare->p_svetlo = p_light;
  3104. }
  3105. p_light = p_light->p_next;
  3106. }
  3107. }
  3108. void lo_preved_svetla_do_sceny(EDIT_KONTEJNER * p_kont,
  3109. STATIC_LIGHT * p_light, int lightnum, DYN_LIGHT * p_dlist, int dlistnum,
  3110. EXTRA_DYN_LIGHT * p_elist, int elistnum)
  3111. {
  3112. STATIC_LIGHT *p_slight, *p_next;
  3113. DYN_LIGHT *p_dlight, *p_dnext;
  3114. EXTRA_DYN_LIGHT *p_elight, *p_enext;
  3115. int l, p;
  3116. while (p_kont) {
  3117. /* Prevedu staticky svetla
  3118. */
  3119. if (p_light) {
  3120. p_slight = p_kont->p_slight;
  3121. if (p_slight) {
  3122. l = lo_najdi_volne_stat_svetlo(p_light, lightnum);
  3123. if (l != K_CHYBA) {
  3124. p_light[l] = *p_slight;
  3125. if (p_light[l].p_flare)
  3126. ((LENS_FLARE *) (p_light[l].p_flare))->p_svetlo = p_light + l;
  3127. p_light[l].index = l;
  3128. p_light[l].p_mesh_data = p_kont;
  3129. p_light[l].p_prev = NULL;
  3130. p_light[l].p_next = NULL;
  3131. p_kont->p_slight = p_light + l;
  3132. p = l;
  3133. p_next = p_slight->p_next;
  3134. free(p_slight);
  3135. p_slight = p_next;
  3136. while (p_slight) {
  3137. l = lo_najdi_volne_stat_svetlo(p_light, lightnum);
  3138. if (l != K_CHYBA) {
  3139. p_light[l] = *p_slight;
  3140. if (p_light[l].p_flare)
  3141. ((LENS_FLARE *) (p_light[l].p_flare))->p_svetlo = p_light + l;
  3142. p_light[l].index = l;
  3143. p_light[l].p_mesh_data = p_kont;
  3144. p_light[l].p_next = NULL;
  3145. p_light[l].p_prev = p_light + p;
  3146. p_light[p].p_next = p_light + l;
  3147. p = l;
  3148. p_next = p_slight->p_next;
  3149. free(p_slight);
  3150. p_slight = p_next;
  3151. }
  3152. else {
  3153. ddw("Madrfakr!!!! - Dosly nam svetla!");
  3154. }
  3155. }
  3156. }
  3157. }
  3158. }
  3159. /* Prevedu dynamicky svetla
  3160. */
  3161. if (p_dlist) {
  3162. p_dlight = p_kont->p_dlight;
  3163. if (p_dlight) {
  3164. l = lo_najdi_volne_dsvetlo(p_dlist, dlistnum);
  3165. if (l != K_CHYBA) {
  3166. p_dlist[l] = *p_dlight;
  3167. p_dlist[l].index = l;
  3168. p_dlist[l].p_mesh_data = p_kont;
  3169. p_dlist[l].p_prev = NULL;
  3170. p_dlist[l].p_next = NULL;
  3171. p_kont->p_dlight = p_dlist + l;
  3172. p = l;
  3173. p_dnext = p_dlight->p_next;
  3174. free(p_dlight);
  3175. p_dlight = p_dnext;
  3176. while (p_dlight) {
  3177. l = lo_najdi_volne_dsvetlo(p_dlist, dlistnum);
  3178. if (l != K_CHYBA) {
  3179. p_dlist[l] = *p_dlight;
  3180. p_dlist[l].index = l;
  3181. p_dlist[l].p_mesh_data = p_kont;
  3182. p_dlist[l].p_next = NULL;
  3183. p_dlist[l].p_prev = p_dlist + p;
  3184. p_dlist[p].p_next = p_dlist + l;
  3185. p = l;
  3186. p_dnext = p_dlight->p_next;
  3187. free(p_dlight);
  3188. p_dlight = p_dnext;
  3189. }
  3190. else {
  3191. ddw("Madrfakr!!!! - Dosly nam dyn. svetla!");
  3192. }
  3193. }
  3194. }
  3195. }
  3196. }
  3197. /* Prevedu extra-dynamicky svetla
  3198. */
  3199. if (p_elist) {
  3200. p_elight = p_kont->p_edlight;
  3201. if (p_elight) {
  3202. l = lo_najdi_volne_extra_svetlo(p_elist, elistnum);
  3203. if (l != K_CHYBA) {
  3204. p_elist[l] = *p_elight;
  3205. p_elist[l].index = l;
  3206. p_elist[l].p_mesh_data = p_kont;
  3207. p_elist[l].p_prev = NULL;
  3208. p_elist[l].p_next = NULL;
  3209. p_kont->p_edlight = p_elist + l;
  3210. p = l;
  3211. p_enext = p_elight->p_next;
  3212. free(p_elight);
  3213. p_elight = p_enext;
  3214. while (p_elight) {
  3215. l = lo_najdi_volne_extra_svetlo(p_elist, elistnum);
  3216. if (l != K_CHYBA) {
  3217. p_elist[l] = *p_elight;
  3218. p_elist[l].index = l;
  3219. p_elist[l].p_mesh_data = p_kont;
  3220. p_elist[l].p_next = NULL;
  3221. p_elist[l].p_prev = p_elist + p;
  3222. p_elist[p].p_next = p_elist + l;
  3223. p = l;
  3224. p_enext = p_elight->p_next;
  3225. free(p_elight);
  3226. p_elight = p_enext;
  3227. }
  3228. else {
  3229. ddw("Madrfakr!!!! - Dosly nam extra svetla!");
  3230. }
  3231. }
  3232. }
  3233. }
  3234. }
  3235. p_kont = p_kont->p_next;
  3236. }
  3237. }
  3238. // skopiruje svetla do sceny a navaze se na nove svetla
  3239. void lo_kopiruj_svetla_do_sceny(EDIT_KONTEJNER * p_kont,
  3240. STATIC_LIGHT * p_light, int lightnum, DYN_LIGHT * p_dlist, int dlistnum,
  3241. EXTRA_DYN_LIGHT * p_elist, int elistnum, LENS_FLARE * p_flares,
  3242. int maxflares)
  3243. {
  3244. STATIC_LIGHT *p_slight;
  3245. DYN_LIGHT *p_dlight;
  3246. EXTRA_DYN_LIGHT *p_elight;
  3247. int l, p;
  3248. // Prevedu staticky svetla
  3249. if (p_light) {
  3250. p_slight = p_kont->p_slight;
  3251. if (p_slight) {
  3252. l = lo_najdi_volne_stat_svetlo(p_light, lightnum);
  3253. if (l != K_CHYBA) {
  3254. p_light[l] = *p_slight;
  3255. if (p_light[l].p_flare) {
  3256. p_light[l].p_flare =
  3257. lo_kopiruj_flare(p_flares, maxflares,
  3258. (LENS_FLARE *) p_light[l].p_flare);
  3259. ((LENS_FLARE *) (p_light[l].p_flare))->p_svetlo = p_light + l;
  3260. }
  3261. p_light[l].index = l;
  3262. p_light[l].p_mesh_data = p_kont;
  3263. p_light[l].p_prev = NULL;
  3264. p_light[l].p_next = NULL;
  3265. p_kont->p_slight = p_light + l;
  3266. p = l;
  3267. p_slight = p_slight->p_next;
  3268. while (p_slight) {
  3269. l = lo_najdi_volne_stat_svetlo(p_light, lightnum);
  3270. if (l != K_CHYBA) {
  3271. p_light[l] = *p_slight;
  3272. if (p_light[l].p_flare) {
  3273. p_light[l].p_flare =
  3274. lo_kopiruj_flare(p_flares, maxflares,
  3275. (LENS_FLARE *) p_light[l].p_flare);
  3276. ((LENS_FLARE *) (p_light[l].p_flare))->p_svetlo = p_light + l;
  3277. }
  3278. p_light[l].index = l;
  3279. p_light[l].p_mesh_data = p_kont;
  3280. p_light[l].p_next = NULL;
  3281. p_light[l].p_prev = p_light + p;
  3282. p_light[p].p_next = p_light + l;
  3283. p = l;
  3284. p_slight = p_slight->p_next;
  3285. }
  3286. else {
  3287. ddw("Madrfakr!!!! - Dosly nam svetla!");
  3288. }
  3289. }
  3290. }
  3291. }
  3292. }
  3293. // Prevedu dynamicky svetla
  3294. if (p_dlist) {
  3295. p_dlight = p_kont->p_dlight;
  3296. if (p_dlight) {
  3297. l = lo_najdi_volne_dsvetlo(p_dlist, dlistnum);
  3298. if (l != K_CHYBA) {
  3299. p_dlist[l] = *p_dlight;
  3300. p_dlist[l].index = l;
  3301. p_dlist[l].p_mesh_data = p_kont;
  3302. p_dlist[l].p_prev = NULL;
  3303. p_dlist[l].p_next = NULL;
  3304. p_kont->p_dlight = p_dlist + l;
  3305. p = l;
  3306. p_dlight = p_dlight->p_next;
  3307. while (p_dlight) {
  3308. l = lo_najdi_volne_dsvetlo(p_dlist, dlistnum);
  3309. if (l != K_CHYBA) {
  3310. p_dlist[l] = *p_dlight;
  3311. p_dlist[l].index = l;
  3312. p_dlist[l].p_mesh_data = p_kont;
  3313. p_dlist[l].p_next = NULL;
  3314. p_dlist[l].p_prev = p_dlist + p;
  3315. p_dlist[p].p_next = p_dlist + l;
  3316. p = l;
  3317. p_dlight = p_dlight->p_next;
  3318. }
  3319. else {
  3320. ddw("Madrfakr!!!! - Dosly nam dyn. svetla!");
  3321. }
  3322. }
  3323. }
  3324. }
  3325. }
  3326. // Prevedu extra svetla
  3327. if (p_elist) {
  3328. p_elight = p_kont->p_edlight;
  3329. if (p_elight) {
  3330. l = lo_najdi_volne_extra_svetlo(p_elist, elistnum);
  3331. if (l != K_CHYBA) {
  3332. p_elist[l] = *p_elight;
  3333. p_elist[l].index = l;
  3334. p_elist[l].p_mesh_data = p_kont;
  3335. p_elist[l].p_prev = NULL;
  3336. p_elist[l].p_next = NULL;
  3337. p_kont->p_edlight = p_elist + l;
  3338. p = l;
  3339. p_elight = p_elight->p_next;
  3340. while (p_elight) {
  3341. l = lo_najdi_volne_extra_svetlo(p_elist, elistnum);
  3342. if (l != K_CHYBA) {
  3343. p_elist[l] = *p_elight;
  3344. p_elist[l].index = l;
  3345. p_elist[l].p_mesh_data = p_kont;
  3346. p_elist[l].p_next = NULL;
  3347. p_elist[l].p_prev = p_elist + p;
  3348. p_elist[p].p_next = p_elist + l;
  3349. p = l;
  3350. p_elight = p_elight->p_next;
  3351. }
  3352. else {
  3353. ddw("Madrfakr!!!! - Dosly nam extra svetla!");
  3354. }
  3355. }
  3356. }
  3357. }
  3358. }
  3359. }
  3360. // skopiruje svetla do sceny a navaze se na nove svetla
  3361. void lo_kopiruj_svetla_do_sceny_mesh(GAME_MESH_DATA * p_data,
  3362. DYN_LIGHT * p_dlist, int dlistnum, EXTRA_DYN_LIGHT * p_elist, int elistnum)
  3363. {
  3364. DYN_LIGHT *p_dlight = NULL;
  3365. EXTRA_DYN_LIGHT *p_elight;
  3366. int l, p;
  3367. /* Prevedu dynamicky svetla
  3368. */
  3369. if (p_dlist) {
  3370. p_dlight = p_data->p_ldlight;
  3371. if (p_dlight) {
  3372. l = lo_najdi_volne_dsvetlo(p_dlist, dlistnum);
  3373. if (l != K_CHYBA) {
  3374. p_dlist[l] = *p_dlight;
  3375. p_dlist[l].index = l;
  3376. p_dlist[l].p_mesh_data = p_data;
  3377. p_dlist[l].p_prev = NULL;
  3378. p_dlist[l].p_next = NULL;
  3379. p_data->p_ldlight = p_dlist + l;
  3380. p = l;
  3381. p_dlight = p_dlight->p_next;
  3382. while (p_dlight) {
  3383. l = lo_najdi_volne_dsvetlo(p_dlist, dlistnum);
  3384. if (l != K_CHYBA) {
  3385. p_dlist[l] = *p_dlight;
  3386. p_dlist[l].index = l;
  3387. p_dlist[l].p_mesh_data = p_data;
  3388. p_dlist[l].p_next = NULL;
  3389. p_dlist[l].p_prev = p_dlist + p;
  3390. p_dlist[p].p_next = p_dlist + l;
  3391. p = l;
  3392. p_dlight = p_dlight->p_next;
  3393. }
  3394. else {
  3395. ddw("Madrfakr!!!! - Dosly nam dyn. svetla!");
  3396. }
  3397. }
  3398. }
  3399. }
  3400. }
  3401. /* Prevedu extra svetla
  3402. */
  3403. if (p_elist) {
  3404. p_elight = p_data->p_lelight;
  3405. if (p_dlight) {
  3406. l = lo_najdi_volne_extra_svetlo(p_elist, elistnum);
  3407. if (l != K_CHYBA) {
  3408. p_elist[l] = *p_elight;
  3409. p_elist[l].index = l;
  3410. p_elist[l].p_mesh_data = p_data;
  3411. p_elist[l].p_prev = NULL;
  3412. p_elist[l].p_next = NULL;
  3413. p_data->p_lelight = p_elist + l;
  3414. p = l;
  3415. p_elight = p_elight->p_next;
  3416. while (p_elight) {
  3417. l = lo_najdi_volne_extra_svetlo(p_elist, elistnum);
  3418. if (l != K_CHYBA) {
  3419. p_elist[l] = *p_elight;
  3420. p_elist[l].index = l;
  3421. p_elist[l].p_mesh_data = p_data;
  3422. p_elist[l].p_next = NULL;
  3423. p_elist[l].p_prev = p_elist + p;
  3424. p_elist[p].p_next = p_elist + l;
  3425. p = l;
  3426. p_elight = p_elight->p_next;
  3427. }
  3428. else {
  3429. ddw("Madrfakr!!!! - Dosly nam dyn. svetla!");
  3430. }
  3431. }
  3432. }
  3433. }
  3434. }
  3435. }
  3436. void lo_preved_svetla_do_sceny_mesh(GAME_MESH_DATA * p_data,
  3437. DYN_LIGHT * p_dlist, int dlistnum, EXTRA_DYN_LIGHT * p_elist, int elistnum)
  3438. {
  3439. DYN_LIGHT *p_dlight, *p_dnext;
  3440. EXTRA_DYN_LIGHT *p_elight, *p_enext;
  3441. int l, p;
  3442. /* Prevedu dynamicky svetla
  3443. */
  3444. if (p_dlist) {
  3445. p_dlight = p_data->p_ldlight;
  3446. if (p_dlight) {
  3447. l = lo_najdi_volne_dsvetlo(p_dlist, dlistnum);
  3448. if (l != K_CHYBA) {
  3449. p_dlist[l] = *p_dlight;
  3450. p_dlist[l].index = l;
  3451. p_dlist[l].p_mesh_data = p_data;
  3452. p_dlist[l].p_prev = NULL;
  3453. p_dlist[l].p_next = NULL;
  3454. p_data->p_ldlight = p_dlist + l;
  3455. p = l;
  3456. p_dnext = p_dlight->p_next;
  3457. free(p_dlight);
  3458. p_dlight = p_dnext;
  3459. while (p_dlight) {
  3460. l = lo_najdi_volne_dsvetlo(p_dlist, dlistnum);
  3461. if (l != K_CHYBA) {
  3462. p_dlist[l] = *p_dlight;
  3463. p_dlist[l].index = l;
  3464. p_dlist[l].p_mesh_data = p_data;
  3465. p_dlist[l].p_next = NULL;
  3466. p_dlist[l].p_prev = p_dlist + p;
  3467. p_dlist[p].p_next = p_dlist + l;
  3468. p = l;
  3469. p_dnext = p_dlight->p_next;
  3470. free(p_dlight);
  3471. p_dlight = p_dnext;
  3472. }
  3473. else {
  3474. ddw("Madrfakr!!!! - Dosly nam dyn. svetla!");
  3475. }
  3476. }
  3477. }
  3478. }
  3479. }
  3480. /* Prevedu dynamicky svetla
  3481. */
  3482. if (p_elist) {
  3483. p_elight = p_data->p_lelight;
  3484. if (p_elight) {
  3485. l = lo_najdi_volne_extra_svetlo(p_elist, elistnum);
  3486. if (l != K_CHYBA) {
  3487. p_elist[l] = *p_elight;
  3488. p_elist[l].index = l;
  3489. p_elist[l].p_mesh_data = p_data;
  3490. p_elist[l].p_prev = NULL;
  3491. p_elist[l].p_next = NULL;
  3492. p_data->p_lelight = p_elist + l;
  3493. p = l;
  3494. p_enext = p_elight->p_next;
  3495. free(p_elight);
  3496. p_elight = p_enext;
  3497. while (p_elight) {
  3498. l = lo_najdi_volne_extra_svetlo(p_elist, elistnum);
  3499. if (l != K_CHYBA) {
  3500. p_elist[l] = *p_elight;
  3501. p_elist[l].index = l;
  3502. p_elist[l].p_mesh_data = p_data;
  3503. p_elist[l].p_next = NULL;
  3504. p_elist[l].p_prev = p_elist + p;
  3505. p_elist[p].p_next = p_elist + l;
  3506. p = l;
  3507. p_enext = p_elight->p_next;
  3508. free(p_elight);
  3509. p_elight = p_enext;
  3510. }
  3511. else {
  3512. ddw("Madrfakr!!!! - Dosly nam dyn. svetla!");
  3513. }
  3514. }
  3515. }
  3516. }
  3517. }
  3518. }
  3519. void lo_vymaz_svetla_ze_sceny(EDIT_KONTEJNER * p_kont)
  3520. {
  3521. STATIC_LIGHT *p_slight = p_kont->p_slight, *p_snext;
  3522. DYN_LIGHT *p_dlight = p_kont->p_dlight, *p_dnext;
  3523. EXTRA_DYN_LIGHT *p_elight = p_kont->p_edlight, *p_enext;
  3524. while (p_slight) {
  3525. p_snext = p_slight->p_next;
  3526. memset((void *) p_slight, 0, sizeof(p_slight[0]));
  3527. p_slight = p_snext;
  3528. }
  3529. while (p_dlight) {
  3530. p_dnext = p_dlight->p_next;
  3531. memset((void *) p_dlight, 0, sizeof(p_dlight[0]));
  3532. p_dlight = p_dnext;
  3533. }
  3534. while (p_elight) {
  3535. p_enext = p_elight->p_next;
  3536. memset((void *) p_elight, 0, sizeof(p_elight[0]));
  3537. p_elight = p_enext;
  3538. }
  3539. }
  3540. void lo_vymaz_svetla(EDIT_KONTEJNER * p_kont)
  3541. {
  3542. STATIC_LIGHT *p_slight = p_kont->p_slight, *p_snext;
  3543. DYN_LIGHT *p_dlight = p_kont->p_dlight, *p_dnext;
  3544. EXTRA_DYN_LIGHT *p_elight = p_kont->p_edlight, *p_enext;
  3545. while (p_slight) {
  3546. p_snext = p_slight->p_next;
  3547. free(p_slight);
  3548. p_slight = p_snext;
  3549. }
  3550. while (p_dlight) {
  3551. p_dnext = p_dlight->p_next;
  3552. free(p_dlight);
  3553. p_dlight = p_dnext;
  3554. }
  3555. while (p_elight) {
  3556. p_enext = p_elight->p_next;
  3557. free(p_elight);
  3558. p_elight = p_enext;
  3559. }
  3560. }
  3561. void lo_vymaz_svetla_ze_sceny_mesh(GAME_MESH_DATA * p_data)
  3562. {
  3563. DYN_LIGHT *p_dlight = p_data->p_ldlight, *p_dnext;
  3564. EXTRA_DYN_LIGHT *p_elight = p_data->p_lelight, *p_enext;
  3565. while (p_dlight) {
  3566. p_dnext = p_dlight->p_next;
  3567. memset((void *) p_dlight, 0, sizeof(p_dlight[0]));
  3568. p_dlight = p_dnext;
  3569. }
  3570. while (p_elight) {
  3571. p_enext = p_elight->p_next;
  3572. memset((void *) p_elight, 0, sizeof(p_elight[0]));
  3573. p_elight = p_enext;
  3574. }
  3575. }
  3576. void lo_vymaz_svetla_mesh(GAME_MESH_DATA * p_data)
  3577. {
  3578. DYN_LIGHT *p_dlight = p_data->p_ldlight, *p_dnext;
  3579. EXTRA_DYN_LIGHT *p_elight = p_data->p_lelight, *p_enext;
  3580. while (p_dlight) {
  3581. p_dnext = p_dlight->p_next;
  3582. free(p_dlight);
  3583. p_dlight = p_dnext;
  3584. }
  3585. while (p_elight) {
  3586. p_enext = p_elight->p_next;
  3587. free(p_elight);
  3588. p_elight = p_enext;
  3589. }
  3590. }
  3591. void lo_premapuj_svetla_kont_mesh(EDIT_KONTEJNER * p_src,
  3592. GAME_MESH_OLD * p_dest)
  3593. {
  3594. STATIC_LIGHT *p_slight = p_src->p_slight;
  3595. DYN_LIGHT *p_dlight = p_src->p_dlight;
  3596. EXTRA_DYN_LIGHT *p_elight = p_src->p_edlight;
  3597. if (!p_src || !p_dest)
  3598. return;
  3599. while (p_slight) {
  3600. p_slight->p_mesh_data = p_dest->p_data;
  3601. p_slight = p_slight->p_next;
  3602. }
  3603. p_dest->p_data->p_lslight = p_src->p_slight;
  3604. p_src->p_slight = NULL;
  3605. while (p_dlight) {
  3606. p_dlight->p_mesh_data = p_dest->p_data;
  3607. p_dlight = p_dlight->p_next;
  3608. }
  3609. p_dest->p_data->p_ldlight = p_src->p_dlight;
  3610. p_src->p_dlight = NULL;
  3611. while (p_elight) {
  3612. p_elight->p_mesh_data = p_dest->p_data;
  3613. p_elight = p_elight->p_next;
  3614. }
  3615. p_dest->p_data->p_lelight = p_src->p_edlight;
  3616. p_src->p_edlight = NULL;
  3617. }
  3618. void lo_premapuj_svetla_mesh(GAME_MESH_OLD * p_src, GAME_MESH_OLD * p_dest)
  3619. {
  3620. STATIC_LIGHT *p_slight = p_src->p_data->p_lslight;
  3621. DYN_LIGHT *p_dlight = p_src->p_data->p_ldlight;
  3622. EXTRA_DYN_LIGHT *p_elight = p_src->p_data->p_lelight;
  3623. if (!p_src || !p_dest)
  3624. return;
  3625. while (p_slight) {
  3626. p_slight->p_mesh_data = p_dest->p_data;
  3627. p_slight = p_slight->p_next;
  3628. }
  3629. p_dest->p_data->p_lslight = p_src->p_data->p_lslight;
  3630. p_src->p_data->p_lslight = NULL;
  3631. while (p_dlight) {
  3632. p_dlight->p_mesh_data = p_dest->p_data;
  3633. p_dlight = p_dlight->p_next;
  3634. }
  3635. p_dest->p_data->p_ldlight = p_src->p_data->p_ldlight;
  3636. p_src->p_data->p_ldlight = NULL;
  3637. while (p_elight) {
  3638. p_elight->p_mesh_data = p_dest->p_data;
  3639. p_elight = p_elight->p_next;
  3640. }
  3641. p_dest->p_data->p_lelight = p_src->p_data->p_lelight;
  3642. p_src->p_data->p_lelight = NULL;
  3643. }
  3644. void lo_transformuj_svetla_do_wordspace(EDIT_KONTEJNER * p_src)
  3645. {
  3646. STATIC_LIGHT *p_slight = p_src->p_slight;
  3647. DYN_LIGHT *p_dlight = p_src->p_dlight;
  3648. EXTRA_DYN_LIGHT *p_elight = p_src->p_edlight;
  3649. GLMATRIX *p_top = kont_world_matrix(p_src);
  3650. if (!p_src)
  3651. return;
  3652. while (p_slight) {
  3653. transformuj_bod_bod_matici(&p_slight->p, p_top);
  3654. p_slight = p_slight->p_next;
  3655. }
  3656. while (p_dlight) {
  3657. transformuj_bod_bod_matici(&p_dlight->np, p_top);
  3658. p_dlight->tp = p_dlight->np;
  3659. p_dlight = p_dlight->p_next;
  3660. }
  3661. while (p_elight) {
  3662. transformuj_bod_bod_matici(&p_elight->np, p_top);
  3663. p_elight->tp = p_elight->np;
  3664. p_elight = p_elight->p_next;
  3665. }
  3666. }
  3667. void lo_premapuj_svetla_do_wordspace(EDIT_KONTEJNER * p_src)
  3668. {
  3669. if (!p_src)
  3670. return;
  3671. STATIC_LIGHT *p_slight = p_src->p_slight;
  3672. DYN_LIGHT *p_dlight = p_src->p_dlight;
  3673. EXTRA_DYN_LIGHT *p_elight = p_src->p_edlight;
  3674. kont_world_matrix(p_src);
  3675. lo_transformuj_svetla_do_wordspace(p_src);
  3676. while (p_slight) {
  3677. if (p_slight->p_flare) {
  3678. ((LENS_FLARE *) (p_slight->p_flare))->p = p_slight->p;
  3679. ((LENS_FLARE *) (p_slight->p_flare))->p_svetlo = NULL;
  3680. }
  3681. p_slight->p_mesh_data = NULL;
  3682. p_slight->flag2 &= ~SL2_MESH;
  3683. p_slight = p_slight->p_next;
  3684. }
  3685. p_src->p_slight = NULL;
  3686. while (p_dlight) {
  3687. p_dlight->p_mesh_data = NULL;
  3688. p_dlight->flag &= ~SDL_MESH;
  3689. p_dlight = p_dlight->p_next;
  3690. }
  3691. p_src->p_dlight = NULL;
  3692. while (p_elight) {
  3693. p_elight->p_mesh_data = NULL;
  3694. p_elight->flag &= ~SDL_MESH;
  3695. p_elight = p_elight->p_next;
  3696. }
  3697. p_src->p_edlight = NULL;
  3698. }
  3699. LENS_FLARE *lo_kopiruj_flare(LENS_FLARE * p_flarelist, int max,
  3700. LENS_FLARE * p_flare)
  3701. {
  3702. int f = lo_najdi_volny_flare(p_flarelist, max);
  3703. p_flarelist[f] = *p_flare;
  3704. if (p_flare->p_sloz) {
  3705. p_flarelist[f].p_sloz =
  3706. (LENS_FLARE_SLOZ *) mmalloc(sizeof(p_flarelist[f].p_sloz[0]) *
  3707. SLOZ_FLARE);
  3708. memcpy(p_flarelist[f].p_sloz, p_flare->p_sloz,
  3709. sizeof(p_flarelist[f].p_sloz[0]) * SLOZ_FLARE);
  3710. }
  3711. p_flarelist[f].index = f;
  3712. return (p_flarelist + f);
  3713. }
  3714. void mesh_pridej_vertex_array(GAME_MESH_OLD * p_mesh)
  3715. {
  3716. int flag, m2flag = p_mesh->p_data->m2flag;
  3717. if (gl_ext::extlist_vertex_array) {
  3718. mesh_vertex_array_init(p_mesh);
  3719. flag = KONT2_UPDATE_POS | KONT2_UPDATE_NORM;
  3720. if (m2flag & MAT2_DIFFUSE)
  3721. flag |= KONT2_UPDATE_DIFF;
  3722. if (m2flag & (MAT2_SPECULAR | MAT2_ENV_SPEC))
  3723. flag |= KONT2_UPDATE_SPEC;
  3724. if (m2flag & MAT2_MAP1)
  3725. flag |= KONT2_UPDATE_TEXT1;
  3726. if (m2flag & MAT2_MAP2)
  3727. flag |= KONT2_UPDATE_TEXT2;
  3728. if (m2flag & MAT2_MAP3)
  3729. flag |= KONT2_UPDATE_TEXT3;
  3730. if (m2flag & MAT2_MAP4)
  3731. flag |= KONT2_UPDATE_TEXT4;
  3732. p_mesh->p_data->k2flag |= flag | KONT2_UPDATE_NEW;
  3733. mesh_vertex_array_upload(p_mesh);
  3734. }
  3735. }
  3736. void poly_pridej_vertex_array(EDIT_MESH_POLY * p_poly)
  3737. {
  3738. memset(&p_poly->varray, 0, sizeof(p_poly->varray));
  3739. poly_vertex_array_init(p_poly);
  3740. poly_vertex_array_upload(p_poly);
  3741. }