summaryrefslogtreecommitdiff
path: root/lib/texture.cc
blob: 43c8ac38cbaf69505b36ffbcd685945fd8c435be (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <sys/types.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <generic.h>
#include "texture.h"
#include "engine.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gettext.h"

#define DEBUG 1

#ifdef TRACE_TEXTURES
mogltk::texture * mogltk::texture::header = 0;
mogltk::texture * mogltk::texture::footer = 0;
#endif

mogltk::texture * mogltk::texture::active = 0;

mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h),
    texture_allocated(false), planar(plane), tainted(true) {
    if ((!ISPOT(w)) || (!ISPOT(h)))
	throw GeneralException(_("Size of the texture not a power of 2!"));
    
    if (!(surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	0xff000000,
	0x00ff0000,
	0x0000ff00,
	0x000000ff
#else
	0x000000ff,
	0x0000ff00,
	0x00ff0000,
	0xff000000
#endif
    ))) {
	throw GeneralException(_("Can't create RGB Surface"));
    }
    
    SDL_FillRect(surface, 0, 0);
    
#ifdef TRACE_TEXTURES
    next = 0;
    prev = footer;
    footer = this;
    if (!header) {
	header = this;
    }
    if (prev) {
	prev->next = this;
    }
#endif
}

mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) :
    texture_allocated(false), planar(plane), tainted(true) {

    SDL_Surface * temp;
    
    temp = LoadNTEX(h);
    width = temp->w;
    height = temp->h;

#ifdef DEBUG
    printm(M_INFO, "Creating texture from file: size %ix%i\n", height, width);
#endif

    if ((!ISPOT(width)) || (!ISPOT(height))) {
	SDL_FreeSurface(temp);
	throw GeneralException(_("Size of the texture not a power of 2!"));
    }

    SDL_PixelFormat f;

    f.palette = 0;
    f.BitsPerPixel = 32;
    f.BytesPerPixel = 4;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    f.Amask = 0x000000ff;
    f.Bmask = 0x0000ff00;
    f.Gmask = 0x00ff0000;
    f.Rmask = 0xff000000;
    f.Ashift = 0;
    f.Bshift = 8;
    f.Gshift = 16;
    f.Rshift = 24;
#else
    f.Rmask = 0x000000ff;
    f.Gmask = 0x0000ff00;
    f.Bmask = 0x00ff0000;
    f.Amask = 0xff000000;
    f.Rshift = 0;
    f.Gshift = 8;
    f.Bshift = 16;
    f.Ashift = 24;
#endif
    f.Rloss = 0;
    f.Gloss = 0;
    f.Bloss = 0;
    f.Aloss = 0;

    if (!(surface = SDL_ConvertSurface(temp, &f, 0))) {
	throw GeneralException("Could not convert texture to OpenGL format");
    }
    
    SDL_FreeSurface(temp);

#ifdef TRACE_TEXTURES
    next = 0;
    prev = footer;
    footer = this;
    if (!header) {
	header = this;
    }
    if (prev) {
	prev->next = this;
    }
#endif
}

mogltk::texture::~texture() {
    if (surface) {
	SDL_FreeSurface(surface);
    }
    
    if (texture_allocated) {
	glDeleteTextures(1, &tex);
    }
    
#ifdef TRACE_TEXTURES
    if (prev) {
	prev->next = next;
    }
    
    if (next) {
	next->prev = prev;
    }
    
    if (this == footer) {
	footer = prev;
    }
    
    if (this == header) {
	header = next;
    }
#endif
}

Uint32 * mogltk::texture::GetPixels() {
    return (Uint32 *) surface->pixels;
}

SDL_Surface * mogltk::texture::GetSurface() {
    return surface;
}

SDL_PixelFormat * mogltk::texture::GetFormat() {
    return surface->format;
}

void mogltk::texture::Generate() {
    if (texture_allocated) {
	glDeleteTextures(1, &tex);
    }
    
    glGenTextures(1, &tex);
#ifdef DEBUG
    printm(M_INFO, _("Generated texture index: %i\n"), tex);
#endif

    glBindTexture(GL_TEXTURE_2D, tex);

#if 0
    if (planar) {
#ifdef DEBUG
	printm(M_INFO, _("Generating planar texture: %i\n"), tex);
#endif
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
    } else {
#endif
#ifdef DEBUG
	printm(M_INFO, _("Generating 3D texture: %i\n"), tex);
#endif
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
//	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);	
#if 0
    }
#endif

    texture_allocated = true;
    tainted = false;
}

void mogltk::texture::Bind(bool expand) {
    if ((!texture_allocated) || tainted)
	Generate();
    glEnable(GL_TEXTURE_2D);
    if (active == this)
	return;
    glBindTexture(GL_TEXTURE_2D, tex);
    if (expand) {
	glMatrixMode(GL_TEXTURE);
	glLoadIdentity();
	glScaled(1 / (double) width, 1 / (double) height, 1);
	glMatrixMode(GL_MODELVIEW);
    }
    
    active = this;
    
#ifdef TRACE_TEXTURES
    if (header == this)
	return;
    
    if (prev) {
	prev->next = next;
    }
    
    if (next) {
	next->prev = prev;
    }
    
    if (footer = this) {
	footer = prev;
    }
    
    header->prev = this;
    header = this;
#endif
}

GLuint mogltk::texture::GetWidth() {
    return width;
}

GLuint mogltk::texture::GetHeight() {
    return height;
}

void mogltk::texture::Unbind(void) {
    if (active) {
	glBindTexture(GL_TEXTURE_2D, 0);
	glDisable(GL_TEXTURE_2D);
	active = 0;
    }
}

void mogltk::texture::Taint(void) {
    tainted = true;
}

#ifdef WORDS_BIGENDIAN
#define NTEX_SIGNATURE 0x4e544558
#else
#define NTEX_SIGNATURE 0x5845544e
#endif

SDL_Surface * mogltk::texture::LoadNTEX(Handle * h) throw (GeneralException) {
    SDL_Surface * r;
    char buffer[4];
    Uint16 height, width;
    
    h->read(buffer, 4);
    buffer[4] = 0;
    if (*((Uint32 *) buffer) != NTEX_SIGNATURE)
	throw GeneralException("Texture file " + h->GetName() + " corrupted");
    
    height = h->readU16();
    width = h->readU16();
    
    if (!(r = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	0xff000000,
	0x00ff0000,
	0x0000ff00,
	0x000000ff
#else
	0x000000ff,
	0x0000ff00,
	0x00ff0000,
	0xff000000
#endif
    ))) {
	throw GeneralException(_("Can't create RGB Surface for LoadNTEX"));
    }
    
    h->read(r->pixels, height * width * 4);
    
    return r;
}