//DJXPrimitives v1.0
unit DJXPrimitives;

interface

uses
  Windows,
  Classes,
  DJXClasses,
  DJXTextures,
  Direct3D9,
  D3DX9;

type
  TDJXPrimitives2D = class
  private
    m_DanJetX: Pointer;
    m_VBF: TDJXVBFlex;
    m_VB: TDJXVB2D;
    m_Filled: boolean;
  public
    property DanJetX: Pointer read m_DanJetX write m_DanJetX;
    property Filled: boolean read m_Filled write m_Filled;
    property VB: TDJXVB2D read m_VB;
    property VBF: TDJXVBFlex read m_VBF;
    procedure Line(x1,y1,x2,y2:single; col: TDJXColor);
    procedure Line2col(x1,y1,x2,y2:single; col1, col2: TDJXColor);
    procedure Rectangle(
      x,y,width,height: single;
      col: TDJXColor
    );
    procedure RectangleMT(
      x,y,width,height: single;
      col: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure Rectangle4col(
      x,y,width,height: single;
      col1,col2,col3,col4: TDJXColor
    );
    procedure Rectangle4colMT(
      x,y,width,height: single;
      col1,col2,col3,col4: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure RectangleRaw(
      X, Y, Width, Height: single;
      Col1, Col2, Col3, Col4: TDJXColor;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure Triangle(x1,y1,x2,y2,x3,y3: single; col: TDJXColor);
    procedure Triangle3col(x1,y1,x2,y2,x3,y3: single; col1,col2,col3: TDJXColor);
    procedure Quadrangle(
      x1,y1,x2,y2,x3,y3,x4,y4: single;
      col: TDJXColor;
      Texture: TDJXTexture = nil;
      tu1: single = 0; tv1: single = 0;
      tu2: single = 1; tv2: single = 0;
      tu3: single = 1; tv3: single = 1;
      tu4: single = 0; tv4: single = 1
    );
    procedure QuadrangleMT(
      x1,y1,x2,y2,x3,y3,x4,y4: single;
      col: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure Quadrangle4col(
      x1,y1,x2,y2,x3,y3,x4,y4: single;
      col1,col2,col3,col4: TDJXColor;
      Texture: TDJXTexture = nil;
      tu1: single = 0; tv1: single = 0;
      tu2: single = 1; tv2: single = 0;
      tu3: single = 1; tv3: single = 1;
      tu4: single = 0; tv4: single = 1
    );
    procedure Quadrangle4colMT(
      x1,y1,x2,y2,x3,y3,x4,y4: single;
      col1,col2,col3,col4: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure QuadrangleRaw(
      x1, y1, x2, y2, x3, y3, x4, y4: single;
      Col1, Col2, Col3, Col4: TDJXColor;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure Circle(cX,cY,radius: single; VertexNumber: integer; Col: TDJXColor);
    procedure Circle2col(cX,cY,radius: single; VertexNumber: integer; IntCol,ExtCol: TDJXColor);
    procedure Polygon(Points: array of TDJXVertex2D);
    procedure Init;
    procedure UnInit;
  end;

  TDJXPrimitives3D = class
  private
    m_DanJetX: Pointer;
    m_VBF: TDJXVBFlex;
    m_VB: TDJXVB3D;
    m_Filled: boolean;
  public
    property DanJetX: Pointer read m_DanJetX write m_DanJetX;
    property Filled: boolean read m_Filled write m_Filled;
    procedure Line(x1,y1,z1,x2,y2,z2:single; col: TDJXColor);
    procedure Line2col(x1,y1,z1,x2,y2,z2:single; col1,col2: TDJXColor);
    procedure Triangle(x1,y1,z1,x2,y2,z2,x3,y3,z3: single; col: TDJXColor);
    procedure Triangle3col(x1,y1,z1,x2,y2,z2,x3,y3,z3: single; col1,col2,col3: TDJXColor);
    procedure Quadrangle(
      x1,y1,z1,
      x2,y2,z2,
      x3,y3,z3,
      x4,y4,z4: single;
      col: TDJXColor;
      Texture: TDJXTexture = nil;
      ImageIndex: integer = 0;
      TileWidth: integer = 0;
      TileHeight: integer = 0
    );
    procedure QuadrangleMT(
      x1,y1,z1,
      x2,y2,z2,
      x3,y3,z3,
      x4,y4,z4: single;
      col: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure Quadrangle4Col(
      x1,y1,z1,
      x2,y2,z2,
      x3,y3,z3,
      x4,y4,z4: single;
      col1,col2,col3,col4: TDJXColor;
      Texture: TDJXTexture = nil;
      ImageIndex: integer = 0;
      TileWidth: integer = 0;
      TileHeight: integer = 0
    );
    procedure Quadrangle4ColMT(
      x1,y1,z1,
      x2,y2,z2,
      x3,y3,z3,
      x4,y4,z4: single;
      col1,col2,col3,col4: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure Cube(
      cX,cY,cZ,
      Size: single;
      col: TDJXColor
    );
    procedure CubeTex(
      cX,cY,cZ,
      Size: single;
      col: TDJXColor;
      Texture: TDJXTexture
    );
    procedure Box(
      X, Y, Z: single;
      SizeX, SizeY, SizeZ: single;
      col: TDJXColor
    );
    procedure BoxTex(
      X, Y, Z: single;
      SizeX, SizeY, SizeZ: single;
      col: TDJXColor;
      Texture: TDJXTexture
    );
    procedure Sphere(
      cX, cY, cZ, Radius: single;
      VSegments, HSegments: integer;
      Col: TDJXColor
    );
    procedure SphereTex(
      cX,cY,cZ,Radius: single;
      VSegments,HSegments: integer;
      Col: TDJXColor;
      Texture: TDJXTexture
    );
    procedure SphereMT(
      cX,cY,cZ,Radius: single;
      VSegments,HSegments: integer;
      Col: TDJXColor;
      Texture0: TDJXTexture = nil;
      Texture1: TDJXTexture = nil;
      Texture2: TDJXTexture = nil;
      Texture3: TDJXTexture = nil
    );
    procedure Capsule(
      x, y, z: single;
      Length: single;
      Radius: single;
      VSegments, HSegments: integer;
      Col: TDJXColor
    );
    procedure BillboardMT(
      x,y,z,
      x1,y1,
      x2,y2,
      x3,y3,
      x4,y4: single;
      col1,col2,
      col3,col4: TDJXColor;
      Rotation,ScaleX,ScaleY: single;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
    procedure Strip(
      VectorArray: PVector4Array;
      Width1: single; Width2: single;
      Texture: TDJXTexture = nil;
      tu0: single = 0; tv0: single = 0;
      tu1: single = 1; tv1: single = 0;
      tu2: single = 1; tv2: single = 1;
      tu3: single = 0; tv3: single = 1
    );
    procedure StripCol(
      VectorArray: PColorVector3Array;
      Width1: single; Width2: single;
      Texture: TDJXTexture = nil;
      tu0: single = 0; tv0: single = 0;
      tu1: single = 1; tv1: single = 0;
      tu2: single = 1; tv2: single = 1;
      tu3: single = 0; tv3: single = 1
    );
    procedure Init;
    procedure UnInit;
  end;

implementation

uses
  DJX, DJXRender, DJXUtils;



//TDJXPrimitives3D BEGIN
procedure TDJXPrimitives3D.Cube(cX,cY,cZ,Size: single; col: TDJXColor);
var
  HSize: single;
  procedure DrawWired;
  begin
    m_VB.Indices[0]:=0;
    m_VB.Indices[1]:=1;
    m_VB.Indices[2]:=1;
    m_VB.Indices[3]:=2;
    m_VB.Indices[4]:=2;
    m_VB.Indices[5]:=3;
    m_VB.Indices[6]:=3;
    m_VB.Indices[7]:=0;

    m_VB.Indices[8]:=4;
    m_VB.Indices[9]:=5;
    m_VB.Indices[10]:=5;
    m_VB.Indices[11]:=6;
    m_VB.Indices[12]:=6;
    m_VB.Indices[13]:=7;
    m_VB.Indices[14]:=7;
    m_VB.Indices[15]:=4;

    m_VB.Indices[16]:=0;
    m_VB.Indices[17]:=4;
    m_VB.Indices[18]:=1;
    m_VB.Indices[19]:=5;
    m_VB.Indices[20]:=2;
    m_VB.Indices[21]:=6;
    m_VB.Indices[22]:=3;
    m_VB.Indices[23]:=7;

    m_VB.setVertices(0,8);
    m_VB.setIndices(0,24);
    m_VB.DrawIndexedLineList(0,12);
  end;
  procedure DrawSolid;
  begin
    //top
    m_VB.Indices[0]:=0;
    m_VB.Indices[1]:=1;
    m_VB.Indices[2]:=2;
    m_VB.Indices[3]:=2;
    m_VB.Indices[4]:=3;
    m_VB.Indices[5]:=0;

    //bottom
    m_VB.Indices[6]:=7;
    m_VB.Indices[7]:=6;
    m_VB.Indices[8]:=5;
    m_VB.Indices[9]:=5;
    m_VB.Indices[10]:=4;
    m_VB.Indices[11]:=7;

    //front
    m_VB.Indices[12]:=3;
    m_VB.Indices[13]:=2;
    m_VB.Indices[14]:=6;
    m_VB.Indices[15]:=6;
    m_VB.Indices[16]:=7;
    m_VB.Indices[17]:=3;

    //back
    m_VB.Indices[18]:=1;
    m_VB.Indices[19]:=0;
    m_VB.Indices[20]:=4;
    m_VB.Indices[21]:=4;
    m_VB.Indices[22]:=5;
    m_VB.Indices[23]:=1;

    //left
    m_VB.Indices[24]:=0;
    m_VB.Indices[25]:=3;
    m_VB.Indices[26]:=7;
    m_VB.Indices[27]:=7;
    m_VB.Indices[28]:=4;
    m_VB.Indices[29]:=0;

    //right
    m_VB.Indices[30]:=2;
    m_VB.Indices[31]:=1;
    m_VB.Indices[32]:=5;
    m_VB.Indices[33]:=5;
    m_VB.Indices[34]:=6;
    m_VB.Indices[35]:=2;

    m_VB.setVertices(0,8);
    m_VB.setIndices(0,36);
    m_VB.DrawIndexedTriangleList(0,12);
  end;
var
  Norm: single;
begin
  Norm:=Sqrt(1/3);
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  HSize:=Size/2;
  //
  //  0 --- 1
  //  |     |
  //  |     |
  //  3 --- 2
  //
  //  4 --- 5
  //  |     |
  //  |     |
  //  7 --- 6
  //
  m_VB.Vertices[0]:=DJXVertex3D(cX-HSize,cY+HSize,cZ+HSize,col,0,0,-Norm,Norm,Norm);
  m_VB.Vertices[1]:=DJXVertex3D(cX+HSize,cY+HSize,cZ+HSize,col,0,0,Norm,Norm,Norm);
  m_VB.Vertices[2]:=DJXVertex3D(cX+HSize,cY+HSize,cZ-HSize,col,0,0,Norm,Norm,-Norm);
  m_VB.Vertices[3]:=DJXVertex3D(cX-HSize,cY+HSize,cZ-HSize,col,0,0,-Norm,Norm,-Norm);

  m_VB.Vertices[4]:=DJXVertex3D(cX-HSize,cY-HSize,cZ+HSize,col,0,0,-Norm,-Norm,Norm);
  m_VB.Vertices[5]:=DJXVertex3D(cX+HSize,cY-HSize,cZ+HSize,col,0,0,Norm,-Norm,Norm);
  m_VB.Vertices[6]:=DJXVertex3D(cX+HSize,cY-HSize,cZ-HSize,col,0,0,Norm,-Norm,-Norm);
  m_VB.Vertices[7]:=DJXVertex3D(cX-HSize,cY-HSize,cZ-HSize,col,0,0,-Norm,-Norm,-Norm);

  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives3D.CubeTex(
      cX,cY,cZ,
      Size: single;
      col: TDJXColor;
      Texture: TDJXTexture
    );
var
  HSize: single;
  procedure DrawWired;
  begin
    m_VB.Indices[0]:=0;
    m_VB.Indices[1]:=1;
    m_VB.Indices[2]:=1;
    m_VB.Indices[3]:=2;
    m_VB.Indices[4]:=2;
    m_VB.Indices[5]:=3;
    m_VB.Indices[6]:=3;
    m_VB.Indices[7]:=0;

    m_VB.Indices[8]:=4;
    m_VB.Indices[9]:=5;
    m_VB.Indices[10]:=5;
    m_VB.Indices[11]:=6;
    m_VB.Indices[12]:=6;
    m_VB.Indices[13]:=7;
    m_VB.Indices[14]:=7;
    m_VB.Indices[15]:=4;

    m_VB.Indices[16]:=0;
    m_VB.Indices[17]:=7;
    m_VB.Indices[18]:=1;
    m_VB.Indices[19]:=6;
    m_VB.Indices[20]:=2;
    m_VB.Indices[21]:=5;
    m_VB.Indices[22]:=3;
    m_VB.Indices[23]:=4;

    m_VB.setVertices(0,8);
    m_VB.setIndices(0,24);
    m_VB.DrawIndexedLineList(0,12);
  end;
  procedure DrawSolid;
  begin
    m_VB.setVertices(0,24);
    m_VB.setIndices(0,36);
    m_VB.DrawIndexedTriangleList(0,12);
  end;
var
  Norm: single;
begin
  Norm:=Sqrt(1/3);
  if Texture=nil then
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil)
  else
  Texture.SetToDevice(0);
  HSize:=Size/2;
  
  //Top
  m_VB.Vertices[0]:=DJXVertex3D(cX-HSize,cY+HSize,cZ+HSize,col,0,0,-Norm,Norm,Norm);
  m_VB.Vertices[1]:=DJXVertex3D(cX+HSize,cY+HSize,cZ+HSize,col,1,0,Norm,Norm,Norm);
  m_VB.Vertices[2]:=DJXVertex3D(cX+HSize,cY+HSize,cZ-HSize,col,1,1,Norm,Norm,-Norm);
  m_VB.Vertices[3]:=DJXVertex3D(cX-HSize,cY+HSize,cZ-HSize,col,0,1,-Norm,Norm,-Norm);
  m_VB.Indices[0]:=0;m_VB.Indices[1]:=1;m_VB.Indices[2]:=2;
  m_VB.Indices[3]:=2;m_VB.Indices[4]:=3;m_VB.Indices[5]:=0;

  //Bottom
  m_VB.Vertices[4]:=DJXVertex3D(cX-HSize,cY-HSize,cZ-HSize,col,0,0,-Norm,-Norm,-Norm);
  m_VB.Vertices[5]:=DJXVertex3D(cX+HSize,cY-HSize,cZ-HSize,col,1,0,Norm,-Norm,-Norm);
  m_VB.Vertices[6]:=DJXVertex3D(cX+HSize,cY-HSize,cZ+HSize,col,1,1,Norm,-Norm,Norm);
  m_VB.Vertices[7]:=DJXVertex3D(cX-HSize,cY-HSize,cZ+HSize,col,0,1,-Norm,-Norm,Norm);
  m_VB.Indices[6]:=4;m_VB.Indices[7]:=5;m_VB.Indices[8]:=6;
  m_VB.Indices[9]:=6;m_VB.Indices[10]:=7;m_VB.Indices[11]:=4;

  //Left
  m_VB.Vertices[8]:=DJXVertex3D(cX-HSize,cY+HSize,cZ+HSize,col,0,0,-Norm,Norm,Norm);
  m_VB.Vertices[9]:=DJXVertex3D(cX-HSize,cY+HSize,cZ-HSize,col,1,0,-Norm,Norm,-Norm);
  m_VB.Vertices[10]:=DJXVertex3D(cX-HSize,cY-HSize,cZ-HSize,col,1,1,-Norm,-Norm,-Norm);
  m_VB.Vertices[11]:=DJXVertex3D(cX-HSize,cY-HSize,cZ+HSize,col,0,1,-Norm,-Norm,Norm);
  m_VB.Indices[12]:=8;m_VB.Indices[13]:=9;m_VB.Indices[14]:=10;
  m_VB.Indices[15]:=10;m_VB.Indices[16]:=11;m_VB.Indices[17]:=8;

  //Right
  m_VB.Vertices[12]:=DJXVertex3D(cX+HSize,cY+HSize,cZ-HSize,col,0,0,Norm,Norm,-Norm);
  m_VB.Vertices[13]:=DJXVertex3D(cX+HSize,cY+HSize,cZ+HSize,col,1,0,Norm,Norm,Norm);
  m_VB.Vertices[14]:=DJXVertex3D(cX+HSize,cY-HSize,cZ+HSize,col,1,1,Norm,-Norm,Norm);
  m_VB.Vertices[15]:=DJXVertex3D(cX+HSize,cY-HSize,cZ-HSize,col,0,1,Norm,-Norm,-Norm);
  m_VB.Indices[18]:=12;m_VB.Indices[19]:=13;m_VB.Indices[20]:=14;
  m_VB.Indices[21]:=14;m_VB.Indices[22]:=15;m_VB.Indices[23]:=12;

  //Front
  m_VB.Vertices[16]:=DJXVertex3D(cX-HSize,cY+HSize,cZ-HSize,col,0,0,-Norm,Norm,-Norm);
  m_VB.Vertices[17]:=DJXVertex3D(cX+HSize,cY+HSize,cZ-HSize,col,1,0,Norm,Norm,-Norm);
  m_VB.Vertices[18]:=DJXVertex3D(cX+HSize,cY-HSize,cZ-HSize,col,1,1,Norm,-Norm,-Norm);
  m_VB.Vertices[19]:=DJXVertex3D(cX-HSize,cY-HSize,cZ-HSize,col,0,1,-Norm,-Norm,-Norm);
  m_VB.Indices[24]:=16;m_VB.Indices[25]:=17;m_VB.Indices[26]:=18;
  m_VB.Indices[27]:=18;m_VB.Indices[28]:=19;m_VB.Indices[29]:=16;

  //Back
  m_VB.Vertices[20]:=DJXVertex3D(cX+HSize,cY+HSize,cZ+HSize,col,0,0,Norm,Norm,Norm);
  m_VB.Vertices[21]:=DJXVertex3D(cX-HSize,cY+HSize,cZ+HSize,col,1,0,-Norm,Norm,Norm);
  m_VB.Vertices[22]:=DJXVertex3D(cX-HSize,cY-HSize,cZ+HSize,col,1,1,-Norm,-Norm,Norm);
  m_VB.Vertices[23]:=DJXVertex3D(cX+HSize,cY-HSize,cZ+HSize,col,0,1,Norm,-Norm,Norm);
  m_VB.Indices[30]:=20;m_VB.Indices[31]:=21;m_VB.Indices[32]:=22;
  m_VB.Indices[33]:=22;m_VB.Indices[34]:=23;m_VB.Indices[35]:=20;

  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives3D.Box(
      X, Y, Z: single;
      SizeX, SizeY, SizeZ: single;
      col: TDJXColor
    );
  procedure DrawSolid;
  begin
    //Indices
    //Bottom
    m_VB.Indices[0] := 0;
    m_VB.Indices[1] := 2;
    m_VB.Indices[2] := 1;
    m_VB.Indices[3] := 0;
    m_VB.Indices[4] := 3;
    m_VB.Indices[5] := 2;

    //Back
    m_VB.Indices[6] := 1;
    m_VB.Indices[7] := 6;
    m_VB.Indices[8] := 5;
    m_VB.Indices[9] := 1;
    m_VB.Indices[10] := 2;
    m_VB.Indices[11] := 6;

    //Left
    m_VB.Indices[12] := 0;
    m_VB.Indices[13] := 5;
    m_VB.Indices[14] := 4;
    m_VB.Indices[15] := 0;
    m_VB.Indices[16] := 1;
    m_VB.Indices[17] := 5;

    //Right
    m_VB.Indices[18] := 2;
    m_VB.Indices[19] := 7;
    m_VB.Indices[20] := 6;
    m_VB.Indices[21] := 2;
    m_VB.Indices[22] := 3;
    m_VB.Indices[23] := 7;

    //Front
    m_VB.Indices[24] := 3;
    m_VB.Indices[25] := 4;
    m_VB.Indices[26] := 7;
    m_VB.Indices[27] := 3;
    m_VB.Indices[28] := 0;
    m_VB.Indices[29] := 4;

    //Top
    m_VB.Indices[30] := 4;
    m_VB.Indices[31] := 5;
    m_VB.Indices[32] := 6;
    m_VB.Indices[33] := 4;
    m_VB.Indices[34] := 6;
    m_VB.Indices[35] := 7;

    m_VB.setIndices(0, 36);
    m_VB.DrawIndexedTriangleList(0,12);
  end;
  procedure DrawWired;
  begin
    m_VB.Indices[0] := 0;
    m_VB.Indices[1] := 1;
    m_VB.Indices[2] := 1;
    m_VB.Indices[3] := 2;
    m_VB.Indices[4] := 2;
    m_VB.Indices[5] := 3;
    m_VB.Indices[6] := 3;
    m_VB.Indices[7] := 0;
    m_VB.Indices[8] := 4;
    m_VB.Indices[9] := 5;
    m_VB.Indices[10] := 5;
    m_VB.Indices[11] := 6;
    m_VB.Indices[12] := 6;
    m_VB.Indices[13] := 7;
    m_VB.Indices[14] := 7;
    m_VB.Indices[15] := 4;
    m_VB.Indices[16] := 0;
    m_VB.Indices[17] := 4;
    m_VB.Indices[18] := 1;
    m_VB.Indices[19] := 5;
    m_VB.Indices[20] := 2;
    m_VB.Indices[21] := 6;
    m_VB.Indices[22] := 3;
    m_VB.Indices[23] := 7;

    m_VB.setIndices(0, 24);
    m_VB.DrawIndexedLineList(0, 12);
  end;
var
  Norm: single;
begin
  Norm := Sqrt(1/3);
  //Vertices
  m_VB.Vertices[0] := DJXVertex3D(X, Y, Z, col, 0, 0, -Norm, -Norm, -Norm);
  m_VB.Vertices[1] := DJXVertex3D(X, Y, Z + SizeZ, col, 0, 0, -Norm, -Norm, Norm);
  m_VB.Vertices[2] := DJXVertex3D(X + SizeX, Y, Z + SizeZ, col, 0, 0, Norm, -Norm, Norm);
  m_VB.Vertices[3] := DJXVertex3D(X + SizeX, Y, Z, col, 0, 0, Norm, -Norm, -Norm);
  m_VB.Vertices[4] := DJXVertex3D(X, Y + SizeY, Z, col, 0, 0, -Norm, Norm, -Norm);
  m_VB.Vertices[5] := DJXVertex3D(X, Y + SizeY, Z + SizeZ, col, 0, 0, -Norm, Norm, Norm);
  m_VB.Vertices[6] := DJXVertex3D(X + SizeX, Y + SizeY, Z + SizeZ, col, 0, 0, Norm, Norm, Norm);
  m_VB.Vertices[7] := DJXVertex3D(X + SizeX, Y + SizeY, Z, col, 0, 0, Norm, Norm, -Norm);
  m_VB.setVertices(0, 8);

  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives3D.BoxTex(
      X, Y, Z: single;
      SizeX, SizeY, SizeZ: single;
      col: TDJXColor;
      Texture: TDJXTexture
    );
  procedure DrawSolid;
  begin
    //Bottom
    m_VB.Vertices[0] := DJXVertex3D(X, Y, Z, col, 0, 1, 0, -1, 0);
    m_VB.Vertices[1] := DJXVertex3D(X, Y, Z + SizeZ, col, 0, 0, 0, -1, 0);
    m_VB.Vertices[2] := DJXVertex3D(X + SizeX, Y, Z + SizeZ, col, 1, 0, 0, -1, 0);
    m_VB.Vertices[3] := DJXVertex3D(X + SizeX, Y, Z, col, 1, 1, 0, -1, 0);
    m_VB.Indices[0] := 0; m_VB.Indices[1] := 3; m_VB.Indices[2] := 2;
    m_VB.Indices[3] := 0; m_VB.Indices[4] := 2; m_VB.Indices[5] := 1;

    //Back
    m_VB.Vertices[4] := DJXVertex3D(X, Y, Z + SizeZ, col, 0, 0, 0, 0, 1);
    m_VB.Vertices[5] := DJXVertex3D(X, Y + SizeY, Z + SizeZ, col, 0, 1, 0, 0, 1);
    m_VB.Vertices[6] := DJXVertex3D(X + SizeX, Y + SizeY, Z + SizeZ, col, 1, 1, 0, 0, 1);
    m_VB.Vertices[7] := DJXVertex3D(X + SizeX, Y, Z + SizeZ, col, 1, 0, 0, 0, 1);
    m_VB.Indices[6] := 4; m_VB.Indices[7] := 6; m_VB.Indices[8] := 5;
    m_VB.Indices[9] := 4; m_VB.Indices[10] := 7; m_VB.Indices[11] := 6;

    //Left
    m_VB.Vertices[8] := DJXVertex3D(X, Y, Z, col, 1, 0, -1, 0, 0);
    m_VB.Vertices[9] := DJXVertex3D(X, Y, Z + SizeZ, col, 0, 0, -1, 0, 0);
    m_VB.Vertices[10] := DJXVertex3D(X, Y + SizeY, Z + SizeZ, col, 0, 1, -1, 0, 0);
    m_VB.Vertices[11] := DJXVertex3D(X, Y + SizeY, Z, col, 1, 1, -1, 0, 0);
    m_VB.Indices[12] := 8; m_VB.Indices[13] := 9; m_VB.Indices[14] := 10;
    m_VB.Indices[15] := 8; m_VB.Indices[16] := 10; m_VB.Indices[17] := 11;

    //Right
    m_VB.Vertices[12] := DJXVertex3D(X + SizeX, Y, Z + SizeZ, col, 1, 0, 1, 0, 0);
    m_VB.Vertices[13] := DJXVertex3D(X + SizeX, Y + SizeY, Z + SizeZ, col, 1, 1, 1, 0, 0);
    m_VB.Vertices[14] := DJXVertex3D(X + SizeX, Y + SizeY, Z, col, 0, 1, 1, 0, 0);
    m_VB.Vertices[15] := DJXVertex3D(X + SizeX, Y, Z, col, 0, 0, 1, 0, 0);
    m_VB.Indices[18] := 12; m_VB.Indices[19] := 14; m_VB.Indices[20] := 13;
    m_VB.Indices[21] := 12; m_VB.Indices[22] := 15; m_VB.Indices[23] := 14;

    //Front
    m_VB.Vertices[16] := DJXVertex3D(X + SizeX, Y, Z, col, 1, 0, 0, 0, -1);
    m_VB.Vertices[17] := DJXVertex3D(X + SizeX, Y + SizeY, Z, col, 1, 1, 0, 0, -1);
    m_VB.Vertices[18] := DJXVertex3D(X, Y + SizeY, Z, col, 0, 1, 0, 0, -1);
    m_VB.Vertices[19] := DJXVertex3D(X, Y, Z, col, 0, 0, 0, 0, -1);
    m_VB.Indices[24] := 16; m_VB.Indices[25] := 18; m_VB.Indices[26] := 17;
    m_VB.Indices[27] := 16; m_VB.Indices[28] := 19; m_VB.Indices[29] := 18;

    //Top
    m_VB.Vertices[20] := DJXVertex3D(X, Y + SizeY, Z + SizeZ, col, 0, 1, 0, 1, 0);
    m_VB.Vertices[21] := DJXVertex3D(X, Y + SizeY, Z, col, 0, 0, 0, 1, 0);
    m_VB.Vertices[22] := DJXVertex3D(X + SizeX, Y + SizeY, Z, col, 1, 0, 0, 1, 0);
    m_VB.Vertices[23] := DJXVertex3D(X + SizeX, Y + SizeY, Z + SizeZ, col, 1, 1, 0, 1, 0);
    m_VB.Indices[30] := 20; m_VB.Indices[31] := 22; m_VB.Indices[32] := 21;
    m_VB.Indices[33] := 20; m_VB.Indices[34] := 23; m_VB.Indices[35] := 22;

    m_VB.setVertices(0, 28);
    m_VB.setIndices(0, 36);
    m_VB.DrawIndexedTriangleList(0, 12);
  end;
begin
  if Texture = nil then
  TDanJetX(m_DanJetX).Device.SetTexture(0, nil)
  else
  Texture.SetToDevice(0);

  if m_Filled then
  DrawSolid
  else
  Box(X, Y, Z, SizeX, SizeY, SizeZ, col);
end;
procedure TDJXPrimitives3D.Sphere(
      cX,cY,cZ,Radius: single;
      VSegments,HSegments: integer;
      Col: TDJXColor
    );
var
  i,j: integer;
  RPointCount, VPointCount: integer;
  cRad: single;
  procedure DrawWired;
  var
  i,j: integer;
  begin
    for j:=0 to VPointCount-1 do
    begin
      for i:=0 to RPointCount-1 do
      begin
        m_VB.Indices[(j*(RPointCount) + i)*4 + 0]:=(j)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*4 + 1]:=(j+1)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*4 + 2]:=(j)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*4 + 3]:=(j)*RPointCount + i + 1;
      end;
    end;
    m_VB.setVertices(0,(VPointCount+1)*(RPointCount+1));
    m_VB.setIndices(0,(VPointCount)*(RPointCount)*4);
    m_VB.DrawIndexedLineList(0,(VPointCount)*(RPointCount)*2-2);
  end;
  procedure DrawSolid;
  var
  i,j: integer;
  begin
    for j:=0 to VPointCount-1 do
    begin
      for i:=0 to RPointCount-1 do
      begin
        m_VB.Indices[(j*(RPointCount) + i)*6 + 0]:=(j+0)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 1]:=(j+0)*RPointCount + i + 1;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 2]:=(j+1)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 3]:=(j+1)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 4]:=(j+0)*RPointCount + i + 1;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 5]:=(j+1)*RPointCount + i + 1;
      end;
    end;
    m_VB.setVertices(0,(VPointCount+1)*(RPointCount+1));
    m_VB.setIndices(0,(VPointCount)*(RPointCount)*6);
    m_VB.DrawIndexedTriangleList(0,(VPointCount)*(RPointCount)*2-2);
  end;
begin
  if (VSegments-1)*(HSegments-1)>450 then exit;
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  VPointCount:=VSegments+1;
  RPointCount:=HSegments+1;

  //Set Vertices
  for j:=0 to VPointCount do
  begin
    cRad:=Sin((j/VPointCount)*pi)*radius;
    for i:=0 to RPointCount-1 do
    begin
      m_VB.Vertices[j*(RPointCount) + i]:=DJXVertex3D(
        cx + Cos((i/(RPointCount-1))*2*pi)*cRad,
        cy + Cos((j/(VPointCount))*pi)*radius,
        cz + Sin((i/(RPointCount-1))*2*pi)*cRad,
        col
      );
    end;
  end;

  //Set Normals
  for i:=0 to RPointCount-1 do
  begin
    m_VB.Vertices[0*(RPointCount) + i].nx:=0;
    m_VB.Vertices[0*(RPointCount) + i].ny:=1;
    m_VB.Vertices[0*(RPointCount) + i].nz:=0;
    m_VB.Vertices[VPointCount*(RPointCount) + i].nx:=0;
    m_VB.Vertices[VPointCount*(RPointCount) + i].ny:=-1;
    m_VB.Vertices[VPointCount*(RPointCount) + i].nz:=0;
  end;
  for j:=1 to VPointCount-1 do
  begin
    for i:=0 to RPointCount-1 do
    begin
      NormalizeTriangle(
        m_VB.Vertices[(j+0)*(RPointCount) + i + 0],
        m_VB.Vertices[(j+0)*(RPointCount) + i + 1],
        m_VB.Vertices[(j+1)*(RPointCount) + i + 0]
      );
      NormalizeTriangle(
        m_VB.Vertices[(j+1)*(RPointCount) + i + 0],
        m_VB.Vertices[(j+0)*(RPointCount) + i + 1],
        m_VB.Vertices[(j+1)*(RPointCount) + i + 1]
      );
    end;
    m_VB.Vertices[j*(RPointCount) + RPointCount-1].nx:=m_VB.Vertices[j*(RPointCount) + 0].nx;
    m_VB.Vertices[j*(RPointCount) + RPointCount-1].ny:=m_VB.Vertices[j*(RPointCount) + 0].ny;
    m_VB.Vertices[j*(RPointCount) + RPointCount-1].nz:=m_VB.Vertices[j*(RPointCount) + 0].nz;
  end;

  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives3D.SphereTex(
      cX,cY,cZ,Radius: single;
      VSegments,HSegments: integer;
      Col: TDJXColor;
      Texture: TDJXTexture
    );
var
  i,j: integer;
  RPointCount, VPointCount: integer;
  cRad: single;
  procedure DrawWired;
  var
  i,j: integer;
  begin
    for j:=0 to VPointCount-1 do
    begin
      for i:=0 to RPointCount-1 do
      begin
        m_VB.Indices[(j*(RPointCount) + i)*4 + 0]:=(j)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*4 + 1]:=(j+1)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*4 + 2]:=(j)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*4 + 3]:=(j)*RPointCount + i + 1;
      end;
    end;
    m_VB.setVertices(0,(VPointCount+1)*(RPointCount+1));
    m_VB.setIndices(0,(VPointCount)*(RPointCount)*4);
    m_VB.DrawIndexedLineList(0,(VPointCount)*(RPointCount)*2-2);
  end;
  procedure DrawSolid;
  var
  i,j: integer;
  begin
    for j:=0 to VPointCount-1 do
    begin
      for i:=0 to RPointCount-1 do
      begin
        m_VB.Indices[(j*(RPointCount) + i)*6 + 0]:=(j+0)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 1]:=(j+0)*RPointCount + i + 1;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 2]:=(j+1)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 3]:=(j+1)*RPointCount + i + 0;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 4]:=(j+0)*RPointCount + i + 1;
        m_VB.Indices[(j*(RPointCount) + i)*6 + 5]:=(j+1)*RPointCount + i + 1;
      end;
    end;
    m_VB.setVertices(0,(VPointCount+1)*(RPointCount+1));
    m_VB.setIndices(0,(VPointCount)*(RPointCount)*6);
    m_VB.DrawIndexedTriangleList(0,(VPointCount)*(RPointCount)*2-2);
  end;
begin
  if (VSegments-1)*(HSegments-1)>450 then exit;
  if Texture=nil then
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil)
  else
  Texture.SetToDevice(0);
  VPointCount:=VSegments+1;
  RPointCount:=HSegments+1;
  //Set Vertices
  for j:=0 to VPointCount do
  begin
    cRad:=Sin((j/VPointCount)*pi)*radius;
    for i:=0 to RPointCount-1 do
    begin
      m_VB.Vertices[j*(RPointCount) + i]:=DJXVertex3D(
        cx + Cos((i/(RPointCount-1))*2*pi)*cRad,
        cy + Cos((j/(VPointCount))*pi)*radius,
        cz + Sin((i/(RPointCount-1))*2*pi)*cRad,
        col,
        i/(RPointCount-1),
        j/VPointCount
      );
    end;
  end;
  //Set Normals
  for i:=0 to RPointCount-1 do
  begin
    m_VB.Vertices[0*(RPointCount) + i].nx:=0;
    m_VB.Vertices[0*(RPointCount) + i].ny:=1;
    m_VB.Vertices[0*(RPointCount) + i].nz:=0;
    m_VB.Vertices[VPointCount*(RPointCount) + i].nx:=0;
    m_VB.Vertices[VPointCount*(RPointCount) + i].ny:=-1;
    m_VB.Vertices[VPointCount*(RPointCount) + i].nz:=0;
  end;
  for j:=1 to VPointCount-1 do
  begin
    for i:=0 to RPointCount-1 do
    begin
      NormalizeTriangle(
        m_VB.Vertices[(j+0)*(RPointCount) + i + 0],
        m_VB.Vertices[(j+0)*(RPointCount) + i + 1],
        m_VB.Vertices[(j+1)*(RPointCount) + i + 0]
      );
      NormalizeTriangle(
        m_VB.Vertices[(j+1)*(RPointCount) + i + 0],
        m_VB.Vertices[(j+0)*(RPointCount) + i + 1],
        m_VB.Vertices[(j+1)*(RPointCount) + i + 1]
      );
    end;
    m_VB.Vertices[j*(RPointCount) + RPointCount-1].nx:=m_VB.Vertices[j*(RPointCount) + 0].nx;
    m_VB.Vertices[j*(RPointCount) + RPointCount-1].ny:=m_VB.Vertices[j*(RPointCount) + 0].ny;
    m_VB.Vertices[j*(RPointCount) + RPointCount-1].nz:=m_VB.Vertices[j*(RPointCount) + 0].nz;
  end;

  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives3D.SphereMT(
      cX,cY,cZ,Radius: single;
      VSegments,HSegments: integer;
      Col: TDJXColor;
      Texture0: TDJXTexture = nil;
      Texture1: TDJXTexture = nil;
      Texture2: TDJXTexture = nil;
      Texture3: TDJXTexture = nil
    );
var
  i,j: integer;
  RPointCount, VPointCount: integer;
  cRad: single;
  tu,tv: single;
  procedure DrawWired;
  var
  i,j: integer;
  begin
    for j:=0 to VPointCount-1 do
    begin
      for i:=0 to RPointCount-1 do
      begin
        m_VBF.Indices[(j*(RPointCount) + i)*4 + 0]:=(j)*RPointCount + i + 0;
        m_VBF.Indices[(j*(RPointCount) + i)*4 + 1]:=(j+1)*RPointCount + i + 0;
        m_VBF.Indices[(j*(RPointCount) + i)*4 + 2]:=(j)*RPointCount + i + 0;
        m_VBF.Indices[(j*(RPointCount) + i)*4 + 3]:=(j)*RPointCount + i + 1;
      end;
    end;
    m_VBF.setVertices(0,(VPointCount+1)*(RPointCount+1));
    m_VBF.setIndices(0,(VPointCount)*(RPointCount)*4);
    m_VBF.DrawIndexedLineList(0,(VPointCount)*(RPointCount)*2-2);
  end;
  procedure DrawSolid;
  var
  i,j: integer;
  begin
    for j:=0 to VPointCount-1 do
    begin
      for i:=0 to RPointCount-1 do
      begin
        m_VBF.Indices[(j*(RPointCount) + i)*6 + 0]:=(j+0)*RPointCount + i + 0;
        m_VBF.Indices[(j*(RPointCount) + i)*6 + 1]:=(j+0)*RPointCount + i + 1;
        m_VBF.Indices[(j*(RPointCount) + i)*6 + 2]:=(j+1)*RPointCount + i + 0;
        m_VBF.Indices[(j*(RPointCount) + i)*6 + 3]:=(j+1)*RPointCount + i + 0;
        m_VBF.Indices[(j*(RPointCount) + i)*6 + 4]:=(j+0)*RPointCount + i + 1;
        m_VBF.Indices[(j*(RPointCount) + i)*6 + 5]:=(j+1)*RPointCount + i + 1;
      end;
    end;
    m_VBF.setVertices(0,(VPointCount+1)*(RPointCount+1));
    m_VBF.setIndices(0,(VPointCount)*(RPointCount)*6);
    m_VBF.DrawIndexedTriangleList(0,(VPointCount)*(RPointCount)*2-2);
  end;
begin
  if (VSegments-1)*(HSegments-1)>450 then exit;
  //Set textures BEGIN
  if Texture0<>nil then
  Texture0.SetToDevice(0)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  if Texture1<>nil then
  Texture1.SetToDevice(1)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(1,nil);
  if Texture2<>nil then
  Texture2.SetToDevice(2)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(2,nil);
  if Texture3<>nil then
  Texture3.SetToDevice(3)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(3,nil);
  //Set textures END
  VPointCount:=VSegments+1;
  RPointCount:=HSegments+1;
  //Set Vertices
  for j:=0 to VPointCount do
  begin
    cRad:=Sin((j/VPointCount)*pi)*radius;
    for i:=0 to RPointCount-1 do
    begin
      tu := i/(RPointCount-1);
      tv := j/VPointCount;
      m_VBF.Vertices[j*(RPointCount) + i]:=DJXVertexFlex(
        cx + Cos((i/(RPointCount-1))*2*pi)*cRad,
        cy + Cos((j/(VPointCount))*pi)*radius,
        cz + Sin((i/(RPointCount-1))*2*pi)*cRad,
        0,0,0,
        col,
        tu,tv,
        tu,tv,
        tu,tv,
        tu,tv
      );
    end;
  end;
  //Set Normals
  for i:=0 to RPointCount-1 do
  begin
    m_VBF.Vertices[0*(RPointCount) + i].nx:=0;
    m_VBF.Vertices[0*(RPointCount) + i].ny:=1;
    m_VBF.Vertices[0*(RPointCount) + i].nz:=0;
    m_VBF.Vertices[VPointCount*(RPointCount) + i].nx:=0;
    m_VBF.Vertices[VPointCount*(RPointCount) + i].ny:=-1;
    m_VBF.Vertices[VPointCount*(RPointCount) + i].nz:=0;
  end;
  for j:=1 to VPointCount-1 do
  begin
    for i:=0 to RPointCount-1 do
    begin
      NormalizeTriangle(
        m_VBF.Vertices[(j+0)*(RPointCount) + i + 0],
        m_VBF.Vertices[(j+0)*(RPointCount) + i + 1],
        m_VBF.Vertices[(j+1)*(RPointCount) + i + 0]
      );
      NormalizeTriangle(
        m_VBF.Vertices[(j+1)*(RPointCount) + i + 0],
        m_VBF.Vertices[(j+0)*(RPointCount) + i + 1],
        m_VBF.Vertices[(j+1)*(RPointCount) + i + 1]
      );
    end;
    m_VBF.Vertices[j*(RPointCount) + RPointCount-1].nx:=m_VBF.Vertices[j*(RPointCount) + 0].nx;
    m_VBF.Vertices[j*(RPointCount) + RPointCount-1].ny:=m_VBF.Vertices[j*(RPointCount) + 0].ny;
    m_VBF.Vertices[j*(RPointCount) + RPointCount-1].nz:=m_VBF.Vertices[j*(RPointCount) + 0].nz;
  end;

  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;

procedure TDJXPrimitives3D.Capsule(
      x, y, z: single;
      Length: single;
      Radius: single;
      VSegments, HSegments: integer;
      Col: TDJXColor
    );
const
  HalfPi = pi / 2;
  TwoPi = pi * 2;
var
  i, j: integer;
  grad1, grad2: single;
  CapHSegs: integer;
  Cap2: integer;
begin
  CapHSegs := HSegments div 2;
  Cap2 := CapHSegs * VSegments;
  for j := 0 to CapHSegs - 1 do
  for i := 0 to VSegments - 1 do
  begin
    grad1 := j / (CapHSegs - 1);
    grad2 := i / (VSegments);
    m_VB.Vertices[j * VSegments + i] := DJXVertex3D(
      x + Sin(grad2 * TwoPi) * Sin(grad1 * HalfPi) * Radius,
      y + (Sin(grad1 * HalfPi - HalfPi) + 1) * Radius,
      z + Cos(grad2 * TwoPi) * Sin(grad1 * HalfPi) * Radius,
      Col,
      0, 0,
      Sin(grad2 * TwoPi) * Sin(grad1 * HalfPi),
      Sin(grad1 * HalfPi - HalfPi),
      Cos(grad2 * TwoPi) * Sin(grad1 * HalfPi)
    );
    m_VB.Vertices[Cap2 + j * VSegments + i] := DJXVertex3D(
      x + Sin(grad2 * TwoPi) * Cos(grad1 * HalfPi) * Radius,
      y + Radius + Length + (Sin(grad1 * HalfPi)) * Radius,
      z + Cos(grad2 * TwoPi) * Cos(grad1 * HalfPi) * Radius,
      Col,
      0, 0,
      Sin(grad2 * TwoPi) * Cos(grad1 * HalfPi),
      Sin(grad1 * HalfPi),
      Cos(grad2 * TwoPi) * Cos(grad1 * HalfPi)
    );
    if i < VSegments - 1 then
    begin
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 0] := (j) * VSegments + i;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 1] := (j) * VSegments + i + 1;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 2] := (j + 1) * VSegments + i + 1;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 3] := (j) * VSegments + i;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 4] := (j + 1) * VSegments + i + 1;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 5] := (j + 1) * VSegments + i;
      
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 0] := Cap2 + (j) * VSegments + i;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 1] := Cap2 + (j) * VSegments + i + 1;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 2] := Cap2 + (j + 1) * VSegments + i + 1;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 3] := Cap2 + (j) * VSegments + i;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 4] := Cap2 + (j + 1) * VSegments + i + 1;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 5] := Cap2 + (j + 1) * VSegments + i;
    end
    else
    begin
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 0] := (j) * VSegments + i;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 1] := (j) * VSegments + 0;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 2] := (j + 1) * VSegments + 0;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 3] := (j) * VSegments + i;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 4] := (j + 1) * VSegments + 0;
      m_VB.Indices[((j + 0) * VSegments + (i + 0)) * 6 + 5] := (j + 1) * VSegments + i;

      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 0] := Cap2 + (j) * VSegments + i;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 1] := Cap2 + (j) * VSegments + 0;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 2] := Cap2 + (j + 1) * VSegments + 0;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 3] := Cap2 + (j) * VSegments + i;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 4] := Cap2 + (j + 1) * VSegments + 0;
      m_VB.Indices[(Cap2 + (j + 0) * VSegments + (i + 0)) * 6 + 5] := Cap2 + (j + 1) * VSegments + i;
    end;
    if j = 0 then
    begin
    
    end;
  end;
  m_VB.setVertices(0, CapHSegs * VSegments * 2);
  m_VB.setIndices(0, CapHSegs * VSegments * 6 * 2);
  m_VB.DrawIndexedTriangleList(0, (CapHSegs - 1) * (VSegments) * 4 + (VSegments - 1) * 4);
end;

procedure TDJXPrimitives3D.Quadrangle4col(
      x1,y1,z1,
      x2,y2,z2,
      x3,y3,z3,
      x4,y4,z4: single;
      col1,col2,col3,col4: TDJXColor;
      Texture: TDJXTexture = nil;
      ImageIndex: integer = 0;
      TileWidth: integer = 0;
      TileHeight: integer = 0
    );
  procedure DrawWired;
  begin
    m_VB.Vertices[0]:=DJXVertex3D(x1,y1,z1,col1);
    m_VB.Vertices[1]:=DJXVertex3D(x2,y2,z2,col2);
    m_VB.Vertices[2]:=DJXVertex3D(x3,y3,z3,col3);
    m_VB.Vertices[3]:=DJXVertex3D(x4,y4,z4,col4);
    m_VB.Vertices[4]:=DJXVertex3D(x1,y1,z1,col1);
    m_VB.setVertices(0,5);
    m_VB.DrawLineStrip(0,4);
  end;
  procedure DrawSolid;
  var
  Norm: TD3DXVector3;
  begin
    m_VB.Vertices[0]:=DJXVertex3D(x1,y1,z1,col1);
    m_VB.Vertices[1]:=DJXVertex3D(x2,y2,z2,col2);
    m_VB.Vertices[2]:=DJXVertex3D(x3,y3,z3,col3);
    m_VB.Vertices[3]:=DJXVertex3D(x4,y4,z4,col4);
    if Texture<>nil then
    SetTextureToVertices(
      m_VB.Vertices[0],
      m_VB.Vertices[1],
      m_VB.Vertices[2],
      m_VB.Vertices[3],
      Texture,
      false,false,
      ImageIndex,
      TileWidth,
      TileHeight,
      TDanJetX(m_DanJetX).TruncatePixel
    );
    Norm:=GetTriangleNormal(
      D3DXVector3(m_VB.Vertices[0].x,m_VB.Vertices[0].y,m_VB.Vertices[0].z),
      D3DXVector3(m_VB.Vertices[1].x,m_VB.Vertices[1].y,m_VB.Vertices[1].z),
      D3DXVector3(m_VB.Vertices[2].x,m_VB.Vertices[2].y,m_VB.Vertices[2].z)
    );
    m_VB.Vertices[0].nx:=Norm.x; m_VB.Vertices[0].ny:=Norm.y; m_VB.Vertices[0].nz:=Norm.z;
    m_VB.Vertices[1].nx:=Norm.x; m_VB.Vertices[1].ny:=Norm.y; m_VB.Vertices[1].nz:=Norm.z;
    m_VB.Vertices[2].nx:=Norm.x; m_VB.Vertices[2].ny:=Norm.y; m_VB.Vertices[2].nz:=Norm.z;
    m_VB.Vertices[3].nx:=Norm.x; m_VB.Vertices[3].ny:=Norm.y; m_VB.Vertices[3].nz:=Norm.z;
    m_VB.setVertices(0,4);
    m_VB.DrawTriangleFan(0,2);
  end;
begin
  if Texture=nil then
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil)
  else
  Texture.SetToDevice(0);
  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives3D.Quadrangle4ColMT(
      x1,y1,z1,
      x2,y2,z2,
      x3,y3,z3,
      x4,y4,z4: single;
      col1,col2,col3,col4: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
  procedure DrawWired;
  begin
    m_VB.Vertices[0]:=DJXVertex3D(x1,y1,z1,col1);
    m_VB.Vertices[1]:=DJXVertex3D(x2,y2,z2,col2);
    m_VB.Vertices[2]:=DJXVertex3D(x3,y3,z3,col3);
    m_VB.Vertices[3]:=DJXVertex3D(x4,y4,z4,col4);
    m_VB.Vertices[4]:=DJXVertex3D(x1,y1,z1,col1);
    m_VB.setVertices(0,5);
    m_VB.DrawLineStrip(0,4);
  end;
  procedure DrawSolid;
  var
  Norm: TD3DXVector3;
  begin
    m_VBF.Vertices[0]:=DJXVertexFlex(
      x1,y1,z1,0,0,0,col1,
      t0u1,t0v1,t1u1,t1v1,t2u1,t2v1,t3u1,t3v1
    );
    m_VBF.Vertices[1]:=DJXVertexFlex(
      x2,y2,z2,0,0,0,col2,
      t0u2,t0v2,t1u2,t1v2,t2u2,t2v2,t3u2,t3v2
    );
    m_VBF.Vertices[2]:=DJXVertexFlex(
      x3,y3,z3,0,0,0,col3,
      t0u3,t0v3,t1u3,t1v3,t2u3,t2v3,t3u3,t3v3
    );
    m_VBF.Vertices[3]:=DJXVertexFlex(
      x4,y4,z4,0,0,0,col4,
      t0u4,t0v4,t1u4,t1v4,t2u4,t2v4,t3u4,t3v4
    );
    Norm:=GetTriangleNormal(
      D3DXVector3(m_VBF.Vertices[0].x,m_VBF.Vertices[0].y,m_VBF.Vertices[0].z),
      D3DXVector3(m_VBF.Vertices[1].x,m_VBF.Vertices[1].y,m_VBF.Vertices[1].z),
      D3DXVector3(m_VBF.Vertices[2].x,m_VBF.Vertices[2].y,m_VBF.Vertices[2].z)
    );
    m_VBF.Vertices[0].nx:=Norm.x; m_VB.Vertices[0].ny:=Norm.y; m_VB.Vertices[0].nz:=Norm.z;
    m_VBF.Vertices[1].nx:=Norm.x; m_VB.Vertices[1].ny:=Norm.y; m_VB.Vertices[1].nz:=Norm.z;
    m_VBF.Vertices[2].nx:=Norm.x; m_VB.Vertices[2].ny:=Norm.y; m_VB.Vertices[2].nz:=Norm.z;
    m_VBF.Vertices[3].nx:=Norm.x; m_VB.Vertices[3].ny:=Norm.y; m_VB.Vertices[3].nz:=Norm.z;
    m_VBF.setVertices(0,4);
    m_VBF.DrawTriangleFan(0,2);
  end;
begin
  //Set textures BEGIN
  if Texture0<>nil then
  Texture0.SetToDevice(0)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  if Texture1<>nil then
  Texture1.SetToDevice(1)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(1,nil);
  if Texture2<>nil then
  Texture2.SetToDevice(2)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(2,nil);
  if Texture3<>nil then
  Texture3.SetToDevice(3)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(3,nil);
  //Set textures END
  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives3D.Quadrangle(
      x1,y1,z1,
      x2,y2,z2,
      x3,y3,z3,
      x4,y4,z4: single;
      col: TDJXColor;
      Texture: TDJXTexture = nil;
      ImageIndex: integer = 0;
      TileWidth: integer = 0;
      TileHeight: integer = 0
    );
begin
  Quadrangle4col(
    x1,y1,z1,
    x2,y2,z2,
    x3,y3,z3,
    x4,y4,z4,
    col,col,col,col,
    Texture,
    ImageIndex,
    TileWidth,
    TileHeight
  );
end;
procedure TDJXPrimitives3D.QuadrangleMT(
      x1,y1,z1,
      x2,y2,z2,
      x3,y3,z3,
      x4,y4,z4: single;
      col: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
begin
  Quadrangle4colMT(
    x1,y1,z1,
    x2,y2,z2,
    x3,y3,z3,
    x4,y4,z4,
    col,col,
    col,col,
    Texture0,t0u1,t0v1,t0u2,t0v2,t0u3,t0v3,t0u4,t0v4,
    Texture1,t1u1,t1v1,t1u2,t1v2,t1u3,t1v3,t1u4,t1v4,
    Texture2,t2u1,t2v1,t2u2,t2v2,t2u3,t2v3,t2u4,t2v4,
    Texture3,t3u1,t3v1,t3u2,t3v2,t3u3,t3v3,t3u4,t3v4
  );
end;
procedure TDJXPrimitives3D.Triangle3col(x1,y1,z1,x2,y2,z2,x3,y3,z3: single; col1,col2,col3: TDJXColor);
  procedure DrawWired;
  begin
    m_VB.Vertices[0]:=DJXVertex3D(x1,y1,z1,col1);
    m_VB.Vertices[1]:=DJXVertex3D(x2,y2,z2,col2);
    m_VB.Vertices[2]:=DJXVertex3D(x3,y3,z3,col3);
    m_VB.Vertices[3]:=DJXVertex3D(x1,y1,z1,col1);
    m_VB.setVertices(0,4);
    m_VB.DrawLineStrip(0,3);
  end;
  procedure DrawSolid;
  begin
    m_VB.Vertices[0]:=DJXVertex3D(x1,y1,z1,col1);
    m_VB.Vertices[1]:=DJXVertex3D(x2,y2,z2,col2);
    m_VB.Vertices[2]:=DJXVertex3D(x3,y3,z3,col3);
    m_VB.setVertices(0,3);
    m_VB.DrawTriangleList(0,1);
  end;
begin
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives3D.Triangle(x1,y1,z1,x2,y2,z2,x3,y3,z3: single; col: TDJXColor);
begin
  Triangle3col(
    x1,y1,z1,
    x2,y2,z2,
    x3,y3,z3,
    col,col,col
  );
end;
procedure TDJXPrimitives3D.Line2col(x1,y1,z1,x2,y2,z2:single; col1,col2: TDJXColor);
begin
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  m_VB.Vertices[0]:=DJXVertex3D(x1,y1,z1,col1);
  m_VB.Vertices[1]:=DJXVertex3D(x2,y2,z2,col2);
  m_VB.setVertices(0,2);
  m_VB.DrawLineList(0,1);
end;
procedure TDJXPrimitives3D.Line(x1,y1,z1,x2,y2,z2:single; col: TDJXColor);
begin
  Line2col(x1,y1,z1,x2,y2,z2,col,col);
end;
procedure TDJXPrimitives3D.BillboardMT(
      x,y,z,
      x1,y1,
      x2,y2,
      x3,y3,
      x4,y4: single;
      col1,col2,
      col3,col4: TDJXColor;
      Rotation,ScaleX,ScaleY: single;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
var
  Norm: TD3DXVector3;
begin
  //Set textures BEGIN
  if Texture0<>nil then
  Texture0.SetToDevice(0)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  if Texture1<>nil then
  Texture1.SetToDevice(1)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(1,nil);
  if Texture2<>nil then
  Texture2.SetToDevice(2)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(2,nil);
  if Texture3<>nil then
  Texture3.SetToDevice(3)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(3,nil);
  //Set textures END
  m_VBF.Vertices[0]:=DJXVertexFlex(
    x1,y1,0,0,0,0,col1,
    t0u1,t0v1,t1u1,t1v1,t2u1,t2v1,t3u1,t3v1
  );
  m_VBF.Vertices[1]:=DJXVertexFlex(
    x2,y2,0,0,0,0,col2,
    t0u2,t0v2,t1u2,t1v2,t2u2,t2v2,t3u2,t3v2
  );
  m_VBF.Vertices[2]:=DJXVertexFlex(
    x3,y3,0,0,0,0,col3,
    t0u3,t0v3,t1u3,t1v3,t2u3,t2v3,t3u3,t3v3
  );
  m_VBF.Vertices[3]:=DJXVertexFlex(
    x4,y4,0,0,0,0,col3,
    t0u4,t0v4,t1u4,t1v4,t2u4,t2v4,t3u4,t3v4
  );

  Norm:=GetTriangleNormal(
    D3DXVector3(x1,y1,0),
    D3DXVector3(x2,y2,0),
    D3DXVector3(x3,y3,0)
  );
  m_VBF.Vertices[0].nx:=Norm.x; m_VBF.Vertices[0].ny:=Norm.y; m_VBF.Vertices[0].nz:=Norm.z;
  m_VBF.Vertices[1].nx:=Norm.x; m_VBF.Vertices[1].ny:=Norm.y; m_VBF.Vertices[1].nz:=Norm.z;
  m_VBF.Vertices[2].nx:=Norm.x; m_VBF.Vertices[2].ny:=Norm.y; m_VBF.Vertices[2].nz:=Norm.z;
  m_VBF.Vertices[3].nx:=Norm.x; m_VBF.Vertices[3].ny:=Norm.y; m_VBF.Vertices[3].nz:=Norm.z;

  TDanJetX(m_DanJetX).Matrices3D.ClearWorldMatrix;
  TDanJetX(m_DanJetX).Matrices3D.ScaleWorld(ScaleX,ScaleY,1);
  TDanJetX(m_DanJetX).Matrices3D.RotateWorld(0,0,Rotation);
  TDJXRender(TDanJetX(m_DanJetX).Render).Prs3DInverseWorldTo(TDJXRender(TDanJetX(m_DanJetX).Render).MatView^);
  TDJXRender(TDanJetX(m_DanJetX).Render).MatWorld^._41:=0;
  TDJXRender(TDanJetX(m_DanJetX).Render).MatWorld^._42:=0;
  TDJXRender(TDanJetX(m_DanJetX).Render).MatWorld^._43:=0;
  TDanJetX(m_DanJetX).Matrices3D.MoveWorld(x,y,z);
  TDanJetX(m_DanJetX).Matrices3D.ApplyWorldMatrix;

  m_VBF.setVertices(0,4);
  m_VBF.DrawTriangleFan(0,2);
end;
procedure TDJXPrimitives3D.Strip(
      VectorArray: PVector4Array;
      Width1: single; Width2: single;
      Texture: TDJXTexture = nil;
      tu0: single = 0; tv0: single = 0;
      tu1: single = 1; tv1: single = 0;
      tu2: single = 1; tv2: single = 1;
      tu3: single = 0; tv3: single = 1
    );
var
  i: integer;
  v1, v2, v3, s1n1, s1n2, s2n1, s2n2, n1, n2: TD3DXVector3;
  grad, w1, w2: single;
  a: DWORD;
begin
  if Texture <> nil then Texture.SetToDevice(0);
  w1 := Width1 * 0.5;
  w2 := Width2 * 0.5;
  v2 := D3DXVector3(
    TDanJetX(m_DanJetX).Camera.EyePtX,
    TDanJetX(m_DanJetX).Camera.EyePtY,
    TDanJetX(m_DanJetX).Camera.EyePtZ
  );
  for i := 0 to High(VectorArray^) do
  begin
    grad := i / High(VectorArray^);
    v1 := D3DXVector3(VectorArray^[i].x, VectorArray^[i].y, VectorArray^[i].z);
    if i < High(VectorArray^) then
    begin
      v3 := D3DXVector3(VectorArray^[i + 1].x, VectorArray^[i + 1].y, VectorArray^[i + 1].z);
      s1n1 := GetTriangleNormal(v1, v2, v3);
      s1n2 := D3DXVector3(-s1n1.x, -s1n1.y, -s1n1.z);
      if i = 0 then
      begin
        move(s1n1, n1, SizeOf(TD3DXVector3));
        move(s1n2, n2, SizeOf(TD3DXVector3));
      end;
    end;
    if i > 0 then
    begin
      v3 := D3DXVector3(VectorArray^[i - 1].x, VectorArray^[i - 1].y, VectorArray^[i - 1].z);
      s2n1 := GetTriangleNormal(v2, v1, v3);
      s2n2 := D3DXVector3(-s2n1.x, -s2n1.y, -s2n1.z);
      if i = High(VectorArray^) then
      begin
        move(s2n1, n1, SizeOf(TD3DXVector3));
        move(s2n2, n2, SizeOf(TD3DXVector3));
      end;
    end;
    if (i > 0) and (i < High(VectorArray^)) then
    begin
      D3DXVec3Add(n1, s1n1, s2n1);
      D3DXVec3Normalize(n1, n1);
      D3DXVec3Add(n2, s1n2, s2n2);
      D3DXVec3Normalize(n2, n2);
    end;
    a := Round(255 * VectorArray^[i].w) shl 24;
    m_VB.Vertices[i * 2 + 0] := DJXVertex3D(
      VectorArray^[i].x + n1.x * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].y + n1.y * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].z + n1.z * (w1 * (1 - grad) + w2 * grad),
      $ffffff or a,
      tu0 * (1 - grad) + tu3 * grad,
      tv0 * (1 - grad) + tv3 * grad
    );
    m_VB.Vertices[i * 2 + 1] := DJXVertex3D(
      VectorArray^[i].x + n2.x * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].y + n2.y * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].z + n2.z * (w1 * (1 - grad) + w2 * grad),
      $ffffff or a,
      tu1 * (1 - grad) + tu2 * grad,
      tv1 * (1 - grad) + tv2 * grad 
    );
  end;
  m_VB.setVertices(0, Length(VectorArray^) * 2);
  m_VB.DrawTriangleStrip(0, High(VectorArray^) * 2);
end;
procedure TDJXPrimitives3D.StripCol(
      VectorArray: PColorVector3Array;
      Width1: single; Width2: single;
      Texture: TDJXTexture = nil;
      tu0: single = 0; tv0: single = 0;
      tu1: single = 1; tv1: single = 0;
      tu2: single = 1; tv2: single = 1;
      tu3: single = 0; tv3: single = 1
    );
var
  i: integer;
  v1, v2, v3, s1n1, s1n2, s2n1, s2n2, n1, n2: TD3DXVector3;
  grad, w1, w2: single;
begin
  if Texture <> nil then Texture.SetToDevice(0);
  w1 := Width1 * 0.5;
  w2 := Width2 * 0.5;
  v2 := D3DXVector3(
    TDanJetX(m_DanJetX).Camera.EyePtX,
    TDanJetX(m_DanJetX).Camera.EyePtY,
    TDanJetX(m_DanJetX).Camera.EyePtZ
  );
  for i := 0 to High(VectorArray^) do
  begin
    grad := i / High(VectorArray^);
    v1 := D3DXVector3(VectorArray^[i].x, VectorArray^[i].y, VectorArray^[i].z);
    if i < High(VectorArray^) then
    begin
      v3 := D3DXVector3(VectorArray^[i + 1].x, VectorArray^[i + 1].y, VectorArray^[i + 1].z);
      s1n1 := GetTriangleNormal(v1, v2, v3);
      s1n2 := D3DXVector3(-s1n1.x, -s1n1.y, -s1n1.z);
      if i = 0 then
      begin
        move(s1n1, n1, SizeOf(TD3DXVector3));
        move(s1n2, n2, SizeOf(TD3DXVector3));
      end;
    end;
    if i > 0 then
    begin
      v3 := D3DXVector3(VectorArray^[i - 1].x, VectorArray^[i - 1].y, VectorArray^[i - 1].z);
      s2n1 := GetTriangleNormal(v2, v1, v3);
      s2n2 := D3DXVector3(-s2n1.x, -s2n1.y, -s2n1.z);
      if i = High(VectorArray^) then
      begin
        move(s2n1, n1, SizeOf(TD3DXVector3));
        move(s2n2, n2, SizeOf(TD3DXVector3));
      end;
    end;
    if (i > 0) and (i < High(VectorArray^)) then
    begin
      D3DXVec3Add(n1, s1n1, s2n1);
      D3DXVec3Normalize(n1, n1);
      D3DXVec3Add(n2, s1n2, s2n2);
      D3DXVec3Normalize(n2, n2);
    end;
    m_VB.Vertices[i * 2 + 0] := DJXVertex3D(
      VectorArray^[i].x + n1.x * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].y + n1.y * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].z + n1.z * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].color,
      tu0 * (1 - grad) + tu3 * grad,
      tv0 * (1 - grad) + tv3 * grad
    );
    m_VB.Vertices[i * 2 + 1] := DJXVertex3D(
      VectorArray^[i].x + n2.x * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].y + n2.y * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].z + n2.z * (w1 * (1 - grad) + w2 * grad),
      VectorArray^[i].color,
      tu1 * (1 - grad) + tu2 * grad,
      tv1 * (1 - grad) + tv2 * grad 
    );
  end;
  m_VB.setVertices(0, Length(VectorArray^) * 2);
  m_VB.DrawTriangleStrip(0, High(VectorArray^) * 2);
end;
procedure TDJXPrimitives3D.Init;
begin
  m_VB:=TDJXVB3D.Create(m_DanJetX);
  m_VB.initVB(512, D3DUSAGE_WRITEONLY);
  m_VB.initIB(2048, D3DUSAGE_WRITEONLY);
  m_VBF:=TDJXVBFlex.Create(m_DanJetX);
  m_VBF.VF_Col:=true;
  m_VBF.VF_Norm:=true;
  m_VBF.VF_TexCount:=4;
  m_VBF.VF_Pos2D:=false;
  m_VBF.initVB(512, D3DUSAGE_WRITEONLY);
  m_VBF.initIB(2048, D3DUSAGE_WRITEONLY);
end;
procedure TDJXPrimitives3D.UnInit; 
begin
  m_VB.Free;
  m_VBF.Free;
end;
//TDJXPrimitives3D END

//TDJXPrimitives2D BEGIN
procedure TDJXPrimitives2D.Polygon(Points: array of TDJXVertex2D);
  procedure DrawWired;
  begin
    move(Points[0],m_VB.Vertices[0],sizeof(TDJXVertex2D)*length(Points));
    m_VB.Vertices[length(Points)]:=Points[0];
    m_VB.setVertices(0,length(Points)+1);
    m_VB.DrawLineStrip(0,length(Points));
  end;
  procedure DrawSolid;
  begin
    move(Points[0],m_VB.Vertices[0],sizeof(TDJXVertex2D)*length(Points));
    m_VB.setVertices(0,length(Points));
    m_VB.DrawTriangleFan(0,length(Points)-2);
  end;
begin
  if (length(Points)<3)or(length(Points)>1023) then exit;
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives2D.Circle2col(cX,cY,radius: single; VertexNumber: integer; IntCol,ExtCol: TDJXColor);
var
  vCount: integer;
  procedure DrawWired;
  var
    i: integer;
  begin
    vCount:=VertexNumber+1;
    if vCount<4 then vCount:=4;
    if vCount>128 then vCount:=128;
    for i:=0 to vCount-1 do
    m_VB.Vertices[i]:=DJXVertex2D(
      Cos((i/(vCount-1))*2*pi)*radius + cX,
      Sin((i/(vCount-1))*2*pi)*radius + cY,
      ExtCol
    );
    m_VB.setVertices(0,vCount);
    m_VB.DrawLineStrip(0,vCount-1);
  end;
  procedure DrawSolid;
  var
    i: integer;
  begin
    vCount:=VertexNumber+1;
    if vCount<4 then vCount:=4;
    if vCount>127 then vCount:=127;
    m_VB.Vertices[0]:=DJXVertex2D(cX,cY,IntCol);
    for i:=0 to vCount-1 do
    m_VB.Vertices[1+i]:=DJXVertex2D(
      Cos((i/(vCount-1))*2*pi)*radius + cX,
      Sin((i/(vCount-1))*2*pi)*radius + cY,
      ExtCol
    );
    m_VB.setVertices(0,vCount+1);
    m_VB.DrawTriangleFan(0,vCount-1);
  end;
begin
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives2D.Circle(cX,cY,radius: single; VertexNumber: integer; Col: TDJXColor);
begin
  Circle2col(
    cX,cY,
    radius,
    VertexNumber,
    col,col
  );
end;
procedure TDJXPrimitives2D.Quadrangle4col(
      x1,y1,x2,y2,x3,y3,x4,y4: single;
      col1,col2,col3,col4: TDJXColor;
      Texture: TDJXTexture = nil;
      tu1: single = 0; tv1: single = 0;
      tu2: single = 1; tv2: single = 0;
      tu3: single = 1; tv3: single = 1;
      tu4: single = 0; tv4: single = 1
    );
  procedure DrawWired;
  begin
    m_VB.Vertices[0] := DJXVertex2D(x1,y1,col1,tu1,tv1);
    m_VB.Vertices[1] := DJXVertex2D(x2,y2,col2,tu2,tv2);
    m_VB.Vertices[2] := DJXVertex2D(x3,y3,col3,tu3,tv3);
    m_VB.Vertices[3] := DJXVertex2D(x4,y4,col4,tu4,tv4);
    m_VB.Vertices[4] := DJXVertex2D(x1,y1,col1,tu1,tv1);
    m_VB.setVertices(0,5);
    m_VB.DrawLineStrip(0,4);
  end;
  procedure DrawSolid;
  begin
    m_VB.Vertices[0] := DJXVertex2D(x1,y1,col1,tu1,tv1);
    m_VB.Vertices[1] := DJXVertex2D(x2,y2,col2,tu2,tv2);
    m_VB.Vertices[2] := DJXVertex2D(x3,y3,col3,tu3,tv3);
    m_VB.Vertices[3] := DJXVertex2D(x4,y4,col4,tu4,tv4);
    m_VB.setVertices(0,4);
    m_VB.DrawTriangleFan(0,2);
  end;
begin
  if Texture <> nil then Texture.SetToDevice(0)
  else
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives2D.Quadrangle4colMT(
      x1,y1,x2,y2,x3,y3,x4,y4: single;
      col1,col2,col3,col4: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
  procedure DrawWired;
  begin
    m_VB.Vertices[0]:=DJXVertex2D(x1,y1,col1);
    m_VB.Vertices[1]:=DJXVertex2D(x2,y2,col2);
    m_VB.Vertices[2]:=DJXVertex2D(x3,y3,col3);
    m_VB.Vertices[3]:=DJXVertex2D(x4,y4,col4);
    m_VB.Vertices[4]:=DJXVertex2D(x1,y1,col1);
    m_VB.setVertices(0,5);
    m_VB.DrawLineStrip(0,4);
  end;
  procedure DrawSolid;
  begin
    m_VBF.Vertices[0]:=DJXVertexFlex(
      x1,y1,0, 0,0,0,col1, t0u1,t0v1, t1u1,t1v1, t2u1,t2v1, t3u1,t2v1
    );
    m_VBF.Vertices[1]:=DJXVertexFlex(
      x2,y2,0, 0,0,0,col2, t0u2,t0v2, t1u2,t1v2, t2u2,t2v2, t3u2,t2v2
    );
    m_VBF.Vertices[2]:=DJXVertexFlex(
      x3,y3,0, 0,0,0,col3, t0u3,t0v3, t1u3,t1v3, t2u3,t2v3, t3u3,t2v3
    );
    m_VBF.Vertices[3]:=DJXVertexFlex(
      x4,y4,0, 0,0,0,col4, t0u4,t0v4, t1u4,t1v4, t2u4,t2v4, t3u4,t2v4
    );
    //Set textures BEGIN
    if Texture0<>nil then
    Texture0.SetToDevice(0)
    else
    TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
    if Texture1<>nil then
    Texture1.SetToDevice(1)
    else
    TDanJetX(m_DanJetX).Device.SetTexture(1,nil);
    if Texture2<>nil then
    Texture2.SetToDevice(2)
    else
    TDanJetX(m_DanJetX).Device.SetTexture(2,nil);
    if Texture3<>nil then
    Texture3.SetToDevice(3)
    else
    TDanJetX(m_DanJetX).Device.SetTexture(3,nil);
    //Set textures END

    m_VBF.setVertices(0,4);
    m_VBF.DrawTriangleFan(0,2);
  end;
begin
  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives2D.QuadrangleRaw(
      x1, y1, x2, y2, x3, y3, x4, y4: single;
      Col1, Col2, Col3, Col4: TDJXColor;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
begin
  m_VBF.Vertices[0]:=DJXVertexFlex(
    x1,y1,0, 0,0,0,col1, t0u1,t0v1, t1u1,t1v1, t2u1,t2v1, t3u1,t2v1
  );
  m_VBF.Vertices[1]:=DJXVertexFlex(
    x2,y2,0, 0,0,0,col2, t0u2,t0v2, t1u2,t1v2, t2u2,t2v2, t3u2,t2v2
  );
  m_VBF.Vertices[2]:=DJXVertexFlex(
    x3,y3,0, 0,0,0,col3, t0u3,t0v3, t1u3,t1v3, t2u3,t2v3, t3u3,t2v3
  );
  m_VBF.Vertices[3]:=DJXVertexFlex(
    x4,y4,0, 0,0,0,col4, t0u4,t0v4, t1u4,t1v4, t2u4,t2v4, t3u4,t2v4
  );
  m_VBF.setVertices(0,4);
  m_VBF.DrawTriangleFan(0,2);
end;
procedure TDJXPrimitives2D.Quadrangle(
      x1,y1,x2,y2,x3,y3,x4,y4: single;
      col: TDJXColor;
      Texture: TDJXTexture = nil;
      tu1: single = 0; tv1: single = 0;
      tu2: single = 1; tv2: single = 0;
      tu3: single = 1; tv3: single = 1;
      tu4: single = 0; tv4: single = 1
    );
begin
  Quadrangle4col(
    x1,y1,x2,y2,x3,y3,x4,y4,
    col,col,col,col,
    Texture,
    tu1,tv1,
    tu2,tv2,
    tu3,tv3,
    tu4,tv4
  );
end;
procedure TDJXPrimitives2D.QuadrangleMT(
      x1,y1,x2,y2,x3,y3,x4,y4: single;
      col: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
begin
  Quadrangle4colMT(
    x1,y1,x2,y2,x3,y3,x4,y4,
    col,col,col,col,
    Texture0,t0u1,t0v1,t0u2,t0v2,t0u3,t0v3,t0u4,t0v4,
    Texture1,t1u1,t1v1,t1u2,t1v2,t1u3,t1v3,t1u4,t1v4,
    Texture2,t2u1,t2v1,t2u2,t2v2,t2u3,t2v3,t2u4,t2v4,
    Texture3,t3u1,t3v1,t3u2,t3v2,t3u3,t3v3,t3u4,t3v4
  );
end;
procedure TDJXPrimitives2D.Triangle3col(x1,y1,x2,y2,x3,y3: single; col1,col2,col3: TDJXColor);
  procedure DrawWired;
  begin
    m_VB.Vertices[0]:=DJXVertex2D(x1,y1,col1);
    m_VB.Vertices[1]:=DJXVertex2D(x2,y2,col2);
    m_VB.Vertices[2]:=DJXVertex2D(x3,y3,col3);
    m_VB.Vertices[3]:=DJXVertex2D(x1,y1,col1);
    m_VB.setVertices(0,4);
    m_VB.DrawLineStrip(0,3);
  end;
  procedure DrawSolid;
  begin
    m_VB.Vertices[0]:=DJXVertex2D(x1,y1,col1);
    m_VB.Vertices[1]:=DJXVertex2D(x2,y2,col2);
    m_VB.Vertices[2]:=DJXVertex2D(x3,y3,col3);
    m_VB.setVertices(0,3);
    m_VB.DrawTriangleList(0,1);
  end;
begin
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  if m_Filled then
  DrawSolid
  else
  DrawWired;
end;
procedure TDJXPrimitives2D.Triangle(x1,y1,x2,y2,x3,y3: single; col: TDJXColor);
begin
  Triangle3col(
    x1,y1,
    x2,y2,
    x3,y3,
    col,col,col
  );
end;
procedure TDJXPrimitives2D.Rectangle(
      x,y,width,height: single;
      col: TDJXColor
    );
begin
  Rectangle4col(
    x,y,
    width,height,
    col,col,col,col
  );
end;
procedure TDJXPrimitives2D.RectangleMT(
      x,y,width,height: single;
      col: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
begin
  Rectangle4colMT(
    x,y,
    width,height,
    col,col,col,col,
    Texture0,t0u1,t0v1,t0u2,t0v2,t0u3,t0v3,t0u4,t0v4,
    Texture1,t1u1,t1v1,t1u2,t1v2,t1u3,t1v3,t1u4,t1v4,
    Texture2,t2u1,t2v1,t2u2,t2v2,t2u3,t2v3,t2u4,t2v4,
    Texture3,t3u1,t3v1,t3u2,t3v2,t3u3,t3v3,t3u4,t3v4
  );
end;
procedure TDJXPrimitives2D.Rectangle4col(
      x,y,width,height: single;
      col1,col2,col3,col4: TDJXColor
    );
begin
  Quadrangle4col(
    x, y,
    x + width, y,
    x + width, y + height,
    x, y + height,
    col1,col2,col3,col4
  );
end;
procedure TDJXPrimitives2D.Rectangle4colMT(
      x,y,width,height: single;
      col1,col2,col3,col4: TDJXColor;
      Texture0: TDJXTexture = nil;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      Texture1: TDJXTexture = nil;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      Texture2: TDJXTexture = nil;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      Texture3: TDJXTexture = nil;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
begin
  Quadrangle4colMT(
    x, y,
    x + width, y,
    x + width, y + height,
    x, y + height,
    col1,col2,col3,col4,
    Texture0,t0u1,t0v1,t0u2,t0v2,t0u3,t0v3,t0u4,t0v4,
    Texture1,t1u1,t1v1,t1u2,t1v2,t1u3,t1v3,t1u4,t1v4,
    Texture2,t2u1,t2v1,t2u2,t2v2,t2u3,t2v3,t2u4,t2v4,
    Texture3,t3u1,t3v1,t3u2,t3v2,t3u3,t3v3,t3u4,t3v4
  );
end;
procedure TDJXPrimitives2D.RectangleRaw(
      X, Y, Width, Height: single;
      Col1, Col2, Col3, Col4: TDJXColor;
      t0u1: single = 0; t0v1: single = 0;
      t0u2: single = 1; t0v2: single = 0;
      t0u3: single = 1; t0v3: single = 1;
      t0u4: single = 0; t0v4: single = 1;
      t1u1: single = 0; t1v1: single = 0;
      t1u2: single = 1; t1v2: single = 0;
      t1u3: single = 1; t1v3: single = 1;
      t1u4: single = 0; t1v4: single = 1;
      t2u1: single = 0; t2v1: single = 0;
      t2u2: single = 1; t2v2: single = 0;
      t2u3: single = 1; t2v3: single = 1;
      t2u4: single = 0; t2v4: single = 1;
      t3u1: single = 0; t3v1: single = 0;
      t3u2: single = 1; t3v2: single = 0;
      t3u3: single = 1; t3v3: single = 1;
      t3u4: single = 0; t3v4: single = 1
    );
begin
  QuadrangleRaw(
    X, Y, X + Width, Y, X + Width, Y + Height, X, Y + Height,
    Col1, Col2, Col3, Col4,
    t0u1,t0v1,t0u2,t0v2,t0u3,t0v3,t0u4,t0v4,
    t1u1,t1v1,t1u2,t1v2,t1u3,t1v3,t1u4,t1v4,
    t2u1,t2v1,t2u2,t2v2,t2u3,t2v3,t2u4,t2v4,
    t3u1,t3v1,t3u2,t3v2,t3u3,t3v3,t3u4,t3v4
  );
end;
procedure TDJXPrimitives2D.Line(x1,y1,x2,y2: single; col: TDJXColor);
begin
  Line2col(x1,y1,x2,y2,col,col);
end;
procedure TDJXPrimitives2D.Line2col(x1,y1,x2,y2: single; col1, col2: TDJXColor);
begin
  TDanJetX(m_DanJetX).Device.SetTexture(0,nil);
  m_VB.Vertices[0]:=DJXVertex2D(x1,y1,col1);
  m_VB.Vertices[1]:=DJXVertex2D(x2,y2,col2);
  m_VB.setVertices(0,2);
  m_VB.DrawLineList(0,1);
end;
procedure TDJXPrimitives2D.Init;
begin
  m_VB:=TDJXVB2D.Create(m_DanJetX);
  m_VB.initVB(128, D3DUSAGE_WRITEONLY);
  m_VBF:=TDJXVBFlex.Create(m_DanJetX);
  m_VBF.VF_Col:=true;
  m_VBF.VF_Norm:=false;
  m_VBF.VF_TexCount:=4;
  m_VBF.VF_Pos2D:=true;
  m_VBF.initVB(256, D3DUSAGE_WRITEONLY);
end;
procedure TDJXPrimitives2D.UnInit;
begin
  m_VB.Free;
  m_VBF.Free;
end;
//TDJXPrimitives2D END


end.
