OpenCores
URL https://opencores.org/ocsvn/pdp1/pdp1/trunk

Subversion Repositories pdp1

[/] [pdp1/] [trunk/] [sw/] [src/] [main.rs] - Blame information for rev 13

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 yannv
#[macro_use]
2
extern crate glium;
3
extern crate rand;
4
 
5
fn main() {
6
    use rand::Rng;
7
    let mut rng = rand::thread_rng();
8
    use glium::DisplayBuild;
9
    let builder = glium::glutin::WindowBuilder::new();
10
    let window = builder
11
        .with_dimensions(1024,1024)
12
        .with_title("GLscope")
13
        .with_vsync()
14
        .build_glium()
15
        .unwrap();
16
    let mut now: f32 = 20000.0;
17
 
18
    // 2D vertex type for plot points
19
    #[derive(Copy, Clone)]
20
    struct Vertex {
21
        position: [f32; 2],
22
        time: f32
23
    }
24
    implement_vertex!(Vertex, position, time);
25
    // Note: need to transform -1023..1023 to -1..1
26
    // Could extend PDP-1 display instruction to 18 bit
27
 
28
    // TODO: Set up OpenGL stuff to draw something
29
    //window.make_current().expect("Couldn't make context current");
30
    let mut frame = window.draw();
31
    use glium::Surface;
32
    // So far, so good. I can clear the image.
33
    // I can also plot points, using two shaders.
34
    // Those can be blended (additive in this case).
35
    // Time to extend the vertices with a timestamp for age?
36
 
37
    // Set up a random array of points
38
    let mut shape: Vec = Vec::new();
39
    for t in 0..20000 {
40
        shape.push(Vertex {
41
            position: [rng.next_f32()*2.0-1.0,
42
                       rng.next_f32()*2.0-1.0],
43
            time: t as f32 });
44
    }
45
    let vertex_buffer =
46
        glium::VertexBuffer::new(&window, &shape).unwrap();
47
    let indices = glium::index::NoIndices(
48
        glium::index::PrimitiveType::Points);
49
 
50
    let vertex_shader_src = r#"
51
        #version 140
52
        in vec2 position;
53
        in float time;
54
        out float age;
55
        uniform float now;
56
        void main() {
57
            age = now-time;
58
            if (age >= 0) {
59
                gl_Position = vec4(position, 0.0, 1.0);
60
            }
61
        }
62
    "#;
63
 
64
    // TODO: falloff, blending?
65
    // Should use a uniform to indicate current time
66
    // Possibly add a timestamp per lit dot?
67
    let fragment_shader_src = r#"
68
        #version 140
69
        in float age;
70
        out vec4 color;
71
        void main() {
72
            float intensity = 1e2*exp(-1e-2*age);
73
            color = vec4(intensity, intensity, intensity, 1.0);
74
        }
75
    "#;
76
    let program =
77
       glium::Program::from_source(&window,
78
                                   vertex_shader_src,
79
                                   fragment_shader_src,
80
                                   None).unwrap();
81
    let drawparams = glium::DrawParameters {
82
        blend: glium::Blend {
83
            color: glium::BlendingFunction::Addition {
84
                source: glium::LinearBlendingFactor::One,
85
                destination: glium::LinearBlendingFactor::One },
86
            .. Default::default() },
87
        .. Default::default()
88
    };
89
    loop {
90
        for event in window.poll_events() {
91
            // Simplest nowait: use poll_events()
92
            match event {
93
                glium::glutin::Event::Closed => {
94
                    frame.finish().unwrap();
95
                    return;
96
                },
97
                _ => ()
98
            }
99
        }
100
 
101
        // Closing the window closes the program.
102
        frame.clear_color(0.0, 0.0, 0.0, 1.0);
103
        frame.draw(&vertex_buffer, &indices, &program,
104
                   //&glium::uniforms::EmptyUniforms,
105
                   &uniform! { now: now },
106
                   &drawparams).unwrap();
107
        now += 1e1;
108
        if now > 13.0e3 {
109
            now = 0.0e3;
110
        }
111
 
112
        window.swap_buffers().unwrap();
113
    }
114
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.