update: played with lighting

main
_ 2020-12-25 11:58:56 -06:00
parent 0d4ba9455e
commit aadc08194f
2 changed files with 47 additions and 12 deletions

View File

@ -3,6 +3,7 @@
#define highp
#line 0
attribute highp vec4 attr_pos;
attribute lowp vec3 attr_normal;
attribute lowp vec3 attr_color;
uniform highp mat4 uni_mvp;
@ -10,6 +11,11 @@ uniform highp mat4 uni_mvp;
varying lowp vec3 vary_color;
void main (void) {
vary_color = attr_color;
vec3 sun_dir = normalize (vec3 (1.0, 1.0, 4.0));
vec3 sun_light = vec3 (1.0, 0.75, 0.5) * max (0.0, dot (attr_normal, sun_dir));
vec3 sky_light = vec3 (0.0, 0.25, 0.5) * (attr_normal.z * 0.5 + 0.5);
vary_color = sqrt (attr_color * (sun_light + sky_light));
gl_Position = uni_mvp * attr_pos;
}

View File

@ -45,6 +45,7 @@ struct GraphicsContext {
struct ShaderLocations {
attr_pos: u32,
attr_normal: u32,
attr_color: u32,
uni_mvp: i32,
}
@ -57,6 +58,7 @@ impl ShaderLocations {
Ok (Self {
attr_pos: attr ("attr_pos")?,
attr_normal: attr ("attr_normal")?,
attr_color: attr ("attr_color")?,
uni_mvp: uni ("uni_mvp")?,
})
@ -68,6 +70,7 @@ fn draw_graphics (ctx: &GraphicsContext, state: &GameState)
{
let shader_locs = &ctx.shader_locations;
let attr_pos = shader_locs.attr_pos;
let attr_normal = shader_locs.attr_normal;
let attr_color = shader_locs.attr_color;
let uni_mvp = shader_locs.uni_mvp;
@ -97,15 +100,17 @@ fn draw_graphics (ctx: &GraphicsContext, state: &GameState)
ctx.shader_program.use_program ();
glezz::enable_vertex_attrib_array (Some (attr_pos));
glezz::enable_vertex_attrib_array (Some (attr_color));
glezz::enable_vertex_attrib_array (Some (attr_normal));
ctx.vertex_buffer.bind ();
ctx.index_buffer.bind ();
unsafe {
let num_quads = 64 * 64;
let stride = 4 * 6;
let stride = 4 * 9;
gl::VertexAttribPointer (attr_pos, 3, gl::FLOAT, 0, stride, 0 as *const u8 as *const c_void);
gl::VertexAttribPointer (attr_color, 3, gl::FLOAT, 0, stride, (4 * 3) as *const u8 as *const c_void);
gl::VertexAttribPointer (attr_normal, 3, gl::FLOAT, 0, stride, (4 * 3) as *const u8 as *const c_void);
gl::VertexAttribPointer (attr_color, 3, gl::FLOAT, 0, stride, (4 * 6) as *const u8 as *const c_void);
gl::DrawRangeElements (gl::TRIANGLES, 0, num_quads * 4, num_quads as i32 * 6, gl::UNSIGNED_INT, 0 as *const u8 as *const c_void);
}
@ -154,22 +159,28 @@ fn main () -> anyhow::Result <()> {
let mut indexes = vec![];
let mut start_index = 0;
let height_fn = |x, y| {
let x = x - 32.0;
let y = y - 32.0;
let bump_at = |bump_x, bump_y, x, y| {
let x = x - bump_x;
let y = y - bump_y;
let d = x * x + y * y;
f32::max (0.0, 5.0 - d * 0.125)
};
let height_fn = |(x, y)| {
bump_at (32.0, 32.0, x, y) +
bump_at (64.0, 32.0, x, y) +
bump_at (32.0, 48.0, x, y)
};
for y in 0..64 {
for x in 0..64 {
let (r, g, b) = if (x + y) % 2 == 0 {
(0.4, 0.4, 0.4)
}
else {
(0.6, 0.6, 0.6)
(0.5, 0.5, 0.5)
};
let x = x as f32;
@ -183,9 +194,27 @@ fn main () -> anyhow::Result <()> {
(x + 0.0, y + 1.0),
];
for (x, y) in &xys {
let zs = [
height_fn (xys [0]),
height_fn (xys [1]),
height_fn (xys [2]),
height_fn (xys [3]),
];
let slope_x = (zs [1] + zs [2]) - (zs [0] + zs [3]);
let slope_y = (zs [2] + zs [3]) - (zs [0] + zs [1]);
let tangent_x = Vec3::from ((1.0, 0.0, slope_x));
let tangent_y = Vec3::from ((0.0, 1.0, slope_y));
let normal = Vec3::cross (tangent_x, tangent_y)
.normalize ();
let (nx, ny, nz) = normal.into ();
for ((x, y), z) in xys.iter ().zip (&zs) {
vertexes.extend (&[
*x, *y, height_fn (*x, *y),
*x, *y, *z,
nx, ny, nz,
r, g, b,
])
}