texture.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*************************************************************************/
  2. /* texture.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef TEXTURE_H
  30. #define TEXTURE_H
  31. #include "resource.h"
  32. #include "servers/visual_server.h"
  33. #include "math_2d.h"
  34. /**
  35. @author Juan Linietsky <[email protected]>
  36. */
  37. class Texture : public Resource {
  38. OBJ_TYPE( Texture, Resource );
  39. OBJ_SAVE_TYPE( Texture ); //children are all saved as Texture, so they can be exchanged
  40. protected:
  41. static void _bind_methods();
  42. public:
  43. enum Flags {
  44. FLAG_MIPMAPS=VisualServer::TEXTURE_FLAG_MIPMAPS,
  45. FLAG_REPEAT=VisualServer::TEXTURE_FLAG_REPEAT,
  46. FLAG_FILTER=VisualServer::TEXTURE_FLAG_FILTER,
  47. FLAG_VIDEO_SURFACE=VisualServer::TEXTURE_FLAG_VIDEO_SURFACE,
  48. FLAGS_DEFAULT=FLAG_MIPMAPS|FLAG_REPEAT|FLAG_FILTER,
  49. };
  50. virtual int get_width() const=0;
  51. virtual int get_height() const=0;
  52. virtual Size2 get_size() const;
  53. virtual RID get_rid() const=0;
  54. virtual bool has_alpha() const=0;
  55. virtual void set_flags(uint32_t p_flags)=0;
  56. virtual uint32_t get_flags() const=0;
  57. virtual void draw(RID p_canvas_item, const Point2& p_pos, const Color& p_modulate=Color(1,1,1)) const;
  58. virtual void draw_rect(RID p_canvas_item,const Rect2& p_rect, bool p_tile=false,const Color& p_modulate=Color(1,1,1)) const;
  59. 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)) const;
  60. virtual bool get_rect_region(const Rect2& p_rect, const Rect2& p_src_rect,Rect2& r_rect,Rect2& r_src_rect) const;
  61. Texture();
  62. };
  63. VARIANT_ENUM_CAST( Texture::Flags );
  64. class ImageTexture : public Texture {
  65. OBJ_TYPE( ImageTexture, Texture );
  66. RES_BASE_EXTENSION("tex");
  67. public:
  68. enum Storage {
  69. STORAGE_RAW,
  70. STORAGE_COMPRESS_LOSSY,
  71. STORAGE_COMPRESS_LOSSLESS
  72. };
  73. private:
  74. RID texture;
  75. Image::Format format;
  76. uint32_t flags;
  77. int w,h;
  78. Storage storage;
  79. Size2 size_override;
  80. float lossy_storage_quality;
  81. protected:
  82. virtual bool can_reload_from_file();
  83. virtual void reload_from_file();
  84. bool _set(const StringName& p_name, const Variant& p_value);
  85. bool _get(const StringName& p_name,Variant &r_ret) const;
  86. void _get_property_list( List<PropertyInfo> *p_list) const;
  87. void _reload_hook(const RID& p_hook);
  88. virtual void _resource_path_changed();
  89. static void _bind_methods();
  90. void _set_data(Dictionary p_data);
  91. public:
  92. void create(int p_width, int p_height,Image::Format p_format,uint32_t p_flags=FLAGS_DEFAULT);
  93. void create_from_image(const Image& p_image, uint32_t p_flags=FLAGS_DEFAULT);
  94. void set_flags(uint32_t p_flags);
  95. uint32_t get_flags() const;
  96. Image::Format get_format() const;
  97. void load(const String& p_path);
  98. void set_data(const Image& p_image);
  99. Image get_data() const;
  100. int get_width() const;
  101. int get_height() const;
  102. virtual RID get_rid() const;
  103. bool has_alpha() const;
  104. virtual void draw(RID p_canvas_item, const Point2& p_pos, const Color& p_modulate=Color(1,1,1)) const;
  105. virtual void draw_rect(RID p_canvas_item,const Rect2& p_rect, bool p_tile=false,const Color& p_modulate=Color(1,1,1)) const;
  106. 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)) const;
  107. void set_storage(Storage p_storage);
  108. Storage get_storage() const;
  109. void set_lossy_storage_quality(float p_lossy_storage_quality);
  110. float get_lossy_storage_quality() const;
  111. void fix_alpha_edges();
  112. void premultiply_alpha();
  113. void set_size_override(const Size2& p_size);
  114. ImageTexture();
  115. ~ImageTexture();
  116. };
  117. VARIANT_ENUM_CAST( ImageTexture::Storage );
  118. class AtlasTexture : public Texture {
  119. OBJ_TYPE( AtlasTexture, Texture );
  120. RES_BASE_EXTENSION("atex");
  121. protected:
  122. Ref<Texture> atlas;
  123. Rect2 region;
  124. Rect2 margin;
  125. static void _bind_methods();
  126. public:
  127. virtual int get_width() const;
  128. virtual int get_height() const;
  129. virtual RID get_rid() const;
  130. virtual bool has_alpha() const;
  131. virtual void set_flags(uint32_t p_flags);
  132. virtual uint32_t get_flags() const;
  133. void set_atlas(const Ref<Texture>& p_atlas);
  134. Ref<Texture> get_atlas() const;
  135. void set_region(const Rect2& p_region);
  136. Rect2 get_region() const ;
  137. void set_margin(const Rect2& p_margin);
  138. Rect2 get_margin() const ;
  139. virtual void draw(RID p_canvas_item, const Point2& p_pos, const Color& p_modulate=Color(1,1,1)) const;
  140. virtual void draw_rect(RID p_canvas_item,const Rect2& p_rect, bool p_tile=false,const Color& p_modulate=Color(1,1,1)) const;
  141. 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)) const;
  142. virtual bool get_rect_region(const Rect2& p_rect, const Rect2& p_src_rect,Rect2& r_rect,Rect2& r_src_rect) const;
  143. AtlasTexture();
  144. };
  145. class LargeTexture : public Texture {
  146. OBJ_TYPE( LargeTexture, Texture );
  147. RES_BASE_EXTENSION("ltex");
  148. protected:
  149. struct Piece {
  150. Point2 offset;
  151. Ref<Texture> texture;
  152. };
  153. Vector<Piece> pieces;
  154. Size2i size;
  155. Array _get_data() const;
  156. void _set_data(const Array& p_array);
  157. static void _bind_methods();
  158. public:
  159. virtual int get_width() const;
  160. virtual int get_height() const;
  161. virtual RID get_rid() const;
  162. virtual bool has_alpha() const;
  163. virtual void set_flags(uint32_t p_flags);
  164. virtual uint32_t get_flags() const;
  165. int add_piece(const Point2& p_offset,const Ref<Texture>& p_texture);
  166. void set_piece_offset(int p_idx, const Point2& p_offset);
  167. void set_piece_texture(int p_idx, const Ref<Texture>& p_texture);
  168. void set_size(const Size2& p_size);
  169. void clear();
  170. int get_piece_count() const;
  171. Vector2 get_piece_offset(int p_idx) const;
  172. Ref<Texture> get_piece_texture(int p_idx) const;
  173. virtual void draw(RID p_canvas_item, const Point2& p_pos, const Color& p_modulate=Color(1,1,1)) const;
  174. virtual void draw_rect(RID p_canvas_item,const Rect2& p_rect, bool p_tile=false,const Color& p_modulate=Color(1,1,1)) const;
  175. 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)) const;
  176. LargeTexture();
  177. };
  178. class CubeMap : public Resource {
  179. OBJ_TYPE( CubeMap, Resource );
  180. RES_BASE_EXTENSION("cbm");
  181. public:
  182. enum Storage {
  183. STORAGE_RAW,
  184. STORAGE_COMPRESS_LOSSY,
  185. STORAGE_COMPRESS_LOSSLESS
  186. };
  187. enum Side {
  188. SIDE_LEFT,
  189. SIDE_RIGHT,
  190. SIDE_BOTTOM,
  191. SIDE_TOP,
  192. SIDE_FRONT,
  193. SIDE_BACK
  194. };
  195. enum Flags {
  196. FLAG_MIPMAPS=VisualServer::TEXTURE_FLAG_MIPMAPS,
  197. FLAG_REPEAT=VisualServer::TEXTURE_FLAG_REPEAT,
  198. FLAG_FILTER=VisualServer::TEXTURE_FLAG_FILTER,
  199. FLAGS_DEFAULT=FLAG_MIPMAPS|FLAG_REPEAT|FLAG_FILTER,
  200. };
  201. private:
  202. bool valid[6];
  203. RID cubemap;
  204. Image::Format format;
  205. uint32_t flags;
  206. int w,h;
  207. Storage storage;
  208. Size2 size_override;
  209. float lossy_storage_quality;
  210. _FORCE_INLINE_ bool _is_valid() const { for(int i=0;i<6;i++) { if (valid[i]) return true; } return false; }
  211. protected:
  212. bool _set(const StringName& p_name, const Variant& p_value);
  213. bool _get(const StringName& p_name,Variant &r_ret) const;
  214. void _get_property_list( List<PropertyInfo> *p_list) const;
  215. static void _bind_methods();
  216. public:
  217. void set_flags(uint32_t p_flags);
  218. uint32_t get_flags() const;
  219. void set_side(Side p_side,const Image& p_image);
  220. Image get_side(Side p_side) const;
  221. Image::Format get_format() const;
  222. int get_width() const;
  223. int get_height() const;
  224. virtual RID get_rid() const;
  225. void set_storage(Storage p_storage);
  226. Storage get_storage() const;
  227. void set_lossy_storage_quality(float p_lossy_storage_quality);
  228. float get_lossy_storage_quality() const;
  229. CubeMap();
  230. ~CubeMap();
  231. };
  232. VARIANT_ENUM_CAST( CubeMap::Flags );
  233. VARIANT_ENUM_CAST( CubeMap::Side );
  234. VARIANT_ENUM_CAST( CubeMap::Storage );
  235. /*
  236. enum CubeMapSide {
  237. CUBEMAP_LEFT,
  238. CUBEMAP_RIGHT,
  239. CUBEMAP_BOTTOM,
  240. CUBEMAP_TOP,
  241. CUBEMAP_FRONT,
  242. CUBEMAP_BACK,
  243. };
  244. */
  245. //VARIANT_ENUM_CAST( Texture::CubeMapSide );
  246. #endif