diff options
Diffstat (limited to 'src/graphics/engine/engine.h')
-rw-r--r-- | src/graphics/engine/engine.h | 361 |
1 files changed, 198 insertions, 163 deletions
diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h index 27f0173..c9391db 100644 --- a/src/graphics/engine/engine.h +++ b/src/graphics/engine/engine.h @@ -151,7 +151,7 @@ struct EngineTriangle //! 2nd texture std::string tex2Name; - EngineTriangle() + inline EngineTriangle() { state = ENG_RSTATE_NORMAL; } @@ -178,6 +178,92 @@ enum EngineObjectType ENG_OBJTYPE_METAL = 6 }; + +/** + * \struct EngineBaseObjDataTier + * \brief Tier 4 of object tree (data) + */ +struct EngineBaseObjDataTier +{ + EngineTriangleType type; + Material material; + int state; + std::vector<VertexTex2> vertices; + unsigned int staticBufferId; + bool updateStaticBuffer; + + inline EngineBaseObjDataTier(EngineTriangleType type = ENG_TRIANGLE_TYPE_TRIANGLES, + const Material& material = Material(), + int state = ENG_RSTATE_NORMAL) + : type(type), material(material), state(state), staticBufferId(0), updateStaticBuffer(false) {} +}; + +/** + * \struct EngineBaseObjLODTier + * \brief Tier 3 of base object tree (LOD) + */ +struct EngineBaseObjLODTier +{ + float min; + float max; + std::vector<EngineBaseObjDataTier> next; + + inline EngineBaseObjLODTier(float min = 0.0f, float max = 0.0f) + : min(min), max(max) {} +}; + +/** + * \struct EngineBaseObjTexTier + * \brief Tier 2 of base object tree (textures) + */ +struct EngineBaseObjTexTier +{ + std::string tex1Name; + Texture tex1; + std::string tex2Name; + Texture tex2; + std::vector<EngineBaseObjLODTier> next; + + inline EngineBaseObjTexTier(const std::string& tex1Name = "", const std::string& tex2Name = "") + : tex1Name(tex1Name), tex2Name(tex2Name) {} +}; + +/** + * \struct BaseEngineObject + * \brief Base (template) object - geometry for engine objects + * + * This is also the tier 1 of base object tree. + */ +struct EngineBaseObject +{ + //! If true, base object is valid in objects vector + bool used; + //! Number of triangles + int totalTriangles; + //! Bounding box min (origin 0,0,0 always included) + Math::Vector bboxMin; + //! bounding box max (origin 0,0,0 always included) + Math::Vector bboxMax; + //! Radius of the sphere at the origin + float radius; + //! Next tier (LOD) + std::vector<EngineBaseObjTexTier> next; + + inline EngineBaseObject() + { + LoadDefault(); + } + + inline void LoadDefault() + { + used = false; + totalTriangles = 0; + bboxMax.LoadZero(); + bboxMin.LoadZero(); + radius = 0.0f; + } +}; + /** * \struct EngineObject * \brief Object drawn by the graphics engine @@ -186,33 +272,27 @@ struct EngineObject { //! If true, object is valid in objects vector bool used; + //! Rank of associated base engine object + int baseObjRank; //! If true, the object is drawn bool visible; //! If true, object is behind the 2D interface bool drawWorld; //! If true, the shape is before the 2D interface bool drawFront; - //! Number of triangles - int totalTriangles; //! Type of object - EngineObjectType type; + EngineObjectType type; //! Transformation matrix Math::Matrix transform; //! Distance to object from eye point float distance; - //! Bounding box min (origin 0,0,0 always included) - Math::Vector bboxMin; - //! bounding box max (origin 0,0,0 always included) - Math::Vector bboxMax; - //! Radius of the sphere at the origin - float radius; //! Rank of the associated shadow int shadowRank; //! Transparency of the object [0, 1] float transparency; //! Calls LoadDefault() - EngineObject() + inline EngineObject() { LoadDefault(); } @@ -221,88 +301,18 @@ struct EngineObject inline void LoadDefault() { used = false; + baseObjRank = -1; visible = false; drawWorld = false; drawFront = false; - totalTriangles = 0; type = ENG_OBJTYPE_NULL; transform.LoadIdentity(); - bboxMax.LoadZero(); - bboxMin.LoadZero(); distance = 0.0f; - radius = 0.0f; shadowRank = -1; transparency = 0.0f; } }; -struct EngineObjLevel1; -struct EngineObjLevel2; -struct EngineObjLevel3; -struct EngineObjLevel4; - -/** - * \struct EngineObjLevel4 - * \brief Tier 4 of object tree - */ -struct EngineObjLevel4 -{ - bool used; - EngineTriangleType type; - Material material; - int state; - std::vector<VertexTex2> vertices; - - EngineObjLevel4(bool used = false, - EngineTriangleType type = ENG_TRIANGLE_TYPE_TRIANGLES, - const Material& material = Material(), - int state = ENG_RSTATE_NORMAL); -}; - -/** - * \struct EngineObjLevel3 - * \brief Tier 3 of object tree - */ -struct EngineObjLevel3 -{ - bool used; - float min; - float max; - std::vector<EngineObjLevel4> next; - - EngineObjLevel3(bool used = false, float min = 0.0f, float max = 0.0f); -}; - -/** - * \struct EngineObjLevel2 - * \brief Tier 2 of object tree - */ -struct EngineObjLevel2 -{ - bool used; - int objRank; - std::vector<EngineObjLevel3> next; - - EngineObjLevel2(bool used = false, int objRank = -1); -}; - -/** - * \struct EngineObjLevel1 - * \brief Tier 1 of object tree - */ -struct EngineObjLevel1 -{ - bool used; - std::string tex1Name; - Texture tex1; - std::string tex2Name; - Texture tex2; - std::vector<EngineObjLevel2> next; - - EngineObjLevel1(bool used = false, const std::string& tex1Name = "", - const std::string& tex2Name = ""); -}; - /** * \struct EngineShadowType * \brief Type of shadow drawn by the graphics engine @@ -342,12 +352,12 @@ struct EngineShadow //! Height from the ground float height; - EngineShadow() + inline EngineShadow() { LoadDefault(); } - void LoadDefault() + inline void LoadDefault() { used = false; hide = false; @@ -384,12 +394,12 @@ struct EngineGroundSpot //! Radius of the shadow drawn float drawRadius; - EngineGroundSpot() + inline EngineGroundSpot() { LoadDefault(); } - void LoadDefault() + inline void LoadDefault() { used = false; color = Color(); @@ -448,12 +458,12 @@ struct EngineGroundMark //! Pointer to the table char* table; - EngineGroundMark() + inline EngineGroundMark() { LoadDefault(); } - void LoadDefault() + inline void LoadDefault() { draw = false; phase = ENG_GR_MARK_PHASE_NULL; @@ -545,10 +555,10 @@ struct EngineMouse //! Hot point Math::Point hotPoint; - EngineMouse(int icon1 = -1, int icon2 = -1, int iconShadow = -1, - EngineRenderState mode1 = ENG_RSTATE_NORMAL, - EngineRenderState mode2 = ENG_RSTATE_NORMAL, - Math::Point hotPoint = Math::Point()) + inline EngineMouse(int icon1 = -1, int icon2 = -1, int iconShadow = -1, + EngineRenderState mode1 = ENG_RSTATE_NORMAL, + EngineRenderState mode2 = ENG_RSTATE_NORMAL, + Math::Point hotPoint = Math::Point()) { this->icon1 = icon1; this->icon2 = icon2; @@ -609,27 +619,36 @@ struct EngineMouse * * Objects are uniquely identified by object rank obtained at object creation. Creating an * object equals to allocating space for EngineObject structure which holds object parameters. - * Object's geometric data is stored in a separate structure - a 4-tier tree which splits - * the information of each geometric triangle. + * + * Object's geometric data is stored as a separate object -- base engine object. Each object + * must reference a valid base engine object or an empty base engine object (with rank = -1). + * This many-to-one association allows to share same geometric data (e.g. from same model) + * across objects. + * + * Base engine object data is stored in a 4-tier tree which splits the data describing triangles. * * The 4 tiers contain the following information: - * - level 1 (EngineObjLevel1) - two textures (names and structs) applied to triangles, - * - level 2 (EngineObjLevel2) - object rank - * - level 3 (EngineObjLevel3) - minumum and maximum LOD (=level of detail) - * - level 4 (EngineObjLevel4) - type of object*, material, render state and the actual triangle data + * - level 1 (EngineBaseObject) - geometric statistics + * - level 2 (EngineBaseObjTexTier) - two textures (names and structs) applied to triangles, + * - level 3 (EngineBaseObjLODTier) - minumum and maximum LOD (=level of detail) + * - level 4 (EngineBaseObjDataTier) - type of object*, material, render state and the actual vertex data * - * NOTE: type of object in this context means only the internal type in 3D engine. It is not related + * *NOTE: type of object in this context means only the internal type in 3D engine. It is not related * to CObject types. * + * Last tier containing vertex data contains also an ID of static buffer holding the data. + * The static buffer is created and updated with new data as needed. + * * Such tiered structure complicates loops over all object data, but saves a lot of memory and - * optimizes the rendering process (for instance, switching of textures is an expensive operation). + * optimizes the rendering process. * * \section Shadows Shadows * * Each engine object can be associated with a shadow (EngineShadow). Like objects, shadows are * identified by their rank obtained upon creation. * - * ... + * Shadows are drawn as circular spots on the ground, except for shadows for worms, which have + * special mode for them. * * \section RenderStates Render States * @@ -692,12 +711,6 @@ public: //! Writes a screenshot containing the current frame bool WriteScreenShot(const std::string& fileName, int width, int height); - - //! Reads settings from INI - bool ReadSettings(); - //! Writes settings to INI - bool WriteSettings(); - //@{ //! Management of game pause mode void SetPause(bool pause); @@ -747,58 +760,73 @@ public: /* *************** Object management *************** */ + // Base objects + + //! Creates a base object and returns its rank + int CreateBaseObject(); + //! Deletes a base object + void DeleteBaseObject(int baseObjRank); + //! Deletes all base objects + void DeleteAllBaseObjects(); + + //! Copies geometry between two base objects + void CopyBaseObject(int sourceBaseObjRank, int destBaseObjRank); + + //! Adds triangles to given object with the specified params + void AddBaseObjTriangles(int baseObjRank, const std::vector<VertexTex2>& vertices, + EngineTriangleType triangleType, + const Material& material, int state, + std::string tex1Name, std::string tex2Name, + float min, float max, bool globalUpdate); + + //! Adds a tier 4 engine object directly + void AddBaseObjQuick(int baseObjRank, const EngineBaseObjDataTier& buffer, + std::string tex1Name, std::string tex2Name, + float min, float max, bool globalUpdate); + + // Objects + //! Creates a new object and returns its rank int CreateObject(); //! Deletes all objects, shadows and ground spots - void FlushObject(); + void DeleteAllObjects(); //! Deletes the given object - bool DeleteObject(int objRank); + void DeleteObject(int objRank); + + //@{ + //! Management of the base object rank for engine object + void SetObjectBaseRank(int objRank, int baseObjRank); + int GetObjectBaseRank(int objRank); + //@} //@{ //! Management of engine object type - bool SetObjectType(int objRank, EngineObjectType type); + void SetObjectType(int objRank, EngineObjectType type); EngineObjectType GetObjectType(int objRank); //@} //@{ //! Management of object transform - bool SetObjectTransform(int objRank, const Math::Matrix& transform); - bool GetObjectTransform(int objRank, Math::Matrix& transform); + void SetObjectTransform(int objRank, const Math::Matrix& transform); + void GetObjectTransform(int objRank, Math::Matrix& transform); //@} //! Sets drawWorld for given object - bool SetObjectDrawWorld(int objRank, bool draw); + void SetObjectDrawWorld(int objRank, bool draw); //! Sets drawFront for given object - bool SetObjectDrawFront(int objRank, bool draw); + void SetObjectDrawFront(int objRank, bool draw); //! Sets the transparency level for given object - bool SetObjectTransparency(int objRank, float value); + void SetObjectTransparency(int objRank, float value); //! Returns the bounding box for an object - bool GetObjectBBox(int objRank, Math::Vector& min, Math::Vector& max); + void GetObjectBBox(int objRank, Math::Vector& min, Math::Vector& max); //! Returns the total number of triangles of given object int GetObjectTotalTriangles(int objRank); - //! Adds triangles to given object with the specified params - bool AddTriangles(int objRank, const std::vector<VertexTex2>& vertices, - const Material& material, int state, - std::string tex1Name, std::string tex2Name, - float min, float max, bool globalUpdate); - - //! Adds a surface to given object with the specified params - bool AddSurface(int objRank, const std::vector<VertexTex2>& vertices, - const Material& material, int state, - std::string tex1Name, std::string tex2Name, - float min, float max, bool globalUpdate); - - //! Adds a tier 4 engine object directly - bool AddQuick(int objRank, const EngineObjLevel4& buffer, - std::string tex1Name, std::string tex2Name, - float min, float max, bool globalUpdate); - //! Returns the first found tier 4 engine object for the given params or nullptr if not found - EngineObjLevel4* FindTriangles(int objRank, const Material& material, + EngineBaseObjDataTier* FindTriangles(int objRank, const Material& material, int state, std::string tex1Name, std::string tex2Name, float min, float max); @@ -810,16 +838,16 @@ public: void ChangeLOD(); //! Changes the 2nd texure for given object - bool ChangeSecondTexture(int objRank, const std::string& tex2Name); + void ChangeSecondTexture(int objRank, const std::string& tex2Name); //! Changes (recalculates) texture mapping for given object - bool ChangeTextureMapping(int objRank, const Material& mat, int state, + void ChangeTextureMapping(int objRank, const Material& mat, int state, const std::string& tex1Name, const std::string& tex2Name, float min, float max, EngineTextureMapping mode, float au, float bu, float av, float bv); //! Changes texture mapping for robot tracks - bool TrackTextureMapping(int objRank, const Material& mat, int state, + void TrackTextureMapping(int objRank, const Material& mat, int state, const std::string& tex1Name, const std::string& tex2Name, float min, float max, EngineTextureMapping mode, float pos, float factor, float tl, float ts, float tt); @@ -829,20 +857,20 @@ public: int DetectObject(Math::Point mouse); //! Creates a shadow for the given object - bool CreateShadow(int objRank); + void CreateShadow(int objRank); //! Deletes the shadow for given object void DeleteShadow(int objRank); //@{ //! Management of different shadow params - bool SetObjectShadowHide(int objRank, bool hide); - bool SetObjectShadowType(int objRank, EngineShadowType type); - bool SetObjectShadowPos(int objRank, const Math::Vector& pos); - bool SetObjectShadowNormal(int objRank, const Math::Vector& normal); - bool SetObjectShadowAngle(int objRank, float angle); - bool SetObjectShadowRadius(int objRank, float radius); - bool SetObjectShadowIntensity(int objRank, float intensity); - bool SetObjectShadowHeight(int objRank, float height); + void SetObjectShadowHide(int objRank, bool hide); + void SetObjectShadowType(int objRank, EngineShadowType type); + void SetObjectShadowPos(int objRank, const Math::Vector& pos); + void SetObjectShadowNormal(int objRank, const Math::Vector& normal); + void SetObjectShadowAngle(int objRank, float angle); + void SetObjectShadowRadius(int objRank, float radius); + void SetObjectShadowIntensity(int objRank, float intensity); + void SetObjectShadowHeight(int objRank, float height); float GetObjectShadowRadius(int objRank); //@} @@ -852,7 +880,7 @@ public: bool GetHighlight(Math::Point& p1, Math::Point& p2); //! Deletes all ground spots - void FlushGroundSpot(); + void DeleteAllGroundSpots(); //! Creates a new ground spot and returns its rank int CreateGroundSpot(); //! Deletes the given ground spot @@ -860,11 +888,11 @@ public: //@{ //! Management of different ground spot params - bool SetObjectGroundSpotPos(int rank, const Math::Vector& pos); - bool SetObjectGroundSpotRadius(int rank, float radius); - bool SetObjectGroundSpotColor(int rank, const Color& color); - bool SetObjectGroundSpotMinMax(int rank, float min, float max); - bool SetObjectGroundSpotSmooth(int rank, float smooth); + void SetObjectGroundSpotPos(int rank, const Math::Vector& pos); + void SetObjectGroundSpotRadius(int rank, float radius); + void SetObjectGroundSpotColor(int rank, const Color& color); + void SetObjectGroundSpotMinMax(int rank, float min, float max); + void SetObjectGroundSpotSmooth(int rank, float smooth); //@} //! Creates the ground mark with the given params @@ -1157,6 +1185,8 @@ public: protected: //! Prepares the interface for 3D scene void Draw3DScene(); + //! Draw 3D object + void DrawObject(const EngineBaseObjDataTier& p4); //! Draws the user interface over the scene void DrawInterface(); @@ -1186,15 +1216,13 @@ protected: //! Draw statistic texts void DrawStats(); - //! Creates new tier 1 object - EngineObjLevel1& AddLevel1(const std::string& tex1Name, const std::string& tex2Name); - //! Creates a new tier 2 object - EngineObjLevel2& AddLevel2(EngineObjLevel1 &p1, int objRank); - //! Creates a new tier 3 object - EngineObjLevel3& AddLevel3(EngineObjLevel2 &p2, float min, float max); - //! Creates a new tier 4 object - EngineObjLevel4& AddLevel4(EngineObjLevel3 &p3, EngineTriangleType type, - const Material& mat, int state); + //! Creates a new tier 2 object (texture) + EngineBaseObjTexTier& AddLevel2(EngineBaseObject& p1, const std::string& tex1Name, const std::string& tex2Name); + //! Creates a new tier 3 object (LOD) + EngineBaseObjLODTier& AddLevel3(EngineBaseObjTexTier &p2, float min, float max); + //! Creates a new tier 4 object (data) + EngineBaseObjDataTier& AddLevel4(EngineBaseObjLODTier &p3, EngineTriangleType type, + const Material& mat, int state); //! Create texture and add it to cache Texture CreateTexture(const std::string &texName, const TextureCreateParams ¶ms, CImage* image = nullptr); @@ -1221,6 +1249,12 @@ protected: //! Updates geometric parameters of objects (bounding box and radius) void UpdateGeometry(); + //! Updates a given static buffer + void UpdateStaticBuffer(EngineBaseObjDataTier& p4); + + //! Updates static buffers of changed objects + void UpdateStaticBuffers(); + protected: CInstanceManager* m_iMan; CApplication* m_app; @@ -1273,8 +1307,8 @@ protected: //! Previous size of viewport window Math::IntPoint m_lastSize; - //! Root of tree object structure (level 1 list) - std::vector<EngineObjLevel1> m_objectTree; + //! Base objects (also level 1 tier list) + std::vector<EngineBaseObject> m_baseObjects; //! Object parameters std::vector<EngineObject> m_objects; //! Shadow list @@ -1299,6 +1333,7 @@ protected: Color m_waterAddColor; int m_statisticTriangle; bool m_updateGeometry; + bool m_updateStaticBuffers; int m_alphaMode; bool m_groundSpotVisible; bool m_shadowVisible; |