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

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