texture.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file blender/blenkernel/intern/texture.c
  28. * \ingroup bke
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <math.h>
  34. #include "MEM_guardedalloc.h"
  35. #include "BLI_math.h"
  36. #include "BLI_kdopbvh.h"
  37. #include "BLI_utildefines.h"
  38. #include "BLI_math_color.h"
  39. #include "DNA_key_types.h"
  40. #include "DNA_object_types.h"
  41. #include "DNA_lamp_types.h"
  42. #include "DNA_material_types.h"
  43. #include "DNA_world_types.h"
  44. #include "DNA_brush_types.h"
  45. #include "DNA_node_types.h"
  46. #include "DNA_color_types.h"
  47. #include "DNA_particle_types.h"
  48. #include "DNA_linestyle_types.h"
  49. #include "IMB_imbuf.h"
  50. #include "BKE_global.h"
  51. #include "BKE_main.h"
  52. #include "BKE_library.h"
  53. #include "BKE_library_query.h"
  54. #include "BKE_library_remap.h"
  55. #include "BKE_image.h"
  56. #include "BKE_material.h"
  57. #include "BKE_texture.h"
  58. #include "BKE_key.h"
  59. #include "BKE_icons.h"
  60. #include "BKE_node.h"
  61. #include "BKE_animsys.h"
  62. #include "BKE_colortools.h"
  63. #include "BKE_scene.h"
  64. #include "RE_shader_ext.h"
  65. /* ****************** Mapping ******************* */
  66. TexMapping *BKE_texture_mapping_add(int type)
  67. {
  68. TexMapping *texmap = MEM_callocN(sizeof(TexMapping), "TexMapping");
  69. BKE_texture_mapping_default(texmap, type);
  70. return texmap;
  71. }
  72. void BKE_texture_mapping_default(TexMapping *texmap, int type)
  73. {
  74. memset(texmap, 0, sizeof(TexMapping));
  75. texmap->size[0] = texmap->size[1] = texmap->size[2] = 1.0f;
  76. texmap->max[0] = texmap->max[1] = texmap->max[2] = 1.0f;
  77. unit_m4(texmap->mat);
  78. texmap->projx = PROJ_X;
  79. texmap->projy = PROJ_Y;
  80. texmap->projz = PROJ_Z;
  81. texmap->mapping = MTEX_FLAT;
  82. texmap->type = type;
  83. }
  84. void BKE_texture_mapping_init(TexMapping *texmap)
  85. {
  86. float smat[4][4], rmat[4][4], tmat[4][4], proj[4][4], size[3];
  87. if (texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z &&
  88. is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size))
  89. {
  90. unit_m4(texmap->mat);
  91. texmap->flag |= TEXMAP_UNIT_MATRIX;
  92. }
  93. else {
  94. /* axis projection */
  95. zero_m4(proj);
  96. proj[3][3] = 1.0f;
  97. if (texmap->projx != PROJ_N)
  98. proj[texmap->projx - 1][0] = 1.0f;
  99. if (texmap->projy != PROJ_N)
  100. proj[texmap->projy - 1][1] = 1.0f;
  101. if (texmap->projz != PROJ_N)
  102. proj[texmap->projz - 1][2] = 1.0f;
  103. /* scale */
  104. copy_v3_v3(size, texmap->size);
  105. if (ELEM(texmap->type, TEXMAP_TYPE_TEXTURE, TEXMAP_TYPE_NORMAL)) {
  106. /* keep matrix invertible */
  107. if (fabsf(size[0]) < 1e-5f)
  108. size[0] = signf(size[0]) * 1e-5f;
  109. if (fabsf(size[1]) < 1e-5f)
  110. size[1] = signf(size[1]) * 1e-5f;
  111. if (fabsf(size[2]) < 1e-5f)
  112. size[2] = signf(size[2]) * 1e-5f;
  113. }
  114. size_to_mat4(smat, texmap->size);
  115. /* rotation */
  116. eul_to_mat4(rmat, texmap->rot);
  117. /* translation */
  118. unit_m4(tmat);
  119. copy_v3_v3(tmat[3], texmap->loc);
  120. if (texmap->type == TEXMAP_TYPE_TEXTURE) {
  121. /* to transform a texture, the inverse transform needs
  122. * to be applied to the texture coordinate */
  123. mul_m4_series(texmap->mat, tmat, rmat, smat);
  124. invert_m4(texmap->mat);
  125. }
  126. else if (texmap->type == TEXMAP_TYPE_POINT) {
  127. /* forward transform */
  128. mul_m4_series(texmap->mat, tmat, rmat, smat);
  129. }
  130. else if (texmap->type == TEXMAP_TYPE_VECTOR) {
  131. /* no translation for vectors */
  132. mul_m4_m4m4(texmap->mat, rmat, smat);
  133. }
  134. else if (texmap->type == TEXMAP_TYPE_NORMAL) {
  135. /* no translation for normals, and inverse transpose */
  136. mul_m4_m4m4(texmap->mat, rmat, smat);
  137. invert_m4(texmap->mat);
  138. transpose_m4(texmap->mat);
  139. }
  140. /* projection last */
  141. mul_m4_m4m4(texmap->mat, texmap->mat, proj);
  142. texmap->flag &= ~TEXMAP_UNIT_MATRIX;
  143. }
  144. }
  145. ColorMapping *BKE_texture_colormapping_add(void)
  146. {
  147. ColorMapping *colormap = MEM_callocN(sizeof(ColorMapping), "ColorMapping");
  148. BKE_texture_colormapping_default(colormap);
  149. return colormap;
  150. }
  151. void BKE_texture_colormapping_default(ColorMapping *colormap)
  152. {
  153. memset(colormap, 0, sizeof(ColorMapping));
  154. init_colorband(&colormap->coba, true);
  155. colormap->bright = 1.0;
  156. colormap->contrast = 1.0;
  157. colormap->saturation = 1.0;
  158. colormap->blend_color[0] = 0.8f;
  159. colormap->blend_color[1] = 0.8f;
  160. colormap->blend_color[2] = 0.8f;
  161. colormap->blend_type = MA_RAMP_BLEND;
  162. colormap->blend_factor = 0.0f;
  163. }
  164. /* ****************** COLORBAND ******************* */
  165. void init_colorband(ColorBand *coba, bool rangetype)
  166. {
  167. int a;
  168. coba->data[0].pos = 0.0;
  169. coba->data[1].pos = 1.0;
  170. if (rangetype == 0) {
  171. coba->data[0].r = 0.0;
  172. coba->data[0].g = 0.0;
  173. coba->data[0].b = 0.0;
  174. coba->data[0].a = 0.0;
  175. coba->data[1].r = 1.0;
  176. coba->data[1].g = 1.0;
  177. coba->data[1].b = 1.0;
  178. coba->data[1].a = 1.0;
  179. }
  180. else {
  181. coba->data[0].r = 0.0;
  182. coba->data[0].g = 0.0;
  183. coba->data[0].b = 0.0;
  184. coba->data[0].a = 1.0;
  185. coba->data[1].r = 1.0;
  186. coba->data[1].g = 1.0;
  187. coba->data[1].b = 1.0;
  188. coba->data[1].a = 1.0;
  189. }
  190. for (a = 2; a < MAXCOLORBAND; a++) {
  191. coba->data[a].r = 0.5;
  192. coba->data[a].g = 0.5;
  193. coba->data[a].b = 0.5;
  194. coba->data[a].a = 1.0;
  195. coba->data[a].pos = 0.5;
  196. }
  197. coba->tot = 2;
  198. coba->color_mode = COLBAND_BLEND_RGB;
  199. }
  200. ColorBand *add_colorband(bool rangetype)
  201. {
  202. ColorBand *coba;
  203. coba = MEM_callocN(sizeof(ColorBand), "colorband");
  204. init_colorband(coba, rangetype);
  205. return coba;
  206. }
  207. /* ------------------------------------------------------------------------- */
  208. static float colorband_hue_interp(
  209. const int ipotype_hue,
  210. const float mfac, const float fac,
  211. float h1, float h2)
  212. {
  213. float h_interp;
  214. int mode = 0;
  215. #define HUE_INTERP(h_a, h_b) ((mfac * (h_a)) + (fac * (h_b)))
  216. #define HUE_MOD(h) (((h) < 1.0f) ? (h) : (h) - 1.0f)
  217. h1 = HUE_MOD(h1);
  218. h2 = HUE_MOD(h2);
  219. BLI_assert(h1 >= 0.0f && h1 < 1.0f);
  220. BLI_assert(h2 >= 0.0f && h2 < 1.0f);
  221. switch (ipotype_hue) {
  222. case COLBAND_HUE_NEAR:
  223. {
  224. if ((h1 < h2) && (h2 - h1) > +0.5f) mode = 1;
  225. else if ((h1 > h2) && (h2 - h1) < -0.5f) mode = 2;
  226. else mode = 0;
  227. break;
  228. }
  229. case COLBAND_HUE_FAR:
  230. {
  231. if ((h1 < h2) && (h2 - h1) < +0.5f) mode = 1;
  232. else if ((h1 > h2) && (h2 - h1) > -0.5f) mode = 2;
  233. else mode = 0;
  234. break;
  235. }
  236. case COLBAND_HUE_CCW:
  237. {
  238. if (h1 > h2) mode = 2;
  239. else mode = 0;
  240. break;
  241. }
  242. case COLBAND_HUE_CW:
  243. {
  244. if (h1 < h2) mode = 1;
  245. else mode = 0;
  246. break;
  247. }
  248. }
  249. switch (mode) {
  250. case 0:
  251. h_interp = HUE_INTERP(h1, h2);
  252. break;
  253. case 1:
  254. h_interp = HUE_INTERP(h1 + 1.0f, h2);
  255. h_interp = HUE_MOD(h_interp);
  256. break;
  257. case 2:
  258. h_interp = HUE_INTERP(h1, h2 + 1.0f);
  259. h_interp = HUE_MOD(h_interp);
  260. break;
  261. }
  262. BLI_assert(h_interp >= 0.0f && h_interp < 1.0f);
  263. #undef HUE_INTERP
  264. #undef HUE_MOD
  265. return h_interp;
  266. }
  267. bool do_colorband(const ColorBand *coba, float in, float out[4])
  268. {
  269. const CBData *cbd1, *cbd2, *cbd0, *cbd3;
  270. float fac;
  271. int ipotype;
  272. int a;
  273. if (coba == NULL || coba->tot == 0) return false;
  274. cbd1 = coba->data;
  275. ipotype = (coba->color_mode == COLBAND_BLEND_RGB) ? coba->ipotype : COLBAND_INTERP_LINEAR;
  276. if (coba->tot == 1) {
  277. out[0] = cbd1->r;
  278. out[1] = cbd1->g;
  279. out[2] = cbd1->b;
  280. out[3] = cbd1->a;
  281. }
  282. else if ((in <= cbd1->pos) && ELEM(ipotype, COLBAND_INTERP_LINEAR, COLBAND_INTERP_EASE)) {
  283. out[0] = cbd1->r;
  284. out[1] = cbd1->g;
  285. out[2] = cbd1->b;
  286. out[3] = cbd1->a;
  287. }
  288. else {
  289. CBData left, right;
  290. /* we're looking for first pos > in */
  291. for (a = 0; a < coba->tot; a++, cbd1++) {
  292. if (cbd1->pos > in) {
  293. break;
  294. }
  295. }
  296. if (a == coba->tot) {
  297. cbd2 = cbd1 - 1;
  298. right = *cbd2;
  299. right.pos = 1.0f;
  300. cbd1 = &right;
  301. }
  302. else if (a == 0) {
  303. left = *cbd1;
  304. left.pos = 0.0f;
  305. cbd2 = &left;
  306. }
  307. else {
  308. cbd2 = cbd1 - 1;
  309. }
  310. if ((in >= cbd1->pos) && ELEM(ipotype, COLBAND_INTERP_LINEAR, COLBAND_INTERP_EASE)) {
  311. out[0] = cbd1->r;
  312. out[1] = cbd1->g;
  313. out[2] = cbd1->b;
  314. out[3] = cbd1->a;
  315. }
  316. else {
  317. if (cbd2->pos != cbd1->pos) {
  318. fac = (in - cbd1->pos) / (cbd2->pos - cbd1->pos);
  319. }
  320. else {
  321. /* was setting to 0.0 in 2.56 & previous, but this
  322. * is incorrect for the last element, see [#26732] */
  323. fac = (a != coba->tot) ? 0.0f : 1.0f;
  324. }
  325. if (ipotype == COLBAND_INTERP_CONSTANT) {
  326. /* constant */
  327. out[0] = cbd2->r;
  328. out[1] = cbd2->g;
  329. out[2] = cbd2->b;
  330. out[3] = cbd2->a;
  331. }
  332. else if (ipotype >= COLBAND_INTERP_B_SPLINE) {
  333. /* ipo from right to left: 3 2 1 0 */
  334. float t[4];
  335. if (a >= coba->tot - 1) cbd0 = cbd1;
  336. else cbd0 = cbd1 + 1;
  337. if (a < 2) cbd3 = cbd2;
  338. else cbd3 = cbd2 - 1;
  339. CLAMP(fac, 0.0f, 1.0f);
  340. if (ipotype == COLBAND_INTERP_CARDINAL) {
  341. key_curve_position_weights(fac, t, KEY_CARDINAL);
  342. }
  343. else {
  344. key_curve_position_weights(fac, t, KEY_BSPLINE);
  345. }
  346. out[0] = t[3] * cbd3->r + t[2] * cbd2->r + t[1] * cbd1->r + t[0] * cbd0->r;
  347. out[1] = t[3] * cbd3->g + t[2] * cbd2->g + t[1] * cbd1->g + t[0] * cbd0->g;
  348. out[2] = t[3] * cbd3->b + t[2] * cbd2->b + t[1] * cbd1->b + t[0] * cbd0->b;
  349. out[3] = t[3] * cbd3->a + t[2] * cbd2->a + t[1] * cbd1->a + t[0] * cbd0->a;
  350. CLAMP(out[0], 0.0f, 1.0f);
  351. CLAMP(out[1], 0.0f, 1.0f);
  352. CLAMP(out[2], 0.0f, 1.0f);
  353. CLAMP(out[3], 0.0f, 1.0f);
  354. }
  355. else {
  356. float mfac;
  357. if (ipotype == COLBAND_INTERP_EASE) {
  358. mfac = fac * fac;
  359. fac = 3.0f * mfac - 2.0f * mfac * fac;
  360. }
  361. mfac = 1.0f - fac;
  362. if (UNLIKELY(coba->color_mode == COLBAND_BLEND_HSV)) {
  363. float col1[3], col2[3];
  364. rgb_to_hsv_v(&cbd1->r, col1);
  365. rgb_to_hsv_v(&cbd2->r, col2);
  366. out[0] = colorband_hue_interp(coba->ipotype_hue, mfac, fac, col1[0], col2[0]);
  367. out[1] = mfac * col1[1] + fac * col2[1];
  368. out[2] = mfac * col1[2] + fac * col2[2];
  369. out[3] = mfac * cbd1->a + fac * cbd2->a;
  370. hsv_to_rgb_v(out, out);
  371. }
  372. else if (UNLIKELY(coba->color_mode == COLBAND_BLEND_HSL)) {
  373. float col1[3], col2[3];
  374. rgb_to_hsl_v(&cbd1->r, col1);
  375. rgb_to_hsl_v(&cbd2->r, col2);
  376. out[0] = colorband_hue_interp(coba->ipotype_hue, mfac, fac, col1[0], col2[0]);
  377. out[1] = mfac * col1[1] + fac * col2[1];
  378. out[2] = mfac * col1[2] + fac * col2[2];
  379. out[3] = mfac * cbd1->a + fac * cbd2->a;
  380. hsl_to_rgb_v(out, out);
  381. }
  382. else {
  383. /* COLBAND_BLEND_RGB */
  384. out[0] = mfac * cbd1->r + fac * cbd2->r;
  385. out[1] = mfac * cbd1->g + fac * cbd2->g;
  386. out[2] = mfac * cbd1->b + fac * cbd2->b;
  387. out[3] = mfac * cbd1->a + fac * cbd2->a;
  388. }
  389. }
  390. }
  391. }
  392. return true; /* OK */
  393. }
  394. void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
  395. {
  396. int a;
  397. *size = CM_TABLE + 1;
  398. *array = MEM_callocN(sizeof(float) * (*size) * 4, "ColorBand");
  399. for (a = 0; a < *size; a++)
  400. do_colorband(coba, (float)a / (float)CM_TABLE, &(*array)[a * 4]);
  401. }
  402. static int vergcband(const void *a1, const void *a2)
  403. {
  404. const CBData *x1 = a1, *x2 = a2;
  405. if (x1->pos > x2->pos) return 1;
  406. else if (x1->pos < x2->pos) return -1;
  407. return 0;
  408. }
  409. void colorband_update_sort(ColorBand *coba)
  410. {
  411. int a;
  412. if (coba->tot < 2)
  413. return;
  414. for (a = 0; a < coba->tot; a++)
  415. coba->data[a].cur = a;
  416. qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
  417. for (a = 0; a < coba->tot; a++) {
  418. if (coba->data[a].cur == coba->cur) {
  419. coba->cur = a;
  420. break;
  421. }
  422. }
  423. }
  424. CBData *colorband_element_add(struct ColorBand *coba, float position)
  425. {
  426. if (coba->tot == MAXCOLORBAND) {
  427. return NULL;
  428. }
  429. else {
  430. CBData *xnew;
  431. xnew = &coba->data[coba->tot];
  432. xnew->pos = position;
  433. if (coba->tot != 0) {
  434. do_colorband(coba, position, &xnew->r);
  435. }
  436. else {
  437. zero_v4(&xnew->r);
  438. }
  439. }
  440. coba->tot++;
  441. coba->cur = coba->tot - 1;
  442. colorband_update_sort(coba);
  443. return coba->data + coba->cur;
  444. }
  445. int colorband_element_remove(struct ColorBand *coba, int index)
  446. {
  447. int a;
  448. if (coba->tot < 2)
  449. return 0;
  450. if (index < 0 || index >= coba->tot)
  451. return 0;
  452. coba->tot--;
  453. for (a = index; a < coba->tot; a++) {
  454. coba->data[a] = coba->data[a + 1];
  455. }
  456. if (coba->cur) coba->cur--;
  457. return 1;
  458. }
  459. /* ******************* TEX ************************ */
  460. /** Free (or release) any data used by this texture (does not free the texure itself). */
  461. void BKE_texture_free(Tex *tex)
  462. {
  463. BKE_animdata_free((ID *)tex, false);
  464. /* is no lib link block, but texture extension */
  465. if (tex->nodetree) {
  466. ntreeFreeTree(tex->nodetree);
  467. MEM_freeN(tex->nodetree);
  468. tex->nodetree = NULL;
  469. }
  470. MEM_SAFE_FREE(tex->coba);
  471. if (tex->env) {
  472. BKE_texture_envmap_free(tex->env);
  473. tex->env = NULL;
  474. }
  475. if (tex->pd) {
  476. BKE_texture_pointdensity_free(tex->pd);
  477. tex->pd = NULL;
  478. }
  479. if (tex->vd) {
  480. BKE_texture_voxeldata_free(tex->vd);
  481. tex->vd = NULL;
  482. }
  483. if (tex->ot) {
  484. BKE_texture_ocean_free(tex->ot);
  485. tex->ot = NULL;
  486. }
  487. BKE_icon_id_delete((ID *)tex);
  488. BKE_previewimg_free(&tex->preview);
  489. }
  490. /* ------------------------------------------------------------------------- */
  491. void BKE_texture_default(Tex *tex)
  492. {
  493. /* BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(tex, id)); */ /* Not here, can be called with some pointers set. :/ */
  494. tex->type = TEX_IMAGE;
  495. tex->ima = NULL;
  496. tex->stype = 0;
  497. tex->flag = TEX_CHECKER_ODD;
  498. tex->imaflag = TEX_INTERPOL | TEX_MIPMAP | TEX_USEALPHA;
  499. tex->extend = TEX_REPEAT;
  500. tex->cropxmin = tex->cropymin = 0.0;
  501. tex->cropxmax = tex->cropymax = 1.0;
  502. tex->texfilter = TXF_EWA;
  503. tex->afmax = 8;
  504. tex->xrepeat = tex->yrepeat = 1;
  505. tex->fie_ima = 2;
  506. tex->sfra = 1;
  507. tex->frames = 0;
  508. tex->offset = 0;
  509. tex->noisesize = 0.25;
  510. tex->noisedepth = 2;
  511. tex->turbul = 5.0;
  512. tex->nabla = 0.025; // also in do_versions
  513. tex->bright = 1.0;
  514. tex->contrast = 1.0;
  515. tex->saturation = 1.0;
  516. tex->filtersize = 1.0;
  517. tex->rfac = 1.0;
  518. tex->gfac = 1.0;
  519. tex->bfac = 1.0;
  520. /* newnoise: init. */
  521. tex->noisebasis = 0;
  522. tex->noisebasis2 = 0;
  523. /* musgrave */
  524. tex->mg_H = 1.0;
  525. tex->mg_lacunarity = 2.0;
  526. tex->mg_octaves = 2.0;
  527. tex->mg_offset = 1.0;
  528. tex->mg_gain = 1.0;
  529. tex->ns_outscale = 1.0;
  530. /* distnoise */
  531. tex->dist_amount = 1.0;
  532. /* voronoi */
  533. tex->vn_w1 = 1.0;
  534. tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
  535. tex->vn_mexp = 2.5;
  536. tex->vn_distm = 0;
  537. tex->vn_coltype = 0;
  538. if (tex->env) {
  539. tex->env->stype = ENV_ANIM;
  540. tex->env->clipsta = 0.1;
  541. tex->env->clipend = 100;
  542. tex->env->cuberes = 512;
  543. tex->env->depth = 0;
  544. }
  545. if (tex->pd) {
  546. tex->pd->radius = 0.3f;
  547. tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
  548. }
  549. if (tex->vd) {
  550. tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
  551. tex->vd->interp_type = TEX_VD_LINEAR;
  552. tex->vd->file_format = TEX_VD_SMOKE;
  553. }
  554. if (tex->ot) {
  555. tex->ot->output = TEX_OCN_DISPLACEMENT;
  556. tex->ot->object = NULL;
  557. }
  558. tex->iuser.fie_ima = 2;
  559. tex->iuser.ok = 1;
  560. tex->iuser.frames = 100;
  561. tex->iuser.sfra = 1;
  562. tex->preview = NULL;
  563. }
  564. void BKE_texture_type_set(Tex *tex, int type)
  565. {
  566. switch (type) {
  567. case TEX_VOXELDATA:
  568. if (tex->vd == NULL)
  569. tex->vd = BKE_texture_voxeldata_add();
  570. break;
  571. case TEX_POINTDENSITY:
  572. if (tex->pd == NULL)
  573. tex->pd = BKE_texture_pointdensity_add();
  574. break;
  575. case TEX_ENVMAP:
  576. if (tex->env == NULL)
  577. tex->env = BKE_texture_envmap_add();
  578. break;
  579. case TEX_OCEAN:
  580. if (tex->ot == NULL)
  581. tex->ot = BKE_texture_ocean_add();
  582. break;
  583. }
  584. tex->type = type;
  585. }
  586. /* ------------------------------------------------------------------------- */
  587. Tex *BKE_texture_add(Main *bmain, const char *name)
  588. {
  589. Tex *tex;
  590. tex = BKE_libblock_alloc(bmain, ID_TE, name);
  591. BKE_texture_default(tex);
  592. return tex;
  593. }
  594. /* ------------------------------------------------------------------------- */
  595. void BKE_texture_mtex_default(MTex *mtex)
  596. {
  597. mtex->texco = TEXCO_UV;
  598. mtex->mapto = MAP_COL;
  599. mtex->object = NULL;
  600. mtex->projx = PROJ_X;
  601. mtex->projy = PROJ_Y;
  602. mtex->projz = PROJ_Z;
  603. mtex->mapping = MTEX_FLAT;
  604. mtex->ofs[0] = 0.0;
  605. mtex->ofs[1] = 0.0;
  606. mtex->ofs[2] = 0.0;
  607. mtex->size[0] = 1.0;
  608. mtex->size[1] = 1.0;
  609. mtex->size[2] = 1.0;
  610. mtex->tex = NULL;
  611. mtex->texflag = MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE | MTEX_MAPTO_BOUNDS;
  612. mtex->colormodel = 0;
  613. mtex->r = 1.0;
  614. mtex->g = 0.0;
  615. mtex->b = 1.0;
  616. mtex->k = 1.0;
  617. mtex->def_var = 1.0;
  618. mtex->blendtype = MTEX_BLEND;
  619. mtex->colfac = 1.0;
  620. mtex->norfac = 1.0;
  621. mtex->varfac = 1.0;
  622. mtex->dispfac = 0.2;
  623. mtex->colspecfac = 1.0f;
  624. mtex->mirrfac = 1.0f;
  625. mtex->alphafac = 1.0f;
  626. mtex->difffac = 1.0f;
  627. mtex->specfac = 1.0f;
  628. mtex->emitfac = 1.0f;
  629. mtex->hardfac = 1.0f;
  630. mtex->raymirrfac = 1.0f;
  631. mtex->translfac = 1.0f;
  632. mtex->ambfac = 1.0f;
  633. mtex->colemitfac = 1.0f;
  634. mtex->colreflfac = 1.0f;
  635. mtex->coltransfac = 1.0f;
  636. mtex->densfac = 1.0f;
  637. mtex->scatterfac = 1.0f;
  638. mtex->reflfac = 1.0f;
  639. mtex->shadowfac = 1.0f;
  640. mtex->zenupfac = 1.0f;
  641. mtex->zendownfac = 1.0f;
  642. mtex->blendfac = 1.0f;
  643. mtex->timefac = 1.0f;
  644. mtex->lengthfac = 1.0f;
  645. mtex->clumpfac = 1.0f;
  646. mtex->kinkfac = 1.0f;
  647. mtex->kinkampfac = 1.0f;
  648. mtex->roughfac = 1.0f;
  649. mtex->padensfac = 1.0f;
  650. mtex->lifefac = 1.0f;
  651. mtex->sizefac = 1.0f;
  652. mtex->ivelfac = 1.0f;
  653. mtex->dampfac = 1.0f;
  654. mtex->gravityfac = 1.0f;
  655. mtex->fieldfac = 1.0f;
  656. mtex->normapspace = MTEX_NSPACE_TANGENT;
  657. mtex->brush_map_mode = MTEX_MAP_MODE_TILED;
  658. mtex->random_angle = 2.0f * (float)M_PI;
  659. mtex->brush_angle_mode = 0;
  660. }
  661. /* ------------------------------------------------------------------------- */
  662. MTex *BKE_texture_mtex_add(void)
  663. {
  664. MTex *mtex;
  665. mtex = MEM_callocN(sizeof(MTex), "BKE_texture_mtex_add");
  666. BKE_texture_mtex_default(mtex);
  667. return mtex;
  668. }
  669. /* slot -1 for first free ID */
  670. MTex *BKE_texture_mtex_add_id(ID *id, int slot)
  671. {
  672. MTex **mtex_ar;
  673. short act;
  674. give_active_mtex(id, &mtex_ar, &act);
  675. if (mtex_ar == NULL) {
  676. return NULL;
  677. }
  678. if (slot == -1) {
  679. /* find first free */
  680. int i;
  681. for (i = 0; i < MAX_MTEX; i++) {
  682. if (!mtex_ar[i]) {
  683. slot = i;
  684. break;
  685. }
  686. }
  687. if (slot == -1) {
  688. return NULL;
  689. }
  690. }
  691. else {
  692. /* make sure slot is valid */
  693. if (slot < 0 || slot >= MAX_MTEX) {
  694. return NULL;
  695. }
  696. }
  697. if (mtex_ar[slot]) {
  698. id_us_min((ID *)mtex_ar[slot]->tex);
  699. MEM_freeN(mtex_ar[slot]);
  700. mtex_ar[slot] = NULL;
  701. }
  702. else if (GS(id->name) == ID_MA) {
  703. /* Reset this slot's ON/OFF toggle, for materials, when slot was empty. */
  704. ((Material *)id)->septex &= ~(1 << slot);
  705. }
  706. mtex_ar[slot] = BKE_texture_mtex_add();
  707. return mtex_ar[slot];
  708. }
  709. /* ------------------------------------------------------------------------- */
  710. Tex *BKE_texture_copy(Main *bmain, const Tex *tex)
  711. {
  712. Tex *texn;
  713. texn = BKE_libblock_copy(bmain, &tex->id);
  714. if (BKE_texture_is_image_user(tex)) {
  715. id_us_plus((ID *)texn->ima);
  716. }
  717. else {
  718. texn->ima = NULL;
  719. }
  720. if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
  721. if (texn->env) texn->env = BKE_texture_envmap_copy(texn->env);
  722. if (texn->pd) texn->pd = BKE_texture_pointdensity_copy(texn->pd);
  723. if (texn->vd) texn->vd = MEM_dupallocN(texn->vd);
  724. if (texn->ot) texn->ot = BKE_texture_ocean_copy(texn->ot);
  725. if (tex->nodetree) {
  726. if (tex->nodetree->execdata) {
  727. ntreeTexEndExecTree(tex->nodetree->execdata);
  728. }
  729. texn->nodetree = ntreeCopyTree(bmain, tex->nodetree);
  730. }
  731. BKE_previewimg_id_copy(&texn->id, &tex->id);
  732. BKE_id_copy_ensure_local(bmain, &tex->id, &texn->id);
  733. return texn;
  734. }
  735. /* texture copy without adding to main dbase */
  736. Tex *BKE_texture_localize(Tex *tex)
  737. {
  738. Tex *texn;
  739. texn = BKE_libblock_copy_nolib(&tex->id, false);
  740. /* image texture: BKE_texture_free also doesn't decrease */
  741. if (texn->coba) texn->coba = MEM_dupallocN(texn->coba);
  742. if (texn->env) {
  743. texn->env = BKE_texture_envmap_copy(texn->env);
  744. id_us_min(&texn->env->ima->id);
  745. }
  746. if (texn->pd) texn->pd = BKE_texture_pointdensity_copy(texn->pd);
  747. if (texn->vd) {
  748. texn->vd = MEM_dupallocN(texn->vd);
  749. if (texn->vd->dataset)
  750. texn->vd->dataset = MEM_dupallocN(texn->vd->dataset);
  751. }
  752. if (texn->ot) {
  753. texn->ot = BKE_texture_ocean_copy(tex->ot);
  754. }
  755. texn->preview = NULL;
  756. if (tex->nodetree) {
  757. texn->nodetree = ntreeLocalize(tex->nodetree);
  758. }
  759. return texn;
  760. }
  761. /* ------------------------------------------------------------------------- */
  762. void BKE_texture_make_local(Main *bmain, Tex *tex, const bool lib_local)
  763. {
  764. BKE_id_make_local_generic(bmain, &tex->id, true, lib_local);
  765. }
  766. Tex *give_current_object_texture(Object *ob)
  767. {
  768. Material *ma, *node_ma;
  769. Tex *tex = NULL;
  770. if (ob == NULL) return NULL;
  771. if (ob->totcol == 0 && !(ob->type == OB_LAMP)) return NULL;
  772. if (ob->type == OB_LAMP) {
  773. tex = give_current_lamp_texture(ob->data);
  774. }
  775. else {
  776. ma = give_current_material(ob, ob->actcol);
  777. if ((node_ma = give_node_material(ma)))
  778. ma = node_ma;
  779. tex = give_current_material_texture(ma);
  780. }
  781. return tex;
  782. }
  783. Tex *give_current_lamp_texture(Lamp *la)
  784. {
  785. MTex *mtex = NULL;
  786. Tex *tex = NULL;
  787. if (la) {
  788. mtex = la->mtex[(int)(la->texact)];
  789. if (mtex) tex = mtex->tex;
  790. }
  791. return tex;
  792. }
  793. void set_current_lamp_texture(Lamp *la, Tex *newtex)
  794. {
  795. int act = la->texact;
  796. if (la->mtex[act] && la->mtex[act]->tex)
  797. id_us_min(&la->mtex[act]->tex->id);
  798. if (newtex) {
  799. if (!la->mtex[act]) {
  800. la->mtex[act] = BKE_texture_mtex_add();
  801. la->mtex[act]->texco = TEXCO_GLOB;
  802. }
  803. la->mtex[act]->tex = newtex;
  804. id_us_plus(&newtex->id);
  805. }
  806. else if (la->mtex[act]) {
  807. MEM_freeN(la->mtex[act]);
  808. la->mtex[act] = NULL;
  809. }
  810. }
  811. Tex *give_current_linestyle_texture(FreestyleLineStyle *linestyle)
  812. {
  813. MTex *mtex = NULL;
  814. Tex *tex = NULL;
  815. if (linestyle) {
  816. mtex = linestyle->mtex[(int)(linestyle->texact)];
  817. if (mtex) tex = mtex->tex;
  818. }
  819. return tex;
  820. }
  821. void set_current_linestyle_texture(FreestyleLineStyle *linestyle, Tex *newtex)
  822. {
  823. int act = linestyle->texact;
  824. if (linestyle->mtex[act] && linestyle->mtex[act]->tex)
  825. id_us_min(&linestyle->mtex[act]->tex->id);
  826. if (newtex) {
  827. if (!linestyle->mtex[act]) {
  828. linestyle->mtex[act] = BKE_texture_mtex_add();
  829. linestyle->mtex[act]->texco = TEXCO_STROKE;
  830. }
  831. linestyle->mtex[act]->tex = newtex;
  832. id_us_plus(&newtex->id);
  833. }
  834. else if (linestyle->mtex[act]) {
  835. MEM_freeN(linestyle->mtex[act]);
  836. linestyle->mtex[act] = NULL;
  837. }
  838. }
  839. bNode *give_current_material_texture_node(Material *ma)
  840. {
  841. if (ma && ma->use_nodes && ma->nodetree)
  842. return nodeGetActiveID(ma->nodetree, ID_TE);
  843. return NULL;
  844. }
  845. Tex *give_current_material_texture(Material *ma)
  846. {
  847. MTex *mtex = NULL;
  848. Tex *tex = NULL;
  849. bNode *node;
  850. if (ma && ma->use_nodes && ma->nodetree) {
  851. /* first check texture, then material, this works together
  852. * with a hack that clears the active ID flag for textures on
  853. * making a material node active */
  854. node = nodeGetActiveID(ma->nodetree, ID_TE);
  855. if (node) {
  856. tex = (Tex *)node->id;
  857. ma = NULL;
  858. }
  859. }
  860. if (ma) {
  861. mtex = ma->mtex[(int)(ma->texact)];
  862. if (mtex) tex = mtex->tex;
  863. }
  864. return tex;
  865. }
  866. bool give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
  867. {
  868. switch (GS(id->name)) {
  869. case ID_MA:
  870. *mtex_ar = ((Material *)id)->mtex;
  871. if (act) *act = (((Material *)id)->texact);
  872. break;
  873. case ID_WO:
  874. *mtex_ar = ((World *)id)->mtex;
  875. if (act) *act = (((World *)id)->texact);
  876. break;
  877. case ID_LA:
  878. *mtex_ar = ((Lamp *)id)->mtex;
  879. if (act) *act = (((Lamp *)id)->texact);
  880. break;
  881. case ID_LS:
  882. *mtex_ar = ((FreestyleLineStyle *)id)->mtex;
  883. if (act) *act = (((FreestyleLineStyle *)id)->texact);
  884. break;
  885. case ID_PA:
  886. *mtex_ar = ((ParticleSettings *)id)->mtex;
  887. if (act) *act = (((ParticleSettings *)id)->texact);
  888. break;
  889. default:
  890. *mtex_ar = NULL;
  891. if (act) *act = 0;
  892. return false;
  893. }
  894. return true;
  895. }
  896. void set_active_mtex(ID *id, short act)
  897. {
  898. if (act < 0) act = 0;
  899. else if (act >= MAX_MTEX) act = MAX_MTEX - 1;
  900. switch (GS(id->name)) {
  901. case ID_MA:
  902. ((Material *)id)->texact = act;
  903. break;
  904. case ID_WO:
  905. ((World *)id)->texact = act;
  906. break;
  907. case ID_LA:
  908. ((Lamp *)id)->texact = act;
  909. break;
  910. case ID_LS:
  911. ((FreestyleLineStyle *)id)->texact = act;
  912. break;
  913. case ID_PA:
  914. ((ParticleSettings *)id)->texact = act;
  915. break;
  916. }
  917. }
  918. void set_current_material_texture(Material *ma, Tex *newtex)
  919. {
  920. Tex *tex = NULL;
  921. bNode *node;
  922. if ((ma->use_nodes && ma->nodetree) &&
  923. (node = nodeGetActiveID(ma->nodetree, ID_TE)))
  924. {
  925. tex = (Tex *)node->id;
  926. id_us_min(&tex->id);
  927. if (newtex) {
  928. node->id = &newtex->id;
  929. id_us_plus(&newtex->id);
  930. }
  931. else {
  932. node->id = NULL;
  933. }
  934. }
  935. else {
  936. int act = (int)ma->texact;
  937. tex = (ma->mtex[act]) ? ma->mtex[act]->tex : NULL;
  938. id_us_min(&tex->id);
  939. if (newtex) {
  940. if (!ma->mtex[act]) {
  941. ma->mtex[act] = BKE_texture_mtex_add();
  942. /* Reset this slot's ON/OFF toggle, for materials, when slot was empty. */
  943. ma->septex &= ~(1 << act);
  944. /* For volumes the default UV texture coordinates are not available. */
  945. if (ma->material_type == MA_TYPE_VOLUME) {
  946. ma->mtex[act]->texco = TEXCO_ORCO;
  947. }
  948. }
  949. ma->mtex[act]->tex = newtex;
  950. id_us_plus(&newtex->id);
  951. }
  952. else if (ma->mtex[act]) {
  953. MEM_freeN(ma->mtex[act]);
  954. ma->mtex[act] = NULL;
  955. }
  956. }
  957. }
  958. bool has_current_material_texture(Material *ma)
  959. {
  960. bNode *node;
  961. if (ma && ma->use_nodes && ma->nodetree) {
  962. node = nodeGetActiveID(ma->nodetree, ID_TE);
  963. if (node)
  964. return true;
  965. }
  966. return (ma != NULL);
  967. }
  968. Tex *give_current_world_texture(World *world)
  969. {
  970. MTex *mtex = NULL;
  971. Tex *tex = NULL;
  972. if (!world) return NULL;
  973. mtex = world->mtex[(int)(world->texact)];
  974. if (mtex) tex = mtex->tex;
  975. return tex;
  976. }
  977. void set_current_world_texture(World *wo, Tex *newtex)
  978. {
  979. int act = wo->texact;
  980. if (wo->mtex[act] && wo->mtex[act]->tex)
  981. id_us_min(&wo->mtex[act]->tex->id);
  982. if (newtex) {
  983. if (!wo->mtex[act]) {
  984. wo->mtex[act] = BKE_texture_mtex_add();
  985. wo->mtex[act]->texco = TEXCO_VIEW;
  986. }
  987. wo->mtex[act]->tex = newtex;
  988. id_us_plus(&newtex->id);
  989. }
  990. else if (wo->mtex[act]) {
  991. MEM_freeN(wo->mtex[act]);
  992. wo->mtex[act] = NULL;
  993. }
  994. }
  995. Tex *give_current_brush_texture(Brush *br)
  996. {
  997. return br->mtex.tex;
  998. }
  999. void set_current_brush_texture(Brush *br, Tex *newtex)
  1000. {
  1001. if (br->mtex.tex)
  1002. id_us_min(&br->mtex.tex->id);
  1003. if (newtex) {
  1004. br->mtex.tex = newtex;
  1005. id_us_plus(&newtex->id);
  1006. }
  1007. }
  1008. Tex *give_current_particle_texture(ParticleSettings *part)
  1009. {
  1010. MTex *mtex = NULL;
  1011. Tex *tex = NULL;
  1012. if (!part) return NULL;
  1013. mtex = part->mtex[(int)(part->texact)];
  1014. if (mtex) tex = mtex->tex;
  1015. return tex;
  1016. }
  1017. void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
  1018. {
  1019. int act = part->texact;
  1020. if (part->mtex[act] && part->mtex[act]->tex)
  1021. id_us_min(&part->mtex[act]->tex->id);
  1022. if (newtex) {
  1023. if (!part->mtex[act]) {
  1024. part->mtex[act] = BKE_texture_mtex_add();
  1025. part->mtex[act]->texco = TEXCO_ORCO;
  1026. part->mtex[act]->blendtype = MTEX_MUL;
  1027. }
  1028. part->mtex[act]->tex = newtex;
  1029. id_us_plus(&newtex->id);
  1030. }
  1031. else if (part->mtex[act]) {
  1032. MEM_freeN(part->mtex[act]);
  1033. part->mtex[act] = NULL;
  1034. }
  1035. }
  1036. /* ------------------------------------------------------------------------- */
  1037. EnvMap *BKE_texture_envmap_add(void)
  1038. {
  1039. EnvMap *env;
  1040. env = MEM_callocN(sizeof(EnvMap), "envmap");
  1041. env->type = ENV_CUBE;
  1042. env->stype = ENV_ANIM;
  1043. env->clipsta = 0.1;
  1044. env->clipend = 100.0;
  1045. env->cuberes = 512;
  1046. env->viewscale = 0.5;
  1047. return env;
  1048. }
  1049. /* ------------------------------------------------------------------------- */
  1050. EnvMap *BKE_texture_envmap_copy(const EnvMap *env)
  1051. {
  1052. EnvMap *envn;
  1053. int a;
  1054. envn = MEM_dupallocN(env);
  1055. envn->ok = 0;
  1056. for (a = 0; a < 6; a++) envn->cube[a] = NULL;
  1057. if (envn->ima) id_us_plus((ID *)envn->ima);
  1058. return envn;
  1059. }
  1060. /* ------------------------------------------------------------------------- */
  1061. void BKE_texture_envmap_free_data(EnvMap *env)
  1062. {
  1063. unsigned int part;
  1064. for (part = 0; part < 6; part++) {
  1065. if (env->cube[part])
  1066. IMB_freeImBuf(env->cube[part]);
  1067. env->cube[part] = NULL;
  1068. }
  1069. env->ok = 0;
  1070. }
  1071. /* ------------------------------------------------------------------------- */
  1072. void BKE_texture_envmap_free(EnvMap *env)
  1073. {
  1074. BKE_texture_envmap_free_data(env);
  1075. MEM_freeN(env);
  1076. }
  1077. /* ------------------------------------------------------------------------- */
  1078. void BKE_texture_pointdensity_init_data(PointDensity *pd)
  1079. {
  1080. pd->flag = 0;
  1081. pd->radius = 0.3f;
  1082. pd->falloff_type = TEX_PD_FALLOFF_STD;
  1083. pd->falloff_softness = 2.0;
  1084. pd->source = TEX_PD_PSYS;
  1085. pd->point_tree = NULL;
  1086. pd->point_data = NULL;
  1087. pd->noise_size = 0.5f;
  1088. pd->noise_depth = 1;
  1089. pd->noise_fac = 1.0f;
  1090. pd->noise_influence = TEX_PD_NOISE_STATIC;
  1091. pd->coba = add_colorband(true);
  1092. pd->speed_scale = 1.0f;
  1093. pd->totpoints = 0;
  1094. pd->object = NULL;
  1095. pd->psys = 0;
  1096. pd->psys_cache_space = TEX_PD_WORLDSPACE;
  1097. pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
  1098. pd->falloff_curve->preset = CURVE_PRESET_LINE;
  1099. pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
  1100. curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
  1101. curvemapping_changed(pd->falloff_curve, false);
  1102. }
  1103. PointDensity *BKE_texture_pointdensity_add(void)
  1104. {
  1105. PointDensity *pd = MEM_callocN(sizeof(PointDensity), "pointdensity");
  1106. BKE_texture_pointdensity_init_data(pd);
  1107. return pd;
  1108. }
  1109. PointDensity *BKE_texture_pointdensity_copy(const PointDensity *pd)
  1110. {
  1111. PointDensity *pdn;
  1112. pdn = MEM_dupallocN(pd);
  1113. pdn->point_tree = NULL;
  1114. pdn->point_data = NULL;
  1115. if (pdn->coba) pdn->coba = MEM_dupallocN(pdn->coba);
  1116. pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
  1117. return pdn;
  1118. }
  1119. void BKE_texture_pointdensity_free_data(PointDensity *pd)
  1120. {
  1121. if (pd->point_tree) {
  1122. BLI_bvhtree_free(pd->point_tree);
  1123. pd->point_tree = NULL;
  1124. }
  1125. if (pd->point_data) {
  1126. MEM_freeN(pd->point_data);
  1127. pd->point_data = NULL;
  1128. }
  1129. if (pd->coba) {
  1130. MEM_freeN(pd->coba);
  1131. pd->coba = NULL;
  1132. }
  1133. curvemapping_free(pd->falloff_curve); /* can be NULL */
  1134. }
  1135. void BKE_texture_pointdensity_free(PointDensity *pd)
  1136. {
  1137. BKE_texture_pointdensity_free_data(pd);
  1138. MEM_freeN(pd);
  1139. }
  1140. /* ------------------------------------------------------------------------- */
  1141. void BKE_texture_voxeldata_free_data(VoxelData *vd)
  1142. {
  1143. if (vd->dataset) {
  1144. MEM_freeN(vd->dataset);
  1145. vd->dataset = NULL;
  1146. }
  1147. }
  1148. void BKE_texture_voxeldata_free(VoxelData *vd)
  1149. {
  1150. BKE_texture_voxeldata_free_data(vd);
  1151. MEM_freeN(vd);
  1152. }
  1153. VoxelData *BKE_texture_voxeldata_add(void)
  1154. {
  1155. VoxelData *vd;
  1156. vd = MEM_callocN(sizeof(VoxelData), "voxeldata");
  1157. vd->dataset = NULL;
  1158. vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
  1159. vd->interp_type = TEX_VD_LINEAR;
  1160. vd->file_format = TEX_VD_SMOKE;
  1161. vd->int_multiplier = 1.0;
  1162. vd->extend = TEX_CLIP;
  1163. vd->object = NULL;
  1164. vd->cachedframe = -1;
  1165. vd->ok = 0;
  1166. return vd;
  1167. }
  1168. VoxelData *BKE_texture_voxeldata_copy(VoxelData *vd)
  1169. {
  1170. VoxelData *vdn;
  1171. vdn = MEM_dupallocN(vd);
  1172. vdn->dataset = NULL;
  1173. return vdn;
  1174. }
  1175. /* ------------------------------------------------------------------------- */
  1176. OceanTex *BKE_texture_ocean_add(void)
  1177. {
  1178. OceanTex *ot;
  1179. ot = MEM_callocN(sizeof(struct OceanTex), "ocean texture");
  1180. ot->output = TEX_OCN_DISPLACEMENT;
  1181. ot->object = NULL;
  1182. return ot;
  1183. }
  1184. OceanTex *BKE_texture_ocean_copy(const OceanTex *ot)
  1185. {
  1186. OceanTex *otn = MEM_dupallocN(ot);
  1187. return otn;
  1188. }
  1189. void BKE_texture_ocean_free(struct OceanTex *ot)
  1190. {
  1191. MEM_freeN(ot);
  1192. }
  1193. /**
  1194. * \returns true if this texture can use its #Texture.ima (even if its NULL)
  1195. */
  1196. bool BKE_texture_is_image_user(const struct Tex *tex)
  1197. {
  1198. switch (tex->type) {
  1199. case TEX_IMAGE:
  1200. {
  1201. return true;
  1202. }
  1203. case TEX_ENVMAP:
  1204. {
  1205. if (tex->env) {
  1206. if (tex->env->stype == ENV_LOAD) {
  1207. return true;
  1208. }
  1209. }
  1210. break;
  1211. }
  1212. }
  1213. return false;
  1214. }
  1215. /* ------------------------------------------------------------------------- */
  1216. bool BKE_texture_dependsOnTime(const struct Tex *texture)
  1217. {
  1218. if (texture->ima && BKE_image_is_animated(texture->ima)) {
  1219. return true;
  1220. }
  1221. else if (texture->adt) {
  1222. /* assume anything in adt means the texture is animated */
  1223. return true;
  1224. }
  1225. else if (texture->type == TEX_NOISE) {
  1226. /* noise always varies with time */
  1227. return true;
  1228. }
  1229. return false;
  1230. }
  1231. /* ------------------------------------------------------------------------- */
  1232. void BKE_texture_get_value_ex(
  1233. const Scene *scene, Tex *texture,
  1234. float *tex_co, TexResult *texres,
  1235. struct ImagePool *pool,
  1236. bool use_color_management)
  1237. {
  1238. int result_type;
  1239. bool do_color_manage = false;
  1240. if (scene && use_color_management) {
  1241. do_color_manage = BKE_scene_check_color_management_enabled(scene);
  1242. }
  1243. /* no node textures for now */
  1244. result_type = multitex_ext_safe(texture, tex_co, texres, pool, do_color_manage, false);
  1245. /* if the texture gave an RGB value, we assume it didn't give a valid
  1246. * intensity, since this is in the context of modifiers don't use perceptual color conversion.
  1247. * if the texture didn't give an RGB value, copy the intensity across
  1248. */
  1249. if (result_type & TEX_RGB) {
  1250. texres->tin = (1.0f / 3.0f) * (texres->tr + texres->tg + texres->tb);
  1251. }
  1252. else {
  1253. copy_v3_fl(&texres->tr, texres->tin);
  1254. }
  1255. }
  1256. void BKE_texture_get_value(
  1257. const Scene *scene, Tex *texture,
  1258. float *tex_co, TexResult *texres, bool use_color_management)
  1259. {
  1260. BKE_texture_get_value_ex(scene, texture, tex_co, texres, NULL, use_color_management);
  1261. }
  1262. static void texture_nodes_fetch_images_for_pool(Tex *texture, bNodeTree *ntree, struct ImagePool *pool)
  1263. {
  1264. for (bNode *node = ntree->nodes.first; node; node = node->next) {
  1265. if (node->type == SH_NODE_TEX_IMAGE && node->id != NULL) {
  1266. Image *image = (Image *)node->id;
  1267. BKE_image_pool_acquire_ibuf(image, &texture->iuser, pool);
  1268. }
  1269. else if (node->type == NODE_GROUP && node->id != NULL) {
  1270. /* TODO(sergey): Do we need to control recursion here? */
  1271. bNodeTree *nested_tree = (bNodeTree *)node->id;
  1272. texture_nodes_fetch_images_for_pool(texture, nested_tree, pool);
  1273. }
  1274. }
  1275. }
  1276. /* Make sure all images used by texture are loaded into pool. */
  1277. void BKE_texture_fetch_images_for_pool(Tex *texture, struct ImagePool *pool)
  1278. {
  1279. if (texture->nodetree != NULL) {
  1280. texture_nodes_fetch_images_for_pool(texture, texture->nodetree, pool);
  1281. }
  1282. else {
  1283. if (texture->type == TEX_IMAGE) {
  1284. if (texture->ima != NULL) {
  1285. BKE_image_pool_acquire_ibuf(texture->ima, &texture->iuser, pool);
  1286. }
  1287. }
  1288. }
  1289. }