Beispiel #1
0
void World::GetVisibleLights(Node * node, List<Light*> & lights)
{
    profile_code();

    lights.Clear();

    List<Light*>::Iterator iter;
    List<Light*>::Iterator end;

    iter = mLights.Begin();
    end = mLights.End();

    Light * light;

    while (iter != end)
    {
        light = *iter;

        if (light->IsVisible())
        {
            LIGHT_TYPE type = light->GetType();

            if (type == LT_DIRECTIONAL)
            {
                lights.PushBack(light);
            }

            else if (type == LT_POINT)
            {
                const Sphere & sph = node->GetWorldBoundingSphere();
                float len = Math::VecLength(light->GetPosition() - sph.center);
                if (len - sph.radius < light->GetRange())
                    lights.PushBack(light);
            }

            else if (type == LT_SPOT)
            {
                Aabb aabb;
                Vec3 ltf, lbf, rtf, rbf;
                Vec3 ltb, lbb, rtb, rbb;
                Vec3 lp;

                float sq1, sq2, sq3, sq4;
                float sq5, sq6, sq7, sq8;
                float rsq;

                Vec3 d1, d2, d3, d4;
                Vec3 d5, d6, d7, d8;
                Vec3 ld;
                float dt1, dt2, dt3, dt4;
                float dt5, dt6, dt7, dt8;
                float mdt;

                aabb = node->GetWorldAabb();

                ltf = aabb.GetLeftTopFrontPoint();
                lbf = aabb.GetLeftBottomFrontPoint();
                rtf = aabb.GetRightTopFrontPoint();
                rbf = aabb.GetRightBottomFrontPoint();

                ltb = aabb.GetLeftTopBackPoint();
                lbb = aabb.GetLeftBottomBackPoint();
                rtb = aabb.GetRightTopBackPoint();
                rbb = aabb.GetRightBottomBackPoint();

                lp = light->GetPosition();
                rsq = light->GetRange() * light->GetRange();

                sq1 = Math::VecDistanceSq(lp, ltf);
                sq2 = Math::VecDistanceSq(lp, lbf);
                sq3 = Math::VecDistanceSq(lp, rtf);
                sq4 = Math::VecDistanceSq(lp, rbf);
                sq5 = Math::VecDistanceSq(lp, ltb);
                sq6 = Math::VecDistanceSq(lp, lbb);
                sq7 = Math::VecDistanceSq(lp, rtb);
                sq8 = Math::VecDistanceSq(lp, rbb);

                if (sq1 < rsq || sq2 < rsq || sq3 < rsq || sq4 < rsq ||
                        sq5 < rsq || sq6 < rsq || sq7 < rsq || sq8 < rsq)
                {
                    ld = light->GetDirection();
                    mdt = light->GetOuter();

                    d1 = ltf - lp;
                    d2 = lbf - lp;
                    d3 = rtf - lp;
                    d4 = rbf - lp;
                    d5 = ltb - lp;
                    d6 = lbb - lp;
                    d7 = rtb - lp;
                    d8 = rbb - lp;

                    Math::VecNormalize(d1, d1);
                    Math::VecNormalize(d2, d2);
                    Math::VecNormalize(d3, d3);
                    Math::VecNormalize(d4, d4);
                    Math::VecNormalize(d5, d5);
                    Math::VecNormalize(d6, d6);
                    Math::VecNormalize(d7, d7);
                    Math::VecNormalize(d8, d8);

                    dt1 = Math::VecDot(ld, d1);
                    dt2 = Math::VecDot(ld, d2);
                    dt3 = Math::VecDot(ld, d3);
                    dt4 = Math::VecDot(ld, d4);
                    dt5 = Math::VecDot(ld, d5);
                    dt6 = Math::VecDot(ld, d6);
                    dt7 = Math::VecDot(ld, d7);
                    dt8 = Math::VecDot(ld, d8);

                    if (dt1 > mdt || dt2 > mdt || dt3 > mdt || dt4 > mdt ||
                            dt5 > mdt || dt6 > mdt || dt7 > mdt || dt7 > mdt)
                    {
                        lights.PushBack(light);
                    }
                }
            }
        }

        ++iter;
    }
}
void DebugRenderSystem::Process()
{
    uint32 size = entities.size();
	for(uint32 i = 0; i < size; ++i)
	{
        Entity * entity = entities[i];
        
        DebugRenderComponent * debugRenderComponent = cast_if_equal<DebugRenderComponent*>(entity->GetComponent(Component::DEBUG_RENDER_COMPONENT));
        TransformComponent * transformComponent = cast_if_equal<TransformComponent*>(entity->GetComponent(Component::TRANSFORM_COMPONENT));
        //RenderComponent * renderComponent = cast_if_equal<RenderComponent*>(entity->GetComponent(Component::RENDER_COMPONENT));
        
        Matrix4 worldTransform = /*(*transformComponent->GetWorldTransform()) * */camera->GetMatrix();
        RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, camera->GetMatrix());

        AABBox3 debugBoundigBox = entity->GetWTMaximumBoundingBoxSlow();
        uint32 debugFlags = debugRenderComponent->GetDebugFlags();

		// Camera debug draw
		if(debugFlags & DebugRenderComponent::DEBUG_DRAW_CAMERA)
		{
			CameraComponent * entityCameraComp = (CameraComponent *) entity->GetComponent(Component::CAMERA_COMPONENT);

			if(NULL != entityCameraComp)
			{
				Camera* entityCamera = entityCameraComp->GetCamera();
				if(NULL != entityCamera && camera != entityCamera)
				{
					Color camColor(0.0f, 1.0f, 0.0f, 1.0f);
					Vector3 camPos = entityCamera->GetPosition();
					//Vector3 camDirect = entityCamera->GetDirection();
					AABBox3 camBox(camPos, 2.5f);

					// If this is clip camera - show it as red camera
					if (entityCamera == entity->GetScene()->GetClipCamera()) camColor = Color(1.0f, 0.0f, 0.0f, 1.0f);

					RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR);
					RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE);
					RenderManager::Instance()->SetColor(camColor);

					RenderHelper::Instance()->DrawBox(camBox, 2.5f);

					RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE);
					RenderManager::Instance()->ResetColor();

					debugBoundigBox = camBox;
				}
			}
		}

		// UserNode debug draw
		if(debugFlags & DebugRenderComponent::DEBUG_DRAW_USERNODE)
		{
			if(NULL != entity->GetComponent(Component::USER_COMPONENT))
			{
				Color dcColor(0.0f, 0.0f, 1.0f, 1.0f);
				AABBox3 dcBox(Vector3(), 1.0f);

				Matrix4 prevMatrix = RenderManager::Instance()->GetMatrix(RenderManager::MATRIX_MODELVIEW);
				Matrix4 finalMatrix = transformComponent->GetWorldTransform() * prevMatrix;
				RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, finalMatrix);

				RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR);
				RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE | RenderState::STATE_DEPTH_TEST);

				RenderManager::Instance()->SetColor(1.f, 1.f, 0, 1.0f);
				RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(1.f, 0, 0));
				RenderManager::Instance()->SetColor(1.f, 0, 1.f, 1.0f);
				RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(0, 1.f, 0));
				RenderManager::Instance()->SetColor(0, 1.f, 1.f, 1.0f);
				RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(0, 0, 1.f));

				RenderManager::Instance()->SetColor(dcColor);
				RenderHelper::Instance()->DrawBox(dcBox);

				RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE);
				RenderManager::Instance()->ResetColor();
				RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, prevMatrix);

				dcBox.GetTransformedBox(transformComponent->GetWorldTransform(), debugBoundigBox);
			}
		}

		// LightNode debug draw
		if (debugFlags & DebugRenderComponent::DEBUG_DRAW_LIGHT_NODE)
		{
			LightComponent *lightComp = (LightComponent *) entity->GetComponent(Component::LIGHT_COMPONENT);

			if(NULL != lightComp)
			{
				Light* light = lightComp->GetLightObject();

				if(NULL != light)
				{
					Vector3 lPosition = light->GetPosition();

					RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR);
					RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE);
					RenderManager::Instance()->SetColor(1.0f, 1.0f, 0.0f, 1.0f);

					switch (light->GetType())
					{
					case Light::TYPE_DIRECTIONAL:
						{
							Vector3 lDirection = light->GetDirection();

							RenderHelper::Instance()->DrawArrow(lPosition, lPosition + lDirection * 10, 2.5f);
							RenderHelper::Instance()->DrawBox(AABBox3(lPosition, 0.5f), 1.5f);

							debugBoundigBox = AABBox3(lPosition, 2.5f);
						}
						break;
					default:
						{
							AABBox3 lightBox(lPosition, 2.5f);
							RenderHelper::Instance()->DrawBox(lightBox, 2.5f);

							debugBoundigBox = lightBox;
						}
						break;
					}

					RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE);
					RenderManager::Instance()->ResetColor();
				}
			}
		}
        
        if ((debugFlags & DebugRenderComponent::DEBUG_DRAW_AABOX_CORNERS))
        {            
            RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR);
            RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE | RenderState::STATE_DEPTH_TEST);
            RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
            RenderHelper::Instance()->DrawCornerBox(debugBoundigBox, 1.5f);
            RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE);
            RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
            //		RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, prevMatrix);
        }
        
        if (debugFlags & DebugRenderComponent::DEBUG_DRAW_RED_AABBOX)
        {
            RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR);
            RenderManager::Instance()->SetState(RenderState::STATE_COLORMASK_ALL | RenderState::STATE_DEPTH_WRITE);
            RenderManager::Instance()->SetColor(1.0f, 0.0f, 0.0f, 1.0f);
            RenderHelper::Instance()->DrawBox(debugBoundigBox, 1.5f);
            RenderManager::Instance()->SetState(RenderState::DEFAULT_3D_STATE);
            RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
        }
        

        // UserNode Draw
#if 0
       	
        if (debugFlags & DEBUG_DRAW_USERNODE)
        {
            Matrix4 prevMatrix = RenderManager::Instance()->GetMatrix(RenderManager::MATRIX_MODELVIEW);
            Matrix4 finalMatrix = worldTransform * prevMatrix;
            RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, finalMatrix);
            
            RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR);
            RenderManager::Instance()->SetState(RenderStateBlock::STATE_COLORMASK_ALL | RenderStateBlock::STATE_DEPTH_WRITE | RenderStateBlock::STATE_DEPTH_TEST);
            RenderManager::Instance()->SetColor(0, 0, 1.0f, 1.0f);
            RenderHelper::Instance()->DrawBox(drawBox);
            RenderManager::Instance()->SetColor(1.f, 1.f, 0, 1.0f);
            RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(1.f, 0, 0));
            RenderManager::Instance()->SetColor(1.f, 0, 1.f, 1.0f);
            RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(0, 1.f, 0));
            RenderManager::Instance()->SetColor(0, 1.f, 1.f, 1.0f);
            RenderHelper::Instance()->DrawLine(Vector3(0, 0, 0), Vector3(0, 0, 1.f));
            RenderManager::Instance()->SetState(RenderStateBlock::DEFAULT_3D_STATE);
            RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
            RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, prevMatrix);
        }
#endif
        
#if 0
        // ParticleEffectNode
        if (debugFlags != DEBUG_DRAW_NONE)
        {
            if (!(flags & SceneNode::NODE_VISIBLE))return;
            
            RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR);
            RenderManager::Instance()->SetState(RenderStateBlock::STATE_COLORMASK_ALL | RenderStateBlock::STATE_DEPTH_WRITE);
            
            Vector3 position = Vector3(0.0f, 0.0f, 0.0f) * GetWorldTransform();
            Matrix3 rotationPart(GetWorldTransform());
            Vector3 direction = Vector3(0.0f, 0.0f, 1.0f) * rotationPart;
            direction.Normalize();
            
            RenderManager::Instance()->SetColor(0.0f, 0.0f, 1.0f, 1.0f);
            
            RenderHelper::Instance()->DrawLine(position, position + direction * 10, 2.f);
            
            RenderManager::Instance()->SetState(RenderStateBlock::DEFAULT_3D_STATE);
            RenderManager::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
        }
#endif
        
    }
}