update: finally drawing a heightmap
parent
a94543b7f7
commit
0d4ba9455e
|
@ -28,6 +28,10 @@ use opengl_rust::{
|
||||||
timestep::TimeStep,
|
timestep::TimeStep,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GameState {
|
||||||
|
logic_frames: u64,
|
||||||
|
}
|
||||||
|
|
||||||
struct GraphicsContext {
|
struct GraphicsContext {
|
||||||
window: sdl2::video::Window,
|
window: sdl2::video::Window,
|
||||||
gl_ctx: sdl2::video::GLContext,
|
gl_ctx: sdl2::video::GLContext,
|
||||||
|
@ -59,14 +63,18 @@ impl ShaderLocations {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument (level = "trace", skip (ctx))]
|
#[instrument (level = "trace", skip (ctx, state))]
|
||||||
fn draw_graphics (ctx: &GraphicsContext) {
|
fn draw_graphics (ctx: &GraphicsContext, state: &GameState)
|
||||||
|
{
|
||||||
let shader_locs = &ctx.shader_locations;
|
let shader_locs = &ctx.shader_locations;
|
||||||
let attr_pos = shader_locs.attr_pos;
|
let attr_pos = shader_locs.attr_pos;
|
||||||
let attr_color = shader_locs.attr_color;
|
let attr_color = shader_locs.attr_color;
|
||||||
let uni_mvp = shader_locs.uni_mvp;
|
let uni_mvp = shader_locs.uni_mvp;
|
||||||
|
|
||||||
ctx.window.gl_make_current (&ctx.gl_ctx).unwrap ();
|
ctx.window.gl_make_current (&ctx.gl_ctx).unwrap ();
|
||||||
|
|
||||||
|
glezz::enable (gl::DEPTH_TEST);
|
||||||
|
|
||||||
glezz::clear_color (0.392f32, 0.710f32, 0.965f32, 1.0f32);
|
glezz::clear_color (0.392f32, 0.710f32, 0.965f32, 1.0f32);
|
||||||
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
|
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
|
||||||
|
|
||||||
|
@ -74,8 +82,13 @@ fn draw_graphics (ctx: &GraphicsContext) {
|
||||||
|
|
||||||
let proj_mat = Mat4::perspective_rh_gl (30.0f32.to_radians (), screen_size.0 / screen_size.1, 0.125, 200.0);
|
let proj_mat = Mat4::perspective_rh_gl (30.0f32.to_radians (), screen_size.0 / screen_size.1, 0.125, 200.0);
|
||||||
|
|
||||||
|
let spin_period = 360 * 4;
|
||||||
|
|
||||||
let view_mat = proj_mat *
|
let view_mat = proj_mat *
|
||||||
Mat4::from_translation (Vec3::from ((0.0, 0.0, -20.0)));
|
Mat4::from_translation (Vec3::from ((0.0, 0.0, -80.0))) *
|
||||||
|
Mat4::from_rotation_x (-60.0 * 3.14159 / 180.0) *
|
||||||
|
Mat4::from_rotation_z ((state.logic_frames % spin_period) as f32 * 3.1415926535 * 2.0 / spin_period as f32) *
|
||||||
|
Mat4::from_translation (Vec3::from ((-32.0, -32.0, -5.0)));
|
||||||
|
|
||||||
let mvp = view_mat;
|
let mvp = view_mat;
|
||||||
|
|
||||||
|
@ -141,6 +154,15 @@ fn main () -> anyhow::Result <()> {
|
||||||
let mut indexes = vec![];
|
let mut indexes = vec![];
|
||||||
let mut start_index = 0;
|
let mut start_index = 0;
|
||||||
|
|
||||||
|
let height_fn = |x, y| {
|
||||||
|
let x = x - 32.0;
|
||||||
|
let y = y - 32.0;
|
||||||
|
|
||||||
|
let d = x * x + y * y;
|
||||||
|
|
||||||
|
f32::max (0.0, 5.0 - d * 0.125)
|
||||||
|
};
|
||||||
|
|
||||||
for y in 0..64 {
|
for y in 0..64 {
|
||||||
for x in 0..64 {
|
for x in 0..64 {
|
||||||
let (r, g, b) = if (x + y) % 2 == 0 {
|
let (r, g, b) = if (x + y) % 2 == 0 {
|
||||||
|
@ -154,12 +176,20 @@ fn main () -> anyhow::Result <()> {
|
||||||
let y = y as f32;
|
let y = y as f32;
|
||||||
let i = start_index;
|
let i = start_index;
|
||||||
|
|
||||||
|
let xys = [
|
||||||
|
(x + 0.0, y + 0.0),
|
||||||
|
(x + 1.0, y + 0.0),
|
||||||
|
(x + 1.0, y + 1.0),
|
||||||
|
(x + 0.0, y + 1.0),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (x, y) in &xys {
|
||||||
vertexes.extend (&[
|
vertexes.extend (&[
|
||||||
x + 0.0, y + 0.0, 0.0, r, g, b,
|
*x, *y, height_fn (*x, *y),
|
||||||
x + 1.0, y + 0.0, 0.0, r, g, b,
|
r, g, b,
|
||||||
x + 1.0, y + 1.0, 0.0, r, g, b,
|
])
|
||||||
x + 0.0, y + 1.0, 0.0, r, g, b,
|
}
|
||||||
]);
|
|
||||||
indexes.extend (&[
|
indexes.extend (&[
|
||||||
i + 0, i + 1, i + 2,
|
i + 0, i + 1, i + 2,
|
||||||
i + 0, i + 2, i + 3,
|
i + 0, i + 2, i + 3,
|
||||||
|
@ -182,6 +212,10 @@ fn main () -> anyhow::Result <()> {
|
||||||
shader_locations,
|
shader_locations,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut game_state = GameState {
|
||||||
|
logic_frames: 0,
|
||||||
|
};
|
||||||
|
|
||||||
'running: loop {
|
'running: loop {
|
||||||
let frames_to_do = time_step.step ();
|
let frames_to_do = time_step.step ();
|
||||||
|
|
||||||
|
@ -198,10 +232,10 @@ fn main () -> anyhow::Result <()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _ in 0..frames_to_do {
|
for _ in 0..frames_to_do {
|
||||||
|
game_state.logic_frames += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_graphics (&graphics_ctx);
|
draw_graphics (&graphics_ctx, &game_state);
|
||||||
graphics_frames += 1;
|
graphics_frames += 1;
|
||||||
|
|
||||||
std::thread::sleep (Duration::from_millis (15));
|
std::thread::sleep (Duration::from_millis (15));
|
||||||
|
|
Loading…
Reference in New Issue