Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

glutwin.cpp 9.03 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer 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
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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <stdlib.h>

#include "Utils/glutwin.h"
#include <math.h>
#include <IL/ilu.h>
#include <IL/ilut.h>

#include <iostream>

namespace CGoGN
{

namespace Utils
{

SimpleGlutWin* SimpleGlutWin::instance=NULL;
float SimpleGlutWin::lightZeroPosition[4]= {0.0f,0.0f,10000.0f,1.0f};
float SimpleGlutWin::lightZeroColor[4]= {0.9f,0.9f,0.9f,1.0f};
float SimpleGlutWin::scalefactor = 1.0f;
float SimpleGlutWin::trans_x=0.;
float SimpleGlutWin::trans_y=0.;
float SimpleGlutWin::trans_z=0.;

float SimpleGlutWin::foc=2.0f;

float SimpleGlutWin::curquat[4]={0.0f,0.0f,0.0f,0.0f};
float SimpleGlutWin::lastquat[4]={0.0f,0.0f,0.0f,0.0f};
int SimpleGlutWin::spinning = 0;
int SimpleGlutWin::moving = 0;
int SimpleGlutWin::newModel = 1;
int SimpleGlutWin::translating=0;
int SimpleGlutWin::scaling=0;
int SimpleGlutWin::beginx=0;
int SimpleGlutWin::beginy=0;
int SimpleGlutWin::W=0;
int SimpleGlutWin::H=0;
bool SimpleGlutWin::m_noMouse=false;

Sylvain Thery's avatar
Sylvain Thery committed
63
SimpleGlutWin::SimpleGlutWin(int* argc, char **argv, int winX, int winY)
Pierre Kraemer's avatar
Pierre Kraemer committed
64
65
66
67
68
69
70
71
{
	instance = this;
	spinning = 0;
	moving = 0;
	newModel = 1;
	scalefactor = 1.0f;
	trans_x=0.;
	trans_y=0.;
72
	trans_z=-200.0f;
Pierre Kraemer's avatar
Pierre Kraemer committed
73

74

Pierre Kraemer's avatar
Pierre Kraemer committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	std::cout << "Initialisation Glut" << std::endl;
	glutInit(argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutCreateWindow(argv[0]);
	glutReshapeWindow(winX,winY);
  	W=winX; H=winY;
	trackball(curquat, 0.0f, 0.0f, 0.0f, 0.0f);
	glutDisplayFunc(redraw);
	glutReshapeFunc(reshape);
	glutVisibilityFunc(vis);
	glutMouseFunc(mouse);
	glutMotionFunc(motion);
	glutKeyboardFunc(keyboard);

	std::cout << "Initialisation OpenGL" << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
90
	std::cout << glGetString(GL_VERSION)<<std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	shaderOk = Utils::GLSLShader::init();

	glViewport(0, 0, W, H);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0f,1.0f,-1.0f*H/W,1.0f*H/W,foc,500.0f);
	glMatrixMode(GL_MODELVIEW);

	glClearColor(0.1,0.1,0.3,0.0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);

	glEnable(GL_LIGHTING);
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
	glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
	glEnable(GL_LIGHT0);

Sylvain Thery's avatar
Sylvain Thery committed
110
	//Store context infos
sylvain thery's avatar
sylvain thery committed
111
112
113
114
#ifdef WIN32
	m_drawable = wglGetCurrentDC();
	m_context = wglGetCurrentContext();
#else
Sylvain Thery's avatar
Sylvain Thery committed
115
116
117
118
119
120
121
	#ifdef MAC_OSX
		m_context = CGLGetCurrentContext();
	#else
		m_dpy = glXGetCurrentDisplay();
		m_drawable = glXGetCurrentDrawable();
		m_context = glXGetCurrentContext();
	#endif
sylvain thery's avatar
sylvain thery committed
122
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
123
124
125
126
	// Call other initialization (possibly overloaded in instances)
	instance->init();
}

Sylvain Thery's avatar
Sylvain Thery committed
127
128
void SimpleGlutWin::releaseContext()
{
sylvain thery's avatar
sylvain thery committed
129
130
131
#ifdef WIN32
	wglMakeCurrent(NULL,NULL);
#else
Sylvain Thery's avatar
Sylvain Thery committed
132
133
134
135
136
	#ifdef MAC_OSX
		CGLSetCurrentContext(NULL);
	#else
		glXMakeCurrent(m_dpy,None,NULL);
	#endif
sylvain thery's avatar
sylvain thery committed
137
#endif
Sylvain Thery's avatar
Sylvain Thery committed
138
139
140
141
}

void SimpleGlutWin::useContext()
{
sylvain thery's avatar
sylvain thery committed
142
143
144
#ifdef WIN32
	wglMakeCurrent(m_drawable, m_context);
#else
Sylvain Thery's avatar
Sylvain Thery committed
145
146
147
148
149
	#ifdef MAC_OSX
		CGLSetCurrentContext(m_context);
	#else
		glXMakeCurrent(m_dpy, m_drawable, m_context);
	#endif
sylvain thery's avatar
sylvain thery committed
150
151
#endif

Sylvain Thery's avatar
Sylvain Thery committed
152
153
154
155
}



Pierre Kraemer's avatar
Pierre Kraemer committed
156
157
158
159
160
161
162
void SimpleGlutWin::recalcModelView(void)
{
	GLfloat m[4][4];

	glPopMatrix();
	glPushMatrix();

163
	glTranslatef(trans_x,trans_y,trans_z);
Pierre Kraemer's avatar
Pierre Kraemer committed
164
165
166
167
168
169
170
171
172
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

	build_rotmatrix(m, curquat);
	glMultMatrixf(&m[0][0]);

	newModel = 0;
}

void SimpleGlutWin::redraw(void)
{
	if (newModel) 
	{
	    recalcModelView();
	}
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	instance->myRedraw();

	glutSwapBuffers();
}

void SimpleGlutWin::reshape(int w, int h)
{
	if (w>0) W = w;
	if (h>0) H = h;
	
	glViewport(0,0,W,H);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0f,1.0f,-1.0f*H/W,1.0f*H/W,foc,500.0f);
	glMatrixMode(GL_MODELVIEW);
	
	recalcModelView();
}

void SimpleGlutWin::animate(void)
{
	add_quats(lastquat, curquat, curquat);
	newModel = 1;
	glutPostRedisplay();
}

void SimpleGlutWin::motion(int x, int y)
{
	if (!m_noMouse)
	{
		if (scaling || translating) 
		{
			if (scaling) 
			{
215
216
				trans_z -= 0.1f*(x - beginx);
				trans_z -= 0.1f*(y - beginy);
Pierre Kraemer's avatar
Pierre Kraemer committed
217
218
219
			}
			else if (translating) 
			{
220
221
				trans_x += 0.05f*(x - beginx);
				trans_y += 0.05f*(beginy - y);
Pierre Kraemer's avatar
Pierre Kraemer committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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
353
354
355
			}
			beginx = x;
			beginy = y;
			newModel = 1;
			glutPostRedisplay();
			return;
		}
		
		if (moving) 
		{
			trackball(lastquat, (2.0f * beginx - W) / W,(H - 2.0f * beginy) / H,
								(2.0f * x - W) / W,(H - 2.0f * y) / H );
			beginx = x;
			beginy = y;
			spinning = 1;
			//    glutIdleFunc(animate);
			animate();
		}
	}

	instance->myMouseMotion(x, y);
}

void SimpleGlutWin::vis(int visible)
{
	if (spinning) 
	{
		glutIdleFunc(NULL);
	}
//	if (visible == GLUT_VISIBLE)
//	{
//		if (spinning) glutIdleFunc(animate);
//	}
//	else
//	{
//		if (spinning) glutIdleFunc(NULL);
//	}
}

void SimpleGlutWin::mouse(int button, int state, int x, int y)
{
	if (!m_noMouse)
	{
		if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
		{
			spinning = 0;
			glutIdleFunc(NULL);
			moving = 1;
			beginx = x;
			beginy = y;
			scaling = (glutGetModifiers() & GLUT_ACTIVE_SHIFT) != 0;
			translating = (glutGetModifiers() & GLUT_ACTIVE_CTRL) != 0;
		}
		else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) 
		{
			moving = 0;
		}
	}
	instance->myMouseClick(button, state, x, y);
}

void SimpleGlutWin::keyboard(unsigned char keycode, int x, int y)
{
	if (keycode == 27)
	{
		instance->exitCallback();
		exit(0);
	}
	instance->myKeyboard(keycode, x, y);
}

int SimpleGlutWin::mainLoop()
{
	glutMainLoop();
	return 0;
}

void SimpleGlutWin::capturePNG(const char* filename)
{
	ILuint imgName;
	ilGenImages(1,&imgName);
	ilBindImage(imgName);
	ilutGLScreen();
	
	// save image
	ilEnable(IL_FILE_OVERWRITE);
	ilSaveImage((ILstring) filename);
	ilDeleteImages(1,&imgName);
}

GLfloat SimpleGlutWin::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB)
{
	GLdouble model[16];
	GLdouble proj[16];
	GLint view[4];
	
	// get Z from depth buffer
	GLfloat prof;
	glReadPixels(x,H-y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&prof);
	
	// get matrices
	glGetDoublev(GL_MODELVIEW_MATRIX,model);
	glGetDoublev(GL_PROJECTION_MATRIX,proj);
	glGetIntegerv(GL_VIEWPORT,view);
	
	// unproject x,y,0
	GLdouble xx, yy, zz;
	gluUnProject(x, H-y, 0.0f, model, proj, view, &xx, &yy, &zz);
	rayA[0] = float(xx);
	rayA[1] = float(yy);
	rayA[2] = float(zz);
	
	// unprojet x,y,z
	gluUnProject(x, H-y, prof, model, proj, view, &xx, &yy, &zz);
	rayB[0] = float(xx);
	rayB[1] = float(yy);
	rayB[2] = float(zz);
	
	return prof;
}

void SimpleGlutWin::printString2D(int x, int y, const std::string& str) 
{
	// PASSAGE EN 2D
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0, W, H,0, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);
	
356
357
358
359
360
	// not very optimized but not very important here
	unsigned char blank[180];
	for (unsigned int i=0; i< 180; ++i)
		blank[i]=(unsigned char)(0);

Pierre Kraemer's avatar
Pierre Kraemer committed
361
362
363
364
365
366
367
368
369
370
	// AFFICHAGE EN 2D
	int x1 = x;
	for (unsigned i=0; i<str.length(); ++i) 
	{
		char c = str[i];
		if (c=='\n') 
		{
			x1 = x;
			y += 14;
		}
371
372
373
		glRasterPos2i(x1,y+2);
		glDrawPixels(12,15,GL_LUMINANCE,GL_UNSIGNED_BYTE, blank);

Pierre Kraemer's avatar
Pierre Kraemer committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
		glRasterPos2i(x1,y);
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, c);
		x1 += glutBitmapWidth(GLUT_BITMAP_HELVETICA_12,c);
	}
	
	// RETOUR EN 3D
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
}

} // namespace Utils
} // namespace CGoGN