~ubuntu-branches/debian/squeeze/gmsh/squeeze

« back to all changes in this revision

Viewing changes to utils/api_demos/mainGlut.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme, Christophe Prud'homme
  • Date: 2009-09-02 18:12:15 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20090902181215-yla8zvcas2ucvkm9
[Christophe Prud'homme]
* New upstream release
  + fixed surface mesh orientation bug introduced in 2.4.0;
  + mesh and graphics code refactoring;
  + small usability enhancements and bug fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// A simple example on how to build a GUI frontend to Gmsh using GLUT
 
3
//
 
4
 
 
5
#if defined(__APPLE__)
 
6
#  include <GLUT/glut.h>
 
7
#else
 
8
#  include <GL/glut.h>
 
9
#endif
 
10
#include <Gmsh/Gmsh.h>
 
11
#include <gmsh/GModel.h>
 
12
#include <gmsh/MElement.h>
 
13
#include <Gmsh/drawContext.h>
 
14
 
 
15
drawContext *ctx = 0;
 
16
 
 
17
class drawContextGlut : public drawContextGlobal{
 
18
 public:
 
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)
 
23
  {
 
24
    return glutBitmapLength(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)str);
 
25
  }
 
26
  int getStringHeight(){ return 18; }
 
27
  int getStringDescent(){ return 6; }
 
28
  void drawString(const char *str)
 
29
  {
 
30
    for (int i = 0; i < strlen(str); i++) 
 
31
      glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, str[i]); 
 
32
  }
 
33
};
 
34
 
 
35
// GLUT callbacks
 
36
void display()
 
37
{
 
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();
 
42
  glutSwapBuffers(); 
 
43
}
 
44
 
 
45
void reshape(int w, int h)
 
46
{
 
47
  ctx->viewport[2] = w;
 
48
  ctx->viewport[3] = h;
 
49
  display();
 
50
}
 
51
 
 
52
void keyboard(unsigned char key, int x, int y)
 
53
{
 
54
  switch(key){
 
55
  case '1': GModel::current()->mesh(1); break;
 
56
  case '2': GModel::current()->mesh(2); break;
 
57
  case '3': GModel::current()->mesh(3); break;
 
58
  }
 
59
  display();
 
60
}
 
61
 
 
62
static int xprev = 0, yprev = 0, specialkey = 0;
 
63
 
 
64
void motion(int x, int y)
 
65
{
 
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];
 
76
    }
 
77
  }
 
78
  else{
 
79
    ctx->addQuaternion((2. * xprev - w) / w, (h - 2. * yprev) / h,
 
80
                       (2. * x - w) / w, (h - 2. * y) / h);
 
81
  }
 
82
  xprev = x;
 
83
  yprev = y;
 
84
  display();
 
85
}
 
86
 
 
87
void mouse(int button, int state, int x, int y)
 
88
{
 
89
  specialkey = glutGetModifiers();
 
90
  xprev = x;
 
91
  yprev = y;
 
92
}
 
93
 
 
94
int main(int argc, char **argv)
 
95
{
 
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.); 
 
102
 
 
103
  for(int i = 1; i < argc; i++) GmshMergeFile(argv[i]);
 
104
 
 
105
  ctx = new drawContext();
 
106
  drawContext::setGlobal(new drawContextGlut);
 
107
 
 
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);
 
118
  glutMainLoop();
 
119
 
 
120
  GmshFinalize();
 
121
  return 0;
 
122
}