texturesExample.cpp 7.88 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
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);

Sylvain Thery's avatar
Sylvain Thery committed
61
62
	m_textureWP = new Utils::Texture<2,Geom::Vec3uc>(GL_UNSIGNED_BYTE);

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

Sylvain Thery's avatar
Sylvain Thery committed
69
	computeImage();
70
71
	m_texture->setWrapping(GL_CLAMP_TO_EDGE);
	m_texture->update();
72

Sylvain Thery's avatar
Sylvain Thery committed
73
74
75
76
	computeImage();
	m_textureWP->setWrapping(GL_CLAMP_TO_EDGE);
	m_textureWP->update();

77
78
79
80
81
82
83
	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);

84
	m_shader2 = new Utils::ShaderPhongTexture();
85
86
	m_shader2->setAttributePosition(m_positionVBO);
	m_shader2->setAttributeTexCoord(m_texcoordVBO);
87
88
89
90
91
92
	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));
93
94
	registerShader(m_shader2);

Sylvain Thery's avatar
Sylvain Thery committed
95
96
97
98
99
100
	m_shaderWP = new Utils::ShaderWallPaper();
	m_shaderWP->setTextureUnit(GL_TEXTURE2);
	m_shaderWP->setTexture(m_textureWP);
	registerShader(m_shaderWP);


101
102
	glEnable(GL_TEXTURE_2D);

103
104
	if (!m_obj.hasNormals())
	{
105
		VertexAttribute<Geom::Vec3f, MAP> normal = myMap.getAttribute<VEC3, VERTEX, MAP>("normal") ;
106
		if(!normal.isValid())
107
			normal = myMap.addAttribute<VEC3, VERTEX, MAP>("normal") ;
108
109
110
111
112
113

		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);
114
115
116
117
118
}

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

Sylvain Thery's avatar
Sylvain Thery committed
120
121
	m_shaderWP->draw();

122
	if (m_phong)
123
	{
124
125
126
127
		m_shader2->activeTexture();
		m_shader2->enableVertexAttribs();
		glDrawArrays(GL_TRIANGLES, 0, m_nbIndices);
		m_shader2->disableVertexAttribs();
128
	}
129
130
131
132
133
134
135
	else
	{
		m_shader->activeTexture();
		m_shader->enableVertexAttribs();
		glDrawArrays(GL_TRIANGLES, 0, m_nbIndices);
		m_shader->disableVertexAttribs();
	}
136
137
138
139
140
141
}

void TexView::cb_keyPress(int code)
{
	switch(code)
	{
142
143
	case 'p':
		m_phong = !m_phong;
144
145
146
147
148
149
	case 'l':
		m_texture->setFiltering(GL_LINEAR);
		break;
	case 'n':
		m_texture->setFiltering(GL_NEAREST);
		break;
150
151
	case '1':
		m_shader2->setShininess(10.0f);
152
		break;
153
154
	case '2':
		m_shader2->setShininess(100.0f);
155
		break;
156
157
	case '3':
		m_shader2->setShininess(500.0f);
158
		break;
159
160
	case '4':
		m_shader2->setShininess(25000.0f);
161
		break;
162
163
164
	case '5':
		m_shader2->setShininess(1.0f);
		m_shader2->setSpecular(Geom::Vec4f(0));
165
166
167
168
169
		break;
	}
	updateGL();
}

Sylvain Thery's avatar
Sylvain Thery committed
170
void TexView::init(const std::string& fnm, const std::string& fnt)
171
{
Sylvain Thery's avatar
Sylvain Thery committed
172
	if (fnm.empty()	)
173
	{
174
		computeTore();
Sylvain Thery's avatar
Sylvain Thery committed
175
		m_fileNameTex  = fnt;
176
177
178
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
179
180
		m_fileNameMesh = fnm;
		m_fileNameTex  = fnt;
181
182
183
		std::vector<std::string> attrNames;

		m_obj.import(m_fileNameMesh,attrNames);
184
185
	}

186
187
188
	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);
189

190
191
	// send BB info to interface for centering on GL screen
	setParamObject(lWidthObj, lPosObj.data());
192
193
194
195
196
197
198
199
200
201
202
203
204
}

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
205
206
207
//	m_texture->create(Geom::Vec2ui(512,512));

	m_textureWP->create(Geom::Vec2ui(512,512));
208
209
210

#define WIDTHCHECKER 20

Sylvain Thery's avatar
Sylvain Thery committed
211
212
213
214
215
216
217
218
219
220
//	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];
//		}
//	}

	for (unsigned int ki = 0; ki < m_textureWP->size()[0]; ++ki)
221
	{
Sylvain Thery's avatar
Sylvain Thery committed
222
		for (unsigned int kj = 0; kj <  m_textureWP->size()[1]; ++kj)
223
		{
Sylvain Thery's avatar
Sylvain Thery committed
224
225
			unsigned int kc = ((kj/WIDTHCHECKER)*m_textureWP->size()[0] + (ki/WIDTHCHECKER))%7;
			(*m_textureWP)(ki,kj) =colorTable[kc];
226
227
228
229
230
		}
	}
#undef WIDTHCHECKER
}

231
void TexView::computeTore()
232
233
234
{
#define NB 96

235
236
	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
237
238
239
    //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);
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
	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);
257
			d = myMap.phi<121>(d);
258
		}
259
		d = myMap.phi<211>(d);
260
261
262
	}
#undef NB

263
264
265
	m_obj.setPositionAttribute(position);
	m_obj.setTexCoordAttribute(texcoord);
}
266

267
268
269
270
271
272
273
274
275
276
277
278
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
279
		tv.init("", argv[1]);
280
281
282
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
283
		tv.init("","x");
284
285
	}

286
287
288
289
290
	tv.show();

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