Menu

Newbie Q Castle2DControl

Anonymous
2018-03-05
2018-03-06
  • Anonymous

    Anonymous - 2018-03-05

    Hi All

    Thx in advance!

    I have a Lazarus Form with a Castle2DControl on it. I can draw an image to this and also move it. However, when it moves it does not erase the last one so I get an image trail?

    I tried using: Castle2DControl.Erasebackground(Form1.Canvas.Handle);

    but no joy?

    How to I erase so I do not get the trail?

    I am drawing the image in the controls onRender event and changing the position in the onUpdate event as per instructions on the web site > getting started.

     
  • EugeneLoza

    EugeneLoza - 2018-03-05

    The simplest way to clear the screen is

    uses
      ...
      CastleColors, CastleGLUtils;
    
    ...
    
    procedure TForm1.Castle2DControl1Render(Sender: TObject);
    begin
      RenderContext.Clear([cbColor], Black); //<---------- this will clear the 2DControl to "Black" color
      //(draw something)
    end;
    

    You may also specify the custom color to clear the screen, just use

    RenderContext.Clear([cbColor], Vector4(1,0.5,0,1));
    

    for orange color (also replace CastleColors with CastleVectors in your "uses" section)

     
  • Michalis Kamburelis

    Hi!

    The screen is not automatically cleared at the beginning under Castle2DControl, under the assumption that in some use-cases you want to see things underneath. You need to clear it yourself, various ways:

    • Call "RenderContext.Clear([cbColor], Black)" at the very beginning of OnRender. RenderContext is defined in CastleGLUtils unit, Black constant is from CastleColors, or you can construct any color by Vector4(...).

    • Make the 2D scene manager not transparent: Castle2DControl.SceneManager.Transparent := false.

    • Place TCastleSimpleBackground at the back, like Window.Controls.InsertBack(TCastleSimpleBackground.Create(Application)).

    • Place TCastleRectangleControl at the back, like this:

    R := TCastleRectangleControl.Create(Application);
    R.FullSize := true;
    R.Color := Black;
    Window.Controls.InsertBack(R);
    

    Note: do not use "EraseBackground(Form1.Canvas.Handle)". You cannot clear OpenGL context with EraseBackground.

     
  • Anonymous

    Anonymous - 2018-03-05

    Thank you all very very much.

     
  • Michalis Kamburelis

    As an addendum to this thread:

    I decided to fix it, that is: make the 2D scene manager non-transparent by default, so things would work out-of-the-box as expected. This makes 2D scene manager consistent with default (3D and 2D) scene manager in TCastleSceneManager, which is more intuitve, we had questions about it before already.

    So, now TCastle2DControl is by default not transparent.

    Internally, we have a new class TCastle2DSceneManager that is exactly like T2DSceneManager, but has Transparent = false by default (just like ancestor TCastleSceneManager). The class T2DSceneManager remains for compatibility with Transparent = true, but marked as deprecated. And TCastle2DControl uses new TCastle2DSceneManager.

    You can of course go back to previous situation by setting Transparent:=true, e.g. Castle2DControl.SceneManager.Transparent := true. Note that you can also control the color of background, e.g. Castle2DControl.SceneManager.BackgroundColor := Yellow;.

    You can test it using CGE from GitHub, https://github.com/castle-engine/castle-engine/ .

     

Anonymous
Anonymous

Add attachments
Cancel