| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library 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 Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef GUI_SOFCOFFSCREENRENDERER_H |
| 24 |
#define GUI_SOFCOFFSCREENRENDERER_H |
| 25 |
|
| 26 |
#include <Inventor/SbColor4f.h> |
| 27 |
#include <Inventor/SbMatrix.h> |
| 28 |
#include <Inventor/SoOffscreenRenderer.h> |
| 29 |
|
| 30 |
#include <FCConfig.h> |
| 31 |
|
| 32 |
#ifdef FC_OS_MACOSX |
| 33 |
# include <OpenGL/gl.h> |
| 34 |
#else |
| 35 |
# ifdef FC_OS_WIN32 |
| 36 |
# include <windows.h> |
| 37 |
# endif // FC_OS_WIN32 |
| 38 |
# include <GL/gl.h> |
| 39 |
#endif // FC_OS_MACOSX |
| 40 |
|
| 41 |
#include <QImage> |
| 42 |
#include <QStringList> |
| 43 |
|
| 44 |
#include <FCGlobal.h> |
| 45 |
|
| 46 |
class QOpenGLFramebufferObject; |
| 47 |
|
| 48 |
namespace Gui |
| 49 |
{ |
| 50 |
|
| 51 |
/** |
| 52 |
* The SoFCOffscreenRenderer class is used for rendering scenes in offscreen buffers. |
| 53 |
* @author Werner Mayer |
| 54 |
*/ |
| 55 |
class GuiExport SoFCOffscreenRenderer: public SoOffscreenRenderer |
| 56 |
{ |
| 57 |
public: |
| 58 |
/** The SoOffscreenRenderer base class seems to have a huge memory leak. Whenever |
| 59 |
* an instance is created internal memory doesn't get freed when destroying it. |
| 60 |
* Thus, SoFCOffscreenRenderer is implemented as singleton to allow one to create only |
| 61 |
* one global instance. So, the memory is leaking for this instance only. |
| 62 |
*/ |
| 63 |
static SoFCOffscreenRenderer& instance(); |
| 64 |
|
| 65 |
SoFCOffscreenRenderer(const SoFCOffscreenRenderer&) = delete; |
| 66 |
SoFCOffscreenRenderer& operator=(const SoFCOffscreenRenderer&) = delete; |
| 67 |
|
| 68 |
private: |
| 69 |
static SoFCOffscreenRenderer* inst; |
| 70 |
|
| 71 |
protected: |
| 72 |
/** |
| 73 |
* Constructor. Argument is the \a viewportregion we should use when rendering. An internal |
| 74 |
* SoGLRenderAction will be constructed. |
| 75 |
*/ |
| 76 |
SoFCOffscreenRenderer(const SbViewportRegion& viewportregion); |
| 77 |
/** |
| 78 |
* Constructor. Argument is the \a action we should apply to the scene graph when rendering the |
| 79 |
* scene. Information about the viewport is extracted from the \a action. |
| 80 |
*/ |
| 81 |
SoFCOffscreenRenderer(SoGLRenderAction* action); |
| 82 |
/** |
| 83 |
* Destructor. |
| 84 |
*/ |
| 85 |
~SoFCOffscreenRenderer(); |
| 86 |
|
| 87 |
public: |
| 88 |
/** |
| 89 |
* Writes the rendered image buffer directly into a QImage object |
| 90 |
* instead of an image file. |
| 91 |
*/ |
| 92 |
void writeToImage(QImage&) const; |
| 93 |
/** |
| 94 |
* Saves the buffer to \a filename, in the filetype specified by \a filetypeextensions. |
| 95 |
* |
| 96 |
* Note that you must still specify the full filename for the first argument, i.e. the second |
| 97 |
* argument will not automatically be attached to the filename -- it is only used to decide the |
| 98 |
* filetype. |
| 99 |
* |
| 100 |
* If \a comment is set to '$MIBA' information regarding the MIBA standard is |
| 101 |
* embedded to the picture, otherwise the \a comment is embedded as is. |
| 102 |
* The appropriate file format must support embedding meta information which |
| 103 |
* is provided by JPEG or PNG. |
| 104 |
* |
| 105 |
* This does basically the same as writeToFile() unless that all QImage file formats are |
| 106 |
* supported if not directly supported by Coin3D. |
| 107 |
*/ |
| 108 |
void writeToImageFile( |
| 109 |
const char* filename, |
| 110 |
const char* comment, |
| 111 |
const SbMatrix& mat, |
| 112 |
const QImage& img |
| 113 |
); |
| 114 |
/** |
| 115 |
* This method returns all image file formats supported by Coin3D (see getWriteFiletypeInfo()) |
| 116 |
* with all QImage file formats that are not directly supported by Coin3D, if so. |
| 117 |
*/ |
| 118 |
QStringList getWriteImageFiletypeInfo(); |
| 119 |
|
| 120 |
std::string createMIBA(const SbMatrix& mat) const; |
| 121 |
}; |
| 122 |
|
| 123 |
class GuiExport SoQtOffscreenRenderer |
| 124 |
{ |
| 125 |
public: |
| 126 |
SoQtOffscreenRenderer(const SbViewportRegion& viewportregion); |
| 127 |
SoQtOffscreenRenderer(SoGLRenderAction* action); |
| 128 |
~SoQtOffscreenRenderer(); |
| 129 |
|
| 130 |
void setViewportRegion(const SbViewportRegion& region); |
| 131 |
const SbViewportRegion& getViewportRegion() const; |
| 132 |
|
| 133 |
void setBackgroundColor(const SbColor4f& color); |
| 134 |
const SbColor4f& getBackgroundColor() const; |
| 135 |
|
| 136 |
void setGLRenderAction(SoGLRenderAction* action); |
| 137 |
SoGLRenderAction* getGLRenderAction() const; |
| 138 |
|
| 139 |
void setNumPasses(const int num); |
| 140 |
int getNumPasses() const; |
| 141 |
|
| 142 |
void setInternalTextureFormat(GLenum internalTextureFormat); |
| 143 |
GLenum internalTextureFormat() const; |
| 144 |
|
| 145 |
SbBool render(SoNode* scene); |
| 146 |
SbBool render(SoPath* scene); |
| 147 |
|
| 148 |
void writeToImage(QImage&) const; |
| 149 |
QStringList getWriteImageFiletypeInfo() const; |
| 150 |
|
| 151 |
private: |
| 152 |
void init(const SbViewportRegion& vpr, SoGLRenderAction* glrenderaction = nullptr); |
| 153 |
static void pre_render_cb(void* userdata, SoGLRenderAction* action); |
| 154 |
SbBool renderFromBase(SoBase* base); |
| 155 |
void makeFrameBuffer(int width, int height, int samples); |
| 156 |
|
| 157 |
QOpenGLFramebufferObject* framebuffer; |
| 158 |
uint32_t cache_context; // our unique context id |
| 159 |
|
| 160 |
SbViewportRegion viewport; |
| 161 |
SbColor4f backgroundcolor; |
| 162 |
SbColor4f backgroundopaque; |
| 163 |
SoGLRenderAction* renderaction; |
| 164 |
SbBool didallocation; |
| 165 |
int numSamples; |
| 166 |
GLenum texFormat; |
| 167 |
QImage glImage; |
| 168 |
}; |
| 169 |
|
| 170 |
} // namespace Gui |
| 171 |
|
| 172 |
|
| 173 |
#endif // GUI_SOFCOFFSCREENRENDERER_H |