gl_texture.h
4.6 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
#ifndef STIM_GL_TEXTURE_H
#define STIM_GL_TEXTURE_H
#include <math.h>
#include <iostream>
#include <vector>
#include "../grids/image_stack.h"
#include <GL/glut.h>
#include <GL/glext.h>
#include <stim/gl/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>
{
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;
/// Sets the internal texture_type, based on the data dimensions
void setTextureType(){
if (R[3] > 1) //if the third dimension is greater than 1
texture_type = GL_TEXTURE_3D; //this is a 3D texture
else if (R[2] > 1) //if the second dimension is greater than 1
texture_type = GL_TEXTURE_2D; //this is a 2D texture
else if (R[1] > 1) //if the dimension value is greater than 1
texture_type = GL_TEXTURE_1D; //this is a 1D texture
}
//initializes important variables
void init() {
texID = 0; //initialize texture ID to zero, default if OpenGL returns an error
memset(R, 0, sizeof)
}
public:
///default constructor
gl_texture() : image_stack<T>() {
}
///@param string path to the directory with the image files.
///Creates an instance of the gl_texture object with a path to the data.
gl_texture(std::string file_mask){
image_stack<T>::load_images(file_mask);
setTextureType();
}
///returns the dimentions of the data in the x, y, z directions.
vec<int> getSize(){
stim::vec<int> size(R[1], R[2], R[3]);
return size;
}
void getSize(size_t& x, size_t& y, size_t& z) {
x = R[0]; y = R[1]; z = R[2];
}
///@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;
}
///@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 setSpacing(float sx, float sy, float sz){
S[1] = sx;
S[2] = sy;
S[3] = sz;
}
///Returns a stim::vec that contains the x, y, z sizes of the voxel.
vec<float> getDims(){
vec<float> dims(S[1], S[2], S[3]);
return dims;
}
///@param file_mask specifies the file(s) to be loaded
/// Sets the path and calls the loader on that path.
void load(std::string file_mask){
image_stack<T>::load_images(file_mask);
setTextureType();
}
/// Returns an std::string path associated with an instance of the gl_texture class.
//std::string getPath()
//{
// return path;
//}
/// Returns the GLuint id of the texture created by/associated with the
/// instance of the gl_texture class.
GLuint getTexture(){
return texID;
}
/// 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);
glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, texWrap);
glTexParameteri(texture_type, GL_TEXTURE_WRAP_R, texWrap);
glTexImage3D(texture_type, 0, 1, R[1], R[2], R[3], 0, format, type, ptr);
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