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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. modeLoadScreen,
  79. modeVertexLight,
  80. modeSolid,
  81. modeWireframe,
  82. modeTexture
  83. } RenderMode;
  84. typedef enum {
  85. fParticles = (1 << 0),
  86. fPortals = (1 << 1),
  87. fRoomAlpha = (1 << 2),
  88. fViewModel = (1 << 3),
  89. fSprites = (1 << 4),
  90. fRoomModels = (1 << 5),
  91. fEntityModels = (1 << 6),
  92. fFog = (1 << 7),
  93. fUsePortals = (1 << 8),
  94. fGL_Lights = (1 << 9),
  95. fOneRoom = (1 << 10),
  96. fRenderPonytail = (1 << 11),
  97. fMultiTexture = (1 << 12),
  98. fUpdateRoomListPerFrame = (1 << 13),
  99. fAnimateAllModels = (1 << 14),
  100. fAllRooms = (1 << 15)
  101. } RenderFlags;
  102. typedef enum {
  103. roomMesh,
  104. skeletalMesh
  105. } RenderMeshType;
  106. /*!
  107. * \brief Constructs an object of Render
  108. */
  109. Render();
  110. /*!
  111. * \brief Deconstructs an object of Render
  112. */
  113. ~Render();
  114. /*!
  115. * \brief Makes a screenshot, writes to disk
  116. * \param filenameBase basename of file to be written
  117. */
  118. void screenShot(char *filenameBase);
  119. /*!
  120. * \brief Gets current rendering mode
  121. * \returns current RenderMode
  122. * \fixme Don't return enum as int?!
  123. */
  124. int getMode();
  125. void addRoom(RenderRoom *rRoom);
  126. /*!
  127. * \brief Loads textures in a certain id slot
  128. * \param image Image to load
  129. * \param width width of image
  130. * \param height height of image
  131. * \param id id for texture
  132. * \sa Texture::loadBufferSlot()
  133. */
  134. void loadTexture(unsigned char *image,
  135. unsigned int width, unsigned int height,
  136. unsigned int id);
  137. /*!
  138. * \brief Sets up textures for OpenRaider.
  139. * \param textureDir Is valid and exists with textures
  140. * \param numLoaded returns number of loaded textures and will update
  141. * number of external textures loaded
  142. * \param nextId will update next level texture id
  143. */
  144. void initTextures(char *textureDir, unsigned int *numLoaded,
  145. unsigned int *nextId);
  146. /*!
  147. * \brief Initializes Emitter.
  148. *
  149. * Emitter is set up for rendering with 2 textures in
  150. * a overhead rain or snow like pattern.
  151. * Textures have to be initialized!
  152. * \param name valid C-String name
  153. * \param size valid size
  154. * \param snowTex1 valid first texture
  155. * \param snowTex2 valid second texture
  156. */
  157. void initEmitter(const char *name, unsigned int size,
  158. unsigned int snowTex1, unsigned int snowTex2);
  159. /*!
  160. * Removes current world/entity/etc geometry
  161. */
  162. void ClearWorld();
  163. /*!
  164. * \brief Clears bitflags, changes state of renderer in some way
  165. * \param flags RenderFlags to clear (ORed)
  166. * \fixme use enum not integer as parameter?!
  167. */
  168. void clearFlags(unsigned int flags);
  169. /*!
  170. * \brief Sets bitflags, changes state of renderer in some way
  171. * \param flags RenderFlags to set (ORed)
  172. * \fixme use enum not integer as parameter?!
  173. */
  174. void setFlags(unsigned int flags);
  175. void setMode(int n);
  176. /*!
  177. * \brief Renders a single game frame
  178. */
  179. void Display();
  180. void setSkyMesh(int index, bool rot);
  181. void ViewModel(entity_t *ent, int index);
  182. void addSkeletalModel(SkeletalModel *mdl);
  183. unsigned int getFlags();
  184. //! \fixme should be private
  185. ViewVolume mViewVolume; //!< View Volume for frustum culling
  186. std::vector<SkeletalModel *> mModels;
  187. private:
  188. void drawLoadScreen();
  189. /*!
  190. * \brief Check if a bounding box is in the View Volume
  191. * \param bboxMin Start coordinates of a valid bounding box
  192. * \param bboxMax End coordinates of a valid bounding box
  193. * \returns true if bounding box is visible
  194. */
  195. bool isVisible(float bboxMin[3], float bboxMax[3]);
  196. /*!
  197. * \brief Check if a point is in the View Volume
  198. * \param x X coordinate
  199. * \param y Y coordinate
  200. * \param z Z coordinate
  201. * \returns true if point is visible
  202. */
  203. bool isVisible(float x, float y, float z);
  204. /*!
  205. * \brief Check if a sphere is in the View Volume
  206. * \param x X coordinate of center of sphere
  207. * \param y Y coordinate of center of sphere
  208. * \param z Z coordinate of center of sphere
  209. * \param radius radius of sphere
  210. * \returns true if sphere is visible
  211. */
  212. bool isVisible(float x, float y, float z, float radius);
  213. /*!
  214. * \brief Build a visible room list starting at index
  215. * \param index valid room index where to start the list
  216. */
  217. void newRoomRenderList(int index);
  218. /*!
  219. * \brief Build a visible room list starting from room and
  220. * only considers its linked rooms and their linked rooms.
  221. * \param room First room in list
  222. */
  223. void buildRoomRenderList(RenderRoom *room);
  224. /*!
  225. * \brief Renders visible world object.
  226. *
  227. * Texture must be initialized.
  228. */
  229. void drawObjects();
  230. /*!
  231. * \brief Renders Sky domes/boxes/etc by scaling factor.
  232. *
  233. * Texture must be initialized.
  234. * \param scale correct scale for map size
  235. */
  236. void drawSkyMesh(float scale);
  237. /*!
  238. * \brief Renders a skeletal model.
  239. *
  240. * Texture must be initialized!
  241. * \param model model to render
  242. */
  243. void drawModel(SkeletalModel *model);
  244. /*!
  245. * Renders a room in 2 seperate passes to handle alpha.
  246. *
  247. * Currently doesnt sort alpha but looks pretty good.
  248. * Texture must be initialized.
  249. * Draw all rooms with alpha false, then again with alpha true.
  250. * \param rRoom room to render
  251. * \param draw_alpha once false, once true
  252. */
  253. void drawRoom(RenderRoom *rRoom, bool draw_alpha);
  254. /*!
  255. * \brief Renders static room model.
  256. *
  257. * Texture must be initialized.
  258. * \param mesh Static model to render
  259. */
  260. void drawRoomModel(static_model_t *mesh);
  261. /*!
  262. * \brief Renders a mesh.
  263. *
  264. * Texture must be initialized.
  265. * \param r_mesh Mesh to render.
  266. * \param type Must be object containing mesh
  267. */
  268. void drawModelMesh(model_mesh_t *r_mesh, RenderMeshType type);
  269. /*!
  270. * \brief Renders a sprite.
  271. *
  272. * Texture must be initialized.
  273. * \param sprite sprite to render
  274. */
  275. void drawSprite(sprite_t *sprite);
  276. /*!
  277. * \brief Updates View Volume. Call once per render frame.
  278. */
  279. void updateViewVolume();
  280. //! \fixme Let them eat cake...? O.o
  281. void tmpRenderModelMesh(model_mesh_t *r_mesh, texture_tri_t *ttri);
  282. Texture mTexture; //!< Texture subsystem
  283. std::vector<RenderRoom *> mRoomRenderList;
  284. std::vector<RenderRoom *> mRooms;
  285. unsigned int mFlags; //!< Rendering flags
  286. unsigned int mMode; //!< Rendering mode
  287. unsigned int *mNumTexturesLoaded;
  288. unsigned int *mNextTextureId;
  289. int mLock;
  290. int mSkyMesh; //!< Skymesh model id
  291. bool mSkyMeshRotation; //!< Should Skymesh be rotated?
  292. #ifdef USING_EMITTER
  293. Emitter *mEmitter; //!< Particle emitter test
  294. #endif
  295. };
  296. #endif