00001
00002
00003
00004
00005
00006
00007
00008
00009
#ifndef _SHAPES_H
00010
#define _SHAPES_H
00011
00012
#include <gtkmm.h>
00013
00014
#include <gtkglmm.h>
00015
00016
00018
00019
00020
00022
00023
namespace Shapes
00024 {
00025
00026
class Scene;
00027
00028
00029
00030
00031
00032
class View :
public sigc::trackable
00033 {
00034
friend class Scene;
00035
00036
public:
00037
static const float NEAR_CLIP;
00038
static const float FAR_CLIP;
00039
00040
static const float INIT_POS_X;
00041
static const float INIT_POS_Y;
00042
static const float INIT_POS_Z;
00043
00044
static const float INIT_AXIS_X;
00045
static const float INIT_AXIS_Y;
00046
static const float INIT_AXIS_Z;
00047
static const float INIT_ANGLE;
00048
00049
static const float INIT_SCALE;
00050
00051
static const float SCALE_MAX;
00052
static const float SCALE_MIN;
00053
00054
static const float ANIMATE_THRESHOLD;
00055
00056
public:
00057 View();
00058
virtual ~View();
00059
00060
public:
00061
void frustum(
int w,
int h);
00062
00063
void xform();
00064
00065
void reset();
00066
00067
void set_pos(
float x,
float y,
float z)
00068 { m_Pos[0] = x; m_Pos[1] = y; m_Pos[2] = z; }
00069
00070
void set_quat(
float q0,
float q1,
float q2,
float q3)
00071 { m_Quat[0] = q0; m_Quat[1] = q1; m_Quat[2] = q2; m_Quat[3] = q3; }
00072
00073
void set_scale(
float scale)
00074 { m_Scale = scale; }
00075
00076
void enable_animation();
00077
00078
void disable_animation();
00079
00080
bool is_animate()
const
00081
{
return m_Animate; }
00082
00083
protected:
00084
00085
virtual bool on_button_press_event(GdkEventButton* event, Scene* scene);
00086
virtual bool on_button_release_event(GdkEventButton* event, Scene* scene);
00087
virtual bool on_motion_notify_event(GdkEventMotion* event, Scene* scene);
00088
00089
private:
00090
float m_Pos[3];
00091
float m_Quat[4];
00092
float m_Scale;
00093
00094
float m_QuatDiff[4];
00095
float m_BeginX;
00096
float m_BeginY;
00097
float m_DX;
00098
float m_DY;
00099
00100
bool m_Animate;
00101
00102 };
00103
00104
00105
00106
00107
00108
00109
class Model
00110 {
00111
friend class Scene;
00112
00113
public:
00114
static const unsigned int NUM_SHAPES;
00115
00116
enum ShapeType
00117 {
00118 CUBE,
00119 SPHERE,
00120 CONE,
00121 TORUS,
00122 TETRAHEDRON,
00123 OCTAHEDRON,
00124 DODECAHEDRON,
00125 ICOSAHEDRON,
00126 TEAPOT,
00127 };
00128
00129
static const ShapeType SHAPE_CUBE;
00130
static const ShapeType SHAPE_SPHERE;
00131
static const ShapeType SHAPE_CONE;
00132
static const ShapeType SHAPE_TORUS;
00133
static const ShapeType SHAPE_TETRAHEDRON;
00134
static const ShapeType SHAPE_OCTAHEDRON;
00135
static const ShapeType SHAPE_DODECAHEDRON;
00136
static const ShapeType SHAPE_ICOSAHEDRON;
00137
static const ShapeType SHAPE_TEAPOT;
00138
00139
public:
00140
00141
struct MaterialProp
00142 {
00143 GLfloat ambient[4];
00144 GLfloat diffuse[4];
00145 GLfloat specular[4];
00146 GLfloat shininess;
00147 };
00148
00149
static const MaterialProp MAT_EMERALD;
00150
static const MaterialProp MAT_JADE;
00151
static const MaterialProp MAT_OBSIDIAN;
00152
static const MaterialProp MAT_PEARL;
00153
static const MaterialProp MAT_RUBY;
00154
static const MaterialProp MAT_TURQUOISE;
00155
static const MaterialProp MAT_BRASS;
00156
static const MaterialProp MAT_BRONZE;
00157
static const MaterialProp MAT_CHROME;
00158
static const MaterialProp MAT_COPPER;
00159
static const MaterialProp MAT_GOLD;
00160
static const MaterialProp MAT_SILVER;
00161
00162
public:
00163 Model();
00164
virtual ~Model();
00165
00166
private:
00167
void init_gl(Glib::RefPtr<Gdk::GL::Drawable>& gldrawable);
00168
00169
public:
00170
void draw(Glib::RefPtr<Gdk::GL::Drawable>& gldrawable);
00171
00172
void set_shape(ShapeType shape)
00173 { m_CurrentShape = shape; }
00174
00175
void set_material(
const MaterialProp* material)
00176 { m_CurrentMat = material; }
00177
00178
private:
00179
unsigned int m_ListBase;
00180 ShapeType m_CurrentShape;
00181
const MaterialProp* m_CurrentMat;
00182
00183 };
00184
00185
00186
00187
00188
00189
00190
class Scene :
public Gtk::GL::DrawingArea
00191 {
00192
friend class View;
00193
friend class Model;
00194
00195
public:
00196
00197
static const float CLEAR_COLOR[4];
00198
static const float CLEAR_DEPTH;
00199
00200
static const float LIGHT0_POSITION[4];
00201
static const float LIGHT0_AMBIENT[4];
00202
static const float LIGHT0_DIFFUSE[4];
00203
00204
static const float LIGHT_MODEL_AMBIENT[4];
00205
static const float LIGHT_MODEL_LOCAL_VIEWER[1];
00206
00207
public:
00208
explicit Scene();
00209
virtual ~Scene();
00210
00211
protected:
00212
00213
virtual void on_realize();
00214
virtual bool on_configure_event(GdkEventConfigure* event);
00215
virtual bool on_expose_event(GdkEventExpose* event);
00216
virtual bool on_button_press_event(GdkEventButton* event);
00217
virtual bool on_unmap_event(GdkEventAny* event);
00218
virtual bool on_visibility_notify_event(GdkEventVisibility* event);
00219
virtual bool on_idle();
00220
00221
public:
00222
00223
void invalidate() {
00224 get_window()->invalidate_rect(get_allocation(),
false);
00225 }
00226
00227
00228
void update()
00229 { get_window()->process_updates(
false); }
00230
00231
protected:
00232
00233 sigc::connection m_ConnectionIdle;
00234
00235
void idle_add();
00236
void idle_remove();
00237
00238
protected:
00239
void change_shape(Model::ShapeType shape);
00240
void change_material(
const Model::MaterialProp* material);
00241
00242
protected:
00243 Gtk::Menu* create_popup_menu();
00244
00245
protected:
00246
00247 Gtk::Menu* m_Menu;
00248
00249
protected:
00250
00251 View m_View;
00252 Model m_Model;
00253
00254 };
00255
00256
00257
00258
00259
00260
00261
class Application :
public Gtk::Window
00262 {
00263
public:
00264
static const Glib::ustring APP_NAME;
00265
00266
public:
00267 Application();
00268
virtual ~Application();
00269
00270
protected:
00271
00272
virtual void on_button_quit_clicked();
00273
virtual bool on_key_press_event(GdkEventKey* event);
00274
00275
protected:
00276
00277 Gtk::VBox m_VBox;
00278 Scene m_Scene;
00279 Gtk::Button m_ButtonQuit;
00280 };
00281
00282
00283 }
00284
00285
00286
#endif // _SHAPES_H