1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module desgui.base.gldraw;
26 
27 public import desgui.core.draw;
28 
29 import desgui.base.glcontext;
30 
31 import desgl.base;
32 
33 import desil;
34 
35 auto dataArray(V)( size_t n, in V v )
36     if( isVector!V )
37 {
38     v.datatype[] ret;
39     foreach( i; 0 .. n )
40         ret ~= v.data;
41     return ret;
42 }
43 
44 class DiGLDrawRect : GLObj, DiDrawRect
45 {
46 private:
47     GLVBO pos, col, uv;
48     CommonShaderProgram shader;
49     GLTexture2D texture;
50     UseTexture ut = UseTexture.NONE;
51     col4 clr = col4(0,0,0,0);
52     irect last_rect;
53 
54 public:
55 
56     this()
57     {
58         shader = DiGuiShader.get();
59         int[] loc = shader.getAttribLocations( "vertex", "color", "uv" );
60 
61         pos = new GLVBO( [ 0.0f, 0, 1, 0, 0, 1, 1, 1 ] );
62         setAttribPointer( pos, loc[0], 2, GL_FLOAT );
63 
64         col = new GLVBO( dataArray( 4, col4(1,1,1,1) ) );
65         setAttribPointer( col, loc[1], 4, GL_FLOAT );
66 
67         uv = new GLVBO( [ 0.0f, 0, 1, 0, 0, 1, 1, 1 ], 
68                         GL_ARRAY_BUFFER, GL_STATIC_DRAW );
69         setAttribPointer( uv, loc[2], 2, GL_FLOAT );
70 
71         texture = new GLTexture2D;
72     }
73 
74     @property
75     {
76         ref UseTexture useTexture() { return ut; }
77         ref const(UseTexture) useTexture() const { return ut; }
78         irect rect() const { return last_rect; }
79 
80         col4 color() const { return clr; }
81         void color( in col4 c )
82         {
83             col.setData( dataArray( 4, c ) );
84             clr = c;
85         }
86     }
87 
88     void reshape( in irect r )
89     {
90         last_rect = r;
91         pos.setData( r.points!float ); 
92     }
93 
94     void draw()
95     {
96         vao.bind();
97         shader.use();
98         shader.setUniform!int( "ttu", GL_TEXTURE0 );
99         shader.setUniform!int( "use_texture", cast(int)useTexture );
100         texture.bind();
101         glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ); 
102     }
103 
104     void image( in Image img ) { texture.image( img ); }
105 
106     void image( in ImageReadAccess ira )
107     {
108         texture.image( ira );
109         if( !ira ) useTexture = UseTexture.NONE;
110     }
111 }
112 
113 class DiGLDrawFactory : DiDrawFactory
114 {
115     @property DiDrawRect rect() { return new DiGLDrawRect(); }
116 }