Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Paul Nicholls

By: a guest on Feb 18th, 2010  |  syntax: Delphi  |  size: 44.41 KB  |  hits: 76  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Unit swOpenGL;
  2.  
  3. Interface
  4.  
  5. Const
  6. {...........................................................................}
  7.     GL_MODELVIEW          = 0;
  8.     GL_PROJECTION         = 1;
  9.     GL_TEXTURE            = 2;
  10.     GL_COLOR              = 3;
  11.  
  12.     GL_MODELVIEW_MATRIX   = 0;
  13.     GL_PROJECTION_MATRIX  = 1;
  14.     GL_TEXTURE_MATRIX     = 2;
  15.     GL_COLOR_MATRIX       = 3;
  16.  
  17.     GL_COLOR_BUFFER_BIT   = 1;
  18.     GL_DEPTH_BUFFER_BIT   = 2;
  19.     GL_ACCUM_BUFFER_BIT   = 4;
  20.     GL_STENCIL_BUFFER_BIT = 8;
  21.  
  22.     GL_DEPTH_TEST         = 0;
  23.     GL_TEXTURE_2D         = 1;
  24.  
  25.     GL_POINTS             = 0;
  26.     GL_LINES              = 1;
  27.     GL_LINE_STRIP         = 2;
  28.     GL_LINE_LOOP          = 3;
  29.     GL_TRIANGLES          = 4;
  30.     GL_TRIANGLE_STRIP     = 5;
  31.     GL_TRIANGLE_FAN       = 6;
  32.     GL_QUADS              = 7;
  33.     GL_QUAD_STRIP         = 8;
  34.     GL_POLYGON            = 9;
  35.     GL_NONE               = 1000;
  36.  
  37.     cMaxVertices          = 1000;
  38. {...........................................................................}
  39. Type
  40. {...........................................................................}
  41.     TGLfloat    = Single;
  42.     TGLint      = LongInt;
  43.     TGLubyte    = Byte;
  44.     PGLfloat    = ^TGLfloat;
  45.     PGLint      = ^TGLint;
  46.     PGLubyte    = ^TGLubyte;
  47.     TGLenum     = Cardinal;
  48.     TGLbitfield = Cardinal;
  49.     TGLsizei    = LongInt;
  50.     PMatrix4f   = ^TMatrix4f;
  51.     TMatrix4f   = Array[0..15] Of TGLfloat;
  52.     PVector3f   = ^TVector3f;
  53.     TVector3f   = Array[0..2] Of TGLfloat;
  54.     PVector4f   = ^TVector4f;
  55.     TVector4f   = Array[0..3] Of TGLfloat;
  56.     TVector4i   = Array[0..4] Of TGLint;
  57.     TVector4ub  = Array[0..4] Of TGLubyte;
  58.     PMatrix44f  = ^TMatrix44f;
  59.     TMatrix44f  = Array[0..3,0..3] Of TGLfloat;
  60.  
  61.     TRGB32Bit = Packed Record
  62.     Case Integer Of
  63.         0: (r,g,b,a : TGLubyte);
  64.         1: (rgba    : LongWord);
  65.     End;
  66.     PVideoBuffer = ^TByteArray;
  67.     TByteArray   = Array[0..(MaxInt Div SizeOf(TRGB32Bit)) - 1] Of TRGB32Bit;
  68.  
  69. Procedure glTranslatef(x,y,z: TGLfloat);
  70. Procedure glScalef(x,y,z: TGLfloat);
  71. Procedure glRotatef(angle,x,y,z: TGLfloat);
  72. Procedure glLoadIdentity;
  73. Procedure glPushMatrix;
  74. Procedure glPopMatrix;
  75. Procedure glMatrixMode(mode: TGLenum);
  76. Procedure glMultMatrixf(t: PGLfloat);
  77. Procedure glBegin(mode: TGLenum);
  78. Procedure glEnd;
  79. Procedure glClearColor(red,green,blue,alpha: TGLfloat);
  80. Procedure glClear(mask: TGLbitfield);
  81. Procedure glVertex2f(x,y: TGLfloat);
  82. Procedure glVertex3f(x,y,z: TGLfloat);
  83. Procedure glVertex3fv(v: PGLfloat);
  84. Procedure glVertex4f(x,y,z,w: TGLfloat);
  85. Procedure glColor3f(red,green,blue: TGLfloat);
  86. Procedure glColor4f(red,green,blue,alpha: TGLfloat);
  87. Procedure glViewPort(x,y: TGLint; width,height: TGLsizei);
  88. Procedure gluPerspective(fovy, aspect, zNear, zFar: TGLfloat);
  89.  
  90. Function  GetVideoBuffer: PVideoBuffer;
  91.  
  92. Implementation
  93.  
  94. Uses
  95.     SysUtils;
  96.  
  97. Const
  98. {...........................................................................}
  99.     cIdentityMatrix: TMatrix44f = (
  100.         (1,0,0,0),
  101.         (0,1,0,0),
  102.         (0,0,1,0),
  103.         (0,0,0,1)
  104.     );
  105.     cModelViewMatrixStackSize  = 32;
  106.     cProjectionMatrixStackSize = 2;
  107.     cTextureMatrixStackSize    = 2;
  108.     cColorMatrixStackSize      = 2;
  109.     cEpsilon                   = 0.001;                      
  110. {...........................................................................}
  111.  
  112. Type
  113. {...........................................................................}
  114.     TVideoBuffer = Array Of TRGB32Bit;
  115.     TDepthBuffer = Array Of Word;
  116.     TVertexInfo  = Record
  117.         Mode   : TGLenum;
  118.         Color  : TVector4f;
  119.         Normal : TVector4f;
  120.         Vertex : TVector4f;
  121.     End;
  122.     TMatrixStackInfo = Record
  123.         Stack    : Array[1..32] Of TMatrix44f;
  124.         Index    : Integer;
  125.         MaxIndex : Integer;
  126.     End;
  127. {...........................................................................}
  128.     TGLContext = Class
  129.         MatrixStacks  : Array[0..3] Of TMatrixStackInfo;
  130.         MatrixMode    : TGLenum;
  131.         ViewPort      : TVector4i;
  132.         zbNear        : TGLfloat;
  133.         zbFar         : TGLfloat;
  134.         Vertices      : Array[1..cMaxVertices] Of TVertexInfo;
  135.         VertexCount   : Integer;
  136.         PointSize     : TGLfloat;
  137.         CurrentColor  : TVector4f;
  138.         CurrentNormal : TVector4f;
  139.         VertexMode    : TGLenum;
  140.         ClearColor    : TVector4f;
  141.         VideoBuffer   : TVideoBuffer;
  142.         DepthBuffer   : TDepthBuffer;
  143.         Constructor Create;
  144.         Destructor  Destroy; Override;
  145.  
  146.         Procedure TransformObjectCoordsToEyeCoords(    ox,oy,oz,ow : TGLfloat;
  147.                                                     Var ex,ey,ez,ew : TGLfloat);
  148.         Procedure TransformEyeCoordsToClipCoords(    ex,ey,ez,ew : TGLfloat;
  149.                                                   Var cx,cy,cz,cw : TGLfloat);
  150.         Procedure TransformClipCoordsToNDCoords(    cx,cy,cz,cw : TGLfloat;
  151.                                                  Var nx,ny,nz,nw : TGLfloat);
  152.         Procedure TransformNDCoordsToWindowCoords(    nx,ny,nz : TGLfloat;
  153.                                                    Var wx,wy,wz : TGLfloat);
  154.         Function  GetVideoBuffer: PVideoBuffer;
  155.         Function  BackFaceCullTriangle(Var v1,v2,v3: TVector4f): Boolean;
  156.         Procedure ResizeBuffers;
  157.         Procedure RenderPoint(Var v: TVertexInfo);
  158.         Procedure RenderLine(Var v1,v2: TVertexInfo);
  159.         Procedure RenderTriangle(Var v1,v2,v3: TVertexInfo);
  160.         Procedure ProcessPoints;
  161.         Procedure ProcessLines;
  162.         Procedure ProcessLineStrip;
  163.         Procedure ProcessLineLoop;
  164.         Procedure ProcessTriangles;
  165.         Procedure ProcessTriangleStrip;
  166.         Procedure ProcessTriangleFan;
  167.         Procedure ProcessQuads;
  168.         Procedure ProcessQuadStrip;
  169.         Procedure ProcessPolygon;
  170.         Procedure ProcessVertices;
  171.         Procedure AddVertexf(x,y,z,w: TGLfloat);
  172.         Procedure Init;
  173.         Procedure Close;
  174.         Function  GetMatrix: PMatrix44f;
  175.         Procedure glTranslatef(x,y,z: TGLfloat);
  176.         Procedure glScalef(x,y,z: TGLfloat);
  177.         Procedure glRotatef(angle,x,y,z: TGLfloat);
  178.         Procedure glLoadIdentity;
  179.         Procedure glPushMatrix;
  180.         Procedure glPopMatrix;
  181.         Procedure glMatrixMode(mode: TGLenum);
  182.         Procedure glMultMatrixf(t: Pointer);
  183.         Procedure glBegin(mode: TGLenum);
  184.         Procedure glEnd;
  185.         Procedure glClearColor(red,green,blue,alpha: TGLfloat);
  186.         Procedure ClearVideoBuffer;
  187.         Procedure ClearDepthBuffer;
  188.         Procedure glClear(mask: TGLbitfield);
  189.         Procedure glVertex3fv(v: PGLfloat);
  190.         Procedure glVertex4f(x,y,z,w: TGLfloat);
  191.         Procedure glColor3f(red,green,blue: TGLfloat);
  192.         Procedure glColor4f(red,green,blue,alpha: TGLfloat);
  193.         Procedure glViewPort(x,y,width,height: TGLint);
  194.         Procedure gluPerspective(fovy, aspect, zNear, zFar: TGLfloat);
  195.     End;
  196. {...........................................................................}
  197.     TGLDisplayListItem = Packed Record
  198. //        Case Integer Of
  199. //        End;
  200.     End;
  201.  
  202.     TGLDisplayList = Array Of TGLDisplayListItem;
  203. {...........................................................................}
  204.  
  205. Const
  206.     GLContext: TGLContext = Nil;
  207.  
  208. Function  Vector4f(x,y,z,w: TGLfloat): TVector4f;
  209. Begin
  210.     Result[0] := x;
  211.     Result[1] := y;
  212.     Result[2] := z;
  213.     Result[3] := w;
  214. End;
  215. {...........................................................................}
  216.  
  217. {...........................................................................}
  218. Procedure ClampValuef(Var n: TGLfloat; l,h: TGLfloat);
  219. Begin
  220.     If n < l Then
  221.         n := l
  222.     Else
  223.     If n > h Then
  224.         n := h;
  225. End;
  226. {...........................................................................}
  227.  
  228. {...........................................................................}
  229. Procedure FloatToByte(n: TGLfloat; Var r: TGLubyte);
  230. Var
  231.     w: Integer;
  232. Begin
  233.     w := Round(255 * n);
  234.     If w < 0 Then
  235.         w := 0
  236.     Else
  237.     If w > 255 Then
  238.         w := 255;
  239.     r := w And 255;
  240. End;
  241. {...........................................................................}
  242.  
  243. {...........................................................................}
  244. Procedure PrintMatrix(m: TMatrix44f);
  245. Begin
  246.     WriteLn('[',m[0,0]:12:8,m[0,1]:12:8,m[0,2]:12:8,m[0,3]:10:8,']');
  247.     WriteLn('[',m[1,0]:12:8,m[1,1]:12:8,m[1,2]:12:8,m[1,3]:12:8,']');
  248.     WriteLn('[',m[2,0]:12:8,m[2,1]:12:8,m[2,2]:12:8,m[2,3]:12:8,']');
  249.     WriteLn('[',m[3,0]:12:8,m[3,1]:12:8,m[3,2]:12:8,m[3,3]:12:8,']');
  250.     WriteLn;
  251. End;
  252. {...........................................................................}
  253.  
  254. {...........................................................................}
  255. Procedure MultMatrixf(Const a,b,r: PMatrix44f);
  256. Begin
  257.     r^[0,0] := a^[0,0]*b^[0,0] + a^[0,1]*b^[1,0] + a^[0,2]*b^[2,0] + a^[0,3]*b^[3,0];
  258.     r^[0,1] := a^[0,0]*b^[0,1] + a^[0,1]*b^[1,1] + a^[0,2]*b^[2,1] + a^[0,3]*b^[3,1];
  259.     r^[0,2] := a^[0,0]*b^[0,2] + a^[0,1]*b^[1,2] + a^[0,2]*b^[2,2] + a^[0,3]*b^[3,2];
  260.     r^[0,3] := a^[0,0]*b^[0,3] + a^[0,1]*b^[1,3] + a^[0,2]*b^[2,3] + a^[0,3]*b^[3,3];
  261.  
  262.     r^[1,0] := a^[1,0]*b^[0,0] + a^[1,1]*b^[1,0] + a^[1,2]*b^[2,0] + a^[1,3]*b^[3,0];
  263.     r^[1,1] := a^[1,0]*b^[0,1] + a^[1,1]*b^[1,1] + a^[1,2]*b^[2,1] + a^[1,3]*b^[3,1];
  264.     r^[1,2] := a^[1,0]*b^[0,2] + a^[1,1]*b^[1,2] + a^[1,2]*b^[2,2] + a^[1,3]*b^[3,2];
  265.     r^[1,3] := a^[1,0]*b^[0,3] + a^[1,1]*b^[1,3] + a^[1,2]*b^[2,3] + a^[1,3]*b^[3,3];
  266.  
  267.     r^[2,0] := a^[2,0]*b^[0,0] + a^[2,1]*b^[1,0] + a^[2,2]*b^[2,0] + a^[2,3]*b^[3,0];
  268.     r^[2,1] := a^[2,0]*b^[0,1] + a^[2,1]*b^[1,1] + a^[2,2]*b^[2,1] + a^[2,3]*b^[3,1];
  269.     r^[2,2] := a^[2,0]*b^[0,2] + a^[2,1]*b^[1,2] + a^[2,2]*b^[2,2] + a^[2,3]*b^[3,2];
  270.     r^[2,3] := a^[2,0]*b^[0,3] + a^[2,1]*b^[1,3] + a^[2,2]*b^[2,3] + a^[2,3]*b^[3,3];
  271.  
  272.     r^[3,0] := a^[3,0]*b^[0,0] + a^[3,1]*b^[1,0] + a^[3,2]*b^[2,0] + a^[3,3]*b^[3,0];
  273.     r^[3,1] := a^[3,0]*b^[0,1] + a^[3,1]*b^[1,1] + a^[3,2]*b^[2,1] + a^[3,3]*b^[3,1];
  274.     r^[3,2] := a^[3,0]*b^[0,2] + a^[3,1]*b^[1,2] + a^[3,2]*b^[2,2] + a^[3,3]*b^[3,2];
  275.     r^[3,3] := a^[3,0]*b^[0,3] + a^[3,1]*b^[1,3] + a^[3,2]*b^[2,3] + a^[3,3]*b^[3,3];
  276. End;
  277. {...........................................................................}
  278.  
  279. {...........................................................................}
  280. Procedure MultMatrixVectorf(Const m: PMatrix44f; v,r: PVector4f);
  281. Begin
  282.     r^[0] := m^[0,0]*v^[0] + m^[0,1]*v^[1] + m^[0,2]*v^[2] + m^[0,3]*v^[3];
  283.     r^[1] := m^[1,0]*v^[0] + m^[1,1]*v^[1] + m^[1,2]*v^[2] + m^[1,3]*v^[3];
  284.     r^[2] := m^[2,0]*v^[0] + m^[2,1]*v^[1] + m^[2,2]*v^[2] + m^[2,3]*v^[3];
  285.     r^[3] := m^[3,0]*v^[0] + m^[3,1]*v^[1] + m^[3,2]*v^[2] + m^[3,3]*v^[3];
  286. End;
  287. {...........................................................................}
  288.  
  289. {...........................................................................}
  290. Procedure Rotatef(angle,x,y,z: TGLfloat; m: PMatrix44f);
  291. Var
  292.     c  : Double;
  293.     s  : Double;
  294.     dd : Double;
  295.     d  : Double;
  296. Begin
  297.     m^ := cIdentityMatrix;
  298.     dd := x*x + y*y + z*z;
  299.     If dd < cEpsilon Then Exit;
  300.     If Abs(1 - dd) > cEpsilon Then
  301.     Begin
  302.         d := Sqrt(dd);
  303.         x := x / d;
  304.         y := y / d;
  305.         z := z / d;
  306.     End;
  307.     angle := angle * PI / 180;
  308.     c := Cos(angle);
  309.     s := Sin(angle);
  310.  
  311.     m^[0,0] := x*x*(1-c)+c;
  312.     m^[0,1] := x*y*(1-c)-z*s;
  313.     m^[0,2] := x*z*(1-c)+y*s;
  314.     m^[0,3] := 0;
  315.  
  316.     m^[1,0] := y*x*(1-c)+z*s;
  317.     m^[1,1] := y*y*(1-c)+c;
  318.     m^[1,2] := y*z*(1-c)-x*s;
  319.     m^[1,3] := 0;
  320.  
  321.     m^[2,0] := x*z*(1-c)-y*s;
  322.     m^[2,1] := y*z*(1-c)+x*s;
  323.     m^[2,2] := z*z*(1-c)+c;
  324.     m^[2,3] := 0;
  325.  
  326.     m^[3,0] := 0;
  327.     m^[3,1] := 0;
  328.     m^[3,2] := 0;
  329.     m^[3,3] := 1;
  330. End;
  331. {...........................................................................}
  332.  
  333. {...........................................................................}
  334. Procedure Perspective(fovy, aspect, zNear, zFar: TGLfloat; t: PMatrix44f);
  335. Var
  336.     xScale : TGLfloat;
  337.     yScale : TGLfloat;
  338.     m      : TMatrix44f;
  339. Begin
  340.     fovy := fovy * PI / 360.0;
  341.     yScale := Sin(fovy)/Cos(fovy);
  342.     xScale := aspect * yScale;
  343.  
  344.     m[0,0] := 1.0 / xScale;
  345.     m[0,1] := 0;
  346.     m[0,2] := 0;
  347.     m[0,3] := 0;
  348.  
  349.     m[1,0] := 0;
  350.     m[1,1] := 1.0 / yScale;
  351.     m[1,2] := 0;
  352.     m[1,3] := 0;
  353.  
  354.     m[2,0] := 0;
  355.     m[2,1] := 0;
  356.     m[2,2] := -(zFar + zNear) / (zFar - zNear);
  357.     m[2,3] := -1;
  358.  
  359.     m[3,0] := 0;
  360.     m[3,1] := 0;
  361.     m[3,2] := -(2.0 * zFar * zNear) / (zFar - zNear);
  362.     m[3,3] := 0;
  363.     t^ := m;
  364. {    WriteLn('[',m[0,0]:12:8,m[0,1]:12:8,m[0,2]:12:8,m[0,3]:10:8,']');
  365.     WriteLn('[',m[1,0]:12:8,m[1,1]:12:8,m[1,2]:12:8,m[1,3]:12:8,']');
  366.     WriteLn('[',m[2,0]:12:8,m[2,1]:12:8,m[2,2]:12:8,m[2,3]:12:8,']');
  367.     WriteLn('[',m[3,0]:12:8,m[3,1]:12:8,m[3,2]:12:8,m[3,3]:12:8,']');}
  368. End;
  369. {...........................................................................}
  370.  
  371. {...........................................................................}
  372. Function  GetVideoBuffer: PVideoBuffer;
  373. Begin
  374.     Result := GLContext.GetVideoBuffer;
  375. End;
  376. {...........................................................................}
  377.  
  378. {...........................................................................}
  379. Procedure glTranslatef(x,y,z: TGLfloat);
  380. Begin
  381.     GLContext.glTranslatef(x,y,z);
  382. End;
  383. {...........................................................................}
  384.  
  385. {...........................................................................}
  386. Procedure glScalef(x,y,z: TGLfloat);
  387. Begin
  388.     GLContext.glScalef(x,y,z);
  389. End;
  390. {...........................................................................}
  391.  
  392. {...........................................................................}
  393. Procedure glRotatef(angle,x,y,z: TGLfloat);
  394. Begin
  395.     GLContext.glRotatef(angle,x,y,z);
  396. End;
  397. {...........................................................................}
  398.  
  399. {...........................................................................}
  400. Procedure glLoadIdentity;
  401. Begin
  402.     GLContext.glLoadIdentity;
  403. End;
  404. {...........................................................................}
  405.  
  406. {...........................................................................}
  407. Procedure glPushMatrix;
  408. Begin
  409.     GLContext.glPushMatrix;
  410. End;
  411. {...........................................................................}
  412.  
  413. {...........................................................................}
  414. Procedure glPopMatrix;
  415. Begin
  416.     GLContext.glPopMatrix;
  417. End;
  418. {...........................................................................}
  419.  
  420. {...........................................................................}
  421. Procedure glMatrixMode(mode: TGLenum);
  422. Begin
  423.     GLContext.glMatrixMode(mode);
  424. End;
  425. {...........................................................................}
  426.  
  427. {...........................................................................}
  428. Procedure glMultMatrixf(t: PGLfloat);
  429. Begin
  430.     GLContext.glMultMatrixf(t);
  431. End;
  432. {...........................................................................}
  433.  
  434. {...........................................................................}
  435. Procedure glBegin(mode: TGLenum);
  436. Begin
  437.     GLContext.glBegin(mode);
  438. End;
  439. {...........................................................................}
  440.  
  441. {...........................................................................}
  442. Procedure glEnd;
  443. Begin
  444.     GLContext.glEnd;
  445. End;
  446. {...........................................................................}
  447.  
  448. {...........................................................................}
  449. Procedure glClearColor(red,green,blue,alpha: TGLfloat);
  450. Begin
  451.     GLContext.glClearColor(red,green,blue,alpha);
  452. End;
  453. {...........................................................................}
  454.  
  455. {...........................................................................}
  456. Procedure glClear(mask: TGLbitfield);
  457. Begin
  458.     GLContext.glClear(mask);
  459. End;
  460. {...........................................................................}
  461.  
  462. {...........................................................................}
  463. Procedure glVertex2f(x,y: TGLfloat);
  464. Begin
  465.     GLContext.glVertex4f(x,y,0,1);
  466. End;
  467. {...........................................................................}
  468.  
  469. {...........................................................................}
  470. Procedure glVertex3f(x,y,z: TGLfloat);
  471. Begin
  472.     GLContext.glVertex4f(x,y,z,1);
  473. End;
  474. {...........................................................................}
  475.  
  476. {...........................................................................}
  477. Procedure glVertex3fv(v: PGLfloat);
  478. Begin
  479.     GLContext.glVertex3fv(v);
  480. End;
  481. {...........................................................................}
  482.  
  483. {...........................................................................}
  484. Procedure glVertex4f(x,y,z,w: TGLfloat);
  485. Begin
  486.     GLContext.glVertex4f(x,y,z,w);
  487. End;
  488. {...........................................................................}
  489.  
  490. {...........................................................................}
  491. Procedure glColor3f(red,green,blue: TGLfloat);
  492. Begin
  493.     GLContext.glColor3f(red,green,blue);
  494. End;
  495. {...........................................................................}
  496.  
  497. {...........................................................................}
  498. Procedure glColor4f(red,green,blue,alpha: TGLfloat);
  499. Begin
  500.     GLContext.glColor4f(red,green,blue,alpha);
  501. End;
  502. {...........................................................................}
  503.  
  504. {...........................................................................}
  505. Procedure glViewPort(x,y,width,height: TGLint);
  506. Begin
  507.     GLContext.glViewPort(x,y,width,height);
  508. End;
  509. {...........................................................................}
  510.  
  511. {...........................................................................}
  512. Procedure gluPerspective(fovy,aspect,zNear,zFar: TGLfloat);
  513. Begin
  514.     GLContext.gluPerspective(fovy,aspect,zNear,zFar);
  515. End;
  516. {...........................................................................}
  517.  
  518. {...........................................................................}
  519. Constructor TGLContext.Create;
  520. Begin
  521.     Inherited Create;
  522.     Init;
  523. End;
  524. {...........................................................................}
  525.  
  526. {...........................................................................}
  527. Destructor  TGLContext.Destroy;
  528. Begin
  529.     Close;
  530.     Inherited Destroy;
  531. End;
  532. {...........................................................................}
  533.  
  534. {...........................................................................}
  535. Function  TGLContext.GetVideoBuffer: PVideoBuffer;
  536. Begin
  537.     Result := @VideoBuffer[0];
  538. End;
  539. {...........................................................................}
  540.  
  541. {...........................................................................}
  542. Function  TGLContext.GetMatrix: PMatrix44f;
  543. Begin
  544.     Result := Nil;
  545.     If Not(MatrixMode In[GL_MODELVIEW,GL_PROJECTION,GL_TEXTURE,GL_COLOR]) Then Exit;
  546.     Result := @MatrixStacks[MatrixMode].Stack[MatrixStacks[MatrixMode].Index];
  547. End;
  548. {...........................................................................}
  549.  
  550. {...........................................................................}
  551. Procedure TGLContext.TransformObjectCoordsToEyeCoords(    ox,oy,oz,ow : TGLfloat;
  552.                                                       Var ex,ey,ez,ew : TGLfloat);
  553. Var
  554.     m : PMatrix44f;
  555.     v : TVector4f;
  556.     r : TVector4f;
  557. Begin
  558.     m := @MatrixStacks[GL_MODELVIEW].Stack[MatrixStacks[GL_MODELVIEW].Index];
  559.     v[0] := ox;
  560.     v[1] := oy;
  561.     v[2] := oz;
  562.     v[3] := ow;
  563.     MultMatrixVectorf(m,@v,@r);
  564.     ex := r[0];
  565.     ey := r[1];
  566.     ez := r[2];
  567.     ew := r[3];
  568. End;
  569. {...........................................................................}
  570.  
  571. {...........................................................................}
  572. Procedure TGLContext.TransformEyeCoordsToClipCoords(    ex,ey,ez,ew : TGLfloat;
  573.                                                     Var cx,cy,cz,cw : TGLfloat);
  574. Var
  575.     m : PMatrix44f;
  576.     v : TVector4f;
  577.     r : TVector4f;
  578. Begin
  579.     m := @MatrixStacks[GL_PROJECTION].Stack[MatrixStacks[GL_PROJECTION].Index];
  580.     v[0] := ex;
  581.     v[1] := ey;
  582.     v[2] := ez;
  583.     v[3] := ew;
  584.     MultMatrixVectorf(m,@v,@r);
  585.     cx := r[0];
  586.     cy := r[1];
  587.     cz := r[2];
  588.     cw := r[3];
  589. End;
  590. {...........................................................................}
  591.  
  592. {...........................................................................}
  593. Procedure TGLContext.TransformClipCoordsToNDCoords(    cx,cy,cz,cw : TGLfloat;
  594.                                                    Var nx,ny,nz,nw : TGLfloat);
  595. Begin
  596.     nx := cx / cw;
  597.     ny := cy / cw;
  598.     nz := cz / cw;
  599.     nw := 1;
  600. End;
  601. {...........................................................................}
  602.  
  603. {...........................................................................}
  604. Procedure TGLContext.TransformNDCoordsToWindowCoords(    nx,ny,nz : TGLfloat;
  605.                                                      Var wx,wy,wz : TGLfloat);
  606. Begin
  607.     wx := (nx + 1)*(ViewPort[2] / 2) + ViewPort[0];
  608.     wy := (ny + 1)*(ViewPort[3] / 2) + ViewPort[1];
  609.     wz := nz;
  610. End;
  611. {...........................................................................}
  612.  
  613. {...........................................................................}
  614. Function ClipCoordIsVisible(Var v: TVector4f): Boolean;
  615. Var
  616.     MinCoord : Single;
  617.     MaxCoord : Single;
  618. Begin
  619.     MinCoord := -v[3] + cEpsilon;
  620.     MaxCoord := +v[3] - cEpsilon;
  621.     Result := (v[0] >= MinCoord) And (v[0] <= MaxCoord) And
  622.               (v[1] >= MinCoord) And (v[1] <= MaxCoord) And
  623.               (v[2] >= MinCoord) And (v[2] <= MaxCoord);
  624. End;
  625. {...........................................................................}
  626.  
  627. {...........................................................................}
  628. Function Mini(a,b: Integer): Integer;
  629. Begin
  630.     Result := a * Ord(a <= b) + b * Ord(b < a);
  631. End;
  632. {...........................................................................}
  633.  
  634. {...........................................................................}
  635. Function Maxi(a,b: Integer): Integer;
  636. Begin
  637.     Result := a * Ord(a >= b) + b * Ord(b > a);
  638. End;
  639. {...........................................................................}
  640.  
  641. {...........................................................................}
  642. Function Minf(a,b: Single): Single;
  643. Begin
  644.     Result := a * Ord(a <= b) + b * Ord(b < a);
  645. End;
  646. {...........................................................................}
  647.  
  648. {...........................................................................}
  649. Function Maxf(a,b: Single): Single;
  650. Begin
  651.     Result := a * Ord(a >= b) + b * Ord(b > a);
  652. End;
  653. {...........................................................................}
  654.  
  655. {...........................................................................}
  656. Procedure TGLContext.RenderPoint(Var v: TVertexInfo);
  657. Var
  658.     px,py       : Integer;
  659.     x,y         : Integer;
  660.     x1,y1,x2,y2 : Single;
  661.     dd          : Single;
  662.     r,g,b       : Byte;
  663.     cr,cg,cb    : Byte;
  664. Begin
  665.     px := Round(v.Vertex[0]);
  666.     py := Round(v.Vertex[1]);
  667.     FloatToByte(CurrentColor[0],r);
  668.     FloatToByte(CurrentColor[1],g);
  669.     FloatToByte(CurrentColor[2],b);
  670.     FloatToByte(ClearColor[0],cr);
  671.     FloatToByte(ClearColor[1],cg);
  672.     FloatToByte(ClearColor[2],cb);
  673.     If Abs(1 - PointSize) < cEpsilon Then
  674.     Begin
  675.         VideoBuffer[px + py * ViewPort[2]].r := r;
  676.         VideoBuffer[px + py * ViewPort[2]].g := g;
  677.         VideoBuffer[px + py * ViewPort[2]].b := b;
  678.         Exit;
  679.     End;
  680.     x1 := Maxf(0,v.Vertex[0] - PointSize);
  681.     x2 := Minf(ViewPort[2]-1,v.Vertex[0] + PointSize);
  682.     y1 := Maxf(0,v.Vertex[1] - PointSize);
  683.     y2 := Minf(ViewPort[3]-1,v.Vertex[1] + PointSize);
  684.     For y := Round(y1) To Round(y2) Do
  685.         For x := Round(x1) To Round(x2) Do
  686.         Begin
  687.             dd := Sqr(x - px) + Sqr(y - py);
  688.             If dd <= PointSize Then
  689.             Begin
  690.                 VideoBuffer[x + y * ViewPort[2]].r := r;
  691.                 VideoBuffer[x + y * ViewPort[2]].g := g;
  692.                 VideoBuffer[x + y * ViewPort[2]].b := b;
  693.             End
  694.             Else
  695.             Begin
  696.                 VideoBuffer[x + y * ViewPort[2]].r := cr;
  697.                 VideoBuffer[x + y * ViewPort[2]].g := cg;
  698.                 VideoBuffer[x + y * ViewPort[2]].b := cb;
  699.             End;
  700.         End;
  701. End;
  702. {...........................................................................}
  703.  
  704. {...........................................................................}
  705. Procedure TGLContext.RenderLine(Var v1,v2: TVertexInfo);
  706. Var
  707.     i, deltax, deltay, numpixels,
  708.     d, dinc1, dinc2,
  709.     x, xinc1, xinc2,
  710.     y, yinc1, yinc2 : integer;
  711.     addryinc1,addryinc2: Integer;
  712.     lineAddr : Integer;
  713.     x1,y1,x2,y2 : Integer;
  714.     r,g,b       : Byte;
  715. Begin
  716.     FloatToByte(CurrentColor[0],r);
  717.     FloatToByte(CurrentColor[1],g);
  718.     FloatToByte(CurrentColor[2],b);
  719.     x1 := Round(v1.Vertex[0]);
  720.     y1 := Round(v1.Vertex[1]);
  721.     x2 := Round(v2.Vertex[0]);
  722.     y2 := Round(v2.Vertex[1]);
  723.     // Calculate deltax and deltay for initialisation
  724.     deltax := abs(x2 - x1);
  725.     deltay := abs(y2 - y1);
  726.     // Initialize all vars based on which is the independent variable
  727.     If deltax >= deltay Then
  728.     Begin
  729.         // x is independent variable
  730.         numpixels := deltax + 1;
  731.         d := (2 * deltay) - deltax;
  732.         dinc1 := deltay Shl 1;
  733.         dinc2 := (deltay - deltax) Shl 1;
  734.         xinc1 := 1;
  735.         xinc2 := 1;
  736.         yinc1 := 0;
  737.         yinc2 := 1;
  738.     End
  739.     Else
  740.     Begin
  741.       // y is independent variable
  742.         numpixels := deltay + 1;
  743.         d := (2 * deltax) - deltay;
  744.         dinc1 := deltax Shl 1;
  745.         dinc2 := (deltax - deltay) Shl 1;
  746.         xinc1 := 0;
  747.         xinc2 := 1;
  748.         yinc1 := 1;
  749.         yinc2 := 1;
  750.     End;
  751.     // Make sure x and y move in the right directions
  752.     If x1 > x2 Then
  753.     Begin
  754.         xinc1 := - xinc1;
  755.         xinc2 := - xinc2;
  756.     End;
  757.     If y1 > y2 Then
  758.     Begin
  759.         yinc1 := - yinc1;
  760.         yinc2 := - yinc2;
  761.     End;
  762.     // Start drawing at
  763.     x := x1;
  764.     y := y1;
  765.     lineAddr := y * ViewPort[2];
  766.     addryinc1 := ViewPort[2] * yinc1;
  767.     addryinc2 := ViewPort[2] * yinc2;
  768.     // Draw the pixels
  769.     for i := 1 to numpixels do
  770.     Begin
  771.         VideoBuffer[lineAddr + x].r := r;
  772.         VideoBuffer[lineAddr + x].g := g;
  773.         VideoBuffer[lineAddr + x].b := b;
  774.         If d < 0 Then
  775.         Begin
  776.             d := d + dinc1;
  777.             x := x + xinc1;
  778. //            y := y + yinc1;
  779.             Inc(lineAddr,addryinc1);
  780.         End
  781.         Else
  782.         Begin
  783.             d := d + dinc2;
  784.             x := x + xinc2;
  785. //            y := y + yinc2;
  786.             Inc(lineAddr,addryinc2);
  787.         End;
  788.     End;
  789. End;
  790. {...........................................................................}
  791.  
  792. {...........................................................................}
  793. Procedure TGLContext.RenderTriangle(Var v1,v2,v3: TVertexInfo);
  794. Begin
  795.     RenderLine(v1,v2);
  796.     RenderLine(v2,v3);
  797.     RenderLine(v3,v1);
  798. End;
  799. {...........................................................................}
  800.  
  801. {...........................................................................}
  802. Procedure TGLContext.ProcessPoints;
  803. Var
  804.     i  : Integer;
  805.     vi : TVertexInfo;
  806.     v  : TVector4f;
  807. Begin
  808.     For i := 1 To VertexCount Do
  809.     Begin
  810.         vi := Vertices[i];
  811.         v  := vi.Vertex;
  812.         TransformObjectCoordsToEyeCoords(v[0],v[1],v[2],v[3],v[0],v[1],v[2],v[3]);
  813.         TransformEyeCoordsToClipCoords  (v[0],v[1],v[2],v[3],v[0],v[1],v[2],v[3]);
  814.  
  815.         If ClipCoordIsVisible(v) Then
  816.         Begin
  817.             TransformClipCoordsToNDCoords  (v[0],v[1],v[2],v[3],v[0],v[1],v[2],v[3]);
  818.             TransformNDCoordsToWindowCoords(v[0],v[1],v[2],vi.Vertex[0],vi.Vertex[1],vi.Vertex[2]);
  819.             RenderPoint(vi);
  820.         End;
  821.     End;
  822. End;
  823. {...........................................................................}
  824.  
  825. {...........................................................................}
  826. Procedure TGLContext.ProcessLines;
  827. Var
  828.     i   : Integer;
  829.     vi1 : TVertexInfo;
  830.     vi2 : TVertexInfo;
  831.     v1   : TVector4f;
  832.     v2   : TVector4f;
  833. Begin
  834.     VertexCount := (VertexCount Div 2) * 2;
  835.     i := 1;
  836.     While i < VertexCount Do
  837.     Begin
  838.         vi1 := Vertices[i + 0];
  839.         vi2 := Vertices[i + 1];
  840.         v1  := vi1.Vertex;
  841.         v2  := vi2.Vertex;
  842.         TransformObjectCoordsToEyeCoords(v1[0],v1[1],v1[2],v1[3],v1[0],v1[1],v1[2],v1[3]);
  843.         TransformEyeCoordsToClipCoords  (v1[0],v1[1],v1[2],v1[3],v1[0],v1[1],v1[2],v1[3]);
  844.  
  845.         TransformObjectCoordsToEyeCoords(v2[0],v2[1],v2[2],v2[3],v2[0],v2[1],v2[2],v2[3]);
  846.         TransformEyeCoordsToClipCoords  (v2[0],v2[1],v2[2],v2[3],v2[0],v2[1],v2[2],v2[3]);
  847.  
  848.         If ClipCoordIsVisible(v1) And
  849.            ClipCoordIsVisible(v2) Then
  850.         Begin
  851.             TransformClipCoordsToNDCoords  (v1[0],v1[1],v1[2],v1[3],v1[0],v1[1],v1[2],v1[3]);
  852.             TransformNDCoordsToWindowCoords(v1[0],v1[1],v1[2],vi1.Vertex[0],vi1.Vertex[1],vi1.Vertex[2]);
  853.  
  854.             TransformClipCoordsToNDCoords  (v2[0],v2[1],v2[2],v2[3],v2[0],v2[1],v2[2],v2[3]);
  855.             TransformNDCoordsToWindowCoords(v2[0],v2[1],v2[2],vi2.Vertex[0],vi2.Vertex[1],vi2.Vertex[2]);
  856.             RenderLine(vi1,vi2);
  857.         End;
  858.         Inc(i,2);
  859.     End;
  860. End;
  861. {...........................................................................}
  862.  
  863. {...........................................................................}
  864. Procedure TGLContext.ProcessLineStrip;
  865. Begin
  866. End;
  867. {...........................................................................}
  868.  
  869. {...........................................................................}
  870. Procedure TGLContext.ProcessLineLoop;
  871. Begin
  872. End;
  873. {...........................................................................}
  874.  
  875. {...........................................................................}
  876. Function  TGLContext.BackFaceCullTriangle(Var v1,v2,v3: TVector4f): Boolean;
  877. Var
  878.     d1: TVector4f;
  879.     d2: TVector4f;
  880. Begin
  881.     d1[0] := v3[0] - v1[0];
  882.     d1[1] := v3[1] - v1[1];
  883.     d2[0] := v3[0] - v2[0];
  884.     d2[1] := v3[1] - v2[1];
  885.     Result := (d1[0] * d2[1]) - (d1[1] * d2[0]) > 0;
  886. End;
  887. {...........................................................................}
  888.  
  889. {...........................................................................}
  890. Procedure TGLContext.ProcessTriangles;
  891. Var
  892.     i   : Integer;
  893.     vi1 : TVertexInfo;
  894.     vi2 : TVertexInfo;
  895.     vi3 : TVertexInfo;
  896.     v1   : TVector4f;
  897.     v2   : TVector4f;
  898.     v3   : TVector4f;
  899. Begin
  900.     VertexCount := (VertexCount Div 3) * 3;
  901.     i := 1;
  902.     While i < VertexCount Do
  903.     Begin
  904.         vi1 := Vertices[i + 0];
  905.         vi2 := Vertices[i + 1];
  906.         vi3 := Vertices[i + 2];
  907.         v1  := vi1.Vertex;
  908.         v2  := vi2.Vertex;
  909.         v3  := vi3.Vertex;
  910.         TransformObjectCoordsToEyeCoords(v1[0],v1[1],v1[2],v1[3],v1[0],v1[1],v1[2],v1[3]);
  911.         TransformEyeCoordsToClipCoords  (v1[0],v1[1],v1[2],v1[3],v1[0],v1[1],v1[2],v1[3]);
  912.  
  913.         TransformObjectCoordsToEyeCoords(v2[0],v2[1],v2[2],v2[3],v2[0],v2[1],v2[2],v2[3]);
  914.         TransformEyeCoordsToClipCoords  (v2[0],v2[1],v2[2],v2[3],v2[0],v2[1],v2[2],v2[3]);
  915.  
  916.         TransformObjectCoordsToEyeCoords(v3[0],v3[1],v3[2],v3[3],v3[0],v3[1],v3[2],v3[3]);
  917.         TransformEyeCoordsToClipCoords  (v3[0],v3[1],v3[2],v3[3],v3[0],v3[1],v3[2],v3[3]);
  918.  
  919.         If ClipCoordIsVisible(v1) And
  920.            ClipCoordIsVisible(v2) And
  921.            ClipCoordIsVisible(v3) Then
  922.         Begin
  923.             TransformClipCoordsToNDCoords  (v1[0],v1[1],v1[2],v1[3],v1[0],v1[1],v1[2],v1[3]);
  924.             TransformNDCoordsToWindowCoords(v1[0],v1[1],v1[2],vi1.Vertex[0],vi1.Vertex[1],vi1.Vertex[2]);
  925.  
  926.             TransformClipCoordsToNDCoords  (v2[0],v2[1],v2[2],v2[3],v2[0],v2[1],v2[2],v2[3]);
  927.             TransformNDCoordsToWindowCoords(v2[0],v2[1],v2[2],vi2.Vertex[0],vi2.Vertex[1],vi2.Vertex[2]);
  928.  
  929.             TransformClipCoordsToNDCoords  (v3[0],v3[1],v3[2],v3[3],v3[0],v3[1],v3[2],v3[3]);
  930.             TransformNDCoordsToWindowCoords(v3[0],v3[1],v3[2],vi3.Vertex[0],vi3.Vertex[1],vi3.Vertex[2]);
  931.             If Not BackFaceCullTriangle(v1,v2,v3) Then
  932.                 RenderTriangle(vi1,vi2,vi3);
  933.         End;
  934.         Inc(i,3);
  935.     End;
  936. End;
  937. {...........................................................................}
  938.  
  939. {...........................................................................}
  940. Procedure TGLContext.ProcessTriangleStrip;
  941. Begin
  942. End;
  943. {...........................................................................}
  944.  
  945. {...........................................................................}
  946. Procedure TGLContext.ProcessTriangleFan;
  947. Begin
  948. End;
  949. {...........................................................................}
  950.  
  951. {...........................................................................}
  952. Procedure TGLContext.ProcessQuads;
  953. Begin
  954. End;
  955. {...........................................................................}
  956.  
  957. {...........................................................................}
  958. Procedure TGLContext.ProcessQuadStrip;
  959. Begin
  960. End;
  961. {...........................................................................}
  962.  
  963. {...........................................................................}
  964. Procedure TGLContext.ProcessPolygon;
  965. Begin
  966. End;
  967. {...........................................................................}
  968.  
  969. {...........................................................................}
  970. Procedure TGLContext.ProcessVertices;
  971. Begin
  972.     If VertexCount <= 0 Then Exit;
  973. //    ClearPrimitives;
  974.     Case Vertices[1].Mode Of
  975.         GL_POINTS         : ProcessPoints;
  976.         GL_LINES          : ProcessLines;
  977.         GL_LINE_STRIP     : ProcessLineStrip;
  978.         GL_LINE_LOOP      : ProcessLineLoop;
  979.         GL_TRIANGLES      : ProcessTriangles;
  980.         GL_TRIANGLE_STRIP : ProcessTriangleStrip;
  981.         GL_TRIANGLE_FAN   : ProcessTriangleFan;
  982.         GL_QUADS          : ProcessQuads;
  983.         GL_QUAD_STRIP     : ProcessQuadStrip;
  984.         GL_POLYGON        : ProcessPolygon;
  985.     End;
  986. End;
  987. {...........................................................................}
  988.  
  989. {...........................................................................}
  990. Procedure TGLContext.AddVertexf(x,y,z,w: TGLfloat);
  991. Begin
  992.     If VertexMode = GL_NONE Then Exit;
  993.     If VertexCount >= cMaxVertices Then Exit;
  994.     Inc(VertexCount);
  995.     Vertices[VertexCount].Mode   := VertexMode;
  996.     Vertices[VertexCount].Color  := CurrentColor;
  997.     Vertices[VertexCount].Normal := CurrentNormal;
  998.     Vertices[VertexCount].Vertex := Vector4f(x,y,z,w);
  999. End;
  1000. {...........................................................................}
  1001.  
  1002. {...........................................................................}
  1003. Procedure TGLContext.glTranslatef(x,y,z: TGLfloat);
  1004. Var
  1005.     t : TMatrix44f;
  1006.     r : TMatrix44f;
  1007.     m : PMatrix44f;
  1008. Begin
  1009.     t := cIdentityMatrix;
  1010.     t[0,3] := x;
  1011.     t[1,3] := y;
  1012.     t[2,3] := z;
  1013.     m := GetMatrix;
  1014.     MultMatrixf(m,@t,@r);
  1015.     m^ := r;
  1016. End;
  1017. {...........................................................................}
  1018.  
  1019. {...........................................................................}
  1020. Procedure TGLContext.glScalef(x,y,z: TGLfloat);
  1021. Var
  1022.     t : TMatrix44f;
  1023.     r : TMatrix44f;
  1024.     m : PMatrix44f;
  1025. Begin
  1026.     t := cIdentityMatrix;
  1027.     t[0,0] := x;
  1028.     t[1,1] := y;
  1029.     t[2,2] := z;
  1030.     m := GetMatrix;
  1031.     MultMatrixf(m,@t,@r);
  1032.     m^ := r;
  1033. End;
  1034. {...........................................................................}
  1035.  
  1036. {...........................................................................}
  1037. Procedure TGLContext.glRotatef(angle,x,y,z: TGLfloat);
  1038. Var
  1039.     t : TMatrix44f;
  1040.     r : TMatrix44f;
  1041.     m : PMatrix44f;
  1042. Begin
  1043.     Rotatef(angle,x,y,z,@t);
  1044.     m := GetMatrix;
  1045.     MultMatrixf(m,@t,@r);
  1046.     m^ := r;
  1047. End;
  1048. {...........................................................................}
  1049.  
  1050. {...........................................................................}
  1051. Procedure TGLContext.glLoadIdentity;
  1052. Var
  1053.     m : PMatrix44f;
  1054. Begin
  1055.     m  := GetMatrix;
  1056.     m^ := cIdentityMatrix;
  1057. End;
  1058. {...........................................................................}
  1059.  
  1060. {...........................................................................}
  1061. Procedure TGLContext.glPushMatrix;
  1062. Var
  1063.     m : PMatrix44f;
  1064. Begin
  1065.     If MatrixStacks[MatrixMode].Index >= MatrixStacks[MatrixMode].MaxIndex Then Exit;
  1066.     m := @MatrixStacks[MatrixMode].Stack[MatrixStacks[MatrixMode].Index];
  1067.     Inc(MatrixStacks[MatrixMode].Index);
  1068.     MatrixStacks[MatrixMode].Stack[MatrixStacks[MatrixMode].Index] := m^;
  1069. End;
  1070. {...........................................................................}
  1071.  
  1072. {...........................................................................}
  1073. Procedure TGLContext.glPopMatrix;
  1074. Begin
  1075.     If MatrixStacks[MatrixMode].Index < 2 Then Exit;
  1076.     Dec(MatrixStacks[MatrixMode].Index);
  1077. End;
  1078. {...........................................................................}
  1079.  
  1080. {...........................................................................}
  1081. Procedure TGLContext.glMatrixMode(mode: TGLenum);
  1082. Begin
  1083.     If Not(mode In[GL_MODELVIEW,GL_PROJECTION,GL_TEXTURE,GL_COLOR]) Then Exit;
  1084.     MatrixMode := mode;
  1085. End;
  1086. {...........................................................................}
  1087.  
  1088. {...........................................................................}
  1089. Procedure TGLContext.glMultMatrixf(t: Pointer);
  1090. Var
  1091.     r : TMatrix44f;
  1092.     m : PMatrix44f;
  1093. Begin
  1094.     m := GetMatrix;
  1095.     MultMatrixf(m,t,@r);
  1096.     m^ := r;
  1097. End;
  1098. {...........................................................................}
  1099.  
  1100. {...........................................................................}
  1101. Procedure TGLContext.glBegin(mode: TGLenum);
  1102. Begin
  1103.     If VertexMode <> GL_NONE Then Exit;
  1104.     If mode > GL_POLYGON Then Exit;
  1105.     VertexMode := mode;
  1106. End;
  1107. {...........................................................................}
  1108.  
  1109. {...........................................................................}
  1110. Procedure TGLContext.glEnd;
  1111. Begin
  1112.     If VertexMode = GL_NONE Then Exit;
  1113.     VertexMode := GL_NONE;
  1114.     ProcessVertices;
  1115.     VertexCount := 0;
  1116. End;
  1117. {...........................................................................}
  1118.  
  1119. {...........................................................................}
  1120. Procedure TGLContext.glClearColor(red,green,blue,alpha: TGLfloat);
  1121. Begin
  1122.     ClampValuef(red   ,0,1);
  1123.     ClampValuef(green ,0,1);
  1124.     ClampValuef(blue  ,0,1);
  1125.     ClampValuef(alpha ,0,1);
  1126.     ClearColor := Vector4f(red,green,blue,alpha);
  1127. End;
  1128. {...........................................................................}
  1129.  
  1130. {...........................................................................}
  1131. Procedure TGLContext.ClearVideoBuffer;
  1132. Var
  1133.     r    : Byte;
  1134.     g    : Byte;
  1135.     b    : Byte;
  1136.     a    : Byte;
  1137.     x,y  : Integer;
  1138.     addr : Integer;
  1139. Begin
  1140.     FloatToByte(ClearColor[0],r);
  1141.     FloatToByte(ClearColor[1],g);
  1142.     FloatToByte(ClearColor[2],b);
  1143.     FloatToByte(ClearColor[3],a);
  1144.  
  1145.     addr := 0;
  1146.     For y := 0 To ViewPort[3] - 1 Do
  1147.     Begin
  1148.         For x := 0 To ViewPort[2] - 1 Do
  1149.         Begin
  1150.             VideoBuffer[addr + x].r := r;
  1151.             VideoBuffer[addr + x].g := g;
  1152.             VideoBuffer[addr + x].b := b;
  1153.         End;
  1154.         Inc(addr,ViewPort[2]);
  1155.     End;
  1156. End;
  1157. {...........................................................................}
  1158.  
  1159. {...........................................................................}
  1160. Procedure TGLContext.ClearDepthBuffer;
  1161. Begin
  1162. End;
  1163. {...........................................................................}
  1164.  
  1165. {...........................................................................}
  1166. Procedure TGLContext.glClear(mask: TGLbitfield);
  1167. Begin
  1168.     If mask And GL_COLOR_BUFFER_BIT <> 0 Then
  1169.         ClearVideoBuffer;
  1170.     If mask And GL_DEPTH_BUFFER_BIT <> 0 Then
  1171.         ClearDepthBuffer;
  1172. End;
  1173. {...........................................................................}
  1174.  
  1175. {...........................................................................}
  1176. Procedure TGLContext.glVertex3fv(v: PGLfloat);
  1177. Begin
  1178.     AddVertexf(PVector3f(v)^[0],PVector3f(v)^[1],PVector3f(v)^[2],1);
  1179. End;
  1180. {...........................................................................}
  1181.  
  1182. {...........................................................................}
  1183. Procedure TGLContext.glVertex4f(x,y,z,w: TGLfloat);
  1184. Begin
  1185.     AddVertexf(x,y,z,w);
  1186. End;
  1187. {...........................................................................}
  1188.  
  1189. {...........................................................................}
  1190. Procedure TGLContext.glColor3f(red,green,blue: TGLfloat);
  1191. Begin
  1192.     ClampValuef(red   ,0,1);
  1193.     ClampValuef(green ,0,1);
  1194.     ClampValuef(blue  ,0,1);
  1195.     CurrentColor[0] := red;
  1196.     CurrentColor[1] := green;
  1197.     CurrentColor[2] := blue;
  1198. End;
  1199. {...........................................................................}
  1200.  
  1201. {...........................................................................}
  1202. Procedure TGLContext.glColor4f(red,green,blue,alpha: TGLfloat);
  1203. Begin
  1204.     ClampValuef(red   ,0,1);
  1205.     ClampValuef(green ,0,1);
  1206.     ClampValuef(blue  ,0,1);
  1207.     ClampValuef(alpha ,0,1);
  1208.     CurrentColor := Vector4f(red,green,blue,alpha);
  1209. End;
  1210. {...........................................................................}
  1211.  
  1212. {...........................................................................}
  1213. Procedure TGLContext.gluPerspective(fovy,aspect,zNear,zFar: TGLfloat);
  1214. Var
  1215.     r : TMatrix44f;
  1216.     t : TMatrix44f;
  1217.     m : PMatrix44f;
  1218. Begin
  1219.     m := GetMatrix;
  1220.     Perspective(fovy,aspect,zNear,zFar,@t);
  1221.     MultMatrixf(m,@t,@r);
  1222.     m^ := r;
  1223. End;
  1224. {...........................................................................}
  1225.  
  1226. {...........................................................................}
  1227. Procedure TGLContext.ResizeBuffers;
  1228. Begin
  1229.     SetLength(VideoBuffer,ViewPort[2] * ViewPort[3]);
  1230.     SetLength(DepthBuffer,ViewPort[2] * ViewPort[3]);
  1231. End;
  1232. {...........................................................................}
  1233.  
  1234. {...........................................................................}
  1235. Procedure TGLContext.glViewPort(x,y,width,height: TGLint);
  1236. Begin
  1237.     ViewPort[0] := x;
  1238.     ViewPort[1] := y;
  1239.     ViewPort[2] := width;
  1240.     ViewPort[3] := height;
  1241.     ResizeBuffers;
  1242. End;
  1243. {...........................................................................}
  1244.  
  1245. {...........................................................................}
  1246. Procedure TGLContext.Init;
  1247. Begin
  1248.     zbNear       := 0;
  1249.     zbFar        := 1;
  1250.     glClearColor(0,0,0,1);
  1251.     glColor4f(1,1,1,1);
  1252.     PointSize := 1;
  1253.     VertexMode   := GL_NONE;
  1254.     VertexCount  := 0;
  1255.  
  1256.     MatrixStacks[GL_MODELVIEW].Index    := 1;
  1257.     MatrixStacks[GL_MODELVIEW].MaxIndex := cModelViewMatrixStackSize;
  1258.     MatrixStacks[GL_MODELVIEW].Stack[1] := cIdentityMatrix;
  1259.  
  1260.     MatrixStacks[GL_PROJECTION].Index    := 1;
  1261.     MatrixStacks[GL_PROJECTION].MaxIndex := cProjectionMatrixStackSize;
  1262.     MatrixStacks[GL_PROJECTION].Stack[1] := cIdentityMatrix;
  1263.  
  1264.     MatrixStacks[GL_TEXTURE].Index    := 1;
  1265.     MatrixStacks[GL_TEXTURE].MaxIndex := cTextureMatrixStackSize;
  1266.     MatrixStacks[GL_TEXTURE].Stack[1] := cIdentityMatrix;
  1267.  
  1268.     MatrixStacks[GL_COLOR].Index    := 1;
  1269.     MatrixStacks[GL_COLOR].MaxIndex := cColorMatrixStackSize;
  1270.     MatrixStacks[GL_COLOR].Stack[1] := cIdentityMatrix;
  1271.  
  1272.     glMatrixMode(GL_PROJECTION);
  1273.     glLoadIdentity;
  1274.     glViewPort(0,0,320,240);
  1275.     gluPerspective(45,ViewPort[2]/ViewPort[3],1,100);
  1276.     glMatrixMode(GL_MODELVIEW);
  1277.     glLoadIdentity;
  1278. End;
  1279. {...........................................................................}
  1280.  
  1281. {...........................................................................}
  1282. Procedure TGLContext.Close;
  1283. Begin
  1284. End;
  1285. {...........................................................................}
  1286.  
  1287. {...........................................................................}
  1288. Procedure InitGL;
  1289. Begin
  1290.     GLContext := TGLContext.Create;
  1291. End;
  1292. {...........................................................................}
  1293.  
  1294. {...........................................................................}
  1295. Procedure CloseGL;
  1296. Begin
  1297.     GLContext.Free;
  1298. End;
  1299. {...........................................................................}
  1300.  
  1301. {...........................................................................}
  1302.  
  1303. Initialization
  1304.     InitGL;
  1305. Finalization
  1306.     CloseGL;
  1307. End.