2
// A simple example on how to build a GUI frontend to Gmsh using GLUT
6
# include <GLUT/glut.h>
10
#include <Gmsh/Gmsh.h>
11
#include <gmsh/GModel.h>
12
#include <gmsh/MElement.h>
13
#include <Gmsh/drawContext.h>
17
class drawContextGlut : public drawContextGlobal{
19
void draw(){ ctx->draw3d(); ctx->draw2d(); }
20
const char *getFontName(int index){ return "Helvetica"; }
21
int getFontSize(){ return 18; }
22
double getStringWidth(const char *str)
24
return glutBitmapLength(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)str);
26
int getStringHeight(){ return 18; }
27
int getStringDescent(){ return 6; }
28
void drawString(const char *str)
30
for (int i = 0; i < strlen(str); i++)
31
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, str[i]);
38
glViewport(ctx->viewport[0], ctx->viewport[1],
39
ctx->viewport[2], ctx->viewport[3]);
40
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
41
drawContext::global()->draw();
45
void reshape(int w, int h)
52
void keyboard(unsigned char key, int x, int y)
55
case '1': GModel::current()->mesh(1); break;
56
case '2': GModel::current()->mesh(2); break;
57
case '3': GModel::current()->mesh(3); break;
62
static int xprev = 0, yprev = 0, specialkey = 0;
64
void motion(int x, int y)
66
int w = ctx->viewport[2];
67
int h = ctx->viewport[3];
68
if(specialkey == GLUT_ACTIVE_SHIFT){
69
double dx = x - xprev;
70
double dy = y - yprev;
71
if(fabs(dy) > fabs(dx)) {
72
double fact = (4. * fabs(dy) + h) / (double)h;
73
ctx->s[0] *= ((dy > 0) ? fact : 1. / fact);
74
ctx->s[1] = ctx->s[0];
75
ctx->s[2] = ctx->s[0];
79
ctx->addQuaternion((2. * xprev - w) / w, (h - 2. * yprev) / h,
80
(2. * x - w) / w, (h - 2. * y) / h);
87
void mouse(int button, int state, int x, int y)
89
specialkey = glutGetModifiers();
94
int main(int argc, char **argv)
96
GmshInitialize(argc, argv);
97
GmshSetOption("General", "Terminal", 1.);
98
GmshSetOption("View", "IntervalsType", 1.);
99
GmshSetOption("View", "AdaptVisualizationGrid", 1.);
100
GmshSetOption("View", "TargetError", 0.00001);
101
GmshSetOption("View", "MaxRecursionLevel", 3.);
103
for(int i = 1; i < argc; i++) GmshMergeFile(argv[i]);
105
ctx = new drawContext();
106
drawContext::setGlobal(new drawContextGlut);
108
glutInit(&argc, argv);
109
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
110
glutInitWindowSize(ctx->viewport[2], ctx->viewport[3]);
111
glutInitWindowPosition(100, 100);
112
glutCreateWindow("GLUT Gmsh Viewer");
113
glutDisplayFunc(display);
114
glutReshapeFunc(reshape);
115
glutKeyboardFunc(keyboard);
116
glutMotionFunc(motion);
117
glutMouseFunc(mouse);