1 /*
2 Copyright (c) 2017-2018 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.graphics.texture;
29 
30 import std.stdio;
31 import std.math;
32 
33 import derelict.opengl;
34 
35 import dlib.core.memory;
36 import dlib.image.image;
37 import dlib.math.vector;
38 
39 import dagon.core.ownership;
40 
41 class Texture: Owner
42 {
43     SuperImage image;
44     
45     GLuint tex;
46     GLenum format;
47     GLint intFormat;
48     GLenum type;
49     
50     int width;
51     int height;
52     int numMipmapLevels;
53     
54     Vector2f translation;
55     Vector2f scale;
56     float rotation;
57     
58     bool useMipmapFiltering = true;
59     bool useLinearFiltering = true;
60 
61     this(Owner o)
62     {
63         super(o);
64         translation = Vector2f(0.0f, 0.0f);
65         scale = Vector2f(1.0f, 1.0f);
66         rotation = 0.0f;
67     }
68 
69     this(SuperImage img, Owner o, bool genMipmaps = false)
70     {
71         super(o);
72         translation = Vector2f(0.0f, 0.0f);
73         scale = Vector2f(1.0f, 1.0f);
74         rotation = 0.0f;
75         createFromImage(img, genMipmaps);
76     }
77 
78     void createFromImage(SuperImage img, bool genMipmaps = true)
79     {
80         image = img;
81         width = img.width;
82         height = img.height;
83 
84         //type = GL_UNSIGNED_BYTE;
85 
86         switch (img.pixelFormat)
87         {
88             case PixelFormat.L8:         intFormat = GL_R8;      format = GL_RED;  type = GL_UNSIGNED_BYTE; break;
89             case PixelFormat.LA8:        intFormat = GL_RG8;     format = GL_RG;   type = GL_UNSIGNED_BYTE; break;
90             case PixelFormat.RGB8:       intFormat = GL_RGB8;    format = GL_RGB;  type = GL_UNSIGNED_BYTE; break;
91             case PixelFormat.RGBA8:      intFormat = GL_RGBA8;   format = GL_RGBA; type = GL_UNSIGNED_BYTE; break;
92             case PixelFormat.RGBA_FLOAT: intFormat = GL_RGBA32F; format = GL_RGBA; type = GL_FLOAT; break;
93             default:
94                 writefln("Unsupported pixel format %s", img.pixelFormat);
95                 return;
96         }
97 
98         glGenTextures(1, &tex);
99         glActiveTexture(GL_TEXTURE0);
100         glBindTexture(GL_TEXTURE_2D, tex);
101 
102         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
103         
104         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
105         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
106         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
107         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
108         
109         //if (DerelictGL.isExtSupported("GL_EXT_texture_filter_anisotropic"))
110         //    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
111        
112         glTexImage2D(GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, cast(void*)img.data.ptr);
113 
114         //if (genMipmaps)
115         glGenerateMipmap(GL_TEXTURE_2D);
116 
117         glBindTexture(GL_TEXTURE_2D, 0);
118     }
119 
120     void bind()
121     {
122         if (glIsTexture(tex))
123             glBindTexture(GL_TEXTURE_2D, tex);
124             
125         if (useMipmapFiltering)
126             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
127         else if (useLinearFiltering)
128             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
129         else
130         {
131             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
132             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
133         }
134     }
135 
136     void unbind()
137     {
138         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
139         glBindTexture(GL_TEXTURE_2D, 0);
140     }
141     
142     bool valid()
143     {
144         return cast(bool)glIsTexture(tex);
145     }
146 
147     void release()
148     {
149         if (glIsTexture(tex))
150             glDeleteTextures(1, &tex);
151         if (image)
152         {
153             Delete(image);
154             image = null;
155         }
156     }
157 
158     ~this()
159     {
160         release();
161     }
162 }
163