Blame view

stim/gl/gl_texture.h 3.85 KB
197e13de   Pavel Govyadinov   Added new class g...
1
2
3
4
  #ifndef STIM_GL_TEXTURE_H
  #define STIM_GL_TEXTURE_H
  
  
a4042c24   Pavel Govyadinov   Updated the gl_te...
5
6
7
8
9
10
11
12
13
14
15
  
  
  /*
  includes not necessary (yet)
  
  #include <iterator>
  #include <algorithm>
  
  
  */
  
197e13de   Pavel Govyadinov   Added new class g...
16
17
  #include <math.h>
  #include <iostream>
197e13de   Pavel Govyadinov   Added new class g...
18
  #include <vector>
197e13de   Pavel Govyadinov   Added new class g...
19
  #include "../grids/image_stack.h"
846040be   Pavel Govyadinov   working version
20
21
  #include <GL/glut.h>
  //#include <GL/glext.h>
a4042c24   Pavel Govyadinov   Updated the gl_te...
22
  #include "./error.h"	
197e13de   Pavel Govyadinov   Added new class g...
23
24
  namespace stim{
  
a4042c24   Pavel Govyadinov   Updated the gl_te...
25
26
27
28
29
  /*
  class gl_texture
  	Uses image_stack class in order to create a texture object.
  */
  
197e13de   Pavel Govyadinov   Added new class g...
30
  template<typename T>
a4042c24   Pavel Govyadinov   Updated the gl_te...
31
  class gl_texture : public virtual image_stack<T>
197e13de   Pavel Govyadinov   Added new class g...
32
  {
372b5963   Pavel Govyadinov   added support for...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  	private:
  		/*
  		Method: setTextureType
  		Inputs:
  		Outputs:
  			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;
  		}
197e13de   Pavel Govyadinov   Added new class g...
52
  	protected:
a4042c24   Pavel Govyadinov   Updated the gl_te...
53
  		std::string path;
372b5963   Pavel Govyadinov   added support for...
54
55
  		GLuint texID;				//OpenGL object
  		GLenum texture_type;			//1D, 2D, 3D
a4042c24   Pavel Govyadinov   Updated the gl_te...
56
57
58
  		using image_stack<T>::R;
  		using image_stack<T>::ptr;
  		using image_stack<T>::samples;
197e13de   Pavel Govyadinov   Added new class g...
59
60
61
  
  	public:
  
a4042c24   Pavel Govyadinov   Updated the gl_te...
62
63
64
65
66
67
  		/*
  		Method: Basic Constructor
  		Inputs:
  		Outputs:
  			Creates an instance of the gl_texture object.
  		*/
372b5963   Pavel Govyadinov   added support for...
68
69
70
  		gl_texture()
  		{
  		
a4042c24   Pavel Govyadinov   Updated the gl_te...
71
  		}
197e13de   Pavel Govyadinov   Added new class g...
72
  
a4042c24   Pavel Govyadinov   Updated the gl_te...
73
74
75
76
77
78
  		/*
  		Method: Path Constructor
  		Inputs: string file_path
  		Outputs:
  			Creates an instance of the gl_texture object with a path to the data.
  		*/
372b5963   Pavel Govyadinov   added support for...
79
  
a4042c24   Pavel Govyadinov   Updated the gl_te...
80
81
  		gl_texture(std::string file_path)
  		{
197e13de   Pavel Govyadinov   Added new class g...
82
  			path = file_path;
372b5963   Pavel Govyadinov   added support for...
83
84
  			image_stack<T>::load_images(path.append("/*.jpg"));
  			setTextureType();
197e13de   Pavel Govyadinov   Added new class g...
85
  		}
a4042c24   Pavel Govyadinov   Updated the gl_te...
86
87
88
89
90
91
92
93
94
  
  		/*
  		Method: setPath
  		Inputs:string file_Path
  		Outputs:
  			sets the protected path variable of an instance of the gl_texture class
  			to the method input.
  		*/
  
197e13de   Pavel Govyadinov   Added new class g...
95
96
97
98
  		void
  		setPath(std::string file_path)
  		{
  			path = file_path;
372b5963   Pavel Govyadinov   added support for...
99
100
  			image_stack<T>::load_images(path.append("/*.jpg"));
  			setTextureType();
197e13de   Pavel Govyadinov   Added new class g...
101
102
  		}
  		
a4042c24   Pavel Govyadinov   Updated the gl_te...
103
104
105
106
107
108
109
  		/*
  		Method: getPath
  		Inputs:
  		Outputs: string path
  			Returns the path associated with an instance of the gl_texture class.
  		*/
  
197e13de   Pavel Govyadinov   Added new class g...
110
111
112
113
114
  		std::string
  		getPath()
  		{
  			return path;
  		}
a4042c24   Pavel Govyadinov   Updated the gl_te...
115
116
117
118
119
120
121
122
123
  
  		/*
  		Method: getTexture
  		Inputs:
  		Outputs: GLuint texID
  			Returns the id of the texture create by/associated with the 
  			instance of the gl_texture class.
  		*/
  				
197e13de   Pavel Govyadinov   Added new class g...
124
  		GLuint
a4042c24   Pavel Govyadinov   Updated the gl_te...
125
  		getTexture()
197e13de   Pavel Govyadinov   Added new class g...
126
  		{
a4042c24   Pavel Govyadinov   Updated the gl_te...
127
  			return texID;
197e13de   Pavel Govyadinov   Added new class g...
128
129
  		}
  	
a4042c24   Pavel Govyadinov   Updated the gl_te...
130
131
132
133
134
135
136
  		/*
  		Method: createTexture
  		Inputs:
  		Outputs:
  			Creates a texture and from the data located at <path> and
  			assigns that texture to texID
  		*/
372b5963   Pavel Govyadinov   added support for...
137
138
139
140
  		//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.	
197e13de   Pavel Govyadinov   Added new class g...
141
142
143
  		void
  		createTexture()
  		{
154fe135   Pavel Govyadinov   fixed some bad co...
144
145
146
147
148
149
150
151
152
  			glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  			glGenTextures(1, &texID);
  			glBindTexture(texture_type, texID);
  			glTexParameteri(texture_type,
  				 GL_TEXTURE_MIN_FILTER,
  				 GL_LINEAR);
  			glTexParameteri(texture_type,
  				 GL_TEXTURE_MAG_FILTER,
  				 GL_LINEAR);
372b5963   Pavel Govyadinov   added support for...
153
154
  			switch(texture_type)
  			{
372b5963   Pavel Govyadinov   added support for...
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
  				case GL_TEXTURE_3D:
  					glTexParameteri(texture_type,
  						 GL_TEXTURE_WRAP_S, GL_REPEAT);
  					glTexParameteri(texture_type,
  						 GL_TEXTURE_WRAP_T, GL_REPEAT);
  					glTexParameteri(texture_type,
  						 GL_TEXTURE_WRAP_R, GL_REPEAT);
  					glTexImage3D(texture_type,
  						0,
  					//	GL_RGB16,
  						1,
  						R[1],
  						R[2],
  						R[3],
  						0,
  						GL_LUMINANCE,
  						GL_UNSIGNED_BYTE, 
  						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, GL_REPEAT);
  					glTexParameteri(texture_type,
  						 GL_TEXTURE_WRAP_T, GL_REPEAT);
  					glTexImage2D(texture_type,
  						0,
  						1,
  						R[1],
  						R[2],
  						0,
  						GL_LUMINANCE,
  						GL_UNSIGNED_BYTE, 
  						ptr);
  					break;
372b5963   Pavel Govyadinov   added support for...
191
  			}
197e13de   Pavel Govyadinov   Added new class g...
192
  		}
372b5963   Pavel Govyadinov   added support for...
193
194
195
196
  		/*
  			Temporary methods for debugging and testing are below.
  			Self-explanatory.
  		*/
63fcb5c8   Pavel Govyadinov   added some tempor...
197
198
199
200
201
202
203
204
205
206
207
208
  		stim::vec<unsigned long,4>
  		getDims()
  		{
  			return R;
  		}
  		
  		T*
  		getData()
  		{
  			return ptr;
  		}
  		
197e13de   Pavel Govyadinov   Added new class g...
209
  
197e13de   Pavel Govyadinov   Added new class g...
210
  		
a4042c24   Pavel Govyadinov   Updated the gl_te...
211
  	};
197e13de   Pavel Govyadinov   Added new class g...
212
213
214
215
216
217
218
219
  }
  
  
  	
  	
  
  
  #endif