Difference between revisions of "BGRABitmap tutorial 9"

From Free Pascal wiki
Jump to navigationJump to search
Line 224: Line 224:
  
 
[[Image:BGRATutorial9b.png]]
 
[[Image:BGRATutorial9b.png]]
 +
 +
=== Using Perlin noise and phong shading together ===
 +
 +
The idea is to create a map with a Perlin noise, and then use phong shading to render it. Here is how to create a stone texture :
 +
 +
<delphi>  function CreateStoneTexture(tx,ty: integer): TBGRABitmap;
 +
  var
 +
    temp: TBGRABitmap;
 +
    phong: TPhongShading;
 +
  begin
 +
    result := CreateCyclicPerlinNoiseMap(tx,ty,1,1,0.6);
 +
    temp:= result.GetPart(rect(-2,-2,tx+2,ty+2)) as TBGRABitmap;
 +
 +
    phong := TPhongShading.Create;
 +
    phong.LightSourceDistanceFactor := 0;
 +
    phong.LightDestFactor := 0;
 +
    phong.LightSourceIntensity := 100;
 +
    phong.LightPositionZ := 100;
 +
    phong.NegativeDiffusionFactor := 0.3;
 +
    phong.AmbientFactor := 0.5;
 +
    phong.Draw(result,temp,30,-2,-2,BGRA(170,170,170));
 +
 +
    phong.Free;
 +
    temp.Free;
 +
  end;  </delphi>
 +
 +
First, we create a cyclic map. It's important that it be cyclic in order to make a tilable texture. But then, when we will apply phong shading, we need to make the shader aware of the cycle. So, with GetPart, we extract the generated map with 2 more pixels on each border, so the shader can be applied to the map with the cycle.
 +
 +
The call to phong.Draw with offset (-2,-2) renders the map at the correct location, taking into account that we've added two pixels.
 +
 +
Now in the OnPaint event :
 +
<delphi>procedure TForm1.FormPaint(Sender: TObject);
 +
var
 +
  image: TBGRABitmap;
 +
  stone: TBGRABitmap;
 +
begin
 +
    image := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clBtnFace)));
 +
 +
    stone := CreateStoneTexture(100,100);
 +
    image.FillEllipseAntialias(200,100,150,50,stone);
 +
    stone.free;
 +
 +
    image.Draw(Canvas,0,0,True);
 +
    image.free;
 +
end;</delphi>
 +
 +
=== Run the program ===
 +
 +
You should see a form with a stoned background.
 +
 +
[[Image:BGRATutorial9c.png]]

Revision as of 21:05, 2 April 2011

This tutorial shows how to use phong shading to make textures.

Create a new project

Create a new project and add a reference to BGRABitmap, the same way as in the first tutorial.

Phong shading and light

To use phong shading, you need to instanciate a TPhongShading class. It's located in BGRAGradients unit.

Let's add a variable in the form definition : <delphi>TForm1 = class(TForm)

 ...
 phong: TPhongShading;     </delphi>

When the form is created, we can create the class : <delphi>procedure TForm1.FormCreate(Sender: TObject); begin

 phong := TPhongShading.Create;
 phong.LightPositionZ := 150;
 phong.SpecularIndex := 20;
 phong.AmbientFactor := 0.4;
 phong.LightSourceIntensity := 250;
 phong.LightSourceDistanceTerm := 200;  

end; </delphi>

The specular index indicates how concentrated the reflected light is.

When the form is destroyed : <delphi>procedure TForm1.FormDestroy(Sender: TObject); begin

 phong.Free;

end; </delphi>

When the form is painted, add some phong shaded object : <delphi>var

 image: TBGRABitmap;

begin

   image := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clBtnFace)));
   phong.DrawSphere(image,rect(20,20,120,120),50,BGRA(255,0,0));
   image.Draw(Canvas,0,0,True);
   image.free;

end; </delphi>

The parameter of DrawSphere are the destination image, the bounds of the object, the maximum altitude and the color. The diameter of the sphere is 100 so the maximum altitude of a hemisphere is 50.

Finally when the mouse is moved, it would be nice that the light source followed : <delphi>procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,

 Y: Integer);

begin

 phong.LightPosition := point(X,Y);
 FormPaint(Sender);

end; </delphi>

Run the program

You should be able to play with the light on the sphere :

BGRATutorial9a.png

Use phong shading to create textures

The following procedure creates a piece of chocolate : <delphi>function CreateChocolateTexture(tx,ty: integer): TBGRABitmap; var

 square,map: TBGRABitmap;
 phong: TPhongShading;
 margin: integer;

begin

 margin := tx div 20; //empty space around the square
 square := CreateRectangleMap(tx-2*margin,ty-2*margin,tx div 8);
 //create a map with the square at the middle
 map := TBGRABitmap.Create(tx,ty,BGRABlack);
 map.PutImage(margin,margin,square,dmDrawWithTransparency);
 //apply blur to make it smoother
 BGRAReplace(map,map.FilterBlurRadial(tx div 40,rbFast));
 square.free;
 //create resulting bitmap
 result := TBGRABitmap.Create(tx,ty);
 //use phong shading
 phong := TPhongShading.Create;
 phong.LightSourceDistanceFactor := 0;
 phong.LightDestFactor := 0;
 phong.LightSourceIntensity := 200;
 phong.AmbientFactor := 0.5;
 phong.LightPosition := Point(-50,-100);
 phong.LightPositionZ := 80;
 //draw the piece of chocolate with max altitude 20
 phong.Draw(result,map,20,0,0,BGRA(86,41,38));
 map.Free;
 phong.Free;

end; </delphi>

The phong shader uses a map of altitudes to render the light effects. Here, the map contains a square.

Among properties of the phong shader, there is LightSourceDistanceFactor and LightDestFactor. Setting these values to zero make the result tilable. Indeed, when the distance factor is zero, the distance between the light and the object is not taken into account, and when the light destination factor is zero, the position of the object is not taken into account when computing the angle of the light.

Now, when the form is created, create the chocolate texture : <delphi> chocolate := CreateChocolateTexture(80,80); </delphi>

And when the form is destroyed : <delphi> chocolate.Free; </delphi>

Before phong.DrawSphere in the OnPaint event, add this line : <delphi> image.FillRect(0,0,80*7,80*4,chocolate,dmSet); </delphi>

Resulting code

<delphi>unit UMain;

{$mode objfpc}{$H+}

interface

uses

 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
 ExtCtrls, Buttons, BGRABitmap, BGRABitmapTypes, BGRAGradients;

type

 { TForm1 }
 TForm1 = class(TForm)
   Timer1: TTimer;
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
   procedure FormPaint(Sender: TObject);
 private
   { private declarations }
 public
   { public declarations }
   phong: TPhongShading;
   chocolate: TBGRABitmap;
 end; 

var

 Form1: TForm1; 

implementation

function CreateChocolateTexture(tx,ty: integer): TBGRABitmap; var

 square,map: TBGRABitmap;
 phong: TPhongShading;
 margin: integer;

begin

 margin := tx div 20;
 square := CreateRectangleMap(tx-2*margin,ty-2*margin,tx div 8);
 map := TBGRABitmap.Create(tx,ty,BGRABlack);
 map.PutImage(margin,margin,square,dmDrawWithTransparency);
 BGRAReplace(map,map.FilterBlurRadial(tx div 40,rbFast));
 square.free;
 result := TBGRABitmap.Create(tx,ty);
 phong := TPhongShading.Create;
 phong.LightSourceDistanceFactor := 0;
 phong.LightDestFactor := 0;
 phong.LightSourceIntensity := 200;
 phong.AmbientFactor := 0.5;
 phong.LightPosition := Point(-50,-100);
 phong.LightPositionZ := 80;
 phong.Draw(result,map,20,0,0,BGRA(86,41,38));
 map.Free;
 phong.Free;

end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject); begin

 phong := TPhongShading.Create;
 phong.LightPositionZ := 150;
 phong.SpecularIndex := 20;
 phong.AmbientFactor := 0.4;
 phong.LightSourceIntensity := 250;
 phong.LightSourceDistanceTerm := 200;
 chocolate := CreateChocolateTexture(80,80);

end;

procedure TForm1.FormDestroy(Sender: TObject); begin

 phong.Free;
 chocolate.Free;

end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,

 Y: Integer);

begin

 phong.LightPosition := point(X,Y);
 FormPaint(Sender);

end;


procedure TForm1.FormPaint(Sender: TObject); var

 image: TBGRABitmap;

begin

   image := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clBtnFace)));
   image.FillRect(0,0,80*7,80*4,chocolate,dmSet);
   phong.DrawSphere(image,rect(20,20,120,120),50,BGRA(255,0,0));
   image.Draw(Canvas,0,0,True);
   image.free;

end;

initialization

 {$I UMain.lrs}

end. </delphi>

Run the program

You should see a nice bar of chocolate with a big cherry :

BGRATutorial9b.png

Using Perlin noise and phong shading together

The idea is to create a map with a Perlin noise, and then use phong shading to render it. Here is how to create a stone texture :

<delphi> function CreateStoneTexture(tx,ty: integer): TBGRABitmap;

 var
   temp: TBGRABitmap;
   phong: TPhongShading;
 begin
   result := CreateCyclicPerlinNoiseMap(tx,ty,1,1,0.6);
   temp:= result.GetPart(rect(-2,-2,tx+2,ty+2)) as TBGRABitmap;
   phong := TPhongShading.Create;
   phong.LightSourceDistanceFactor := 0;
   phong.LightDestFactor := 0;
   phong.LightSourceIntensity := 100;
   phong.LightPositionZ := 100;
   phong.NegativeDiffusionFactor := 0.3;
   phong.AmbientFactor := 0.5;
   phong.Draw(result,temp,30,-2,-2,BGRA(170,170,170));
   phong.Free;
   temp.Free;
 end;   </delphi>

First, we create a cyclic map. It's important that it be cyclic in order to make a tilable texture. But then, when we will apply phong shading, we need to make the shader aware of the cycle. So, with GetPart, we extract the generated map with 2 more pixels on each border, so the shader can be applied to the map with the cycle.

The call to phong.Draw with offset (-2,-2) renders the map at the correct location, taking into account that we've added two pixels.

Now in the OnPaint event : <delphi>procedure TForm1.FormPaint(Sender: TObject); var

 image: TBGRABitmap;
 stone: TBGRABitmap;

begin

   image := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clBtnFace)));
   stone := CreateStoneTexture(100,100);
   image.FillEllipseAntialias(200,100,150,50,stone);
   stone.free;
   image.Draw(Canvas,0,0,True);
   image.free;

end;</delphi>

Run the program

You should see a form with a stoned background.

BGRATutorial9c.png