lldrawpooltree.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "lldrawpooltree.h"
00035 
00036 #include "lldrawable.h"
00037 #include "llface.h"
00038 #include "llsky.h"
00039 #include "llviewerwindow.h"
00040 #include "llvotree.h"
00041 #include "pipeline.h"
00042 #include "llviewercamera.h"
00043 #include "llglslshader.h"
00044 #include "llglimmediate.h"
00045 
00046 S32 LLDrawPoolTree::sDiffTex = 0;
00047 static LLGLSLShader* shader = NULL;
00048 
00049 LLDrawPoolTree::LLDrawPoolTree(LLViewerImage *texturep) :
00050         LLFacePool(POOL_TREE),
00051         mTexturep(texturep)
00052 {
00053         mTexturep->bind(0);
00054         mTexturep->setClamp(FALSE, FALSE);
00055 }
00056 
00057 LLDrawPool *LLDrawPoolTree::instancePool()
00058 {
00059         return new LLDrawPoolTree(mTexturep);
00060 }
00061 
00062 void LLDrawPoolTree::prerender()
00063 {
00064         mVertexShaderLevel = LLShaderMgr::getVertexShaderLevel(LLShaderMgr::SHADER_OBJECT);
00065 }
00066 
00067 void LLDrawPoolTree::beginRenderPass(S32 pass)
00068 {
00069         LLFastTimer t(LLFastTimer::FTM_RENDER_TREES);
00070         glAlphaFunc(GL_GREATER, 0.5f);
00071         
00072         if (LLPipeline::sUnderWaterRender)
00073         {
00074                 shader = &gObjectSimpleWaterProgram;
00075         }
00076         else
00077         {
00078                 shader = &gObjectSimpleProgram;
00079         }
00080 
00081         if (gPipeline.canUseWindLightShadersOnObjects())
00082         {
00083                 shader->bind();
00084         }
00085         else
00086         {
00087                 gPipeline.enableLightsDynamic();
00088         }
00089 }
00090 
00091 void LLDrawPoolTree::render(S32 pass)
00092 {
00093         LLFastTimer t(LLFastTimer::FTM_RENDER_TREES);
00094 
00095         if (mDrawFace.empty())
00096         {
00097                 return;
00098         }
00099 
00100         LLGLEnable test(GL_ALPHA_TEST);
00101         LLOverrideFaceColor color(this, 1.f, 1.f, 1.f, 1.f);
00102 
00103         renderTree();
00104 }
00105 
00106 void LLDrawPoolTree::endRenderPass(S32 pass)
00107 {
00108         LLFastTimer t(LLFastTimer::FTM_RENDER_TREES);
00109         glAlphaFunc(GL_GREATER, 0.01f);
00110         
00111         if (gPipeline.canUseWindLightShadersOnObjects())
00112         {
00113                 shader->unbind();
00114         }
00115 }
00116 
00117 void LLDrawPoolTree::renderForSelect()
00118 {
00119         if (mDrawFace.empty())
00120         {
00121                 return;
00122         }
00123 
00124         LLOverrideFaceColor color(this, 1.f, 1.f, 1.f, 1.f);
00125 
00126         LLGLSObjectSelectAlpha gls_alpha;
00127 
00128         gGL.blendFunc(GL_ONE, GL_ZERO);
00129         glAlphaFunc(GL_GREATER, 0.5f);
00130 
00131         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,          GL_COMBINE_ARB);
00132         glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,           GL_REPLACE);
00133         glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB,         GL_MODULATE);
00134 
00135         glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB,           GL_PREVIOUS);
00136         glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB,          GL_SRC_COLOR);
00137 
00138         glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB,         GL_TEXTURE);
00139         glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB,        GL_SRC_ALPHA);
00140 
00141         glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB,         GL_PRIMARY_COLOR_ARB);
00142         glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB,        GL_SRC_ALPHA);
00143 
00144         renderTree(TRUE);
00145 
00146         glAlphaFunc(GL_GREATER, 0.01f);
00147         gGL.blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00148 
00149         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00150 }
00151 
00152 void LLDrawPoolTree::renderTree(BOOL selecting)
00153 {
00154         LLGLState normalize(GL_NORMALIZE, TRUE);
00155         
00156         // Bind the texture for this tree.
00157         LLViewerImage::bindTexture(mTexturep,sDiffTex);
00158                 
00159         U32 indices_drawn = 0;
00160 
00161         glMatrixMode(GL_MODELVIEW);
00162         
00163         for (std::vector<LLFace*>::iterator iter = mDrawFace.begin();
00164                  iter != mDrawFace.end(); iter++)
00165         {
00166                 LLFace *face = *iter;
00167                 LLDrawable *drawablep = face->getDrawable();
00168 
00169                 if (drawablep->isDead() || face->mVertexBuffer.isNull())
00170                 {
00171                         continue;
00172                 }
00173 
00174                 face->mVertexBuffer->setBuffer(LLDrawPoolTree::VERTEX_DATA_MASK);
00175                 U16* indicesp = (U16*) face->mVertexBuffer->getIndicesPointer();
00176 
00177                 // Render each of the trees
00178                 LLVOTree *treep = (LLVOTree *)drawablep->getVObj().get();
00179 
00180                 LLColor4U color(255,255,255,255);
00181 
00182                 if (!selecting || treep->mGLName != 0)
00183                 {
00184                         if (selecting)
00185                         {
00186                                 S32 name = treep->mGLName;
00187                                 
00188                                 color = LLColor4U((U8)(name >> 16), (U8)(name >> 8), (U8)name, 255);
00189                         }
00190                         
00191                         gGLLastMatrix = NULL;
00192                         glLoadMatrixd(gGLModelView);
00193                         //glPushMatrix();
00194                         F32 mat[16];
00195                         for (U32 i = 0; i < 16; i++)
00196                                 mat[i] = (F32) gGLModelView[i];
00197 
00198                         LLMatrix4 matrix(mat);
00199                         
00200                         // Translate to tree base  HACK - adjustment in Z plants tree underground
00201                         const LLVector3 &pos_agent = treep->getPositionAgent();
00202                         //glTranslatef(pos_agent.mV[VX], pos_agent.mV[VY], pos_agent.mV[VZ] - 0.1f);
00203                         LLMatrix4 trans_mat;
00204                         trans_mat.setTranslation(pos_agent.mV[VX], pos_agent.mV[VY], pos_agent.mV[VZ] - 0.1f);
00205                         trans_mat *= matrix;
00206                         
00207                         // Rotate to tree position and bend for current trunk/wind
00208                         // Note that trunk stiffness controls the amount of bend at the trunk as 
00209                         // opposed to the crown of the tree
00210                         // 
00211                         const F32 TRUNK_STIFF = 22.f;
00212                         
00213                         LLQuaternion rot = 
00214                                 LLQuaternion(treep->mTrunkBend.magVec()*TRUNK_STIFF*DEG_TO_RAD, LLVector4(treep->mTrunkBend.mV[VX], treep->mTrunkBend.mV[VY], 0)) *
00215                                 LLQuaternion(90.f*DEG_TO_RAD, LLVector4(0,0,1)) *
00216                                 treep->getRotation();
00217 
00218                         LLMatrix4 rot_mat(rot);
00219                         rot_mat *= trans_mat;
00220 
00221                         F32 radius = treep->getScale().magVec()*0.05f;
00222                         LLMatrix4 scale_mat;
00223                         scale_mat.mMatrix[0][0] = 
00224                                 scale_mat.mMatrix[1][1] =
00225                                 scale_mat.mMatrix[2][2] = radius;
00226 
00227                         scale_mat *= rot_mat;
00228 
00229                         const F32 THRESH_ANGLE_FOR_BILLBOARD = 15.f;
00230                         const F32 BLEND_RANGE_FOR_BILLBOARD = 3.f;
00231 
00232                         F32 droop = treep->mDroop + 25.f*(1.f - treep->mTrunkBend.magVec());
00233                         
00234                         S32 stop_depth = 0;
00235                         F32 app_angle = treep->getAppAngle()*LLVOTree::sTreeFactor;
00236                         F32 alpha = 1.0;
00237                         S32 trunk_LOD = 0;
00238 
00239                         for (S32 j = 0; j < 4; j++)
00240                         {
00241 
00242                                 if (app_angle > LLVOTree::sLODAngles[j])
00243                                 {
00244                                         trunk_LOD = j;
00245                                         break;
00246                                 }
00247                         } 
00248 
00249                         if (app_angle < (THRESH_ANGLE_FOR_BILLBOARD - BLEND_RANGE_FOR_BILLBOARD))
00250                         {
00251                                 //
00252                                 //  Draw only the billboard 
00253                                 //
00254                                 //  Only the billboard, can use closer to normal alpha func.
00255                                 stop_depth = -1;
00256                                 LLFacePool::LLOverrideFaceColor clr(this, color); 
00257                                 indices_drawn += treep->drawBranchPipeline(scale_mat, indicesp, trunk_LOD, stop_depth, treep->mDepth, treep->mTrunkDepth, 1.0, treep->mTwist, droop, treep->mBranches, alpha);
00258                         }
00259                         else // if (app_angle > (THRESH_ANGLE_FOR_BILLBOARD + BLEND_RANGE_FOR_BILLBOARD))
00260                         {
00261                                 //
00262                                 //  Draw only the full geometry tree
00263                                 //
00264                                 //stop_depth = (app_angle < THRESH_ANGLE_FOR_RECURSION_REDUCTION);
00265                                 LLFacePool::LLOverrideFaceColor clr(this, color); 
00266                                 indices_drawn += treep->drawBranchPipeline(scale_mat, indicesp, trunk_LOD, stop_depth, treep->mDepth, treep->mTrunkDepth, 1.0, treep->mTwist, droop, treep->mBranches, alpha);
00267                         }
00268                         
00269                         //glPopMatrix();
00270                 }
00271         }
00272 }
00273 
00274 BOOL LLDrawPoolTree::verify() const
00275 {
00276 /*      BOOL ok = TRUE;
00277 
00278         if (!ok)
00279         {
00280                 printDebugInfo();
00281         }
00282         return ok;*/
00283 
00284         return TRUE;
00285 }
00286 
00287 LLViewerImage *LLDrawPoolTree::getTexture()
00288 {
00289         return mTexturep;
00290 }
00291 
00292 LLViewerImage *LLDrawPoolTree::getDebugTexture()
00293 {
00294         return mTexturep;
00295 }
00296 
00297 
00298 LLColor3 LLDrawPoolTree::getDebugColor() const
00299 {
00300         return LLColor3(1.f, 0.f, 1.f);
00301 }
00302 

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