gl_texture.h 3.85 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:
		/*
		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;
		}
	protected:
		std::string path;
		GLuint texID;				//OpenGL object
		GLenum texture_type;			//1D, 2D, 3D
		using image_stack<T>::R;
		using image_stack<T>::ptr;
		using image_stack<T>::samples;

	public:

		/*
		Method: Basic Constructor
		Inputs:
		Outputs:
			Creates an instance of the gl_texture object.
		*/
		gl_texture()
		{
		
		}

		/*
		Method: Path Constructor
		Inputs: string file_path
		Outputs:
			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();
		}

		/*
		Method: setPath
		Inputs:string file_Path
		Outputs:
			sets the protected path variable of an instance of the gl_texture class
			to the method input.
		*/

		void
		setPath(std::string file_path)
		{
			path = file_path;
			image_stack<T>::load_images(path.append("/*.jpg"));
			setTextureType();
		}
		
		/*
		Method: getPath
		Inputs:
		Outputs: string path
			Returns the path associated with an instance of the gl_texture class.
		*/

		std::string
		getPath()
		{
			return path;
		}

		/*
		Method: getTexture
		Inputs:
		Outputs: GLuint texID
			Returns the id of the texture create by/associated with the 
			instance of the gl_texture class.
		*/
				
		GLuint
		getTexture()
		{
			return texID;
		}
	
		/*
		Method: createTexture
		Inputs:
		Outputs:
			Creates a texture and from the data located at <path> 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,
				 GL_LINEAR);
			glTexParameteri(texture_type,
				 GL_TEXTURE_MAG_FILTER,
				 GL_LINEAR);
			switch(texture_type)
			{
				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;
			}
		}
		/*
			Temporary methods for debugging and testing are below.
			Self-explanatory.
		*/
		stim::vec<unsigned long,4>
		getDims()
		{
			return R;
		}
		
		T*
		getData()
		{
			return ptr;
		}
		

		
	};
}


	
	


#endif