1 module evael.renderer.gl.gl_texture;
2 
3 import evael.renderer.gl.gl_wrapper;
4 import evael.renderer.texture;
5 
6 class GLTexture : Texture
7 {
8     private uint m_id;
9 
10     /**
11      * GLTexture constructor.
12      */
13     @nogc
14     public this()
15     {
16         super();
17         
18         gl.GenTextures(1, &this.m_id);
19     }
20 
21     /**
22      * GLTexture destructor.
23      */
24     @nogc
25     public ~this()
26     {
27         gl.DeleteTextures(1, &this.m_id);
28     }
29 
30 
31     /**
32      * Loads a texture.
33      * Params:
34      *      fileName : texture to load
35      */
36     public static GLTexture load(in string fileName)
37     {
38         import evael.lib.memory : MemoryHelper;
39         import evael.lib.image.image : Image;
40 
41         auto texture = MemoryHelper.create!GLTexture();
42 
43         auto image = Image.fromFile(fileName);
44 
45         gl.BindTexture(GL_TEXTURE_2D, texture.id);
46         gl.TexImage2D(GL_TEXTURE_2D,
47                     0,                // Mipmap level (0 being the top level i.e. full size)
48                     GL_RGBA,          // Internal format
49                     image.width,      // Width of the texture
50                     image.height,     // Height of the texture,
51                     0,                // Border in pixels
52                     GL_BGRA,          // Data format
53                     GL_UNSIGNED_BYTE, // Type of texture data
54                     image.bytes);     // The image data to use for this texture
55     
56         MemoryHelper.dispose(image);
57         
58         auto minificationFilter = GL_LINEAR;
59         auto magnificationFilter = GL_LINEAR;
60 
61         // Anisotropic filter
62         float fLargest;
63         gl.GetFloatv(0x84FF, &fLargest);
64         gl.TexParameterf(GL_TEXTURE_2D, 0x84FE, fLargest);
65 
66         // Specify our minification and magnification filters
67         gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minificationFilter);
68         gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magnificationFilter);
69     
70         // If we're using MipMaps, then we'll generate them here.
71         // Note: The glGenerateMipmap call requires OpenGL 3.0 as a minimum.
72         if (minificationFilter == GL_LINEAR_MIPMAP_LINEAR   ||
73             minificationFilter == GL_LINEAR_MIPMAP_NEAREST  ||
74             minificationFilter == GL_NEAREST_MIPMAP_LINEAR  ||
75             minificationFilter == GL_NEAREST_MIPMAP_NEAREST)
76         {
77             gl.GenerateMipmap(GL_TEXTURE_2D);
78         }
79     
80         return texture;
81     }
82 
83     @nogc
84     @property nothrow
85     {
86         public uint id() const
87         {
88             return this.m_id;
89         }
90     }
91 }
92