lldynamictexture.cpp

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

Generated on Fri May 16 08:33:20 2008 for SecondLife by  doxygen 1.5.5