Exemplo n.º 1
0
Vector3 Colorful::Shade(const HitInfo& hit, const Scene& scene) const {
    const Ray& ray = hit.eyeRay;
    const Vector3 viewDir = -ray.direction;
    const std::vector<Light*>& lights = scene.GetLights();
    Vector3 shadedColor(0.0f, 0.0f, 0.0f);

    // Loop over all lights
    std::vector<Light*>::const_iterator light_it = lights.begin();
    for (light_it=lights.begin(); light_it != lights.end(); ++light_it) {
		Light* light = *light_it;
		Vector3 direction = light->GetPosition() - hit.point;

		float distance2 = direction.Length2();
		float falloff = light->Falloff(distance2);
		direction.Normalize();

		const float intensity = hit.normal.Dot(direction) * falloff * light->GetWattage() * math::INV_PI;
		if( intensity > 0.0f ) {
			Vector3 color = light->GetColor();
			color *= intensity;

			color = Vector3 (  (1-hit.texCoord.y)*(1-hit.texCoord.y),
							hit.texCoord.x*hit.texCoord.x,
							hit.texCoord.x );

			shadedColor += color;
		}
	}

    return shadedColor;
}
Exemplo n.º 2
0
void DAVA::ShadowVolumeNode::DrawShadow()
{
	Matrix4 prevMatrix = RenderManager::Instance()->GetMatrix(RenderManager::MATRIX_MODELVIEW); 
	Matrix4 meshFinalMatrix = GetWorldTransform() * prevMatrix;
	RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, meshFinalMatrix);

	Matrix4 projMatrix = RenderManager::Instance()->GetMatrix(RenderManager::MATRIX_PROJECTION);

	RenderManager::Instance()->SetShader(shader);
	RenderManager::Instance()->SetRenderData(shadowPolygonGroup->renderDataObject);
	RenderManager::Instance()->FlushState();
	RenderManager::Instance()->AttachRenderData();

	Vector3 position = Vector3() * GetWorldTransform();
	Light * light = scene->GetNearestDynamicLight(Light::TYPE_COUNT, position);
	if (light && uniformLightPosition0 != -1)
	{
		Vector3 lightPosition0 = light->GetPosition();
		const Matrix4 & matrix = scene->GetCurrentCamera()->GetMatrix();
		lightPosition0 = lightPosition0 * matrix;

		shader->SetUniformValueByIndex(uniformLightPosition0, lightPosition0);
	}

	if (shadowPolygonGroup->renderDataObject->GetIndexBufferID() != 0)
	{
		RenderManager::Instance()->HWDrawElements(PRIMITIVETYPE_TRIANGLELIST, shadowPolygonGroup->indexCount, EIF_16, 0);
	}
	else
	{
		RenderManager::Instance()->HWDrawElements(PRIMITIVETYPE_TRIANGLELIST, shadowPolygonGroup->indexCount, EIF_16, shadowPolygonGroup->indexArray);
	}

	RenderManager::Instance()->SetMatrix(RenderManager::MATRIX_MODELVIEW, prevMatrix);
}
Exemplo n.º 3
0
void World::CullLights(VisibleCullResult & result, Camera * cam)
{
    List<Light*>::Iterator whr = mLights.Begin();
    List<Light*>::Iterator end = mLights.End();

    while (whr != end)
    {
        // current it's false
        Light * light = *whr;

        if (!light->IsVisible())
            continue;

        switch (light->GetType())
        {
        case LT_DIRECTIONAL:
            result.lights.PushBack(light);
            break;

        case LT_POINT:
        case LT_SPOT:
        {
            Sphere sph = Sphere(light->GetPosition(), light->GetRange());
            if (cam->IsVisible(sph))
                result.lights.PushBack(light);
        }
        break;
        }

        ++whr;
    }
}
Exemplo n.º 4
0
Light * Scene::GetNearestDynamicLight(Light::eType type, Vector3 position)
{
    switch(type)
    {
        case Light::TYPE_DIRECTIONAL:
            
            break;
            
        default:
            break;
    };
    
	float32 squareMinDistance = 10000000.0f;
	Light * nearestLight = 0;

	Set<Light*> & lights = GetLights();
	const Set<Light*>::iterator & endIt = lights.end();
	for (Set<Light*>::iterator it = lights.begin(); it != endIt; ++it)
	{
		Light * node = *it;
		if(node->IsDynamic())
		{
			const Vector3 & lightPosition = node->GetPosition();

			float32 squareDistanceToLight = (position - lightPosition).SquareLength();
			if (squareDistanceToLight < squareMinDistance)
			{
				squareMinDistance = squareDistanceToLight;
				nearestLight = node;
			}
		}
	}

	return nearestLight;
}
Exemplo n.º 5
0
bool Scene::IsLightUnblocked(const Light& light, const Point<float>& point) const
{
	Point<float> lightPosition = light.GetPosition();
	Vector3<float> lightDirection = light.IsDirectional() ? Vector3<>(lightPosition.x, lightPosition.y, lightPosition.z) : lightPosition - point;

	// Create a ray to cast at the direction of the light:
	Ray ray(point, lightDirection);

	// Add a little bit of offset to handle the numeric errors:
	ray.origin = ray.origin + ray.direction * 0.00001f;

	// Calculate distance between the light and the intersection point:
	float lightDistance = lightDirection.length();
	
	// Find if there is any object between the light and the object:
	if (!IsLightUnblocked<Sphere>(m_spheres, light, lightDistance, ray) ||
		!IsLightUnblocked<GenericMesh>(m_genericMeshes, light, lightDistance, ray)
		)
		return false;

	return true;
}
void TerrainShader::LoadLight(Light& light, float ambientLight)
{
	LoadVector(location_lightPosition, light.GetPosition());
	LoadVector(location_lightColor, light.GetColor());
	LoadFloat(location_ambientLight, ambientLight);
}
Exemplo n.º 7
0
int main(){
	try{
		ILogger::Init();
		Settings::Call().Parse();
		ResourceManager::Call().AddPath("Data/shaders", "Shader");
		ResourceManager::Call().AddPath("Data/textures", "Image");

		{
			Window myWindow;
			Image Crate;
			Texture CrateTexture;
			Text FPSText, MousePosText;
			Clock FrameClock, FpsClock;
		
			Input myInput;
			myInput.Init(myWindow);

			Renderer& myRenderer = Renderer::Call();
			myRenderer.Init(myWindow);
		
			Crate.LoadFromFile("crate.jpg");
			CrateTexture.LoadFromImage(Crate);

			Light l;
			l.SetPosition(Vector3F(1,3,1.5));
			l.SetDiffuse(Color(1.f,1.f,1.f));
			l.SetRange(8);

			Shader ColorShader;
			ColorShader.Compile("shaderColor.vs", "shaderColor.fs");
			ColorShader.Bind();
			ColorShader.SendColor("ambientColor", myRenderer.GetSpecifications().mAmbientColor);
			ColorShader.SendFloat("constantAtt", l.GetAttenuationConstant());
			ColorShader.SendFloat("linearAtt", l.GetAttenuationLinear());
			ColorShader.SendFloat("quadraticAtt", l.GetAttenuationQuadratic());
			ColorShader.SendFloat("range", l.GetRange());
	
			ColorShader.SendVector3("lightPosition", l.GetPosition());
			ColorShader.SendColor("lightColor", l.GetDiffuse());
			ColorShader.UnBind();

			Object obj1;
			obj1.MakeCube("cube", ColorShader);
			obj1.GetMaterial().mAmbient = Color(0.f, 0.08f, 0.08f);
			obj1.GetMaterial().mDiffuse = Color(0.f, 0.8f, 0.8f);
			obj1.GetMaterial().mSpecular = Color(0.0f, 0.5f, 0.5f);
			obj1.GetMaterial().mShininess = 50.f;

			Camera cam;
			cam.LookAt(Vector3F(0.5f,0,1), Vector3F(-2.5f,2,4));
		
			FPSText.SetSize(12);
			FPSText.SetPosition(10,10);
			MousePosText.SetSize(12);
			MousePosText.SetPosition(10,22);

			while(myWindow.IsOpened()){
				ElapsedTime = FrameClock.GetElapsedTime();
				FrameClock.Reset();

				if(FpsClock.GetElapsedTime() > 1.f){
					FPSText.SetText(String(1.f/ElapsedTime));
					FpsClock.Reset();
				}

			
				while(myInput.GetEvent()){
					if(myInput.GetEventType() == sf::Event::Closed)
						myWindow.Close();

					if(myInput.IsKeyHit(Space))
						if(!paused){
							paused = true;
							FrameClock.Pause();
						}else{
							paused = false;
							FrameClock.Resume();
						}
				}

				MousePosText.SetText(String("X : ")+myInput.GetMouseX()+" Y : "+myInput.GetMouseY());
			
				MousePosText.Draw();
				FPSText.Draw();
				obj1.Draw();

				myRenderer.BeginScene(myRenderer.GetSpecifications().mAmbientColor);
					myRenderer.Render();
				myRenderer.EndScene();
			}
		}
	}catch(Exception e){
		std::cout << e.what() << std::endl;
		system("PAUSE");
	}

	Renderer::Kill();
	ResourceManager::Kill();
	Settings::Kill();
	ILogger::Kill();
	#ifdef _DEBUG
		MemoryManager::Kill();
	#endif

    return 0;
}
Exemplo n.º 8
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
        
    }
}