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

#include "texturesExample.h"
#include "Algo/Geometry/boundingbox.h"
untereiner's avatar
untereiner committed
27
#include "Algo/Tiling/Surface/square.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

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

void TexView::cb_initGL()
{
	// create VBO for position
	m_positionVBO = new Utils::VBO;
	m_texcoordVBO = new Utils::VBO;
54
	m_normalVBO = new Utils::VBO;
55 56 57

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

58 59 60 61 62 63 64 65
	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();
66 67 68 69 70 71 72 73

	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);

74
	m_shader2 = new Utils::ShaderPhongTexture();
75 76
	m_shader2->setAttributePosition(m_positionVBO);
	m_shader2->setAttributeTexCoord(m_texcoordVBO);
77 78 79 80 81 82
	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));
83 84 85 86
	registerShader(m_shader2);

	glEnable(GL_TEXTURE_2D);

87 88
	if (!m_obj.hasNormals())
	{
89
		VertexAttribute<Geom::Vec3f, MAP> normal = myMap.getAttribute<VEC3, VERTEX, MAP>("normal") ;
90
		if(!normal.isValid())
91
			normal = myMap.addAttribute<VEC3, VERTEX, MAP>("normal") ;
92 93 94 95 96 97

		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);
98 99 100 101 102
}

void TexView::cb_redraw()
{
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
103 104

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

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

Sylvain Thery's avatar
Sylvain Thery committed
152
void TexView::init(const std::string& fnm, const std::string& fnt)
153
{
Sylvain Thery's avatar
Sylvain Thery committed
154
	if (fnm.empty()	)
155
	{
156
		computeTore();
Sylvain Thery's avatar
Sylvain Thery committed
157
		m_fileNameTex  = fnt;
158 159 160
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
161 162
		m_fileNameMesh = fnm;
		m_fileNameTex  = fnt;
163 164 165
		std::vector<std::string> attrNames;

		m_obj.import(m_fileNameMesh,attrNames);
166 167
	}

168 169 170
	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);
171

172 173
	// send BB info to interface for centering on GL screen
	setParamObject(lWidthObj, lPosObj.data());
174 175 176 177 178 179 180 181 182 183 184 185 186
}

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));

Sylvain Thery's avatar
Sylvain Thery committed
187
	m_texture->create(Geom::Vec2ui(512,512));
188 189 190

#define WIDTHCHECKER 20

Sylvain Thery's avatar
Sylvain Thery committed
191
	for (unsigned int ki = 0; ki < m_texture->size()[0]; ++ki)
192
	{
Sylvain Thery's avatar
Sylvain Thery committed
193
		for (unsigned int kj = 0; kj <  m_texture->size()[1]; ++kj)
194
		{
Sylvain Thery's avatar
Sylvain Thery committed
195 196
			unsigned int kc = ((kj/WIDTHCHECKER)*m_texture->size()[0] + (ki/WIDTHCHECKER))%7;
			(*m_texture)(ki,kj) =colorTable[kc];
197 198 199 200 201
		}
	}
#undef WIDTHCHECKER
}

202
void TexView::computeTore()
203 204 205
{
#define NB 96

206 207
	VertexAttribute<VEC3, MAP> position = myMap.addAttribute<VEC3, VERTEX, MAP>("position");
	VertexAttribute<Geom::Vec2f, MAP> texcoord = myMap.addAttribute<Geom::Vec2f, VERTEX, MAP>("texcoord");
untereiner's avatar
untereiner committed
208 209 210
    //Algo::Surface::Modelisation::Polyhedron<PFP> prim(myMap, position);
    Algo::Surface::Tilings::Square::Tore<PFP> prim(myMap, NB, NB);
    prim.embedIntoTore(position, 40.0f,20.0f);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	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);
228
			d = myMap.phi<121>(d);
229
		}
230
		d = myMap.phi<211>(d);
231 232 233
	}
#undef NB

234 235 236
	m_obj.setPositionAttribute(position);
	m_obj.setTexCoordAttribute(texcoord);
}
237

238 239 240 241 242 243 244 245 246 247 248 249
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
250
		tv.init("", argv[1]);
251 252 253
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
254
		tv.init("","x");
255 256
	}

257 258 259 260 261
	tv.show();

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