R2 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
gbufferThe G-Buffer that will be sampled
viewportThe current viewport
view_raysThe current view rays
screen_positionThe 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
118  albedo,
119  emission,
120  specular_color,
121  specular_exponent,
122  normal,
123  position_eye,
124  screen_uv
125  );
126 
127  return surface;
128 }
sampler2D normal
The normal texture containing compressed normals (see R2_normalsDecompress).
sampler2D albedo
The albedo texture, with the emission level stored in the a component.
Reconstructed surface data, taken from the G-Buffer.
sampler2D specular
The specular texture, with the exponent stored in the a component.
sampler2D depth
The logarithmic depth texture (see R2_logDepthDecode).
float R2_logDepthDecode(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:72
vec3 R2_normalsDecompress(const vec2 n)
Definition: R2Normals.h:34
vec2 R2_viewportFragmentPositionToUV(const R2_viewport_t v, const vec2 f_pos)
Definition: R2Viewport.h:24
vec4 R2_positionReconstructFromEyeZ(const float eye_z, const vec2 uv, const R2_view_rays_t view_rays)