Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Render.h 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 Starts and sets up OpenGL target
  127. * \param width width of window
  128. * \param height height of window
  129. */
  130. void Init(int width, int height);
  131. /*!
  132. * \brief Loads textures in a certain id slot
  133. * \param image Image to load
  134. * \param width width of image
  135. * \param height height of image
  136. * \param id id for texture
  137. * \sa Texture::loadBufferSlot()
  138. */
  139. void loadTexture(unsigned char *image,
  140. unsigned int width, unsigned int height,
  141. unsigned int id);
  142. /*!
  143. * \brief Sets up textures for OpenRaider.
  144. * \param textureDir Is valid and exists with textures
  145. * \param numLoaded returns number of loaded textures and will update
  146. * number of external textures loaded
  147. * \param nextId will update next level texture id
  148. */
  149. void initTextures(char *textureDir, unsigned int *numLoaded,
  150. unsigned int *nextId);
  151. /*!
  152. * \brief Initializes Emitter.
  153. *
  154. * Emitter is set up for rendering with 2 textures in
  155. * a overhead rain or snow like pattern.
  156. * Textures have to be initialized!
  157. * \param name valid C-String name
  158. * \param size valid size
  159. * \param snowTex1 valid first texture
  160. * \param snowTex2 valid second texture
  161. */
  162. void initEmitter(const char *name, unsigned int size,
  163. unsigned int snowTex1, unsigned int snowTex2);
  164. /*!
  165. * Removes current world/entity/etc geometry
  166. */
  167. void ClearWorld();
  168. /*!
  169. * \brief Clears bitflags, changes state of renderer in some way
  170. * \param flags RenderFlags to clear (ORed)
  171. * \fixme use enum not integer as parameter?!
  172. */
  173. void clearFlags(unsigned int flags);
  174. /*!
  175. * \brief Sets bitflags, changes state of renderer in some way
  176. * \param flags RenderFlags to set (ORed)
  177. * \fixme use enum not integer as parameter?!
  178. */
  179. void setFlags(unsigned int flags);
  180. void setMode(int n);
  181. void Update(int width, int height);
  182. /*!
  183. * \brief Renders a single game frame
  184. */
  185. void Display();
  186. void setSkyMesh(int index, bool rot);
  187. void ViewModel(entity_t *ent, int index);
  188. void RegisterCamera(Camera *camera);
  189. void addSkeletalModel(SkeletalModel *mdl);
  190. private:
  191. /*!
  192. * \brief Check if a bounding box is in the View Volume
  193. * \param bboxMin Start coordinates of a valid bounding box
  194. * \param bboxMax End coordinates of a valid bounding box
  195. * \returns true if bounding box is visible
  196. */
  197. bool isVisible(float bboxMin[3], float bboxMax[3]);
  198. /*!
  199. * \brief Check if a point is in the View Volume
  200. * \param x X coordinate
  201. * \param y Y coordinate
  202. * \param z Z coordinate
  203. * \returns true if point is visible
  204. */
  205. bool isVisible(float x, float y, float z);
  206. /*!
  207. * \brief Check if a sphere is in the View Volume
  208. * \param x X coordinate of center of sphere
  209. * \param y Y coordinate of center of sphere
  210. * \param z Z coordinate of center of sphere
  211. * \param radius radius of sphere
  212. * \returns true if sphere is visible
  213. */
  214. bool isVisible(float x, float y, float z, float radius);
  215. /*!
  216. * \brief Build a visible room list starting at index
  217. * \param index valid room index where to start the list
  218. */
  219. void newRoomRenderList(int index);
  220. /*!
  221. * \brief Build a visible room list starting from room and
  222. * only considers its linked rooms and their linked rooms.
  223. * \param room First room in list
  224. */
  225. void buildRoomRenderList(RenderRoom *room);
  226. /*!
  227. * \brief Renders visible world object.
  228. *
  229. * Texture must be initialized.
  230. */
  231. void drawObjects();
  232. /*!
  233. * \brief Renders Sky domes/boxes/etc by scaling factor.
  234. *
  235. * Texture must be initialized.
  236. * \param scale correct scale for map size
  237. */
  238. void drawSkyMesh(float scale);
  239. /*!
  240. * \brief Renders a skeletal model.
  241. *
  242. * Texture must be initialized!
  243. * \param model model to render
  244. */
  245. void drawModel(SkeletalModel *model);
  246. /*!
  247. * Renders a room in 2 seperate passes to handle alpha.
  248. *
  249. * Currently doesnt sort alpha but looks pretty good.
  250. * Texture must be initialized.
  251. * Draw all rooms with alpha false, then again with alpha true.
  252. * \param rRoom room to render
  253. * \param draw_alpha once false, once true
  254. */
  255. void drawRoom(RenderRoom *rRoom, bool draw_alpha);
  256. /*!
  257. * \brief Renders static room model.
  258. *
  259. * Texture must be initialized.
  260. * \param mesh Static model to render
  261. */
  262. void drawRoomModel(static_model_t *mesh);
  263. /*!
  264. * \brief Renders a mesh.
  265. *
  266. * Texture must be initialized.
  267. * \param r_mesh Mesh to render.
  268. * \param type Must be object containing mesh
  269. */
  270. void drawModelMesh(model_mesh_t *r_mesh, RenderMeshType type);
  271. /*!
  272. * \brief Renders a sprite.
  273. *
  274. * Texture must be initialized.
  275. * \param sprite sprite to render
  276. */
  277. void drawSprite(sprite_t *sprite);
  278. /*!
  279. * \brief Updates View Volume. Call once per render frame.
  280. */
  281. void updateViewVolume();
  282. //! \fixme Let them eat cake...? O.o
  283. void tmpRenderModelMesh(model_mesh_t *r_mesh, texture_tri_t *ttri);
  284. Texture mTexture; //!< Texture subsystem
  285. Camera *mCamera; //!< Camera subsystem
  286. std::vector<RenderRoom *> mRoomRenderList;
  287. std::vector<RenderRoom *> mRooms;
  288. std::vector<SkeletalModel *> mModels;
  289. unsigned int mFlags; //!< Rendering flags
  290. unsigned int mWidth; //!< Viewport width
  291. unsigned int mHeight; //!< Viewport height
  292. unsigned int mMode; //!< Rendering mode
  293. unsigned int *mNumTexturesLoaded;
  294. unsigned int *mNextTextureId;
  295. int mLock;
  296. int mSkyMesh; //!< Skymesh model id
  297. bool mSkyMeshRotation; //!< Should Skymesh be rotated?
  298. #ifdef USING_EMITTER
  299. Emitter *mEmitter; //!< Particle emitter test
  300. #endif
  301. };
  302. #endif