public void AddSceneObject(SceneObject sceneObject) { if (sceneObject is Light) { SceneLights.AddSceneLight(sceneObject as Light); } if (!sceneObjects.ContainsKey(sceneObject.Name)) { sceneObjects.Add(sceneObject.Name, sceneObject); } }
public void Render(float SceneWidth, float SceneHeight) { if (!TimeManager.Instance.IsStarted) { TimeManager.Instance.Start(); } TotalTime = TimeManager.Instance.GetElapsedSeconds(); //Delta time calculation - refactor CurrentScene.deltaTime = TotalTime - lastFrameTime; lastFrameTime = TotalTime; gridFloor.Render(CurrentScene.ActiveCamera.GetViewMatrix(), CurrentScene.ActiveCamera.GetProjectionMatrix(SceneWidth, SceneHeight)); foreach (Renderable renderObj in GetAllRenderables()) { Light simpleLight = null; if (SceneLights.PointLightsCount > 0) { simpleLight = SceneLights.GetLightsByType(Light.eLightType.Point)[0]; } simpleLight.Position.X = 2.0f * (float)Math.Sin(TimeManager.Instance.GetElapsedSeconds() / 2); simpleLight.Position.Z = 2.0f * (float)Math.Cos(TimeManager.Instance.GetElapsedSeconds() / 2); Light simpleLight2 = null; if (SceneLights.PointLightsCount > 1) { simpleLight2 = SceneLights.GetLightsByType(Light.eLightType.Point)[1]; } simpleLight2.Position.X = 2.0f * (float)Math.Sin(-TimeManager.Instance.GetElapsedSeconds()); simpleLight2.Position.Z = 2.0f * (float)Math.Cos(-TimeManager.Instance.GetElapsedSeconds()); renderObj.Render(CurrentScene.ActiveCamera.GetViewMatrix(), CurrentScene.ActiveCamera.GetProjectionMatrix(SceneWidth, SceneHeight)); renderObj?.ShaderProgram?.SetVector(Constants.ShaderConstants.SHADER_PARAM_LIT_CAMERA_POSITION, Scene.CurrentScene.ActiveCamera.Position); renderObj?.ShaderProgram?.SetVector(Constants.ShaderConstants.SHADER_PARAM_LIT_AMBIENT_COLOR, SceneLights.Ambient.LightColor, false); renderObj?.ShaderProgram?.SetFloat(Constants.ShaderConstants.SHADER_PARAM_LIT_AMBIENT_INTENSITY, SceneLights.Ambient.LightIntensity); if (simpleLight != null) { renderObj?.ShaderProgram?.SetVector(Constants.ShaderConstants.SHADER_PARAM_LIT_SIMPLELIGHT_COLOR, simpleLight.LightColor, false); renderObj?.ShaderProgram?.SetFloat(Constants.ShaderConstants.SHADER_PARAM_LIT_SIMPLELIGHT_INTENSITY, simpleLight.LightIntensity); renderObj?.ShaderProgram?.SetVector(Constants.ShaderConstants.SHADER_PARAM_LIT_SIMPLELIGHT_POSITION, simpleLight.Position); } renderObj?.ShaderProgram?.SetVector("material.ambient", 1.0f, 0.5f, 0.31f); renderObj?.ShaderProgram?.SetVector("material.diffuse", 1.0f, 0.5f, 0.31f); renderObj?.ShaderProgram?.SetVector("material.specular", 0.5f, 0.5f, 0.5f); renderObj?.ShaderProgram?.SetFloat("material.shininess", 32.0f); int lightIndex = 0; foreach (PointLight pointLight in SceneLights.GetLightsByType(Light.eLightType.Point)) { renderObj?.ShaderProgram?.SetVector(string.Format("PointLights[{0}].position", lightIndex), pointLight.Position); renderObj?.ShaderProgram?.SetVector(string.Format("PointLights[{0}].diffuse", lightIndex), pointLight.LightColor, false); renderObj?.ShaderProgram?.SetVector(string.Format("PointLights[{0}].specular", lightIndex), pointLight.LightColor, false); renderObj?.ShaderProgram?.SetFloat(string.Format("PointLights[{0}].intensity", lightIndex), pointLight.LightIntensity); renderObj?.ShaderProgram?.SetFloat(string.Format("PointLights[{0}].constant", lightIndex), pointLight.Constant); renderObj?.ShaderProgram?.SetFloat(string.Format("PointLights[{0}].linear", lightIndex), pointLight.Linear); renderObj?.ShaderProgram?.SetFloat(string.Format("PointLights[{0}].quadratic", lightIndex), pointLight.Quadratic); lightIndex++; } lightIndex = 0; foreach (DirectionalLight directionalLight in SceneLights.GetLightsByType(Light.eLightType.Directional)) { renderObj?.ShaderProgram?.SetVector(string.Format("DirLights[{0}].direction", lightIndex), directionalLight.Direction); renderObj?.ShaderProgram?.SetVector(string.Format("DirLights[{0}].diffuse", lightIndex), directionalLight.LightColor, false); renderObj?.ShaderProgram?.SetVector(string.Format("DirLights[{0}].specular", lightIndex), directionalLight.LightColor, false); renderObj?.ShaderProgram?.SetFloat(string.Format("DirLights[{0}].intensity", lightIndex), directionalLight.LightIntensity); lightIndex++; } lightIndex = 0; foreach (SpotLight spotLight in SceneLights.GetLightsByType(Light.eLightType.Spot)) { renderObj?.ShaderProgram?.SetVector(string.Format("SpotLights[{0}].position", lightIndex), spotLight.Position); renderObj?.ShaderProgram?.SetVector(string.Format("SpotLights[{0}].direction", lightIndex), spotLight.Direction); renderObj?.ShaderProgram?.SetFloat(string.Format("SpotLights[{0}].cutoff", lightIndex), (float)Math.Cos(XNA.MathHelper.ToRadians(spotLight.InnerAngle))); renderObj?.ShaderProgram?.SetFloat(string.Format("SpotLights[{0}].outerCutoff", lightIndex), (float)Math.Cos(XNA.MathHelper.ToRadians(spotLight.OuterAngle))); renderObj?.ShaderProgram?.SetVector(string.Format("SpotLights[{0}].diffuse", lightIndex), spotLight.LightColor, false); renderObj?.ShaderProgram?.SetVector(string.Format("SpotLights[{0}].specular", lightIndex), spotLight.LightColor, false); renderObj?.ShaderProgram?.SetFloat(string.Format("SpotLights[{0}].intensity", lightIndex), spotLight.LightIntensity); lightIndex++; } } }
public Scene() { sceneObjects = new Dictionary <string, SceneObject>(); gridFloor = new Grid(); SceneLights = new SceneLights(); }