Lighting is decent enough

main
_ 2020-01-13 04:07:56 +00:00
parent aa36fddb09
commit b82b39c8d2
2 changed files with 19 additions and 26 deletions

BIN
pumpking.iqm Normal file

Binary file not shown.

View File

@ -292,8 +292,7 @@ const VERT_SHADER_SRC: &str =
#define mediump
#define highp
#line 0
uniform highp mat4 uni_model;
uniform highp mat4 uni_viewproj;
uniform highp mat4 uni_mvp;
attribute highp vec4 attr_pos;
attribute mediump vec2 attr_uv;
@ -311,9 +310,7 @@ void main (void) {
//vary_color.xyz = attr_normal;
vary_normal = attr_normal;
vec4 world_pos = uni_model * attr_pos;
gl_Position = uni_viewproj * world_pos;
gl_Position = uni_mvp * attr_pos;
}";
const FRAG_SHADER_SRC: &str =
@ -324,7 +321,7 @@ const FRAG_SHADER_SRC: &str =
#line 0
//uniform lowp sampler2D uni_texture;
uniform lowp vec3 uni_albedo;
uniform lowp vec4 uni_object_space_light;
uniform lowp vec3 uni_object_space_light;
//varying lowp vec4 vary_color;
varying lowp vec3 vary_normal;
@ -335,7 +332,7 @@ void main (void) {
lowp vec3 normal = normalize (vary_normal);
//lowp vec3 albedo = pow (vec3 (255.0 / 255.0, 154.0 / 255.0, 0.0 / 255.0), vec3 (2.0));
lowp float diffuse_factor = dot (normal, uni_object_space_light.xyz);
lowp float diffuse_factor = dot (normal, uni_object_space_light);
lowp vec3 sun = max (diffuse_factor, 0.0) * vec3 (0.95, 0.9, 0.85);
lowp vec3 sky = (diffuse_factor * 0.45 + 0.55) * vec3 (0.05, 0.1, 0.15);
lowp vec3 diffuse_color = uni_albedo * (sun + sky);
@ -532,8 +529,7 @@ fn main () {
let shader_program = ShaderProgram::new (&vert_shader, &frag_shader).unwrap ();
let unis: HashMap <_, _> = vec! [
"model",
"viewproj",
"mvp",
"object_space_light",
"albedo",
].iter ()
@ -576,14 +572,6 @@ fn main () {
let model_data = load_small_file ("pumpking.iqm");
let model = IqmModel::from_slice (&model_data [..]);
let id_mat = Mat4::identity ();
let view_mat: Vec <f32> = vec! [
0.3125, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.3125, 0.0,
0.0, 0.0, 0.0, 1.0,
];
const FALSE_U8: u8 = 0;
unsafe {
@ -593,8 +581,6 @@ fn main () {
let num_coords = 3;
let stride = 4 * num_coords;
gl::UniformMatrix4fv (unis ["viewproj"], 1, FALSE_U8, &view_mat [0]);
}
let mut last_frame_time = Instant::now ();
@ -640,15 +626,22 @@ fn main () {
window.gl_make_current (&gl_ctx).unwrap ();
let longitude = (frame as f32).to_radians ();
let latitude = (frame as f32 / 5.0).to_radians ().sin () * 30.0f32.to_radians () - 120.0f32.to_radians ();
let latitude = (frame as f32 / 5.0).to_radians ().sin () * -40.0f32.to_radians () - 75.0f32.to_radians ();
let model_mat =
Mat4::from_rotation_x (latitude) *
Mat4::from_rotation_z (longitude) *
Mat4::from_translation (Vec3::from ((0.0, 0.0, -2.7 * 0.5)))
;
let light = Vec4::from ((0.0, 0.0, 0.0, 1.0));
let object_space_light = model_mat.inverse () * light;
let mvp_mat =
Mat4::perspective_rh_gl (30.0f32.to_radians (), 1280.0 / 720.0, 0.5, 500.0) *
Mat4::from_translation (Vec3::from ((0.0, 0.0, -8.0))) *
Mat4::from_rotation_x (latitude) *
model_mat
;
let light = Vec3::from ((2.0, 0.0, 5.0)).normalize ();
let object_space_light = model_mat.inverse () * Vec4::from ((light.x (), light.y (), light.z (), 0.0));
let orange = Vec3::from ((255.0 / 255.0, 154.0 / 255.0, 0.0 / 255.0));
let green = Vec3::from ((14.0 / 255.0, 127.0 / 255.0, 24.0 / 255.0));
@ -657,12 +650,12 @@ fn main () {
let green = green * green;
unsafe {
gl::ClearColor (1.0f32, 0.0f32, 1.0f32, 1.0f32);
gl::ClearColor (1.0f32, 1.0f32, 1.0f32, 1.0f32);
gl::Clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
gl::Disable (gl::CULL_FACE);
gl::UniformMatrix4fv (unis ["model"], 1, FALSE_U8, &model_mat as *const Mat4 as *const f32);
gl::Uniform4fv (unis ["object_space_light"], 1, &object_space_light as *const Vec4 as *const f32);
gl::UniformMatrix4fv (unis ["mvp"], 1, FALSE_U8, &mvp_mat as *const Mat4 as *const f32);
gl::Uniform3fv (unis ["object_space_light"], 1, &object_space_light as *const Vec4 as *const f32);
gl::Uniform3fv (unis ["albedo"], 1, &orange as *const Vec3 as *const f32);