Re-worked animated linden trees to reduce duplicate code and provide a cleaner diff.

This commit is contained in:
Shyotl
2014-06-24 01:17:47 -05:00
parent a59e87a896
commit 3ead892b1c
3 changed files with 106 additions and 287 deletions

View File

@@ -104,18 +104,37 @@ void LLDrawPoolTree::render(S32 pass)
LLGLState test(GL_ALPHA_TEST, LLGLSLShader::sNoFixedFunction ? 0 : 1);
LLOverrideFaceColor color(this, 1.f, 1.f, 1.f, 1.f);
static LLCachedControl<bool> sRenderAnimateTrees("RenderAnimateTrees", false);
if (sRenderAnimateTrees)
{
renderTree();
}
else
gGL.getTexUnit(sDiffTex)->bind(mTexturep);
for (std::vector<LLFace*>::iterator iter = mDrawFace.begin();
iter != mDrawFace.end(); iter++)
{
LLFace *face = *iter;
if(face->getViewerObject())
{
LLVOTree* pTree = dynamic_cast<LLVOTree*>(face->getViewerObject());
if(pTree && !pTree->mDrawList.empty() )
{
LLMatrix4a* model_matrix = &(face->getDrawable()->getRegion()->mRenderMatrix);
gGL.loadMatrix(gGLModelView);
gGL.multMatrix(*model_matrix);
gPipeline.mMatrixOpCount++;
for(std::vector<LLPointer<LLDrawInfo> >::iterator iter2 = pTree->mDrawList.begin();
iter2 != pTree->mDrawList.end(); iter2++)
{
LLDrawInfo& params = *iter2->get();
gGL.pushMatrix();
gGL.multMatrix(*params.mModelMatrix);
gPipeline.mMatrixOpCount++;
params.mVertexBuffer->setBuffer(LLDrawPoolTree::VERTEX_DATA_MASK);
params.mVertexBuffer->drawRange(params.mDrawMode, params.mStart, params.mEnd, params.mCount, params.mOffset);
gGL.popMatrix();
}
continue;
}
}
LLVertexBuffer* buff = face->getVertexBuffer();
if(buff)
@@ -212,125 +231,6 @@ void LLDrawPoolTree::endShadowPass(S32 pass)
gDeferredTreeShadowProgram.unbind();
}
//
void LLDrawPoolTree::renderTree(BOOL selecting)
{
LLGLState normalize(GL_NORMALIZE, TRUE);
// Bind the texture for this tree.
gGL.getTexUnit(sDiffTex)->bind(mTexturep.get(), TRUE);
U32 indices_drawn = 0;
gGL.matrixMode(LLRender::MM_MODELVIEW);
for (std::vector<LLFace*>::iterator iter = mDrawFace.begin();
iter != mDrawFace.end(); iter++)
{
LLFace *face = *iter;
LLDrawable *drawablep = face->getDrawable();
if (drawablep->isDead() || !face->getVertexBuffer())
{
continue;
}
face->getVertexBuffer()->setBuffer(LLDrawPoolTree::VERTEX_DATA_MASK);
U16* indicesp = (U16*) face->getVertexBuffer()->getIndicesPointer();
// Render each of the trees
LLVOTree *treep = (LLVOTree *)drawablep->getVObj().get();
LLColor4U color(255,255,255,255);
if (!selecting || treep->mGLName != 0)
{
if (selecting)
{
S32 name = treep->mGLName;
color = LLColor4U((U8)(name >> 16), (U8)(name >> 8), (U8)name, 255);
}
gGLLastMatrix = NULL;
gGL.loadMatrix(gGLModelView);
//gGL.pushMatrix();
LLMatrix4a matrix(gGLModelView);
// Translate to tree base HACK - adjustment in Z plants tree underground
const LLVector3 &pos_agent = treep->getPositionAgent();
//gGL.translatef(pos_agent.mV[VX], pos_agent.mV[VY], pos_agent.mV[VZ] - 0.1f);
LLMatrix4a trans_mat = matrix;
trans_mat.applyTranslation_affine(pos_agent - LLVector3(0.f,0.f,1.f));
// Rotate to tree position and bend for current trunk/wind
// Note that trunk stiffness controls the amount of bend at the trunk as
// opposed to the crown of the tree
//
const F32 TRUNK_STIFF = 22.f;
LLQuaternion rot =
LLQuaternion(treep->mTrunkBend.magVec()*TRUNK_STIFF*DEG_TO_RAD, LLVector4(treep->mTrunkBend.mV[VX], treep->mTrunkBend.mV[VY], 0)) *
LLQuaternion(90.f*DEG_TO_RAD, LLVector4(0,0,1)) *
treep->getRotation();
LLMatrix4a rot_mat = trans_mat;
rot_mat.mul(LLQuaternion2(rot));
F32 radius = treep->getScale().magVec()*0.05f;
LLMatrix4a scale_mat = rot_mat;
scale_mat.applyScale_affine(radius);
//TO-DO: Make these set-able?
const F32 THRESH_ANGLE_FOR_BILLBOARD = 7.5f; //Made LoD now a little less aggressive here -Shyotl
const F32 BLEND_RANGE_FOR_BILLBOARD = 1.5f;
F32 droop = treep->mDroop + 25.f*(1.f - treep->mTrunkBend.magVec());
S32 stop_depth = 0;
F32 app_angle = treep->getAppAngle()*LLVOTree::sTreeFactor;
F32 alpha = 1.0;
S32 trunk_LOD = LLVOTree::sMAX_NUM_TREE_LOD_LEVELS;
for (S32 j = 0; j < 4; j++)
{
if (app_angle > LLVOTree::sLODAngles[j])
{
trunk_LOD = j;
break;
}
}
if(trunk_LOD >= LLVOTree::sMAX_NUM_TREE_LOD_LEVELS)
{
continue ; //do not render.
}
if (app_angle < (THRESH_ANGLE_FOR_BILLBOARD - BLEND_RANGE_FOR_BILLBOARD))
{
//
// Draw only the billboard
//
// Only the billboard, can use closer to normal alpha func.
stop_depth = -1;
LLFacePool::LLOverrideFaceColor clr(this, color);
indices_drawn += treep->drawBranchPipeline(scale_mat, indicesp, trunk_LOD, stop_depth, treep->mDepth, treep->mTrunkDepth, 1.0, treep->mTwist, droop, treep->mBranches, alpha);
}
else // if (app_angle > (THRESH_ANGLE_FOR_BILLBOARD + BLEND_RANGE_FOR_BILLBOARD))
{
//
// Draw only the full geometry tree
//
LLFacePool::LLOverrideFaceColor clr(this, color);
indices_drawn += treep->drawBranchPipeline(scale_mat, indicesp, trunk_LOD, stop_depth, treep->mDepth, treep->mTrunkDepth, 1.0, treep->mTwist, droop, treep->mBranches, alpha);
}
//gGL.popMatrix();
}
}
}//
BOOL LLDrawPoolTree::verify() const
{
/* BOOL ok = TRUE;

View File

@@ -107,6 +107,10 @@ LLVOTree::~LLVOTree()
delete[] mData;
mData = NULL;
}
for(std::vector<LLPointer<LLDrawInfo> >::iterator iter = mDrawList.begin(); iter != mDrawList.end(); iter++)
{
delete (*iter)->mModelMatrix;
}
}
//static
@@ -397,6 +401,11 @@ void LLVOTree::idleUpdate(LLAgent &agent, LLWorld &world, const F64 &time)
mTrunkVel.normalize();
}
}
else
{
mTrunkBend.clear();
mTrunkVel.clear();
}
S32 trunk_LOD = sMAX_NUM_TREE_LOD_LEVELS;
F32 app_angle = getAppAngle()*LLVOTree::sTreeFactor;
@@ -446,6 +455,10 @@ void LLVOTree::idleUpdate(LLAgent &agent, LLWorld &world, const F64 &time)
}
}
}
else
{
gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_ALL, FALSE);
}
mTrunkLOD = trunk_LOD;
//return TRUE;
@@ -541,6 +554,12 @@ BOOL LLVOTree::updateGeometry(LLDrawable *drawable)
{
LLFastTimer ftm(FTM_UPDATE_TREE);
for(std::vector<LLPointer<LLDrawInfo> >::iterator iter = mDrawList.begin(); iter != mDrawList.end(); iter++)
{
delete (*iter)->mModelMatrix;
}
mDrawList.clear();
if(mTrunkLOD >= sMAX_NUM_TREE_LOD_LEVELS) //do not display the tree.
{
mReferenceBuffer = NULL ;
@@ -582,8 +601,7 @@ BOOL LLVOTree::updateGeometry(LLDrawable *drawable)
max_vertices += sLODVertexCount[lod];
}
static LLCachedControl<bool> sRenderAnimateTrees(gSavedSettings, "RenderAnimateTrees");
mReferenceBuffer = new LLVertexBuffer(LLDrawPoolTree::VERTEX_DATA_MASK, sRenderAnimateTrees ? GL_STATIC_DRAW_ARB : 0);
mReferenceBuffer = new LLVertexBuffer(LLDrawPoolTree::VERTEX_DATA_MASK, GL_STATIC_DRAW_ARB);
mReferenceBuffer->allocateBuffer(max_vertices, max_indices, TRUE);
LLStrider<LLVector3> vertices;
@@ -886,17 +904,9 @@ BOOL LLVOTree::updateGeometry(LLDrawable *drawable)
llassert(vertex_count == max_vertices);
llassert(index_count == max_indices);
}
static LLCachedControl<bool> sRenderAnimateTrees(gSavedSettings, "RenderAnimateTrees");
if (sRenderAnimateTrees)
{
mDrawable->getFace(0)->setVertexBuffer(mReferenceBuffer);
}
else
{
//generate tree mesh
updateMesh();
}
//generate tree mesh
updateMesh();
return TRUE;
}
@@ -926,7 +936,6 @@ void LLVOTree::updateMesh()
rot_mat.mul(LLQuaternion2(rot));
F32 radius = getScale().magVec()*0.05f;
LLMatrix4a scale_mat = rot_mat;
rot_mat.applyScale_affine(radius);
// const F32 THRESH_ANGLE_FOR_BILLBOARD = 15.f;
@@ -944,9 +953,6 @@ void LLVOTree::updateMesh()
LLFace* facep = mDrawable->getFace(0);
if (!facep) return;
LLVertexBuffer* buff = new LLVertexBuffer(LLDrawPoolTree::VERTEX_DATA_MASK, GL_STATIC_DRAW_ARB);
buff->allocateBuffer(vert_count, index_count, TRUE);
facep->setVertexBuffer(buff);
LLStrider<LLVector4a> vertices;
LLStrider<LLVector4a> normals;
@@ -954,15 +960,32 @@ void LLVOTree::updateMesh()
LLStrider<U16> indices;
U16 idx_offset = 0;
buff->getVertexStrider(vertices);
buff->getNormalStrider(normals);
buff->getTexCoord0Strider(tex_coords);
buff->getIndexStrider(indices);
LLVertexBuffer* buff = NULL;
static LLCachedControl<bool> sRenderAnimateTrees("RenderAnimateTrees", false);
if (sRenderAnimateTrees)
{
facep->setVertexBuffer(NULL);
}
else
{
buff = new LLVertexBuffer(LLDrawPoolTree::VERTEX_DATA_MASK, GL_STATIC_DRAW_ARB);
buff->allocateBuffer(vert_count, index_count, TRUE);
facep->setVertexBuffer(buff);
buff->getVertexStrider(vertices);
buff->getNormalStrider(normals);
buff->getTexCoord0Strider(tex_coords);
buff->getIndexStrider(indices);
}
genBranchPipeline(vertices, normals, tex_coords, indices, idx_offset, rot_mat, mTrunkLOD, stop_depth, mDepth, mTrunkDepth, 1.0, mTwist, droop, mBranches, alpha);
mReferenceBuffer->flush();
buff->flush();
if(buff)
{
mReferenceBuffer->flush();
buff->flush();
}
}
void LLVOTree::appendMesh(LLStrider<LLVector4a>& vertices,
@@ -982,31 +1005,41 @@ void LLVOTree::appendMesh(LLStrider<LLVector4a>& vertices,
LLStrider<LLVector2> t;
LLStrider<U16> idx;
mReferenceBuffer->getVertexStrider(v);
mReferenceBuffer->getNormalStrider(n);
mReferenceBuffer->getTexCoord0Strider(t);
mReferenceBuffer->getIndexStrider(idx);
//copy/transform vertices into mesh - check
for (S32 i = 0; i < vert_count; i++)
{
U16 index = vert_start + i;
matrix.affineTransform(v[index],*vertices++);
LLVector4a& norm = *normals++;
norm_mat.perspectiveTransform(n[index],norm);
norm.normalize3fast();
*tex_coords++ = t[index];
}
//copy offset indices into mesh - check
for (S32 i = 0; i < index_count; i++)
static LLCachedControl<bool> sRenderAnimateTrees(gSavedSettings, "RenderAnimateTrees");
if(sRenderAnimateTrees) //Instead of manipulating the vbo, use the reference vbo and apply the transformation matrix to the matrix stack at draw-time.
{
U16 index = index_offset + i;
*indices++ = idx[index]-vert_start+cur_idx;
LLDrawInfo* draw_info = new LLDrawInfo(vert_start,vert_start+vert_count-1,index_count,index_offset,NULL,mReferenceBuffer);
draw_info->mModelMatrix = new LLMatrix4a(matrix); //Make sure these are deleted before clearing/destructing mDrawList!
mDrawList.push_back(draw_info);
}
else
{
mReferenceBuffer->getVertexStrider(v);
mReferenceBuffer->getNormalStrider(n);
mReferenceBuffer->getTexCoord0Strider(t);
mReferenceBuffer->getIndexStrider(idx);
//copy/transform vertices into mesh - check
for (S32 i = 0; i < vert_count; i++)
{
U16 index = vert_start + i;
matrix.affineTransform(v[index],*vertices++);
LLVector4a& norm = *normals++;
norm_mat.perspectiveTransform(n[index],norm);
norm.normalize3fast();
*tex_coords++ = t[index];
}
//increment index offset - check
cur_idx += vert_count;
//copy offset indices into mesh - check
for (S32 i = 0; i < index_count; i++)
{
U16 index = index_offset + i;
*indices++ = idx[index]-vert_start+cur_idx;
}
//increment index offset - check
cur_idx += vert_count;
}
}
@@ -1140,119 +1173,6 @@ void LLVOTree::calcNumVerts(U32& vert_count, U32& index_count, S32 trunk_LOD, S3
}
}
U32 LLVOTree::drawBranchPipeline(LLMatrix4a& matrix, U16* indicesp, S32 trunk_LOD, S32 stop_level, U16 depth, U16 trunk_depth, F32 scale, F32 twist, F32 droop, F32 branches, F32 alpha)
{
U32 ret = 0;
//
// Draws a tree by recursing, drawing branches and then a 'leaf' texture.
// If stop_level = -1, simply draws the whole tree as a billboarded texture
//
static F32 constant_twist;
static F32 width = 0;
//F32 length = ((scale == 1.f)? mTrunkLength:mBranchLength);
//F32 aspect = ((scale == 1.f)? mTrunkAspect:mBranchAspect);
F32 length = ((trunk_depth || (scale == 1.f))? mTrunkLength:mBranchLength);
F32 aspect = ((trunk_depth || (scale == 1.f))? mTrunkAspect:mBranchAspect);
constant_twist = 360.f/branches;
if (!LLPipeline::sReflectionRender && stop_level >= 0)
{
//
// Draw the tree using recursion
//
if (depth > stop_level)
{
{
llassert(sLODIndexCount[trunk_LOD] > 0);
width = scale * length * aspect;
LLMatrix4a scale_mat = matrix;
scale_mat.applyScale_affine(width, width, scale*length);
gGL.loadMatrix(scale_mat);
gGL.syncMatrices();
glDrawElements(GL_TRIANGLES, sLODIndexCount[trunk_LOD], GL_UNSIGNED_SHORT, indicesp + sLODIndexOffset[trunk_LOD]);
gPipeline.addTrianglesDrawn(LEAF_INDICES);
stop_glerror();
ret += sLODIndexCount[trunk_LOD];
}
LLMatrix4a trans_matrix = matrix;
trans_matrix.applyTranslation_affine(0.f,0.f,scale*length);
const LLMatrix4a& trans_mat = trans_matrix;
// Recurse to create more branches
for (S32 i=0; i < (S32)branches; i++)
{
LLQuaternion rot =
LLQuaternion(20.f*DEG_TO_RAD, LLVector4(0.f, 0.f, 1.f)) *
LLQuaternion(droop*DEG_TO_RAD, LLVector4(0.f, 1.f, 0.f)) *
LLQuaternion(((constant_twist + ((i%2==0)?twist:-twist))*i)*DEG_TO_RAD, LLVector4(0.f, 0.f, 1.f));
LLMatrix4a rot_mat = trans_mat;
rot_mat.mul(LLQuaternion2(rot));
ret += drawBranchPipeline(rot_mat, indicesp, trunk_LOD, stop_level, depth - 1, 0, scale*mScaleStep, twist, droop, branches, alpha);
}
// Recurse to continue trunk
if (trunk_depth)
{
static const LLMatrix4a srot_mat = gGL.genRot(70.5f,0.f,0.f,1.f);
LLMatrix4a rot_mat;
rot_mat.setMul(trans_mat, srot_mat); // rotate a bit around Z when ascending
ret += drawBranchPipeline(rot_mat, indicesp, trunk_LOD, stop_level, depth, trunk_depth-1, scale*mScaleStep, twist, droop, branches, alpha);
}
}
else
{
//
// Draw leaves as two 90 deg crossed quads with leaf textures
//
{
LLMatrix4a scale_mat = matrix;
scale_mat.applyScale_affine(scale*mLeafScale);
gGL.loadMatrix(scale_mat);
gGL.syncMatrices();
glDrawElements(GL_TRIANGLES, LEAF_INDICES, GL_UNSIGNED_SHORT, indicesp);
gPipeline.addTrianglesDrawn(LEAF_INDICES);
stop_glerror();
ret += LEAF_INDICES;
}
}
}
else
{
//
// Draw the tree as a single billboard texture
//
LLMatrix4a scale_mat = matrix;
scale_mat.applyScale_affine(mBillboardScale*mBillboardRatio);
gGL.matrixMode(LLRender::MM_TEXTURE);
gGL.translatef(0.0, -0.5, 0.0);
gGL.matrixMode(LLRender::MM_MODELVIEW);
gGL.loadMatrix(scale_mat);
gGL.syncMatrices();
glDrawElements(GL_TRIANGLES, LEAF_INDICES, GL_UNSIGNED_SHORT, indicesp);
gPipeline.addTrianglesDrawn(LEAF_INDICES);
stop_glerror();
ret += LEAF_INDICES;
gGL.matrixMode(LLRender::MM_TEXTURE);
gGL.loadIdentity();
gGL.matrixMode(LLRender::MM_MODELVIEW);
}
return ret;
}
void LLVOTree::updateRadius()
{
if (mDrawable.isNull())

View File

@@ -113,9 +113,6 @@ public:
F32 branches,
F32 alpha);
U32 drawBranchPipeline(LLMatrix4a& matrix, U16* indicesp, S32 trunk_LOD, S32 stop_level, U16 depth, U16 trunk_depth, F32 scale, F32 twist, F32 droop, F32 branches, F32 alpha);
/*virtual*/ BOOL lineSegmentIntersect(const LLVector4a& start, const LLVector4a& end,
S32 face = -1, // which face to check, -1 = ALL_SIDES
BOOL pick_transparent = FALSE,
@@ -196,6 +193,8 @@ protected:
U32 mFrameCount;
std::vector<LLPointer<LLDrawInfo> > mDrawList;
typedef std::map<U32, TreeSpeciesData*> SpeciesMap;
static SpeciesMap sSpeciesTable;