Mirror the viewport

Hi there,

I want to create a F1 racing game. However, my rear-view mirror shows everything backwards. Is there a way to mirror a viewport? Since I couldn’t find anything helpful about this, I would be grateful for any help.

regards
Didi

Oh, interesting application. I understand you’re using a 2nd viewport to display the mirror, as discussed on Multiple viewports to display one world | Manual | Castle Game Engine . Indeed, by default it does not act like a mirror. If you point the camera backward, it will be like as if you had rotated your head 180 degees and looked back. Which is not what mirror image looks like.

Luckily we have a solution to this: Screen Effects | Castle Game Engine . You can easily make a screen effect that inverts the viewport contents horizontally.

You can take a look at examples/screen_effects_demo/ how to create such effects. This is what I would do to mirror a viewport:

function CreateMirrorEffect: TScreenEffectNode;
var
  ComposedShader: TComposedShaderNode;
  FragmentShader: TShaderPartNode;
begin
  FragmentShader := TShaderPartNode.Create;
  FragmentShader.ShaderType := stFragment;
  FragmentShader.Contents :=
    'void main (void)' + NL +
    '{' + NL +
    '  vec2 mirror_position = vec2(1.0 - screenf_01_position.x, screenf_01_position.y);' + NL +
    '  gl_FragColor = screenf_01_get_color(mirror_position);' + NL +
    '}';

  ComposedShader := TComposedShaderNode.Create;
  ComposedShader.SetParts([FragmentShader]);

  Result := TScreenEffectNode.Create;
  Result.SetShaders([ComposedShader]);
end;

Then in some initialization code (like in start Start method) attach this effect to your viewport with a mirror, like

MirrorViewport.AddScreenEffect(CreateMirrorEffect);

Let me know if this is clear enough :slight_smile: I have not tested the above code in practice, I just wrote it following my examples/screen_effects_demo/code/gameinitialize.pas example and knowing our capabilities for screen effects, documented on Screen Effects | Castle Game Engine .

Hi Michalis,

thank you for this fast reply. It works, but I do not really understand the function. Is this C?
I will study the document of Screen Effects.
By the way, CGE is a very interesting engine. It is my first contact to 3D and maybe I will come up with some other problems in the future.

regards
Didi

It’s GLSL, “OpenGL Shading Languge”, see OpenGL Shading Language - OpenGL Wiki . Such programs are called “shaders” and are executed by your graphic card. You can use GLSL in a few places in CGE, like

You’re welcome to ask if you need anything! :slight_smile: