Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
VkCV Demos
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Vulkan2021
VkCV Demos
Commits
835b5897
Verified
Commit
835b5897
authored
2 years ago
by
Tobias Frisch
Browse files
Options
Downloads
Patches
Plain Diff
Adjust shaders for normal mapping
Signed-off-by:
Tobias Frisch
<
tfrisch@uni-koblenz.de
>
parent
ba77d9cc
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
demos/NormalMapping/shaders/NormalMapping.frag
+25
-8
25 additions, 8 deletions
demos/NormalMapping/shaders/NormalMapping.frag
demos/NormalMapping/shaders/NormalMapping.vert
+13
-33
13 additions, 33 deletions
demos/NormalMapping/shaders/NormalMapping.vert
with
38 additions
and
41 deletions
demos/NormalMapping/shaders/NormalMapping.frag
+
25
−
8
View file @
835b5897
...
...
@@ -33,20 +33,37 @@ layout(set=0, binding=4) uniform texturePropsBuffer {
int
useColorTexture
;
};
layout
(
location
=
0
)
in
vec3
passLightVector
;
layout
(
location
=
1
)
in
vec3
passEyeVector
;
layout
(
location
=
2
)
in
vec2
passUV
;
layout
(
location
=
0
)
in
vec3
passPosition
;
layout
(
location
=
1
)
in
vec3
passNormal
;
layout
(
location
=
2
)
in
vec3
passTangent
;
layout
(
location
=
3
)
in
vec2
passUV
;
layout
(
location
=
0
)
out
vec4
fragmentColor
;
void
main
(){
vec3
normal
=
normalize
(
passNormal
);
vec3
tangent
=
normalize
(
passTangent
);
vec3
bitangent
=
cross
(
tangent
,
normal
);
/***************** Diffuse ******************************************************/
// local normal, in tangent space
vec3
normal
=
texture
(
sampler2D
(
normalTexture
,
colorSampler
),
passUV
).
rgb
*
2
.
0
-
1
.
0
;
vec3
normalWeights
=
texture
(
sampler2D
(
normalTexture
,
colorSampler
),
passUV
).
rgb
*
2
.
0
-
1
.
0
;
mat3
tbn
=
transpose
(
mat3
(
tangent
,
bitangent
,
normal
));
normal
=
tbn
*
normalWeights
;
// direction of the light (from the fragment to the light) in tangent space
vec3
lightVector
=
normalize
(
passLightVector
);
vec3
lightVector
;
vec3
light_camcoord
=
(
light
.
pos
).
xyz
;
if
(
light
.
pos
.
w
>
0
.
001
f
)
{
lightVector
=
normalize
(
light_camcoord
-
passPosition
);
}
else
{
lightVector
=
normalize
(
light_camcoord
);
}
//compute the diffuse lighting factor
float
cos_phi
=
max
(
dot
(
normal
,
lightVector
),
0
);
...
...
@@ -56,7 +73,7 @@ void main(){
vec3
reflection
=
normalize
(
reflect
(
-
lightVector
,
normal
));
// eye vector in tangent space
vec3
eyeVector
=
normalize
(
pass
EyeVector
);
vec3
eyeVector
=
normalize
(
-
pass
Position
);
// compute the specular lighting factor
float
cos_psi_n
=
pow
(
max
(
dot
(
reflection
,
eyeVector
),
0
.
0
f
),
mat
.
shininess
);
...
...
This diff is collapsed.
Click to expand it.
demos/NormalMapping/shaders/NormalMapping.vert
+
13
−
33
View file @
835b5897
...
...
@@ -5,8 +5,6 @@ layout(location = 1) in vec3 normal;
layout
(
location
=
2
)
in
vec2
uv
;
layout
(
location
=
3
)
in
vec3
tangent
;
//vec3 tangent = vec3(0);
layout
(
set
=
0
,
binding
=
0
)
uniform
matrixBuffer
{
mat4
modelMatrix
;
mat4
viewMatrix
;
...
...
@@ -26,42 +24,24 @@ layout(set=0, binding=2) uniform lightBuffer {
vec3
lightAmbient
;
};
layout
(
location
=
0
)
out
vec3
passLightVector
;
layout
(
location
=
1
)
out
vec3
passEyeVector
;
layout
(
location
=
2
)
out
vec2
passUV
;
layout
(
location
=
0
)
out
vec3
passPosition
;
layout
(
location
=
1
)
out
vec3
passNormal
;
layout
(
location
=
2
)
out
vec3
passTangent
;
layout
(
location
=
3
)
out
vec2
passUV
;
void
main
(){
void
main
()
{
gl_Position
=
projectionMatrix
*
viewMatrix
*
modelMatrix
*
vec4
(
position
,
1
);
/*********** Calculate matrix that goes from camera space to tangent space **********/
mat4
normalMatrix
=
transpose
(
inverse
(
viewMatrix
*
modelMatrix
));
vec3
normal_cs
=
normalize
((
normalMatrix
*
vec4
(
normal
,
0
)).
xyz
);
vec3
tangent_cs
=
normalize
((
normalMatrix
*
vec4
(
tangent
,
0
)).
xyz
);
vec3
bitangent_cs
=
cross
(
tangent_cs
,
normal_cs
);
mat3
tbn
=
transpose
(
mat3
(
tangent_cs
,
bitangent_cs
,
normal_cs
));
/*********** Calculate position in camera space *************************************/
vec3
position_cs
=
(
viewMatrix
*
modelMatrix
*
vec4
(
position
,
1
)).
xyz
;
/*********** Calculate light vector (tangent space) *********************************/
// light position in world coordinates
vec3
lightPosition_cs
=
(
light
.
pos
).
xyz
;
// calcluate vector that goes from the vertex to the light, in tangent space
passLightVector
=
tbn
*
normalize
(
lightPosition_cs
-
position_cs
);
/*********** Calculate eye vector (tangent space) **********************************/
// calculate eye vector in camera space
vec3
eye_cs
=
normalize
(
-
position_cs
);
//transform the position correctly into view space
//and pass it to the fragment shader
passPosition
=
(
viewMatrix
*
modelMatrix
*
vec4
(
position
,
1
)).
xyz
;
// calculate eye vector in tangent space
passEyeVector
=
tbn
*
eye_cs
;
//transform the normal and tangent correctly into view space
//and pass it to the fragment shader
mat3
normalMatrix
=
mat3
(
transpose
(
inverse
(
viewMatrix
*
modelMatrix
)));
passNormal
=
normalize
(
normalMatrix
*
normal
);
passTangent
=
normalize
(
normalMatrix
*
tangent
);
/*********** Pass uv of the vertex *************************************************/
// no special space for this one
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment