⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dxrender.pas

📁 传奇源代码的delphi版本
💻 PAS
📖 第 1 页 / 共 5 页
字号:
unit DXRender;

interface

{$INCLUDE DelphiXcfg.inc}

uses
  Windows,
{$IfDef StandardDX}
  DirectDraw,
  {$ifdef DX7}
  Direct3D;
  {$endif}
  {$IfDef DX9}
  Direct3D9, Direct3D, D3DX9, {Direct3D8,} DX7toDX8;
//  {$Else}
//  {$IfDef DX81}
//  D3DX8, DirectXGraphics, DX7toDX8;
//  {$Else}
//   Direct3DRM;
//  {$EndIf}
  {$EndIf}
{$Else}
  DirectX;
{$EndIf}

const
  DXR_MAXTEXTURE = 4;

type
  TDXR_Value = Double;

  TDXR_Color = DWORD;
  TDXR_SurfaceColor = DWORD;

  {  TDXR_Option  }

  PDXR_Option = ^TDXR_Option;
  TDXR_Option = (
    DXR_OPTION_VERSION,
    DXR_OPTION_MMXENABLE,
    DXR_OPTION_RENDERPRIMITIVES
  );                            

  {  TDXR_ShadeMode  }

  TDXR_ShadeMode = (
    DXR_SHADEMODE_FLAT,
    DXR_SHADEMODE_GOURAUD
  );

  {  TDXR_Blend  }

  TDXR_Blend = (
    // for blending
    DXR_BLEND_ZERO,                        // r=0
    DXR_BLEND_ONE1,                        // r=c1
    DXR_BLEND_ONE2,                        // r=c2
    DXR_BLEND_ONE1_ADD_ONE2,               // r=c1+c2
    DXR_BLEND_ONE1_SUB_ONE2,               // r=c1-c2
    DXR_BLEND_ONE2_SUB_ONE1,               // r=c2-c1
    DXR_BLEND_ONE1_MUL_ONE2,               // r=c1*c2
    DXR_BLEND_SRCALPHA1,                   // r=c1*a1
    DXR_BLEND_SRCALPHA1_ADD_ONE2,          // r=c1*a1+c2
    DXR_BLEND_ONE2_SUB_SRCALPHA1,          // r=c2-c1*a1
    DXR_BLEND_SRCALPHA1_ADD_INVSRCALPHA2,  // r=c1*a1+c2*(1-a2)
    DXR_BLEND_INVSRCALPHA1_ADD_SRCALPHA2,  // r=c1*(1-a1)+c2*a2
    // for lighting
    DXR_BLEND_DECAL,                       // r=c1
    DXR_BLEND_DECALALPHA,                  // r=c1    ra=a2
    DXR_BLEND_MODULATE,                    // r=c1*c2 ra=a2
    DXR_BLEND_MODULATEALPHA,               // r=c1*c2
    DXR_BLEND_ADD                          // r=c1+c2 ra=a2
  );

  {  TDXR_TextureFilter  }

  TDXR_TextureFilter = (
    DXR_TEXTUREFILTER_NEAREST,
    DXR_TEXTUREFILTER_LINEAR,
    DXR_TEXTUREFILTER_MIPMAP_NEAREST,
    DXR_TEXTUREFILTER_MIPMAP_LINEAR
  );

  {  TDXR_TextureAddress  }

  TDXR_TextureAddress = (
    DXR_TEXTUREADDRESS_TILE,           // tx=tx and WidthMask ty=ty and HeightMask
    DXR_TEXTUREADDRESS_DONOTCLIP       // tx=tx               ty=ty
  );

  {  TDXR_CmpFunc  }

  TDXR_CmpFunc = (
    DXR_CMPFUNC_NEVER,
    DXR_CMPFUNC_LESS,
    DXR_CMPFUNC_EQUAL,
    DXR_CMPFUNC_LESSEQUAL,
    DXR_CMPFUNC_GREATER,
    DXR_CMPFUNC_NOTEQUAL,
    DXR_CMPFUNC_GREATEREQUAL,
    DXR_CMPFUNC_ALWAYS
  );

  {  TDXR_ColorType  }

  TDXR_ColorType = (
    DXR_COLORTYPE_INDEXED,     // Palette indexed color
    DXR_COLORTYPE_RGB          // RGB color
  );

  {  TDXR_ColorChannel  }

  TDXR_ColorChannel = record
    Mask: DWORD;                // Bit Mask
    BitCount: DWORD;            // Number of bit
    rshift: DWORD;
    lshift: DWORD;
  end;

  {  TDXR_Surface  }

  PDXR_Surface = ^TDXR_Surface;
  TDXR_Surface = record
    ColorType: TDXR_ColorType;   // Color type
    Width, Height: DWORD;        // Size of surface
    WidthBit, HeightBit: DWORD;  // Size of surface (Number of bit)
    Width2, Height2: DWORD;      // 1 shl WidthBit, 1 shl HeightBit
    WidthMask, HeightMask: DWORD;// Bit Mask of size of surface
    BitCount: DWORD;             // BitCount per Pixel(1, 2, 4, 8, 16, 24, 32 only)
    Bits: Pointer;               // Pointer to pixeldata(x:0 y:0)
    Pitch: Integer;              // Offset of next scanline
    PitchBit: Integer;           // Offset of next scanline (Number of bit)
    MipmapChain: PDXR_Surface;
    case Integer of
      0: (
        { Indexed color }
        idx_index: TDXR_ColorChannel;  // Index channel
        idx_alpha: TDXR_ColorChannel;  // Alpha channel
        idx_palette: array[0..255] of TPaletteEntry;
                                       // Palette
      );
      1: (
        { RGB color }
        rgb_red: TDXR_ColorChannel;    // Red channel
        rgb_green: TDXR_ColorChannel;  // Green channel
        rgb_blue: TDXR_ColorChannel;   // Blue channel
        rgb_alpha: TDXR_ColorChannel;  // Alpha channel
      );
  end;

  {  TDXR_Vertex  }

  PDXR_Vertex = ^TDXR_Vertex;
  TDXR_Vertex = record
    sx: TDXR_Value;            // Screen coordinates
    sy: TDXR_Value;
    sz: TDXR_Value;
    rhw: TDXR_Value;           // 1/sz
    color: TDXR_Color;
    specular: TDXR_Color;
    tu, tv: array[0..DXR_MAXTEXTURE-1] of TDXR_Value;
  end;

  PPDXR_Vertex = ^PDXR_Vertex;

  {  TDXR_PrimitiveType  }

  TDXR_PrimitiveType = (
    DXR_PRIMITIVETYPE_TRIANGLELIST,
    DXR_PRIMITIVETYPE_TRIANGLESTRIP
  );

  {  TDXR_TextureLayerBlend  }

  TDXR_TextureLayerBlend = (
    DXR_TEXTURELAYERBLEND_TEXTURE,
    DXR_TEXTURELAYERBLEND_LAST,
    DXR_TEXTURELAYERBLEND_NOBLEND
  );

  {  TDXR_TextureLayer  }

  PDXR_TextureLayer = ^TDXR_TextureLayer;
  TDXR_TextureLayer = record
    Surface: PDXR_Surface;
    LayerBlend: TDXR_TextureLayerBlend;
    Blend: TDXR_Blend;
    ColorKeyEnable: Boolean;
    ColorKey: TDXR_SurfaceColor;
    TextureAddress: TDXR_TextureAddress;
    BumpTexture: Integer;
  end;

  {  TDXR_Cull  }

  TDXR_Cull = (
    DXR_CULL_NONE,
    DXR_CULL_CW,
    DXR_CULL_CCW
  );

  {  TDXR_RenderStates  }

  TDXR_RenderStates = record
    DitherEnable: Boolean;
    SpecularEnable: Boolean;
    CullMode: TDXR_Cull;
    Shade: TDXR_ShadeMode;
    TexBlend: TDXR_Blend;
    Blend: TDXR_Blend;
    TextureEnable: Boolean;
    TextureList: array[0..DXR_MAXTEXTURE-1] of TDXR_TextureLayer;
    TextureFilter: TDXR_TextureFilter;
    ZBuffer: PDXR_Surface;
    ZFunc: TDXR_CmpFunc;
    ZWriteEnable: Boolean;
    EnableDrawLine: Integer;
  end;

function dxrGetOption(Option: TDXR_Option): DWORD;
procedure dxrSetOption(Option: TDXR_Option; Value: DWORD);

procedure dxrMakeIndexedSurface(var Surface: TDXR_Surface; Width, Height, BitCount: DWORD;
  Bits: Pointer; pitch: Integer; idx_index, idx_alpha: DWORD);
procedure dxrMakeRGBSurface(var Surface: TDXR_Surface; Width, Height, BitCount: DWORD;
  Bits: Pointer; pitch: Integer; rgb_red, rgb_green, rgb_blue, rgb_alpha: DWORD);
function dxrScanLine(const Surface: TDXR_Surface; y: DWORD): Pointer;
procedure dxrZBufferClear(const Surface: TDXR_Surface);

function dxrDDSurfaceLock(DDSurface: IDirectDrawSurface; var Surface: TDXR_Surface): Boolean;
function dxrDDSurfaceLock2(DDSurface: IDirectDrawSurface; var ddsd: TDDSurfaceDesc;
  var Surface: TDXR_Surface): Boolean;
procedure dxrDDSurfaceUnLock(DDSurface: IDirectDrawSurface; const Surface: TDXR_Surface);

procedure dxrDefRenderStates(var States: TDXR_RenderStates);

procedure dxrDrawPrimitive(const Dest: TDXR_Surface; const States: TDXR_RenderStates; PrimitiveType: TDXR_PrimitiveType;
  VertexList: PDXR_Vertex; VertexCount: DWORD);
procedure dxrDrawPointeredPrimitive(const Dest: TDXR_Surface; const States: TDXR_RenderStates; PrimitiveType: TDXR_PrimitiveType;
  VertexList: PPDXR_Vertex; VertexCount: DWORD);
procedure dxrDrawIndexedPrimitive(const Dest: TDXR_Surface; const States: TDXR_RenderStates; PrimitiveType: TDXR_PrimitiveType;
  VertexList: PDXR_Vertex; VertexCount: DWORD; IndexList: PDWORD; IndexCount: DWORD);

procedure dxrCopyRectBlend(const Dest, Src: TDXR_Surface;
  const DestRect, SrcRect: TRect; Blend: TDXR_Blend; Alpha: Integer;
  ColorKeyEnable: Boolean; ColorKey: DWORD);

procedure dxrFillRectColorBlend(const Dest: TDXR_Surface;
  const DestRect: TRect; Blend: TDXR_Blend; Col: COLORREF);

procedure dxrDrawWaveXBlend(const Dest, Src: TDXR_Surface;
  X, Y, Width, Height: Integer; const SrcRect: TRect; amp, Len, ph: Integer;
  Blend: TDXR_Blend; Alpha: Integer;
  ColorKeyEnable: Boolean; ColorKey: DWORD);

procedure dxrDrawRotateBlend(const Dest, Src: TDXR_Surface;
  X, Y, Width, Height: Integer; const SrcRect: TRect; CenterX, CenterY: Double;
  Angle: Integer; Blend: TDXR_Blend; Alpha: Integer;
  ColorKeyEnable: Boolean; ColorKey: DWORD);

implementation

const
  TextureAxisFloatBit = 16;
  TextureAxisFloat = 1 shl TextureAxisFloatBit;

  ColorFloatBit = 8;
  ColorFloat = 1 shl ColorFloatBit;
               
type

  PInteger = ^Integer;

//  {  TDXR_CmpFunc  }
//
//  TDXR_CmpFunc = (
//    DXR_CMPFUNC_NEVER,
//    DXR_CMPFUNC_LESS,
//    DXR_CMPFUNC_EQUAL,
//    DXR_CMPFUNC_LESSEQUAL,
//    DXR_CMPFUNC_GREATER,
//    DXR_CMPFUNC_NOTEQUAL,
//    DXR_CMPFUNC_GREATEREQUAL,
//    DXR_CMPFUNC_ALWAYS
//  );

  {  TDXRMachine  }

  TDXRMachine_TreeType = (
    DXR_TREETYPE_LOADBLACK,      // Load black color
    DXR_TREETYPE_LOADCOLOR,      // Load vertex color
    DXR_TREETYPE_LOADCONSTCOLOR, // Load constant color
    DXR_TREETYPE_LOADTEXTURE,    // Load texel
    DXR_TREETYPE_LOADBUMPTEXTURE,// Load texel with Bump mapping
                                 //   dx := nx + (BumpTexture[nx-1, ny]-BumpTexture[nx+1, ny]);
                                 //   dy := ny + (BumpTexture[nx, ny-1]-BumpTexture[nx, ny+1]);
    DXR_TREETYPE_LOADDESTPIXEL,  // Load dest pixel
    DXR_TREETYPE_BLEND           // Blend color
  );

  TDXRColorChannel = (chRed, chGreen, chBlue, chAlpha);
  TDXRColorChannels = set of TDXRColorChannel;

  PDXRMachine_Color = ^TDXRMachine_Color;
  TDXRMachine_Color = packed record
    R, G, B, A: WORD;
  end;

  PDXRMachine_Axis = ^TDXRMachine_Axis;
  TDXRMachine_Axis = packed record
    X, Y: Integer;
  end;

  PDXRMachine_Int64 = ^TDXRMachine_Int64;
  TDXRMachine_Int64 = Comp;

  PDXRMachine_Reg_Color = ^TDXRMachine_Reg_Color;
  TDXRMachine_Reg_Color = record
    Enable: Boolean;
    nColor: TDXRMachine_Color;
    iColor: TDXRMachine_Color;
    Gouraud: Boolean;
    Channels: TDXRColorChannels;
  end;

  PDXRMachine_Reg_Texture = ^TDXRMachine_Reg_Texture;
  TDXRMachine_Reg_Texture = record
    Enable: Boolean;
    Surface: PDXR_Surface;
    nAxis: TDXRMachine_Axis;
    iAxis: TDXRMachine_Axis;
    iAxisConstant: Boolean;
    Filter: TDXR_TextureFilter;
    ColorKeyEnable: Boolean;
    ColorKey: TDXR_SurfaceColor;
    EnableChannels: TDXRColorChannels;
    TextureAddress: TDXR_TextureAddress;
    DefaultColor: TDXRMachine_Color;
  end;

  TDXRMachine_Reg_RHW = record
    Enable: Boolean;
    nRHW: TDXRMachine_Int64;
    iRHW: TDXRMachine_Int64;
  end;

  TDXRMachine_Reg_Dither = record
    Enable: Boolean;
  end;

  TDXRMachine_Reg_ZBuffer = record
    Enable: Boolean;
    Surface: PDXR_Surface;
    CmpFunc: TDXR_CmpFunc;
    WriteEnable: Boolean;
  end;

  TDXRMachine_Reg_Axis = record
    Axis: TDXRMachine_Axis;
    IncEnable: Boolean;
  end;

  PDXRMachine_Tree = ^TDXRMachine_Tree;
  TDXRMachine_Tree = record
    Typ: TDXRMachine_TreeType;
    Channels: TDXRColorChannels;
    case TDXRMachine_TreeType of
      DXR_TREETYPE_LOADBLACK: (
        );
      DXR_TREETYPE_LOADCOLOR: (
        Color: Integer
        );
      DXR_TREETYPE_LOADCONSTCOLOR: (
        ConstColor: TDXRMachine_Color;
        );
      DXR_TREETYPE_LOADTEXTURE: (
        Texture: Integer
        );
      DXR_TREETYPE_LOADBUMPTEXTURE: (
        _Texture: Integer;
        BumpTexture: Integer;
        );
      DXR_TREETYPE_LOADDESTPIXEL: (
        );
      DXR_TREETYPE_BLEND: (
        Blend: TDXR_Blend;
        BlendTree1: PDXRMachine_Tree;
        BlendTree2: PDXRMachine_Tree;
        );
  end;

  TDXRMachine = class
  private
    FBuf: Pointer;
    FCall: Pointer;
    FCompiled: Boolean;
    FTreeCount: Integer;
    FTreeList: array[0..127] of TDXRMachine_Tree;
    FMMXUsed: Boolean;
    F_ZBuf: Pointer;
    F_BiLinearAxis: TDXRMachine_Axis;
    F_BiLinearCol1: TDXRMachine_Color;
    F_BiLinearCol2: TDXRMachine_Color;
    F_BiLinearCol3: TDXRMachine_Color;
    F_BiLinearCol4: TDXRMachine_Color;
    F_BumpAxis: TDXRMachine_Axis;
    F_BumpAxis2: TDXRMachine_Axis;
    F_BumpTempCol: DWORD;
    FStack: array[0..255] of TDXRMachine_Color;
    procedure GenerateCode(var Code: Pointer; Tree: PDXRMachine_Tree);
  public
    Dest: PDXR_Surface;
    ColorList: array[0..7] of TDXRMachine_Reg_Color;
    ColorIndex: array[0..7] of Integer;
    ColorIndexCount: Integer;
    TextureList: array[0..7] of TDXRMachine_Reg_Texture;
    TextureIndex: array[0..7] of Integer;
    TextureIndexCount: Integer;
    Dither: TDXRMachine_Reg_Dither;
    ZBuffer: TDXRMachine_Reg_ZBuffer;
    Axis: TDXRMachine_Reg_Axis;
    RHW: TDXRMachine_Reg_RHW;
    constructor Create;
    destructor Destroy; override;
    function CreateTree: PDXRMachine_Tree;
    function CreateTree2(Typ: TDXRMachine_TreeType): PDXRMachine_Tree;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -