Newbie: How to do very basic drawing only?

Hello,

I took notice about Castle Engine by this topic. I have a very similar problem because I have an application in which I use OpenGL for drawing. Additionally, GLUT is in use and because of its ancient age an lack of 64-bit support, I’m looking for an alternative.

I am sure that Castle will be capable to do what I want to do. Quite the contrary, as far as I have seen now, it’ll probably be overdimensioned. It seems to be very well documented, but due to the huge amount of features, it’s a bit hard to get started. I would appreciate if anyone could point me to a very basic tutorial so that I can do a few first steps.

What I want to do is using TCastleControl, i.e. my scene should only be one part of the app the rest of which is completely implemented with LCL. I’m happy that Castle allows this, and this point is why I can’t use SDL2.

I want to do only some basic drawings: lines, rectangles, hex-shapes, circles, etc. Additionally, text has to be printed. I do not want to (and cannot either) create some predefined scenes which will be loaded afterwards. As far as I have seen, using a designer seems to be the prefered way of using Castle.

I need to do everything at runtime, because the scene will be delivered from a server. Currently, I’m using OpenGL and have a model which is maintained in model coordinates so that I can display either the whole model or just a part of it, at any zoom level. I would like to keep this approach because it is working very well.

Everything is just 2D. No textures, shading, etc. required. But it need to be able to view the scene with at an arbitrary rotation angle and to show rotated text labels. If possible, text should be “anchored” in the model view so that it will be zoomed in and out with the rest of the drawings.

So my question is whether someone could show me how to start. I already tried by myself by I get access violations only because of missing intialization of components. What would be a most minimal example, say for properly initializing TCastleControl and drawing only a single line.

Thank you very much for any help.

Even if the model and everything else is 2D, OpenGL treats and handles those as 3D. So, in the end you will have to use “3D with 2D setup”. Using zooming and rotating lines and text labels only supports that: you can’t just use “drawing features” like TCanvas, but will need to create a set of 3D (setup as 2D) objects to add those elements and be able to manipulate them.

Check examples in examples\lazarus - this should help you to get started with using TCastleControl allowing to mix 3D content with 2D. Next example to try would be examples\lviewport_and_scenes\build_3d_object_by_code - it will show you how to construct simple 2D/3D primitives by code. Example in examples\viewport_and_scenes\detect_scene_hit will show how to attach a click event to your newly constructed objects if you need this. And finally you can see how to add/change 3D (or rotatable 2D) text in examples\localization\gettext.

I’m not sure if Castle Editor will help you with your task. As you design your UI in Lazarus/Delphi and don’t have any specific static UI in the 3D/2D view it won’t be of much use to you.

For the beginning overview of CGE in general, I’d definitely suggest to read the manual – Manual | Manual | Castle Game Engine :slight_smile:

To draw basic things in 2D, you can use routines mentioned on Custom drawn 2D controls: player HUD | Manual | Castle Game Engine . When using TCastleControl, you can just assign OnRender event and call there things like SomeFont.Print, DrawRectangle, use TDrawableImage, DrawPrimitive2D etc.

Using CGE like this, you don’t really use much of its features – you just draw 2D things, while CGE can do much more, including drawing 3D, realizing cameras and navigation and lighting, using physics, making interactive UI etc.

You can also construct scenes at runtime, so just because you receive the data from somewhere (like the Internet) – you can still load scenes, construct them. The way to build a scene using code is on Writing code to modify scenes and transformations | Manual | Castle Game Engine , with various examples in Pascal on Geometry3D component | Castle Game Engine and Geometry2D component | Castle Game Engine .

Using a designer, while advised and suitable for many normal projects, is definitely not necessary to use CGE. Everything that is rendered can be build and modified by code, if neeeded.

If you want to rotate stuff, most of the routines I mentioned above will no longer be applicable.

The solution is to render things by creating TCastleScene instances with the appropriate contents (lines, rectangles etc.) and add these TCastleScene instances to a viewport. You can then freely rotate and zoom in/out in your 2D world. See my links above.

You can also use e.g. TCastleText to display text that is actually in 3D and can be freely rotated.

Alternatively you could rotate / scale your 2D points yourself, before passing them to DrawPrimitive2D. E.g. you can rotate points with RotatePoint2D. But this will not rotate the text, to rotate a text you really need to place text within TCastleText or TCastleScene.

Without a testcase, I cannot say why you get access violations. We don’t require any special “intialization of components”.

The basic examples is really simple. Install castle_components package following Engine on a form (VCL, FMX, LCL) using TCastleControl | Manual | Castle Game Engine , drop TCastleControl on form, double-click in Object Inspector on OnRender and fill it with some content using routines I mentioned above.

Attaching an example that does even more than you asked, draws rectangle, text, and a line strip (2 connected line segments) :slight_smile: It’s really trivial, just this:

implementation

uses CastleRectangles, CastleColors, CastleGLUtils, CastleControls, CastleVectors;

{$R *.lfm}

{ TForm1 }

procedure TForm1.CastleControl1Render(Sender: TObject);
begin
  DrawRectangle(FloatRectangle(10, 10, 100, 100), Yellow);
  FallbackFont.Print(10, 10, Red, 'Some text');
  DrawPrimitive2D(pmLineStrip, [
    Vector2(50, 50),
    Vector2(150, 150),
    Vector2(250, 550)
  ], Blue);
end;

end.

But note that freely rotating it will require a bit different approach, as I mentioned above.

I would advise to go ahead with the manual and learn how to use TCastleScene and other components in the “normal” way, which means you write some code and you use CGE editor. Even if later you will not need the CGE editor, it is definitely easiest to “discover” first how some things work in CGE editor, and later use them from code.


laz_control_example.zip (106.6 KB)

1 Like

Thank you very much. I think this will help me starting.