DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

OpenGL.hpp (6962B)


      1 /*
      2  * DISTRHO Plugin Framework (DPF)
      3  * Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
      4  *
      5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
      6  * or without fee is hereby granted, provided that the above copyright notice and this
      7  * permission notice appear in all copies.
      8  *
      9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
     10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
     11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #ifndef DGL_OPENGL_HPP_INCLUDED
     18 #define DGL_OPENGL_HPP_INCLUDED
     19 
     20 #include "ImageBase.hpp"
     21 #include "ImageBaseWidgets.hpp"
     22 
     23 #include "OpenGL-include.hpp"
     24 
     25 START_NAMESPACE_DGL
     26 
     27 // -----------------------------------------------------------------------
     28 
     29 /**
     30    OpenGL Graphics context.
     31  */
     32 struct OpenGLGraphicsContext : GraphicsContext
     33 {
     34 #ifdef DGL_USE_OPENGL3
     35 #endif
     36 };
     37 
     38 // -----------------------------------------------------------------------
     39 
     40 static inline
     41 ImageFormat asDISTRHOImageFormat(const GLenum format)
     42 {
     43     switch (format)
     44     {
     45 #ifdef DGL_USE_OPENGL3
     46     case GL_RED:
     47 #else
     48     case GL_LUMINANCE:
     49 #endif
     50         return kImageFormatGrayscale;
     51     case GL_BGR:
     52         return kImageFormatBGR;
     53     case GL_BGRA:
     54         return kImageFormatBGRA;
     55     case GL_RGB:
     56         return kImageFormatRGB;
     57     case GL_RGBA:
     58         return kImageFormatRGBA;
     59     }
     60 
     61     return kImageFormatNull;
     62 }
     63 
     64 static inline
     65 GLenum asOpenGLImageFormat(const ImageFormat format)
     66 {
     67     switch (format)
     68     {
     69     case kImageFormatNull:
     70         break;
     71     case kImageFormatGrayscale:
     72 #ifdef DGL_USE_OPENGL3
     73         return GL_RED;
     74 #else
     75         return GL_LUMINANCE;
     76 #endif
     77     case kImageFormatBGR:
     78         return GL_BGR;
     79     case kImageFormatBGRA:
     80         return GL_BGRA;
     81     case kImageFormatRGB:
     82         return GL_RGB;
     83     case kImageFormatRGBA:
     84         return GL_RGBA;
     85     }
     86 
     87     return 0x0;
     88 }
     89 
     90 // -----------------------------------------------------------------------
     91 
     92 /**
     93    OpenGL Image class.
     94 
     95    This is an Image class that handles raw image data in pixels.
     96    You can init the image data on the contructor or later on by calling loadFromMemory().
     97 
     98    To generate raw data useful for this class see the utils/png2rgba.py script.
     99    Be careful when using a PNG without alpha channel, for those the format is 'GL_BGR'
    100    instead of the default 'GL_BGRA'.
    101 
    102    Images are drawn on screen via 2D textures.
    103  */
    104 class OpenGLImage : public ImageBase
    105 {
    106 public:
    107    /**
    108       Constructor for a null Image.
    109     */
    110     OpenGLImage();
    111 
    112    /**
    113       Constructor using raw image data.
    114       @note @a rawData must remain valid for the lifetime of this Image.
    115     */
    116     OpenGLImage(const char* rawData, uint width, uint height, ImageFormat format = kImageFormatBGRA);
    117 
    118    /**
    119       Constructor using raw image data.
    120       @note @a rawData must remain valid for the lifetime of this Image.
    121     */
    122     OpenGLImage(const char* rawData, const Size<uint>& size, ImageFormat format = kImageFormatBGRA);
    123 
    124    /**
    125       Constructor using another image data.
    126     */
    127     OpenGLImage(const OpenGLImage& image);
    128 
    129    /**
    130       Destructor.
    131     */
    132     ~OpenGLImage() override;
    133 
    134    /**
    135       Load image data from memory.
    136       @note @a rawData must remain valid for the lifetime of this Image.
    137     */
    138     void loadFromMemory(const char* rawData,
    139                         const Size<uint>& size,
    140                         ImageFormat format = kImageFormatBGRA) noexcept override;
    141 
    142    /**
    143       Draw this image at position @a pos using the graphics context @a context.
    144     */
    145     void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
    146 
    147    /**
    148       TODO document this.
    149     */
    150     OpenGLImage& operator=(const OpenGLImage& image) noexcept;
    151 
    152     // FIXME this should not be needed
    153     inline void loadFromMemory(const char* rdata, uint w, uint h, ImageFormat fmt = kImageFormatBGRA)
    154     { loadFromMemory(rdata, Size<uint>(w, h), fmt); }
    155     inline void draw(const GraphicsContext& context)
    156     { drawAt(context, Point<int>(0, 0)); }
    157     inline void drawAt(const GraphicsContext& context, int x, int y)
    158     { drawAt(context, Point<int>(x, y)); }
    159 
    160    /**
    161       Constructor using raw image data, specifying an OpenGL image format.
    162       @note @a rawData must remain valid for the lifetime of this Image.
    163       DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one.
    164     */
    165     DISTRHO_DEPRECATED_BY("OpenGLImage(const char*, uint, uint, ImageFormat)")
    166     explicit OpenGLImage(const char* rawData, uint width, uint height, GLenum glFormat);
    167 
    168    /**
    169       Constructor using raw image data, specifying an OpenGL image format.
    170       @note @a rawData must remain valid for the lifetime of this Image.
    171       DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one.
    172     */
    173     DISTRHO_DEPRECATED_BY("OpenGLImage(const char*, const Size<uint>&, ImageFormat)")
    174     explicit OpenGLImage(const char* rawData, const Size<uint>& size, GLenum glFormat);
    175 
    176    /**
    177       Draw this image at (0, 0) point using the current OpenGL context.
    178       DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
    179     */
    180     DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
    181     void draw();
    182 
    183    /**
    184       Draw this image at (x, y) point using the current OpenGL context.
    185       DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
    186     */
    187     DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, int, int)")
    188     void drawAt(int x, int y);
    189 
    190    /**
    191       Draw this image at position @a pos using the current OpenGL context.
    192       DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
    193     */
    194     DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, const Point<int>&)")
    195     void drawAt(const Point<int>& pos);
    196 
    197    /**
    198       Get the image type.
    199       DEPRECATED Type is always assumed to be GL_UNSIGNED_BYTE.
    200     */
    201     DISTRHO_DEPRECATED
    202     GLenum getType() const noexcept { return GL_UNSIGNED_BYTE; }
    203 
    204 private:
    205     bool setupCalled;
    206     bool textureInit;
    207     GLuint textureId;
    208 };
    209 
    210 // -----------------------------------------------------------------------
    211 
    212 typedef ImageBaseAboutWindow<OpenGLImage> OpenGLImageAboutWindow;
    213 typedef ImageBaseButton<OpenGLImage> OpenGLImageButton;
    214 typedef ImageBaseKnob<OpenGLImage> OpenGLImageKnob;
    215 typedef ImageBaseSlider<OpenGLImage> OpenGLImageSlider;
    216 typedef ImageBaseSwitch<OpenGLImage> OpenGLImageSwitch;
    217 
    218 // -----------------------------------------------------------------------
    219 
    220 END_NAMESPACE_DGL
    221 
    222 #endif // DGL_OPENGL_HPP_INCLUDED