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

Render.h 10KB

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