Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
386 jab 1
/*
2
 *  MeshEdit is a small application which allows you to load and edit a mesh.
3
 *  The mesh will be stored in GEL's half edge based Manifold data structure.
4
 *  A number of editing operations are supported. Most of these are accessible from the 
5
 *  console that pops up when you hit 'esc'.
6
 *
7
 *  Created by J. Andreas Bærentzen on 15/08/08.
8
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
9
 *
10
 */
11
#include <iostream>
12
#include <CGLA/eigensolution.h>
13
#include <CGLA/Vec2d.h>
14
#include <CGLA/Vec3d.h>
15
#include <CGLA/Mat3x3d.h>
16
#include <CGLA/Mat2x2d.h>
17
#include <CGLA/Mat2x3d.h>
18
 
19
#include <LinAlg/Matrix.h>
20
#include <LinAlg/Vector.h>
21
#include <LinAlg/LapackFunc.h>
22
 
23
#include <Util/Timer.h>
24
#include <Util/ArgExtracter.h>
25
 
26
#include <GL/glew.h>
27
#include <GLGraphics/gel_glut.h>
28
#include <GLGraphics/draw.h>
29
#include <GLGraphics/glsl_shader.h>
30
#include <GLGraphics/GLViewController.h>
31
 
32
#include <HMesh/Manifold.h>
33
#include <HMesh/VertexCirculator.h>
34
#include <HMesh/FaceCirculator.h>
35
#include <HMesh/build_manifold.h>
36
#include <HMesh/mesh_optimization.h>
37
#include <HMesh/triangulate.h>
38
#include <HMesh/load.h>
39
#include <HMesh/x3d_save.h>
40
 
41
#include <GLConsole/GLConsole.h>
42
 
43
#include "harmonics.h"
44
#include "wireframe.h"
45
 
46
 
47
// Single global instance so glut can get access
48
GLConsole theConsole;
49
 
50
////////////////////////////////////////////////////////////////////////////////
51
char* ConsoleHelp(std::vector<std::string> &args)
52
{
53
    theConsole.Printf("");
54
    theConsole.Printf("----------------- HELP -----------------");
55
    theConsole.Printf("Press ~ key to open and close console");
56
    theConsole.Printf("Press TAB to see the available commands and functions");
57
    theConsole.Printf("Functions are shown in green and variables in yellow");
58
    theConsole.Printf("Setting a value: [command] = value");
59
    theConsole.Printf("Getting a value: [command]");
60
    theConsole.Printf("Functions: [function] [arg1] [arg2] ...");
61
    theConsole.Printf("Entering just the function name will give a description.");
62
    theConsole.Printf("History: Up and Down arrow keys move through history.");
63
    theConsole.Printf("Tab Completion: TAB does tab completion and makes suggestions.");
64
    theConsole.Printf("----------------- HELP -----------------");
65
    theConsole.Printf("");
66
    return "";
67
}
68
 
69
using namespace std;
70
using namespace HMesh;
71
using namespace Geometry;
72
using namespace GLGraphics;
73
using namespace CGLA;
74
using namespace Util;
75
using namespace LinAlg;
76
 
77
 
78
 
79
namespace 
80
{
81
	GLViewController* view_ctrl;
82
	int WINX=800, WINY=800;
83
 
84
	Manifold mani;	
85
	bool create_display_list = true;
86
}
87
 
88
 
89
 
90
char* console_partial_reconstruct(std::vector<std::string> &args)
91
{
92
	int E0,E1;
93
	float scale;
94
	istringstream a0(args[0]);
95
	a0 >> E0;
96
	istringstream a1(args[1]);
97
	a1 >> E1;
98
	istringstream a2(args[2]);
99
	a2 >> scale;
100
	partial_reconstruct(mani, E0,E1,scale);
101
	return "";
102
}
103
 
104
char* console_reset_shape(std::vector<std::string> &args)
105
{
106
	reset_shape(mani);
107
	return "";
108
}
109
 
110
void reshape(int W, int H)
111
{
112
	view_ctrl->reshape(W,H);
113
}
114
 
115
 
116
 
117
void display() 
118
{
119
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
120
 
121
	static CVar<int> do_wireframe("display.wireframe",0);
122
 
123
	glPushMatrix();
124
 
125
	view_ctrl->set_gl_modelview();
126
 
127
	if(create_display_list)
128
	{
129
		create_display_list = false;
130
 
131
		glNewList(1,GL_COMPILE);
132
		if(do_wireframe)
133
			{
134
				enable_wireframe();
135
				draw(mani);
136
			}
137
		else
138
			draw_eigenvalues(mani);
139
		glEndList();
140
	}
141
	glCallList(1);
142
 
143
	glPopMatrix();
144
 
145
	glUseProgram(0);
146
	theConsole.RenderConsole();
147
 
148
	glutSwapBuffers();
149
}
150
 
151
void animate() 
152
{	
153
	usleep( (int)1e4 );
154
	view_ctrl->try_spin();
155
	glutPostRedisplay();
156
}
157
 
158
 
159
void mouse(int button, int state, int x, int y) 
160
{
161
	Vec2i pos(x,y);
162
	if (state==GLUT_DOWN) 
163
	{
164
		if (button==GLUT_LEFT_BUTTON) 
165
			view_ctrl->grab_ball(ROTATE_ACTION,pos);
166
		else if (button==GLUT_MIDDLE_BUTTON) 
167
			view_ctrl->grab_ball(ZOOM_ACTION,pos);
168
		else if (button==GLUT_RIGHT_BUTTON) 
169
			view_ctrl->grab_ball(PAN_ACTION,pos);
170
	}
171
	else if (state==GLUT_UP)
172
		view_ctrl->release_ball();
173
}
174
 
175
void motion(int x, int y) {
176
	Vec2i pos(x,y);
177
	view_ctrl->roll_ball(Vec2i(x,y));
178
}
179
 
180
void keyboard_spec(int key, int x, int y)
181
{
182
   int mod = glutGetModifiers();
183
 
184
    if( theConsole.isOpen() ) {
185
        // If shift held, scroll the console
186
        if( mod == GLUT_ACTIVE_SHIFT ) {
187
            switch (key){
188
                case GLUT_KEY_UP:
189
                    theConsole.ScrollDownLine();
190
                    break;
191
                case GLUT_KEY_DOWN: 
192
                    theConsole.ScrollUpLine();
193
                    break;
194
            }
195
        } else {
196
            theConsole.StandardKeyBindings( key );
197
        }
198
    }
199
}
200
 
201
template<typename T>
202
T& get_CVar_ref(const std::string& s)
203
{
204
	return *reinterpret_cast<T*> (GetCVarData(s));
205
}
206
 
207
void keyboard(unsigned char key, int x, int y) 
208
{	
209
	if(theConsole.isOpen())
210
		switch(key) {
211
			case '\033': 
212
				theConsole.ToggleConsole();
213
			default:      
214
				//send keystroke to console
215
				if( theConsole.isOpen() ){
216
					theConsole.EnterCommandCharacter(key);
217
				}
218
				break;
219
		}	
220
	else {
221
		int& display_eigenvalue = get_CVar_ref<int>("display.eigenvalue");
222
		int& display_wireframe = get_CVar_ref<int>("display.wireframe");
223
		int& display_diffuse = get_CVar_ref<int>("display.diffuse");
224
		int& display_highlight = get_CVar_ref<int>("display.highlight");
225
 
226
		switch(key) {
227
			case 'q': exit(0);
228
			case '\033':
229
				theConsole.ToggleConsole();
230
				break;
231
			case '+': 
232
				display_eigenvalue = min(display_eigenvalue+1, MAX_E); 
233
				break;
234
			case '-': 
235
				display_eigenvalue = max(display_eigenvalue-1, 0); 
236
				break;
237
			case '1': E = 1; reconstruct(mani,E); 
238
				break;
239
			case '>': if(E < MAX_E) partial_reconstruct(mani,E,++E); 
240
				break;
241
			case '<': E = max(E-1, 0); reconstruct(mani,E); 
242
				break;
243
			case 'd':	
244
				display_diffuse = !display_diffuse; 
245
				break;
246
			case 'h':
247
				display_highlight = !display_highlight;
248
				break;			
249
			case 'w':
250
				display_wireframe = !display_wireframe;
251
				break;
252
		}
253
	}
254
	create_display_list = true;
255
}
256
 
257
void init_glut(int argc, char** argv)
258
{
259
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
260
	glutInitWindowSize(WINX, WINY);
261
	glutInit(&argc, argv);
262
	glutCreateWindow("Shape Harmonics");
263
	glutDisplayFunc(display);
264
	glutKeyboardFunc(keyboard);
265
	glutSpecialFunc(keyboard_spec);
266
	glutReshapeFunc(reshape);
267
	glutMouseFunc(mouse);
268
	glutMotionFunc(motion);
269
	glutIdleFunc(animate);
270
}
271
 
272
void init_gl()
273
{
274
	glewInit();
275
	glEnable(GL_LIGHTING);
276
	glEnable(GL_LIGHT0);
277
	Vec3f c(0,0,0);
278
	float r = 5;
279
	mani.get_bsphere(c,r);
280
	view_ctrl = new GLViewController(WINX,WINY, c,r*2);
281
 
282
	initialize_wireframe_shaders();
283
 
284
 
285
	// Set the value of a uniform
286
	//glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
287
 
288
	glMatrixMode(GL_MODELVIEW);
289
	glLoadIdentity();
290
	glClearColor(0.50f, 0.50f, 0.50f, 0.f);
291
	glColor4f(1.0f, 1.0f, 1.0f, 0.f);
292
	glEnable(GL_DEPTH_TEST);
293
 
294
	static CVar<ConsoleFunc> help( "help", ConsoleHelp );
295
	static CVar<ConsoleFunc> rs("reset_shape", console_reset_shape);
296
	static CVar<ConsoleFunc> pr("partial_reconstruct", console_partial_reconstruct);
297
 
298
}
299
 
300
 
301
int main(int argc, char** argv)
302
{
303
	ArgExtracter ae(argc, argv);
304
	ae.extract("-E", E);
305
    if(argc>1)
306
	{		
307
		string file = ae.get_last_arg();
308
		load(file, mani);
309
	}
310
 
311
 
312
	init_glut(argc,argv);
313
	init_gl();
314
	init_harmonics(mani);
315
 
316
	glutMainLoop();
317
}
318
 
319