Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Examples

shapes.h

Geometric object rendering example.
// -*- C++ -*- /* * shapes.h: * shapes demo. * * written by Naofumi Yasufuku <naofumi@users.sourceforge.net> */ #ifndef _SHAPES_H #define _SHAPES_H #include <gtkmm.h> #include <gtkglmm.h> // // Shapes classes. // namespace Shapes { class Scene; // // View class. // class View : public sigc::trackable { friend class Scene; public: static const float NEAR_CLIP; static const float FAR_CLIP; static const float INIT_POS_X; static const float INIT_POS_Y; static const float INIT_POS_Z; static const float INIT_AXIS_X; static const float INIT_AXIS_Y; static const float INIT_AXIS_Z; static const float INIT_ANGLE; static const float INIT_SCALE; static const float SCALE_MAX; static const float SCALE_MIN; static const float ANIMATE_THRESHOLD; public: View(); virtual ~View(); public: void frustum(int w, int h); void xform(); void reset(); void set_pos(float x, float y, float z) { m_Pos[0] = x; m_Pos[1] = y; m_Pos[2] = z; } void set_quat(float q0, float q1, float q2, float q3) { m_Quat[0] = q0; m_Quat[1] = q1; m_Quat[2] = q2; m_Quat[3] = q3; } void set_scale(float scale) { m_Scale = scale; } void enable_animation(); void disable_animation(); bool is_animate() const { return m_Animate; } protected: // Signal handlers: virtual bool on_button_press_event(GdkEventButton* event, Scene* scene); virtual bool on_button_release_event(GdkEventButton* event, Scene* scene); virtual bool on_motion_notify_event(GdkEventMotion* event, Scene* scene); private: float m_Pos[3]; float m_Quat[4]; float m_Scale; float m_QuatDiff[4]; float m_BeginX; float m_BeginY; float m_DX; float m_DY; bool m_Animate; }; // // Model class. // class Model { friend class Scene; public: static const unsigned int NUM_SHAPES; enum ShapeType { CUBE, SPHERE, CONE, TORUS, TETRAHEDRON, OCTAHEDRON, DODECAHEDRON, ICOSAHEDRON, TEAPOT, }; static const ShapeType SHAPE_CUBE; static const ShapeType SHAPE_SPHERE; static const ShapeType SHAPE_CONE; static const ShapeType SHAPE_TORUS; static const ShapeType SHAPE_TETRAHEDRON; static const ShapeType SHAPE_OCTAHEDRON; static const ShapeType SHAPE_DODECAHEDRON; static const ShapeType SHAPE_ICOSAHEDRON; static const ShapeType SHAPE_TEAPOT; public: struct MaterialProp { GLfloat ambient[4]; GLfloat diffuse[4]; GLfloat specular[4]; GLfloat shininess; }; static const MaterialProp MAT_EMERALD; static const MaterialProp MAT_JADE; static const MaterialProp MAT_OBSIDIAN; static const MaterialProp MAT_PEARL; static const MaterialProp MAT_RUBY; static const MaterialProp MAT_TURQUOISE; static const MaterialProp MAT_BRASS; static const MaterialProp MAT_BRONZE; static const MaterialProp MAT_CHROME; static const MaterialProp MAT_COPPER; static const MaterialProp MAT_GOLD; static const MaterialProp MAT_SILVER; public: Model(); virtual ~Model(); private: void init_gl(Glib::RefPtr<Gdk::GL::Drawable>& gldrawable); public: void draw(Glib::RefPtr<Gdk::GL::Drawable>& gldrawable); void set_shape(ShapeType shape) { m_CurrentShape = shape; } void set_material(const MaterialProp* material) { m_CurrentMat = material; } private: unsigned int m_ListBase; ShapeType m_CurrentShape; const MaterialProp* m_CurrentMat; }; // // Scene class. // class Scene : public Gtk::GL::DrawingArea { friend class View; friend class Model; public: // OpenGL scene related constants: static const float CLEAR_COLOR[4]; static const float CLEAR_DEPTH; static const float LIGHT0_POSITION[4]; static const float LIGHT0_AMBIENT[4]; static const float LIGHT0_DIFFUSE[4]; static const float LIGHT_MODEL_AMBIENT[4]; static const float LIGHT_MODEL_LOCAL_VIEWER[1]; public: explicit Scene(); virtual ~Scene(); protected: // signal handlers: virtual void on_realize(); virtual bool on_configure_event(GdkEventConfigure* event); virtual bool on_expose_event(GdkEventExpose* event); virtual bool on_button_press_event(GdkEventButton* event); virtual bool on_unmap_event(GdkEventAny* event); virtual bool on_visibility_notify_event(GdkEventVisibility* event); virtual bool on_idle(); 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: // idle signal connection: sigc::connection m_ConnectionIdle; void idle_add(); void idle_remove(); protected: void change_shape(Model::ShapeType shape); void change_material(const Model::MaterialProp* material); protected: Gtk::Menu* create_popup_menu(); protected: // Popup menu: Gtk::Menu* m_Menu; protected: // OpenGL scene related objects: View m_View; Model m_Model; }; // // Application class. // class Application : public Gtk::Window { public: static const Glib::ustring APP_NAME; public: Application(); virtual ~Application(); protected: // signal handlers: virtual void on_button_quit_clicked(); virtual bool on_key_press_event(GdkEventKey* event); protected: // member widgets: Gtk::VBox m_VBox; Scene m_Scene; Gtk::Button m_ButtonQuit; }; } // namespace Shapes #endif // _SHAPES_H
00001 // -*- C++ -*- 00002 /* 00003 * shapes.h: 00004 * shapes demo. 00005 * 00006 * written by Naofumi Yasufuku <naofumi@users.sourceforge.net> 00007 */ 00008 00009 #ifndef _SHAPES_H 00010 #define _SHAPES_H 00011 00012 #include <gtkmm.h> 00013 00014 #include <gtkglmm.h> 00015 00016 00018 // 00019 // Shapes classes. 00020 // 00022 00023 namespace Shapes 00024 { 00025 00026 class Scene; 00027 00028 // 00029 // View class. 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 // Signal handlers: 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 // Model class. 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 // Scene class. 00188 // 00189 00190 class Scene : public Gtk::GL::DrawingArea 00191 { 00192 friend class View; 00193 friend class Model; 00194 00195 public: 00196 // OpenGL scene related constants: 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 // signal handlers: 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 // Invalidate whole window. 00223 void invalidate() { 00224 get_window()->invalidate_rect(get_allocation(), false); 00225 } 00226 00227 // Update window synchronously (fast). 00228 void update() 00229 { get_window()->process_updates(false); } 00230 00231 protected: 00232 // idle signal connection: 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 // Popup menu: 00247 Gtk::Menu* m_Menu; 00248 00249 protected: 00250 // OpenGL scene related objects: 00251 View m_View; 00252 Model m_Model; 00253 00254 }; 00255 00256 00257 // 00258 // Application class. 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 // signal handlers: 00272 virtual void on_button_quit_clicked(); 00273 virtual bool on_key_press_event(GdkEventKey* event); 00274 00275 protected: 00276 // member widgets: 00277 Gtk::VBox m_VBox; 00278 Scene m_Scene; 00279 Gtk::Button m_ButtonQuit; 00280 }; 00281 00282 00283 } // namespace Shapes 00284 00285 00286 #endif // _SHAPES_H

Generated on Sun Jun 20 16:59:42 2004 for gtkglextmm by doxygen 1.3.7