Rev 387 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* MeshEdit is a small application which allows you to load and edit a mesh.
* The mesh will be stored in GEL's half edge based Manifold data structure.
* A number of editing operations are supported. Most of these are accessible from the
* console that pops up when you hit 'esc'.
*
* Created by J. Andreas Bærentzen on 15/08/08.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/
#include <iostream>
#include <CGLA/eigensolution.h>
#include <CGLA/Vec2d.h>
#include <CGLA/Vec3d.h>
#include <CGLA/Mat3x3d.h>
#include <CGLA/Mat2x2d.h>
#include <CGLA/Mat2x3d.h>
#include <LinAlg/Matrix.h>
#include <LinAlg/Vector.h>
#include <LinAlg/LapackFunc.h>
#include <Util/Timer.h>
#include <Util/ArgExtracter.h>
#include <GL/glew.h>
#include <GLGraphics/gel_glut.h>
#include <GLGraphics/draw.h>
#include <GLGraphics/glsl_shader.h>
#include <GLGraphics/GLViewController.h>
#include <HMesh/Manifold.h>
#include <HMesh/VertexCirculator.h>
#include <HMesh/FaceCirculator.h>
#include <HMesh/build_manifold.h>
#include <HMesh/mesh_optimization.h>
#include <HMesh/triangulate.h>
#include <HMesh/load.h>
#include <HMesh/x3d_save.h>
#include <GLConsole/GLConsole.h>
#include "harmonics.h"
#include "wireframe.h"
// Single global instance so glut can get access
GLConsole theConsole;
////////////////////////////////////////////////////////////////////////////////
char* ConsoleHelp(std::vector<std::string> &args)
{
theConsole.Printf("");
theConsole.Printf("----------------- HELP -----------------");
theConsole.Printf("Press ~ key to open and close console");
theConsole.Printf("Press TAB to see the available commands and functions");
theConsole.Printf("Functions are shown in green and variables in yellow");
theConsole.Printf("Setting a value: [command] = value");
theConsole.Printf("Getting a value: [command]");
theConsole.Printf("Functions: [function] [arg1] [arg2] ...");
theConsole.Printf("Entering just the function name will give a description.");
theConsole.Printf("History: Up and Down arrow keys move through history.");
theConsole.Printf("Tab Completion: TAB does tab completion and makes suggestions.");
theConsole.Printf("----------------- HELP -----------------");
theConsole.Printf("");
return "";
}
using namespace std;
using namespace HMesh;
using namespace Geometry;
using namespace GLGraphics;
using namespace CGLA;
using namespace Util;
using namespace LinAlg;
namespace
{
GLViewController* view_ctrl;
int WINX=800, WINY=800;
Manifold mani;
bool create_display_list = true;
}
char* console_partial_reconstruct(std::vector<std::string> &args)
{
int E0,E1;
float scale;
istringstream a0(args[0]);
a0 >> E0;
istringstream a1(args[1]);
a1 >> E1;
istringstream a2(args[2]);
a2 >> scale;
partial_reconstruct(mani, E0,E1,scale);
return "";
}
char* console_reset_shape(std::vector<std::string> &args)
{
reset_shape(mani);
return "";
}
void reshape(int W, int H)
{
view_ctrl->reshape(W,H);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
static CVar<int> do_wireframe("display.wireframe",0);
glPushMatrix();
view_ctrl->set_gl_modelview();
if(create_display_list)
{
create_display_list = false;
glNewList(1,GL_COMPILE);
if(do_wireframe)
{
enable_wireframe();
draw(mani);
}
else
draw_eigenvalues(mani);
glEndList();
}
glCallList(1);
glPopMatrix();
glUseProgram(0);
theConsole.RenderConsole();
glutSwapBuffers();
}
void animate()
{
usleep( (int)1e4 );
view_ctrl->try_spin();
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
Vec2i pos(x,y);
if (state==GLUT_DOWN)
{
if (button==GLUT_LEFT_BUTTON)
view_ctrl->grab_ball(ROTATE_ACTION,pos);
else if (button==GLUT_MIDDLE_BUTTON)
view_ctrl->grab_ball(ZOOM_ACTION,pos);
else if (button==GLUT_RIGHT_BUTTON)
view_ctrl->grab_ball(PAN_ACTION,pos);
}
else if (state==GLUT_UP)
view_ctrl->release_ball();
}
void motion(int x, int y) {
Vec2i pos(x,y);
view_ctrl->roll_ball(Vec2i(x,y));
}
void keyboard_spec(int key, int x, int y)
{
int mod = glutGetModifiers();
if( theConsole.isOpen() ) {
// If shift held, scroll the console
if( mod == GLUT_ACTIVE_SHIFT ) {
switch (key){
case GLUT_KEY_UP:
theConsole.ScrollDownLine();
break;
case GLUT_KEY_DOWN:
theConsole.ScrollUpLine();
break;
}
} else {
theConsole.StandardKeyBindings( key );
}
}
}
template<typename T>
T& get_CVar_ref(const std::string& s)
{
return *reinterpret_cast<T*> (GetCVarData(s));
}
void keyboard(unsigned char key, int x, int y)
{
if(theConsole.isOpen())
switch(key) {
case '\033':
theConsole.ToggleConsole();
default:
//send keystroke to console
if( theConsole.isOpen() ){
theConsole.EnterCommandCharacter(key);
}
break;
}
else {
int& display_eigenvalue = get_CVar_ref<int>("display.eigenvalue");
int& display_wireframe = get_CVar_ref<int>("display.wireframe");
int& display_diffuse = get_CVar_ref<int>("display.diffuse");
int& display_highlight = get_CVar_ref<int>("display.highlight");
switch(key) {
case 'q': exit(0);
case '\033':
theConsole.ToggleConsole();
break;
case '+':
display_eigenvalue = min(display_eigenvalue+1, MAX_E);
break;
case '-':
display_eigenvalue = max(display_eigenvalue-1, 0);
break;
case '1': E = 1; reconstruct(mani,E);
break;
case '>': if(E < MAX_E) partial_reconstruct(mani,E,++E);
break;
case '<': E = max(E-1, 0); reconstruct(mani,E);
break;
case 'd':
display_diffuse = !display_diffuse;
break;
case 'h':
display_highlight = !display_highlight;
break;
case 'w':
display_wireframe = !display_wireframe;
break;
}
}
create_display_list = true;
}
void init_glut(int argc, char** argv)
{
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(WINX, WINY);
glutInit(&argc, argv);
glutCreateWindow("Shape Harmonics");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(keyboard_spec);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutIdleFunc(animate);
}
void init_gl()
{
glewInit();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
Vec3f c(0,0,0);
float r = 5;
mani.get_bsphere(c,r);
view_ctrl = new GLViewController(WINX,WINY, c,r*2);
initialize_wireframe_shaders();
// Set the value of a uniform
//glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.50f, 0.50f, 0.50f, 0.f);
glColor4f(1.0f, 1.0f, 1.0f, 0.f);
glEnable(GL_DEPTH_TEST);
static CVar<ConsoleFunc> help( "help", ConsoleHelp );
static CVar<ConsoleFunc> rs("reset_shape", console_reset_shape);
static CVar<ConsoleFunc> pr("partial_reconstruct", console_partial_reconstruct);
}
int main(int argc, char** argv)
{
ArgExtracter ae(argc, argv);
ae.extract("-E", E);
if(argc>1)
{
string file = ae.get_last_arg();
load(file, mani);
}
init_glut(argc,argv);
init_gl();
init_harmonics(mani);
glutMainLoop();
}