mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-28 03:35:51 +03:00
changing glium example to draw a few colored triangles, not the teapot
This commit is contained in:
parent
87b885fcd7
commit
6064aa10af
@ -65,6 +65,5 @@
|
||||
|
||||
## Switch to OpenGL (for speed)
|
||||
|
||||
- simpler geometry, with color per object
|
||||
- render text
|
||||
- switch ezgui (could make it generic and have piston or glium support, but maybe not worth it)
|
||||
- render text
|
||||
|
@ -5,7 +5,5 @@ authors = ["Dustin Carlino <dabreegster@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
genmesh = "0.6.2"
|
||||
glium = "0.23.0"
|
||||
glutin = "0.19.0"
|
||||
obj = { version = "0.9", features = ["genmesh"] }
|
||||
|
@ -1,4 +1,5 @@
|
||||
use glutin;
|
||||
use std::f32;
|
||||
|
||||
pub struct CameraState {
|
||||
aspect_ratio: f32,
|
||||
@ -29,7 +30,7 @@ impl CameraState {
|
||||
}
|
||||
|
||||
pub fn get_perspective(&self) -> [[f32; 4]; 4] {
|
||||
let fov: f32 = 3.141592 / 2.0;
|
||||
let fov = f32::consts::PI / 2.0;
|
||||
let zfar = 1024.0;
|
||||
let znear = 0.1;
|
||||
|
||||
|
8
tmp_gfx/src/fragment.glsl
Normal file
8
tmp_gfx/src/fragment.glsl
Normal file
@ -0,0 +1,8 @@
|
||||
#version 140
|
||||
|
||||
in vec4 pass_color;
|
||||
out vec4 f_color;
|
||||
|
||||
void main() {
|
||||
f_color = pass_color;
|
||||
}
|
@ -1,11 +1,9 @@
|
||||
use glium::vertex::VertexBufferAny;
|
||||
use glium::{glutin, program, uniform, Surface};
|
||||
use glium::{glutin, implement_vertex, uniform, Surface};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::{env, process};
|
||||
|
||||
mod camera;
|
||||
mod support;
|
||||
|
||||
fn main() {
|
||||
// DPI is broken on my system; force the old behavior.
|
||||
@ -18,44 +16,62 @@ fn main() {
|
||||
let context = glutin::ContextBuilder::new().with_depth_buffer(24);
|
||||
let display = glium::Display::new(window, context, &events_loop).unwrap();
|
||||
|
||||
// TODO The geometry...
|
||||
let vertex_buffer = support::load_wavefront(&display, include_bytes!("teapot.obj"));
|
||||
let red_triangle = {
|
||||
let red = [1.0, 0.0, 0.0, 1.0];
|
||||
let vb = glium::VertexBuffer::new(
|
||||
&display,
|
||||
&[
|
||||
Vertex {
|
||||
position: [0.0, 0.5],
|
||||
color: red,
|
||||
},
|
||||
Vertex {
|
||||
position: [0.0, 0.0],
|
||||
color: red,
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.0],
|
||||
color: red,
|
||||
},
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
Drawable {
|
||||
vb,
|
||||
indices: glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
|
||||
}
|
||||
};
|
||||
let green_triangle = {
|
||||
let green = [0.0, 1.0, 0.0, 1.0];
|
||||
let vb = glium::VertexBuffer::new(
|
||||
&display,
|
||||
&[
|
||||
Vertex {
|
||||
position: [-0.5, 0.0],
|
||||
color: green,
|
||||
},
|
||||
Vertex {
|
||||
position: [0.0, 0.0],
|
||||
color: green,
|
||||
},
|
||||
Vertex {
|
||||
position: [0.0, -0.5],
|
||||
color: green,
|
||||
},
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
Drawable {
|
||||
vb,
|
||||
indices: glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
|
||||
}
|
||||
};
|
||||
|
||||
let program = program!(&display,
|
||||
140 => {
|
||||
vertex: "
|
||||
#version 140
|
||||
|
||||
uniform mat4 persp_matrix;
|
||||
uniform mat4 view_matrix;
|
||||
|
||||
in vec3 position;
|
||||
in vec3 normal;
|
||||
out vec3 v_position;
|
||||
out vec3 v_normal;
|
||||
|
||||
void main() {
|
||||
v_position = position;
|
||||
v_normal = normal;
|
||||
gl_Position = persp_matrix * view_matrix * vec4(v_position * 0.005, 1.0);
|
||||
}
|
||||
",
|
||||
|
||||
fragment: "
|
||||
#version 140
|
||||
|
||||
in vec3 v_normal;
|
||||
out vec4 f_color;
|
||||
|
||||
const vec3 LIGHT = vec3(-0.2, 0.8, 0.1);
|
||||
|
||||
void main() {
|
||||
float lum = max(dot(normalize(v_normal), normalize(LIGHT)), 0.0);
|
||||
vec3 color = (0.3 + 0.7 * lum) * vec3(1.0, 1.0, 1.0);
|
||||
f_color = vec4(color, 1.0);
|
||||
}
|
||||
",
|
||||
},
|
||||
let program = glium::Program::from_source(
|
||||
&display,
|
||||
include_str!("vertex.glsl"),
|
||||
include_str!("fragment.glsl"),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@ -65,14 +81,19 @@ fn main() {
|
||||
let mut previous_clock = Instant::now();
|
||||
|
||||
loop {
|
||||
draw(&camera, &display, &program, &vertex_buffer);
|
||||
draw(
|
||||
&camera,
|
||||
&display,
|
||||
&program,
|
||||
vec![&red_triangle, &green_triangle],
|
||||
);
|
||||
handle_events(&mut camera, &mut events_loop);
|
||||
|
||||
let now = Instant::now();
|
||||
accumulator += now - previous_clock;
|
||||
previous_clock = now;
|
||||
|
||||
let fixed_time_stamp = Duration::new(0, 16666667);
|
||||
let fixed_time_stamp = Duration::new(0, 16_666_667);
|
||||
while accumulator >= fixed_time_stamp {
|
||||
accumulator -= fixed_time_stamp;
|
||||
// TODO send off an update event
|
||||
@ -86,7 +107,7 @@ fn draw(
|
||||
camera: &camera::CameraState,
|
||||
display: &glium::Display,
|
||||
program: &glium::Program,
|
||||
vertex_buffer: &VertexBufferAny,
|
||||
stuff: Vec<&Drawable>,
|
||||
) {
|
||||
let uniforms = uniform! {
|
||||
persp_matrix: camera.get_perspective(),
|
||||
@ -103,34 +124,51 @@ fn draw(
|
||||
};
|
||||
|
||||
let mut target = display.draw();
|
||||
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
|
||||
target
|
||||
.draw(
|
||||
vertex_buffer,
|
||||
&glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
|
||||
&program,
|
||||
&uniforms,
|
||||
¶ms,
|
||||
)
|
||||
.unwrap();
|
||||
target.clear_color_and_depth((1.0, 1.0, 1.0, 0.0), 1.0);
|
||||
for triangle in stuff {
|
||||
target
|
||||
.draw(
|
||||
&triangle.vb,
|
||||
&triangle.indices,
|
||||
&program,
|
||||
&uniforms,
|
||||
¶ms,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
target.finish().unwrap();
|
||||
}
|
||||
|
||||
fn handle_events(camera: &mut camera::CameraState, events_loop: &mut glutin::EventsLoop) {
|
||||
events_loop.poll_events(|event| match event {
|
||||
glutin::Event::WindowEvent { event, .. } => match event {
|
||||
glutin::WindowEvent::CloseRequested => {
|
||||
process::exit(0);
|
||||
}
|
||||
glutin::WindowEvent::KeyboardInput { input, .. } => {
|
||||
if input.virtual_keycode == Some(glutin::VirtualKeyCode::Escape) {
|
||||
events_loop.poll_events(|event| {
|
||||
if let glutin::Event::WindowEvent { event, .. } = event {
|
||||
match event {
|
||||
glutin::WindowEvent::CloseRequested => {
|
||||
process::exit(0);
|
||||
}
|
||||
glutin::WindowEvent::KeyboardInput { input, .. } => {
|
||||
if input.virtual_keycode == Some(glutin::VirtualKeyCode::Escape) {
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
camera.process_input(input);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
camera.process_input(input);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct Vertex {
|
||||
position: [f32; 2],
|
||||
// TODO Maybe pass color as a uniform instead
|
||||
color: [f32; 4],
|
||||
}
|
||||
|
||||
implement_vertex!(Vertex, position, color);
|
||||
|
||||
struct Drawable {
|
||||
vb: glium::VertexBuffer<Vertex>,
|
||||
indices: glium::index::NoIndices,
|
||||
}
|
||||
|
@ -1,53 +0,0 @@
|
||||
use genmesh;
|
||||
use glium::vertex::VertexBufferAny;
|
||||
use glium::{self, implement_vertex, Display};
|
||||
use obj;
|
||||
|
||||
/// Returns a vertex buffer that should be rendered as `TrianglesList`.
|
||||
pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny {
|
||||
#[derive(Copy, Clone)]
|
||||
struct Vertex {
|
||||
position: [f32; 3],
|
||||
normal: [f32; 3],
|
||||
texture: [f32; 2],
|
||||
}
|
||||
|
||||
implement_vertex!(Vertex, position, normal, texture);
|
||||
|
||||
let mut data = ::std::io::BufReader::new(data);
|
||||
let data = obj::Obj::load_buf(&mut data).unwrap();
|
||||
|
||||
let mut vertex_data = Vec::new();
|
||||
|
||||
for object in data.objects.iter() {
|
||||
for polygon in object.groups.iter().flat_map(|g| g.polys.iter()) {
|
||||
match polygon {
|
||||
&genmesh::Polygon::PolyTri(genmesh::Triangle {
|
||||
x: v1,
|
||||
y: v2,
|
||||
z: v3,
|
||||
}) => {
|
||||
for v in [v1, v2, v3].iter() {
|
||||
let position = data.position[v.0];
|
||||
let texture = v.1.map(|index| data.texture[index]);
|
||||
let normal = v.2.map(|index| data.normal[index]);
|
||||
|
||||
let texture = texture.unwrap_or([0.0, 0.0]);
|
||||
let normal = normal.unwrap_or([0.0, 0.0, 0.0]);
|
||||
|
||||
vertex_data.push(Vertex {
|
||||
position: position,
|
||||
normal: normal,
|
||||
texture: texture,
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glium::vertex::VertexBuffer::new(display, &vertex_data)
|
||||
.unwrap()
|
||||
.into_vertex_buffer_any()
|
||||
}
|
File diff suppressed because it is too large
Load Diff
15
tmp_gfx/src/vertex.glsl
Normal file
15
tmp_gfx/src/vertex.glsl
Normal file
@ -0,0 +1,15 @@
|
||||
#version 140
|
||||
|
||||
uniform mat4 persp_matrix;
|
||||
uniform mat4 view_matrix;
|
||||
|
||||
in vec2 position;
|
||||
in vec4 color;
|
||||
out vec4 pass_color;
|
||||
|
||||
void main() {
|
||||
pass_color = color;
|
||||
|
||||
gl_Position = vec4(position, 0.0, 1.0);
|
||||
gl_Position = persp_matrix * view_matrix * vec4(position, 0.0, 1.0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user