texturesExample.cpp 7.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
24 25 26 27

#include "texturesExample.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Modelisation/polyhedron.h"
Thery Sylvain's avatar
Thery Sylvain committed
28
#include "Utils/vbo.h" 
29 30

TexView::TexView():
31
	m_obj(myMap),
32 33
	m_positionVBO(NULL),
	m_texcoordVBO(NULL),
34
	m_nbIndices(0),
35 36
	m_texture(NULL),
	m_shader(NULL),
37 38
	m_shader2(NULL),
	m_phong(true)
39
{}
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

TexView::~TexView()
{
	delete m_shader;
	delete m_positionVBO;
	delete m_texcoordVBO;
	delete m_texture;
}

void TexView::cb_initGL()
{
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);

	// create VBO for position
	m_positionVBO = new Utils::VBO;
	m_texcoordVBO = new Utils::VBO;
57
	m_normalVBO = new Utils::VBO;
58 59 60

	m_texture = new Utils::Texture<2,Geom::Vec3uc>(GL_UNSIGNED_BYTE);

61 62 63 64 65 66 67 68
	if (m_texture->load(m_fileNameTex))
		m_texture->update();
	else
		computeImage();
//		CGoGNerr << "Problem loading image"<< CGoGNendl;

	m_texture->setWrapping(GL_CLAMP_TO_EDGE);
	m_texture->update();
69 70 71 72 73 74 75 76

	m_shader = new Utils::ShaderSimpleTexture();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setAttributeTexCoord(m_texcoordVBO);
	m_shader->setTextureUnit(GL_TEXTURE0);
	m_shader->setTexture(m_texture);
	registerShader(m_shader);

77
	m_shader2 = new Utils::ShaderPhongTexture();
78 79
	m_shader2->setAttributePosition(m_positionVBO);
	m_shader2->setAttributeTexCoord(m_texcoordVBO);
80 81 82 83 84 85
	m_shader2->setAttributeNormal(m_normalVBO);
	m_shader2->setTextureUnit(GL_TEXTURE1);
	m_shader2->setTexture(m_texture);
	m_shader2->setShininess(10.0f);
	m_shader2->setAmbient(0.1f);
	m_shader2->setSpecular(Geom::Vec4f(0.5));
86 87
	registerShader(m_shader2);

88

89 90
	glEnable(GL_TEXTURE_2D);

91 92 93 94 95 96 97 98 99 100 101 102
	if (!m_obj.hasNormals())
	{
		VertexAttribute<Geom::Vec3f> normal = myMap.getAttribute<VEC3, VERTEX>("normal") ;
		if(!normal.isValid())
			normal = myMap.addAttribute<VEC3, VERTEX>("normal") ;

		Algo::Surface::Geometry::computeNormalVertices<PFP>(myMap, m_obj.m_positions, normal) ;
		m_obj.setNormalAttribute(normal);
	}

	m_nbIndices = m_obj.createSimpleVBO_PTN(m_positionVBO,m_texcoordVBO,m_normalVBO);

103 104 105 106 107
}

void TexView::cb_redraw()
{
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
108 109

	if (m_phong)
110
	{
111 112 113 114
		m_shader2->activeTexture();
		m_shader2->enableVertexAttribs();
		glDrawArrays(GL_TRIANGLES, 0, m_nbIndices);
		m_shader2->disableVertexAttribs();
115
	}
116 117 118 119 120 121 122 123 124
	else
	{
		m_shader->activeTexture();
		m_shader->enableVertexAttribs();
		glDrawArrays(GL_TRIANGLES, 0, m_nbIndices);
		m_shader->disableVertexAttribs();
	}


125 126 127 128 129 130
}

void TexView::cb_keyPress(int code)
{
	switch(code)
	{
131 132
	case 'p':
		m_phong = !m_phong;
133 134 135 136 137 138
	case 'l':
		m_texture->setFiltering(GL_LINEAR);
		break;
	case 'n':
		m_texture->setFiltering(GL_NEAREST);
		break;
139 140
	case '1':
		m_shader2->setShininess(10.0f);
141
		break;
142 143
	case '2':
		m_shader2->setShininess(100.0f);
144
		break;
145 146
	case '3':
		m_shader2->setShininess(500.0f);
147
		break;
148 149
	case '4':
		m_shader2->setShininess(25000.0f);
150
		break;
151 152 153
	case '5':
		m_shader2->setShininess(1.0f);
		m_shader2->setSpecular(Geom::Vec4f(0));
154
		break;
155

156 157 158 159
	}
	updateGL();
}

Sylvain Thery's avatar
Sylvain Thery committed
160
void TexView::init(const std::string& fnm, const std::string& fnt)
161
{
Sylvain Thery's avatar
Sylvain Thery committed
162
	if (fnm.empty()	)
163
	{
164
		computeTore();
Sylvain Thery's avatar
Sylvain Thery committed
165
		m_fileNameTex  = fnt;
166 167 168
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
169 170
		m_fileNameMesh = fnm;
		m_fileNameTex  = fnt;
171 172 173
		std::vector<std::string> attrNames;

		m_obj.import(m_fileNameMesh,attrNames);
174 175
	}

176 177 178
	Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, m_obj.m_positions);
	float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
	Geom::Vec3f lPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);
179

180 181
	// send BB info to interface for centering on GL screen
	setParamObject(lWidthObj, lPosObj.data());
182 183 184

}

185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
void TexView::computeImage()
{
	std::vector<Geom::Vec3f> colorTable;
	colorTable.push_back(Geom::Vec3uc(255,0,0));
	colorTable.push_back(Geom::Vec3uc(0,255,0));
	colorTable.push_back(Geom::Vec3uc(0,0,255));
	colorTable.push_back(Geom::Vec3uc(255,255,0));
	colorTable.push_back(Geom::Vec3uc(255,0,255));
	colorTable.push_back(Geom::Vec3uc(0,255,255));
	colorTable.push_back(Geom::Vec3uc(255,255,255));

	m_texture->create(Geom::Vec2ui(512,512));

#define WIDTHCHECKER 20

	for (unsigned int ki = 0; ki < m_texture->size()[0]; ++ki)
	{
		for (unsigned int kj = 0; kj <  m_texture->size()[1]; ++kj)
		{
			unsigned int kc = ((kj/WIDTHCHECKER)*m_texture->size()[0] + (ki/WIDTHCHECKER))%7;
			(*m_texture)(ki,kj) =colorTable[kc];
		}
	}
#undef WIDTHCHECKER
}

212
void TexView::computeTore()
213 214 215
{
#define NB 96

216 217 218
	VertexAttribute<VEC3> position = myMap.addAttribute<VEC3, VERTEX>("position");
	VertexAttribute<Geom::Vec2f> texcoord = myMap.addAttribute<Geom::Vec2f, VERTEX>("texcoord");
	Algo::Surface::Modelisation::Polyhedron<PFP> prim(myMap, position);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	prim.tore_topo(NB, NB);
	prim.embedTore(40.0f,20.0f);
	Dart d = prim.getDart();
	for(unsigned int i=0; i<NB; ++i)
	{
		for(unsigned int j=0; j<NB; ++j)
		{
			float a;
			if (i<=NB/2)
				a = (2.0f/NB)*i;
			else
				a = (2.0f/NB)*(NB-i);
			float b;
			if (j<=NB/2)
				b = (2.0f/NB)*j;
			else
				b =(2.0f/NB)*(NB-j);

			texcoord[d] = Geom::Vec2f(a,b);
238
			d = myMap.phi<121>(d);
239
		}
240
		d = myMap.phi<211>(d);
241 242 243
	}
#undef NB

244 245 246
	m_obj.setPositionAttribute(position);
	m_obj.setTexCoordAttribute(texcoord);
}
247 248 249



250 251 252 253 254 255 256 257 258 259 260 261 262

int main(int argc, char**argv)
{
	// interface:
	QApplication app(argc, argv);
	TexView tv;

	if (argc == 3)
	{
		tv.init(argv[1], argv[2]);
	}
	else if (argc == 2)
	{
Sylvain Thery's avatar
Sylvain Thery committed
263
		tv.init("", argv[1]);
264 265 266
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
267
		tv.init("","x");
268 269 270
	}


271 272
	tv.show();

273

274 275 276
	// et on attend la fin.
	return app.exec();
}