Commit 372b596366ab3b93833a9ed12b3a6bfb6a6b7f1e
1 parent
846040be
added support for 2d and 3d textures. Cleaned up the code and reformulated the constructor.
Showing
1 changed file
with
85 additions
and
49 deletions
Show diff stats
gl/gl_texture.h
... | ... | @@ -30,9 +30,29 @@ class gl_texture |
30 | 30 | template<typename T> |
31 | 31 | class gl_texture : public virtual image_stack<T> |
32 | 32 | { |
33 | + private: | |
34 | + /* | |
35 | + Method: setTextureType | |
36 | + Inputs: | |
37 | + Outputs: | |
38 | + Sets the internal texture_type, based on the data | |
39 | + size. Either 3D, 2D, 1D textures. | |
40 | + */ | |
41 | + | |
42 | + void | |
43 | + setTextureType() | |
44 | + { | |
45 | + if (R[3] > 1) | |
46 | + texture_type = GL_TEXTURE_3D; | |
47 | + else if (R[3] == 1 && R[2] == 0) | |
48 | + texture_type = GL_TEXTURE_1D; | |
49 | + else if (R[3] == 1) | |
50 | + texture_type = GL_TEXTURE_2D; | |
51 | + } | |
33 | 52 | protected: |
34 | 53 | std::string path; |
35 | - GLuint texID; | |
54 | + GLuint texID; //OpenGL object | |
55 | + GLenum texture_type; //1D, 2D, 3D | |
36 | 56 | using image_stack<T>::R; |
37 | 57 | using image_stack<T>::ptr; |
38 | 58 | using image_stack<T>::samples; |
... | ... | @@ -45,7 +65,9 @@ class gl_texture : public virtual image_stack<T> |
45 | 65 | Outputs: |
46 | 66 | Creates an instance of the gl_texture object. |
47 | 67 | */ |
48 | - gl_texture(){ | |
68 | + gl_texture() | |
69 | + { | |
70 | + | |
49 | 71 | } |
50 | 72 | |
51 | 73 | /* |
... | ... | @@ -54,9 +76,12 @@ class gl_texture : public virtual image_stack<T> |
54 | 76 | Outputs: |
55 | 77 | Creates an instance of the gl_texture object with a path to the data. |
56 | 78 | */ |
79 | + | |
57 | 80 | gl_texture(std::string file_path) |
58 | 81 | { |
59 | 82 | path = file_path; |
83 | + image_stack<T>::load_images(path.append("/*.jpg")); | |
84 | + setTextureType(); | |
60 | 85 | } |
61 | 86 | |
62 | 87 | /* |
... | ... | @@ -71,6 +96,8 @@ class gl_texture : public virtual image_stack<T> |
71 | 96 | setPath(std::string file_path) |
72 | 97 | { |
73 | 98 | path = file_path; |
99 | + image_stack<T>::load_images(path.append("/*.jpg")); | |
100 | + setTextureType(); | |
74 | 101 | } |
75 | 102 | |
76 | 103 | /* |
... | ... | @@ -107,56 +134,66 @@ class gl_texture : public virtual image_stack<T> |
107 | 134 | Creates a texture and from the data located at <path> and |
108 | 135 | assigns that texture to texID |
109 | 136 | */ |
110 | - //TO DO :::: Add handling for 2D and 1D textures | |
137 | + //TO DO :::: 1D textures | |
138 | + //TO DO:::add methods for handling the cases of T | |
139 | + // and convert them to GL equivalent. | |
140 | + // i.e. an overloaded function that handles paramenter conversion. | |
111 | 141 | void |
112 | 142 | createTexture() |
113 | 143 | { |
114 | - | |
115 | - image_stack<T>::load_images(path.append("/*.jpg")); | |
116 | - | |
117 | - glPixelStorei(GL_UNPACK_ALIGNMENT,1); | |
118 | - glGenTextures(1, &texID); | |
119 | - CHECK_OPENGL_ERROR | |
120 | - glBindTexture(GL_TEXTURE_3D, texID); | |
121 | - CHECK_OPENGL_ERROR | |
122 | - glTexParameteri(GL_TEXTURE_3D, | |
123 | - GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
124 | - glTexParameteri(GL_TEXTURE_3D, | |
125 | - GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
126 | - glTexParameteri(GL_TEXTURE_3D, | |
127 | - GL_TEXTURE_WRAP_S, GL_REPEAT); | |
128 | - | |
129 | - CHECK_OPENGL_ERROR | |
130 | - glTexParameteri(GL_TEXTURE_3D, | |
131 | - GL_TEXTURE_WRAP_S, GL_REPEAT); | |
132 | - glTexParameteri(GL_TEXTURE_3D, | |
133 | - GL_TEXTURE_WRAP_T, GL_REPEAT); | |
134 | - glTexParameteri(GL_TEXTURE_3D, | |
135 | - GL_TEXTURE_WRAP_R, GL_REPEAT); | |
136 | - CHECK_OPENGL_ERROR | |
137 | - glTexImage3D(GL_TEXTURE_3D, | |
138 | - 0, | |
139 | - // GL_RGB16, | |
140 | - 1, | |
141 | - R[1], | |
142 | - R[2], | |
143 | - R[3], | |
144 | - 0, | |
145 | - GL_LUMINANCE, | |
146 | - // GL_RGB, | |
147 | - GL_UNSIGNED_BYTE, | |
148 | - ptr); | |
149 | - //GL_UNSIGNED_BYTE can be TYPES, convert to GL equivalents | |
150 | - CHECK_OPENGL_ERROR | |
151 | - glPixelStorei(GL_PACK_ALIGNMENT,1); | |
152 | - } | |
153 | - //Binds the texture | |
154 | - void | |
155 | - Bind() | |
156 | - { | |
157 | - glBindTexture(GL_TEXTURE_3D, texID); | |
144 | + switch(texture_type) | |
145 | + { | |
146 | + glPixelStorei(GL_UNPACK_ALIGNMENT,1); | |
147 | + glGenTextures(1, &texID); | |
148 | + glBindTexture(texture_type, texID); | |
149 | + glTexParameteri(texture_type, | |
150 | + GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
151 | + glTexParameteri(texture_type, | |
152 | + GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
153 | + case GL_TEXTURE_3D: | |
154 | + glTexParameteri(texture_type, | |
155 | + GL_TEXTURE_WRAP_S, GL_REPEAT); | |
156 | + glTexParameteri(texture_type, | |
157 | + GL_TEXTURE_WRAP_T, GL_REPEAT); | |
158 | + glTexParameteri(texture_type, | |
159 | + GL_TEXTURE_WRAP_R, GL_REPEAT); | |
160 | + glTexImage3D(texture_type, | |
161 | + 0, | |
162 | + // GL_RGB16, | |
163 | + 1, | |
164 | + R[1], | |
165 | + R[2], | |
166 | + R[3], | |
167 | + 0, | |
168 | + GL_LUMINANCE, | |
169 | + GL_UNSIGNED_BYTE, | |
170 | + ptr); | |
171 | + //GL_UNSIGNED_BYTE can be TYPES, convert to GL equivalents | |
172 | + glPixelStorei(GL_PACK_ALIGNMENT,1); | |
173 | + break; | |
174 | + case GL_TEXTURE_2D: | |
175 | + glTexParameteri(texture_type, | |
176 | + GL_TEXTURE_WRAP_S, GL_REPEAT); | |
177 | + glTexParameteri(texture_type, | |
178 | + GL_TEXTURE_WRAP_T, GL_REPEAT); | |
179 | + glTexImage2D(texture_type, | |
180 | + 0, | |
181 | + 1, | |
182 | + R[1], | |
183 | + R[2], | |
184 | + 0, | |
185 | + GL_LUMINANCE, | |
186 | + GL_UNSIGNED_BYTE, | |
187 | + ptr); | |
188 | + break; | |
189 | + default: | |
190 | + break; | |
191 | + } | |
158 | 192 | } |
159 | - | |
193 | + /* | |
194 | + Temporary methods for debugging and testing are below. | |
195 | + Self-explanatory. | |
196 | + */ | |
160 | 197 | stim::vec<unsigned long,4> |
161 | 198 | getDims() |
162 | 199 | { |
... | ... | @@ -170,7 +207,6 @@ class gl_texture : public virtual image_stack<T> |
170 | 207 | } |
171 | 208 | |
172 | 209 | |
173 | - //TO DO:::add methods for handling the cases of T and convert them to GL equivalent. | |
174 | 210 | |
175 | 211 | }; |
176 | 212 | } | ... | ... |