llanimalcontrols.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "llanimalcontrols.h"
00035 #include "llglheaders.h"
00036 #include "llsphere.h"
00037 #include "llrand.h"
00038 #include "llviewerobject.h"
00039 
00040 /*
00041 //const F32 ANIMAL_CONTROLS_MAX_TENSION_FORCE = 0.99f;// I'll explain in a minute...
00042 //const F32 ANIMAL_CONTROLS_MINIMUM_RADIUS      = 0.005f;
00043 
00044 const F32 OPACITY               = 1.0f;
00045 const int _X_                   = 0;
00046 const int _Y_                   = 1;
00047 const int _Z_                   = 2;
00048 const F32 BODY_RADIUS   = 0.2f;
00049 const F32 HEAD_RADIUS   = 0.1f;
00050 const F32 ONE_HALF              = 0.5f;
00051 
00052 //-----------------------------------------------
00053 // constructor
00054 //-----------------------------------------------
00055 LLHUDAnimalControls::LLHUDAnimalControls()
00056 : LLHUDObject( LL_HUD_ANIMAL_CONTROLS )// cal the base class and pass this in, K?
00057 {
00058         mParentObject = NULL;
00059 
00060 }//------------------------------------------------------------------------------
00061 
00062 
00063 
00064 
00065 //---------------------------------------------------------------------------------
00066 //---------------------------------------------------------------------------------
00067 // Virtual Server methods...
00068 //---------------------------------------------------------------------------------
00069 //---------------------------------------------------------------------------------
00070 LLAnimalControlsVirtualServer::LLAnimalControlsVirtualServer()
00071 {
00072         focusPosition.setVec( 0.0f, 0.0f, 0.0f );
00073         animalPosition.setVec( 0.0f, 0.0f, 0.0f );
00074         animalRotation                  = LLQuaternion::DEFAULT;
00075         fakeClock                               = 0;
00076         settingFocusPosition    = false;
00077 
00078 }//---------------------------------------------------------------------------------
00079 
00080 
00081 
00082 //---------------------------------------------------------------------------------
00083 void LLAnimalControlsVirtualServer::update()
00084 {
00085         fakeClock ++;
00086 
00087         if ( settingFocusPosition )
00088         {
00089                 settingFocusPosition = false;
00090         }
00091 
00092         if ( fakeClock == 20 )
00093         {
00094                 fakeClock = 0;
00095 
00096                 settingFocusPosition = true;
00097 
00098                 LLMatrix3 bodyRotationMatrix = animalRotation.getMatrix3();
00099 
00100                 LLVector3 forwardDirection      = bodyRotationMatrix.getLeftRow();
00101                 LLVector3 leftDirection         = bodyRotationMatrix.getFwdRow();
00102 
00103                 forwardDirection.mV[_Z_] = 0.0f;
00104                 forwardDirection.normVec();
00105                 
00106                 focusPosition = animalPosition + forwardDirection * 5.0f;
00107 
00108                 F32 leftRightSpan = 5.0f;
00109 
00110                 F32 randomSpan = ll_frand( leftRightSpan );
00111 
00112                 focusPosition += leftDirection * ( - leftRightSpan * ONE_HALF + randomSpan );
00113         }
00114 
00115 }//------------------------------------------------------------------------------
00116 
00117 
00118 
00119 //---------------------------------------------------------------------------------
00120 bool LLAnimalControlsVirtualServer::getSettingFocusPosition()
00121 {
00122         return settingFocusPosition;
00123 
00124 }//------------------------------------------------------------------------------
00125 
00126 //---------------------------------------------------------------------------------
00127 LLVector3 LLAnimalControlsVirtualServer::getFocusPosition()
00128 {
00129         return focusPosition;
00130 
00131 }//------------------------------------------------------------------------------
00132 
00133 
00134 //---------------------------------------------------------------------------------
00135 void LLAnimalControlsVirtualServer::setParentPositionAndRotation( LLVector3 p, LLQuaternion r )
00136 {
00137         animalPosition  = p;
00138         animalRotation  = r;
00139 
00140 }//------------------------------------------------------------------------------
00141 
00142 //---------------------------------------------------------------------------------
00143 //---------------------------------------------------------------------------------
00144 // End of Virtual Server methods...
00145 //---------------------------------------------------------------------------------
00146 //---------------------------------------------------------------------------------
00147 
00148 
00149 
00150 
00151 //---------------------------------------------------------------------------------
00152 //---------------------------------------------------------------------------------
00153 // Head behavior methods...
00154 //---------------------------------------------------------------------------------
00155 //---------------------------------------------------------------------------------
00156 LLAnimalControlsHeadBehavior::LLAnimalControlsHeadBehavior()
00157 {
00158         mPosition.setVec                ( 0.0f, 0.0f, 0.0f );
00159         mFocusPosition.setVec   ( 0.0f, 0.0f, 0.0f );
00160         mBodyPosition.setVec    ( 0.0f, 0.0f, 0.0f );
00161         mRotation               = LLQuaternion::DEFAULT;
00162         mEyesBlinking   = true;
00163         mEyeBlinkRate   = 0.1f;
00164         mBodyRotation   = LLQuaternion::DEFAULT;
00165 
00166         LLFlexibleObjectAttributes headFeatherAttributes;
00167         headFeatherAttributes.mAnchorPositionOffset.setVec( 0.0f, 0.0f, 0.1f );
00168         headFeatherAttributes.mAnchorDirection.setVec( 0.0f, 1.0f, 1.0f );
00169         headFeatherAttributes.mAnchorDirection.normVec();
00170         headFeatherAttributes.mColor = LLColor4( 1.0f, 0.4f, 0.0f, 1.0f );
00171         headFeatherAttributes.mAnchorRadius = 0.01f;
00172         headFeatherAttributes.mLength = 0.4f;
00173         headFeatherAttributes.mEndRadius = 0.06f;
00174         headFeatherAttributes.mRenderStyle = "tube";
00175 
00176         mFeather.setAttributes( headFeatherAttributes );
00177 
00178 }//------------------------------------------------------------------
00179 
00180 //------------------------------------------------------------------
00181 void LLAnimalControlsHeadBehavior::setBodyPositionAndRotation( LLVector3 p, LLQuaternion r )
00182 {
00183         mBodyPosition = p;
00184         mBodyRotation = r;
00185 
00186 }//------------------------------------------------------------------
00187 
00188 //------------------------------------------------------------------
00189 void LLAnimalControlsHeadBehavior::setFocusPosition( LLVector3 f )
00190 {
00191         mFocusPosition = f;
00192 
00193 }//------------------------------------------------------------------
00194 
00195 
00196 //------------------------------------------------------------------
00197 void LLAnimalControlsHeadBehavior::update()
00198 {
00199         //--------------------------------------------------------------------------
00200         // Let's get the parent orientation and associated components
00201         //--------------------------------------------------------------------------
00202         LLQuaternion    parentOrientation               = mBodyRotation;
00203         LLMatrix3               parentMatrix                    = parentOrientation.getMatrix3();
00204         LLVector3               parentForwardDirection  = parentMatrix.getLeftRow();
00205         LLVector3               parentUpwardDirection   = parentMatrix.getUpRow();
00206 
00207         //--------------------------------------------------------------------------
00208         // head position is based on this
00209         //--------------------------------------------------------------------------
00210         mPosition = mBodyPosition + parentForwardDirection * 0.5f + parentUpwardDirection * 0.5f;
00211 
00212         //--------------------------------------------------------------------------------------------------
00213         // let's figure out how the head would have to rotate in order to aim in the focus direction
00214         //--------------------------------------------------------------------------------------------------
00215         LLVector3 headFocusDirection = mFocusPosition - mPosition;
00216         LLQuaternion headRotationToFocusDirection;
00217         headRotationToFocusDirection.shortestArc( parentForwardDirection, headFocusDirection );
00218 
00219         //--------------------------------------------------------------------------------------------------
00220         // Now we rotate the head towards its focus direction
00221         //--------------------------------------------------------------------------------------------------
00222         mRotation = parentOrientation * headRotationToFocusDirection;
00223 
00224         //--------------------------------------------------------------------------------------------------
00225         // now, setting the head feather position and orientation, and updating it...
00226         //--------------------------------------------------------------------------------------------------
00227         mFeather.setParentPositionAndRotationDirectly( mPosition, mRotation );
00228         mFeather.update();
00229 
00230 }//------------------------------------------------------------------
00231 
00232 //------------------------------------------------------------------
00233 
00234 
00235 {
00236         //--------------------------------------------------------------------------------------------------
00237         // let's get the head rotation components...
00238         //--------------------------------------------------------------------------------------------------
00239         LLMatrix3       headMatrix                              = mRotation.getMatrix3();
00240         LLVector3       headForwardDirection    = headMatrix.getLeftRow();
00241         LLVector3       headUpwardDirection             = headMatrix.getUpRow();
00242         LLVector3       headLeftDirection               = headMatrix.getFwdRow();
00243 
00244         //------------------------------------------------------
00245         // show head ball
00246         //------------------------------------------------------
00247         glColor4fv( LLColor4( 1.0f, 1.0f, 1.0f, OPACITY ).mV );
00248         glPushMatrix();
00249         glTranslatef( mPosition.mV[_X_], mPosition.mV[_Y_], mPosition.mV[_Z_] );
00250         glScalef( HEAD_RADIUS, HEAD_RADIUS, HEAD_RADIUS );
00251         gSphere.render();
00252         glPopMatrix();
00253 
00254         //------------------------------------------------------
00255         // show head direction
00256         //------------------------------------------------------
00257         LLVector3 end = mPosition + headForwardDirection * 0.4f;
00258         glBegin( GL_LINES );
00259                 glVertex3fv( mPosition.mV );
00260                 glVertex3fv( end.mV );
00261         glEnd();
00262 
00263 
00264         //------------------------------------------------------
00265         // show eye behavior
00266         //------------------------------------------------------
00267         LLVector3 leftEyePosition       = mPosition + headForwardDirection * HEAD_RADIUS + headUpwardDirection * HEAD_RADIUS + headLeftDirection * HEAD_RADIUS * ONE_HALF;
00268         LLVector3 rightEyePosition      = mPosition + headForwardDirection * HEAD_RADIUS + headUpwardDirection * HEAD_RADIUS - headLeftDirection * HEAD_RADIUS * ONE_HALF;
00269         glColor4fv( LLColor4( 0.0f, 0.0f, 0.0f, 1.0f ).mV );
00270 
00271         glPushMatrix();
00272         glTranslatef( leftEyePosition.mV[_X_], leftEyePosition.mV[_Y_], leftEyePosition.mV[_Z_] );
00273         glScalef( 0.03f, 0.03f, 0.03f );
00274         gSphere.render();
00275         glPopMatrix();
00276 
00277         glPushMatrix();
00278         glTranslatef( rightEyePosition.mV[_X_], rightEyePosition.mV[_Y_], rightEyePosition.mV[_Z_] );
00279         glScalef( 0.03f, 0.03f, 0.03f );
00280         gSphere.render();
00281         glPopMatrix();
00282 
00283         //------------------------------------------------------------------
00284         // render feather
00285         //------------------------------------------------------------------
00286         mFeather.render();
00287 
00288 }//------------------------------------------------------------------
00289 
00290 //---------------------------------------------------------------------------------
00291 //---------------------------------------------------------------------------------
00292 // End of head behavior methods...
00293 //---------------------------------------------------------------------------------
00294 //---------------------------------------------------------------------------------
00295 
00296 
00297 
00298 
00299 //---------------------------------------------------------------------------------
00300 //---------------------------------------------------------------------------------
00301 // Leg behavior methods...
00302 //---------------------------------------------------------------------------------
00303 //---------------------------------------------------------------------------------
00304 LLAnimalControlsLegBehavior::LLAnimalControlsLegBehavior()
00305 {
00306         mBodyPosition.setVec            ( 0.0f, 0.0f, 0.0f );
00307         mHipAnchorPosition.setVec       ( 0.0f, 0.0f, 0.0f ); 
00308         mBodyRotation           = LLQuaternion::DEFAULT;
00309         mWalking                        = false;
00310         mIsLeft                         = true;
00311 
00312 }//-------------------------------------------------------------
00313 
00314 
00315 //------------------------------------------------------------------
00316 void LLAnimalControlsLegBehavior::setBodyPositionAndRotation( LLVector3 p, LLQuaternion r )
00317 {
00318         mBodyPosition = p;
00319         mBodyRotation = r;
00320 
00321 }//------------------------------------------------------------------
00322 
00323 
00324 //---------------------------------------------------------------------------------
00325 void LLAnimalControlsLegBehavior::update()
00326 {
00327 
00328 }//-------------------------------------------------------------
00329 
00330 
00331 //---------------------------------------------------------------------------------
00332 void LLAnimalControlsLegBehavior::render()
00333 {
00334 }//-------------------------------------------------------------
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342 
00343 //---------------------------------------------------------------------------------
00344 void LLHUDAnimalControls::update()
00345 {
00346         if ( ! mParentObject )
00347         {
00348                 return;
00349         }
00350 
00351         //-----------------------------------------------------------------------------------
00352         // To help in development of this client-side functionality, 
00353         // we are pretending that the server is periodically sending us information,
00354         // such as focus position (what we want the animal head to look at)
00355         //-----------------------------------------------------------------------------------
00356         mVirtualServer.setParentPositionAndRotation( mParentObject->getRenderPosition(), mParentObject->getRenderRotation() ); // fake, hacky, temporary, just ignore this.
00357         mVirtualServer.update();
00358 
00359         // here is where we ask the virtual server stuff.
00360         if ( mVirtualServer.getSettingFocusPosition() )
00361         {
00362                 mHeadBehavior.setFocusPosition( mVirtualServer.getFocusPosition() );
00363         }
00364 
00365         updateBodyBehavior();
00366 
00367         mHeadBehavior.setBodyPositionAndRotation        ( mParentObject->getPosition(), mParentObject->getRenderRotation() );
00368         mLeftLegBehavior.setBodyPositionAndRotation     ( mParentObject->getPosition(), mParentObject->getRenderRotation() );
00369         mRightLegBehavior.setBodyPositionAndRotation( mParentObject->getPosition(), mParentObject->getRenderRotation() );
00370 
00371         mHeadBehavior.update();
00372         mLeftLegBehavior.update();
00373         mRightLegBehavior.update();
00374 
00375 }//------------------------------------------------------------------
00376 
00377 
00378 
00379 
00380 
00381 //------------------------------------------------------------------
00382 void LLHUDAnimalControls::updateBodyBehavior()
00383 {
00384 
00385 }//------------------------------------------------------------------
00386 
00387 
00388 
00389 
00390 //------------------------------------------------------------------
00391 void LLHUDAnimalControls::render()
00392 {
00393         mHeadBehavior.render();
00394         mLeftLegBehavior.render();
00395         mRightLegBehavior.render();
00396 
00397 }//------------------------------------------------------------------
00398 
00399 
00400 
00401 
00402 
00403 
00404 
00405 
00406 
00407 //------------------------------------------------------------------
00408 void LLHUDAnimalControls::setAttributes( LLAnimalControlsAttributes a )
00409 {
00410         //mAttributes = a; // ???? // how do I do this?
00411 
00412         //mAttributes.mAnchorPositionOffset     = a.mAnchorPositionOffset;
00413         //mAttributes.mAnchorDirection          = a.mAnchorDirection;
00414         //mAttributes.mAnchorRadius                     = a.mAnchorRadius;      
00415         //mAttributes.mNumSections                      = a.mNumSections;                       
00416         //mAttributes.mLength                                   = a.mLength;                    
00417         //mAttributes.mGravity                          = a.mGravity;                   
00418         //mAttributes.mAirFriction                      = a.mAirFriction;               
00419         //mAttributes.mTension                          = a.mTension;                   
00420         //mAttributes.mRadiusChange                     = a.mRadiusChange;      
00421 
00422 }//------------------------------------------------------------------
00423 
00424 
00425 
00426 //------------------------------------------------------------------
00427 void LLHUDAnimalControls::setParentObject( LLViewerObject * p )
00428 {
00429         printf( "Setting parent of animal controls %x to object %x \n", this, p );
00430         mParentObject = p;
00431 
00432 }//------------------------------------------------------------------
00433 
00434 
00435 //------------------------------------------------------------------
00436 LLViewerObject * LLHUDAnimalControls::getParentObject()
00437 {
00438  return mParentObject;
00439 
00440 }//------------------------------------------------------------------
00441 
00442 
00443 
00444 //------------------------------------------------------------------
00445 void LLHUDAnimalControls::markAsDead()
00446 {
00447         mDead = TRUE;
00448         mParentObject = NULL;
00449 
00450 }//------------------------------------------------------------------
00451 
00452 
00453 
00454 
00455 */
00456 
00457 
00458 

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