forked from ntoand/windmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorTexture.cpp
211 lines (185 loc) · 6.15 KB
/
ColorTexture.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
#include "ColorTexture.h"
#include <iostream>
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
using namespace std;
namespace windmap {
const unsigned int ColorTexture::LINEAR = GL_LINEAR;
const unsigned int ColorTexture::NEAREST = GL_NEAREST;
const unsigned int ColorTexture::MIPMAP = GL_LINEAR_MIPMAP_LINEAR;
// for colormap
/*
ColorTexture::ColorTexture(const char* filename, unsigned int _format, unsigned int _globalFormat) {
FILE* fp;
char* content = NULL;
fp = fopen( filename, "rb" );
if(fp == NULL) {
cout << "Error: cannot load file " << filename << endl;
exit(0);
}
//load content (rgb1)
unsigned int length;
fseek( fp, 0, SEEK_END );
length = ftell( fp );
fseek( fp, 0, SEEK_SET );
content = new char [length];
fread( content, sizeof( char ), length, fp );
fclose( fp );
width = length / 4;
height = 1;
//cout << "width: " << width << endl;
//unsigned char* buff = (unsigned char*) content;
//for(int i=0; i < length; i+=4)
// printf("%d %d %d %d %d\n", i / 4, buff[i], buff[i+1], buff[i+2], buff[i+3]);
//init texture
index = 0;
gluid = 0;
glunit = GL_TEXTURE0;
minFilter = GL_NEAREST;
magFilter = GL_NEAREST;
format = _format;
globalFormat = _globalFormat;
glActiveTexture(glunit);
glGenTextures(1, &gluid);
glBindTexture(GL_TEXTURE_2D, gluid);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, 1, 0, globalFormat, GL_UNSIGNED_BYTE, content); //GL_UNSIGNED_INT_8_8_8_8_REV
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);
}
*/
// for framebuffer
ColorTexture::ColorTexture(unsigned int _index, unsigned int _width, unsigned int _height,
unsigned int _format, unsigned int _globalFormat,
unsigned int _minFilter, unsigned int _magFilter)
{
index = _index;
glunit = unitFromIndex(_index);
gluid = 0;
width = _width;
height = _height;
minFilter = _minFilter;
magFilter = _magFilter;
format = _format;
globalFormat = _globalFormat;
glActiveTexture(glunit);
glGenTextures(1, &gluid);
glBindTexture(GL_TEXTURE_2D, gluid);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, globalFormat, GL_UNSIGNED_BYTE, NULL);
if(format == GL_DEPTH_COMPONENT)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColorB);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
glActiveTexture(GL_TEXTURE0);
}
ColorTexture::~ColorTexture() {
glDeleteTextures(1, &gluid);
}
void ColorTexture::bind() {
glActiveTexture(glunit);
glBindTexture(GL_TEXTURE_2D, gluid);
}
void ColorTexture::unbind() {
//glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
}
void ColorTexture::resize(unsigned int _width, unsigned int _height)
{
width = _width;
height = _height;
bind();
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, globalFormat, GL_UNSIGNED_BYTE, NULL);
}
void ColorTexture::update(const char* filename)
{
int x,y,n;
unsigned char* data;
data = stbi_load(filename, &x, &y, &n, 0);
if(!data) {
cout << "Failed to load texture " << filename << endl;
exit(0);
}
update(data, x, y);
stbi_image_free(data);
}
void ColorTexture::update(const unsigned char* buffer, unsigned int w, unsigned int h)
{
if(w != width || h != height)
{
width = w;
height = h;
bind();
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, globalFormat, GL_UNSIGNED_BYTE, buffer);
}
else
{
bind();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, globalFormat, GL_UNSIGNED_BYTE, buffer);
}
}
void ColorTexture::update(const float* buffer, unsigned int w, unsigned int h)
{
if(w != width || h != height)
{
width = w;
height = h;
bind();
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, globalFormat, GL_FLOAT, buffer);
}
else
{
bind();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, globalFormat, GL_FLOAT, buffer);
}
}
void ColorTexture::setFilters(unsigned int min, unsigned int mag)
{
minFilter = min;
magFilter = mag;
bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
glActiveTexture(GL_TEXTURE0);
}
// static
unsigned int ColorTexture::unitCount = 0;
float ColorTexture::borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
float ColorTexture::borderColorB[] = {0.0f, 0.0f, 0.0f, 0.0f};
void ColorTexture::resetUnit(int textureUnitOffset)
{
unitCount = textureUnitOffset;
}
ColorTexture* ColorTexture::newFromNextUnit(unsigned int _width, unsigned int _height, unsigned int _format, unsigned int _globalFormat)
{
return new ColorTexture(unitCount++, _width, _height, _format, _globalFormat);
}
unsigned int ColorTexture::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 gigapoint