File: button.cc

package info (click to toggle)
gtkglextmm 1.2.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,140 kB
  • sloc: sh: 8,904; cpp: 4,594; xml: 1,605; ansic: 832; makefile: 394; perl: 179
file content (454 lines) | stat: -rw-r--r-- 9,144 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// -*- C++ -*-
/*
 * button.cc:
 * Simple toggle button example.
 *
 * written by Naofumi Yasufuku  <naofumi@users.sourceforge.net>
 */

#include <iostream>
#include <cstdlib>
#include <cmath>

#include <gtkmm.h>

#include <gtkglmm.h>

#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif

#include <GL/gl.h>
#include <GL/glu.h>


///////////////////////////////////////////////////////////////////////////////
//
// OpenGL scene.
//
///////////////////////////////////////////////////////////////////////////////

class GLScene : public Gtk::GL::DrawingArea
{
public:
  static const unsigned int TIMEOUT_INTERVAL;

public:
  GLScene(const Glib::RefPtr<const Gdk::GL::Config>& config);
  virtual ~GLScene();

protected:
  virtual void on_realize();
  virtual bool on_configure_event(GdkEventConfigure* event);
  virtual bool on_expose_event(GdkEventExpose* event);
  virtual bool on_map_event(GdkEventAny* event);
  virtual bool on_unmap_event(GdkEventAny* event);
  virtual bool on_visibility_notify_event(GdkEventVisibility* event);
  virtual bool on_timeout();

public:
  // Invalidate whole window.
  void invalidate() {
    get_window()->invalidate_rect(get_allocation(), false);
  }

  // Update window synchronously (fast).
  void update()
  { get_window()->process_updates(false); }

protected:
  void timeout_add();
  void timeout_remove();

public:
  void toggle_animation();

protected:
  bool m_Animate;
  // timeout signal connection:
  sigc::connection m_ConnectionTimeout;

protected:
  GLfloat m_Angle;
  GLfloat m_PosY;

};

const unsigned int GLScene::TIMEOUT_INTERVAL = 10;

GLScene::GLScene(const Glib::RefPtr<const Gdk::GL::Config>& config)
  : Gtk::GL::DrawingArea(config),
    m_Animate(true), m_Angle(0.0), m_PosY(0.0)
{
  set_size_request(200, 200);
  add_events(Gdk::VISIBILITY_NOTIFY_MASK);
}

GLScene::~GLScene()
{
}

void GLScene::on_realize()
{
  // We need to call the base on_realize()
  Gtk::GL::DrawingArea::on_realize();

  //
  // Get GL::Window.
  //

  Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();

  //
  // GL calls.
  //

  // *** OpenGL BEGIN ***
  if (!glwindow->gl_begin(get_gl_context()))
    return;

  static GLfloat ambient[]  = { 0.0, 0.0, 0.0, 1.0 };
  static GLfloat diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
  static GLfloat position[] = { 1.0, 1.0, 1.0, 0.0 };
  static GLfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0};
  static GLfloat local_view[] = {0.0};

  glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
  glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
  glLightfv (GL_LIGHT0, GL_POSITION, position);
  glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  glEnable (GL_LIGHTING);
  glEnable (GL_LIGHT0);
  glEnable (GL_DEPTH_TEST);

  glClearColor (1.0, 1.0, 1.0, 1.0);
  glClearDepth (1.0);

  glwindow->gl_end();
  // *** OpenGL END ***
}

bool GLScene::on_configure_event(GdkEventConfigure* event)
{
  //
  // Get GL::Window.
  //

  Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();

  //
  // GL calls.
  //

  // *** OpenGL BEGIN ***
  if (!glwindow->gl_begin(get_gl_context()))
    return false;

  GLfloat w = get_width();
  GLfloat h = get_height();
  GLfloat aspect;

  glViewport(0, 0, static_cast<GLsizei>(w), static_cast<GLsizei>(h));

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w > h)
    {
      aspect = w / h;
      glFrustum(-aspect, aspect, -1.0, 1.0, 5.0, 60.0);
    }
  else
    {
      aspect = h / w;
      glFrustum(-1.0, 1.0, -aspect, aspect, 5.0, 60.0);
    }

  glMatrixMode(GL_MODELVIEW);

  glwindow->gl_end();
  // *** OpenGL END ***

  return true;
}

bool GLScene::on_expose_event(GdkEventExpose* event)
{
  //
  // Get GL::Window.
  //

  Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();

  //
  // GL calls.
  //

  // *** OpenGL BEGIN ***
  if (!glwindow->gl_begin(get_gl_context()))
    return false;

  // brass
  static GLfloat ambient[4]  = { 0.329412, 0.223529, 0.027451, 1.0 };
  static GLfloat diffuse[4]  = { 0.780392, 0.568627, 0.113725, 1.0 };
  static GLfloat specular[4] = { 0.992157, 0.941176, 0.807843, 1.0 };
  static GLfloat shininess   = 0.21794872 * 128.0;

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();
  glTranslatef(0.0, 0.0, -10.0);

  glPushMatrix();
    glTranslatef(0.0, m_PosY, 0.0);
    glRotatef(m_Angle, 0.0, 1.0, 0.0);
    glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
    glMaterialf(GL_FRONT, GL_SHININESS, shininess);
    glwindow->draw_torus(true, 0.3, 0.6, 30, 30);
  glPopMatrix();

  // Swap buffers.
  if (glwindow->is_double_buffered())
    glwindow->swap_buffers();
  else
    glFlush();

  glwindow->gl_end();
  // *** OpenGL END ***

  return true;
}

bool GLScene::on_timeout()
{
  m_Angle += 1.0;
  if (m_Angle >= 360.0)
    m_Angle -= 360.0;

  float t = m_Angle * G_PI / 180.0;
  if (t > G_PI)
    t = 2.0 * G_PI - t;

  m_PosY = 2.0 * (std::sin (t) + 0.4 * std::sin (3.0*t)) - 1.0;

  // Invalidate whole window.
  invalidate();
  // Update window synchronously (fast).
  update();

  return true;
}

void GLScene::timeout_add()
{
  if (!m_ConnectionTimeout.connected())
    m_ConnectionTimeout = Glib::signal_timeout().connect(
      sigc::mem_fun(*this, &GLScene::on_timeout), TIMEOUT_INTERVAL);
}

void GLScene::timeout_remove()
{
  if (m_ConnectionTimeout.connected())
    m_ConnectionTimeout.disconnect();
}

bool GLScene::on_map_event(GdkEventAny* event)
{
  if (m_Animate)
    timeout_add();

  return true;
}

bool GLScene::on_unmap_event(GdkEventAny* event)
{
  timeout_remove();

  return true;
}

bool GLScene::on_visibility_notify_event(GdkEventVisibility* event)
{
  if (m_Animate)
    {
      if (event->state == GDK_VISIBILITY_FULLY_OBSCURED)
        timeout_remove();
      else
        timeout_add();
    }

  return true;
}

void GLScene::toggle_animation()
{
  m_Animate = !m_Animate;

  if (m_Animate)
    {
      timeout_add();
    }
  else
    {
      timeout_remove();
      invalidate();
    }
}


///////////////////////////////////////////////////////////////////////////////
//
// Toggle button which contains an OpenGL scene.
//
///////////////////////////////////////////////////////////////////////////////

class GLToggleButton : public Gtk::ToggleButton
{
public:
  GLToggleButton(const Glib::RefPtr<const Gdk::GL::Config>& config);
  virtual ~GLToggleButton();

protected:
  virtual void on_toggled();

protected:
  Gtk::VBox m_VBox;
  GLScene m_GLScene;
  Gtk::Label m_Label;

};

GLToggleButton::GLToggleButton(const Glib::RefPtr<const Gdk::GL::Config>& config)
  : m_VBox(false, 0), m_GLScene(config), m_Label("Toggle Animation")
{
  m_VBox.set_border_width(10);
  m_VBox.pack_start(m_GLScene);
  m_VBox.pack_start(m_Label, Gtk::PACK_SHRINK, 10);

  add(m_VBox);
}

GLToggleButton::~GLToggleButton()
{
}

void GLToggleButton::on_toggled()
{
  m_GLScene.toggle_animation();
}


///////////////////////////////////////////////////////////////////////////////
//
// The application class.
//
///////////////////////////////////////////////////////////////////////////////

class Button : public Gtk::Window
{
public:
  Button(const Glib::RefPtr<const Gdk::GL::Config>& config);
  virtual ~Button();

protected:
  virtual bool on_delete_event(GdkEventAny* event);

protected:
  // member widgets:
  GLToggleButton m_Button;

};

Button::Button(const Glib::RefPtr<const Gdk::GL::Config>& config)
  : m_Button(config)
{
  //
  // Top-level window.
  //

  set_title("button");

  // Get automatically redrawn if any of their children changed allocation.
  set_reallocate_redraws(true);
  // Set border width.
  set_border_width(10);

  //
  // Add toggle button.
  //

  add(m_Button);

  //
  // Show window.
  //

  show_all();
}

Button::~Button()
{
}

bool Button::on_delete_event(GdkEventAny* event)
{
  Gtk::Main::quit();

  return true;
}


///////////////////////////////////////////////////////////////////////////////
//
// Main.
//
///////////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
  Gtk::Main kit(argc, argv);

  //
  // Init gtkglextmm.
  //

  Gtk::GL::init(argc, argv);

  //
  // Configure OpenGL-capable visual.
  //

  Glib::RefPtr<Gdk::GL::Config> glconfig;

  // Try double-buffered visual
  glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB    |
                                     Gdk::GL::MODE_DEPTH  |
                                     Gdk::GL::MODE_DOUBLE);
  if (!glconfig)
    {
      std::cerr << "*** Cannot find the double-buffered visual.\n"
                << "*** Trying single-buffered visual.\n";

      // Try single-buffered visual
      glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB   |
                                         Gdk::GL::MODE_DEPTH);
      if (!glconfig)
        {
          std::cerr << "*** Cannot find any OpenGL-capable visual.\n";
          std::exit(1);
        }
    }

  //
  // Instantiate and run the application.
  //

  Button button(glconfig);

  kit.run(button);

  return 0;
}