void Entity::BillboardXZ(Entity *Target) { Vector3d Projection; // Vector dirección proyectado en el plano XZ. Vector3d AxisX(1,0,0); // Eje de las X. Nos interesa averiguar el ángulo que forma Projection con este eje. Vector3d Normal; // Indica si el ángulo es negativo o positivo. float AngleCosine; // Coseno del ángulo. // Obtenemos el vector dirección, lo proyectamos sobre XZ y lo normalizamos. Target->Position.Subtract(Position, Projection); Projection.y = 0; Projection.Normalize(); // Calculamos el angulo que forman el vector direccion y el eje de las X. AngleCosine = AxisX.DotProduct(Projection); // Evitamos posibles problemas de precisión if (AngleCosine > 1) AngleCosine = 1; else if (AngleCosine < -1) AngleCosine = -1; // Determinamos si el angulo es positivo o negativo. Normal = AxisX.CrossProduct(Projection); if( Normal.x==0 && Normal.y==0 && Normal.z==0 ) Normal.y=1; // Realizamos el giro de las coordenadas cilíndricas if( Normal.y>0 ) SetRotationY(acos(AngleCosine) * 180.0f/3.14f); else SetRotationY(-acos(AngleCosine) * 180.0f/3.14f); }
void keyboard2(unsigned char key, int x, int y) { float rotation[16]; float translation[16]; float camera_disp = -10.0; if ( key == 'w' || key == 'a' || key == 's' || key == 'd' || key == ' ') { SetTranslation(0.0, 0.0, camera_disp, translation); MultiplyMatrix(translation, ViewMatrix, ViewMatrix); } else { return; } switch(key) { case 'w' : SetRotationX(5, rotation); break; case 'a' : SetRotationY(5, rotation); break; case 's' : SetRotationX(-5, rotation); break; case 'd' : SetRotationY(-5, rotation); break; case ' ' : SetTranslation(0.0, 0.0, camera_disp, ViewMatrix); return; default : return; } MultiplyMatrix(ViewMatrix, rotation, ViewMatrix); SetTranslation(0.0, 0.0,camera_disp * -1, translation); MultiplyMatrix(translation, ViewMatrix, ViewMatrix); }
void OnIdle() { float angle = (glutGet(GLUT_ELAPSED_TIME) / 1000.0) * (180.0/M_PI); float RotationMatrixAnim[16]; /* Time dependent rotation */ SetRotationY(angle, RotationMatrixAnim); /* Apply model rotation; finally move cube down */ int i = 0; for(; i < OBJECTS; i++) { MultiplyMatrix(RotationMatrixAnim, InitialTransform, ModelMatrix[i]); MultiplyMatrix(TranslateDown, ModelMatrix[i], ModelMatrix[i]); } // transform karusell transform(0, 0.0, 0.3, 1.0, -2.5, 0.0, 0); transform(1, 0.0, 0.3, 1.0, -2.5, 0.0, 0); transform(2, 0.0, 0.3, 1.0, -2.5, 0.0, 0); // transform cube transform(3, 0.0, 0.3, 0.25, -1.0, 2.0, 1); transform(4, 90.0, 0.3, 0.5 , -1.0, 2.0, 1); transform(5, 180.0, 0.3, 0.25, -1.0, 2.0, 1); transform(6, 270.0, 0.3, 0.5 , -1.0, 2.0, 1); /* Request redrawing forof window content */ glutPostRedisplay(); }
void transform(int i, float rot_offset, float rot_speed, float size, float height, float radius, int wobble) { float angle = (glutGet(GLUT_ELAPSED_TIME) / 1000.0) * (180/M_PI); float RotationMatrixAnim[16]; float ScaleMatrix[16]; float InitialTransform[16]; float scale = 0.001; float maxHeight = height + 0.5; float minHeight = height - 0.5; static int up[OBJECTS]; static int time[OBJECTS]; if(wobble == 1) { if(up[i] == 0) height = maxHeight - (time[i]++) * scale; else height = minHeight + (time[i]++) * scale; if(time[i] >= 1000) { up[i] = (up[i] == 0) ? 1 : 0; time[i] = 0; } } SetScaling(size, size, size, ScaleMatrix); SetRotationY(rot_speed*angle + rot_offset, RotationMatrixAnim); SetTranslation(radius, height, 0, InitialTransform); MultiplyMatrix(InitialTransform, ScaleMatrix, ModelMatrix[i]); MultiplyMatrix(RotationMatrixAnim, ModelMatrix[i], ModelMatrix[i]); }
void Actor::ScaleTo( const RectI &rect, StretchType st ) { // width and height of rectangle float rect_width = (float) rect.GetWidth(); float rect_height = (float) rect.GetHeight(); if( rect_width < 0 ) SetRotationY( 180 ); if( rect_height < 0 ) SetRotationX( 180 ); // center of the rectangle float rect_cx = rect.left + rect_width/2; float rect_cy = rect.top + rect_height/2; // zoom fActor needed to scale the Actor to fill the rectangle float fNewZoomX = fabsf(rect_width / m_size.x); float fNewZoomY = fabsf(rect_height / m_size.y); float fNewZoom = 0.f; switch( st ) { case cover: fNewZoom = fNewZoomX>fNewZoomY ? fNewZoomX : fNewZoomY; // use larger zoom break; case fit_inside: fNewZoom = fNewZoomX>fNewZoomY ? fNewZoomY : fNewZoomX; // use smaller zoom break; } SetXY( rect_cx, rect_cy ); SetZoom( fNewZoom ); }
void Rotateable::SetRotation(float x,float y,float z) { SetRotationX(x); SetRotationY(y); SetRotationZ(z); }
void mouse(int button, int state, int x, int y){ float rotation[16]; float translation[16]; float camera_disp = -10.0; if(button != 0) {return;} /* mouse click*/ if (state == 0) { mouse_x = x; mouse_y = y; return; /* mouse release*/ } else if ( state == 1) { SetTranslation(0.0, 0.0, camera_disp, translation); MultiplyMatrix(translation, ViewMatrix, ViewMatrix); int diffX = mouse_x - x; int diffY = mouse_y - y; /* right to left or reverse*/ if (fabs(diffX) > fabs(diffY)) { if(diffX < 0) { SetRotationY(-5, rotation); } else { SetRotationY(5, rotation); } } else { if (diffY < 0) { SetRotationX(-5, rotation); } else { SetRotationX(5, rotation); } } } MultiplyMatrix(ViewMatrix, rotation, ViewMatrix); SetTranslation(0.0, 0.0,camera_disp * -1, translation); MultiplyMatrix(translation, ViewMatrix, ViewMatrix); }
std::unique_ptr<CExchangePost> CExchangePost::Create( const ObjectCreateParams& params, Gfx::COldModelManager* modelManager, Gfx::CEngine* engine) { auto obj = MakeUnique<CExchangePost>(params.id); obj->SetTeam(params.team); int rank = engine->CreateObject(); engine->SetObjectType(rank, Gfx::ENG_OBJTYPE_FIX); // it is a stationary object obj->SetObjectRank(0, rank); modelManager->AddModelReference("info1.mod", false, rank); obj->SetPosition(params.pos); obj->SetRotationY(params.angle); obj->SetFloorHeight(0.0f); rank = engine->CreateObject(); engine->SetObjectType(rank, Gfx::ENG_OBJTYPE_DESCENDANT); obj->SetObjectRank(1, rank); obj->SetObjectParent(1, 0); modelManager->AddModelReference("info2.mod", false, rank); obj->SetPartPosition(1, Math::Vector(0.0f, 5.0f, 0.0f)); for (int i = 0; i < 3; ++i) { rank = engine->CreateObject(); engine->SetObjectType(rank, Gfx::ENG_OBJTYPE_DESCENDANT); obj->SetObjectRank(2+i*2, rank); obj->SetObjectParent(2+i*2, 1); modelManager->AddModelReference("info3.mod", false, rank); obj->SetPartPosition(2+i*2, Math::Vector(0.0f, 4.5f, 0.0f)); rank = engine->CreateObject(); engine->SetObjectType(rank, Gfx::ENG_OBJTYPE_DESCENDANT); obj->SetObjectRank(3+i*2, rank); obj->SetObjectParent(3+i*2, 2+i*2); modelManager->AddModelReference("radar4.mod", false, rank); obj->SetPartPosition(3+i*2, Math::Vector(0.0f, 0.0f, -4.0f)); obj->SetPartRotationY(2+i*2, 2.0f*Math::PI/3.0f*i); } obj->AddCrashSphere(CrashSphere(Math::Vector(0.0f, 3.0f, 0.0f), 6.0f, SOUND_BOUMm, 0.45f)); obj->AddCrashSphere(CrashSphere(Math::Vector(0.0f, 11.0f, 0.0f), 6.0f, SOUND_BOUMm, 0.45f)); obj->SetCameraCollisionSphere(Math::Sphere(Math::Vector(0.0f, 5.0f, 0.0f), 6.0f)); obj->CreateShadowCircle(8.0f, 1.0f); Math::Vector pos = obj->GetPosition(); pos.y += params.height; obj->SetPosition(pos); // to display the shadows immediately auto objAuto = MakeUnique<CAutoInfo>(obj.get()); objAuto->Init(); obj->SetAuto(std::move(objAuto)); engine->LoadAllTextures(); return obj; }
void OnIdle(){ /* Determine delta time between two frames to ensure constant animation */ int newTime = glutGet(GLUT_ELAPSED_TIME); int delta = newTime - oldTime; oldTime = newTime; SetIdentityMatrix(IdentityMatrixAnim); SetIdentityMatrix(TranslationMatrixAnim); /* automatic camera mode: press 'm' to start, and 'n' to reset */ if(anim_cam){ if(camera_disp > -50){ // zoom out of world (until camera position on z == -50) and rotate on x axis camera_disp -= 0.1; } if(camMov2 < 500){ angle1 += 0.1; } else if(camMov2 < 800){ angle1 -= 0.1; } else{ camMov2 = 200; } ++camMov2; if(camMov1 < 10 && camMov1 > -1){ // move camera to left and right with some pause of movement // Pause movement } else if(camMov1 < 80){ angle -= 0.1; } else if(camMov1 < 100){ // Pause movement } else if(camMov1 < 240){ angle += 0.1; } else if(camMov1 < 250){ // Pause movement } else if(camMov1 < 320){ angle -= 0.1; } else{ // Pause for some time if(camMov1 > 1000){ camMov1 = 0; } } ++camMov1; SetIdentityMatrix(RotationMatrixCam); SetIdentityMatrix(RotationMatrixCam2); SetTranslation(0.0, 0.0, camera_disp, TranslationMatrixAnim); // translate cameras z position SetRotationY(angle, RotationMatrixCam); // set rotation of camera SetRotationX(angle1, RotationMatrixCam2); MultiplyMatrix(RotationMatrixCam, TranslationMatrixAnim, RotationMatrixCam3); MultiplyMatrix(RotationMatrixCam3, RotationMatrixCam2, ViewMatrix); } /* If animation is set to true, set new rotation angles */ if(anim){ /* Increment rotation angles and update matrix */ if(axis == YaxisStop){ // angleY = 0; SetRotationY(angleY, RotationMatrixAnimY); } else if(axis == Yaxis){ angleY = fmod(angleY + delta/20.0, 360.0); SetRotationY(angleY, RotationMatrixAnimY); } } else { /* else: do not apply rotation below */ return; } // Set Translation for Center-Imported Objects (ball_01, ball_02 and ring) // NEW SetIdentityMatrix(IdentityMatrixAnim); SetIdentityMatrix(TranslationMatrixAnim); SetIdentityMatrix(TranslationMatrixAnim2); SetIdentityMatrix(TranslationMatrixAnim3); SetIdentityMatrix(TranslationMatrixAnim4); SetIdentityMatrix(TranslationMatrixAnim5); SetTranslation(-1.8, 1, -1.8, TranslationMatrixAnim); // ball_01 -1,8/1,8/1 SetTranslation(1.8, 1, 1.8, TranslationMatrixAnim3); // ball_02 1,8/-1,8/1 SetTranslation(-0.96, -1.73403, -0.66, TranslationMatrixAnim5); // elliptic_ring -0,96/0,66/-1,73403 /* Rotation of models */ int k; for(k=1; k<13; ++k){ // do not rotate top ring (index 0) and fixed background structures (index 13 and 14) // Set main rotaiton /* Update of transformation matrices * Note order of transformations and rotation of reference axes */ SetIdentityMatrix(IdentityMatrixAnim); MultiplyMatrix(IdentityMatrixAnim, RotationMatrixAnimY, RotationMatrixAnim); // rotate top bar in different direction and double the rotation speed if(k==1 || k==6 || k == 7){ angleY2 = -fmod(angleY + delta/20.0, 360.0); SetRotationY(angleY2, RotationMatrixAnimY2); // in daddition: rotate models ball_01 and ball_02 around their own center if(k==6){ MultiplyMatrix(RotationMatrixAnimY2, RotationMatrixAnimY2, RotationMatrixAnimY3); MultiplyMatrix(RotationMatrixAnimY3, TranslationMatrixAnim, TranslationMatrixAnim2); MultiplyMatrix(TranslationMatrixAnim2, RotationMatrixAnimY2, ModelMatrix[k]); continue; } else if(k==7){ MultiplyMatrix(RotationMatrixAnimY2, RotationMatrixAnimY2, RotationMatrixAnimY3); MultiplyMatrix(RotationMatrixAnimY3, TranslationMatrixAnim3, TranslationMatrixAnim2); MultiplyMatrix(TranslationMatrixAnim2, RotationMatrixAnimY2, ModelMatrix[k]); continue; } MultiplyMatrix(RotationMatrixAnimY2, IdentityMatrixAnim, ModelMatrix[k]); MultiplyMatrix(RotationMatrixAnimY2,RotationMatrixAnimY2,ModelMatrix[k]); } else { // additional rotation of ring with double speed oround its center if(k==11){ MultiplyMatrix(RotationMatrixAnimY, TranslationMatrixAnim5, RotationMatrixAnimY3); MultiplyMatrix(RotationMatrixAnimY3, RotationMatrixAnimY, TranslationMatrixAnim4); MultiplyMatrix(TranslationMatrixAnim4, RotationMatrixAnimY, ModelMatrix[k]); continue; } MultiplyMatrix(RotationMatrixAnim, IdentityMatrixAnim, ModelMatrix[k]); } } /* Issue display refresh */ glutPostRedisplay(); }
void Keyboard(unsigned char key, int x, int y){ switch( key ) { // NEW: Manual camera mode: allways on -> use WASDRC to translate cam position and IJKL for cam rotation case 'w' : camera_disp += 0.1f; break; case 's' : camera_disp -= 0.1f; break; case 'd' : xx -= 0.1; break; case 'a' : xx += 0.1; break; case 'i' : angle2 -= 0.3; break; case 'k' : angle2 += 0.3; break; case 'l' : angle3 += 0.3; break; case 'j' : angle3 -= 0.3; break; case 'r' : yy -= 0.1f; break; case 'c' : yy += 0.1f; break; case 'm' : // set automatic camera mode and reset camera position an rotation (angle) anim_cam = GL_TRUE; camera_disp = -10.0; angle=0; angle1=0; angle2=0; angle3=0; xx=0; yy=0; SetTranslation(xx, yy, camera_disp, ViewMatrix); // camera_disp == z coordinate of camera return; break; case 'n' : // unset automatic camera mode and reset camera position an rotation (angle) anim_cam = GL_FALSE; camera_disp = -10.0; angle=0; angle1=0; angle2=0; angle3=0; xx=0; yy=0; SetTranslation(xx, yy, camera_disp, ViewMatrix); // camera_disp == z coordinate of camera return; break; } SetIdentityMatrix(RotationMatrixCam); SetIdentityMatrix(RotationMatrixCam2); SetIdentityMatrix(RotationMatrixCam3); SetIdentityMatrix(TranslationMatrixCam); // set camera movement SetTranslation(xx, yy, camera_disp, TranslationMatrixCam); SetRotationX(angle2, RotationMatrixCam); SetRotationY(angle3, RotationMatrixCam2); // apply movement to viewMatrix (camera matrix) MultiplyMatrix(RotationMatrixCam, RotationMatrixCam2, RotationMatrixCam3); MultiplyMatrix(RotationMatrixCam3, TranslationMatrixCam, ViewMatrix); glutPostRedisplay(); }
void Rotateable::Yaw(float val) { SetRotationY(rotation.y + val); }
void Actor::HandleCommand( const ParsedCommand &command ) { HandleParams; const CString& sName = sParam(0); // Commands that go in the tweening queue: if ( sName=="sleep" ) Sleep( fParam(1) ); else if( sName=="linear" ) BeginTweening( fParam(1), TWEEN_LINEAR ); else if( sName=="accelerate" ) BeginTweening( fParam(1), TWEEN_ACCELERATE ); else if( sName=="decelerate" ) BeginTweening( fParam(1), TWEEN_DECELERATE ); else if( sName=="bouncebegin" ) BeginTweening( fParam(1), TWEEN_BOUNCE_BEGIN ); else if( sName=="bounceend" ) BeginTweening( fParam(1), TWEEN_BOUNCE_END ); else if( sName=="spring" ) BeginTweening( fParam(1), TWEEN_SPRING ); else if( sName=="stoptweening" ) { StopTweening(); BeginTweening( 0.0001f, TWEEN_LINEAR ); } // Why BeginT again? -Chris else if( sName=="finishtweening" ) FinishTweening(); else if( sName=="hurrytweening" ) HurryTweening( fParam(1) ); else if( sName=="x" ) SetX( fParam(1) ); else if( sName=="y" ) SetY( fParam(1) ); else if( sName=="z" ) SetZ( fParam(1) ); else if( sName=="addx" ) SetX( GetDestX()+fParam(1) ); else if( sName=="addy" ) SetY( GetDestY()+fParam(1) ); else if( sName=="addz" ) SetZ( GetDestZ()+fParam(1) ); else if( sName=="zoom" ) SetZoom( fParam(1) ); else if( sName=="zoomx" ) SetZoomX( fParam(1) ); else if( sName=="zoomy" ) SetZoomY( fParam(1) ); else if( sName=="zoomz" ) SetZoomZ( fParam(1) ); else if( sName=="zoomtowidth" ) ZoomToWidth( fParam(1) ); else if( sName=="zoomtoheight" ) ZoomToHeight( fParam(1) ); else if( sName=="stretchto" ) StretchTo( RectF( fParam(1), fParam(2), fParam(3), fParam(4) ) ); else if( sName=="cropleft" ) SetCropLeft( fParam(1) ); else if( sName=="croptop" ) SetCropTop( fParam(1) ); else if( sName=="cropright" ) SetCropRight( fParam(1) ); else if( sName=="cropbottom" ) SetCropBottom( fParam(1) ); else if( sName=="fadeleft" ) SetFadeLeft( fParam(1) ); else if( sName=="fadetop" ) SetFadeTop( fParam(1) ); else if( sName=="faderight" ) SetFadeRight( fParam(1) ); else if( sName=="fadebottom" ) SetFadeBottom( fParam(1) ); else if( sName=="fadecolor" ) SetFadeDiffuseColor( cParam(1) ); else if( sName=="diffuse" ) SetDiffuse( cParam(1) ); else if( sName=="diffuseleftedge" ) SetDiffuseLeftEdge( cParam(1) ); else if( sName=="diffuserightedge" ) SetDiffuseRightEdge( cParam(1) ); else if( sName=="diffusetopedge" ) SetDiffuseTopEdge( cParam(1) ); else if( sName=="diffusebottomedge" ) SetDiffuseBottomEdge( cParam(1) ); /* Add left/right/top/bottom for alpha if needed. */ else if( sName=="diffusealpha" ) SetDiffuseAlpha( fParam(1) ); else if( sName=="diffusecolor" ) SetDiffuseColor( cParam(1) ); else if( sName=="glow" ) SetGlow( cParam(1) ); else if( sName=="glowmode" ) { if(!sParam(1).CompareNoCase("whiten")) SetGlowMode( GLOW_WHITEN ); else if(!sParam(1).CompareNoCase("brighten")) SetGlowMode( GLOW_BRIGHTEN ); else ASSERT(0); } else if( sName=="rotationx" ) SetRotationX( fParam(1) ); else if( sName=="rotationy" ) SetRotationY( fParam(1) ); else if( sName=="rotationz" ) SetRotationZ( fParam(1) ); else if( sName=="heading" ) AddRotationH( fParam(1) ); else if( sName=="pitch" ) AddRotationP( fParam(1) ); else if( sName=="roll" ) AddRotationR( fParam(1) ); else if( sName=="shadowlength" ) SetShadowLength( fParam(1) ); else if( sName=="horizalign" ) SetHorizAlign( sParam(1) ); else if( sName=="vertalign" ) SetVertAlign( sParam(1) ); else if( sName=="diffuseblink" ) SetEffectDiffuseBlink(); else if( sName=="diffuseshift" ) SetEffectDiffuseShift(); else if( sName=="glowblink" ) SetEffectGlowBlink(); else if( sName=="glowshift" ) SetEffectGlowShift(); else if( sName=="rainbow" ) SetEffectRainbow(); else if( sName=="wag" ) SetEffectWag(); else if( sName=="bounce" ) SetEffectBounce(); else if( sName=="bob" ) SetEffectBob(); else if( sName=="pulse" ) SetEffectPulse(); else if( sName=="spin" ) SetEffectSpin(); else if( sName=="vibrate" ) SetEffectVibrate(); else if( sName=="stopeffect" ) SetEffectNone(); else if( sName=="effectcolor1" ) SetEffectColor1( cParam(1) ); else if( sName=="effectcolor2" ) SetEffectColor2( cParam(1) ); else if( sName=="effectperiod" ) SetEffectPeriod( fParam(1) ); else if( sName=="effectoffset" ) SetEffectOffset( fParam(1) ); else if( sName=="effectdelay" ) SetEffectDelay( fParam(1) ); else if( sName=="effectclock" ) SetEffectClock( sParam(1) ); else if( sName=="effectmagnitude" ) SetEffectMagnitude( RageVector3(fParam(1),fParam(2),fParam(3)) ); else if( sName=="scaletocover" ) { RectI R(iParam(1), iParam(2), iParam(3), iParam(4)); ScaleToCover(R); } else if( sName=="scaletofit" ) { RectI R(iParam(1), iParam(2), iParam(3), iParam(4)); ScaleToFitInside(R); } // Commands that take effect immediately (ignoring the tweening queue): else if( sName=="animate" ) EnableAnimation( bParam(1) ); else if( sName=="setstate" ) SetState( iParam(1) ); else if( sName=="texturewrapping" ) SetTextureWrapping( bParam(1) ); else if( sName=="additiveblend" ) SetBlendMode( bParam(1) ? BLEND_ADD : BLEND_NORMAL ); else if( sName=="blend" ) SetBlendMode( sParam(1) ); else if( sName=="zbuffer" ) SetUseZBuffer( bParam(1) ); else if( sName=="ztest" ) SetZTestMode( bParam(1)?ZTEST_WRITE_ON_PASS:ZTEST_OFF ); else if( sName=="ztestmode" ) SetZTestMode( sParam(1) ); else if( sName=="zwrite" ) SetZWrite( bParam(1) ); else if( sName=="clearzbuffer" ) SetClearZBuffer( bParam(1) ); else if( sName=="backfacecull" ) SetCullMode( bParam(1) ? CULL_BACK : CULL_NONE ); else if( sName=="cullmode" ) SetCullMode( sParam(1) ); else if( sName=="hidden" ) SetHidden( bParam(1) ); else if( sName=="hibernate" ) SetHibernate( fParam(1) ); else if( sName=="draworder" ) SetDrawOrder( iParam(1) ); else if( sName=="playcommand" ) PlayCommand( sParam(1) ); else if( sName=="queuecommand" ) { ParsedCommand newcommand = command; newcommand.vTokens.erase( newcommand.vTokens.begin() ); QueueCommand( newcommand ); return; // don't do parameter number checking } /* These are commands intended for a Sprite commands, but they will get * sent to all sub-actors (which aren't necessarily Sprites) on * GainFocus and LoseFocus. So, don't run CheckHandledParams * on these commands. */ else if( sName=="customtexturerect" || sName=="texcoordvelocity" || sName=="scaletoclipped" || sName=="stretchtexcoords" || sName=="position" || sName=="loop" || sName=="play" || sName=="pause" || sName=="rate" ) return; else { CString sError = ssprintf( "Actor::HandleCommand: Unrecognized command name '%s'.", sName.c_str() ); LOG->Warn( sError ); Dialog::OK( sError ); } CheckHandledParams; }