glutwin_atb.cpp 9.19 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 63 64 65 66 67
/*******************************************************************************
* 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 "Utils/glutwin_atb.h"

#include <stdlib.h>
#include <math.h>
#include <iostream>

namespace CGoGN
{

namespace Utils
{

GlutWin_ATB* GlutWin_ATB::instance = NULL ;

int GlutWin_ATB::rotating = 0 ;
int GlutWin_ATB::scaling = 0 ;
int GlutWin_ATB::translating = 0 ;
int GlutWin_ATB::beginx = 0 ;
int GlutWin_ATB::beginy = 0 ;
float GlutWin_ATB::curquat[4] = { 0.0f, 0.0f, 0.0f, 0.0f } ;
float GlutWin_ATB::lastquat[4] = { 0.0f, 0.0f, 0.0f, 0.0f } ;
int GlutWin_ATB::newModel = 1 ;
float GlutWin_ATB::scalefactor = 1.0f ;
float GlutWin_ATB::focal = 2.5f ;
float GlutWin_ATB::trans_x = 0.0f ;
float GlutWin_ATB::trans_y = 0.0f ;
float GlutWin_ATB::trans_z = 0.0f ;

int GlutWin_ATB::W = 0 ;
int GlutWin_ATB::H = 0 ;

float GlutWin_ATB::lightZeroPosition[4]= { 0.0f, 0.0f, 10000.0f, 1.0f } ;
float GlutWin_ATB::lightZeroColor[4]= { 0.9f, 0.9f, 0.9f, 1.0f } ;


GlutWin_ATB::GlutWin_ATB(int* argc, char **argv, int winX, int winY)
{
	instance = this ;

	newModel = 1 ;
	scalefactor = 1.0f ;
	trans_x = 0.0f ;
	trans_y = 0.0f ;
68
	trans_z = -200.0f ;
Pierre Kraemer's avatar
Pierre Kraemer committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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

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

	// AntTweakBar
	TwInit(TW_OPENGL, NULL) ;
	TwWindowSize(W, H) ;
	glutSpecialFunc((GLUTspecialfun)TwEventSpecialGLUT) ;
	TwGLUTModifiersFunc(glutGetModifiers) ;

	glutDisplayFunc(redraw) ;
	glutReshapeFunc(reshape) ;
	glutIdleFunc(NULL) ;
	glutMouseFunc(mouse) ;
	glutPassiveMotionFunc(passive_mouse) ;
	glutMotionFunc(motion) ;
	glutKeyboardFunc(keyboard) ;

	std::cout << "Initialisation OpenGL" << std::endl ;
	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, focal, 500.0f) ;
	glMatrixMode(GL_MODELVIEW) ;

	glClearColor(0.1f, 0.1f, 0.3f, 0.0f) ;
	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) ;
}

GlutWin_ATB::~GlutWin_ATB()
{
	// AntTweakBar
	TwTerminate() ;
}

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

	instance->myRedraw() ;
	// AntTweakBar
	TwDraw() ;

	glutSwapBuffers() ;
}

void GlutWin_ATB::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, focal, 500.0f) ;
	glMatrixMode(GL_MODELVIEW) ;
	
	recalcModelView() ;
	
	// AntTweakBar
	TwWindowSize(W, H) ;
}

void GlutWin_ATB::recalcModelView()
{
	GLfloat m[4][4] ;

	glPopMatrix() ;
	glPushMatrix() ;

158
	glTranslatef(trans_x, trans_y, trans_z) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

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

	newModel = 0 ;
}

void GlutWin_ATB::motion(int x, int y)
{
	// AntTweakBar
	if(TwEventMouseMotionGLUT(x, y))
	{
		glutPostRedisplay() ;
		return ;
	}

	if(rotating)
	{
		trackball(lastquat, (2.0f * beginx - W) / W, (H - 2.0f * beginy) / H,
							(2.0f * x - W) / W, (H - 2.0f * y) / H ) ;
		add_quats(lastquat, curquat, curquat) ;
		newModel = 1 ;
		glutPostRedisplay() ;
	}
	else if(scaling)
	{
185 186
		trans_z -= 0.1f*(x - beginx);
		trans_z -= 0.1f*(y - beginy);
Pierre Kraemer's avatar
Pierre Kraemer committed
187 188 189 190 191
		newModel = 1 ;
		glutPostRedisplay() ;
	}
	else if(translating)
	{
192 193
		trans_x += 0.05f * (x - beginx) ;
		trans_y += 0.05f * (beginy - y) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 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
		newModel = 1 ;
		glutPostRedisplay() ;
	}

	instance->myMotion(x, y) ;

	beginx = x ;
	beginy = y ;
}

void GlutWin_ATB::mouse(int button, int state, int x, int y)
{
	// AntTweakBar
	if(TwEventMouseButtonGLUT(button, state, x, y))
	{
		glutPostRedisplay() ;
		return ;
	}

	beginx = x ;
	beginy = y ;

	if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		if(! ((glutGetModifiers() & GLUT_ACTIVE_SHIFT) || (glutGetModifiers() & GLUT_ACTIVE_CTRL)) )
			rotating = 1 ;
	}
	else if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)
		rotating = 0 ;
	else if(button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
	{
		if(! ((glutGetModifiers() & GLUT_ACTIVE_SHIFT) || (glutGetModifiers() & GLUT_ACTIVE_CTRL)) )
			scaling = 1 ;
	}
	else if(button == GLUT_MIDDLE_BUTTON && state == GLUT_UP)
		scaling = 0 ;
	else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
	{
		if(! ((glutGetModifiers() & GLUT_ACTIVE_SHIFT) || (glutGetModifiers() & GLUT_ACTIVE_CTRL)) )
			translating = 1 ;
	}
	else if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
		translating = 0 ;

	// zoom a la mollette
	if (button == 4 && state == GLUT_UP)
	{
		scalefactor = scalefactor * 1.1f;
		newModel = 1 ;
		glutPostRedisplay() ;
	}
	if (button == 3 && state == GLUT_UP)
	{
		scalefactor = scalefactor * 0.9f;
		newModel = 1 ;
		glutPostRedisplay() ;
	}



	instance->myMouse(button, state, x, y) ;
}

void GlutWin_ATB::passive_mouse(int x, int y)
{
	// AntTweakBar
	if(TwEventMouseMotionGLUT(x, y))
	{
		glutPostRedisplay() ;
		return ;
	}
}

void GlutWin_ATB::keyboard(unsigned char keycode, int x, int y)
{
	// AntTweakBar
	if(TwEventKeyboardGLUT(keycode, x, y))
	{
		glutPostRedisplay() ;
		return ;
	}

	if(keycode == 27)
	{
		instance->exitCallback();
		exit(0) ;
	}
	instance->myKeyboard(keycode, x, y) ;
}

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

GLfloat GlutWin_ATB::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 GlutWin_ATB::capturePNG(const char* filename)
{
	ILuint imgName ;
	ilGenImages(1, &imgName) ;
	ilBindImage(imgName) ;
	ilutGLScreen() ;

	// save image
	ilEnable(IL_FILE_OVERWRITE) ;
	ilSaveImage((ILstring)filename) ;
	ilDeleteImages(1, &imgName) ;
}

void GlutWin_ATB::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) ;

347 348 349 350
	unsigned char blank[180];
	for (unsigned int i=0; i< 180; ++i)
		blank[i]=(unsigned char)(0);

Pierre Kraemer's avatar
Pierre Kraemer committed
351 352 353 354 355 356 357 358 359 360
	// AFFICHAGE EN 2D
	int x1 = x ;
	for(unsigned int i = 0; i < str.length(); ++i)
	{
		char c = str[i] ;
		if(c == '\n')
		{
			x1 = x ;
			y += 14 ;
		}
361 362 363
		glRasterPos2i(x1,y+2);
		glDrawPixels(12,15,GL_LUMINANCE,GL_UNSIGNED_BYTE, blank);

Pierre Kraemer's avatar
Pierre Kraemer committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
		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