home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Delphi / TeeChartPro / TeeChart5Delphi5Eval.exe / %MAINDIR% / Examples / Features / Draw3D_Canvas.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-10  |  6.3 KB  |  237 lines

  1. unit Draw3D_Canvas;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   TeeProcs, TeeDraw3D, TeeComma, StdCtrls, ExtCtrls, TeeOpenGL;
  8.  
  9. type
  10.   TDraw3DCanvas = class(TForm)
  11.     Panel1: TPanel;
  12.     Memo1: TMemo;
  13.     CheckBox1: TCheckBox;
  14.     CheckBoxDrawAxes: TCheckBox;
  15.     CheckBoxDrawRoom: TCheckBox;
  16.     CheckBoxGradient: TCheckBox;
  17.     TeeCommander1: TTeeCommander;
  18.     Draw3D1: TDraw3D;
  19.     TeeOpenGL1: TTeeOpenGL;
  20.     CheckBox2: TCheckBox;
  21.     procedure Draw3D1Paint(Sender: TObject; const ARect: TRect);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure CheckBox1Click(Sender: TObject);
  24.     procedure CheckBoxDrawAxesClick(Sender: TObject);
  25.     procedure CheckBoxDrawRoomClick(Sender: TObject);
  26.     procedure CheckBoxGradientClick(Sender: TObject);
  27.     procedure CheckBox2Click(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.   public
  31.     { Public declarations }
  32.   end;
  33.  
  34. implementation
  35.  
  36. {$R *.DFM}
  37.  
  38. Uses TeCanvas;
  39.  
  40. { This small function returns a random color }
  41. Function RandomColor:TColor;
  42. begin
  43.   Case Random(8) of
  44.     0: result:=clRed;
  45.     1: result:=clYellow;
  46.     2: result:=clGreen;
  47.     3: result:=clBlue;
  48.     4: result:=clNavy;
  49.     5: result:=clAqua;
  50.     6: result:=clLime;
  51.   else
  52.     result:=clWhite;
  53.   end;
  54. end;
  55.  
  56. procedure TDraw3DCanvas.Draw3D1Paint(Sender: TObject; const ARect: TRect);
  57. Var CenterX     : Integer;
  58.     CenterY     : Integer;
  59.     CenterZ     : Integer;
  60.     WithShadows : Boolean;
  61.     t           : Integer;
  62.     tmpX        : Integer;
  63.     tmpY        : Integer;
  64. begin
  65.   { We should use this event to "Paint" shapes onto a Draw3D
  66.     component Canvas.
  67.   }
  68.  
  69.   { We can for example, use the "Center" position in the 3D space }
  70.   CenterX:=Draw3D1.ChartXCenter;
  71.   CenterY:=Draw3D1.ChartYCenter;
  72.   CenterZ:=0;
  73.  
  74.   { Determine if draw dark shape shadows or not }
  75.   WithShadows:=CheckBox1.Checked;
  76.  
  77.   { Using the Canvas, let's draw some shapes... }
  78.   With Draw3D1.Canvas do
  79.   begin
  80.  
  81.     { Draw a "room". Notice only 2D drawing methods are used here }
  82.     if CheckBoxDrawRoom.Checked and (not TeeOpenGL1.Active) then
  83.     begin
  84.       { use a gradient to simulate a "room" ... }
  85.       if CheckBoxGradient.Checked then
  86.          GradientFill(Draw3D1.ChartBounds,clWhite,clGray,gdFromCenter)
  87.       else
  88.       { or use polygons... }
  89.       begin
  90.         Pen.Style:=psSolid;
  91.         Pen.Color:=clBlack;
  92.         Brush.Color:=clWhite;
  93.         With Draw3D1.ChartBounds do
  94.         begin
  95.           tmpX:=(Right-Left) div 4;
  96.           tmpY:=(Bottom-Top) div 4;
  97.           Polygon([ Point(Left,Bottom),Point(Left,Top),
  98.                     Point(Left+tmpX,Top+tmpY),Point(Left+tmpX,Bottom-tmpY)]);
  99.           Polygon([ Point(Right,Bottom),Point(Right,Top),
  100.                     Point(Right-tmpX,Top+tmpY),Point(Right-tmpX,Bottom-tmpY)]);
  101.           Brush.Color:=clGray;
  102.           Rectangle(Left+tmpX,Top+tmpY,Right-tmpX,Bottom-tmpY);
  103.         end;
  104.       end;
  105.     end;
  106.  
  107.     if CheckBoxDrawAxes.Checked then
  108.     begin
  109.       { Draw the "axis" }
  110.       Pen.Color:=clNavy;
  111.       Pen.Style:=psDot;
  112.       BackMode:=cbmTransparent;
  113.  
  114.       VertLine3D(CenterX,CenterY-150,CenterY+150,CenterZ);
  115.       HorizLine3D(CenterX-150,CenterX+150,CenterY,CenterZ);
  116.       ZLine3D(CenterX,CenterY,CenterZ-150,CenterZ+150);
  117.     end;
  118.  
  119.     { Draw 100 small random dots, to simulate "dust" }
  120.     for t:=0 to 100 do
  121.         Pixels3D[200+Random(50),120+Random(100),20+Random(100)]:=RandomColor;
  122.  
  123.     { Draw several shapes, planes, pyramids, cubes, etc }
  124.     Pen.Color:=clBlack;
  125.     Pen.Style:=psSolid;
  126.  
  127.     Brush.Color:=clAqua;
  128.     Plane3D(Point(5,200),Point(90,240), -10,30);
  129.  
  130.     Brush.Color:=clYellow;
  131.     Pyramid(False,5,150,130,200,5,30,WithShadows);
  132.  
  133.     Brush.Color:=clWhite;
  134.     Cube(5,80,80,120,5,30,WithShadows);
  135.  
  136.     Brush.Color:=clLime;
  137.     Cylinder(False,150,50,190,200,5,50,WithShadows);
  138.  
  139.     { Draw a 3D cone }
  140.     Pen.Width:=1;
  141.     Pen.Color:=clBlue;
  142.     Brush.Color:=clWhite;
  143.     Cone(True,CenterX-50,20,CenterX,50,20,80,WithShadows,0);
  144.  
  145.     Brush.Color:=clRed;
  146.     EllipseWithZ(40,100,80,140,-20);
  147.  
  148.     Brush.Color:=clBlue;
  149.     Pen.Color:=clYellow;
  150.     Cube(CenterX+130,CenterX+170,120,190,70,100,WithShadows);
  151.  
  152.     Brush.Color:=clRed;
  153.     Pen.Color:=clWhite;
  154.     Cube(CenterX+30,CenterX+70,120,190,70,100,WithShadows);
  155.  
  156.     Brush.Color:=clRed;
  157.     Pen.Color:=clYellow;
  158.     Cube(CenterX+130,CenterX+170,120,190,10,30,WithShadows);
  159.  
  160.     Brush.Color:=clBlue;
  161.     Pen.Color:=clWhite;
  162.     Cube(CenterX+30,CenterX+70,120,190,10,30,WithShadows);
  163.  
  164.     Pen.Style:=psClear;
  165.     Brush.Color:=clRed;
  166.     Cylinder(False,CenterX+30,70,CenterX+170,100,70,100,WithShadows);
  167.     Cylinder(False,CenterX+30,70,CenterX+170,100,10,40,WithShadows);
  168.     Pen.Style:=psSolid;
  169.  
  170.     { Draw a pyramid on top of the cylinder }
  171.     Brush.Color:=clWhite;
  172.     Pen.Color:=clSilver;
  173.     Pyramid(True,CenterX+30,40,CenterX+170,60,10,100,WithShadows);
  174.  
  175.     { Draw a black line on top of the pyramid }
  176.     Pen.Color:=clBlack;
  177.     Pen.Width:=2;
  178.     MoveTo3D( CenterX+100, 40, 45 );
  179.     LineTo3D( CenterX+100, 20, 45 );
  180.  
  181.     { Draw text at 3D xyz position, on top of the previous line }
  182.     Font.Color:=clBlack;
  183.     TextAlign:=ta_Center;
  184.     BackMode:=cbmTransparent;
  185.     TextOut3D(CenterX+100,0,45,'Hello!');
  186.   end;
  187. end;
  188.  
  189. procedure TDraw3DCanvas.FormCreate(Sender: TObject);
  190. begin
  191.   { Disable "ortho" perspective. We want full 3D }
  192.   Draw3D1.View3DOptions.Orthogonal:=False;
  193.  
  194.   { Hide the toolbar "normal" button }
  195.   TeeCommander1.ButtonNormal.Visible:=False;
  196.  
  197.   { ...and select the "rotate" button }
  198.   TeeCommander1.ButtonRotate.Down:=True;
  199.  
  200.   { Nice rotation parameters... }
  201.   With Draw3D1.View3DOptions do
  202.   begin
  203.     Rotation:=330;
  204.     Elevation:=325;
  205.   end;
  206. end;
  207.  
  208. procedure TDraw3DCanvas.CheckBox1Click(Sender: TObject);
  209. begin
  210.   Draw3D1.Repaint;
  211. end;
  212.  
  213. procedure TDraw3DCanvas.CheckBoxDrawAxesClick(Sender: TObject);
  214. begin
  215.   Draw3D1.Repaint;
  216. end;
  217.  
  218. procedure TDraw3DCanvas.CheckBoxDrawRoomClick(Sender: TObject);
  219. begin
  220.   Draw3D1.Repaint;
  221.   CheckBoxGradient.Enabled:=CheckBoxDrawRoom.Checked;
  222. end;
  223.  
  224. procedure TDraw3DCanvas.CheckBoxGradientClick(Sender: TObject);
  225. begin
  226.   Draw3D1.Repaint;
  227. end;
  228.  
  229. procedure TDraw3DCanvas.CheckBox2Click(Sender: TObject);
  230. begin
  231.   TeeOpenGL1.Active:=CheckBox2.Checked;
  232. end;
  233.  
  234. initialization
  235.   RegisterClass(TDraw3DCanvas);
  236. end.
  237.