From: Bernie Innocenti Date: Sun, 11 Jun 2017 01:09:50 +0000 (-0400) Subject: Light fading in the distance on the sea, add green ripples on the waves. X-Git-Url: https://codewiz.org/gitweb?p=mandelwow.git;a=commitdiff_plain;h=b505362bf6e4fc1bba7bb817a970462cf2e6011d Light fading in the distance on the sea, add green ripples on the waves. --- diff --git a/Cargo.toml b/Cargo.toml index 2b04e63..f3f65b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,14 +14,19 @@ glium = "0.16.0" glutin = "0.7.4" genmesh = "0.4.1" image = { version = "0.13.0", features = ["png_codec"] } -obj = { version = "0.5", features = ["usegenmesh"] } libxm = "1.0.0" +obj = { version = "0.5", features = ["usegenmesh"] } +rand = "*" sdl2 = "*" #[replace] #"glutin:0.7.4" = { path = "/home/bernie/src/glutin" } #"glium:0.16.0" = { path = "/home/bernie/src/glium" } +[target.armv7-linux-androideabi] +ar = "$ANDROID_NDK_HOME/arm/bin/arm-linux-androideabi-ar" +linker = "$ANDROID_NDK_HOME/arm/bin/arm-linux-androideabi-clang" + [lib] name = "mandelwow_lib" path = "lib.rs" diff --git a/main.rs b/main.rs index b492676..49b28b1 100644 --- a/main.rs +++ b/main.rs @@ -129,16 +129,13 @@ fn main() { let mut frame_cnt = 0; let mut last_report_time = Instant::now(); let mut last_report_frame_cnt = 0; + let mut accum_draw_time = Duration::new(0, 0); + let mut accum_idle_time = Duration::new(0, 0); set_main_loop_callback(|| { camera.update(); let perspview = camera.get_perspview(); - if !pause { - // Increment time - t += 0.01; - } - // Vary the wow factor to slice the Mandelwow along its 4th dimension. let wmin = -0.8; let wmax = 0.8; @@ -147,8 +144,10 @@ fn main() { //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos()); + let time_before_swap = Instant::now(); let mut frame = display.draw(); frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0); + let time_before_draw = Instant::now(); let rotation = Matrix4::from( Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t / 1.5)}); @@ -176,13 +175,16 @@ fn main() { let uniforms = uniform! { model: array4x4(model), perspview: perspview, + col: [0., (1. - wave).abs() * 0.5, wave.abs()], }; shaded_cube.draw(&mut frame, &uniforms); } } mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow); + frame.finish().unwrap(); + let time_after_draw = Instant::now(); for ev in display.poll_events() { match ev { @@ -223,14 +225,29 @@ fn main() { } } - frame_cnt += 1; let now = Instant::now(); - if now - last_report_time > Duration::from_secs(10) { - let fps = (frame_cnt - last_report_frame_cnt) as f32 / - (now - last_report_time).as_secs() as f32; - println!("fps={}", fps); + frame_cnt += 1; + accum_idle_time += time_before_draw - time_before_swap; + accum_draw_time += time_after_draw - time_before_draw; + if now - last_report_time > Duration::from_secs(5) { + fn millis(d : Duration) -> f32 { + d.as_secs() as f32 * 1e3 + d.subsec_nanos() as f32 / 1e6 + } + let frames_done = frame_cnt - last_report_frame_cnt; + let fps = frames_done as f32 / (now - last_report_time).as_secs() as f32; + let avg_draw_time = millis(accum_draw_time / frames_done); + let avg_idle_time = millis(accum_idle_time / frames_done); + println!("fps={:.1} draw={:.1}ms idle={:.1}ms", fps, avg_draw_time, avg_idle_time); + last_report_time = now; last_report_frame_cnt = frame_cnt; + accum_draw_time = Duration::new(0, 0); + accum_idle_time = Duration::new(0, 0); + } + + if !pause { + // Increment time + t += 0.01; } support::Action::Continue diff --git a/shaded.vert b/shaded.vert index 887d94c..e21f5fc 100644 --- a/shaded.vert +++ b/shaded.vert @@ -3,18 +3,22 @@ precision lowp float; in vec3 position; in vec3 normal; -out vec4 color; +out vec4 color; // Shaded color uniform mat4 model; uniform mat4 perspview; +uniform vec3 col; void main() { mat4 m = perspview * model; - vec3 dark = vec3(0.0, 0.0, 0.1); - vec3 bright = vec3(0.0, 0.0, 0.9); + vec3 dark = col * 0.1; vec3 u_light = vec3(-0.5, -0.7, -0.6); vec3 v_normal = transpose(inverse(mat3(model))) * normal; - float brightness = max(dot(normalize(v_normal), normalize(u_light)), 0.0); - color = vec4(mix(dark, bright, brightness), 1.0); + + float distance = model[3][2]; + float attenuation = 1. / (1. + distance * distance * 0.05); + float brightness = max(dot(normalize(v_normal), normalize(u_light)) * attenuation, 0.0); + color = vec4(mix(dark, col, brightness), 1.0); + gl_Position = m * vec4(position, 1.0); }