Ejemplo n.º 1
0
void WalkTestLights::AddLight (const char* arg)
{
  csVector3 dir (0,0,0);
  csVector3 pos = walktest->views->GetCamera ()->GetTransform ().This2Other (dir);
  csRef<iLight> dyn;

  bool rnd;
  float r, g, b, radius;
  if (arg && csScanStr (arg, "%f,%f,%f,%f", &r, &g, &b, &radius) == 4)
  {
    dyn = walktest->Engine->CreateLight ("", pos,
      radius, csColor (r, g, b), CS_LIGHT_DYNAMICTYPE_DYNAMIC);
    rnd = false;
  }
  else
  {
    dyn = walktest->Engine->CreateLight ("", pos,
      6, csColor (1, 1, 1), CS_LIGHT_DYNAMICTYPE_DYNAMIC);
    rnd = true;
  }
  iLightList* ll = walktest->views->GetCamera ()->GetSector ()->GetLights ();
  ll->Add (dyn);
  dynamic_lights.Push (dyn);
  if (rnd)
    AttachRandomLight (dyn);
  walktest->Report (CS_REPORTER_SEVERITY_NOTIFY, "Dynamic light added.");
}
Ejemplo n.º 2
0
 DynamicsDebugger::DynamicsDebugger (DebuggerManager* manager)
   : scfImplementationType (this), manager (manager), debugMode (false)
 {
   // Init debug materials
   materials[0] = CS::Material::MaterialBuilder::CreateColorMaterial
     (manager->object_reg, "dyndebug_static", csColor (0, 0, 1));
   materials[1] = CS::Material::MaterialBuilder::CreateColorMaterial
     (manager->object_reg, "dyndebug_dynamic", csColor (0, 1, 0));
   materials[2] = CS::Material::MaterialBuilder::CreateColorMaterial
     (manager->object_reg, "dyndebug_kinematic", csColor (0, 0, 1));
 }
Ejemplo n.º 3
0
void WalkTestLights::EnableFlashlight (bool enable)
{
  if (enable && !flashlight)
  {
    static const float flashlightIntensity = 5.0f;
    
    flashlight = walktest->Engine->CreateLight ("flashlight",
      csVector3 (0),
      flashlightIntensity*256,
      csColor (flashlightIntensity),
      CS_LIGHT_DYNAMICTYPE_DYNAMIC);
    flashlight->SetType (CS_LIGHT_SPOTLIGHT);
    flashlight->SetAttenuationMode (CS_ATTN_INVERSE);
    flashlight->SetSpotLightFalloff (1, cosf ((25.0/180.0)*PI));
      
    iLightList* ll = walktest->views->GetCamera ()->GetSector ()->GetLights ();
    ll->Add (flashlight);
    
    flashlight->GetMovable()->SetTransform (FlashlightShift (walktest->views->GetCamera ()->GetTransform ()));
    flashlight->GetMovable()->UpdateMove();
  }
  else if (!enable && flashlight)
  {
    iLightList* ll = flashlight->GetMovable()->GetSectors()->Get (0)->GetLights ();
    ll->Remove (flashlight);
    flashlight.Invalidate();
  }
}
Ejemplo n.º 4
0
bool psEffectObjMesh::Update(csTicks elapsed)
{
    if (!anchor || !anchor->IsReady()) // wait for anchor to be ready
        return true;

    if (!psEffectObj::Update(elapsed))
        return false;

    if (keyFrames->GetSize() == 0)
        return true;
    
    // COLOUR
    float lerpfactor = LERP_FACTOR;
    csVector3 lerpColour = LERP_VEC_KEY(KA_COLOUR,lerpfactor);
    mesh->GetMeshObject()->SetColor(csColor(lerpColour.x, lerpColour.y, lerpColour.z));

    // ALPHA
    if (mixmode == CS_FX_ALPHA)
    {
        float lerpAlpha = LERP_KEY(KA_ALPHA,lerpfactor);
        sprState->SetMixMode(CS_FX_SETALPHA(lerpAlpha));
    }

    return true;
}
Ejemplo n.º 5
0
  csColor ProxyLight::SampleLight (const csVector3& point, const csVector3& n,
    float u1, float u2, csVector3& lightVec, float& pdf, VisibilityTester& vistest,
    const csPlane3* visLimitPlane)
  {
    // Setup clipped visibility ray
    const csVector3 lightPos = GetLightSamplePosition (u1, u2);
    csSegment3 visSegment (lightPos, point);
    if (!lightFrustum.Contains (point-lightFrustum.GetOrigin ()) ||
      !lightFrustum.Intersect (visSegment))
    {
      pdf = 0.0f;
      return csColor (0,0,0);
    }

    if (visLimitPlane)
      csIntersect3::SegmentPlane (*visLimitPlane, visSegment);

    vistest.AddSegment (ownerSector->kdTree, visSegment.Start (), visSegment.End ());

    csVector3 parentLightVec;
    csPlane3 transformedPlane;
    transformedPlane = proxyTransform.Other2This (portalPlane);

    const csColor parentLight = parent->SampleLight (point, n, u1, u2, 
      parentLightVec, pdf, vistest, &transformedPlane);
    lightVec = proxyTransform.Other2ThisRelative (parentLightVec);

    return parentLight;
  }
Ejemplo n.º 6
0
  csColor SpotLight::SampleLight (const csVector3& point, const csVector3& n,
    float u1, float u2, csVector3& lightVec, float& pdf, VisibilityTester& vistest,
    const csPlane3* visLimitPlane)
  {
    lightVec = position - point;
    float sqD = lightVec.SquaredNorm ();
    float d = sqrtf (sqD);
    lightVec /= d;
    float dot = -dir * lightVec; // negate because dir points away from the light

    // early out if we're out of the light cone
    if (dot < outer)
      return csColor (0.f);

    csSegment3 visSegment (position, point);

    pdf = 1;

    if (visLimitPlane)
      csIntersect3::SegmentPlane (*visLimitPlane, visSegment);

    vistest.AddSegment (ownerSector->kdTree, visSegment.Start (), visSegment.End ());

    float falloff;
    // early out if no edge softening
    if (fabs (outer - inner) < 0.0001)
      falloff = 1.f;
    else
      falloff = csSmoothStep(dot, inner, outer);
    csColor res = color * ComputeAttenuation (sqD) * falloff;

    return res;
  }
Ejemplo n.º 7
0
bool psEffectObjMesh::Render(const csVector3 &up)
{
    static unsigned long nextUniqueID = 0;
    csString effectID = "effect_mesh_";
    effectID += nextUniqueID++;

    // create a mesh wrapper from the factory we just created
    if(mesh.IsValid())
    {
        engine->RemoveObject(mesh);
    }
    mesh = engine->CreateMeshWrapper(meshFact, effectID.GetData());

    // do the up vector
    objUp = up;
    csReversibleTransform rt;
    rt.LookAt(up, csVector3(1,2,0));
    matUp = rt.GetT2O();
    matBase = matUp;

    // common flags
    mesh->GetFlags().Set(CS_ENTITY_NOHITBEAM);
    mesh->SetZBufMode(zFunc);
    mesh->SetRenderPriority(priority);

    // disable culling
    csStringID viscull_id = globalStringSet->Request ("viscull");
    mesh->GetMeshObject()->GetObjectModel()->SetTriangleData(viscull_id, 0);

    // add the custom material if set
    if (!materialName.IsEmpty())
    {
        csRef<iMaterialWrapper> mat = effectsCollection->FindMaterial(materialName);
        if (mat.IsValid())
        {
            mesh->GetMeshObject()->SetMaterialWrapper(mat);
        }
    }

    // obj specific    
    sprState =  scfQueryInterface<iSprite3DState> (mesh->GetMeshObject());

    if (sprState)
    {
        sprState->EnableTweening(true);
        sprState->SetAction("default");
        sprState->SetLighting(false);

        if (mixmode != CS_FX_ALPHA)
        {
            sprState->SetMixMode(mixmode);
        }
    }
    
    mesh->GetMeshObject()->SetColor(csColor(1.0f, 1.0f, 1.0f));
    
    return true;
}
Ejemplo n.º 8
0
void AvatarTest::CreateRoom ()
{
  // Creating the background
  // First we make a primitive for our geometry.
  CS::Geometry::DensityTextureMapper bgMapper (0.3f);
  CS::Geometry::TesselatedBox bgBox (csVector3 (-4000), csVector3 (4000));
  bgBox.SetMapper (&bgMapper);
  bgBox.SetFlags (CS::Geometry::Primitives::CS_PRIMBOX_INSIDE);
  
  // Now we make a factory and a mesh at once.
  csRef<iMeshWrapper> background =
    CS::Geometry::GeneralMeshBuilder::CreateFactoryAndMesh (engine, room,
				   "background", "background_factory", &bgBox);

  csRef<iMaterialWrapper> bgMaterial =
    CS::Material::MaterialBuilder::CreateColorMaterial
    (GetObjectRegistry (), "background", csColor (0.398f));
  background->GetMeshObject()->SetMaterialWrapper (bgMaterial);

  // Set up of the physical collider for the roof
  if (physicsEnabled)
    dynamicSystem->AttachColliderPlane (csPlane3 (csVector3 (0.0f, 1.0f, 0.0f), 0.0f),
					10.0f, 0.0f);
  // Creating lights
  csRef<iLight> light;
  iLightList* ll = room->GetLights ();

  // This light is for the background
  // TODO: putting instead the following line creates a black background, otherwise it is grey
  // the behavior doesn't persist if the other lights are removed
  //light = engine->CreateLight(0, csVector3(1, 1, -1), 9000, csColor (1));
  light = engine->CreateLight(0, csVector3(1, 1, 0), 9000, csColor (1));
  light->SetAttenuationMode (CS_ATTN_NONE);
  ll->Add (light);

  // Other lights
  light = engine->CreateLight (0, csVector3 (3, 0, 0), 8, csColor (1));
  light->SetAttenuationMode (CS_ATTN_REALISTIC);
  ll->Add (light);

  light = engine->CreateLight (0, csVector3 (-3, 0,  0), 8, csColor (1));
  light->SetAttenuationMode (CS_ATTN_REALISTIC);
  ll->Add (light);

  light = engine->CreateLight (0, csVector3 (0, 0, -3), 8, csColor (1));
  light->SetAttenuationMode (CS_ATTN_REALISTIC);
  ll->Add (light);

  light = engine->CreateLight (0, csVector3 (0, 0, 3), 8, csColor (1));
  light->SetAttenuationMode (CS_ATTN_REALISTIC);
  ll->Add (light);

  light = engine->CreateLight (0, csVector3 (0, -3, 0), 8, csColor (1));
  light->SetAttenuationMode (CS_ATTN_REALISTIC);
  ll->Add (light);

  engine->Prepare ();

  CS::Lighting::SimpleStaticLighter::ShineLights (room, engine, 4);
}
Ejemplo n.º 9
0
static csColor GetPixelWrap (iImage* img, int img_w, int img_h, 
                             int x, int y)
{
  // Wrap around the texture coordinates.
  x = x % img_w;
  y = y % img_h;

  // Get the pixel.
  csRGBpixel* px = (csRGBpixel*)img->GetImageData() + x + (y * img_h);
  return csColor (px->red, px->green, px->blue);
}
Ejemplo n.º 10
0
void Simple::CreateRoom ()
{
  // We create a new sector called "room".
  room = engine->CreateSector ("room");

  // Create our world.
  ReportInfo ("Creating world!...");

  loader->LoadTexture ("stone", "/lib/std/stone4.gif");
  
  csRef<iPluginManager> plugin_mgr (
    csQueryRegistry<iPluginManager> (GetObjectRegistry()));
  // Initialize the python plugin.
  csRef<iScript> is = csQueryRegistry<iScript> (GetObjectRegistry());
  if (is)
  {
    char const* module = "pysimp";
    csRef<iCommandLineParser> cmd =
      csQueryRegistry<iCommandLineParser> (GetObjectRegistry());
    if (cmd.IsValid())
    {
      char const* file = cmd->GetName(0);
      if (file != 0)
        module = file;
    }

    // Load a python module.
    ReportInfo ("Loading script file %s...", CS::Quote::Single (module));
    if (!is->LoadModule (module))
      return;

    // Set up our room.
    // Execute one method defined in pysimp.py
    // This will create the polygons in the room.
    csString run;
    run << module << ".CreateRoom";
    // prepare arguments
    csRefArray<iScriptValue> args;
    args.Push(csRef<iScriptValue>(is->RValue("stone")));
    // run method
    csRef<iScriptValue> ret = is->Call(run,args);
    if(!ret.IsValid())
    {
      ReportError ("Failed running '%s.CreateRoom'...",module);
    }
  }
  else
    ReportError ("Could not load Python plugin");

  csRef<iLight> light;
  light = engine->CreateLight (0, csVector3 (0, 5, 0), 10,
    csColor (1, 0, 0));
  room->GetLights ()->Add (light);
}
Ejemplo n.º 11
0
csRef<iLight> WalkTestLights::CreateRandomLight (const csVector3& pos, iSector* where,
    float dyn_radius)
{
  csRef<iLight> dyn;
  float r, g, b;
  RandomColor (r, g, b);
  dyn = walktest->Engine->CreateLight ("",
    	pos, dyn_radius, csColor(r, g, b), CS_LIGHT_DYNAMICTYPE_DYNAMIC);
  where->GetLights ()->Add (dyn);
  dynamic_lights.Push (dyn);
  return dyn;
}
bool psEffectObjSpire::Render(const csVector3 &up)
{
    static unsigned long nextUniqueID = 0;
    csString effectID = "effect_spire_";
    effectID += nextUniqueID++;

    // create a mesh wrapper from the factory we just created
    mesh = engine->CreateMeshWrapper(meshFact, effectID.GetData());

    // do the up vector
    objUp = up;
    csReversibleTransform rt;
    rt.LookAt(csVector3(up.x, up.z, up.y), csVector3(0,2,1));
    matUp = rt.GetT2O();
    matBase = matUp;

    // common flags
    mesh->GetFlags().Set(CS_ENTITY_NOHITBEAM);
    mesh->SetZBufMode(zFunc);
    mesh->SetRenderPriority(priority);

    // disable culling
    csStringID viscull_id = globalStringSet->Request("viscull");
    mesh->GetMeshObject()->GetObjectModel()->SetTriangleData(viscull_id, 0);

    // obj specific
    genState =  scfQueryInterface<iGeneralMeshState> (mesh->GetMeshObject());

    mesh->GetMeshObject()->SetColor(csColor(1,1,1)); // to check
    //genState->SetColor(csColor(1,1,1)); was not working with latest CS

    if(mixmode != CS_FX_ALPHA)
        mesh->GetMeshObject()->SetMixMode(mixmode);  // to check
    //genState->SetMixMode(mixmode); was not working with latest CS

    genState->SetAnimationControl(meshControl);

    vert = new csVector3[vertCount];
    texel = new csVector2[vertCount];
    colour = new csColor4[vertCount];
    for(int a=0; a<vertCount; ++a)
        colour[a].Set(1,1,1,1);

    return true;
}
Ejemplo n.º 13
0
void IslandDemo::Frame ()
{
  iCamera* camera = view->GetCamera ();

  // Update the fog when the camera transitions in/out the water
  if ((inWater && camera->GetTransform ().GetOrigin ().y > WATER_LEVEL)
      || (!inWater && camera->GetTransform ().GetOrigin ().y < WATER_LEVEL))
  {
    inWater = !inWater;

    if (inWater)
      room->SetFog (0.001f, csColor (0.3f, 0.3, 0.9f));

    else
      room->DisableFog ();
  }

  // Default behavior from DemoApplication
  DemoApplication::Frame ();
}
Ejemplo n.º 14
0
bool WalkTestLights::HandleDynLight (iLight* dyn)
{
  LightStruct* ls = (LightStruct*)(WalkDataObject::GetData(dyn->QueryObject ()));
  switch (ls->type)
  {
  case DYN_TYPE_MISSILE:
    {
      MissileStruct* ms = (MissileStruct*)(WalkDataObject::GetData(
        dyn->QueryObject ()));
      csVector3 v (0, 0, 2.5);
      csVector3 old = dyn->GetMovable()->GetPosition ();
      v = old + ms->dir.GetT2O () * v;
      iSector* s = dyn->GetMovable()->GetSectors()->Get(0);
      bool mirror = false;
      csVector3 old_v = v;
      s = s->FollowSegment (ms->dir, v, mirror);
      if (ABS (v.x-old_v.x) > SMALL_EPSILON ||
        ABS (v.y-old_v.y) > SMALL_EPSILON ||
        ABS (v.z-old_v.z) > SMALL_EPSILON)
      {
        v = old;
        if (ms->sprite)
        {
          ms->sprite->GetMovable ()->ClearSectors ();
          Sys->Engine->GetMeshes ()->Remove (ms->sprite);
        }
        csRef<WalkDataObject> ido (
          CS::GetChildObject<WalkDataObject>(dyn->QueryObject()));
        dyn->QueryObject ()->ObjRemove (ido);
        if (ms->snd)
        {
          ms->snd_stream->Pause();
        }
        delete ms;
        if (Sys->mySound)
        {
          if (Sys->wMissile_boom)
          {
            csRef<iSndSysStream> st = Sys->mySound->CreateStream (
              Sys->wMissile_boom->GetData (), CS_SND3D_ABSOLUTE);
            csRef<iSndSysSource> sndsource = Sys->mySound->
              CreateSource (st);
            if (sndsource)
            {
              csRef<iSndSysSource3D> sndsource3d
                = scfQueryInterface<iSndSysSource3D> (sndsource);

              sndsource3d->SetPosition (v);
              sndsource->SetVolume (1.0f);
              st->SetLoopState (CS_SNDSYS_STREAM_DONTLOOP);
              st->Unpause ();
            }
          }
        }
        ExplosionStruct* es = new ExplosionStruct;
        es->type = DYN_TYPE_EXPLOSION;
        es->radius = 2;
        es->dir = 1;
        WalkDataObject* esdata = new WalkDataObject (es);
        dyn->QueryObject ()->ObjAdd (esdata);
        esdata->DecRef ();
        WalkTestParticleDemos::Explosion (Sys,
          dyn->GetMovable ()->GetSectors ()->Get (0),
          dyn->GetMovable ()->GetPosition (), "explo");
        return false;
      }
      else ms->dir.SetOrigin (v);
      iSector* sector = dyn->GetMovable ()->GetSectors ()->Get (0);
      if (sector != s)
      {
        dyn->IncRef ();
        sector->GetLights ()->Remove (dyn);
        s->GetLights ()->Add (dyn);
        dyn->DecRef ();
      }
      dyn->GetMovable ()->SetPosition (v);
      if (ms->sprite) move_mesh (ms->sprite, s, v);
      if (Sys->mySound && ms->snd)
      {
        csRef<iSndSysSource3D> sndsource3d
          = scfQueryInterface<iSndSysSource3D> (ms->snd);
        sndsource3d->SetPosition (v);
        ms->snd->SetVolume (1.0f);
      }
      break;
    }
  case DYN_TYPE_EXPLOSION:
    {
      ExplosionStruct* es = (ExplosionStruct*)(WalkDataObject::GetData(
        dyn->QueryObject ()));
      if (es->dir == 1)
      {
        es->radius += 3;
	if (es->radius > 6) es->dir = -1;
      }
      else
      {
        es->radius -= 2;
	if (es->radius < 1)
	{
	  csRef<WalkDataObject> ido (
		CS::GetChildObject<WalkDataObject>(dyn->QueryObject()));
	  dyn->QueryObject ()->ObjRemove (ido);
	  delete es;
    iSector* sector = dyn->GetMovable ()->GetSectors ()->Get (0);
	  sector->GetLights ()->Remove (dyn);
	  return true;
	}
      }
      dyn->SetCutoffDistance (es->radius);
      break;
    }
    case DYN_TYPE_RANDOM:
    {
      RandomLight* rl = (RandomLight*)(WalkDataObject::GetData(
      	dyn->QueryObject ()));
      rl->dyn_move += rl->dyn_move_dir;
      if (rl->dyn_move < 0 || rl->dyn_move > 2)
      	rl->dyn_move_dir = -rl->dyn_move_dir;
      if (ABS (rl->dyn_r1-dyn->GetColor ().red) < .01 &&
      	  ABS (rl->dyn_g1-dyn->GetColor ().green) < .01 &&
	  ABS (rl->dyn_b1-dyn->GetColor ().blue) < .01)
        RandomColor (rl->dyn_r1, rl->dyn_g1, rl->dyn_b1);
      else
        dyn->SetColor (csColor ((rl->dyn_r1+7.*dyn->GetColor ().red)/8.,
		(rl->dyn_g1+7.*dyn->GetColor ().green)/8.,
		(rl->dyn_b1+7.*dyn->GetColor ().blue)/8.));
      dyn->GetMovable()->SetPosition (dyn->GetMovable()->GetPosition () + csVector3 (0, rl->dyn_move_dir, 0));
      dyn->GetMovable()->UpdateMove();
      break;
    }
  }
  return false;
}
Ejemplo n.º 15
0
//===========================================================================
// Demo particle system (explosion).
//===========================================================================
static void add_particles_explosion (WalkTest* Sys, iSector* sector, iEngine* engine,
	const csVector3& center, const char* matname)
{
  // First check if the material exists.
  iMaterialWrapper* mat = Sys->Engine->GetMaterialList ()->
  	FindByName (matname);
  if (!mat)
  {
    Sys->Report (CS_REPORTER_SEVERITY_NOTIFY, "Can't find material '%s' in memory!", matname);
    return;
  }

  csRef<iMeshFactoryWrapper> mfw = engine->CreateMeshFactory (
      "crystalspace.mesh.object.particles", "explosion");
  if (!mfw) return;

  csRef<iMeshWrapper> exp = engine->CreateMeshWrapper (mfw, "custom explosion",
	sector, center);

  exp->SetZBufMode(CS_ZBUF_TEST);
  exp->SetRenderPriority (engine->GetAlphaRenderPriority ());
  exp->GetMeshObject()->SetMaterialWrapper (mat);
  exp->GetMeshObject()->SetMixMode (CS_FX_ALPHA);
  exp->GetMeshObject()->SetColor (csColor (1, 1, 0));

  csRef<iParticleBuiltinEmitterFactory> emit_factory = 
      csLoadPluginCheck<iParticleBuiltinEmitterFactory> (
        Sys->object_reg, "crystalspace.mesh.object.particles.emitter", false);
  csRef<iParticleBuiltinEffectorFactory> eff_factory = 
      csLoadPluginCheck<iParticleBuiltinEffectorFactory> (
        Sys->object_reg, "crystalspace.mesh.object.particles.effector", false);

  csRef<iParticleBuiltinEmitterSphere> sphereemit = emit_factory->
    CreateSphere ();
  sphereemit->SetRadius (0.1f);
  sphereemit->SetParticlePlacement (CS_PARTICLE_BUILTIN_CENTER);
  sphereemit->SetPosition (csVector3 (0, 0, 0));
  sphereemit->SetInitialVelocity (csVector3 (1, 0, 0), csVector3 (3, 3, 3));
  sphereemit->SetUniformVelocity (false);
  sphereemit->SetDuration (0.1f);
  sphereemit->SetEmissionRate (1000.0f);
  sphereemit->SetInitialTTL (1.0f, 1.0f);

  csRef<iParticleBuiltinEffectorLinColor> lincol = eff_factory->
    CreateLinColor ();
  lincol->AddColor (csColor4 (1,1,1,1), 1.0f);
  lincol->AddColor (csColor4 (1,1,1,0), 0.0f);

  csRef<iParticleSystem> partstate =
  	scfQueryInterface<iParticleSystem> (exp->GetMeshObject ());
  partstate->SetParticleSize (csVector2 (0.15f, 0.15f));
  partstate->SetRotationMode (CS_PARTICLE_ROTATE_VERTICES);
  partstate->SetIntegrationMode (CS_PARTICLE_INTEGRATE_BOTH);
  partstate->AddEmitter (sphereemit);
  partstate->AddEffector (lincol);

  Sys->Engine->DelayedRemoveObject (1100, exp);
  Sys->Engine->DelayedRemoveObject (1101, mfw);

  exp->PlaceMesh ();
}