Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGClusterViewBuffer.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 //---------------------------------------------------------------------------
00039 //  Includes
00040 //---------------------------------------------------------------------------
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043 
00044 #include "OSGConfig.h"
00045 #include "OSGGL.h"
00046 #include "OSGGLU.h"
00047 #include "OSGBaseFunctions.h"
00048 #include "OSGLog.h"
00049 #include "OSGClusterViewBuffer.h"
00050 #include "OSGImageFileHandler.h"
00051 #include "OSGImageFileType.h"
00052 #include "OSGPointConnection.h"
00053 #include "OSGGroupConnection.h"
00054 
00055 OSG_USING_NAMESPACE
00056 
00076 /*-------------------------------------------------------------------------*/
00077 /*                            Constructors/Destructor                      */
00078 
00085 ClusterViewBuffer::ClusterViewBuffer(void) :
00086     _imgTransType(NULL),
00087     _subTileSize(32),
00088     _rgbDataType(GL_UNSIGNED_BYTE),
00089     _rgbDataSize(3),
00090     _rgbaDataType(GL_UNSIGNED_BYTE),
00091     _rgbaDataSize(4),
00092     _depthDataType(GL_UNSIGNED_INT),
00093     _depthDataSize(4)
00094 {
00095 }
00096 
00099 ClusterViewBuffer::~ClusterViewBuffer(void)
00100 {
00101 }
00102 
00103 /*-------------------------------------------------------------------------*/
00104 /*                            send/recv                                    */
00105 
00109 void ClusterViewBuffer::recv(GroupConnection &connection)
00110 {
00111     UInt32              tx, ty, tw, th;
00112     UInt32              missing;
00113     BufferT             data;
00114     BufferT             imageData;
00115     UInt32              dataSize;
00116     UInt32              component;
00117     GLenum              glformat;
00118     int                 componentCnt;
00119     Connection::Channel channel;
00120 
00121     missing = connection.getChannelCount();
00122 
00123     glPushMatrix();
00124     glLoadIdentity();
00125     glMatrixMode(GL_PROJECTION);
00126     glPushMatrix();
00127     glLoadIdentity();
00128     gluOrtho2D(0, getBufferWidth(), 0, getBufferHeight());
00129     glDisable(GL_DEPTH_TEST);
00130     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00131 
00132     // we expect tiles form all connected servers
00133     while(missing)
00134     {
00135         channel = connection.selectChannel();
00136         connection.getValue(component);
00137         if(!component)
00138         {
00139             connection.subSelection(channel);
00140             missing--;
00141             continue;
00142         }
00143 
00144         // get dimension
00145         connection.getValue(tx);
00146         connection.getValue(ty);
00147         connection.getValue(tw);
00148         connection.getValue(th);
00149         glRasterPos2i(tx, ty);
00150 
00151         // =========== recv stencil =====================================
00152         if(component & STENCIL)
00153         {
00154             data.resize(tw * th);
00155             connection.get(&data[0], tw * th);
00156             glDrawPixels(tw, th, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &data[0]);
00157             glEnable(GL_STENCIL_TEST);
00158         }
00159 
00160         // =========== recv depth =======================================
00161         if(component & DEPTH)
00162         {
00163             glEnable(GL_DEPTH_TEST);
00164             glEnable(GL_STENCIL_TEST);
00165             glStencilFunc(GL_ALWAYS, 1, 1);
00166             glStencilOp(GL_KEEP, GL_ZERO, GL_REPLACE);
00167             data.resize(tw * th * sizeof(float));
00168             connection.get(&data[0], tw * th * sizeof(float));
00169             glDrawPixels(tw, th, GL_DEPTH_COMPONENT, GL_FLOAT, &data[0]);
00170             glDisable(GL_DEPTH_TEST);
00171         }
00172 
00173         // =========== recv RGBA ========================================
00174         if(component & RGBA)
00175         {
00176             if(component & (DEPTH | STENCIL))
00177             {
00178                 glStencilFunc(GL_EQUAL, 1, 1);
00179             }
00180 
00181             switch(component & RGBA)
00182             {
00183             case RGB:
00184                 glformat = GL_RGB;
00185                 componentCnt = 3;
00186                 break;
00187             case RGBA:
00188                 glformat = GL_RGBA;
00189                 componentCnt = 4;
00190                 break;
00191             default:
00192                 SFATAL << "Component combination not supported" << std::endl;
00193                 return;
00194             }
00195 
00196             connection.getValue(dataSize);
00197 
00198             // compression ?
00199             if(dataSize > 0)
00200             {
00201 #if 0
00202                 pImage = new Image;
00203 
00204                 data.resize(dataSize);
00205                 connection.get(&data[0], dataSize);
00206                 imageData.resize(tw * th * componentCnt);
00207                 ImageFileType::restore(*pImage, (UChar8 *) &data[0], dataSize);
00208                 glDrawPixels(tw, th, glformat, GL_UNSIGNED_BYTE,
00209                                              pImage->getData());
00210 
00211                 subRefP(pImage);
00212 #endif
00213             }
00214             else
00215             {
00216                 data.resize(tw * th * componentCnt);
00217                 connection.get(&data[0], tw * th * componentCnt);
00218                 glDrawPixels(tw, th, glformat, GL_UNSIGNED_BYTE, &data[0]);
00219             }
00220         }
00221 
00222         if(component & (DEPTH | STENCIL))
00223         {
00224             glDisable(GL_STENCIL_TEST);
00225         }
00226     }
00227     connection.resetSelection();
00228     glPopMatrix();
00229     glMatrixMode(GL_MODELVIEW);
00230     glPopMatrix();
00231     glEnable(GL_DEPTH_TEST);
00232 }
00233 
00241 void ClusterViewBuffer::send(PointConnection &connection, 
00242                              UInt32 component, UInt32 x1,
00243                              UInt32 y1, UInt32 x2, UInt32 y2, UInt32 toX,
00244                              UInt32 toY)
00245 {
00246     UInt32              tx, ty, tw, th;
00247 //    Image               *pImage;
00248     BufferT             data;
00249     BufferT             imageData;
00250     UInt32              dataSize;
00251     GLenum              glformat;
00252 //    Image::PixelFormat  imgformat;
00253     int                 componentCnt;
00254     int                 imgtranssize = 0;
00255 
00256     switch(component & RGBA)
00257     {
00258     case RGB:  glformat = GL_RGB;
00259 //               imgformat = Image::OSG_RGB_PF;
00260                componentCnt = 3;
00261                break;
00262     case RGBA: glformat = GL_RGBA;
00263 //               imgformat = Image::OSG_RGBA_PF;
00264                componentCnt = 4;
00265                break;
00266     default:   SFATAL << "Component combination not supported" << std::endl;
00267                return;
00268     }
00269 
00270     // resize image buffer
00271     imageData.resize(_subTileSize * _subTileSize * componentCnt);
00272 
00273     glPixelStorei(GL_PACK_ALIGNMENT, 1);
00274 
00275     for(ty = y1; ty <= y2; ty += _subTileSize)
00276     {
00277         for(tx = x1; tx <= x2; tx += _subTileSize)
00278         {
00279             tw = osgMin(_subTileSize, x2 + 1 - tx);
00280             th = osgMin(_subTileSize, y2 + 1 - ty);
00281 
00282             connection.putValue(component);
00283             connection.putValue(tx + toX);
00284             connection.putValue(ty + toY);
00285             connection.putValue(tw);
00286             connection.putValue(th);
00287 
00288             // =========== send STENCIL =======================================
00289             if(component & STENCIL)
00290             {
00291                 // read stencil buffer
00292                 data.resize(tw * th);
00293                 glReadPixels(tx, ty, tw, th, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
00294                                              &data[0]);
00295                 connection.put(&data[0], tw * th);
00296             }
00297 
00298             // =========== send DEPTH =======================================
00299             if(component & DEPTH)
00300             {
00301                 // read stencil buffer
00302                 data.resize(tw * th * sizeof(float));
00303                 glReadPixels(tx, ty, tw, th, GL_DEPTH_COMPONENT, GL_FLOAT,
00304                                              &data[0]);
00305                 connection.put(&data[0], tw * th * sizeof(float));
00306             }
00307 
00308             // =========== send RGBA ========================================
00309             if(component & RGBA)
00310             {
00311                 // use compression ?
00312                 if(_imgTransType)
00313                 {
00314 #if 0
00315                     // set image size
00316                     pImage = new Image;
00317 
00318                     pImage->set(imgformat, tw, th, 1, 1, 1, 0.0,
00319                                 (UChar8 *) &imageData[0]);
00320 
00321                     // read buffer data into image
00322                     glReadPixels(tx, ty, tw, th, glformat, GL_UNSIGNED_BYTE,
00323                                  pImage->getData());
00324 
00325                     // bug maxsize is not big enugh
00326                     data.resize(_imgTransType->maxBufferSize(*pImage) + 1000);
00327                     dataSize = _imgTransType->store(*pImage, (UChar8 *) &data[0],
00328                                                                         data.size());
00329                     connection.putValue(dataSize);
00330                     connection.put(&data[0], dataSize);
00331                     imgtranssize += dataSize;
00332 
00333                     subRefP(pImage);
00334 #endif
00335                 }
00336                 else
00337                 {
00338                     data.resize(tw * th * componentCnt);
00339 
00340                     // read buffer data
00341                     glReadPixels(tx, ty, tw, th, glformat, GL_UNSIGNED_BYTE,
00342                                                      &data[0]);
00343                     dataSize = 0;
00344                     connection.putValue(dataSize);
00345                     connection.put(&data[0], tw * th * componentCnt);
00346                     imgtranssize += tw * th * componentCnt;
00347                 }
00348             }
00349 
00350             connection.flush();
00351         }
00352     }
00353 
00354     component = 0;
00355     connection.putValue(component);
00356     connection.flush();
00357 }
00358 
00361 void ClusterViewBuffer::send(PointConnection &connection, UInt32 component,
00362                              UInt32 toX, UInt32 toY)
00363 {
00364     send(connection, component, 0, 0, getBufferWidth(), getBufferHeight(), toX,
00365          toY);
00366 }
00367 
00368 /*-------------------------------------------------------------------------*/
00369 /*                            set parameters                               */
00370 
00375 void ClusterViewBuffer::setImgTransType(const char *mimeType)
00376 {
00377     if(mimeType == NULL)
00378     {
00379         _imgTransType = NULL;
00380     }
00381     else
00382     {
00383         _imgTransType = ImageFileHandler::the().getFileType(mimeType);
00384     }
00385 }
00386 
00392 void ClusterViewBuffer::setSubtileSize(UInt32 size)
00393 {
00394     _subTileSize = size;
00395 }
00396 
00400 void ClusterViewBuffer::setRGBADataType(UInt32 type,UInt32 size)
00401 {
00402     _rgbaDataType = type;
00403     _rgbaDataSize = size;
00404 }
00405 
00408 void ClusterViewBuffer::setRGBDataType(UInt32 type,UInt32 size)
00409 {
00410     _rgbDataType = type;
00411     _rgbDataSize = size;
00412 }
00413 
00416 void ClusterViewBuffer::setDepthDataType(UInt32 type,UInt32 size)
00417 {
00418     _depthDataType = type;
00419     _depthDataSize = size;
00420 }
00421 
00422 /*-------------------------------------------------------------------------*/
00423 /*                            get                                          */
00424 
00427 UInt32 ClusterViewBuffer::getBufferWidth(void)
00428 {
00429     GLint   view[4];
00430     glGetIntegerv(GL_VIEWPORT, view);
00431     return view[2];
00432 }
00433 
00436 UInt32 ClusterViewBuffer::getBufferHeight(void)
00437 {
00438     GLint   view[4];
00439     glGetIntegerv(GL_VIEWPORT, view);
00440     return view[3];
00441 }
00442 
00443 /*-------------------------------------------------------------------------*/
00444 /*                              cvs id's                                   */
00445 
00446 #ifdef __sgi
00447 #pragma set woff 1174
00448 #endif
00449 #ifdef OSG_LINUX_ICC
00450 #pragma warning(disable : 177)
00451 #endif
00452 
00453 namespace
00454 {
00455     static Char8    cvsid_cpp[] = "@(#)$Id:$";
00456     static Char8    cvsid_hpp[] = OSG_VIEWBUFFERHANDLER_HEADER_CVSID;
00457 }

Generated on Thu Aug 25 04:01:54 2005 for OpenSG by  doxygen 1.4.3