viewerOBJ.cpp 10.7 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

#include "viewerOBJ.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Modelisation/polyhedron.h"
#include "Utils/vbo.h" 

Sylvain Thery's avatar
Sylvain Thery committed
30

Sylvain Thery's avatar
Sylvain Thery committed
31
32
33
34
35
36
ObjView::ObjView():
	m_obj(myMap),
	m_positionVBO(NULL),
	m_normalVBO(NULL),
	m_texcoordVBO(NULL),
	m_shader(NULL),
Sylvain Thery's avatar
Sylvain Thery committed
37
	m_shader2(NULL),
38
39
	m_RenderStyle(2),
	m_currentGroupDrawn(0xffffffff)
Sylvain Thery's avatar
Sylvain Thery committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{}

ObjView::~ObjView()
{
	if (m_shader!=NULL)
		delete m_shader;
	if (m_shader2!=NULL)
		delete m_shader2;
	if (m_phongShader!=NULL)
		delete m_phongShader;

	if (m_positionVBO!=NULL)
		delete m_positionVBO;
	if (m_normalVBO!=NULL)
		delete m_normalVBO;
	if (m_texcoordVBO!=NULL)
		delete m_texcoordVBO;
}

Sylvain Thery's avatar
Sylvain Thery committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72

void ObjView::cb_keyPress(int k)
{
	switch(k)
	{
		case 'm':
			m_RenderStyle=0;
			break;
		case 'c':
			m_RenderStyle=1;
			break;
		case 't':
			m_RenderStyle=2;
			break;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

	case 'o':
		m_currentGroupDrawn=0xffffffff;
		break;
	case 'O':
		m_currentGroupDrawn=0;
		std::cout << "Group "<< m_obj.objGroupName(m_currentGroupDrawn)<< std::endl;
		break;
	case '-':
		m_currentGroupDrawn--;
		std::cout << "Group "<< m_obj.objGroupName(m_currentGroupDrawn)<< std::endl;
		break;
	case '+':
		m_currentGroupDrawn++;
		std::cout << "Group "<< m_obj.objGroupName(m_currentGroupDrawn)<< std::endl;
		break;

Sylvain Thery's avatar
Sylvain Thery committed
90
91
92
93
94
95
96
		default:
			break;
	}
	updateGL();
}


Sylvain Thery's avatar
Sylvain Thery committed
97
98
99
100
101
void ObjView::cb_initGL()
{
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);

102
103
	m_dr = new Utils::Drawer;

Sylvain Thery's avatar
Sylvain Thery committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	// create VBO for position
	m_positionVBO = new Utils::VBO;
	m_texcoordVBO = new Utils::VBO;
	m_normalVBO = new Utils::VBO;

	m_shader2 = new Utils::ShaderPhongTexture();
	m_shader2->setAttributePosition(m_positionVBO);
	m_shader2->setAttributeTexCoord(m_texcoordVBO);
	m_shader2->setAttributeNormal(m_normalVBO);
	m_shader2->setTextureUnit(GL_TEXTURE1);
	m_shader2->setShininess(10.0f);
	m_shader2->setAmbient(0.1f);
	m_shader2->setSpecular(Geom::Vec4f(0.5));
	registerShader(m_shader2);

	m_phongShader = new Utils::ShaderPhong() ;
	m_phongShader->setAttributePosition(m_positionVBO) ;
	m_phongShader->setAttributeNormal(m_normalVBO) ;
	registerShader(m_phongShader);

	glEnable(GL_TEXTURE_2D);
	m_obj.createGroupMatVBO_PTN(m_positionVBO,m_texcoordVBO,m_normalVBO);
}

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
void ObjView::drawBB( const Geom::BoundingBox<VEC3>& bb)
{
	const VEC3& mi = bb.min();
	const VEC3& ma = bb.max();

	m_dr->newList(GL_COMPILE_AND_EXECUTE);
	m_dr->lineWidth(3.0f);
	m_dr->begin(GL_LINE_LOOP);
	m_dr->color3f(1.0f,1.0f,0.0f);
	m_dr->vertex3f(mi[0],mi[1],mi[2]);
	m_dr->vertex3f(ma[0],mi[1],mi[2]);
	m_dr->vertex3f(ma[0],ma[1],mi[2]);
	m_dr->vertex3f(mi[0],ma[1],mi[2]);
	m_dr->end();
	m_dr->begin(GL_LINE_LOOP);
	m_dr->color3f(1.0f,1.0f,0.0f);
	m_dr->vertex3f(mi[0],mi[1],ma[2]);
	m_dr->vertex3f(ma[0],mi[1],ma[2]);
	m_dr->vertex3f(ma[0],ma[1],ma[2]);
	m_dr->vertex3f(mi[0],ma[1],ma[2]);
	m_dr->end();
	m_dr->begin(GL_LINES);
	m_dr->color3f(1.0f,1.0f,0.0f);
	m_dr->vertex3f(mi[0],mi[1],mi[2]);
	m_dr->vertex3f(mi[0],mi[1],ma[2]);
	m_dr->vertex3f(mi[0],ma[1],mi[2]);
	m_dr->vertex3f(mi[0],ma[1],ma[2]);
	m_dr->vertex3f(ma[0],ma[1],mi[2]);
	m_dr->vertex3f(ma[0],ma[1],ma[2]);
	m_dr->vertex3f(ma[0],mi[1],mi[2]);
	m_dr->vertex3f(ma[0],mi[1],ma[2]);
	m_dr->end();
	m_dr->endList();

}
Sylvain Thery's avatar
Sylvain Thery committed
163
164
165
166


void ObjView::cb_redraw()
{
167
168


Sylvain Thery's avatar
Sylvain Thery committed
169
170
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

171
172
	unsigned int nb = m_obj.nbObjGroups();
//	const std::vector<Algo::Surface::Import::MaterialOBJ*>& mats = m_obj.getMaterials();
Sylvain Thery's avatar
Sylvain Thery committed
173
174


175
176
177
178
179
180
181
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
	if (m_currentGroupDrawn<(nb-1))
	{
		std::cout <<  "Draw Object "<< m_currentGroupDrawn << std::endl;
		unsigned int i = m_currentGroupDrawn;
		drawBB(m_obj.getGroupBB(i));
		std::cout << "BB drawn" << std::endl;
		switch(m_RenderStyle)
		{
			case 0: // MONO
			for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
			{
					Geom::Vec4f v(0.2f,1.0f,0.4f,0.0f); // color here green
					m_phongShader->setAmbiant(0.2f*v) ;
					m_phongShader->setDiffuse(v) ;
					m_phongShader->setSpecular(v) ;
					m_phongShader->setShininess(10000.0) ;
					m_phongShader->enableVertexAttribs();
					glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
					m_phongShader->disableVertexAttribs();
				}
				break;
			case 1: // COLOR
			for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
			{
				const Algo::Surface::Import::MaterialOBJ* mat = m_obj.materialOf(i,j);
					Geom::Vec4f v(mat->diffuseColor[0],mat->diffuseColor[1],mat->diffuseColor[2],0.0f);
					m_phongShader->setAmbiant(0.2f*v) ;
					m_phongShader->setDiffuse(v) ;
					m_phongShader->setSpecular(v) ;
					m_phongShader->setShininess(10000.0) ;
					m_phongShader->enableVertexAttribs();
					glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
					m_phongShader->disableVertexAttribs();

				}
			default:
			for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
			{
				const Algo::Surface::Import::MaterialOBJ* mat = m_obj.materialOf(i,j);
					if (mat->textureDiffuse != NULL)
					{
						m_shader2->setTexture(mat->textureDiffuse);
						m_shader2->setShininess(mat->shininess);
						m_shader2->setAmbient(0.8f);
						m_shader2->activeTexture();
						m_shader2->enableVertexAttribs();
						glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
						m_shader2->disableVertexAttribs();
					}
					else
					{
						Geom::Vec4f v;
						v[0] = 0.5f; v[1] = 0.5f; v[2] = 0.5f; v[3] = 0.0f;
						m_phongShader->setAmbiant(v) ;
						v[0] = mat->diffuseColor[0]; v[1] = mat->diffuseColor[1]; v[2] = mat->diffuseColor[2]; v[3] = 0.0f;
						m_phongShader->setDiffuse(v) ;
						v[0] = mat->specularColor[0]; v[1] = mat->specularColor[1]; v[2] = mat->specularColor[2]; v[3] = 0.0f;
						m_phongShader->setSpecular(v) ;
						m_phongShader->setShininess(mat->shininess) ;
						m_phongShader->enableVertexAttribs();
						glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
						m_phongShader->disableVertexAttribs();
					}
				}
		}
	}
	else
Sylvain Thery's avatar
Sylvain Thery committed
242
243
	for (unsigned int i=0; i<nb; ++i)
	{
244
245
246

		drawBB(m_obj.getGroupBB(i));

Sylvain Thery's avatar
Sylvain Thery committed
247
		switch(m_RenderStyle)
Sylvain Thery's avatar
Sylvain Thery committed
248
		{
Sylvain Thery's avatar
Sylvain Thery committed
249
			case 0: // MONO
250
251
			for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
			{
Sylvain Thery's avatar
Sylvain Thery committed
252
253
254
255
256
257
258
					Geom::Vec4f v(0.2f,1.0f,0.4f,0.0f); // color here green
					m_phongShader->setAmbiant(0.2f*v) ;
					m_phongShader->setDiffuse(v) ;
		//			v[0] = 1.0f; v[1] = 1.0f; v[2] = 1.0f; v[3] = 0.0f; // use this for specular effected
					m_phongShader->setSpecular(v) ;
					m_phongShader->setShininess(10000.0) ;
					m_phongShader->enableVertexAttribs();
259
					glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
Sylvain Thery's avatar
Sylvain Thery committed
260
261
262
263
					m_phongShader->disableVertexAttribs();
				}
				break;
			case 1: // COLOR
264
265
266
267
268
			for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
			{
				const Algo::Surface::Import::MaterialOBJ* mat = m_obj.materialOf(i,j);

					Geom::Vec4f v(mat->diffuseColor[0],mat->diffuseColor[1],mat->diffuseColor[2],0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
269
270
271
272
273
274
					m_phongShader->setAmbiant(0.2f*v) ;
					m_phongShader->setDiffuse(v) ;
		//			v[0] = 1.0f; v[1] = 1.0f; v[2] = 1.0f; v[3] = 0.0f; // use this for specular effected
					m_phongShader->setSpecular(v) ;
					m_phongShader->setShininess(10000.0) ;
					m_phongShader->enableVertexAttribs();
275
					glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
Sylvain Thery's avatar
Sylvain Thery committed
276
277
278
279
					m_phongShader->disableVertexAttribs();

				}
			default:
280
281
282
283
284
			for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
			{
				const Algo::Surface::Import::MaterialOBJ* mat = m_obj.materialOf(i,j);

					if (mat->textureDiffuse != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
285
					{
286
287
						m_shader2->setTexture(mat->textureDiffuse);
						m_shader2->setShininess(mat->shininess);
Sylvain Thery's avatar
Sylvain Thery committed
288
289
290
						m_shader2->setAmbient(0.8f);
						m_shader2->activeTexture();
						m_shader2->enableVertexAttribs();
291
						glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
Sylvain Thery's avatar
Sylvain Thery committed
292
293
294
295
296
297
298
						m_shader2->disableVertexAttribs();
					}
					else
					{
						Geom::Vec4f v;
						v[0] = 0.5f; v[1] = 0.5f; v[2] = 0.5f; v[3] = 0.0f;
						m_phongShader->setAmbiant(v) ;
299
						v[0] = mat->diffuseColor[0]; v[1] = mat->diffuseColor[1]; v[2] = mat->diffuseColor[2]; v[3] = 0.0f;
Sylvain Thery's avatar
Sylvain Thery committed
300
						m_phongShader->setDiffuse(v) ;
301
						v[0] = mat->specularColor[0]; v[1] = mat->specularColor[1]; v[2] = mat->specularColor[2]; v[3] = 0.0f;
Sylvain Thery's avatar
Sylvain Thery committed
302
						m_phongShader->setSpecular(v) ;
303
						m_phongShader->setShininess(mat->shininess) ;
Sylvain Thery's avatar
Sylvain Thery committed
304
						m_phongShader->enableVertexAttribs();
305
						glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
Sylvain Thery's avatar
Sylvain Thery committed
306
307
308
						m_phongShader->disableVertexAttribs();
					}
				}
Sylvain Thery's avatar
Sylvain Thery committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
		}
	}
}


void ObjView::init(const std::string& fnm)
{

	std::vector<std::string> attrNames;
	m_obj.import(fnm,attrNames);


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

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

}



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

	if (argc == 2)
	{
		tv.init(argv[1]);
	}
	else
	{
		CGoGNerr << argv[0] << " objFile"<< CGoGNendl;
	}

	tv.show();


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