gl_texture.h 4.6 KB
#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