unit skybox;

interface

uses
  System.Drawing,
  System.Windows.Forms,
  Microsoft.DirectX,
  Microsoft.DirectX.DirectInput,
  Microsoft.DirectX.Direct3D;

type

  TEventHandler      = System.EventHandler;
  TEventArgs         = System.EventArgs;
  TKeyEventArgs      = System.Windows.Forms.KeyEventArgs;
  TMouseEventArgs    = System.Windows.Forms.MouseEventArgs;
  TPaintEventArgs    = System.Windows.Forms.PaintEventArgs;
  TKeys              = System.Windows.Forms.Keys;
  TForm              = System.Windows.Forms.Form;
  TColor             = System.Drawing.Color;
  TIcon              = System.Drawing.Icon;

  TSkybox = class

    public
      FVertexBuffer : VertexBuffer;
      Ftexture: texture;
      constructor create;
      procedure OnCreateVertexBuffer(sender: TObject; e: TEventArgs);
      procedure OnCreateDevice(sender: TObject; e: TEventArgs);
      procedure OnResetDevice(sender: TObject; e: TEventArgs);
  end;

implementation

constructor TSkybox.create;
begin
   inherited;
end;


procedure TSkybox.OnCreateVertexBuffer(sender: System.Object; e: EventArgs);
var
  verts: array[0..35]  of CustomVertex.Positiontextured;
  stm: GraphicsStream;
begin
  Self.FVertexBuffer := VertexBuffer( sender ) ;
  stm := Self.FVertexBuffer.Lock( 0, 0, LockFlags( 0 ) );

      // 1ere face de la boîte (face avant) -------------------------------
      // 1er triangle

      verts[0] := CustomVertex.Positiontextured.Create(-200, 400, 400, 0, 1);
			verts[1] := CustomVertex.Positiontextured.Create(400, 400, 400, 1, 1);
			verts[2] := CustomVertex.Positiontextured.Create(-200, -200, 400, 0, 0);

			// 2eme triangle
			verts[3] := verts[2];
			verts[4] := verts[1];
			verts[5] := CustomVertex.Positiontextured.Create(400, -200, 400, 1, 0);

			// 2eme face de la boîte (face droite) ---------------------------
			// 1er triangle
			verts[6] := CustomVertex.Positiontextured.Create(400, 400, 400, 0, 1);
			verts[7] := CustomVertex.Positiontextured.Create(400, 400, -200, 1, 1);
			verts[8] := CustomVertex.Positiontextured.Create(400, -200, 400, 0, 0); ;

			// 2eme triangle
			verts[9] := verts[8];
			verts[10] := verts[7];
			verts[11] := CustomVertex.Positiontextured.Create(400, -200, -200, 1, 0);

			// 3eme face de la boîte (face derrière) -------------------------
			// 1er triangle
			verts[12] := CustomVertex.Positiontextured.Create(400, 400, -200, 0, 1);
			verts[13] := CustomVertex.Positiontextured.Create(-200, 400, -200, 1, 1);
			verts[14] := CustomVertex.Positiontextured.Create(400, -200, -200, 0, 0);

			// 2eme triangle
			verts[15] := verts[14];
			verts[16] := verts[13];
			verts[17] := CustomVertex.Positiontextured.Create(-200, -200, -200, 1, 0);

			// 4eme face de la boîte (face gauche) ---------------------------
			// 1er triangle
			verts[18] := CustomVertex.Positiontextured.Create(-200, 400, -200, 0, 1);
			verts[19] := CustomVertex.Positiontextured.Create(-200, 400, 400, 1, 1);
			verts[20] := CustomVertex.Positiontextured.Create(-200, -200, -200, 0, 0);

			// 2eme triangle
			verts[21] := verts[20];
			verts[22] := verts[19];
			verts[23] := CustomVertex.Positiontextured.Create(-200, -200, 400, 1, 0);

			// 5eme face de la boîte (dessus) -------------------------------
			// 1er triangle
			verts[24] := CustomVertex.Positiontextured.Create(-200, 400, -200, 0, 1);
			verts[25] := CustomVertex.Positiontextured.Create(400, 400, -200, 1, 1);
			verts[26] := CustomVertex.Positiontextured.Create(-200, 400, 400, 0, 0);

			// 2eme triangle
			verts[27] := verts[26];
			verts[28] := verts[25];
			verts[29] := CustomVertex.Positiontextured.Create(400, 400, 400, 1, 0);

			// 6eme face de la boîte (dessous) -------------------------------
			// 1er triangle
			verts[30] := CustomVertex.Positiontextured.Create(-200, -200, -200, 0, 1);
			verts[31] := CustomVertex.Positiontextured.Create(400, -200, -200, 1, 1);
			verts[32] := CustomVertex.Positiontextured.Create(-200, -200, 400, 0, 0);

			// 2eme triangle
			verts[33] := verts[32];
			verts[34] := verts[31];
			verts[35] := CustomVertex.Positiontextured.Create(400, -200, 400, 1, 0);

    // 6eme face

  stm.Write(verts);
  Self.FVertexBuffer.Unlock;
end;

procedure TSkybox.OnCreateDevice(sender: System.Object; e: EventArgs);
var
  dev: Device;
begin
  dev := Device( sender );
  // Now Create the VB
  Self.FVertexBuffer := VertexBuffer.Create( TypeOf( CustomVertex.Positiontextured ), 36, dev, Usage( 0 ), CustomVertex.PositionColored.Format, Pool.Default );
  //Include( Self.FVertexBuffer.Created, TPerso.OnCreateVertexBuffer );
  Self.OnCreateVertexBuffer( Self.FVertexBuffer, nil );
end;

procedure TSkybox.OnResetDevice(sender: TObject; e: TEventArgs);
var
  dev: Device;
begin
  dev := Device( sender );
  // Turn off culling, so we see the front and back of the triangle
  //dev.RenderState.CullMode := Cull.None;
  // Turn off D3D lighting, since we are providing our own vertex colors
  dev.RenderState.Lighting := False;

  FTexture:=TextureLoader.FromFile( dev, 'texture/skybox/' + 'oupas.bmp' );
end;

end.
