R2 Core Shaders

Data Structures | Functions
R2ReconstructedSurface.h File Reference

Surface data reconstructed from the G-Buffer. More...

`#include "R2GBufferInput.h"`
`#include "R2LogDepth.h"`
`#include "R2Normals.h"`
`#include "R2PositionReconstruction.h"`
`#include "R2Viewport.h"`
`#include "R2ViewRays.h"`
Include dependency graph for R2ReconstructedSurface.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures
struct | R2_reconstructed_surface_t
| Reconstructed surface data, taken from the G-Buffer. More...

## Functions
R2_reconstructed_surface_t | R2_deferredSurfaceReconstruct (const R2_gbuffer_input_t gbuffer, const R2_viewport_t viewport, const R2_view_rays_t view_rays, const float depth_coefficient, const vec2 screen_position)

## Detailed Description

Surface data reconstructed from the G-Buffer.

Definition in file R2ReconstructedSurface.h.

## Function Documentation

## § R2_deferredSurfaceReconstruct()

R2_reconstructed_surface_t R2_deferredSurfaceReconstruct | ( | const R2_gbuffer_input_t | gbuffer,
|  | const R2_viewport_t | viewport,
|  | const R2_view_rays_t | view_rays,
|  | const float | depth_coefficient,
|  | const vec2 | screen_position
| ) |  |

Perform a full reconstruction of the surface in the given G-Buffer.

The implementation of this function is affected by the following preprocessor defines:

If `R2_RECONSTRUCT_DIFFUSE_ONLY` is defined, the function only samples those parts of the geometry buffer that are required for diffuse lighting (specifically depth and normals).

If `R2_RECONSTRUCT_DIFFUSE_SPECULAR_ONLY` is defined, the function only samples those parts of the geometry buffer that are required for diffuse lighting (specifically depth, normals, and specular).

Otherwise, the surface is fully reconstructed.

Parameters

  gbuffer | The G-Buffer that will be sampled
  viewport | The current viewport
  view_rays | The current view rays
  screen_position | The current screen position (typically `gl_FragCoord.xy`).

Returns
  The fully reconstructed surface

Definition at line 60 of file R2ReconstructedSurface.h.

66 {
67 // Get the current screen coordinates in UV coordinate form
68 vec2 screen_uv =
69 R2_viewportFragmentPositionToUV (viewport, screen_position);
70
71 // Reconstruct the eye-space Z from the depth texture
72 float log_depth =
73 texture (gbuffer.depth, screen_uv).x;
74 float eye_z_positive =
75 R2_logDepthDecode (log_depth, depth_coefficient);
76 float eye_z =
77 -eye_z_positive;
78
79 // Reconstruct the full eye-space position
80 vec4 position_eye =
81 R2_positionReconstructFromEyeZ (eye_z, screen_uv, view_rays);
82
83 #if defined(R2_RECONSTRUCT_DIFFUSE_ONLY) || defined(R2_RECONSTRUCT_DIFFUSE_SPECULAR_ONLY)
84 vec3 albedo = vec3 (0.0);
85 float emission = 0.0;
86 #else
87 // Sample albedo/emission
88 vec4 albedo_raw =
89 texture (gbuffer.albedo, screen_uv);
90 vec3 albedo =
91 albedo_raw.rgb;
92 float emission =
93 albedo_raw.a;
94 #endif
95
96 // Sample normals
97 vec2 normal_compressed =
98 texture (gbuffer.normal, screen_uv).xy;
99 vec3 normal =
100 R2_normalsDecompress (normal_compressed);
101
102 #if !defined(R2_RECONSTRUCT_DIFFUSE_ONLY)
103 // Sample specular
104 vec4 specular_raw =
105 texture (gbuffer.specular, screen_uv);
106 vec3 specular_color =
107 specular_raw.rgb;
108 float specular_exponent =
109 specular_raw.a * 256.0;
110 #else
111 vec3 specular_color = vec3 (0.0);
112 float specular_exponent = 0.0;
113 #endif
114
115 // Construct surface data
116 R2_reconstructed_surface_t surface =
117 R2_reconstructed_surface_t (
118 albedo,
119 emission,
120 specular_color,
121 specular_exponent,
122 normal,
123 position_eye,
124 screen_uv
125 );
126
127 return surface;
128 }
R2_gbuffer_input_t::normal
sampler2D normal
The normal texture containing compressed normals (see R2_normalsDecompress).
Definition: R2GBufferInput.h:13
R2_gbuffer_input_t::albedo
sampler2D albedo
The albedo texture, with the emission level stored in the a component.
Definition: R2GBufferInput.h:11
R2_reconstructed_surface_t
Reconstructed surface data, taken from the G-Buffer.
Definition: R2ReconstructedSurface.h:16
R2_gbuffer_input_t::specular
sampler2D specular
The specular texture, with the exponent stored in the a component.
Definition: R2GBufferInput.h:15
R2_gbuffer_input_t::depth
sampler2D depth
The logarithmic depth texture (see R2_logDepthDecode).
Definition: R2GBufferInput.h:17
R2_logDepthDecode
float R2_logDepthDecode(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:72
R2_normalsDecompress
vec3 R2_normalsDecompress(const vec2 n)
Definition: R2Normals.h:34
R2_viewportFragmentPositionToUV
vec2 R2_viewportFragmentPositionToUV(const R2_viewport_t v, const vec2 f_pos)
Definition: R2Viewport.h:24
R2_positionReconstructFromEyeZ
vec4 R2_positionReconstructFromEyeZ(const float eye_z, const vec2 uv, const R2_view_rays_t view_rays)
Definition: R2PositionReconstruction.h:19