1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//!
//! ## Overview
//!
//! This crate provides the opengl internals without the opengl context creation functionality.
//! So this crate does not depend on glutin.
//!
use axgeom::*;

use core::mem;
use gl::types::*;

mod shader;
mod vbo;

#[macro_export]
macro_rules! gl_ok {
    () => {
        assert_eq!(gl::GetError(), gl::NO_ERROR);
    };
}

use circle_program::CircleProgram;
use circle_program::PointMul;
mod circle_program;

pub mod gl {
    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}



const GL_POINT_COMP:f32=2.5;



pub struct SquareSave{
    col:[f32;4],
    radius:f32,
    buffer:vbo::StaticBuffer<circle_program::Vertex>
}
impl SquareSave{
    pub fn draw(&self,session:&mut MySys){
        session.circle_program.set_buffer_and_draw(self.radius*GL_POINT_COMP*session.point_mul.0,self.col,0,self.buffer.get_id(),gl::POINTS,self.buffer.len());
    }
}
pub struct SquareSession<'a> {
    col:[f32;4],
    radius:f32,
    sys: &'a mut MySys,
}
impl<'a> SquareSession<'a> {
    #[inline(always)]
    pub fn add(&mut self, point: Vec2<f32>) -> &mut Self {
        self.sys
            .circle_buffer
            .push(circle_program::Vertex([point.x, point.y]));
        self
    }

    #[inline(always)]
    pub fn addp(&mut self, x:f32,y:f32)->&mut Self{
        self.add(vec2(x,y))
    }
    pub fn save(&mut self)->SquareSave{
        SquareSave{col:self.col,radius:self.radius,buffer:vbo::StaticBuffer::new(self.sys.circle_buffer.get_verts())}
    }


    pub fn send_and_draw(&mut self) {
        self.sys.circle_buffer.update();
        self.sys.circle_buffer.update();
        self.sys.circle_program.set_buffer_and_draw(self.radius*GL_POINT_COMP*self.sys.point_mul.0,self.col,0,self.sys.circle_buffer.get_id(),gl::POINTS,self.sys.circle_buffer.len());       
    }
}
impl<'a> Drop for SquareSession<'a> {
    fn drop(&mut self) {
        self.sys.reset();
    }
}


pub struct CircleSave{
    col:[f32;4],
    radius:f32,
    buffer:vbo::StaticBuffer<circle_program::Vertex>
}
impl CircleSave{
    pub fn draw(&self,session:&mut MySys){
        session.circle_program.set_buffer_and_draw(self.radius*GL_POINT_COMP*session.point_mul.0,self.col,1,self.buffer.get_id(),gl::POINTS,self.buffer.len());
    }
}
pub struct CircleSession<'a> {
    radius:f32,
    col:[f32;4],
    sys: &'a mut MySys,
}
impl<'a> Drop for CircleSession<'a> {
    fn drop(&mut self) {
        self.sys.reset();
    }
}
impl<'a> CircleSession<'a> {
    pub fn save(&mut self)->CircleSave{
        CircleSave{col:self.col,radius:self.radius,buffer:vbo::StaticBuffer::new(self.sys.circle_buffer.get_verts())}
    }

    pub fn send_and_draw(&mut self) {
        self.sys.circle_buffer.update();
        self.sys.circle_program.set_buffer_and_draw(self.radius*GL_POINT_COMP*self.sys.point_mul.0,self.col,1,self.sys.circle_buffer.get_id(),gl::POINTS,self.sys.circle_buffer.len());       
    }

    #[inline(always)]
    pub fn addp(&mut self, x:f32,y:f32)->&mut Self{
        self.add(vec2(x,y))
    }
    #[inline(always)]
    pub fn add(&mut self, point: Vec2<f32>) -> &mut Self {
        self.sys
            .circle_buffer
            .push(circle_program::Vertex([point.x, point.y]));
        self
    }
}

pub struct RectSession<'a> {
    col:[f32;4],
    sys: &'a mut MySys,
}
impl Drop for RectSession<'_> {
    fn drop(&mut self) {
        self.sys.reset();
    }
}

pub struct RectSave{
    col:[f32;4],
    buffer:vbo::StaticBuffer<circle_program::Vertex>
}
impl RectSave{
    pub fn draw(&self,session:&mut MySys){
        session.circle_program.set_buffer_and_draw(0.0,self.col,0,self.buffer.get_id(),gl::TRIANGLES,self.buffer.len());
    }
}

impl RectSession<'_> {
    pub fn save(&mut self)->RectSave{
        RectSave{col:self.col,buffer:vbo::StaticBuffer::new(self.sys.circle_buffer.get_verts())}
    }

    pub fn send_and_draw(&mut self) {
        self.sys.circle_buffer.update();
        self.sys.circle_program.set_buffer_and_draw(0.0,self.col,0,self.sys.circle_buffer.get_id(),gl::TRIANGLES,self.sys.circle_buffer.len());       
    }

    ///NOTE The argument positions
    ///It is x1,x2,y1,y2  not  x1,y1,x2,y2.
    #[inline(always)]
    pub fn addp(&mut self, x1:f32,x2:f32,y1:f32,y2:f32)->&mut Self{
        self.add(rect(x1,x2,y1,y2))
    }
    #[inline(always)]
    pub fn add(&mut self, rect: Rect<f32>) -> &mut Self {
        let [tl, tr, br, bl] = rect.get_corners();
        //let arr = [a, b, c, c, d, a];
        let arr=[tr,tl,bl,bl,br,tr];
        for a in arr.iter() {
            self.sys
                .circle_buffer
                .push(circle_program::Vertex([a.x, a.y]));
        }

        self
    }
}

pub struct ArrowSave{
    col:[f32;4],
    buffer:vbo::StaticBuffer<circle_program::Vertex>
}
impl ArrowSave{
    pub fn draw(&self,session:&mut MySys){
        session.circle_program.set_buffer_and_draw(0.0,self.col,0,self.buffer.get_id(),gl::TRIANGLES,self.buffer.len());
    }
}
pub struct ArrowSession<'a> {
    sys: &'a mut MySys,
    col:[f32;4],
    radius: f32,
}
impl Drop for ArrowSession<'_> {
    fn drop(&mut self) {
        self.sys.reset();
    }
}

impl ArrowSession<'_> {
    pub fn save(&mut self)->ArrowSave{
        ArrowSave{col:self.col,buffer:vbo::StaticBuffer::new(self.sys.circle_buffer.get_verts())}
    }

    pub fn send_and_draw(&mut self) {
        self.sys.circle_buffer.update();
        self.sys.circle_program.set_buffer_and_draw(0.0,self.col,0,self.sys.circle_buffer.get_id(),gl::TRIANGLES,self.sys.circle_buffer.len());
    }

    #[inline(always)]
    pub fn addp(&mut self, x1:f32,y1:f32,x2:f32,y2:f32)->&mut Self{
        self.add(vec2(x1,y1),vec2(x2,y2))
    }
    #[inline(always)]
    pub fn add(&mut self, start: Vec2<f32>, end: Vec2<f32>) -> &mut Self {
        let radius = self.radius;
        let offset = end - start;

        let arrow_head = start + offset * 0.8;

        let k = offset.rotate_90deg_right().normalize_to(1.0);
        let start1 = start + k * radius;
        let start2 = start - k * radius;

        let end1 = arrow_head + k * radius;
        let end2 = arrow_head - k * radius;

        let end11 = arrow_head + k * radius * 2.5;
        let end22 = arrow_head - k * radius * 2.5;
        let arr = [start1, start2, end1, start2, end1, end2, end, end11, end22];

        for a in arr.iter() {
            self.sys
                .circle_buffer
                .push(circle_program::Vertex([a.x, a.y]));
        }
        self
    }
}


pub struct LineSave{
    col:[f32;4],
    buffer:vbo::StaticBuffer<circle_program::Vertex>
}


impl LineSave{
    pub fn draw(&self,session:&mut MySys){
        let _kk = session.point_mul.0;
        session.circle_program.set_buffer_and_draw(0.0,self.col,0,self.buffer.get_id(),gl::TRIANGLES,self.buffer.len());
    }
}

pub struct LineSession<'a> {
    sys: &'a mut MySys,
    radius: f32,
    col:[f32;4]
}
impl Drop for LineSession<'_> {
    fn drop(&mut self) {
        self.sys.reset();
    }
}
impl LineSession<'_> {
    pub fn save(&mut self)->LineSave{
        LineSave{col:self.col,buffer:vbo::StaticBuffer::new(self.sys.circle_buffer.get_verts())}
    }

    pub fn send_and_draw(&mut self) {
        self.sys.circle_buffer.update();
        self.sys.circle_program.set_buffer_and_draw(0.0,self.col,0,self.sys.circle_buffer.get_id(),gl::TRIANGLES,self.sys.circle_buffer.len());
    }

    #[inline(always)]
    pub fn addp(&mut self, x1:f32,y1:f32,x2:f32,y2:f32)->&mut Self{
        self.add(vec2(x1,y1),vec2(x2,y2))
    }
    #[inline(always)]
    pub fn add(&mut self, start: Vec2<f32>, end: Vec2<f32>) -> &mut Self {
        let radius = self.radius;
        let offset = end - start;
        let k = offset.rotate_90deg_right().normalize_to(1.0);
        let start1 = start + k * radius;
        let start2 = start - k * radius;

        let end1 = end + k * radius;
        let end2 = end - k * radius;

        let arr = [start1, start2, end1, start2, end1, end2];

        for a in arr.iter() {
            self.sys
                .circle_buffer
                .push(circle_program::Vertex([a.x, a.y]));
        }
        self
    }
}

///Allows the user to start drawing shapes.
///The top left corner is the origin.
///y grows as you go down.
///x grows as you go right.
pub struct MySys {
    circle_program: CircleProgram,
    point_mul: PointMul,
    circle_buffer: vbo::GrowableBuffer<circle_program::Vertex>,
}




impl MySys {
    fn reset(&mut self) {
        self.circle_buffer.clear();
    }
    pub fn set_viewport(&mut self, width: f32, rect: Rect<f32>) {
        self.point_mul = self.circle_program.set_viewport(width, rect);
    }

    //Unsafe since user might create two instances, both of 
    //which could make opengl calls simultaneously
    pub unsafe fn new(dim: Rect<f32>) -> MySys {
        
        let circle_buffer = vbo::GrowableBuffer::new();
        let mut circle_program = CircleProgram::new();
        let point_mul = circle_program.set_viewport(dim.x.distance(), dim);

        gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
        gl_ok!();
        gl::Enable(gl::BLEND);
        gl_ok!();
        

        MySys {
            point_mul,
            circle_program,
            circle_buffer,
        }
    }

    pub fn circles(&mut self, color: [f32; 4], radius: f32) -> CircleSession {
        CircleSession { col:color,radius,sys: self }
    }
    pub fn squares(&mut self, color: [f32; 4], radius: f32) -> SquareSession {
        SquareSession { col:color,radius,sys: self }
    }
    pub fn rects(&mut self, color: [f32; 4]) -> RectSession {
        RectSession { col:color,sys: self }
    }
    pub fn arrows(&mut self, color: [f32; 4], radius: f32) -> ArrowSession {
        let kk = self.point_mul.0;

        ArrowSession {
            col:color,
            sys: self,
            radius: radius * kk,
        }
    }

    pub fn lines(&mut self, color: [f32; 4], radius: f32) -> LineSession {
        let kk=self.point_mul.0;
        LineSession {
            col:color,
            sys: self,
            radius: radius * kk,
        }
    }


    pub fn clear_color(&mut self,back_color:[f32;3]){
        unsafe {
            gl::ClearColor(back_color[0], back_color[1], back_color[2], 1.0);
            gl_ok!();

            gl::Clear(gl::COLOR_BUFFER_BIT);
            gl_ok!();
            
        }
    }

}