Posted by: EvilNando | November 20, 2008

Shader programming: Brightness

ShaderTitle

I’ve been working on the AnimationEditor for quite a while.

Right now Im working on the rendering of the frames , Im trying to modify each frame according to the actual frame parameters and not just texture coordinates like I have been doing in the past.

I’ve managed to finish all of that work now, but the feature that took me by surprise was the implementation of the brightness parameter.

Brightness is no mistery by itself as it is only a scalar multiplication of the RGB components of each pixel, my surprise was when I found out that for doing this I needed to implement shaders in my program ! :O

All of my previous experience in graphics programming comes from the DX8 and DirectDraw era where fixed functionality was standard, so honestly speaking I was kinda intimdated by jumping to the flexible pipeline and start coding shader functions.

It took me a couple of hours to figure out the example on MSDN because I never found out a place that explained that the shader function (the pixel shader part) is actually applied to every single pixel that you render to a draw surface. I was expecting to see at least a for() or do-while() structure but all of this looping is doing internally so figuring that out took me a little while.

Here is the shader code I managed to write, its very simple nothing spectacular but it was great for me to get my feet wet into the world of shader programming.

//--------------------------------------------------------------------------
// Copyright © 2008 Nando Soft.
//--------------------------------------------------------------------------
//
// MODULE NAME   : Brightness.fx
//
// DESCRIPTION   : Shader effect for increasing sprite's brightness
//
//      AUTHOR   : Armando Alva
//
// CREATION DATE : 20-NOV-2008
//
// REVISION HISTORY
//
// Date Author Description
// ----------- ------ ------------------------------------------------------
// 20-NOV-2008 AAV    Initial Draft.
//
//--------------------------------------------------------------------------

uniform extern texture ScreenTexture;	

sampler ScreenS = sampler_state
{
	Texture = <ScreenTexture>;	
};

float brightness = 0;

float4 PixelShader(float2 texCoord: TEXCOORD0) : COLOR
{
	float4 color = tex2D(ScreenS, texCoord);	

	color.rgb *= brightness;

	return color;
}
technique
{
	pass P0
	{
		PixelShader = compile ps_2_0 PixelShader();
	}
}

A quick reminder: brightness in HLSL goes from 0 (black) to 1(normal)

So to have a double lit sprite its brightness value should be 2 and so on. In the editor I used percentage values to avoid dealing with floating point values, you could have a more fine control on the brightness using float values but I think it is way more practical to just work this way. Oh also I’ve implemented a hard limit of 300% for the brightness value (this means 3 times its normal color value) I think its enough for common use, but again if someone out there for any reason needs more I can alway lift that hardlimit of 300%.

Here are a couple of screenshots showing a single sprite affected by different brightness values , enjoy and see you all next time!

bri 50

bri 100

bri 207

bri 300

Advertisements

Responses

  1. Hi Armando, how would you call that shader to effect a sprite?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: