main.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. // Set dirty flag (node will update texture)
  87. void setDirty()
  88. {
  89. DirtyFlag = true;
  90. }
  91. // Reset the dirty flag
  92. void resetDirty()
  93. {
  94. DirtyFlag = false;
  95. }
  96. // When the texture was changed the dirty flag is set
  97. bool isDirty() const
  98. {
  99. return DirtyFlag;
  100. };
  101. // Put the names of all currently loaded textures in a combobox
  102. void updateTextures(irr::video::IVideoDriver * driver);
  103. private:
  104. bool DirtyFlag;
  105. irr::gui::IGUIComboBox * ComboTexture;
  106. };
  107. /*
  108. Control which allows setting some of the material values for a meshscenenode
  109. */
  110. class CMaterialControl
  111. {
  112. public:
  113. // constructor
  114. CMaterialControl()
  115. : Initialized(false), Driver(0)
  116. , TypicalColorsControl(0), ButtonLighting(0), InfoLighting(0), ComboMaterial(0)
  117. , ShininessControl(0)
  118. {}
  119. // Destructor
  120. ~CMaterialControl()
  121. {
  122. for (irr::u32 i=0; i<TextureControls.size(); ++i)
  123. {
  124. if (TextureControls[i] )
  125. TextureControls[i]->drop();
  126. }
  127. if ( TypicalColorsControl )
  128. TypicalColorsControl->drop();
  129. }
  130. void init(irr::IrrlichtDevice * device, const irr::core::position2d<irr::s32> & pos, const wchar_t * description);
  131. void setMaterial(const irr::video::SMaterial & material);
  132. void update(irr::scene::IMeshSceneNode* sceneNode, irr::scene::IMeshSceneNode* sceneNode2T, irr::scene::IMeshSceneNode* sceneNodeTangents);
  133. void updateTextures();
  134. void selectTextures(const irr::core::stringw& name);
  135. bool isLightingEnabled() const;
  136. protected:
  137. void updateMaterial(irr::video::SMaterial & material);
  138. bool Initialized;
  139. irr::video::IVideoDriver * Driver;
  140. CTypicalColorsControl* TypicalColorsControl;
  141. irr::gui::IGUIButton * ButtonLighting;
  142. irr::gui::IGUIStaticText* InfoLighting;
  143. irr::gui::IGUIComboBox * ComboMaterial;
  144. irr::core::array<CTextureControl*> TextureControls;
  145. irr::gui::IGUIScrollBar* ShininessControl;
  146. };
  147. /*
  148. Control to allow setting the color values of a lightscenenode.
  149. */
  150. class CLightNodeControl
  151. {
  152. public:
  153. // constructor
  154. CLightNodeControl() : Initialized(false), TypicalColorsControl(0)
  155. {}
  156. ~CLightNodeControl()
  157. {
  158. if ( TypicalColorsControl )
  159. TypicalColorsControl->drop();
  160. }
  161. void init(irr::scene::ILightSceneNode* node, irr::gui::IGUIEnvironment* guiEnv, const irr::core::position2d<irr::s32> & pos, const wchar_t * description);
  162. void update(irr::scene::ILightSceneNode* node);
  163. protected:
  164. bool Initialized;
  165. CTypicalColorsControl* TypicalColorsControl;
  166. };
  167. /*
  168. Application configuration
  169. */
  170. struct SConfig
  171. {
  172. SConfig()
  173. : RenderInBackground(true)
  174. , DriverType(irr::video::EDT_NULL)
  175. , ScreenSize(640, 480)
  176. {
  177. }
  178. bool RenderInBackground;
  179. irr::video::E_DRIVER_TYPE DriverType;
  180. irr::core::dimension2d<irr::u32> ScreenSize;
  181. };
  182. /*
  183. Main application class
  184. */
  185. class CApp : public irr::IEventReceiver
  186. {
  187. friend int main(int argc, char *argv[]);
  188. public:
  189. // constructor
  190. CApp()
  191. : IsRunning(false)
  192. , RealTimeTick(0)
  193. , Device(0)
  194. , MeshManipulator(0)
  195. , Camera(0)
  196. , SceneNode(0), SceneNode2T(0), SceneNodeTangents(0), NodeLight(0)
  197. , CameraRotationAxis(irr::core::vector3df(1,0,0))
  198. , LightRotationAxis(irr::core::vector3df(1,0,0))
  199. , MeshMaterialControl(0)
  200. , LightControl(0)
  201. , ComboMeshType(0)
  202. , ControlVertexColors(0)
  203. , GlobalAmbient(0)
  204. , MousePressed(false)
  205. {
  206. memset(KeysPressed, 0, sizeof KeysPressed);
  207. }
  208. // destructor
  209. ~CApp()
  210. {
  211. }
  212. // Tell it to stop running
  213. void setRunning(bool appRuns)
  214. {
  215. IsRunning = appRuns;
  216. }
  217. // Check if it should continue running
  218. bool isRunning() const
  219. {
  220. return IsRunning;
  221. }
  222. // Event handler
  223. virtual bool OnEvent(const irr::SEvent &event);
  224. protected:
  225. // Application initialization
  226. // returns true when it was successful initialized, otherwise false.
  227. bool init(int argc, char *argv[]);
  228. // Update one frame
  229. bool update();
  230. // Close down the application
  231. void quit();
  232. // Create some useful textures.
  233. void createDefaultTextures(irr::video::IVideoDriver * driver);
  234. // Load a texture and make sure nodes know it when more textures are available.
  235. void loadTexture(const irr::io::path &name);
  236. // Rotate a node around the origin (0,0,0)
  237. void RotateHorizontal(irr::scene::ISceneNode* node, irr::f32 angle);
  238. void RotateAroundAxis(irr::scene::ISceneNode* node, irr::f32 angle, const irr::core::vector3df& axis);
  239. void ZoomOut(irr::scene::ISceneNode* node, irr::f32 units);
  240. void UpdateRotationAxis(irr::scene::ISceneNode* node, irr::core::vector3df& axis);
  241. enum ENodeType
  242. {
  243. ENT_CUBE,
  244. ENT_SPHERE,
  245. ENT_SPHERE_HIGHRES,
  246. };
  247. void setActiveMeshNodeType(ENodeType nodeType);
  248. irr::scene::IMeshSceneNode* getVisibleMeshNode() const;
  249. private:
  250. SConfig Config;
  251. bool IsRunning;
  252. irr::u32 RealTimeTick;
  253. irr::IrrlichtDevice * Device;
  254. irr::scene::IMeshManipulator* MeshManipulator;
  255. irr::scene::ICameraSceneNode * Camera;
  256. irr::scene::IMeshSceneNode* SceneNode;
  257. irr::scene::IMeshSceneNode* SceneNode2T;
  258. irr::scene::IMeshSceneNode* SceneNodeTangents;
  259. irr::scene::ILightSceneNode* NodeLight;
  260. irr::core::vector3df CameraRotationAxis;
  261. irr::core::vector3df LightRotationAxis;
  262. CMaterialControl* MeshMaterialControl;
  263. CLightNodeControl* LightControl;
  264. irr::gui::IGUIComboBox* ComboMeshType;
  265. CColorControl* ControlVertexColors;
  266. CColorControl* GlobalAmbient;
  267. bool KeysPressed[irr::KEY_KEY_CODES_COUNT];
  268. bool MousePressed;
  269. irr::core::position2d<irr::s32> MouseStart;
  270. };
  271. #endif