Skip to content

Commit

Permalink
Somewhat final/finished version
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiden committed Nov 16, 2023
1 parent e18b403 commit 1137397
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
21 changes: 14 additions & 7 deletions build/shaders/default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in float layer;
out vec4 fragColor;

uniform int layers;
uniform float time;

float hash(uint x)
{
Expand All @@ -19,10 +20,10 @@ void main()
{
const vec3 light_direction = vec3(0.58, 0.58, 0.58);
const float density = 150.0;
const float attenuation = 1.3;
const float attenuation = 1.2;
const float ambient_bias = 0.1;
const float thickness = 1.8;
const float thickness = 2.0;

// @Note: Layer height
float h = layer / layers;

Expand All @@ -38,14 +39,20 @@ void main()
// @Note: Not square-like shell texturing
vec2 local_uv = fract(new_uv) * 2.0 - 1.0;
float distance_from_center = length(local_uv);
float circ_radius = (thickness * (rand - h));

if (distance_from_center > (thickness * (rand - h)) && layer > 0.0) {
if (distance_from_center > circ_radius && layer > 0.0) {
discard;
}

// @Note: 'Ambient occlusion' (tm)
float ambient = pow(h, attenuation) + ambient_bias;
const vec3 color_top = vec3(0.20, 0.90, 0.0);
const vec3 color_bot = vec3(0.42, 0.87, 0.0);
vec3 color = mix(color_bot, color_top, h);

float ambient = pow(h, attenuation);
ambient += ambient_bias;
ambient = clamp(ambient, 0.0, 1.0);
vec3 ambient_light = mix(vec3(0.41, 0.84, 0.66), vec3(1.0), ambient);

fragColor = vec4(vec3(0.0, 0.98, 0.0) * ambient * vec3(light), 1.0);
fragColor = vec4(color * ambient_light * light, 1.0);
}
11 changes: 6 additions & 5 deletions build/shaders/default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ uniform mat4 projection;
uniform mat4 view;
uniform int layers;
uniform float fur_length;
uniform float time;

out vec3 normal;
out vec2 uv;
out float layer;

void main()
{
const float curvature = 6.9;
const float curvature = 5.5;

float shell_height = current_layer / layers;
shell_height = pow(shell_height, fur_length);

vec3 pos = ver_pos;

// @Note: This is black magic
// ~Shadow wizard money gang
vec3 pos = ver_pos;
pos += ver_norm * fur_length * shell_height;

float k = pow(shell_height, curvature);
pos += vec3(0.0, -1.0, 0.0) * 0.1 * k;
pos += vec3(0.0, -0.1, 0.0) * k;

gl_Position = projection * view * vec4(pos, 1.0);

Expand Down
2 changes: 1 addition & 1 deletion build/shaders/skybox.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ out vec4 fragColor;
void main()
{
fragColor = texture(skybox, uv);
}
}
24 changes: 14 additions & 10 deletions code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#define ZOOM_NEAR 1.2f
#define ZOOM_FAR 3.0f

#define ROTATION_BUMP 1.2f
#define ROTATION_DAMP 8.0f
#define ROTATION_BUMP 1.1f
#define ROTATION_DAMP 9.0f

struct Camera {
vec3 eye;
Expand All @@ -47,10 +47,11 @@ int main(int argc, char **argv)
float zoom_target = camera.eye[2];
vec2 view_angle = {0};
vec2 rotation_speed = {0};

mat4x4 view = {0};

unsigned int current_time = 0;
unsigned int previous_time = SDL_GetTicks();

bool should_quit = false;
while (!should_quit) {
current_time = SDL_GetTicks();
Expand Down Expand Up @@ -116,12 +117,8 @@ int main(int argc, char **argv)
}
}

mat4x4 view = {0};
mat4x4_identity(view);
mat4x4_look_at(view, camera.eye, camera.center, camera.up);
mat4x4_rotate_X(view, view, view_angle[1]);
mat4x4_rotate_Y(view, view, view_angle[0]);
render_update_camera(view);
render_update_time(SDL_GetTicks() / 1000.0f);

render_begin();

Expand All @@ -133,14 +130,21 @@ int main(int argc, char **argv)
}

render_end();


// @Note: Update camera values
vec2 speed_over_time = { rotation_speed[0] * dt, rotation_speed[1] * dt };
vec2_add(view_angle, view_angle, speed_over_time);
view_angle[1] = CLAMP(view_angle[1], -PI32/2.0f, PI32/2.0f);

move_towards(&camera.eye[2], zoom_target, dt, ZOOM_RATE);
move_towards(&rotation_speed[0], 0.0f, dt, ROTATION_DAMP);
move_towards(&rotation_speed[1], 0.0f, dt, ROTATION_DAMP);

// @Note: Update camera matrix
mat4x4_identity(view);
mat4x4_look_at(view, camera.eye, camera.center, camera.up);
mat4x4_rotate_X(view, view, view_angle[1]);
mat4x4_rotate_Y(view, view, view_angle[0]);

int wait_time = FRAME_MS - elapsed_time;
if (wait_time > 0 && wait_time < FRAME_MS) SDL_Delay(wait_time);
Expand Down
11 changes: 9 additions & 2 deletions code/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Render_Internal {
unsigned int shader_skybox;

unsigned int texture_skybox;

Vertex vertices[MAX_VERTICES];
size_t vertices_size;

Expand Down Expand Up @@ -177,7 +177,7 @@ internal void render_generate_sphere_data(float radius, unsigned int sectors, un

float x = rcos * cosf(sector_angle);
float y = rcos * sinf(sector_angle);

Vertex v = {
{ x, y, z }, // position
{ x * ilen, y * ilen, z * ilen }, // normal
Expand Down Expand Up @@ -382,6 +382,7 @@ void render_initialize_context()
// @Note: Indices are always the same as they depend only on stacks and segments
// meaning we can just precompute them and dynamically append sphere vertices later.
render_generate_sphere_indices(SPHERE_SECTOR_COUNT, SPHERE_STACK_COUNT);
render_generate_ssao();
render_generate_skybox();

render_init_sphere(&state.vao, &state.vbo, &state.ebo);
Expand Down Expand Up @@ -516,6 +517,12 @@ void render_error_screen()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void render_update_time(float time)
{
glUseProgram(state.shader_default);
glUniform1f(glGetUniformLocation(state.shader_default, "time"), time);
}

void render_update_camera(mat4x4 view)
{
glUseProgram(state.shader_default);
Expand Down
1 change: 1 addition & 0 deletions code/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void render_reload_shaders();
void render_sphere_controls();
void render_immediate_sphere();
void render_error_screen();
void render_update_time(float time);
void render_update_camera(mat4x4 view);
void render_resize_window(int width, int height);

Expand Down

0 comments on commit 1137397

Please sign in to comment.