Mega Code Archive

 
Categories / Delphi / Graphic
 

MNCadBuilder RapidCAD with Delphi (OpenGL) (part II)

Title: MNCadBuilder - RapidCAD with Delphi (OpenGL) (part II) Question: Delphi and 3D CAD development - drawing user defines shapes Answer: 1. Please make sure you have read article "MNCadBuilder - RapidCAD with Delphi" to avoid installation or configuration problems. 2. Let's take a look at defining "user defined" shapes ----------------------------------------------------- Create a new project. Please make sure, that the root path of your MNCB installation folder is in the search path. Start "Project" - "Options" - select folder "Directories/Conditionals" and add the MNCB folder to the "Search path" settings. Now we drag and drop a TMNOglCanvas component on to the form. TMNOglCanvas is the central component for drawing and render output. When assigning a Window control to the property WinControl, an "OpenGL render context" is created based on the Window Handle of the Window control. So, let us assign the Form (Form1) to the WinControl. We also change the BackgroundColor of the TMNOglCanvas to get a more friendly background color (maybe clCream). Now we define a new type of Entity: TYPE TMyShape = class (TMNEntity) private protected procedure Draw; override; public end; Let's fill the Draw method with some life: procedure TMyShape.Draw; begin // Draw a box of length=3 at position TMyShape.Base MNDevice.DrawBox(NewPt (3,3,3)); // push the current ModelMatrix MNDevice.PushMatrix; // modify the ModelMatrix by a translation MNDevice.MultMatrix(TranslationToMatrix3D (NewPt(1,1,-1))); // draw some primitive MNDevice.DrawBox(NewPt (1,1,1)); // and pop the pushed Modelmatrix MNDevice.PopMatrix; MNDevice.PushMatrix; MNDevice.MultMatrix(TranslationToMatrix3D (NewPt(-1,1,1))); MNDevice.DrawBox(NewPt (1,1,1)); MNDevice.PopMatrix; MNDevice.PushMatrix; MNDevice.MultMatrix(TranslationToMatrix3D (NewPt(1,1,3))); MNDevice.DrawBox(NewPt (1,1,1)); MNDevice.PopMatrix; MNDevice.PushMatrix; MNDevice.MultMatrix(TranslationToMatrix3D (NewPt(3,1,1))); MNDevice.DrawBox(NewPt (1,1,1)); MNDevice.PopMatrix; MNDevice.PushMatrix; MNDevice.MultMatrix(TranslationToMatrix3D (NewPt(1,3,1))); MNDevice.DrawBox(NewPt (1,1,1)); MNDevice.PopMatrix; MNDevice.PushMatrix; MNDevice.MultMatrix(TranslationToMatrix3D (NewPt(1,-1,1))); MNDevice.DrawBox(NewPt (1,1,1)); MNDevice.PopMatrix; end; ... or with variables: procedure TMyShape.Draw; const ssize = 1.5; lsize = 4; var trans : TXYZ; begin // Draw a box of length=lsize at position Base MNDevice.DrawBox(NewPt (lsize,lsize,lsize)); MNDevice.PushMatrix; trans := NewPt(lsize/2-ssize/2,lsize/2-ssize/2,-ssize); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(-ssize,lsize/2-ssize/2,lsize/2-ssize/2); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(lsize/2-ssize/2,lsize/2-ssize/2,lsize); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(lsize,lsize/2-ssize/2,lsize/2-ssize/2); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(lsize/2-ssize/2,lsize,lsize/2-ssize/2); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(lsize/2-ssize/2,-ssize,lsize/2-ssize/2); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; end; We create the shape in the Form's OnShow method ... procedure TForm1.FormShow(Sender: TObject); begin // create a shape of type TMyShape MyShape := TMyShape.Create (NIL); // tell the Device to draw MyShape MNOglCanvas1._Entity := MyShape; end; .. and we dispose the object in the Form's Close method: procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin MyShape.Free; end; When we set the MNOglCanvas property Navigate to TRUE, we can change the view of the MNOglCanvas at runtime (means changing the projection matrix of the canvas). When start the project, we see the "user defined" shape. Because of Navigate is TRUE, we can change the view by pressing the left mouse button and moving the mouse. When you additionally press the [Ctrl] key you can translate the view in X and Y. If you want a perspective view, you can change the MNOglCanvas property FieldOfView to a value 0 (e.g. 20 degree). After restarting the project you see the object in a perspective view. And now we can also translate the view in Z axis by pressing the [Shift] key, left mouse button down and moving the mouse in Y+/- direction. This was an introduction of "user defined shapes" with the MNCadBuilder. Here you will find the source. Leopold Minikus DeveloCAD.com This is the source of the pas file: unit _Beginner2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, _MNDevice, _MNObjects3D, _MNPlatform, _MNOglCanvas, _MNMath, _MNXYZ; type TForm1 = class(TForm) MNOglCanvas1: TMNOglCanvas; procedure FormShow(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.dfm} TYPE TMyShape = class (TMNEntity) private protected procedure Draw; override; public end; VAR MyShape : TMyShape; procedure TForm1.FormShow(Sender: TObject); begin // create a shape of type TMyShape MyShape := TMyShape.Create (NIL); // tell the Device to draw MyShape MNOglCanvas1._Entity := MyShape; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin MyShape.Free; end; { TMyShape } procedure TMyShape.Draw; const ssize = 1.5; lsize = 4; var trans : TXYZ; begin // Draw a box of length=lsize at position Base MNDevice.DrawBox(NewPt (lsize,lsize,lsize)); MNDevice.PushMatrix; trans := NewPt(lsize/2-ssize/2,lsize/2-ssize/2,-ssize); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(-ssize,lsize/2-ssize/2,lsize/2-ssize/2); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(lsize/2-ssize/2,lsize/2-ssize/2,lsize); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(lsize,lsize/2-ssize/2,lsize/2-ssize/2); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(lsize/2-ssize/2,lsize,lsize/2-ssize/2); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; MNDevice.PushMatrix; trans := NewPt(lsize/2-ssize/2,-ssize,lsize/2-ssize/2); MNDevice.MultMatrix(TranslationToMatrix3D (trans)); MNDevice.DrawBox(NewPt (ssize,ssize,ssize)); MNDevice.PopMatrix; end; end. This is the source of the dfm file: object Form1: TForm1 Left = 350 Top = 335 Width = 736 Height = 464 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnClose = FormClose OnShow = FormShow PixelsPerInch = 120 TextHeight = 16 object MNOglCanvas1: TMNOglCanvas MNName = 'MNOglCanvas1' BackgroundColor = 15793151 Culling = True Cursor = CR_Cross DefaultLight = True DefaultPixelsPerUnit = 30.000000000000000000 FieldOfView = 20.000000000000000000 FontName = 'Modern' FontSize = 40 Lightning = True CanFocus = True WinControl = Owner ZoomFactor = 1.000000000000000000 DrawFocusRect = False Navigate = True EyeLight = True _ProjectionBase.BaseX.x = 1.000000000000000000 _ProjectionBase.BaseY.y = 1.000000000000000000 _ProjectionBase.BaseZ.z = 1.000000000000000000 PixelsPerUnit = 30.000000000000000000 Left = 64 Top = 56 end end