summaryrefslogtreecommitdiff
path: root/lib/gltexture.cc
blob: ff791129e36732f0179a04896ee102dabeed4c35 (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
#include <sys/types.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <generic.h>
#include "gltexture.h"
#include "engine.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gettext.h"

mogltk::gltexture * mogltk::gltexture::active = 0;

mogltk::gltexture::gltexture(int w, int h, bool plane) throw (GeneralException) :
    texture(w, h), texture_allocated(false), planar(plane), tainted(true) {
}

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

mogltk::gltexture::~gltexture() {
    if (texture_allocated) {
	glDeleteTextures(1, &tex);
    }
}

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

    glBindTexture(GL_TEXTURE_2D, tex);

#if 0
    if (planar) {
#ifdef DEBUG
	printm(M_INFO, _("Generating planar gltexture: %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 gltexture: %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, GetWidth(), GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, GetPixels());
//	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::gltexture::Bind(bool expand) {
    if ((!texture_allocated) || tainted)
	Generate();
    glEnable(GL_TEXTURE_2D);
    if (active == this)
	return;
#ifdef DEBUG
    printm(M_INFO, _("Binding gltexture index %i.\n"), tex);
#endif
    glBindTexture(GL_TEXTURE_2D, tex);
    if (expand) {
	glMatrixMode(GL_TEXTURE);
	glLoadIdentity();
	glScaled(1 / (double) GetWidth(), 1 / (double) GetHeight(), 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
}

void mogltk::gltexture::Unbind(void) {
    if (active) {
	glBindTexture(GL_TEXTURE_2D, 0);
	glDisable(GL_TEXTURE_2D);
	active = 0;
#ifdef DEBUG
	printm(M_INFO, _("Unbinding gltexture.\n"));
#endif
    }
}

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