gl_texture.h
4.98 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#ifndef STIM_GL_TEXTURE_H
#define STIM_GL_TEXTURE_H
/*
includes not necessary (yet)
#include <iterator>
#include <algorithm>
*/
#include <math.h>
#include <iostream>
#include <vector>
#include "../grids/image_stack.h"
#include <GL/glut.h>
//#include <GL/glext.h>
#include "./error.h"
namespace stim{
/*
class gl_texture
Uses image_stack class in order to create a texture object.
*/
template<typename T>
class gl_texture : public virtual image_stack<T>
{
private:
///Method: setTextureType
/// Sets the internal texture_type, based on the data
/// size. Either 3D, 2D, 1D textures.
void
setTextureType()
{
if (R[3] > 1)
texture_type = GL_TEXTURE_3D;
else if (R[3] == 1 && R[2] == 0)
texture_type = GL_TEXTURE_1D;
else if (R[3] == 1)
texture_type = GL_TEXTURE_2D;
}
protected:
std::string path;
GLuint texID; //OpenGL object
GLenum texture_type; //1D, 2D, 3D
GLint interpType;
GLint texWrap;
GLenum type;
GLenum format;
using image_stack<T>::R;
using image_stack<T>::S;
using image_stack<T>::ptr;
using image_stack<T>::samples;
public:
///Method: Basic Constructor
/// Creates an instance of the gl_texture object.
gl_texture()
{
}
///Method: Path Constructor
///@param string file_path path to the directory with the files.
/// Creates an instance of the gl_texture object with a path to the data.
gl_texture(std::string file_path)
{
path = file_path;
image_stack<T>::load_images(path.append("/*.jpg"));
setTextureType();
}
///Method:getSize
///returns the dimentions of
vec<int>
getSize()
{
stim::vec<int> size(R[1], R[2], R[3]);
return size;
}
///Method:setTexParam
///@param GLint interp --GL_LINEAR, GL_NEAREST...
///@param GLint twrap --GL_REPEAR, GL_CLAMP_TO_EDGE...
///@param GLenum dataType --GL_UNSIGNED_BYTE, GL_FLOAT16...
///@param GLenum dataFormat--GL_LUMINANCE, GL_RGB...
/// Texture paramenters.
void
setTexParam(GLint interp = GL_LINEAR,
GLint twrap = GL_CLAMP_TO_EDGE,
GLenum dataType = GL_UNSIGNED_BYTE,
GLenum dataFormat = GL_LUMINANCE)
{
interpType = interp;
texWrap = twrap;
type = dataType;
format = dataFormat;
}
///Method:setDims
///@param x size of the voxel in x direction
///@param y size of the voxel in y direction
///@param z size of the voxel in z direction
/// Sets the dimenstions of the voxels.
void
setDims(float x, float y, float z)
{
S[1] = x;
S[2] = y;
S[3] = z;
}
///Method:getDims
/// get the dimenstions of the voxels.
vec<float>
getDims()
{
vec<float> dims(S[1], S[2], S[3]);
return dims;
}
///Method:setPath
///@param file_Path location of the directory with the files
/// Sets the path and calls the loader on that path.
void
setPath(std::string file_path)
{
path = file_path;
image_stack<T>::load_images(path.append("/*.jpg"));
setTextureType();
}
///Method: getPath
///Outputs: string path
/// Returns the path associated with an instance of the gl_texture class.
std::string
getPath()
{
return path;
}
///Method: getTexture
///Outputs: GLuint texID
/// Returns the id of the texture create by/associated with the
/// instance of the gl_texture class.
GLuint
getTexture()
{
return texID;
}
///Method: createTexture
/// Creates a texture and from the loaded data and
/// assigns that texture to texID
//TO DO :::: 1D textures
//TO DO:::add methods for handling the cases of T
// and convert them to GL equivalent.
// i.e. an overloaded function that handles paramenter conversion.
void
createTexture()
{
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1, &texID);
glBindTexture(texture_type, texID);
glTexParameteri(texture_type,
GL_TEXTURE_MIN_FILTER,
interpType);
glTexParameteri(texture_type,
GL_TEXTURE_MAG_FILTER,
interpType);
switch(texture_type)
{
case GL_TEXTURE_3D:
glTexParameteri(texture_type,
GL_TEXTURE_WRAP_S,texWrap);
// GL_REPEAT);
// GL_CLAMP_TO_EDGE);
glTexParameteri(texture_type,
GL_TEXTURE_WRAP_T,texWrap);
// GL_REPEAT);
// GL_CLAMP_TO_EDGE);
glTexParameteri(texture_type,
GL_TEXTURE_WRAP_R,texWrap);
// GL_REPEAT);
// GL_CLAMP_TO_EDGE);
glTexImage3D(texture_type,
0,
// GL_RGB16,
1,
R[1],
R[2],
R[3],
0,
format,
type,
ptr);
//GL_UNSIGNED_BYTE can be TYPES, convert to GL equivalents
glPixelStorei(GL_PACK_ALIGNMENT,1);
break;
case GL_TEXTURE_2D:
glTexParameteri(texture_type,
GL_TEXTURE_WRAP_S, texWrap);
glTexParameteri(texture_type,
GL_TEXTURE_WRAP_T, texWrap);
glTexImage2D(texture_type,
0,
1,
R[1],
R[2],
0,
format,
type,
ptr);
break;
}
}
///Temporary methods for debugging and testing are below.
///Self-explanatory.
T*
getData()
{
return ptr;
}
};
}
#endif