00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef _SO_OFFSCREEN_RENDERER_
00052 #define _SO_OFFSCREEN_RENDERER_
00053
00054 #include <Inventor/SbColorRGBA.h>
00055 #include <Inventor/SbColor.h>
00056 #include <Inventor/SbViewportRegion.h>
00057 #include <Inventor/SbLinear.h>
00058 #include <Inventor/SbPList.h>
00059 #include <Inventor/threads/SbThreadSpinlock.h>
00060 #include <Inventor/components/SoGLGraphicConfigTemplate.h>
00061 #include <Inventor/helpers/SbGlContextHelper.h>
00062 #include <Inventor/sys/SoGLType.h>
00063 #include <Inventor/nodes/SoFullSceneAntialiasing.h>
00064
00065 #include <stdio.h>
00066 #undef Display
00067
00068 class SoBase;
00069 class SoNode;
00070 class SoPath;
00071 class SoGLRenderAction;
00072 class SoPBuffer;
00073 class SoOffscreenTileObserver;
00074 class SoGLContext;
00075 class SoGLGraphicConfigTemplate;
00076 class SbRasterImage;
00077 class SoRasterImageFile;
00078 class SoRasterImageRW;
00079 class SoOffRenderItem;
00080
00082
00083
00084
00085
00086
00087
00088
00089
00090
00092
00322 class SoOffscreenRenderer {
00323
00324 public:
00330 SoOffscreenRenderer(const SbViewportRegion &viewportRegion);
00335 SoOffscreenRenderer(SoGLRenderAction *ra);
00336
00340 ~SoOffscreenRenderer();
00341
00343 enum Components {
00345 LUMINANCE = 1,
00347 LUMINANCE_TRANSPARENCY = 2,
00349 RGB = 3,
00351 RGBA = 4,
00353 RGB_TRANSPARENCY = 4
00354 };
00355
00357 enum TIFFCompressionMode {
00359 NO_COMPRESSION,
00361 PACKBITS_COMPRESSION
00362 };
00363
00365 typedef enum {
00367 RGB_BUFFER,
00369 JPEG_BUFFER
00370
00371
00372
00373 } BufferType;
00374
00376 enum AbortCode {
00380 CONTINUE,
00384 ABORT
00385 };
00386
00392 typedef AbortCode SoOffscreenAbortCB(void *userData, int numSubimage, int subimageCount);
00393
00407 void setAbortCallback(SoOffscreenAbortCB *func, void *userData)
00408 { m_abortCB = func; m_abortData = userData; }
00409
00414 static float getScreenPixelsPerInch();
00415
00423 void setComponents(Components components);
00424
00428 Components getComponents() const { return m_comps; }
00429
00443 void setMaxTileSize( SbVec2s size );
00448 void setMaxSubimage( SbVec2s size );
00449
00453 SbVec2s getMaxTileSize() const {
00454 return SbVec2s((short)m_tileSize[0],(short)m_tileSize[1]); }
00458 SbVec2s getMaxSubimage() const {
00459 return SbVec2s((short)m_tileSize[0],(short)m_tileSize[1]); }
00460
00471 void setNumEdgePixels( int nb );
00476 int getNumEdgePixels() const { return m_numEdgePixels; }
00477
00482 void setViewportRegion(const SbViewportRegion ®ion);
00483
00487 const SbViewportRegion &getViewportRegion() const;
00488
00492 static SbVec2s getMaximumResolution();
00493
00506 void setBackgroundColorRGBA(const SbColorRGBA& c);
00507
00511 const SbColorRGBA& getBackgroundColorRGBA() const { return m_backgroundColorRGBA; }
00512
00520 void setBackgroundColor(const SbColor &c);
00524 const SbColor &getBackgroundColor() const { return m_backgroundColor; }
00525
00529 void setGLRenderAction(SoGLRenderAction *ra);
00534 SoGLRenderAction *getGLRenderAction() const;
00535
00545 SbBool render(SoNode *scene);
00555 SbBool render(SoPath *scene);
00556
00557
00566 void setRegion(const SbVec2s& origin, const SbVec2s& size);
00567
00575 void setRegion_i32(const SbVec2i32& origin, const SbVec2i32& size);
00576
00607 unsigned char *getBuffer(BufferType buffType = SoOffscreenRenderer::RGB_BUFFER);
00608
00615 size_t getBufferSize(BufferType buffType = SoOffscreenRenderer::RGB_BUFFER);
00616
00621 SbBool writeToRGB(FILE *fp) const;
00622
00627 SbBool writeToRGB(const SbString& filename) const;
00628
00635 SbBool writeToPostScript(FILE *fp) const;
00636
00643 SbBool writeToPostScript(const SbString& filename) const;
00644
00650 SbBool writeToPostScript(FILE *fp, const SbVec2f &printSize) const;
00651
00657 SbBool writeToPostScript(const SbString& filename, const SbVec2f &printSize) const;
00658
00659
00660
00661
00662
00663
00664
00669 SbBool writeToTIFF(FILE *fp, TIFFCompressionMode TIFFcm = PACKBITS_COMPRESSION) const;
00670
00675 SbBool writeToTIFF(const SbString& filename, TIFFCompressionMode TIFFcm = PACKBITS_COMPRESSION) const;
00676
00677
00678
00679
00680
00681
00694 SbBool writeToJPEG(FILE *fp, float quality = 1.0) const;
00695
00710 SbBool writeToJPEG(const SbString& filename, float quality = 1.0) const;
00711
00717 SbBool writeToPNG(FILE *fp) const;
00718
00723 SbBool writeToPNG(const SbString& filename) const;
00724
00729 SbBool writeToBMP(FILE *fp) const;
00730
00735 SbBool writeToBMP(const SbString& filename);
00736
00746 SbBool writeToRaster(FILE *fp, SoRasterImageRW* imageWriter);
00747
00756 SbBool writeToRaster(const SbString& filename, SoRasterImageRW* imageWriter);
00757
00762 void setTileObserver(SoOffscreenTileObserver* tileObserver);
00763
00764
00773 const HDC &getDC() const;
00774
00783 void setColorDepth(int nbits);
00784
00788 int getColorDepth() const { return m_nCurrentColorDepth; }
00789
00797 void setPbufferEnable( SbBool enable );
00798
00802 SbBool getPbufferEnable() const { return m_pbufferEnable; }
00803
00804
00809 SoGLGraphicConfigTemplate* getGraphicConfigTemplate();
00810
00811
00816 void setGraphicConfigTemplate(SoGLGraphicConfigTemplate* gTemplate);
00817
00818
00827 void setFullSceneAntialiasing( SoGraphicConfigTemplate::Preference pref,
00828 int minFsaaBits = 0,
00829 int maxFsaaBits = INT_MAX );
00830
00848 void setFullSceneAntialiasing( SbBool enable,
00849 float quality = -1.0,
00850 int filterMask = SoFullSceneAntialiasing::ALL );
00851
00858 SoGraphicConfigTemplate::Preference getFullSceneAntialiasing( int &minFsaaBits,
00859 int &maxFsaaBits) const;
00860
00868 SbBool isFullSceneAntialiasing(float& quality);
00869
00883 void setShareContext(const SbGLShareContext shareCxt);
00884
00885
00890 const SbGLShareContext getShareContext() const;
00891
00892
00893
00894 private:
00895
00900 SbBool setupPixmap();
00901
00906 const SoPBuffer *getPBuffer() const { return m_pbuffer; }
00907
00908
00909 private:
00910 SbBool isLargeImage() {return m_largeImage;};
00911 SbBool isMultiTiled() {return m_multiTiled;};
00912
00913 SoGLContext* m_glContext;
00914
00915 private:
00916 unsigned char* m_pixelBuffer;
00917 int m_pixelBufferSize;
00918 unsigned char* m_jpegBuffer;
00919 size_t m_jpegBufferSize;
00920 Components m_comps;
00921 SbColor m_backgroundColor;
00922 SbColorRGBA m_backgroundColorRGBA;
00923
00924 SbVec2i32 m_tileSize;
00925
00926
00927 SbVec2i32 m_previousSubTileBufferSize;
00928
00929
00930 int m_numEdgePixels;
00931
00932 SoOffscreenTileObserver* m_tileObserver;
00933
00934
00935 SoOffscreenAbortCB* m_abortCB;
00936 void *m_abortData;
00937
00938 SbViewportRegion m_vpRegion;
00939
00940 SbVec2i32 m_regionOrigin;
00941 SbVec2i32 m_regionSize;
00942 SbBool m_isRegion;
00943
00944
00945 SbGLShareContext m_glShareContext;
00946
00947 SbGlContextHelper::GLContext m_savedCtx;
00948
00949 #ifdef _WIN32
00950
00951 HDC m_hDC;
00952 HGLRC m_context;
00953 HBITMAP m_hBitmap;
00954 HBITMAP m_hOldBitmap;
00955 HPALETTE m_hOldPalette;
00956
00957
00958
00959
00960
00961 static SbBool initPixmap(SoOffscreenRenderer* offscreen,
00962 HDC &theDC,
00963 PIXELFORMATDESCRIPTOR &thePFD,
00964 HGLRC &cx,
00965 const SbVec2i32 &sz,
00966 HBITMAP &theBitmap,
00967 HBITMAP &theOldBitmap,
00968 HPALETTE &theOldPalette);
00969
00970
00971 #elif defined(__APPLE__)
00972
00973
00974 typedef struct _CGLPBufferObject* CGLPBufferObj;
00975
00976
00977 CGLContextObj m_context;
00978
00980 CGLPBufferObj m_cglPbuffer;
00981
00982
00983 static SbBool initPixmap(SoOffscreenRenderer* offscreen,
00984 CGLContextObj& cx,
00985 const SbVec2i32 &sz,
00986 CGLPBufferObj& pbuffer);
00987
00988 #else
00989
00990
00991 SbGlContextHelper::Display m_display;
00992 SbGlContextHelper::VisualInfo m_visual;
00993 GLXContext m_context;
00994 GLXPixmap m_pixmap;
00995 Pixmap m_pmap;
00996
00997
00998
00999 static SbBool initPixmap(SoOffscreenRenderer* offscreen,
01000 Display * &dpy, SbGlContextHelper::VisualInfo &vi,
01001 GLXContext &cx, const SbVec2i32 &sz,
01002 GLXPixmap &glxPmap, Pixmap &xpmap);
01003
01004
01005 #endif
01006
01007 SbGlContextHelper::Display m_pBufferDisplay;
01008
01009
01010 SoPBuffer *m_pbuffer;
01011 SbBool m_pbufferEnable;
01012
01013 int m_nRequestedColorDepth;
01014 int m_nCurrentColorDepth;
01015
01016
01017 static int m_maxGlRenderSize;
01018 static SbBool m_maxGlRenderSizeFlag;
01019 static SbBool getMaxRenderSize();
01020
01021
01022 static SbBool initPbuffer( SoOffscreenRenderer* osr, SbGlContextHelper::Display &display,
01023 const SbVec2i32 &sz, SoPBuffer* &pbuffer );
01024
01025
01026 void readPixels();
01027
01028
01029 void getAllocSizeAndFormat(size_t& allocSize, GLenum& format);
01030
01031
01032 SbBool bindContext();
01033
01034 SbBool unbindContext();
01035
01036
01037 GLenum getGLformat(void) const;
01038
01039
01040 SbBool checkAbort(int numSubimage, int subimageCount);
01041
01042
01043
01044 SbBool renderNodeOrPath(SoBase *scene);
01045
01046 SbBool doSubTile(SbVec2i32& pixelOffset, SbVec2i32& offset, SbVec2i32& renderSize, SbVec2i32& subTileBufferSize,
01047 SbVec2i32& lastSubTileBufferSize, SbVec2i32& numSubTiles, SoRasterImageRW* imageWriter,
01048 unsigned char*& tileBuffer, int totalNumTiles, unsigned char*& edgedSubTileBuffer,
01049 SbRasterImage* rasterImage, SoRasterImageFile* imageFile, SbVec2i32& vpSize, GLenum format);
01050
01051
01052 SbBool m_multiTiled;
01053
01054 SbBool m_largeImage;
01055
01056 SoBase* m_scene;
01057
01058 static void putHex(FILE *fp, unsigned char val, int &hexPos);
01059 int m_savedContextID;
01060
01061 void commonConstructor(SoGLRenderAction* act, SbBool defaultRA = FALSE);
01062 SbBool m_shareContextSuccessful;
01063 SoGLGraphicConfigTemplate* m_currentGraphicConfigTemplate;
01064
01065 void checkTilingConditions();
01066 bool evalTilesSizes( SbVec2i32& subTileBufferSize, SbVec2i32& lastSubTileBufferSize, SbVec2i32& numSubTiles ) const;
01067 void checkTileSizeAgainstConstraints( SbVec2i32& tileSize ) const;
01068
01070 bool m_isCurrentGraphicConfigTemplateOwner;
01071
01072 SbBool m_currentGraphicConfigTemplateChanged;
01073
01074
01075 int m_minFsaaBits;
01076 int m_maxFsaaBits;
01077 SoGraphicConfigTemplate::Preference m_fsaaPref;
01078 SbBool m_isFsaaEnable;
01079
01080 private:
01081
01082 static SoOffRenderItem* findOffRender(const SoOffscreenRenderer *rnd);
01083 static const SbViewportRegion& getCorrectVP(const SoOffscreenRenderer *rnd);
01084
01085
01086 SbBool writeImageFileToRGB( SoRasterImageFile* imageFile );
01087 SbBool writeImageFileToBMP( SoRasterImageFile* imageFile );
01088 SbBool writeImageFileToPNG( SoRasterImageFile* imageFile );
01089 SbBool writeImageFileToJPEG( SoRasterImageFile* imageFile, float quality );
01090 SbBool writeImageFileToTIFF( SoRasterImageFile* imageFile, TIFFCompressionMode TIFFcm );
01091 SbBool writeImageFileToPostScript( SoRasterImageFile* imageFile );
01092 SbBool writeImageFileToPostScript( SoRasterImageFile* imageFile, const SbVec2f &printSize );
01093 SbBool writeImageFileToRaster( SoRasterImageFile* imageFile, SoRasterImageRW* imageWriter );
01094
01095 static SbThreadSpinlock s_renderListMutex;
01096 static SbPList s_renderList;
01097
01098 int m_filterMask;
01099 };
01100
01101
01102
01119 #include <Inventor/SoOffscreenRenderer.h>
01120
01121 class SoOffscreenTileProperty {
01122
01123 public:
01124
01128 const SbVec2i32& getOrigin() const;
01129
01130
01134 const SbVec2i32& getSize() const;
01135
01141 const unsigned char* getBuffer(int& bufferSize) const;
01142
01143
01147 int getTileCount() const;
01148
01149
01150
01151 private:
01152
01153 void setOrigin(SbVec2i32& origin);
01154 void setSize(SbVec2i32& size);
01155 void setBuffer(const unsigned char* buffer, int bufferSize);
01156 void setTileCount(int count);
01157
01158 private:
01159 SbVec2i32 m_origin;
01160 SbVec2i32 m_size;
01161 const unsigned char* m_buffer;
01162 int m_tileCount;
01163 int m_bufferSize;
01164 };
01165
01166
01186 class SoOffscreenTileObserver {
01187
01188 public:
01189 virtual ~SoOffscreenTileObserver() {}
01190
01196 virtual SoOffscreenRenderer::AbortCode tileUpdated(SoOffscreenRenderer* offscreen,
01197 SoOffscreenTileProperty* tileProperty,
01198 int numTiles) = 0;
01199 };
01200
01201
01202
01203
01204 #endif // _SO_OFFSCREEN_RENDERER_
01205
01206