-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTexture.cpp
221 lines (187 loc) · 7.3 KB
/
Texture.cpp
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
#include "Texture.h"
#include "Utils.h"
#include <iostream>
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
using namespace std;
namespace tessterrain {
const unsigned int Texture::LINEAR = GL_LINEAR;
const unsigned int Texture::NEAREST = GL_NEAREST;
const unsigned int Texture::MIPMAP = GL_LINEAR_MIPMAP_LINEAR;
void Texture::loadData(const char* filename) {
this->filename = filename;
int x,y,n;
//unsigned int t = getTime();
if(initialized && this->data) {
stbi_image_free(data);
if(multiRes && this->data_low)
delete []data_low;
}
//unsigned int t = getTime();
this->data = stbi_load(filename, &x, &y, &n, 0);
//cout << "load file time: " << getTime() - t << endl;
//cout << "Load image " <<filename << " (n= " << n << ") time: " << getTime() - t << endl;
if(!data) {
cout << "Failed to load texture " << filename << endl;
exit(0);
}
if(multiRes) {
int factor = 2;
width_low = width/factor;
height_low = height/factor;
//unsigned int t = getTime();
data_low = new unsigned char[width_low*height_low*numChannel];
for (int r = 0; r < height_low; r++) {
for(int c=0; c < width_low; c++) {
for(int ind=0; ind < numChannel; ind++) {
data_low[r*width_low*numChannel + numChannel*c + ind] = data[r*width*numChannel*factor + numChannel*c*factor + ind];
}
}
}
//cout << numChannel << " resize time: " << getTime() - t << " " << width_low << " " << height_low << endl;
//unsigned int t = getTime();
//stbir_resize_uint8( this->data , width , height , 0,
// this->data_low, width_low, height_low, 0, numChannel);
//cout << "resize time: " << getTime() - t << endl;
}
initialized = false;
}
void Texture::initTexture(int quality) {
if (multiRes && quality != currentQuality) {
//cout << gluid << " change mode to " << quality << endl;
currentQuality = quality;
//initialized = false;
freeTexture(false);
}
if(!created) {
//init texture
glunit = unitFromIndex(index);
minFilter = GL_LINEAR;
magFilter = GL_LINEAR;
type = GL_UNSIGNED_BYTE;
if(numChannel == 1) {
format = globalFormat = GL_RED;
}
else if (numChannel == 3) {
format = globalFormat = GL_RGB;
}
else {
format = GL_RGBA8;
globalFormat = GL_RGBA;
type = GL_UNSIGNED_INT_8_8_8_8_REV;
}
glActiveTexture(glunit);
glGenTextures(1, &gluid);
glBindTexture(GL_TEXTURE_2D, gluid);
//unsigned int t = Utils::getTime();
if (currentQuality == QUALITY_LOW)
glTexImage2D(GL_TEXTURE_2D, 0, format, width_low, height_low, 0, globalFormat, type, NULL);
else
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, globalFormat, type, NULL);
//cout << "recreate texture " << gluid << " quality: " << currentQuality << endl;
//cout << "init texture " << gluid << " " << width << " " << height << " " << numChannel << endl;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); //GL_CLAMP_TO_BORDER GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
glBindTexture(GL_TEXTURE_2D, 0);
created = true;
}
// cout << "initTexture" << endl;
if (!initialized) {
glActiveTexture(glunit);
glBindTexture(GL_TEXTURE_2D, gluid);
if (currentQuality == QUALITY_LOW) {
if(data_low) {
//unsigned int t = Utils::getTime();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_low, height_low, globalFormat, type, data_low);
//cout << numChannel << " glTexSubImage2D (low) time: " << Utils::getTime() - t << endl;
}
}
else {
if(data) {
//unsigned int t = Utils::getTime();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, globalFormat, type, data);
//cout << numChannel << " glTexSubImage2D (height) time: " << Utils::getTime() - t << endl;
}
}
initialized = true;
glBindTexture(GL_TEXTURE_2D, 0);
}
}
// for terrain
Texture::Texture(int width, int height, int numchannels, unsigned int ind, bool multires): data(0), data_low(0), initialized(false), created(false) {
this->numChannel = numchannels;
this->width = width;
this->height = height;
this->gluid = 0;
this->index = ind;
this->multiRes = multires;
this->currentQuality = QUALITY_HIGH;
}
void Texture::freeTexture(bool freedata) {
if(gluid)
glDeleteTextures(1, &gluid);
initialized = false;
created = false;
gluid = 0;
if(freedata && data) {
stbi_image_free(data);
data = NULL;
}
if(freedata && data_low) {
stbi_image_free(data_low);
data_low = NULL;
}
}
Texture::~Texture() {
//glActiveTexture(glunit);
if(gluid)
glDeleteTextures(1, &gluid);
if(data) {
stbi_image_free(data);
data = NULL;
}
if(data_low) {
stbi_image_free(data_low);
data_low = NULL;
}
}
void Texture::bind() {
if(!gluid)
return;
glActiveTexture(glunit);
glBindTexture(GL_TEXTURE_2D, gluid);
}
void Texture::unbind() {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
}
int Texture::getWidth() {
return width;
}
int Texture::getHeight() {
return height;
}
// static
unsigned int Texture::unitCount = 0;
float Texture::borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
float Texture::borderColorB[] = {0.0f, 0.0f, 0.0f, 0.0f};
unsigned int Texture::unitFromIndex(unsigned int index)
{
switch(index)
{
case 1: return GL_TEXTURE1;
case 2: return GL_TEXTURE2;
case 3: return GL_TEXTURE3;
case 4: return GL_TEXTURE4;
case 5: return GL_TEXTURE5;
case 6: return GL_TEXTURE6;
case 7: return GL_TEXTURE7;
case 8: return GL_TEXTURE8;
case 9: return GL_TEXTURE9;
default: return GL_TEXTURE0;
}
}
}; //namespace tessterrain