gl_texture.h 4.73 KB
#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:
		///	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:

		///default constructor
		gl_texture()
		{
			
		}

		///@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_path)
		{
			path = file_path;
			image_stack<T>::load_images(path.append("/*.jpg"));
			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;
		}

		///@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
		setDims(float x, float y, float z)
		{
			S[1] = x;
			S[2] = y;
			S[3] = z;
		}

		///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_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();
		}
		
		///	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);
			// 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