1 /*
2 Copyright (c) 2013-2015 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dgl.graphics.texture;
30 
31 import std.conv;
32 
33 import derelict.opengl.gl;
34 import derelict.opengl.glu;
35 
36 import dlib.core.memory;
37 import dlib.image.image;
38 import dlib.math.vector;
39 
40 import dgl.core.interfaces;
41 
42 class Texture: Modifier
43 {
44     GLuint tex;
45     GLenum format;
46     GLenum type;
47     int width;
48     int height;
49     Vector2f scroll;
50 
51     // TODO
52     /*
53     this(uint w, uint h)
54     {
55         //free();
56 
57         width = w;
58         height = h;
59         ubyte[] data = new ubyte[width * height * 4];
60         glGenTextures(1, &tex);
61         glBindTexture(GL_TEXTURE_2D, tex);
62         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.ptr);
63         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
64         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
65     }
66 */
67 
68     this(uint w, uint h)
69     {
70         scroll = Vector2f(0, 0);
71 
72         width = w;
73         height = h;
74 
75         glGenTextures(1, &tex);
76         glBindTexture(GL_TEXTURE_2D, tex);
77         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
78         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
79         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
81         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
82     }
83 
84 
85     this(SuperImage img, bool genMipmaps = true)
86     {
87         scroll = Vector2f(0, 0);
88         createFromImage(img, genMipmaps);
89     }
90 
91     void createFromImage(SuperImage img, bool genMipmaps = true)
92     {
93         if (glIsTexture(tex))
94             glDeleteTextures(1, &tex);
95 
96         width = img.width;
97         height = img.height;
98 
99         glGenTextures(1, &tex);
100         glBindTexture(GL_TEXTURE_2D, tex);
101 
102         if (genMipmaps)
103         {
104             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
105             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
106             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
107             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
108         }
109         else
110         {
111             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
112             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
114             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
115         }
116 
117         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
118 
119         type = GL_UNSIGNED_BYTE;
120 
121         switch (img.pixelFormat)
122         {
123             case PixelFormat.L8:     format = GL_LUMINANCE; break;
124             case PixelFormat.LA8:    format = GL_LUMINANCE_ALPHA; break;
125             case PixelFormat.RGB8:   format = GL_RGB; break;
126             case PixelFormat.RGBA8:  format = GL_RGBA; break;
127             case PixelFormat.L16:    format = GL_LUMINANCE;       type = GL_UNSIGNED_SHORT; break;
128             case PixelFormat.LA16:   format = GL_LUMINANCE_ALPHA; type = GL_UNSIGNED_SHORT; break;
129             case PixelFormat.RGB16:  format = GL_RGB;             type = GL_UNSIGNED_SHORT; break;
130             case PixelFormat.RGBA16: format = GL_RGBA;            type = GL_UNSIGNED_SHORT; break;
131             default:
132                 assert(0, "Texture.createFromImage is not implemented for PixelFormat " ~ img.pixelFormat.to!string);
133         }
134 
135         gluBuild2DMipmaps(GL_TEXTURE_2D,
136             format, width, height, format,
137             type, cast(void*)img.data.ptr);
138     }
139 
140     void bind(double dt)
141     {
142         glEnable(GL_TEXTURE_2D);
143         if (glIsTexture(tex))
144             glBindTexture(GL_TEXTURE_2D, tex);
145         glMatrixMode(GL_TEXTURE);
146         glPushMatrix();
147         glLoadIdentity();
148         glTranslatef(scroll.x, scroll.y, 0);
149         glMatrixMode(GL_MODELVIEW);
150     }
151 
152     void unbind()
153     {
154         glMatrixMode(GL_TEXTURE);
155         glPopMatrix();
156         glMatrixMode(GL_MODELVIEW);
157 
158         glBindTexture(GL_TEXTURE_2D, 0);
159         glDisable(GL_TEXTURE_2D);
160     }
161 
162     ~this()
163     {
164         if (glIsTexture(tex))
165             glDeleteTextures(1, &tex);
166     }
167 
168     void free()
169     {
170         Delete(this);
171     }
172 
173     void copyRendered()
174     {
175         glBindTexture(GL_TEXTURE_2D, tex);
176         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height);
177         glBindTexture(GL_TEXTURE_2D, 0);
178     }
179 }