texture.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*************************************************************************/
  2. /* texture.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEXTURE_H
  31. #define TEXTURE_H
  32. #include "curve.h"
  33. #include "io/resource_loader.h"
  34. #include "math_2d.h"
  35. #include "resource.h"
  36. #include "scene/resources/color_ramp.h"
  37. #include "servers/visual_server.h"
  38. /**
  39. @author Juan Linietsky <reduzio@gmail.com>
  40. */
  41. class Texture : public Resource {
  42. GDCLASS(Texture, Resource);
  43. OBJ_SAVE_TYPE(Texture); //children are all saved as Texture, so they can be exchanged
  44. protected:
  45. static void _bind_methods();
  46. public:
  47. enum Flags {
  48. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  49. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  50. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  51. FLAG_ANISOTROPIC_FILTER = VisualServer::TEXTURE_FLAG_ANISOTROPIC_FILTER,
  52. FLAG_CONVERT_TO_LINEAR = VisualServer::TEXTURE_FLAG_CONVERT_TO_LINEAR,
  53. FLAG_VIDEO_SURFACE = VisualServer::TEXTURE_FLAG_USED_FOR_STREAMING,
  54. FLAGS_DEFAULT = FLAG_MIPMAPS | FLAG_REPEAT | FLAG_FILTER,
  55. FLAG_MIRRORED_REPEAT = VisualServer::TEXTURE_FLAG_MIRRORED_REPEAT
  56. };
  57. virtual int get_width() const = 0;
  58. virtual int get_height() const = 0;
  59. virtual Size2 get_size() const;
  60. virtual RID get_rid() const = 0;
  61. virtual bool has_alpha() const = 0;
  62. virtual void set_flags(uint32_t p_flags) = 0;
  63. virtual uint32_t get_flags() const = 0;
  64. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  65. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  66. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  67. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  68. virtual Ref<Image> get_data() const { return Ref<Image>(); }
  69. Texture();
  70. };
  71. VARIANT_ENUM_CAST(Texture::Flags);
  72. class ImageTexture : public Texture {
  73. GDCLASS(ImageTexture, Texture);
  74. RES_BASE_EXTENSION("tex");
  75. public:
  76. enum Storage {
  77. STORAGE_RAW,
  78. STORAGE_COMPRESS_LOSSY,
  79. STORAGE_COMPRESS_LOSSLESS
  80. };
  81. private:
  82. RID texture;
  83. Image::Format format;
  84. uint32_t flags;
  85. int w, h;
  86. Storage storage;
  87. Size2 size_override;
  88. float lossy_storage_quality;
  89. protected:
  90. virtual void reload_from_file();
  91. bool _set(const StringName &p_name, const Variant &p_value);
  92. bool _get(const StringName &p_name, Variant &r_ret) const;
  93. void _get_property_list(List<PropertyInfo> *p_list) const;
  94. void _reload_hook(const RID &p_hook);
  95. virtual void _resource_path_changed();
  96. static void _bind_methods();
  97. void _set_data(Dictionary p_data);
  98. public:
  99. void create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT);
  100. void create_from_image(const Ref<Image> &p_image, uint32_t p_flags = FLAGS_DEFAULT);
  101. void set_flags(uint32_t p_flags);
  102. uint32_t get_flags() const;
  103. Image::Format get_format() const;
  104. void load(const String &p_path);
  105. void set_data(const Ref<Image> &p_image);
  106. Ref<Image> get_data() const;
  107. int get_width() const;
  108. int get_height() const;
  109. virtual RID get_rid() const;
  110. bool has_alpha() const;
  111. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  112. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  113. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  114. void set_storage(Storage p_storage);
  115. Storage get_storage() const;
  116. void set_lossy_storage_quality(float p_lossy_storage_quality);
  117. float get_lossy_storage_quality() const;
  118. void set_size_override(const Size2 &p_size);
  119. virtual void set_path(const String &p_path, bool p_take_over = false);
  120. ImageTexture();
  121. ~ImageTexture();
  122. };
  123. class StreamTexture : public Texture {
  124. GDCLASS(StreamTexture, Texture);
  125. public:
  126. enum DataFormat {
  127. DATA_FORMAT_IMAGE,
  128. DATA_FORMAT_LOSSLESS,
  129. DATA_FORMAT_LOSSY
  130. };
  131. enum FormatBits {
  132. FORMAT_MASK_IMAGE_FORMAT = (1 << 20) - 1,
  133. FORMAT_BIT_LOSSLESS = 1 << 20,
  134. FORMAT_BIT_LOSSY = 1 << 21,
  135. FORMAT_BIT_STREAM = 1 << 22,
  136. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  137. FORMAT_BIT_DETECT_3D = 1 << 24,
  138. FORMAT_BIT_DETECT_SRGB = 1 << 25,
  139. FORMAT_BIT_DETECT_NORMAL = 1 << 26,
  140. };
  141. private:
  142. Error _load_data(const String &p_path, int &tw, int &th, int &flags, Ref<Image> &image, int p_size_limit = 0);
  143. String path_to_file;
  144. RID texture;
  145. Image::Format format;
  146. uint32_t flags;
  147. int w, h;
  148. virtual void reload_from_file();
  149. static void _requested_3d(void *p_ud);
  150. static void _requested_srgb(void *p_ud);
  151. static void _requested_normal(void *p_ud);
  152. protected:
  153. static void _bind_methods();
  154. public:
  155. typedef void (*TextureFormatRequestCallback)(const Ref<StreamTexture> &);
  156. static TextureFormatRequestCallback request_3d_callback;
  157. static TextureFormatRequestCallback request_srgb_callback;
  158. static TextureFormatRequestCallback request_normal_callback;
  159. uint32_t get_flags() const;
  160. Image::Format get_format() const;
  161. Error load(const String &p_path);
  162. String get_load_path() const;
  163. int get_width() const;
  164. int get_height() const;
  165. virtual RID get_rid() const;
  166. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  167. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  168. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  169. virtual bool has_alpha() const;
  170. virtual void set_flags(uint32_t p_flags);
  171. virtual Ref<Image> get_data() const;
  172. StreamTexture();
  173. ~StreamTexture();
  174. };
  175. class ResourceFormatLoaderStreamTexture : public ResourceFormatLoader {
  176. public:
  177. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  178. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  179. virtual bool handles_type(const String &p_type) const;
  180. virtual String get_resource_type(const String &p_path) const;
  181. };
  182. VARIANT_ENUM_CAST(ImageTexture::Storage);
  183. class AtlasTexture : public Texture {
  184. GDCLASS(AtlasTexture, Texture);
  185. RES_BASE_EXTENSION("atlastex");
  186. protected:
  187. Ref<Texture> atlas;
  188. Rect2 region;
  189. Rect2 margin;
  190. bool filter_clip;
  191. static void _bind_methods();
  192. public:
  193. virtual int get_width() const;
  194. virtual int get_height() const;
  195. virtual RID get_rid() const;
  196. virtual bool has_alpha() const;
  197. virtual void set_flags(uint32_t p_flags);
  198. virtual uint32_t get_flags() const;
  199. void set_atlas(const Ref<Texture> &p_atlas);
  200. Ref<Texture> get_atlas() const;
  201. void set_region(const Rect2 &p_region);
  202. Rect2 get_region() const;
  203. void set_margin(const Rect2 &p_margin);
  204. Rect2 get_margin() const;
  205. void set_filter_clip(const bool p_enable);
  206. bool has_filter_clip() const;
  207. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  208. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  209. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  210. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  211. AtlasTexture();
  212. };
  213. class LargeTexture : public Texture {
  214. GDCLASS(LargeTexture, Texture);
  215. RES_BASE_EXTENSION("largetex");
  216. protected:
  217. struct Piece {
  218. Point2 offset;
  219. Ref<Texture> texture;
  220. };
  221. Vector<Piece> pieces;
  222. Size2i size;
  223. Array _get_data() const;
  224. void _set_data(const Array &p_array);
  225. static void _bind_methods();
  226. public:
  227. virtual int get_width() const;
  228. virtual int get_height() const;
  229. virtual RID get_rid() const;
  230. virtual bool has_alpha() const;
  231. virtual void set_flags(uint32_t p_flags);
  232. virtual uint32_t get_flags() const;
  233. int add_piece(const Point2 &p_offset, const Ref<Texture> &p_texture);
  234. void set_piece_offset(int p_idx, const Point2 &p_offset);
  235. void set_piece_texture(int p_idx, const Ref<Texture> &p_texture);
  236. void set_size(const Size2 &p_size);
  237. void clear();
  238. int get_piece_count() const;
  239. Vector2 get_piece_offset(int p_idx) const;
  240. Ref<Texture> get_piece_texture(int p_idx) const;
  241. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  242. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  243. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  244. LargeTexture();
  245. };
  246. class CubeMap : public Resource {
  247. GDCLASS(CubeMap, Resource);
  248. RES_BASE_EXTENSION("cubemap");
  249. public:
  250. enum Storage {
  251. STORAGE_RAW,
  252. STORAGE_COMPRESS_LOSSY,
  253. STORAGE_COMPRESS_LOSSLESS
  254. };
  255. enum Side {
  256. SIDE_LEFT,
  257. SIDE_RIGHT,
  258. SIDE_BOTTOM,
  259. SIDE_TOP,
  260. SIDE_FRONT,
  261. SIDE_BACK
  262. };
  263. enum Flags {
  264. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  265. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  266. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  267. FLAGS_DEFAULT = FLAG_MIPMAPS | FLAG_REPEAT | FLAG_FILTER,
  268. };
  269. private:
  270. bool valid[6];
  271. RID cubemap;
  272. Image::Format format;
  273. uint32_t flags;
  274. int w, h;
  275. Storage storage;
  276. Size2 size_override;
  277. float lossy_storage_quality;
  278. _FORCE_INLINE_ bool _is_valid() const {
  279. for (int i = 0; i < 6; i++) {
  280. if (valid[i]) return true;
  281. }
  282. return false;
  283. }
  284. protected:
  285. bool _set(const StringName &p_name, const Variant &p_value);
  286. bool _get(const StringName &p_name, Variant &r_ret) const;
  287. void _get_property_list(List<PropertyInfo> *p_list) const;
  288. static void _bind_methods();
  289. public:
  290. void set_flags(uint32_t p_flags);
  291. uint32_t get_flags() const;
  292. void set_side(Side p_side, const Ref<Image> &p_image);
  293. Ref<Image> get_side(Side p_side) const;
  294. Image::Format get_format() const;
  295. int get_width() const;
  296. int get_height() const;
  297. virtual RID get_rid() const;
  298. void set_storage(Storage p_storage);
  299. Storage get_storage() const;
  300. void set_lossy_storage_quality(float p_lossy_storage_quality);
  301. float get_lossy_storage_quality() const;
  302. virtual void set_path(const String &p_path, bool p_take_over = false);
  303. CubeMap();
  304. ~CubeMap();
  305. };
  306. VARIANT_ENUM_CAST(CubeMap::Flags)
  307. VARIANT_ENUM_CAST(CubeMap::Side)
  308. VARIANT_ENUM_CAST(CubeMap::Storage)
  309. class CurveTexture : public Texture {
  310. GDCLASS(CurveTexture, Texture)
  311. RES_BASE_EXTENSION("curvetex")
  312. private:
  313. RID _texture;
  314. Ref<Curve> _curve;
  315. int _width;
  316. void _update();
  317. protected:
  318. static void _bind_methods();
  319. public:
  320. void set_width(int p_width);
  321. int get_width() const;
  322. void ensure_default_setup(float p_min = 0, float p_max = 1);
  323. void set_curve(Ref<Curve> p_curve);
  324. Ref<Curve> get_curve() const;
  325. virtual RID get_rid() const;
  326. virtual int get_height() const { return 1; }
  327. virtual bool has_alpha() const { return false; }
  328. virtual void set_flags(uint32_t p_flags) {}
  329. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  330. CurveTexture();
  331. ~CurveTexture();
  332. };
  333. /*
  334. enum CubeMapSide {
  335. CUBEMAP_LEFT,
  336. CUBEMAP_RIGHT,
  337. CUBEMAP_BOTTOM,
  338. CUBEMAP_TOP,
  339. CUBEMAP_FRONT,
  340. CUBEMAP_BACK,
  341. };
  342. */
  343. //VARIANT_ENUM_CAST( Texture::CubeMapSide );
  344. class GradientTexture : public Texture {
  345. GDCLASS(GradientTexture, Texture)
  346. public:
  347. struct Point {
  348. float offset;
  349. Color color;
  350. bool operator<(const Point &p_ponit) const {
  351. return offset < p_ponit.offset;
  352. }
  353. };
  354. private:
  355. Ref<Gradient> gradient;
  356. bool update_pending;
  357. RID texture;
  358. int width;
  359. void _queue_update();
  360. void _update();
  361. protected:
  362. static void _bind_methods();
  363. public:
  364. void set_gradient(Ref<Gradient> p_gradient);
  365. Ref<Gradient> get_gradient() const;
  366. void set_width(int p_width);
  367. int get_width() const;
  368. virtual RID get_rid() const { return texture; }
  369. virtual int get_height() const { return 1; }
  370. virtual bool has_alpha() const { return true; }
  371. virtual void set_flags(uint32_t p_flags) {}
  372. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  373. virtual Ref<Image> get_data() const;
  374. GradientTexture();
  375. virtual ~GradientTexture();
  376. };
  377. #endif