Cleanup: DRW: Renaming of glsl utility macros

This commit is contained in:
Clément Foucault 2019-05-09 15:31:54 +02:00
parent 196dbc0f31
commit fdddea676d
11 changed files with 45 additions and 22 deletions

View File

@ -63,12 +63,12 @@ void main()
viewNormal = mat3(ViewMatrix) * worldNormal;
#else
gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
viewPosition = (ModelViewMatrix * vec4(pos, 1.0)).xyz;
worldPosition = (ModelMatrix * vec4(pos, 1.0)).xyz;
worldPosition = point_object_to_world(pos);
viewPosition = point_world_to_view(worldPosition);
worldNormal = normalize(transform_normal_object_to_world(nor));
worldNormal = normalize(normal_object_to_world(nor));
/* No need to normalize since this is just a rotation. */
viewNormal = transform_normal_world_to_view(worldNormal);
viewNormal = normal_world_to_view(worldNormal);
#endif
/* Used for planar reflections */

View File

@ -24,9 +24,9 @@ void main()
viewPosition = (ModelViewMatrix * vec4(pos, 1.0)).xyz;
worldPosition = (ModelMatrix * vec4(pos, 1.0)).xyz;
worldNormal = normalize(transform_normal_object_to_world(nor));
worldNormal = normalize(normal_object_to_world(nor));
/* No need to normalize since this is just a rotation. */
viewNormal = transform_normal_world_to_view(worldNormal);
viewNormal = normal_world_to_view(worldNormal);
# ifdef USE_ATTR
pass_attr(pos);
# endif

View File

@ -88,7 +88,7 @@ void main()
#endif
#ifdef NORMAL_VIEWPORT_PASS_ENABLED
normal_viewport = transform_normal_object_to_view(nor);
normal_viewport = normal_object_to_view(nor);
# ifndef HAIR_SHADER
normal_viewport = normalize(normal_viewport);
# endif

View File

@ -35,7 +35,7 @@ void main()
/* TODO FIX: there is still a problem with this vector
* when the bone is scaled or in persp mode. But it's
* barelly visible at the outline corners. */
ssNor = normalize(transform_normal_world_to_view(normal_mat * snor).xy);
ssNor = normalize(normal_world_to_view(normal_mat * snor).xy);
ssPos = proj(pPos);

View File

@ -15,7 +15,7 @@ void main()
/* This is slow and run per vertex, but it's still faster than
* doing it per instance on CPU and sending it on via instance attribute. */
mat3 normal_mat = transpose(inverse(mat3(InstanceModelMatrix)));
vec3 normal = normalize(transform_normal_world_to_view(normal_mat * nor));
vec3 normal = normalize(normal_world_to_view(normal_mat * nor));
/* Do lighting at an angle to avoid flat shading on front facing bone. */
const vec3 light = vec3(0.1, 0.1, 0.8);

View File

@ -14,11 +14,34 @@ layout(std140) uniform viewBlock
vec4 clipPlanes[2];
};
/* Transform shortcuts. */
#define transform_normal_object_to_world(nor) (transpose(mat3(ModelMatrixInverse)) * nor)
#define transform_normal_world_to_object(nor) (transpose(mat3(ModelMatrix)) * nor)
#define transform_normal_world_to_view(nor) (transpose(mat3(ViewMatrixInverse)) * nor)
#define transform_normal_object_to_view(nor) \
(transpose(mat3(ViewMatrixInverse)) * (transpose(mat3(ModelMatrixInverse)) * nor))
#define transform_point_view_to_object(point) \
((ModelMatrixInverse * (ViewMatrixInverse * vec4(point, 1.0))).xyz)
/** Transform shortcuts. */
/* Rule of thumb: Try to reuse world positions and normals because converting though viewspace
* will always be decomposed in at least 2 matrix operation. */
/**
* Some clarification:
* Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix))
*
* But since it is slow to multiply matrices we decompose it. Decomposing
* inversion and transposition both invert the product order leaving us with
* the same original order:
* transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse)
*
* Knowing that the view matrix is orthogonal, the transpose is also the inverse.
* Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse.
* ViewMatrix * transpose(ModelMatrixInverse)
**/
#define normal_object_to_view(nor) (mat3(ViewMatrix) * normal_object_to_world(nor))
#define normal_object_to_world(nor) (transpose(mat3(ModelMatrixInverse)) * nor)
#define normal_world_to_object(nor) (transpose(mat3(ModelMatrix)) * nor)
#define normal_world_to_view(nor) (mat3(ViewMatrix) * nor)
#define point_object_to_ndc(point) (ViewProjectionMatrix * (ModelMatrix * vec4(pt, 1.0)))
#define point_object_to_view(point) ((ViewMatrix * (ModelMatrix * vec4(pt, 1.0))).xyz)
#define point_object_to_world(point) ((ModelMatrix * vec4(point, 1.0)).xyz)
#define point_view_to_ndc(point) (ProjectionMatrix * vec4(point, 1.0))
#define point_view_to_object(point) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(pt, 1.))).xyz)
#define point_view_to_world(point) ((ViewMatrixInverse * vec4(point, 1.0)).xyz)
#define point_world_to_ndc(point) (ViewProjectionMatrix * vec4(point, 1.0))
#define point_world_to_object(point) ((ModelMatrixInverse * vec4(point, 1.0)).xyz)
#define point_world_to_view(point) ((ViewMatrix * vec4(point, 1.0)).xyz)

View File

@ -77,7 +77,7 @@ void main()
#if !defined(FACE)
/* Facing based color blend */
vec4 vpos = ModelViewMatrix * vec4(pos, 1.0);
vec3 view_normal = normalize(transform_normal_object_to_view(vnor) + 1e-4);
vec3 view_normal = normalize(normal_object_to_view(vnor) + 1e-4);
vec3 view_vec = (ProjectionMatrix[3][3] == 0.0) ? normalize(vpos.xyz) : vec3(0.0, 0.0, 1.0);
float facing = dot(view_vec, view_normal);
facing = 1.0 - abs(facing) * 0.2;

View File

@ -26,7 +26,7 @@ flat out vec4 v2;
void main()
{
v1 = ModelViewProjectionMatrix * vec4(pos, 1.0);
vec3 n = normalize(transform_normal_object_to_view(nor));
vec3 n = normalize(normal_object_to_view(nor));
v2 = v1 + ProjectionMatrix * vec4(n * normalSize, 0.0);
#ifdef USE_WORLD_CLIP_PLANES
world_clip_planes_calc_clip_distance((ModelMatrix * vec4(pos, 1.0)).xyz);

View File

@ -39,7 +39,7 @@ void main()
vec4 wpos = ModelMatrix * vec4(pos, 1.0);
gl_Position = projmat * (ViewMatrix * wpos);
vec3 wnor = normalize(transform_normal_object_to_world(nor));
vec3 wnor = normalize(normal_object_to_world(nor));
facing_g = dot(wnor, ViewMatrixInverse[2].xyz);
edgeSharpness_g = get_edge_sharpness(wd);

View File

@ -2099,7 +2099,7 @@ void node_tex_coord(vec3 I,
out vec3 reflection)
{
generated = attr_orco;
normal = normalize(transform_normal_world_to_object(wN));
normal = normalize(normal_world_to_object(wN));
uv = attr_uv;
object = (obmatinv * (ViewMatrixInverse * vec4(I, 1.0))).xyz;
camera = vec3(I.xy, -I.z);

View File

@ -151,7 +151,7 @@ static int node_shader_gpu_tex_image(GPUMaterial *mat,
blend = GPU_uniform(&tex->projection_blend);
gpu_image = GPU_image(ima, iuser, isdata);
/* equivalent to transform_normal_world_to_object */
/* equivalent to normal_world_to_object */
GPU_link(mat, "normal_transform_transposed_m4v3", vnor, ob_mat, &norm);
GPU_link(
mat, gpu_node_name, *texco, norm, GPU_image(ima, iuser, isdata), &col1, &col2, &col3);