glutwin.h 5.2 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
68
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
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

#ifndef __GLUTWIN
#define __GLUTWIN

#include "Utils/os_spec.h"
#include "Utils/GLSLShader.h"

#define __X_GL_H
#define GLAPIENTRY
#include <GL/glut.h>

#include "Utils/trackball.h"
#include <IL/ilu.h>
#include <IL/ilut.h>
#include <string>

#include "Geometry/vector_gen.h"

namespace CGoGN
{

namespace Utils
{

APIEXPORT class SimpleGlutWin
{
protected:
	// internal values used for the manipulation of object
	static int spinning;
	static int moving;
	static int beginx;
	static int beginy;
	static float curquat[4];
	static float lastquat[4];
	static int newModel;
	static int scaling;
	static int translating;
	static float scalefactor;
	static float foc;

	static float trans_x;
	static float trans_y;
	static float trans_z;

	// width and height of windows
	static int W;
	static int H;

	static bool m_noMouse;

	// are shaders initialized ?
	bool shaderOk;
	// table of shaders
	Utils::GLSLShader shaders[8];

	// internal callbacks
	static void redraw(void);

	static void reshape(int w, int h);

	static void animate(void);

	static void motion(int x, int y);

	static void vis(int visible);

	static void mouse(int button, int state, int x, int y);

	static void keyboard(unsigned char keycode, int x, int y);

	static void recalcModelView();

	static SimpleGlutWin* instance;

protected:
	static GLfloat lightZeroPosition[4];

	static GLfloat lightZeroColor[4];

	virtual void myKeyboard(unsigned char, int, int) {}

	virtual void myRedraw() {}

	virtual void myMouseMotion(int x, int y) {}

	virtual void myMouseClick(int button, int state, int x, int y) {}

public:
	//! Others initialization: should be overloaded in derived classes
	virtual void init() {};

	/**
	* launch the interaction loop
	*/
	int mainLoop();

	/**
	* constructor
	*/
	SimpleGlutWin(int* argc, char **argv, int winX, int winY);

	/**
	* destructor
	*/
Sylvain Thery's avatar
Sylvain Thery committed
128
	virtual ~SimpleGlutWin() {}
Pierre Kraemer's avatar
Pierre Kraemer committed
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
163
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

	/**
	 * set the focale distance (for a screen width of 2), default value is 1
	 */
	static void setFoc(float df) {if (df>3.0f) df=3.0f; if (df<0.2f) df=0.2f;  foc = df;}

	/**
	 * get the focale distance
	 */
	static float getFoc() {return foc;}

	/**
	 * get the focale distance
	 */
	static float getScale() {return scalefactor/foc;}

	/**
	 * get an ray from A to B  deduce from screen coordinates x,y
	 * @param x x in screen coordinates (from glut)
	 * @param y y in screen coordinates (from glut)
	 * @param rayA begin point of ray (close to the screen)
	 * @param rayB end point of ray (far from the screen)
	 * @return the depth value from depth buffer
	 */
	GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB);

	/**
	 * get windows width
	 */
	int getWinWidth() { return W;}

	/**
	 * get windows heigth
	 */
	int getWinHeight() { return H;}

	/**
	 * capture the OpenGL viewport and save it into a png image
	 * in fact format of image is deduced from filename (thanks to DevIL)
	 */
	void capturePNG(const char* filename);

	/**
	* Print text in 2D on screen
	* 
	* @param x screen coordinate (in pixel)
	* @param y screen coordinate (in pixel)
	* @param str strin to print: \n is authorized (and handled)
	*/
	void printString2D(int x, int y, const std::string& str);

	/**
	* callback called just before exiting (ESC pressed)
	*/
	virtual void exitCallback() {}

	/**
	* say to interface that it does no more use mouse to move around the scene
	*/
	void setNoMouse(bool b) { m_noMouse = b; }
};

} // namespace Utils
} // namespace CGoGN

#endif