texture.hpp 5.45 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <vector>
#include <typeinfo>

#include "Utils/os_spec.h"

namespace CGoGN
{

namespace Utils
{

template<typename T>
Texture<T>::Texture()
{
	m_data = NULL;
	m_width = 0;
	m_height = 0;
	m_cpp = 0;
	m_ilName = 0;
	m_texName = 0;
}

template<typename T>
Texture<T>::Texture(std::string fileName)
{
	ilInit();
	iluInit();
	ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	ilGenImages(1,&m_ilName);
	ilBindImage(m_ilName);
	ilLoadImage(fileName.c_str());
	
//	get the info of image
	m_width = ilGetInteger(IL_IMAGE_WIDTH);
	m_height = ilGetInteger(IL_IMAGE_HEIGHT);
	m_cpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL) / sizeof(T);
	m_data = reinterpret_cast<T*>(ilGetData());
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
64
	m_texName = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68 69 70 71 72 73
}

template<typename T>
Texture<T>::Texture( T* ptr, int w, int h, int cpp)
{
	m_data = new T[w*h*cpp];
	m_width = w;
	m_height = h;
	m_cpp = cpp;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
74 75
	m_ilName = 0;
	m_texName = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

template<typename T>
void Texture<T>::load( std::string fileName)
{
	if (m_ilName !=0)
		ilDeleteImages(1,&m_ilName);
	else if(m_data !=0)	
		delete[] m_data;
	if (m_texName != 0)
		glDeleteTextures( 1, &m_texName );

	ilInit();
	iluInit();
	ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
	ilEnable(IL_ORIGIN_SET);

	ilGenImages(1,&m_ilName);
	ilBindImage(m_ilName);
	ilLoadImage(fileName.c_str());
	
//	get the info of image
	m_width = ilGetInteger(IL_IMAGE_WIDTH);
	m_height = ilGetInteger(IL_IMAGE_HEIGHT);
	m_cpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL) / sizeof(T);
	m_data = reinterpret_cast<T*>(ilGetData());
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
102
	m_texName = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

template<typename T>
void Texture<T>::setData( T* ptr, int w, int h, int cpp)
{
	if (m_ilName !=0)
		ilDeleteImages(1,&m_ilName);
	else if(m_data !=0)	
		delete[] m_data;
	if (m_texName != 0)
		glDeleteTextures( 1, &m_texName );

	m_data = new T[w*h*cpp];
	m_width = w;
	m_height = h;
	m_cpp = cpp;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
119 120
	m_ilName = 0;
	m_texName = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
121 122 123 124 125
}

template<typename T>
Texture<T>::~Texture()
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
126 127 128
	if (m_ilName != 0)
		ilDeleteImages(1, &m_ilName);
	else if(m_data != 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
129 130 131 132 133 134 135 136 137 138
		delete[] m_data;

	if (m_texName != 0)
		glDeleteTextures( 1, &m_texName );
}

template<typename T>
void Texture<T>::initGL(GLint filter, GLenum format, const char* uniformName, GLuint prg)
{
	// allocate a texture name
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
139
	glGenTextures(1, &m_texName );
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141

	// select our current texture
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
142
	glBindTexture(GL_TEXTURE_2D, m_texName);
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter );
//	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
//	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

	GLenum ptype;
	if (typeid(T) == typeid(unsigned char))
		ptype = GL_UNSIGNED_BYTE;
    if (typeid(T) == typeid(unsigned short))
		ptype = GL_UNSIGNED_SHORT;
	if (typeid(T) == typeid(unsigned int))
		ptype = GL_UNSIGNED_INT;
	if (typeid(T) == typeid(float))
		ptype = GL_FLOAT;

	switch(format)
	{
	case GL_RGB:
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
162 163 164
		if (m_cpp != 3)
			CGoGNout << "Warning using GL_RGB with image not in RGB format"<<CGoGNendl;
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
165
	case GL_LUMINANCE:
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
166 167 168
		if (m_cpp != 1)
			CGoGNout << "Warning using GL_LUMINANCE with image not in grey level format"<<CGoGNendl;
		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
169 170 171 172 173 174 175 176 177 178 179 180

	}
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 	glTexImage2D (GL_TEXTURE_2D, 0, m_cpp, m_width, m_height, 0 , format, ptype, m_data);
//	glTexImage2D (GL_TEXTURE_2D, 0, m_cpp, m_width, m_height, 0 , GL_RGB, GL_UNSIGNED_BYTE , m_data);

	m_uniform_tex=glGetUniformLocationARB(prg,uniformName);
}

template<typename T>
void Texture<T>::useGL(unsigned int textureEngineIndex)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
181
	glActiveTextureARB(GL_TEXTURE0_ARB + textureEngineIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
182 183 184 185 186
	glUniform1iARB(m_uniform_tex, textureEngineIndex);
	glBindTexture(GL_TEXTURE_2D, m_texName);
	glEnable(GL_TEXTURE_2D);
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
187
} // namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
188

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
189
} // namespace CGoGN