////////////////////////////////////////////////////////////////////////// // update //virtual void ScnParticleSystemComponent::update( BcReal Tick ) { ///* // TEST CODE. for( BcU32 Idx = 0; Idx < 4; ++Idx ) { ScnParticle* pParticle = NULL; if( allocParticle( pParticle ) ) { pParticle->Position_ = BcVec3d( 0.0f, 0.0f, 0.0f ); pParticle->Velocity_ = BcVec3d( BcRandom::Global.randReal() - 0.5f, BcRandom::Global.randReal() - 0.5f, BcRandom::Global.randReal() - 0.5f ).normal() * 16.0f; pParticle->Acceleration_ = -pParticle->Velocity_ * 0.5f; pParticle->RotationMultiplier_ = ( BcRandom::Global.randReal() - 0.5f ) * 4.0f; pParticle->MinScale_ = BcVec2d( 0.5f, 0.5f ); pParticle->MaxScale_ = BcVec2d( 4.0f, 4.0f ); pParticle->MinColour_ = RsColour( 1.0f, 0.0f, 1.0f, 0.1f ); pParticle->MaxColour_ = RsColour( 0.0f, 1.0f, 1.0f, 0.0f ); pParticle->UVBounds_ = BcVec4d( 0.0f, 0.0f, 1.0f, 1.0f ); pParticle->CurrentTime_ = 0.0f; pParticle->MaxTime_ = 4.0f; pParticle->Alive_ = BcTrue; } } //*/ // Allocate particles. updateParticles( Tick ); }
////////////////////////////////////////////////////////////////////////// // GaBallComponent //virtual void GaBallComponent::update( BcReal Tick ) { // Setup rotation. BcQuat Rotation; Rotation.fromEular( Rotation_, 0.0f, 0.0f ); Rotation_ += Tick; //getParentEntity()->setRotation( Rotation ); // Move ball. const BcVec3d& Position = getParentEntity()->getMatrix().translation(); BcVec3d NewPosition = Position + Velocity_ * Tick; if( NewPosition.x() < -18.0f || NewPosition.x() > 18.0f ) { NewPosition -= BcVec3d( Velocity_.x(), 0.0f, 0.0f ) * Tick; Velocity_.x( -Velocity_.x() ); } if( NewPosition.z() < -10.0f || NewPosition.z() > 10.0f ) { NewPosition -= BcVec3d( 0.0f, 0.0f, Velocity_.z() ) * Tick; Velocity_.z( -Velocity_.z() ); } getParentEntity()->setPosition( NewPosition ); }
////////////////////////////////////////////////////////////////////////// // initialise void GaBallComponent::initialise( const Json::Value& Object ) { Super::initialise( Object ); Rotation_ = 0.0f; Velocity_ = BcVec3d( 8.0f, 0.0f, 8.0f ); }
////////////////////////////////////////////////////////////////////////// // open //virtual void ScnCore::open() { // Create spacial tree. pSpacialTree_ = new ScnSpatialTree(); // Create root node. BcVec3d HalfBounds( BcVec3d( 16.0f, 16.0f, 16.0f ) * 1024.0f ); pSpacialTree_->createRoot( BcAABB( -HalfBounds, HalfBounds ) ); }
////////////////////////////////////////////////////////////////////////// // bakeTransform void MdlMesh::bakeTransform( const BcMat4d& Transform ) { BcMat4d NrmTransform = Transform; NrmTransform.translation( BcVec3d( 0.0f, 0.0f, 0.0f ) ); for( BcU32 i = 0; i < aVertices_.size(); ++i ) { aVertices_[ i ].Position_ = aVertices_[ i ].Position_ * Transform; aVertices_[ i ].Normal_ = aVertices_[ i ].Normal_ * NrmTransform; aVertices_[ i ].Normal_.normalise(); aVertices_[ i ].Tangent_ = aVertices_[ i ].Tangent_ * NrmTransform; aVertices_[ i ].Tangent_.normalise(); } }
//////////////////////////////////////////////////////////////////////////////// // render //virtual void GaMainGameState::render() { // NEILO HACK: REMOVE. OsClient* pClient = OsCore::pImpl()->getClient( 0 ); RsViewport Viewport( 0, 0, pClient->getWidth(), pClient->getHeight() ); // Render background. Projection_.perspProjectionHorizontal( BcPIDIV4, (BcReal)pClient->getWidth() / (BcReal)pClient->getHeight(), 1.0f, 1024.0f ); WorldView_.lookAt( BcVec3d( 0.0f, 350.0f, 270.0f ), BcVec3d( 0.0f, 0.0f, 0.0f ), BcVec3d( 0.0f, 0.0f, 1.0f ) ); if( SsCore::pImpl() != NULL ) { SsCore::pImpl()->setListener( BcVec3d( 0.0f, 350.0f, 270.0f ), BcVec3d( 0.0f, -4.0f, -2.0f ).normal(), BcVec3d( 0.0f, 0.0f, 1.0f ) ); } setMaterialComponentParams( BackgroundMaterialComponent_, BcMat4d() ); Canvas_->setMaterialComponent( BackgroundMaterialComponent_ ); Canvas_->drawSpriteCentered3D( BcVec3d( 0.0f, 0.0f, 0.0f ), WorldHalfSize_ * 4.0f, 0, RsColour::WHITE, 0 ); // Render entities. for( BcU32 Idx = 0; Idx < Entities_.size(); ++Idx ) { GaGameComponent* pEntity = Entities_[ Idx ]; pEntity->render( Canvas_ ); } // Draw foreground. BcMat4d Ortho; Ortho.orthoProjection( -WorldHalfSize_.x(), WorldHalfSize_.x(), -WorldHalfSize_.y(), WorldHalfSize_.y(), -1.0f, 0.0f ); Canvas_->pushMatrix( Ortho ); Canvas_->setMaterialComponent( ForegroundMaterialComponent_ ); Canvas_->drawSpriteCentered( BcVec2d( 0.0f, 0.0f ), BcVec2d( WorldHalfSize_.x() * 2.2f, WorldHalfSize_.y() * -2.0f ), 0, RsColour::WHITE, 20 ); BcReal Width = BcMax( 0.0f, FoodHealth_ - 0.5f ) * 2.0f; RsColour Colour; Colour.lerp( RsColour::RED, RsColour::GREEN, Width ); Canvas_->setMaterialComponent( BarMaterialComponent_ ); Canvas_->drawSpriteCentered( BcVec2d( 0.0f, WorldHalfSize_.y() - ( WorldHalfSize_.y() * 0.05f ) ), BcVec2d( WorldHalfSize_.x() * 1.5f * Width, WorldHalfSize_.y() * 0.05f ), 0, Colour, 20 ); Canvas_->popMatrix(); // Base render. GaBaseGameState::render(); }
//////////////////////////////////////////////////////////////////////////////// // update //virtual void GaGameComponent::update( BcReal Tick ) { BcScopedLock< BcMutex > Lock( EmoteLock_ ); TEmoteList::iterator It( EmoteList_.begin() ); while( It != EmoteList_.end() ) { TEmote& Emote = (*It); Emote.Timeleft_ -= Tick; Emote.Position_ += BcVec3d( 0.0f, 0.0f, 4.0f ) * Tick; if( Emote.Timeleft_ < 0.0f ) { It = EmoteList_.erase( It ); } else { ++It; } } EmoteTimer_ -= Tick; }
BcReal BcMat4d::determinant() { const BcMat4d& Lhs = (*this); const BcMat3d A = BcMat3d( BcVec3d( Lhs[1][1], Lhs[1][2], Lhs[1][3] ), BcVec3d( Lhs[2][1], Lhs[2][2], Lhs[2][3] ), BcVec3d( Lhs[3][1], Lhs[3][2], Lhs[3][3] ) ); const BcMat3d B = BcMat3d( BcVec3d( Lhs[1][0], Lhs[1][2], Lhs[1][3] ), BcVec3d( Lhs[2][0], Lhs[2][2], Lhs[2][3] ), BcVec3d( Lhs[3][0], Lhs[3][2], Lhs[3][3] ) ); const BcMat3d C = BcMat3d( BcVec3d( Lhs[1][0], Lhs[1][1], Lhs[1][3] ), BcVec3d( Lhs[2][0], Lhs[2][1], Lhs[2][3] ), BcVec3d( Lhs[3][0], Lhs[3][1], Lhs[3][3] ) ); const BcMat3d D = BcMat3d( BcVec3d( Lhs[1][0], Lhs[1][1], Lhs[1][2] ), BcVec3d( Lhs[2][0], Lhs[2][1], Lhs[2][2] ), BcVec3d( Lhs[3][0], Lhs[3][1], Lhs[3][2] ) ); return ( ( A.determinant() * Lhs[0][0] ) - ( B.determinant() * Lhs[0][1] ) + ( C.determinant() * Lhs[0][2] ) - ( D.determinant() * Lhs[0][3] ) ); }
//virtual void ScnParticleSystemComponent::render( RsFrame* pFrame, RsRenderSort Sort ) { // Grab vertex buffer and flip for next frame to use. TVertexBuffer& VertexBuffer = VertexBuffers_[ CurrentVertexBuffer_ ]; //CurrentVertexBuffer_ = 1 - CurrentVertexBuffer_; // Lock vertex buffer. VertexBuffer.pVertexBuffer_->lock(); // Iterate over alive particles, and setup vertex buffer. BcU32 NoofParticlesToRender = 0; ScnParticleVertex* pVertex = VertexBuffer.pVertexArray_; for( BcU32 Idx = 0; Idx < NoofParticles_; ++Idx ) { ScnParticle& Particle = pParticleBuffer_[ Idx ]; if( Particle.Alive_ ) { // Half size. const BcVec2d HalfSize = Particle.Scale_ * 0.5f; // Crappy rotation implementation :P const BcReal Radians = Particle.CurrentTime_ * Particle.RotationMultiplier_; BcVec2d CornerA = BcVec2d( -1.0f, -1.0f ) * HalfSize; BcVec2d CornerB = BcVec2d( 1.0f, -1.0f ) * HalfSize; BcVec2d CornerC = BcVec2d( 1.0f, 1.0f ) * HalfSize; BcVec2d CornerD = BcVec2d( -1.0f, 1.0f ) * HalfSize; if( Radians != NULL ) { BcMat4d Rotation; Rotation.rotation( BcVec3d( 0.0f, 0.0f, Radians ) ); CornerA = CornerA * Rotation; CornerB = CornerB * Rotation; CornerC = CornerC * Rotation; CornerD = CornerD * Rotation; } const BcU32 Colour = Particle.Colour_.asABGR(); // Grab vertices. ScnParticleVertex& VertexA = *pVertex++; ScnParticleVertex& VertexB = *pVertex++; ScnParticleVertex& VertexC = *pVertex++; ScnParticleVertex& VertexD = *pVertex++; ScnParticleVertex& VertexE = *pVertex++; ScnParticleVertex& VertexF = *pVertex++; // VertexA.X_ = Particle.Position_.x(); VertexA.Y_ = Particle.Position_.y(); VertexA.Z_ = Particle.Position_.z(); VertexA.NX_ = CornerA.x(); VertexA.NY_ = CornerA.y(); VertexA.NZ_ = 0.0f; VertexA.U_ = Particle.UVBounds_.x(); VertexA.V_ = Particle.UVBounds_.y(); VertexA.RGBA_ = Colour; // VertexB.X_ = Particle.Position_.x(); VertexB.Y_ = Particle.Position_.y(); VertexB.Z_ = Particle.Position_.z(); VertexB.NX_ = CornerB.x(); VertexB.NY_ = CornerB.y(); VertexB.NZ_ = 0.0f; VertexB.U_ = Particle.UVBounds_.z(); VertexB.V_ = Particle.UVBounds_.y(); VertexB.RGBA_ = Colour; // VertexC.X_ = Particle.Position_.x(); VertexC.Y_ = Particle.Position_.y(); VertexC.Z_ = Particle.Position_.z(); VertexC.NX_ = CornerC.x(); VertexC.NY_ = CornerC.y(); VertexC.NZ_ = 0.0f; VertexC.U_ = Particle.UVBounds_.z(); VertexC.V_ = Particle.UVBounds_.w(); VertexC.RGBA_ = Colour; // VertexD.X_ = Particle.Position_.x(); VertexD.Y_ = Particle.Position_.y(); VertexD.Z_ = Particle.Position_.z(); VertexD.NX_ = CornerC.x(); VertexD.NY_ = CornerC.y(); VertexD.NZ_ = 0.0f; VertexD.U_ = Particle.UVBounds_.z(); VertexD.V_ = Particle.UVBounds_.w(); VertexD.RGBA_ = Colour; // VertexE.X_ = Particle.Position_.x(); VertexE.Y_ = Particle.Position_.y(); VertexE.Z_ = Particle.Position_.z(); VertexE.NX_ = CornerD.x(); VertexE.NY_ = CornerD.y(); VertexE.NZ_ = 0.0f; VertexE.U_ = Particle.UVBounds_.x(); VertexE.V_ = Particle.UVBounds_.w(); VertexE.RGBA_ = Colour; // VertexF.X_ = Particle.Position_.x(); VertexF.Y_ = Particle.Position_.y(); VertexF.Z_ = Particle.Position_.z(); VertexF.NX_ = CornerA.x(); VertexF.NY_ = CornerA.y(); VertexF.NZ_ = 0.0f; VertexF.U_ = Particle.UVBounds_.x(); VertexF.V_ = Particle.UVBounds_.y(); VertexF.RGBA_ = Colour; // ++NoofParticlesToRender; } } // Update and unlock vertex buffer. VertexBuffer.pVertexBuffer_->setNoofUpdateVertices( NoofParticlesToRender * 6 ); VertexBuffer.pVertexBuffer_->unlock(); // Bind material. MaterialComponent_->setParameter( WorldTransformParam_, BcMat4d() ); MaterialComponent_->bind( pFrame, Sort ); // Setup render node. ScnParticleSystemComponentRenderNode* pRenderNode = pFrame->newObject< ScnParticleSystemComponentRenderNode >(); pRenderNode->pPrimitive_ = VertexBuffer.pPrimitive_; pRenderNode->NoofIndices_ = NoofParticlesToRender * 6; // Add to frame. pRenderNode->Sort_ = Sort; pFrame->addRenderNode( pRenderNode ); }
////////////////////////////////////////////////////////////////////////// // create //virtual void ScnSoundEmitter::create() { Position_ = BcVec3d( 0.0f, 0.0f, 0.0f ); Gain_ = 1.0f; Pitch_ = 1.0f; }