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

Subversion Repositories pdp1

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

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 14 yannv
extern crate time;
5 13 yannv
 
6
fn main() {
7
    use rand::Rng;
8
    let mut rng = rand::thread_rng();
9
    use glium::DisplayBuild;
10
    let builder = glium::glutin::WindowBuilder::new();
11
    let window = builder
12
        .with_dimensions(1024,1024)
13
        .with_title("GLscope")
14
        .with_vsync()
15
        .build_glium()
16
        .unwrap();
17 14 yannv
    let mut now: f32;
18 13 yannv
 
19
    // 2D vertex type for plot points
20
    #[derive(Copy, Clone)]
21
    struct Vertex {
22
        position: [f32; 2],
23 14 yannv
        time: f32  // Suggest using microseconds
24 13 yannv
    }
25
    implement_vertex!(Vertex, position, time);
26
    // Note: need to transform -1023..1023 to -1..1
27
    // Could extend PDP-1 display instruction to 18 bit
28
 
29
    // TODO: Set up OpenGL stuff to draw something
30
    //window.make_current().expect("Couldn't make context current");
31
    let mut frame = window.draw();
32
    use glium::Surface;
33
    // So far, so good. I can clear the image.
34
    // I can also plot points, using two shaders.
35
    // Those can be blended (additive in this case).
36
    // Time to extend the vertices with a timestamp for age?
37
 
38
    // Set up a random array of points
39 14 yannv
    /*
40 13 yannv
    let mut shape: Vec = Vec::new();
41
    for t in 0..20000 {
42
        shape.push(Vertex {
43
            position: [rng.next_f32()*2.0-1.0,
44
                       rng.next_f32()*2.0-1.0],
45
            time: t as f32 });
46 14 yannv
    }*/
47
    let vbmax = 20000;
48
    let mut vbindex = 0;
49
    // TODO: streaming suited vertexbuffer.
50
    // Docs say to use dynamic, persistent might be GL4?
51
    let mut vertex_buffer : glium::VertexBuffer =
52
        glium::VertexBuffer::empty_dynamic(&window, vbmax).unwrap();
53 13 yannv
    let indices = glium::index::NoIndices(
54
        glium::index::PrimitiveType::Points);
55
 
56
    let vertex_shader_src = r#"
57
        #version 140
58
        in vec2 position;
59
        in float time;
60
        out float age;
61
        uniform float now;
62
        void main() {
63
            age = now-time;
64
            if (age >= 0) {
65
                gl_Position = vec4(position, 0.0, 1.0);
66
            }
67
        }
68
    "#;
69
 
70
    // TODO: falloff, blending?
71
    // Should use a uniform to indicate current time
72
    // Possibly add a timestamp per lit dot?
73
    let fragment_shader_src = r#"
74
        #version 140
75
        in float age;
76
        out vec4 color;
77
        void main() {
78 14 yannv
            float intensity = 1e1*exp(-8e-6*age);
79 13 yannv
            color = vec4(intensity, intensity, intensity, 1.0);
80
        }
81
    "#;
82
    let program =
83
       glium::Program::from_source(&window,
84
                                   vertex_shader_src,
85
                                   fragment_shader_src,
86
                                   None).unwrap();
87
    let drawparams = glium::DrawParameters {
88
        blend: glium::Blend {
89
            color: glium::BlendingFunction::Addition {
90
                source: glium::LinearBlendingFactor::One,
91
                destination: glium::LinearBlendingFactor::One },
92
            .. Default::default() },
93
        .. Default::default()
94
    };
95 14 yannv
 
96
    let t0 = time::get_time();
97
 
98 13 yannv
    loop {
99
        for event in window.poll_events() {
100
            // Simplest nowait: use poll_events()
101
            match event {
102
                glium::glutin::Event::Closed => {
103
                    frame.finish().unwrap();
104
                    return;
105
                },
106
                _ => ()
107
            }
108
        }
109 14 yannv
 
110
        now = (time::get_time() - t0).num_microseconds().unwrap()
111
            as f32;
112
 
113
        // Inject new points
114
        {
115
            let mut wvb = vertex_buffer.map_write();
116
            // Add 21 random points per frame
117
            for _ in 0..20 {
118
                wvb.set(vbindex, Vertex {
119
                    position: [rng.next_f32()*2.0-1.0,
120
                               rng.next_f32()*2.0-1.0],
121
                    time: now });
122
                vbindex = (vbindex+1) % vbmax;
123
            }
124
            // TODO: Read points from e.g. stdin
125
            for _ in 0..30 {
126
                let mut str = String::new();
127
                std::io::stdin().read_line(&mut str).unwrap();
128
                // FIXME: Parse line.
129
                let mut words = str.split_whitespace();
130
                let x = words.next().unwrap().parse().unwrap();
131
                let y = words.next().unwrap().parse().unwrap();
132
 
133
                wvb.set(vbindex,
134
                        Vertex {position: [x, y], time: now });
135
                vbindex = (vbindex+1) % vbmax;
136
            }
137
        }
138
 
139 13 yannv
        // Closing the window closes the program.
140
        frame.clear_color(0.0, 0.0, 0.0, 1.0);
141
        frame.draw(&vertex_buffer, &indices, &program,
142
                   //&glium::uniforms::EmptyUniforms,
143
                   &uniform! { now: now },
144
                   &drawparams).unwrap();
145
 
146
        window.swap_buffers().unwrap();
147
    }
148
}

powered by: WebSVN 2.1.0

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