lldynamictexture.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "lldynamictexture.h"
00035 #include "linked_lists.h"
00036 #include "llimagegl.h"
00037 #include "llglheaders.h"
00038 #include "llviewerwindow.h"
00039 #include "llviewercamera.h"
00040 #include "llviewercontrol.h"
00041 #include "llviewerimage.h"
00042 #include "llvertexbuffer.h"
00043 
00044 
00045 // static
00046 LLLinkedList<LLDynamicTexture> LLDynamicTexture::sInstances[ LLDynamicTexture::ORDER_COUNT ];
00047 S32 LLDynamicTexture::sNumRenders = 0;
00048 
00049 //-----------------------------------------------------------------------------
00050 // LLDynamicTexture()
00051 //-----------------------------------------------------------------------------
00052 LLDynamicTexture::LLDynamicTexture(S32 width, S32 height, S32 components, EOrder order, BOOL clamp) : 
00053         mWidth(width), 
00054         mHeight(height),
00055         mComponents(components),
00056         mTexture(NULL),
00057         mLastBindTime(0),
00058         mClamp(clamp)
00059 {
00060         llassert((1 <= components) && (components <= 4));
00061 
00062         generateGLTexture();
00063 
00064         llassert( 0 <= order && order < ORDER_COUNT );
00065         LLDynamicTexture::sInstances[ order ].addData(this);
00066 }
00067 
00068 //-----------------------------------------------------------------------------
00069 // LLDynamicTexture()
00070 //-----------------------------------------------------------------------------
00071 LLDynamicTexture::~LLDynamicTexture()
00072 {
00073         releaseGLTexture();
00074         for( S32 order = 0; order < ORDER_COUNT; order++ )
00075         {
00076                 LLDynamicTexture::sInstances[order].removeData(this);  // will fail in all but one case.
00077         }
00078 }
00079 
00080 //-----------------------------------------------------------------------------
00081 // releaseGLTexture()
00082 //-----------------------------------------------------------------------------
00083 void LLDynamicTexture::releaseGLTexture()
00084 {
00085         if (mTexture.notNull())
00086         {
00087 //              llinfos << "RELEASING " << (mWidth*mHeight*mComponents)/1024 << "K" << llendl;
00088                 mTexture = NULL;
00089         }
00090 }
00091 
00092 //-----------------------------------------------------------------------------
00093 // generateGLTexture()
00094 //-----------------------------------------------------------------------------
00095 void LLDynamicTexture::generateGLTexture()
00096 {
00097         generateGLTexture(-1, 0, 0, FALSE);
00098 }
00099 
00100 void LLDynamicTexture::generateGLTexture(LLGLint internal_format, LLGLenum primary_format, LLGLenum type_format, BOOL swap_bytes)
00101 {
00102         if (mComponents < 1 || mComponents > 4)
00103         {
00104                 llerrs << "Bad number of components in dynamic texture: " << mComponents << llendl;
00105         }
00106         releaseGLTexture();
00107         LLPointer<LLImageRaw> raw_image = new LLImageRaw(mWidth, mHeight, mComponents);
00108         mTexture = new LLImageGL(mWidth, mHeight, mComponents, FALSE);
00109         if (internal_format >= 0)
00110         {
00111                 mTexture->setExplicitFormat(internal_format, primary_format, type_format, swap_bytes);
00112         }
00113 //      llinfos << "ALLOCATING " << (mWidth*mHeight*mComponents)/1024 << "K" << llendl;
00114         mTexture->createGLTexture(0, raw_image);
00115         mTexture->setClamp(mClamp, mClamp);
00116 }
00117 
00118 //-----------------------------------------------------------------------------
00119 // render()
00120 //-----------------------------------------------------------------------------
00121 BOOL LLDynamicTexture::render()
00122 {
00123         return FALSE;
00124 }
00125 
00126 //-----------------------------------------------------------------------------
00127 // preRender()
00128 //-----------------------------------------------------------------------------
00129 void LLDynamicTexture::preRender(BOOL clear_depth)
00130 {
00131         {
00132                 // force rendering to on-screen portion of frame buffer
00133                 LLCoordScreen window_pos;
00134                 gViewerWindow->getWindow()->getPosition( &window_pos );
00135                 mOrigin.set(0, gViewerWindow->getWindowDisplayHeight() - mHeight);  // top left corner
00136 
00137                 if (window_pos.mX < 0)
00138                 {
00139                         mOrigin.mX = -window_pos.mX;
00140                 }
00141                 if (window_pos.mY < 0)
00142                 {
00143                         mOrigin.mY += window_pos.mY;
00144                 }
00145 
00146                 LLImageGL::unbindTexture(0, GL_TEXTURE_2D);
00147         }
00148         // Set up camera
00149         mCamera.setOrigin(*gCamera);
00150         mCamera.setAxes(*gCamera);
00151         mCamera.setAspect(gCamera->getAspect());
00152         mCamera.setView(gCamera->getView());
00153         mCamera.setNear(gCamera->getNear());
00154 
00155         glViewport(mOrigin.mX, mOrigin.mY, mWidth, mHeight);
00156         if (clear_depth)
00157         {
00158                 glClear(GL_DEPTH_BUFFER_BIT);
00159         }
00160 }
00161 
00162 //-----------------------------------------------------------------------------
00163 // postRender()
00164 //-----------------------------------------------------------------------------
00165 void LLDynamicTexture::postRender(BOOL success)
00166 {
00167         {
00168                 if (success)
00169                 {
00170                         success = mTexture->setSubImageFromFrameBuffer(0, 0, mOrigin.mX, mOrigin.mY, mWidth, mHeight);
00171                 }
00172         }
00173 
00174         // restore viewport
00175         gViewerWindow->setupViewport();
00176 
00177         // restore camera
00178         gCamera->setOrigin(mCamera);
00179         gCamera->setAxes(mCamera);
00180         gCamera->setAspect(mCamera.getAspect());
00181         gCamera->setView(mCamera.getView());
00182         gCamera->setNear(mCamera.getNear());
00183 }
00184 
00185 //-----------------------------------------------------------------------------
00186 // bindTexture()
00187 //-----------------------------------------------------------------------------
00188 void LLDynamicTexture::bindTexture()
00189 {
00190         LLViewerImage::bindTexture(mTexture,0);
00191 }
00192 
00193 void LLDynamicTexture::unbindTexture()
00194 {
00195         LLImageGL::unbindTexture(0, GL_TEXTURE_2D);
00196 }
00197 
00198 //-----------------------------------------------------------------------------
00199 // static
00200 // updateDynamicTextures()
00201 // Calls update on each dynamic texture.  Calls each group in order: "first," then "middle," then "last."
00202 //-----------------------------------------------------------------------------
00203 BOOL LLDynamicTexture::updateAllInstances()
00204 {
00205         sNumRenders = 0;
00206         if (gGLManager.mIsDisabled)
00207         {
00208                 return TRUE;
00209         }
00210 
00211         BOOL result = FALSE;
00212         for( S32 order = 0; order < ORDER_COUNT; order++ )
00213         {
00214                 for (LLDynamicTexture *dynamicTexture = LLDynamicTexture::sInstances[order].getFirstData();
00215                         dynamicTexture;
00216                         dynamicTexture = LLDynamicTexture::sInstances[order].getNextData())
00217                 {
00218                         if (dynamicTexture->needsRender())
00219                         {       
00220                                 dynamicTexture->preRender();    // Must be called outside of startRender()
00221 
00222                                 LLVertexBuffer::startRender();
00223                                 if (dynamicTexture->render())
00224                                 {
00225                                         result = TRUE;
00226                                         sNumRenders++;
00227                                 }
00228                                 LLVertexBuffer::stopRender();
00229                 
00230                                 dynamicTexture->postRender(result);
00231                         }
00232                 }
00233         }
00234 
00235         return result;
00236 }
00237 
00238 //-----------------------------------------------------------------------------
00239 // static
00240 // destroyGL()
00241 //-----------------------------------------------------------------------------
00242 void LLDynamicTexture::destroyGL()
00243 {
00244 }
00245 
00246 //-----------------------------------------------------------------------------
00247 // static
00248 // restoreGL()
00249 //-----------------------------------------------------------------------------
00250 void LLDynamicTexture::restoreGL()
00251 {
00252 }

Generated on Thu Jul 1 06:08:28 2010 for Second Life Viewer by  doxygen 1.4.7