Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
renderpass [2020/01/09 14:07] – [Render-Pass Material] adminrenderpass [2020/01/09 14:07] – [Rendering Method] admin
Line 31: Line 31:
 In the next step you have to decide, if a **one-for-all material shader** should be used //or// the **object specific material shaders**. In the next step you have to decide, if a **one-for-all material shader** should be used //or// the **object specific material shaders**.
 Because sometimes you want the full scene just to be rendered from another perspective (like reflection) or you want to store a specific value (like the depth-value in shadow mapping) in the FBO. Because sometimes you want the full scene just to be rendered from another perspective (like reflection) or you want to store a specific value (like the depth-value in shadow mapping) in the FBO.
 +
 +===== Render-Pass Material =====
 +
 +To create an individual material shader for your render-pass you need to extend **TGorillaRenderPassMaterialSource** and **TGorillaRenderPassMaterial**.
 +
 +<file pascal>
 +uses
 +  FMX.Types3D,
 +  FMX.Materials,
 +  FMX.MaterialSources,
 +  Gorilla.Controller;
 +  
 +  TMyRenderPassMaterial = class(TGorillaRenderPassMaterial)
 +    protected
 +      procedure DoApply(const Context: TContext3D); override;
 +      procedure DoInitialize(); override;
 +    public
 +  end;
 +
 +  TMyRenderPassMaterialSource = class(TGorillaRenderPassMaterialSource)
 +    protected
 +      function CreateMaterial() : TMaterial; override;
 +    public
 +  end;
 +</file>
 +
 +Here you can have a look at this example of a render-pass material implementation.
 +
 +Notice: No multiple targets used here. We just write to default output buffer by gl_FragColor.
 +
 +<file pascal>
 +uses
 +  Gorilla.Context.GLES;
 +  
 +  ResourceString SOURCE_VS =
 +    'attribute vec3 a_Position;' +
 +    '' +
 +    'uniform mat4 _ModelViewProjMatrix;' +
 +    '' +
 +    'varying vec4 v_position;' +
 +    '' +
 +    'void main( void ){' +
 +    '  vec4 fvPosition = vec4(a_Position.x, a_Position.y, a_Position.z, 1.0);' +
 +    '  v_position = _ModelViewProjMatrix * fvPosition;' +
 +    '  gl_Position = v_position;' +
 +    '}';
 +
 +  ResourceString SOURCE_FS =
 +    'varying vec4 v_position;' +
 +    '' +
 +    'void main( void ){' +
 +    '  gl_FragColor = v_position;' +
 +    '}';
 +
 +procedure TMyRenderPassMaterial.DoInitialize();
 +var LOGLBytes : TArray<Byte>;
 +begin
 +  LOGLBytes := TEncoding.ASCII.GetBytes(SOURCE_VS);
 +  FVertexShader := TShaderManager.RegisterShaderFromData('MyRenderPass.fvs', TContextShaderKind.VertexShader, '', [
 +    TContextShaderSource.Create(TContextShaderArch.GLSL,
 +      LOGLBytes,
 +      [
 +      TContextShaderVariable.Create('ModelViewProjMatrix', TContextShaderVariableKind.Matrix, 0, 4)
 +      ]
 +    )
 +  ]);
 +
 +  LOGLBytes := TEncoding.ASCII.GetBytes(SOURCE_FS);
 +  FPixelShader := TShaderManager.RegisterShaderFromData('MyRenderPass.fps', TContextShaderKind.PixelShader, '', [
 +    TContextShaderSource.Create(TContextShaderArch.GLSL,
 +      LOGLBytes,
 +      []
 +    )
 +  ]);
 +end;
 +
 +{ TMyRenderPassMaterial }
 +
 +procedure TMyRenderPassMaterial.DoApply(const Context: TContext3D);
 +begin
 +  // activate shaders
 +  Context.SetShaders(FVertexShader, FPixelShader);
 +  // set model view projection matrix
 +  TCustomContextOpenGL(Context).SetShaderVariable('ModelViewProjMatrix', Context.CurrentModelViewProjectionMatrix, true);
 +end;
 +
 +{ TMyRenderPassMaterialSource }
 +
 +function TMyRenderPassMaterialSource.CreateMaterial() : TMaterial;
 +begin
 +  Result := TMyRenderPassMaterial.Create(Self);
 +end;
 +</file>
 +
 +You than have to overwrite the CreateMaterialSource() method and create your material source.
 +In case you'd like to use the element specific material shaders, return nil in the CreateMaterialSource() method.
 +
 +<file pascal>
 +function TMyRenderPass.CreateMaterialSource() : TGorillaRenderPassMaterialSource;
 +begin
 +  result := TMyRenderPassMaterialSource.Create(Self);
 +end;
 +</file>
  
 ===== Start / End ===== ===== Start / End =====