Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Render.h 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*!
  2. * \file include/Render.h
  3. * \brief OpenRaider Renderer class
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #ifndef _RENDER_H_
  9. #define _RENDER_H_
  10. #include <vector>
  11. #include "Config.h"
  12. #include "math/Matrix.h"
  13. #include "ViewVolume.h"
  14. #include "World.h"
  15. #include "SkeletalModel.h"
  16. #include "Mesh.h"
  17. #include "Texture.h"
  18. #include "Camera.h"
  19. #ifdef USING_EMITTER
  20. #include "Emitter.h"
  21. #endif
  22. /*!
  23. * \brief The GL light class
  24. */
  25. class Light {
  26. public:
  27. /*!
  28. * \brief Type a light can be of
  29. */
  30. typedef enum {
  31. typePoint = 1, //!< Point light
  32. typeSpot = 2, //!< Spot light
  33. typeDirectional = 3 //!< Directional light
  34. } LightType;
  35. vec4_t mPos; //! Light position in 3 space
  36. vec3_t mDir; //! Light direction
  37. float mAtt;
  38. vec4_t mColor; //! Color of light
  39. vec_t mCutoff; //! Fade out distance
  40. LightType mType; //! Type of light
  41. };
  42. /*!
  43. * \brief RenderRoom used by Renderer
  44. */
  45. class RenderRoom {
  46. public:
  47. /*!
  48. * \brief Constructs an object of RenderRoom
  49. */
  50. RenderRoom() {
  51. room = 0x0;
  52. dist = 0.0f;
  53. }
  54. /*!
  55. * \brief Deconstructs an object of RenderRoom
  56. */
  57. ~RenderRoom() {
  58. // \fixme Hangs when erasing - might be shared pointers somewhere
  59. for (std::vector<Light *>::size_type i = 0; i != lights.size(); i++) {
  60. if (lights[i])
  61. delete lights[i];
  62. }
  63. lights.clear();
  64. }
  65. vec_t dist; //!< Distance to near plane, move to room?
  66. std::vector<Light *> lights; //!< List of lights in this room
  67. Mesh mesh; //!< OpenGL mesh that represents this room
  68. //! \fixme very dangerous as allocated and used
  69. room_mesh_t *room; //!< Physical room stored in World
  70. };
  71. /*!
  72. * \brief OpenRaider Renderer class
  73. */
  74. class Render {
  75. public:
  76. typedef enum {
  77. modeDisabled,
  78. modeVertexLight,
  79. modeSolid,
  80. modeWireframe,
  81. modeTexture
  82. } RenderMode;
  83. typedef enum {
  84. fParticles = (1 << 0),
  85. fPortals = (1 << 1),
  86. fRoomAlpha = (1 << 2),
  87. fViewModel = (1 << 3),
  88. fSprites = (1 << 4),
  89. fRoomModels = (1 << 5),
  90. fEntityModels = (1 << 6),
  91. fFog = (1 << 7),
  92. fUsePortals = (1 << 8),
  93. fGL_Lights = (1 << 9),
  94. fOneRoom = (1 << 10),
  95. fRenderPonytail = (1 << 11),
  96. fMultiTexture = (1 << 12),
  97. fUpdateRoomListPerFrame = (1 << 13),
  98. fAnimateAllModels = (1 << 14),
  99. fAllRooms = (1 << 15)
  100. } RenderFlags;
  101. typedef enum {
  102. roomMesh,
  103. skeletalMesh
  104. } RenderMeshType;
  105. /*!
  106. * \brief Constructs an object of Render
  107. */
  108. Render();
  109. /*!
  110. * \brief Deconstructs an object of Render
  111. */
  112. ~Render();
  113. /*!
  114. * \brief Makes a screenshot, writes to disk
  115. * \param filenameBase basename of file to be written
  116. */
  117. void screenShot(char *filenameBase);
  118. /*!
  119. * \brief Gets current rendering mode
  120. * \returns current RenderMode
  121. * \fixme Don't return enum as int?!
  122. */
  123. int getMode();
  124. void addRoom(RenderRoom *rRoom);
  125. /*!
  126. * \brief Loads textures in a certain id slot
  127. * \param image Image to load
  128. * \param width width of image
  129. * \param height height of image
  130. * \param id id for texture
  131. * \sa Texture::loadBufferSlot()
  132. */
  133. void loadTexture(unsigned char *image,
  134. unsigned int width, unsigned int height,
  135. unsigned int id);
  136. /*!
  137. * \brief Sets up textures for OpenRaider.
  138. * \param textureDir Is valid and exists with textures
  139. * \param numLoaded returns number of loaded textures and will update
  140. * number of external textures loaded
  141. * \param nextId will update next level texture id
  142. */
  143. void initTextures(char *textureDir, unsigned int *numLoaded,
  144. unsigned int *nextId);
  145. /*!
  146. * \brief Initializes Emitter.
  147. *
  148. * Emitter is set up for rendering with 2 textures in
  149. * a overhead rain or snow like pattern.
  150. * Textures have to be initialized!
  151. * \param name valid C-String name
  152. * \param size valid size
  153. * \param snowTex1 valid first texture
  154. * \param snowTex2 valid second texture
  155. */
  156. void initEmitter(const char *name, unsigned int size,
  157. unsigned int snowTex1, unsigned int snowTex2);
  158. /*!
  159. * Removes current world/entity/etc geometry
  160. */
  161. void ClearWorld();
  162. /*!
  163. * \brief Clears bitflags, changes state of renderer in some way
  164. * \param flags RenderFlags to clear (ORed)
  165. * \fixme use enum not integer as parameter?!
  166. */
  167. void clearFlags(unsigned int flags);
  168. /*!
  169. * \brief Sets bitflags, changes state of renderer in some way
  170. * \param flags RenderFlags to set (ORed)
  171. * \fixme use enum not integer as parameter?!
  172. */
  173. void setFlags(unsigned int flags);
  174. void setMode(int n);
  175. /*!
  176. * \brief Renders a single game frame
  177. */
  178. void Display();
  179. void setSkyMesh(int index, bool rot);
  180. void ViewModel(entity_t *ent, int index);
  181. void addSkeletalModel(SkeletalModel *mdl);
  182. private:
  183. /*!
  184. * \brief Check if a bounding box is in the View Volume
  185. * \param bboxMin Start coordinates of a valid bounding box
  186. * \param bboxMax End coordinates of a valid bounding box
  187. * \returns true if bounding box is visible
  188. */
  189. bool isVisible(float bboxMin[3], float bboxMax[3]);
  190. /*!
  191. * \brief Check if a point is in the View Volume
  192. * \param x X coordinate
  193. * \param y Y coordinate
  194. * \param z Z coordinate
  195. * \returns true if point is visible
  196. */
  197. bool isVisible(float x, float y, float z);
  198. /*!
  199. * \brief Check if a sphere is in the View Volume
  200. * \param x X coordinate of center of sphere
  201. * \param y Y coordinate of center of sphere
  202. * \param z Z coordinate of center of sphere
  203. * \param radius radius of sphere
  204. * \returns true if sphere is visible
  205. */
  206. bool isVisible(float x, float y, float z, float radius);
  207. /*!
  208. * \brief Build a visible room list starting at index
  209. * \param index valid room index where to start the list
  210. */
  211. void newRoomRenderList(int index);
  212. /*!
  213. * \brief Build a visible room list starting from room and
  214. * only considers its linked rooms and their linked rooms.
  215. * \param room First room in list
  216. */
  217. void buildRoomRenderList(RenderRoom *room);
  218. /*!
  219. * \brief Renders visible world object.
  220. *
  221. * Texture must be initialized.
  222. */
  223. void drawObjects();
  224. /*!
  225. * \brief Renders Sky domes/boxes/etc by scaling factor.
  226. *
  227. * Texture must be initialized.
  228. * \param scale correct scale for map size
  229. */
  230. void drawSkyMesh(float scale);
  231. /*!
  232. * \brief Renders a skeletal model.
  233. *
  234. * Texture must be initialized!
  235. * \param model model to render
  236. */
  237. void drawModel(SkeletalModel *model);
  238. /*!
  239. * Renders a room in 2 seperate passes to handle alpha.
  240. *
  241. * Currently doesnt sort alpha but looks pretty good.
  242. * Texture must be initialized.
  243. * Draw all rooms with alpha false, then again with alpha true.
  244. * \param rRoom room to render
  245. * \param draw_alpha once false, once true
  246. */
  247. void drawRoom(RenderRoom *rRoom, bool draw_alpha);
  248. /*!
  249. * \brief Renders static room model.
  250. *
  251. * Texture must be initialized.
  252. * \param mesh Static model to render
  253. */
  254. void drawRoomModel(static_model_t *mesh);
  255. /*!
  256. * \brief Renders a mesh.
  257. *
  258. * Texture must be initialized.
  259. * \param r_mesh Mesh to render.
  260. * \param type Must be object containing mesh
  261. */
  262. void drawModelMesh(model_mesh_t *r_mesh, RenderMeshType type);
  263. /*!
  264. * \brief Renders a sprite.
  265. *
  266. * Texture must be initialized.
  267. * \param sprite sprite to render
  268. */
  269. void drawSprite(sprite_t *sprite);
  270. /*!
  271. * \brief Updates View Volume. Call once per render frame.
  272. */
  273. void updateViewVolume();
  274. //! \fixme Let them eat cake...? O.o
  275. void tmpRenderModelMesh(model_mesh_t *r_mesh, texture_tri_t *ttri);
  276. Texture mTexture; //!< Texture subsystem
  277. std::vector<RenderRoom *> mRoomRenderList;
  278. std::vector<RenderRoom *> mRooms;
  279. std::vector<SkeletalModel *> mModels;
  280. unsigned int mFlags; //!< Rendering flags
  281. unsigned int mMode; //!< Rendering mode
  282. unsigned int *mNumTexturesLoaded;
  283. unsigned int *mNextTextureId;
  284. int mLock;
  285. int mSkyMesh; //!< Skymesh model id
  286. bool mSkyMeshRotation; //!< Should Skymesh be rotated?
  287. #ifdef USING_EMITTER
  288. Emitter *mEmitter; //!< Particle emitter test
  289. #endif
  290. };
  291. #endif