H3D API  2.4.1
ImageTexture.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __IMAGETEXTURE_H__
30 #define __IMAGETEXTURE_H__
31 
32 #include <H3D/X3DTexture2DNode.h>
33 #include <H3D/H3DImageLoaderNode.h>
34 #include <H3D/X3DUrlObject.h>
35 #include <H3D/MFNode.h>
36 #include <H3D/Scene.h>
37 
38 namespace H3D {
56  class H3DAPI_API ImageTexture :
57  public X3DTexture2DNode,
58  public X3DUrlObject {
59  public:
60 
64 
67 
89  class H3DAPI_API SFImage:
90  public TypedField< X3DTexture2DNode::SFImage,
91  Types< MFString, MFImageLoader > > {
92  protected:
95  virtual void update();
96 
100  Image * loadImage( ImageTexture *texture,
101  const vector< string > &urls,
102  const NodeVector &image_loader );
103 
105  struct ThreadFuncData {
106  ImageTexture *texture;
107  vector< string > urls;
108  NodeVector image_loaders;
109  MutexLock load_thread_mutex;
110  };
111 
113  struct CBData {
114  CBData():texture(NULL), image(NULL){}
115  ImageTexture *texture;
116  Image *image;
117  };
118 
121 
124 
127  static Scene::CallbackCode loadImageCB( void *data );
128 
131  static void *loadImageThreadFunc( void * );
132  public:
133  virtual ~SFImage();
134  };
135 
137  ImageTexture( Inst< DisplayList > _displayList = 0,
138  Inst< SFNode > _metadata = 0,
139  Inst< MFString > _url = 0,
140  Inst< SFBool > _repeatS = 0,
141  Inst< SFBool > _repeatT = 0,
142  Inst< SFBool > _scaleToP2 = 0,
143  Inst< SFImage > _image = 0,
144  Inst< MFImageLoader > _imageLoader = 0,
145  Inst< SFTextureProperties > _textureProperties = 0,
146  Inst< SFString > _loadInThread = 0,
147  Inst< SFBool > _canShare = 0 );
148 
149  virtual ~ImageTexture ();
150 
152  virtual void render();
153 
156  virtual void postRender();
157 
160  virtual void preRender();
161 
164  virtual void enableTexturing();
165 
168  virtual void disableTexturing();
169 
172  virtual GLuint getTextureId();
173 
176  virtual GLuint getTextureUnit();
177 
180  virtual GLenum getTextureTarget();
181 
184  virtual int getTextureWidth();
185 
188  virtual int getTextureHeight();
189 
192  virtual int getTextureDepth();
193 
194 
197  virtual void setTextureId( GLuint id );
200  virtual void setTextureUnit( GLint unit );
203  virtual void setTextureTarget( GLenum target );
204 
207  virtual void setTextureWidth( int _width );
208 
211  virtual void setTextureHeight( int _height );
212 
215  virtual void setTextureDepth( int _depth );
216 
219  virtual bool makeResident ();
220 
223  virtual void makeNonResident ();
224 
227  virtual bool isResident ();
228 
231  virtual GLuint64 getTextureHandle();
232 
235  virtual void invalidateTextureHandle ();
236 
239  virtual void inUse ();
240 
241 
242  virtual Image* renderToImage ( H3DInt32 _width, H3DInt32 _height, bool output_float_texture = false );
243 
248  virtual std::pair<H3DInt32,H3DInt32> getDefaultSaveDimensions ();
249 
253  virtual GLint glInternalFormat( Image *i );
254 
261  H3DUniquePtr< MFImageLoader > imageLoader;
262 
274  H3DUniquePtr< SFString > loadInThread;
275 
284  H3DUniquePtr< SFBool > canShare;
285 
288  protected:
289 
291  bool useSharing ();
292 
295  void removeSharedImage ();
296 
298  bool addSharedImage ( std::vector < std::string > _urls );
299 
301  bool texturePropertiesMatching( const ImageTexture* wrapped_image );
302 
305  H3DUniquePtr< H3DUtil::SimpleThread > load_thread;
306 
308  struct SharedImage {
309  SharedImage () : use_count ( 0 ) {}
310 
311  AutoRef < Node > image;
312  int use_count;
313  };
314 
317  bool inited_sharing;
318  bool share_textures;
319 
320  typedef std::map < std::vector < std::string >, SharedImage > ImageDatabase;
321 
323  static ImageDatabase image_database;
324  };
325 }
326 
327 #endif
Header file for H3DImageLoaderNode, the abstract base class for all image loaders nodes for different...
Contains the MFNode class.
Header file for Scene.
Header file for X3DTexture2DNode, X3D scene-graph node.
Header file for X3DUrlObject, X3D scene-graph node.
CouldNotLoadImage is thrown when not any of the urls could be read with any of the image loaders in a...
Definition: ImageTexture.h:63
SFImage is overridden to update the value from the url and imageLoader fields of the ImageTexture.
Definition: ImageTexture.h:91
CBData cb_data
data used for the callback function.
Definition: ImageTexture.h:123
ThreadFuncData thread_data
data used for the thread function
Definition: ImageTexture.h:120
The ImageTexture node defines a texture map by specifying an image file and general parameters for ma...
Definition: ImageTexture.h:58
H3DUniquePtr< MFImageLoader > imageLoader
The ImageLoader nodes to use to read the image files.
Definition: ImageTexture.h:261
H3DUniquePtr< SFBool > canShare
Share image data for URLs that are the same.
Definition: ImageTexture.h:284
H3DUniquePtr< H3DUtil::SimpleThread > load_thread
The thread used for downloading images when load_images_in_separate_thread is true.
Definition: ImageTexture.h:305
static ImageDatabase image_database
The shared texture database.
Definition: ImageTexture.h:323
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: ImageTexture.h:287
H3DUniquePtr< SFString > loadInThread
Specifies the thread to use to load the image texture.
Definition: ImageTexture.h:274
ImageTexture * wrapped_image
Pointer to the shared texture currently in use, or NULL.
Definition: ImageTexture.h:316
TypedMFNode< H3DImageLoaderNode > MFImageLoader
A MFNode containing ImageLoader.
Definition: ImageTexture.h:66
CallbackCode
Return code for callback functions.
Definition: Scene.h:228
A template modifier class for adding type checking on the routes to any Field class.
Definition: TypedField.h:84
Template to make sure that the Nodes that are added to a MFNode are of a specified Node type.
Definition: MFNode.h:221
This abstract node type is the base type for all node types which specify 2D sources for texture imag...
Definition: X3DTexture2DNode.h:46
This abstract interface is inherited by all nodes that contain data located on the World Wide Web,...
Definition: X3DUrlObject.h:51
H3D_API_EXCEPTION(Vec2dNormalizeError)
int H3DInt32
H3D API namespace.
Definition: Anchor.h:38
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194
struct for storing input needed for the callback function.
Definition: ImageTexture.h:113
struct for storing input needed to the thread function
Definition: ImageTexture.h:105
A shared texture.
Definition: ImageTexture.h:308
int use_count
Number of images referencing this shared texture.
Definition: ImageTexture.h:312