Repository navigation
Expand file tree
/
Copy pathbindTexturePatch.h
More file actions
232 lines (175 loc) · 6.03 KB
/
Copy pathbindTexturePatch.h
File metadata and controls
232 lines (175 loc) · 6.03 KB
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
/* OpenBRF -- by marco tarini. Provided under GNU General Public License */
#ifndef BINDTEXTUREPATCH_H
#define BINDTEXTUREPATCH_H
#include "platform.h"
// DDS format structure
struct DDSFormat {
quint32 dwSize;
quint32 dwFlags;
quint32 dwHeight;
quint32 dwWidth;
quint32 dwLinearSize;
quint32 dummy1;
quint32 dwMipMapCount;
quint32 dummy2[11];
struct {
quint32 dummy3[2];
quint32 dwFourCC;
quint32 dummy4[5];
} ddsPixelFormat;
};
// compressed texture pixel formats
#define FOURCC_DXT1 0x31545844
#define FOURCC_DXT2 0x32545844
#define FOURCC_DXT3 0x33545844
#define FOURCC_DXT4 0x34545844
#define FOURCC_DXT5 0x35545844
#ifndef GL_COMPRESSED_RGB_S3TC_DXT1_EXT
#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#endif
#ifndef GL_GENERATE_MIPMAP_SGIS
#define GL_GENERATE_MIPMAP_SGIS 0x8191
#define GL_GENERATE_MIPMAP_HINT_SGIS 0x8192
#endif
/*
typedef void (APIENTRY *pfn_glCompressedTexImage2DARB) (GLenum, GLint, GLenum, GLsizei,
GLsizei, GLint, GLsizei, const GLvoid *);
static pfn_glCompressedTexImage2DARB qt_glCompressedTexImage2DARB = 0;
*/
typedef QHash<QString, DdsData> QGLDDSCache;
Q_GLOBAL_STATIC(QGLDDSCache, qgl_dds_cache)
;
static void forgetChachedTextures(){
QGLDDSCache::iterator it;
for (it=qgl_dds_cache()->begin(); it!=qgl_dds_cache()->end(); it++){
unsigned int b = it.value().bind;
if (b>0) glDeleteTextures(1,&b);
}
qgl_dds_cache()->clear();
}
bool loadDDSHeader(QFile &f, DdsData &data, DDSFormat &ddsHeader){
f.open(QIODevice::ReadOnly);
data.sx = data.sy = data.bind = data.mipmap = data.filesize = data.ddxversion=0;
char tag[4];
f.read(&tag[0], 4);
if (strncmp(tag,"DDS ", 4) != 0) {
qWarning("QGLContext::bindTexture(): not a DDS image file.");
return false;
}
f.read((char *) &ddsHeader, sizeof(DDSFormat));
//int factor =4;
switch(ddsHeader.ddsPixelFormat.dwFourCC) {
default:
qWarning("QGLContext::bindTexture() DDS image format not supported.");
return false;
case FOURCC_DXT1:
//factor = 2;
data.ddxversion=1;
break;
case FOURCC_DXT3:
data.ddxversion=3;
break;
case FOURCC_DXT5:
data.ddxversion=5;
break;
}
if (ddsHeader.dwMipMapCount == 0) ddsHeader.dwMipMapCount=1;
data.mipmap=ddsHeader.dwMipMapCount;
//int bufferSize;
//if (ddsHeader.dwMipMapCount > 1)
// bufferSize = ddsHeader.dwLinearSize * factor;
//else
// bufferSize = ddsHeader.dwLinearSize;
data.filesize= f.size(); //bufferSize+4+sizeof(ddsHeader);
data.sx = ddsHeader.dwWidth;
data.sy = ddsHeader.dwHeight;
return true;
}
bool loadOnlyDDSHeader(const QString &fileName, DdsData &data){
QFile f(fileName);
DDSFormat unused;
return loadDDSHeader(f,data,unused);
}
bool GLWidget::myBindTexture(const QString &fileName, DdsData &data)
{
/*
if (!qt_glCompressedTexImage2DARB) {
QGLContext cx(QGLFormat::defaultFormat());
cx.getProcAddress(QLatin1String("glCompressedTexImage2D"));
qt_glCompressedTexImage2DARB = (pfn_glCompressedTexImage2DARB) cx.getProcAddress(QLatin1String("glCompressedTexImage2DARB"));
}
*/
QGLDDSCache::const_iterator it = qgl_dds_cache()->constFind(fileName);
if (it != qgl_dds_cache()->constEnd()) {
data = it.value();
glBindTexture(GL_TEXTURE_2D, data.bind);
return true;
}
QFile f(fileName);
DDSFormat ddsHeader;
if (!loadDDSHeader(f, data, ddsHeader )) return false;
int blockSize = 16;
GLenum format=4;
int factor = 4;
switch(data.ddxversion){
case 1: format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; blockSize=8; factor = 2; break;
case 3: format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
case 5: format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
}
if (!ddsHeader.dwLinearSize) {
//qWarning("QGLContext::bindTexture() DDS image size is not valid.");
//return 0;
ddsHeader.dwLinearSize = ddsHeader.dwHeight * ddsHeader.dwWidth * factor / 4;
}
int bufferSize;
if (ddsHeader.dwMipMapCount > 1)
bufferSize = ddsHeader.dwLinearSize * factor;
else
bufferSize = ddsHeader.dwLinearSize;
GLubyte *pixels = (GLubyte *) malloc(bufferSize*sizeof(GLubyte));
f.seek(ddsHeader.dwSize + 4);
f.read((char *) pixels, bufferSize);
f.close();
GLuint tx_id;
glGenTextures(1, &tx_id);
data.bind=tx_id;
glBindTexture(GL_TEXTURE_2D, tx_id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (((1<<(ddsHeader.dwMipMapCount-1))>=(int)ddsHeader.dwWidth) &&
((1<<(ddsHeader.dwMipMapCount-1))>=(int)ddsHeader.dwHeight)) {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//test: glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} else
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
int offset = 0;
int w = ddsHeader.dwWidth;
int h = ddsHeader.dwHeight;
/*if (!qt_glCompressedTexImage2DARB) {
qWarning("QGLContext::bindTexture(): The GL implementation does not support texture"
"compression extensions.");
return false;
}*/
// load mip-maps
for(int i = 0; i < (int) ddsHeader.dwMipMapCount; ++i) {
//if (i>4) continue;
if (w == 0) w = 1;
if (h == 0) h = 1;
int size = ((w+3)/4) * ((h+3)/4) * blockSize;
if (offset+size>=bufferSize) offset = bufferSize-size;
glCompressedTexImage2D
//qt_glCompressedTexImage2DARB
(GL_TEXTURE_2D, i, format, w, h, 0,
size, pixels + offset);
offset += size;
// half size for each mip-map level
w = w/2;
h = h/2;
}
free(pixels);
qgl_dds_cache()->insert(fileName, data);
return true;
}
#endif // BINDTEXTUREPATCH_H