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
00046 LLLinkedList<LLDynamicTexture> LLDynamicTexture::sInstances[ LLDynamicTexture::ORDER_COUNT ];
00047 S32 LLDynamicTexture::sNumRenders = 0;
00048
00049
00050
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
00070
00071 LLDynamicTexture::~LLDynamicTexture()
00072 {
00073 releaseGLTexture();
00074 for( S32 order = 0; order < ORDER_COUNT; order++ )
00075 {
00076 LLDynamicTexture::sInstances[order].removeData(this);
00077 }
00078 }
00079
00080
00081
00082
00083 void LLDynamicTexture::releaseGLTexture()
00084 {
00085 if (mTexture.notNull())
00086 {
00087
00088 mTexture = NULL;
00089 }
00090 }
00091
00092
00093
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
00114 mTexture->createGLTexture(0, raw_image);
00115 mTexture->setClamp(mClamp, mClamp);
00116 }
00117
00118
00119
00120
00121 BOOL LLDynamicTexture::render()
00122 {
00123 return FALSE;
00124 }
00125
00126
00127
00128
00129 void LLDynamicTexture::preRender(BOOL clear_depth)
00130 {
00131 {
00132
00133 LLCoordScreen window_pos;
00134 gViewerWindow->getWindow()->getPosition( &window_pos );
00135 mOrigin.set(0, gViewerWindow->getWindowDisplayHeight() - mHeight);
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
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
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
00175 gViewerWindow->setupViewport();
00176
00177
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
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
00200
00201
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();
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
00240
00241
00242 void LLDynamicTexture::destroyGL()
00243 {
00244 }
00245
00246
00247
00248
00249
00250 void LLDynamicTexture::restoreGL()
00251 {
00252 }