Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TSBK03 Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fabian Johansson
TSBK03 Project
Commits
b66bc83c
Commit
b66bc83c
authored
5 months ago
by
Martin Högstedt
Browse files
Options
Downloads
Patches
Plain Diff
finished with my balls
parent
7de2a348
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
shaders/waterfall.frag
+58
-14
58 additions, 14 deletions
shaders/waterfall.frag
src/waterfall.cpp
+3
-3
3 additions, 3 deletions
src/waterfall.cpp
with
61 additions
and
17 deletions
shaders/waterfall.frag
+
58
−
14
View file @
b66bc83c
...
...
@@ -2,13 +2,16 @@
const
float
step_size
=
0
.
1
;
const
int
NUMBER_OF_STEPS
=
48
;
const
float
MINIMUM_HIT_DISTANCE
=
0
.
01
;
const
int
NUMBER_OF_BALL_STEPS
=
16
;
const
float
START_STEP
=
0
.
1
/
30
.
0
;
// Half the size of the smallest ball radius
const
float
MINIMUM_HIT_DISTANCE
=
0
.
002
;
const
float
MAXIMUM_TRACE_DISTANCE
=
100
.
0
;
// Constants related to reflection/refraction
const
float
R0
=
0
.
04
;
const
float
WATER_REFRACTION
=
0
.
75
;
const
float
PI
=
3
.
1415926535897
f
;
const
int
REFRACTION_ITER
=
3
;
in
vec3
world_pos
;
...
...
@@ -41,7 +44,7 @@ float SDF(vec3 position) {
float
min_dist
=
distance_from_sphere
(
position
,
balls
[
0
]);
// TODO: iterate over uniform num_balls
for
(
int
i
=
1
;
i
<
10
0
;
++
i
)
{
for
(
int
i
=
1
;
i
<
3
0
;
++
i
)
{
float
dist
=
distance_from_sphere
(
position
,
balls
[
i
]);
min_dist
=
opSmoothUnion
(
min_dist
,
dist
,
0
.
2
);
...
...
@@ -62,17 +65,50 @@ vec3 approximate_normal(vec3 position) {
}
float
fresnel
(
vec3
normal
,
vec3
view
)
{
float
cos_theta
=
dot
(
normal
,
-
view
);
float
x
=
1
.
0
-
cos_theta
;
return
mix
(
R0
,
1
.
0
,
x
*
x
*
x
*
x
*
x
);
float
cos_theta
=
dot
(
normal
,
-
view
);
float
x
=
1
.
0
-
cos_theta
;
return
mix
(
R0
,
1
.
0
,
x
*
x
*
x
*
x
*
x
);
}
vec2
sphere_uv
(
vec3
direction
)
{
float
u
=
0
.
5
+
atan
(
direction
.
z
,
direction
.
x
)
/
(
2
*
PI
);
float
v
=
0
.
5
+
asin
(
direction
.
y
)
/
PI
;
float
u
=
0
.
5
+
atan
(
direction
.
z
,
direction
.
x
)
/
(
2
*
PI
);
float
v
=
0
.
5
+
asin
(
direction
.
y
)
/
PI
;
return
vec2
(
u
,
v
);
return
vec2
(
u
,
v
);
}
vec3
reflection
(
vec3
pos
,
vec3
dir
)
{
// Calculate reflection from skybox
vec3
normal
=
normalize
(
approximate_normal
(
pos
));
vec3
reflected
=
reflect
(
dir
,
normal
);
vec3
sky_color
=
texture
(
sky
,
sphere_uv
(
reflected
)).
rgb
;
return
sky_color
;
}
// TODO: implement this
vec3
refraction
(
vec3
world_pos
,
vec3
dir
)
{
float
total_distance_traveled
=
START_STEP
;
for
(
int
i
=
0
;
i
<
NUMBER_OF_BALL_STEPS
;
++
i
)
{
vec3
current_position
=
world_pos
+
total_distance_traveled
*
dir
;
float
min_dist
=
-
SDF
(
current_position
);
// hit
if
(
min_dist
<
MINIMUM_HIT_DISTANCE
)
{
return
reflection
(
current_position
,
dir
);
}
// miss
if
(
total_distance_traveled
>
MAXIMUM_TRACE_DISTANCE
)
{
discard
;
}
total_distance_traveled
+=
min_dist
;
}
discard
;
}
vec3
ray_march
(
vec3
world_pos
,
vec3
dir
)
{
...
...
@@ -87,13 +123,21 @@ vec3 ray_march(vec3 world_pos, vec3 dir) {
if
(
min_dist
<
MINIMUM_HIT_DISTANCE
)
{
//const vec3 color = vec3(0.1, 0.1, 0.6);
vec3
normal
=
normalize
(
approximate_normal
(
current_position
));
vec3
reflected
=
reflect
(
dir
,
normal
);
vec3
sky_color
=
texture
(
sky
,
sphere_uv
(
reflected
)).
rgb
;
//vec3 direction_to_light = normalize(sun - current_position);
//float diffuse_intensity = max(0.0, dot(normal, direction_to_light));
vec3
reflec
=
reflection
(
current_position
,
dir
);
vec3
refrac
=
refraction
(
current_position
,
dir
);
vec3
normal
=
approximate_normal
(
current_position
);
// What percentage of out_Color should be from reflection/refraction
float
change_me
=
fresnel
(
normal
,
dir
);
// TODO: Add refraction from ball/skybox
vec3
refracted
=
refraction
(
current_position
,
dir
);
vec3
out_Color
=
mix
(
reflec
,
refrac
,
change_me
);
return
sky_color
;
return
out_Color
;
//return reflec;
}
// miss
...
...
This diff is collapsed.
Click to expand it.
src/waterfall.cpp
+
3
−
3
View file @
b66bc83c
...
...
@@ -85,8 +85,8 @@ float lerp(float a, float b, float t) { return a + t * (b - a); }
void
Waterfall
::
gen_balls
()
{
for
(
int
i
{
0
};
i
<
NUM_BALLS
;
++
i
)
{
// pseudo random value between 0.
01 and
0.
1
float
radius
=
((
rand
()
/
(
float
)
RAND_MAX
)
*
0.9
+
0.1
)
/
5
0.0
;
// pseudo random value between 0.
1/30.0 and 1.0/3
0.
0
float
radius
=
((
rand
()
/
(
float
)
RAND_MAX
)
*
0.9
+
0.1
)
/
1
0.0
;
// pseudo random value between min-radius and max-radius
float
pos_x
=
lerp
(
x
.
first
,
x
.
second
,
(
rand
()
/
(
float
)
RAND_MAX
));
...
...
@@ -95,7 +95,7 @@ void Waterfall::gen_balls() {
vec4
pos
=
vec4
(
pos_x
,
pos_y
,
pos_z
,
0
);
// pseudo random value between -0.001 and -0.01
float
velocity_y
=
-
((
rand
()
/
(
float
)
RAND_MAX
)
*
0.9
+
0.1
)
/
5
0.0
;
float
velocity_y
=
-
((
rand
()
/
(
float
)
RAND_MAX
)
*
0.9
+
0.1
)
/
10
0.0
;
vec4
velocity
=
vec4
(
0
,
velocity_y
,
0
,
0
);
balls
[
i
]
=
Waterfall
::
Ball
(
pos
,
radius
);
...
...
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