Pixel shader tutorial part 2 - textures

This is the second part of the pixel shader tutorial. In the first part, we simply drew green pixels using a simple pixel shader. A pixel shader is nothing more than a short program executed on the GPU for every pixel, to calculate the color of every pixel. To make the last example a bit more interesting, we will now draw a texture instead of the green color. And because this is quite boring too, we will change the color of the texture a bit: Just make it a little bit brighter. The code for the second pixel shader looks like this, just replace the string in the beginning of the first program:
"sampler2D tex0;                                      \
float4 pixelShader( float2 texCoord : TEXCOORD0, \
float4 color : COLOR0 ) : COLOR0 \
{ \
float4 c = tex2D( tex0, texCoord ); \
c = c * float4(2,2,2,2); \
return c; \
Right, this code is executed for every pixel on the screen. The first line gets the color of the texture tex0 using the tex2D function. The second line multiplies the color by 2, to make it brighter. Simple. If you use a texture like this:

The result of the program will look like this:

Yep, it's better than the green, boring cube. But not much. We'll have to improve this situation in the next part. :)

three comments, already:

this isnt new niko its the same method used for the normal and displacment maps materials shaders in the cpp code
omaremad - 29 10 05 - 01:12

opps i hought you meant it was a new feature
omaremad - 29 10 05 - 01:16

isnt it more interesting to read a pixel color and change it slightly to obtain some fancy effect?

this is pretty cool btw, i had no idea how shaders work!
Armen138 - 29 10 05 - 12:29

Displayed name:  
Remember personal info?
Website (optional):
Enter "layered" (antispam):
Comment:Emoticons / Textile


Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Note: If you type in your email adress above, it will be visible to other visitors, although it will be hidden for bots using javaScript.