Jump to content
  • Advertisement
Sign in to follow this  
DemonLord

HLSL lights problem

This topic is 2260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi.

I have a problem with adding lights to my game (XNA 4.0).
I've made an underwater effect (slight waving of the screen) but I can't get a spot light atop of my submarine to work.
My goal is to attach a spot light in front of my submarine and darken the surroundings, to get more deep ocean feeling.
I suspect I'm doing something wrong with RenderTarget2D, but don't know what (I'm not 100% sure how RenderTarget2D works)

[media]
[/media]

Here's my Draw method and some other lights methods:


public override void Draw(GameTime gameTime)
{
ScreenManager.GraphicsDevice.Clear(Color.Transparent);
// Set the render targets
ScreenManager.GraphicsDevice.SetRenderTarget(colorMapRenderTarget);

// Clear all render targets
ScreenManager.GraphicsDevice.Clear(Color.Transparent);
ScreenManager.GraphicsDevice.SetRenderTarget(null);
ScreenManager.GraphicsDevice.SetRenderTarget(normalMapRenderTarget);

// Clear all render targets
ScreenManager.GraphicsDevice.Clear(Color.Transparent);
DrawNormalMap();
// Deactive the rander targets to resolve them
ScreenManager.GraphicsDevice.SetRenderTarget(null);
GenerateShadowMap();
ScreenManager.GraphicsDevice.Clear(Color.Transparent);
// Finally draw the combined Maps onto the screen
DrawCombinedMaps();
/* Clear the screen */
ScreenManager.GraphicsDevice.SetRenderTarget(renderTarget);
ScreenManager.GraphicsDevice.Clear(Color.Transparent);

//Draw background
DrawColorMap();
/*********************
* Drawing all nodes *
*********************/
//Drawing players
spriteBatch.Begin();
foreach (Node node in Node.Nodes)
node.Draw(spriteBatch);

for (int i = 0; i < Player.Players.Length; i++)
{
PlayerShip ship = Player.Players[0].Ship;
string text;
if (ship != null)
text = String.Format("Shield power: {1}", 0 + 1, ship.Health);
else
text = String.Format("P{0} <>", i + 1);
spriteBatch.DrawString(font, text, new Vector2(10, 0 * 20 + 10), Color.White);
}
spriteBatch.End();
/* end drawing nodes */

/*********************
* PostProcess.fx *
* HLSL water effect *
*********************/
ScreenManager.GraphicsDevice.SetRenderTarget(null);
SceneTexture = renderTarget;
ScreenManager.GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
{
effectPost.CurrentTechnique.Passes[0].Apply();
{
effectPost.Parameters["fTimer"].SetValue(m_Timer);
spriteBatch.Draw(SceneTexture, new Rectangle(0, 0, gfx.PreferredBackBufferWidth, gfx.PreferredBackBufferHeight), Color.White);
}
}
spriteBatch.End();
/* end HLSL water effect */

// If the game is transitioning on or off, fade it out to black.
if (TransitionPosition > 0 || pauseAlpha > 0)
{
float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
ScreenManager.FadeBackBufferToBlack(alpha);
}
}


#region Light Methods
/*****************************
* Combined Maps Draw method *
*****************************/
private void DrawCombinedMaps()
{
lightCombinedEffect.CurrentTechnique = lightCombinedEffectTechnique;
lightCombinedEffectParamAmbient.SetValue(1f);
lightCombinedEffectParamLightAmbient.SetValue(4);
lightCombinedEffectParamAmbientColor.SetValue(ambientLight.ToVector4());
lightCombinedEffectParamColorMap.SetValue(colorMapRenderTarget);
lightCombinedEffectParamShadowMap.SetValue(shadowMapRenderTarget);
lightCombinedEffectParamNormalMap.SetValue(normalMapRenderTarget);
lightCombinedEffect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, lightCombinedEffect);
spriteBatch.Draw(colorMapRenderTarget, Vector2.Zero, Color.White);
spriteBatch.End();
}
/* end Combined Maps Draw method */
/***************************
* Normals Map Draw method *
***************************/
private void DrawNormalMap()
{
spriteBatch.Begin();
spriteBatch.Draw(bgNormals, Vector2.Zero, Color.White);
spriteBatch.End();
}
/* end Normals Map Draw method */
/**********************************************
* Color Map Draw method - regular background *
**********************************************/
private void DrawColorMap()
{
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
spriteBatch.Draw(bg, Vector2.Zero, new Rectangle(0, (int)(-scrollY), bg.Width, bg.Height), Color.White);
spriteBatch.End();
}
/* end Color Map Draw method */
/********************************
* Shadow Map Generation method *
********************************/

private Texture2D GenerateShadowMap()
{
ScreenManager.GraphicsDevice.SetRenderTarget(shadowMapRenderTarget);
ScreenManager.GraphicsDevice.Clear(Color.Transparent);
foreach (var light in lights)
{
if (!light.IsEnabled) continue;
// Draw all the light sources
lightEffectParameterStrength.SetValue(light.ActualPower);
lightEffectParameterPosition.SetValue(light.Position);
lightEffectParameterLightColor.SetValue(light.Color);
lightEffectParameterLightDecay.SetValue(light.LightDecay); // Value between 0.00 and 2.00
lightEffect.Parameters["specularStrength"].SetValue(specularStrength);
if (light.LightType == LightType.Point)
{
lightEffect.CurrentTechnique = lightEffectTechniquePointLight;
}
else
{
lightEffect.CurrentTechnique = lightEffectTechniqueSpotLight;
lightEffectParameterConeAngle.SetValue(((SpotLight)light).SpotAngle);
lightEffectParameterConeDecay.SetValue(((SpotLight)light).SpotDecayExponent);
lightEffectParameterConeDirection.SetValue(((SpotLight)light).Direction);
}
lightEffectParameterScreenWidth.SetValue(ScreenManager.GraphicsDevice.Viewport.Width);
lightEffectParameterScreenHeight.SetValue(ScreenManager.GraphicsDevice.Viewport.Height);
lightEffect.Parameters["ambientColor"].SetValue(ambientLight.ToVector4());
lightEffectParameterNormapMap.SetValue(normalMapRenderTarget);
lightEffect.Parameters["ColorMap"].SetValue(colorMapRenderTarget);
lightEffect.CurrentTechnique.Passes[0].Apply();
// Add Belding (Black background)
ScreenManager.GraphicsDevice.BlendState = BlendBlack;
}
// Deactive the rander targets to resolve them
ScreenManager.GraphicsDevice.SetRenderTarget(null);
return shadowMapRenderTarget;
}
/* end Shadow Map Generation method */
public static BlendState BlendBlack = new BlendState()
{
ColorBlendFunction = BlendFunction.Add,
ColorSourceBlend = Blend.One,
ColorDestinationBlend = Blend.One,
AlphaBlendFunction = BlendFunction.Add,
AlphaSourceBlend = Blend.SourceAlpha,
AlphaDestinationBlend = Blend.One
};
#endregion


Am I missing something or am I doing this completely wrong?
I'd be thankful if anyone could point me in the right direction.
Thanx.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!