Subversion Repositories gelsvn

Rev

Rev 397 | Rev 401 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 397 Rev 399
Line 6... Line 6...
6
 *
6
 *
7
 *  Created by J. Andreas Bærentzen on 15/08/08.
7
 *  Created by J. Andreas Bærentzen on 15/08/08.
8
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
8
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
9
 *
9
 *
10
 */
10
 */
-
 
11
 
-
 
12
#include <string>
11
#include <iostream>
13
#include <iostream>
12
#include <CGLA/eigensolution.h>
14
#include <CGLA/eigensolution.h>
13
#include <CGLA/Vec2d.h>
15
#include <CGLA/Vec2d.h>
14
#include <CGLA/Vec3d.h>
16
#include <CGLA/Vec3d.h>
15
#include <CGLA/Mat3x3d.h>
17
#include <CGLA/Mat3x3d.h>
Line 23... Line 25...
23
#include <Util/Timer.h>
25
#include <Util/Timer.h>
24
#include <Util/ArgExtracter.h>
26
#include <Util/ArgExtracter.h>
25
 
27
 
26
#include <GL/glew.h>
28
#include <GL/glew.h>
27
#include <GLGraphics/gel_glut.h>
29
#include <GLGraphics/gel_glut.h>
28
#include <GLGraphics/draw.h>
-
 
29
#include <GLGraphics/IDBufferWireFrameRenderer.h>
-
 
30
#include <GLGraphics/glsl_shader.h>
-
 
31
#include <GLGraphics/GLViewController.h>
-
 
32
 
30
 
33
#include <HMesh/Manifold.h>
31
#include <HMesh/Manifold.h>
34
#include <HMesh/VertexCirculator.h>
32
#include <HMesh/VertexCirculator.h>
35
#include <HMesh/FaceCirculator.h>
33
#include <HMesh/FaceCirculator.h>
36
#include <HMesh/build_manifold.h>
34
#include <HMesh/build_manifold.h>
Line 49... Line 47...
49
#include <HMesh/subdivision.h>
47
#include <HMesh/subdivision.h>
50
 
48
 
51
#include <GLConsole/GLConsole.h>
49
#include <GLConsole/GLConsole.h>
52
#include <Util/Timer.h>
50
#include <Util/Timer.h>
53
#include "harmonics.h"
51
#include "harmonics.h"
54
#include "wireframe.h"
52
#include "Renderer.h"
-
 
53
#include "VisObj.h"
55
 
54
 
56
using namespace std;
55
using namespace std;
57
using namespace HMesh;
56
using namespace HMesh;
58
using namespace Geometry;
57
using namespace Geometry;
59
using namespace GLGraphics;
58
using namespace GLGraphics;
60
using namespace CGLA;
59
using namespace CGLA;
61
using namespace Util;
60
using namespace Util;
62
using namespace LinAlg;
61
using namespace LinAlg;
63
 
62
 
64
int WINX=800, WINY=800;
-
 
65
 
-
 
66
template<typename T>
63
template<typename T>
67
T& get_CVar_ref(const std::string& s)
64
T& get_CVar_ref(const std::string& s)
68
{
65
{
69
	return *reinterpret_cast<T*> (GetCVarData(s));
66
	return *reinterpret_cast<T*> (GetCVarData(s));
70
}
67
}
71
 
68
 
72
int maximum_face_valency(HMesh::Manifold& m)
-
 
73
{
-
 
74
	int max_val = 0;
-
 
75
	for(FaceIter f = m.faces_begin(); f != m.faces_end(); ++f)
-
 
76
		max_val = max(max_val, no_edges(f));
-
 
77
    return max_val;
-
 
78
}
-
 
79
 
-
 
80
class VisObj
-
 
81
{
-
 
82
	string file;
-
 
83
	GLViewController view_ctrl;
-
 
84
	GLuint display_list;
-
 
85
	bool create_display_list;
-
 
86
	Manifold mani;
-
 
87
	Harmonics* harmonics;
-
 
88
	IDBufferWireframeRenderer* idbuff_renderer;
-
 
89
	
-
 
90
public:
-
 
91
	
-
 
92
	Manifold& mesh() {return mani;}
-
 
93
	GLViewController& view_control() {return view_ctrl;}
-
 
94
	
-
 
95
	bool reload(string _file)
-
 
96
	{
-
 
97
		if(_file != "") file = _file;
-
 
98
		mani.clear();
-
 
99
		if(!load(file, mani))
-
 
100
			return false;
-
 
101
		Vec3f c(0,0,0);
-
 
102
		float r = 5;
-
 
103
		mani.get_bsphere(c,r);
-
 
104
		view_ctrl.set_centre(c);
-
 
105
		view_ctrl.set_eye_dist(2*r);
-
 
106
		return true;
-
 
107
	}
-
 
108
	
-
 
109
	VisObj():
-
 
110
	file(""), view_ctrl(WINX,WINY, Vec3f(0), 1.0), display_list(glGenLists(1)), create_display_list(true), harmonics(0),
-
 
111
	idbuff_renderer(0)
-
 
112
	{
-
 
113
	}
-
 
114
	
-
 
115
	void display(bool wire, bool harm, bool flat)
-
 
116
	{
-
 
117
		if(create_display_list)
-
 
118
		{
-
 
119
			create_display_list = false;
-
 
120
			
-
 
121
			glNewList(display_list,GL_COMPILE);
-
 
122
			if(wire)
-
 
123
			{
-
 
124
				if(idbuff_renderer)
-
 
125
				{
-
 
126
					delete idbuff_renderer;
-
 
127
					idbuff_renderer = 0;
-
 
128
				}
-
 
129
				if(GLEW_EXT_geometry_shader4)
-
 
130
				   {
-
 
131
					if(maximum_face_valency(mani) > 3)
-
 
132
						idbuff_renderer = new IDBufferWireframeRenderer(WINX, WINY, mani);
-
 
133
					else
-
 
134
						draw_triangles_in_wireframe(mani,!get_CVar_ref<int>("display.flatshading"), Vec3f(1,0,0));
-
 
135
				   }
-
 
136
				else
-
 
137
				   draw_wireframe_oldfashioned(mani,!get_CVar_ref<int>("display.flatshading"), Vec3f(1,0,0));
-
 
138
			}
-
 
139
			else if(harm)
-
 
140
				harmonics->draw();
-
 
141
			else 
-
 
142
				draw(mani,!flat);
-
 
143
			glEndList();
-
 
144
		}
-
 
145
		view_ctrl.set_gl_modelview();
-
 
146
		if(wire && idbuff_renderer != 0)
-
 
147
			idbuff_renderer->draw(Vec3f(1,0,0),Vec3f(0.5)); 
-
 
148
		glCallList(display_list);
-
 
149
	}
-
 
150
	
-
 
151
	
-
 
152
	void post_create_display_list()
-
 
153
	{
-
 
154
		create_display_list = true;
-
 
155
	}
-
 
156
	
-
 
157
	void harmonics_analyze_mesh()
-
 
158
	{
-
 
159
		delete harmonics;
-
 
160
		harmonics = new Harmonics(mani);
-
 
161
	}
-
 
162
	
-
 
163
	void harmonics_reset_shape()
-
 
164
	{
-
 
165
		if(harmonics)
-
 
166
			harmonics->reset_shape();
-
 
167
	}
-
 
168
	
-
 
169
	void harmonics_parse_key(unsigned char key)
-
 
170
	{
-
 
171
		harmonics->parse_key(key);
-
 
172
	}
-
 
173
	
-
 
174
	void harmonics_partial_reconstruct(int eig0, int eig1, float scale)
-
 
175
	{
-
 
176
		if(harmonics)
-
 
177
			harmonics->partial_reconstruct(eig0, eig1, scale);
-
 
178
	}
-
 
179
	
-
 
180
};
-
 
181
 
-
 
182
inline VisObj& get_vis_obj(int i)
69
inline VisObj& get_vis_obj(int i)
183
{
70
{
184
	static VisObj vo[9];
71
	static VisObj vo[9];
185
	return vo[i];
72
	return vo[i];
186
}
73
}
Line 218... Line 105...
218
    theConsole.Printf("Entering arg1=? or arg1=help will give a description.");
105
    theConsole.Printf("Entering arg1=? or arg1=help will give a description.");
219
    theConsole.Printf("History: Up and Down arrow keys move through history.");
106
    theConsole.Printf("History: Up and Down arrow keys move through history.");
220
    theConsole.Printf("Tab Completion: TAB does tab completion and makes suggestions.");
107
    theConsole.Printf("Tab Completion: TAB does tab completion and makes suggestions.");
221
    theConsole.Printf("");
108
    theConsole.Printf("");
222
    theConsole.Printf("Keyboard commands (when console is not active):");
109
    theConsole.Printf("Keyboard commands (when console is not active):");
223
    theConsole.Printf("w   : toggle wireframe");
110
    theConsole.Printf("w   : switch to display.render_mode = wireframe");
-
 
111
    theConsole.Printf("i   : switch to display.render_mode = isophotes");
-
 
112
    theConsole.Printf("r   : switch to display.render_mode = reflection");
-
 
113
    theConsole.Printf("h   : switch to display.render_mode = harmonics");
224
    theConsole.Printf("f   : toggle flatshading");
114
    theConsole.Printf("f   : toggle smooth/flat shading");
225
    theConsole.Printf("1-9 : switch between active meshes.");
115
    theConsole.Printf("1-9 : switch between active meshes.");
226
    theConsole.Printf("d   : (display.show_harmonics = 1) diffuse light on and off");
116
    theConsole.Printf("d   : (display.render_mode = harmonics) diffuse light on and off");
227
    theConsole.Printf("h   : (display.show_harmonics = 1) highlight on and off ");
117
    theConsole.Printf("h   : (display.render_mode = harmonics) highlight on and off ");
228
    theConsole.Printf("+/- : (display.show_harmonics = 1) which eigenvector to show");
118
    theConsole.Printf("+/- : (display.render_mode = harmonics) which eigenvector to show");
229
    theConsole.Printf("q   : quit program");
119
    theConsole.Printf("q   : quit program");
230
    theConsole.Printf("ESC : open console");
120
    theConsole.Printf("ESC : open console");
231
    theConsole.Printf("");
121
    theConsole.Printf("");
232
    theConsole.Printf("Mouse: Left button rotates, middle zooms, right pans");
122
    theConsole.Printf("Mouse: Left button rotates, middle zooms, right pans");
233
    theConsole.Printf("----------------- HELP -----------------");
123
    theConsole.Printf("----------------- HELP -----------------");
Line 366... Line 256...
366
	
256
	
367
	// make sure every face knows its number
257
	// make sure every face knows its number
368
	m.enumerate_faces();
258
	m.enumerate_faces();
369
	
259
	
370
	vector<Vec3f> vertices(m.no_faces());
260
	vector<Vec3f> vertices(m.no_faces());
371
	vector<int> faces(m.no_vertices());
261
	vector<int> faces;
372
	vector<int> indices;
262
	vector<int> indices;
373
	
263
	
374
	// Create new vertices. Each face becomes a vertex whose position
264
	// Create new vertices. Each face becomes a vertex whose position
375
	// is the centre of the face
265
	// is the centre of the face
376
	int i=0;
266
	int i=0;
Line 379... Line 269...
379
	
269
	
380
	// Create new faces. Each vertex is a new face with N=valency of vertex
270
	// Create new faces. Each vertex is a new face with N=valency of vertex
381
	// edges.
271
	// edges.
382
	i=0;
272
	i=0;
383
	for(VertexIter v=m.vertices_begin(); v!= m.vertices_end(); ++v,++i)
273
	for(VertexIter v=m.vertices_begin(); v!= m.vertices_end(); ++v,++i)
-
 
274
		if(!is_boundary(v))
384
	{
275
		{
385
		VertexCirculator vc(v);
276
			VertexCirculator vc(v);
386
		vector<int> index_tmp;
277
			vector<int> index_tmp;
387
		for(; !vc.end(); ++vc)
278
			for(; !vc.end(); ++vc)
388
			index_tmp.push_back(vc.get_face()->touched);
279
				index_tmp.push_back(vc.get_face()->touched);
Line 391... Line 282...
391
		// The circulator moves around the face in a clockwise fashion
282
			// The circulator moves around the face in a clockwise fashion
392
		// so we just reverse the ordering.
283
			// so we just reverse the ordering.
393
		indices.insert(indices.end(), index_tmp.rbegin(), index_tmp.rend());
284
			indices.insert(indices.end(), index_tmp.rbegin(), index_tmp.rend());
394
		
285
			
395
		// Insert face valency in the face vector.
286
			// Insert face valency in the face vector.
396
		faces[i] = vc.no_steps();
287
			faces.push_back(vc.no_steps());
397
	}
288
		}
398
	
289
	
399
	// Clear the manifold before new geometry is inserted.
290
	// Clear the manifold before new geometry is inserted.
400
	m.clear();
291
	m.clear();
401
	
292
	
Line 808... Line 699...
808
 
699
 
809
void display() 
700
void display() 
810
{
701
{
811
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
702
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
812
	
703
	
813
	static CVar<int> display_wireframe("display.wireframe",0);
704
	static CVar<string> display_render_mode("display.render_mode","");
814
	static CVar<int> display_eigenmodes("display.show_harmonics",0);
-
 
815
	static CVar<int> display_flat("display.flatshading",0);
705
	static CVar<int> display_smooth("display.smooth_shading",1);
816
	
706
	
817
	glPushMatrix();
707
	glPushMatrix();
818
	
708
	
819
	avo().display(display_wireframe, display_eigenmodes, display_flat);
709
	avo().display(display_render_mode, display_smooth);
820
	
710
	
821
	glPopMatrix();
711
	glPopMatrix();
822
	
712
	
823
	glUseProgram(0);
713
	glUseProgram(0);
824
	theConsole.RenderConsole();
714
	theConsole.RenderConsole();
Line 892... Line 782...
892
		}
782
		}
893
		if(key == 13)	avo().post_create_display_list();
783
		if(key == 13)	avo().post_create_display_list();
894
		
784
		
895
	}	
785
	}	
896
	else {
786
	else {
897
		int& display_wireframe = get_CVar_ref<int>("display.wireframe");
787
		string& display_render_mode = get_CVar_ref<string>("display.render_mode");
898
		int& display_flat = get_CVar_ref<int>("display.flatshading");
788
		int& display_smooth = get_CVar_ref<int>("display.smooth_shading");
899
		int& active  = get_CVar_ref<int>("active_mesh");
789
		int& active  = get_CVar_ref<int>("active_mesh");
900
		
790
		
901
		
791
		
902
		switch(key) {
792
		switch(key) {
903
			case 'q': exit(0);
793
			case 'q': exit(0);
Line 912... Line 802...
912
			case '6':
802
			case '6':
913
			case '7':
803
			case '7':
914
			case '8':
804
			case '8':
915
			case '9':
805
			case '9':
916
				active = key - '1'; break;
806
				active = key - '1'; break;
917
			case 'f': display_flat = !display_flat; break;
807
			case 'f': display_smooth = !display_smooth; break;
918
			case 'w':
808
			case 'w':
-
 
809
				display_render_mode = "wire"; break;
-
 
810
			case 'n':
-
 
811
				display_render_mode = "normal"; break;
-
 
812
			case 'i':
919
				display_wireframe = !display_wireframe;
813
				display_render_mode = "isophotes"; break;
-
 
814
			case 'r':
-
 
815
				display_render_mode = "reflection"; break;
920
				break;
816
			case 'h':
-
 
817
				display_render_mode = "harmonics"; break;
921
		}
818
		}
922
		
819
		
923
		if(get_CVar_ref<int>("display.show_harmonics"))
820
		if(display_render_mode.substr(0,4) == "harm")
924
			avo().harmonics_parse_key(key);
821
			avo().harmonics_parse_key(key);
925
		
822
		
926
		avo().post_create_display_list();		
823
		avo().post_create_display_list();		
927
	}
824
	}
928
}
825
}