main.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #ifndef EXAMPLE22_MATERIAL_VIEWER_MAIN_H
  2. #define EXAMPLE22_MATERIAL_VIEWER_MAIN_H
  3. #include <irrlicht.h>
  4. // Helper control to allow setting colors
  5. class CColorControl : public irr::gui::IGUIElement
  6. {
  7. public:
  8. CColorControl(irr::gui::IGUIEnvironment* guiEnv, const irr::core::position2d<irr::s32> & pos, const wchar_t *text, irr::gui::IGUIElement* parent, irr::s32 id=-1);
  9. // Event receiver
  10. virtual bool OnEvent(const irr::SEvent &event);
  11. // Set the color values
  12. void setColor(const irr::video::SColor& col);
  13. // Get the color values
  14. const irr::video::SColor& getColor() const
  15. {
  16. return Color;
  17. }
  18. // To reset the dirty flag
  19. void resetDirty()
  20. {
  21. DirtyFlag = false;
  22. }
  23. // when the color was changed the dirty flag is set
  24. bool isDirty() const
  25. {
  26. return DirtyFlag;
  27. };
  28. protected:
  29. // Add a staticbox for a description + an editbox so users can enter numbers
  30. irr::gui::IGUIEditBox* addEditForNumbers(irr::gui::IGUIEnvironment* guiEnv, const irr::core::position2d<irr::s32> & pos, const wchar_t *text, irr::s32 id, irr::gui::IGUIElement * parent);
  31. // Get the color value from the editfields
  32. irr::video::SColor getColorFromEdits() const;
  33. // Fill the editfields with the value for the given color
  34. void setEditsFromColor(irr::video::SColor col);
  35. private:
  36. bool DirtyFlag;
  37. irr::video::SColor Color;
  38. irr::s32 ButtonSetId;
  39. irr::gui::IGUIStaticText * ColorStatic;
  40. irr::gui::IGUIEditBox * EditAlpha;
  41. irr::gui::IGUIEditBox * EditRed;
  42. irr::gui::IGUIEditBox * EditGreen;
  43. irr::gui::IGUIEditBox * EditBlue;
  44. };
  45. /*
  46. Custom GUI-control for to edit all colors typically used in materials and lights
  47. */
  48. class CTypicalColorsControl : public irr::gui::IGUIElement
  49. {
  50. public:
  51. // Constructor
  52. CTypicalColorsControl(irr::gui::IGUIEnvironment* guiEnv, const irr::core::position2d<irr::s32> & pos, bool hasEmissive, irr::gui::IGUIElement* parent, irr::s32 id=-1);
  53. // Destructor
  54. virtual ~CTypicalColorsControl();
  55. // Set the color values to those within the material
  56. void setColorsToMaterialColors(const irr::video::SMaterial & material);
  57. // Update all changed colors in the material
  58. void updateMaterialColors(irr::video::SMaterial & material) const;
  59. // Set the color values to those from the light data
  60. void setColorsToLightDataColors(const irr::video::SLight & lightData);
  61. // Update all changed colors in the light data
  62. void updateLightColors(irr::video::SLight & lightData) const;
  63. // To reset the dirty flags
  64. void resetDirty();
  65. private:
  66. CColorControl* ControlAmbientColor;
  67. CColorControl* ControlDiffuseColor;
  68. CColorControl* ControlSpecularColor;
  69. CColorControl* ControlEmissiveColor;
  70. };
  71. /*
  72. GUI-Control to offer a selection of available textures.
  73. */
  74. class CTextureControl : public irr::gui::IGUIElement
  75. {
  76. public:
  77. CTextureControl(irr::gui::IGUIEnvironment* guiEnv, irr::video::IVideoDriver * driver, const irr::core::position2d<irr::s32> & pos, irr::gui::IGUIElement* parent, irr::s32 id=-1);
  78. virtual bool OnEvent(const irr::SEvent &event);
  79. // Workaround for a problem with comboboxes.
  80. // We have to get in front when the combobox wants to get in front or combobox-list might be drawn below other elements.
  81. virtual bool bringToFront(irr::gui::IGUIElement* element);
  82. // Return selected texturename (if any, otherwise 0)
  83. const wchar_t * getSelectedTextureName() const;
  84. // Change active selectionbased on the texture name
  85. void selectTextureByName(const irr::core::stringw& name);
  86. // Reset the dirty flag
  87. void resetDirty()
  88. {
  89. DirtyFlag = false;
  90. }
  91. // When the texture was changed the dirty flag is set
  92. bool isDirty() const
  93. {
  94. return DirtyFlag;
  95. };
  96. // Put the names of all currently loaded textures in a combobox
  97. void updateTextures(irr::video::IVideoDriver * driver);
  98. private:
  99. bool DirtyFlag;
  100. irr::gui::IGUIComboBox * ComboTexture;
  101. };
  102. /*
  103. Control which allows setting some of the material values for a meshscenenode
  104. */
  105. class CMaterialControl
  106. {
  107. public:
  108. // constructor
  109. CMaterialControl()
  110. : Initialized(false), Driver(0)
  111. , TypicalColorsControl(0), ButtonLighting(0), InfoLighting(0), ComboMaterial(0)
  112. {
  113. for (irr::u32 i=0; i<irr::video::MATERIAL_MAX_TEXTURES; ++i)
  114. TextureControls[i] = 0;
  115. }
  116. // Destructor
  117. ~CMaterialControl()
  118. {
  119. for (irr::u32 i=0; i<irr::video::MATERIAL_MAX_TEXTURES; ++i)
  120. {
  121. if (TextureControls[i] )
  122. TextureControls[i]->drop();
  123. }
  124. if ( TypicalColorsControl )
  125. TypicalColorsControl->drop();
  126. }
  127. void init(irr::scene::IMeshSceneNode* node, irr::IrrlichtDevice * device, const irr::core::position2d<irr::s32> & pos, const wchar_t * description);
  128. void update(irr::scene::IMeshSceneNode* sceneNode, irr::scene::IMeshSceneNode* sceneNode2T, irr::scene::IMeshSceneNode* sceneNodeTangents);
  129. void updateTextures();
  130. void selectTextures(const irr::core::stringw& name);
  131. bool isLightingEnabled() const;
  132. protected:
  133. void updateMaterial(irr::video::SMaterial & material);
  134. bool Initialized;
  135. irr::video::IVideoDriver * Driver;
  136. CTypicalColorsControl* TypicalColorsControl;
  137. irr::gui::IGUIButton * ButtonLighting;
  138. irr::gui::IGUIStaticText* InfoLighting;
  139. irr::gui::IGUIComboBox * ComboMaterial;
  140. CTextureControl* TextureControls[irr::video::MATERIAL_MAX_TEXTURES];
  141. };
  142. /*
  143. Control to allow setting the color values of a lightscenenode.
  144. */
  145. class CLightNodeControl
  146. {
  147. public:
  148. // constructor
  149. CLightNodeControl() : Initialized(false), TypicalColorsControl(0)
  150. {}
  151. ~CLightNodeControl()
  152. {
  153. if ( TypicalColorsControl )
  154. TypicalColorsControl->drop();
  155. }
  156. void init(irr::scene::ILightSceneNode* node, irr::gui::IGUIEnvironment* guiEnv, const irr::core::position2d<irr::s32> & pos, const wchar_t * description);
  157. void update(irr::scene::ILightSceneNode* node);
  158. protected:
  159. bool Initialized;
  160. CTypicalColorsControl* TypicalColorsControl;
  161. };
  162. /*
  163. Application configuration
  164. */
  165. struct SConfig
  166. {
  167. SConfig()
  168. : RenderInBackground(true)
  169. , DriverType(irr::video::EDT_NULL)
  170. , ScreenSize(640, 480)
  171. {
  172. }
  173. bool RenderInBackground;
  174. irr::video::E_DRIVER_TYPE DriverType;
  175. irr::core::dimension2d<irr::u32> ScreenSize;
  176. };
  177. /*
  178. Main application class
  179. */
  180. class CApp : public irr::IEventReceiver
  181. {
  182. friend int main(int argc, char *argv[]);
  183. public:
  184. // constructor
  185. CApp()
  186. : IsRunning(false)
  187. , RealTimeTick(0)
  188. , Device(0)
  189. , MeshManipulator(0)
  190. , Camera(0)
  191. , SceneNode(0), SceneNode2T(0), SceneNodeTangents(0), NodeLight(0)
  192. , CameraRotationAxis(irr::core::vector3df(1,0,0))
  193. , LightRotationAxis(irr::core::vector3df(1,0,0))
  194. , MeshMaterialControl(0)
  195. , LightControl(0)
  196. , ControlVertexColors(0)
  197. , GlobalAmbient(0)
  198. , MousePressed(false)
  199. {
  200. memset(KeysPressed, 0, sizeof KeysPressed);
  201. }
  202. // destructor
  203. ~CApp()
  204. {
  205. }
  206. // Tell it to stop running
  207. void setRunning(bool appRuns)
  208. {
  209. IsRunning = appRuns;
  210. }
  211. // Check if it should continue running
  212. bool isRunning() const
  213. {
  214. return IsRunning;
  215. }
  216. // Event handler
  217. virtual bool OnEvent(const irr::SEvent &event);
  218. protected:
  219. // Application initialization
  220. // returns true when it was successful initialized, otherwise false.
  221. bool init(int argc, char *argv[]);
  222. // Update one frame
  223. bool update();
  224. // Close down the application
  225. void quit();
  226. // Create some useful textures.
  227. void createDefaultTextures(irr::video::IVideoDriver * driver);
  228. // Load a texture and make sure nodes know it when more textures are available.
  229. void loadTexture(const irr::io::path &name);
  230. // Rotate a node around the origin (0,0,0)
  231. void RotateHorizontal(irr::scene::ISceneNode* node, irr::f32 angle);
  232. void RotateAroundAxis(irr::scene::ISceneNode* node, irr::f32 angle, const irr::core::vector3df& axis);
  233. void ZoomOut(irr::scene::ISceneNode* node, irr::f32 units);
  234. void UpdateRotationAxis(irr::scene::ISceneNode* node, irr::core::vector3df& axis);
  235. private:
  236. SConfig Config;
  237. bool IsRunning;
  238. irr::u32 RealTimeTick;
  239. irr::IrrlichtDevice * Device;
  240. irr::scene::IMeshManipulator* MeshManipulator;
  241. irr::scene::ICameraSceneNode * Camera;
  242. irr::scene::IMeshSceneNode* SceneNode;
  243. irr::scene::IMeshSceneNode* SceneNode2T;
  244. irr::scene::IMeshSceneNode* SceneNodeTangents;
  245. irr::scene::ILightSceneNode* NodeLight;
  246. irr::core::vector3df CameraRotationAxis;
  247. irr::core::vector3df LightRotationAxis;
  248. CMaterialControl* MeshMaterialControl;
  249. CLightNodeControl* LightControl;
  250. CColorControl* ControlVertexColors;
  251. CColorControl* GlobalAmbient;
  252. bool KeysPressed[irr::KEY_KEY_CODES_COUNT];
  253. bool MousePressed;
  254. irr::core::position2d<irr::s32> MouseStart;
  255. };
  256. #endif