COpenGLTexture.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __C_OPEN_GL_TEXTURE_H_INCLUDED__
  5. #define __C_OPEN_GL_TEXTURE_H_INCLUDED__
  6. #include "ITexture.h"
  7. #include "IImage.h"
  8. #include "IrrCompileConfig.h"
  9. #ifdef _IRR_COMPILE_WITH_OPENGL_
  10. #if defined(_IRR_OPENGL_USE_EXTPOINTER_)
  11. #define GL_GLEXT_LEGACY 1
  12. #else
  13. #define GL_GLEXT_PROTOTYPES 1
  14. #endif
  15. #ifdef _IRR_WINDOWS_API_
  16. // include windows headers for HWND
  17. #define WIN32_LEAN_AND_MEAN
  18. #include <windows.h>
  19. #include <GL/gl.h>
  20. #ifdef _MSC_VER
  21. #pragma comment(lib, "OpenGL32.lib")
  22. #endif
  23. #elif defined(_IRR_OSX_PLATFORM_)
  24. #include <OpenGL/gl.h>
  25. #elif defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
  26. #define NO_SDL_GLEXT
  27. #include <SDL/SDL_video.h>
  28. #include <SDL/SDL_opengl.h>
  29. #else
  30. #if defined(_IRR_OSX_PLATFORM_)
  31. #include <OpenGL/gl.h>
  32. #else
  33. #include <GL/gl.h>
  34. #endif
  35. #endif
  36. namespace irr
  37. {
  38. namespace video
  39. {
  40. class COpenGLDriver;
  41. //! OpenGL texture.
  42. class COpenGLTexture : public ITexture
  43. {
  44. public:
  45. //! constructor
  46. COpenGLTexture(IImage* surface, const io::path& name, void* mipmapData=0, COpenGLDriver* driver=0);
  47. //! destructor
  48. virtual ~COpenGLTexture();
  49. //! lock function
  50. virtual void* lock(E_TEXTURE_LOCK_MODE mode=ETLM_READ_WRITE, u32 mipmapLevel=0);
  51. //! unlock function
  52. virtual void unlock();
  53. //! Returns original size of the texture (image).
  54. virtual const core::dimension2d<u32>& getOriginalSize() const;
  55. //! Returns size of the texture.
  56. virtual const core::dimension2d<u32>& getSize() const;
  57. //! returns driver type of texture (=the driver, that created it)
  58. virtual E_DRIVER_TYPE getDriverType() const;
  59. //! returns color format of texture
  60. virtual ECOLOR_FORMAT getColorFormat() const;
  61. //! returns pitch of texture (in bytes)
  62. virtual u32 getPitch() const;
  63. //! return open gl texture name
  64. virtual u32 getOpenGLTextureName() const;
  65. virtual u64 getHandle() { return 0; }
  66. //! return whether this texture has mipmaps
  67. virtual bool hasMipMaps() const;
  68. //! Regenerates the mip map levels of the texture.
  69. /** Useful after locking and modifying the texture
  70. \param mipmapData Pointer to raw mipmap data, including all necessary mip levels, in the same format as the main texture image. If not set the mipmaps are derived from the main image. */
  71. virtual void regenerateMipMapLevels(void* mipmapData=0);
  72. //! Is it a render target?
  73. virtual bool isRenderTarget() const;
  74. //! Is it a FrameBufferObject?
  75. virtual bool isFrameBufferObject() const;
  76. //! Bind RenderTargetTexture
  77. virtual void bindRTT();
  78. //! Unbind RenderTargetTexture
  79. virtual void unbindRTT();
  80. //! sets whether this texture is intended to be used as a render target.
  81. void setIsRenderTarget(bool isTarget);
  82. void setImage(IImage* new_image)
  83. {
  84. if (Image)
  85. Image->drop();
  86. Image = new_image;
  87. }
  88. protected:
  89. //! protected constructor with basic setup, no GL texture name created, for derived classes
  90. COpenGLTexture(const io::path& name, COpenGLDriver* driver);
  91. //! get the desired color format based on texture creation flags and the input format.
  92. ECOLOR_FORMAT getBestColorFormat(ECOLOR_FORMAT format);
  93. //! Get the OpenGL color format parameters based on the given Irrlicht color format
  94. GLint getOpenGLFormatAndParametersFromColorFormat(
  95. ECOLOR_FORMAT format, GLint& filtering, GLenum& colorformat, GLenum& type);
  96. //! get important numbers of the image and hw texture
  97. void getImageValues(IImage* image);
  98. //! copies the texture into an OpenGL texture.
  99. /** \param newTexture True if method is called for a newly created texture for the first time. Otherwise call with false to improve memory handling.
  100. \param mipmapData Pointer to raw mipmap data, including all necessary mip levels, in the same format as the main texture image.
  101. \param mipLevel If set to non-zero, only that specific miplevel is updated, using the MipImage member. */
  102. void uploadTexture(bool newTexture=false, void* mipmapData=0, u32 mipLevel=0);
  103. core::dimension2d<u32> ImageSize;
  104. core::dimension2d<u32> TextureSize;
  105. ECOLOR_FORMAT ColorFormat;
  106. COpenGLDriver* Driver;
  107. IImage* Image;
  108. IImage* MipImage;
  109. GLuint TextureName;
  110. GLint InternalFormat;
  111. GLenum PixelFormat;
  112. GLenum PixelType;
  113. u8 MipLevelStored;
  114. bool HasMipMaps;
  115. bool MipmapLegacyMode;
  116. bool IsRenderTarget;
  117. bool AutomaticMipmapUpdate;
  118. bool ReadOnlyLock;
  119. bool KeepImage;
  120. };
  121. //! OpenGL FBO texture.
  122. class COpenGLFBOTexture : public COpenGLTexture
  123. {
  124. public:
  125. //! FrameBufferObject constructor
  126. COpenGLFBOTexture(const core::dimension2d<u32>& size, const io::path& name,
  127. COpenGLDriver* driver = 0, const ECOLOR_FORMAT format = ECF_UNKNOWN);
  128. //! destructor
  129. virtual ~COpenGLFBOTexture();
  130. //! Is it a FrameBufferObject?
  131. virtual bool isFrameBufferObject() const;
  132. //! Bind RenderTargetTexture
  133. virtual void bindRTT();
  134. //! Unbind RenderTargetTexture
  135. virtual void unbindRTT();
  136. ITexture* DepthTexture;
  137. GLuint DepthBufferTexture;
  138. protected:
  139. GLuint ColorFrameBuffer;
  140. };
  141. //! OpenGL FBO depth texture.
  142. class COpenGLFBODepthTexture : public COpenGLTexture
  143. {
  144. public:
  145. //! FrameBufferObject depth constructor
  146. COpenGLFBODepthTexture(const core::dimension2d<u32>& size, const io::path& name, COpenGLDriver* driver=0, bool useStencil=false);
  147. //! destructor
  148. virtual ~COpenGLFBODepthTexture();
  149. //! Bind RenderTargetTexture
  150. virtual void bindRTT();
  151. //! Unbind RenderTargetTexture
  152. virtual void unbindRTT();
  153. bool attach(ITexture*);
  154. bool hasStencil();
  155. protected:
  156. GLuint DepthRenderBuffer;
  157. GLuint StencilRenderBuffer;
  158. bool UseStencil;
  159. };
  160. } // end namespace video
  161. } // end namespace irr
  162. #endif
  163. #endif // _IRR_COMPILE_WITH_OPENGL_