/*---------------------------------------------------------------------------* Project: Horizon File: demo_TrianglesRenderData.h Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 46365 $ *---------------------------------------------------------------------------*/ #ifndef DEMO_TRIANGLES_RENDER_DATA_H_ #define DEMO_TRIANGLES_RENDER_DATA_H_ #include #include #include "demo/Utility/demo_MemoryManager.h" #include "demo/RenderData/demo_RenderData.h" namespace demo { /*! :private @brief Enumerated type that indicates vertex attributes */ enum VertexAttributes { VERTEX_NONE_ATTRIBUTE = 0, //!< indicates that vertex attributes have no attributes VERTEX_POSITION_ATTRIBUTE = (1 << 0), //!< Position coordinate attributes of the vertex attributes VERTEX_COLOR_ATTRIBUTE = (1 << 1), //!< Vertex color attributes of the vertex attributes VERTEX_TEXCOORD_ATTRIBUTE = (1 << 2), //!< Texture coordinate attributes of the vertex attributes VERTEX_NORMAL_ATTRIBUTE = (1 << 3), //!< Normal attributes of the vertex attributes VERTEX_TANGENT_ATTRIBUTE = (1 << 4) //!< Tangent attributes of the vertex attributes }; /*! :private @brief Number of elements for the vertex position coordinates (all elements in f32 format as xyzw) */ const int VERTEX_POSITION_ATTRIBUTE_SIZE = 4; /*! :private @brief Number of elements for the vertex color (all elements in f32 format as red, green, blue, and alpha) */ const int VERTEX_COLOR_ATTRIBUTE_SIZE = 4; /*! :private @brief Number of elements for the vertex texture coordinates (all elements in f32 format as s, t, and r) */ const int VERTEX_TEXCOORD_ATTRIBUTE_SIZE = 3; /*! :private @brief Number of elements for the vertex normal (all elements in f32 format as nx, ny, and nz) */ const int VERTEX_NORMAL_ATTRIBUTE_SIZE = 3; /*! :private @brief Number of elements for the vertex tangent (all elements in f32 format as tx, ty, and tz) */ const int VERTEX_TANGENT_ATTRIBUTE_SIZE = 3; /*! :private @brief Maximum number of triangles that the TrianglesRenderData class can have as rendering data. */ const u32 MAX_TRIANGLES_NUM = 8192; /*! :private @brief Maximum number of quadrangles that the TrianglesRenderData class can have as rendering data */ const u32 MAX_SQUARES_NUM = MAX_TRIANGLES_NUM / 2; /*! :private @brief Maximum number of vertices for triangles that the TrianglesRenderData class can have as rendering data. */ const u32 MAX_TRIANGLE_VERTICES_NUM = MAX_TRIANGLES_NUM * 3; /*! :private @brief Maximum number of indices for triangles that the TrianglesRenderData class can have as rendering data */ const u32 MAX_INDICES_NUM = MAX_TRIANGLES_NUM * 3; /*! :private @brief Vertex buffer data type */ const GLenum ARRAY_BUFFER_DATA_TYPE = GL_ARRAY_BUFFER; /*! :private @brief Index buffer data type */ const GLenum ELEMENT_ARRAY_BUFFER_DATA_TYPE = GL_ELEMENT_ARRAY_BUFFER; /*! :private @brief Class for triangular mesh that is the rendering data. */ class TrianglesRenderData : public RenderData { public: /*! :private @brief Constructor. */ TrianglesRenderData(void); /*! :private @brief Destructor. */ virtual ~TrianglesRenderData(void); public: /*! :private @brief Initializes the rendering data. */ virtual void Initialize(void); /*! :private @brief Finalizes the rendering data. */ virtual void Finalize(void); public: /*! :private @brief Performs initialization for the vertex buffer. @param[in] vertexAttributes Bitwise OR of vertex attributes @param[in] triangleType Type of triangle primitive to render @param[in] verticesNum Total number of the vertices of triangular primitive @param[in] trianglesNum Total number of triangle primitives */ void InitializeVertexBuffers(const u32 vertexAttributes, const GLenum triangleType, const u32 verticesNum, const u32 trianglesNum); protected: void SetVertexAttributes(const u32 vertexAttributes); void SetTriangleType(const GLenum triangleType); void SetVerticesNum(const u32 verticesNum); void SetTrianglesNum(const u32 trianglesNum); void CreateArrays(void); void DestroyArrays(void); public: /*! :private @brief Sets the number of packed vertices. @param[in] packedVerticesNum Number of packed vertices */ void SetPackedVerticesNum(const u32 packedVerticesNum); /*! :private @brief Adds the number of packed vertices. @param[in] packedVerticesNum Number of packed vertices to add */ void AddPackedVerticesNum(const u32 packedVerticesNum); /*! :private @brief Gets the number of packed vertices. @return Number of packed vertices */ u32 GetPackedVerticesNum(void) const; public: /*! :private @brief Sets the number of packed triangles. @param[in] packedTrianglesNum Number of packed triangles */ void SetPackedTrianglesNum(const u32 packedTrianglesNum); /*! :private @brief Adds the number of packed triangles. @param[in] packedTrianglesNum Number of packed triangles to add */ void AddPackedTrianglesNum(const u32 packedTrianglesNum); /*! :private @brief Gets the number of packed triangles. @return Number of packed triangles */ u32 GetPackedTrianglesNum(void) const; public: /*! :private @brief Renders the packed triangles. */ void DrawPackedTriangles(void); public: /*! :private @brief Clears the number of packed triangles. */ void ClearPackedNum(void); /*! :private @brief Based on the packed triangle data, finds the size of the vertex buffer to update. */ void SetPackedArraysSize(void); public: /*! :private @brief Performs update of the vertex buffer. */ void UpdateBuffers(void); /*! :private @brief Sets whether to update the vertex buffer before each rendering. */ void SetUpdateBufferBeforeDraw(const bool updateBufferFlag); protected: void DestroyBuffers(void); public: /*! :private @brief Sets the position coordinates in the vertex matrix. @param[in] index Triangle index @param[in] positionX X-coordinate of position coordinates @param[in] positionY Y-coordinate of position coordinates @param[in] positionZ Z-coordinate of position coordinates */ void SetPosition(const u32 index, const GLfloat positionX, const GLfloat positionY, const GLfloat positionZ); /*! :private @brief Sets the position coordinates in the vertex matrix. @param[in] index Triangle index @param[in] positionX X-coordinate of position coordinates @param[in] positionY Y-coordinate of position coordinates @param[in] positionZ Z-coordinate of position coordinates @param[in] positionW W-coordinate of position coordinates */ void SetPosition(const u32 index, const GLfloat positionX, const GLfloat positionY, const GLfloat positionZ, const GLfloat positionW); /*! :private @brief Sets the vertex color in the vertex matrix. @param[in] index Triangle index @param[in] red Red component of vertex color @param[in] green Green component of vertex color @param[in] blue Blue component of vertex color */ void SetColor(const u32 index, const GLfloat red, const GLfloat green, const GLfloat blue); /*! :private @brief Sets the vertex color in the vertex matrix. @param[in] index Triangle index @param[in] red Red component of vertex color @param[in] green Green component of vertex color @param[in] blue Blue component of vertex color @param[in] alpha Alpha component of vertex color */ void SetColor(const u32 index, const GLfloat red, const GLfloat green, const GLfloat blue, const GLfloat alpha); /*! :private @brief Sets the vertex color in the vertex matrix. @param[in] red Red component of vertex color @param[in] green Green component of vertex color @param[in] blue Blue component of vertex color */ virtual void SetColor(const GLfloat red, const GLfloat green, const GLfloat blue); /*! :private @brief Sets the vertex color in the vertex matrix. @param[in] index Triangle index @param[in] red Red component of vertex color @param[in] green Green component of vertex color @param[in] blue Blue component of vertex color @param[in] alpha Alpha component of vertex color */ virtual void SetColor(const GLfloat red, const GLfloat green, const GLfloat blue, const GLfloat alpha); /*! :private @brief Sets the texture coordinates in the vertex matrix. @param[in] index Triangle index @param[in] texcoordS S-coordinate of the texture coordinates @param[in] texcoordT T-coordinate of the texture coordinates */ void SetTexcoord(const u32 index, const GLfloat texcoordS, const GLfloat texcoordT); /*! :private @brief Sets the texture coordinates in the vertex matrix. @param[in] index Triangle index @param[in] texcoordS S-coordinate of the texture coordinates @param[in] texcoordT T-coordinate of the texture coordinates @param[in] texcoordR R-coordinate of the texture coordinates */ void SetTexcoord(const u32 index, const GLfloat texcoordS, const GLfloat texcoordT, const GLfloat texcoordR); /*! :private @brief Sets the normal in the vertex matrix. @param[in] index Triangle index @param[in] normalX X-coordinate of the normal @param[in] normalY Y-coordinate of the normal @param[in] normalZ Z-coordinate of the normal */ void SetNormal(const u32 index, const GLfloat normalX, const GLfloat normalY, const GLfloat normalZ); /*! :private @brief Sets the tangent in the vertex matrix. @param[in] index Triangle index @param[in] tangentX X-coordinate of the tangent @param[in] tangentY Y-coordinate of the tangent @param[in] tangentZ Z-coordinate of the tangent */ void SetTangent(const u32 index, const GLfloat tangentX, const GLfloat tangentY, const GLfloat tangentZ); /*! :private @brief Sets the triangle index in the index matrix. @param[in] index Triangle index @param[in] index0 Index of the first vertex of the triangle @param[in] index1 Index of the second vertex of the triangle @param[in] index2 Index of the third vertex of the triangle */ void SetIndex(const u32 index, const GLuint index0, const GLuint index1, const GLuint index2); /*! :private @brief Sets the index to create a quadrangle with TRIANGLE_STRIP in the index matrix. @param[in] index Quadrangle index @param[in] index0 Index of the first vertex of the quadrangle @param[in] index1 Index of the second vertex of the quadrangle @param[in] index2 Index of the third vertex of the quadrangle @param[in] index3 Index of the fourth vertex of the quadrangle */ void SetSquareIndex(const u32 square_index, const GLuint index0, const GLuint index1, const GLuint index2, const GLuint index3); public: /*! :private @brief Sets the world coordinates. @param[in] worldPositionX X-coordinate of world coordinates @param[in] worldPositionY Y-coordinate of world coordinates @param[in] worldPositionZ Z-coordinate of world coordinates */ void SetWorldPosition(const f32 worldPositionX, const f32 worldPositionY, const f32 worldPositionZ); /*! :private @brief Parallel translate by only the world coordinate vectors. @param[in] worldPositionX X-coordinate of world coordinate vector @param[in] worldPositionY Y-coordinate of world coordinate vector @param[in] worldPositionZ Z-coordinate of world coordinate vector */ void TranslateWorldPosition(const f32 worldPositionX, const f32 worldPositionY, const f32 worldPositionZ); /*! :private @brief Gets the world coordinates. @param[out] worldPositionX X-coordinate of world coordinates @param[out] worldPositionY Y-coordinate of world coordinates @param[out] worldPositionZ Z-coordinate of world coordinates */ void GetWorldPosition(f32& worldPositionX, f32& worldPositionY, f32& worldPositionZ); /*! :private @brief Sets the rotation angle in the world coordinate system. @param[in] worldAngleX Angle around the X-axis @param[in] worldAngleY Angle around the Y-axis @param[in] worldAngleZ Angle around the Z-axis */ void SetWorldAngle(const f32 worldAngleX, const f32 worldAngleY, const f32 worldAngleZ); /*! :private @brief Rotate by only rotation angle in the world coordinate system. @param[in] worldAngleX Angle around the X-axis in world coordinates @param[in] worldAngleY Angle around the Y-axis in world coordinates @param[in] worldAngleZ Angle around the Z-axis in world coordinates */ void RotateWorldAngle(const f32 worldAngleX, const f32 worldAngleY, const f32 worldAngleZ); /*! :private @brief Gets the rotation angle in the world coordinate system. @param[in] worldAngleX Angle around the X-axis @param[in] worldAngleY Angle around the Y-axis @param[in] worldAngleZ Angle around the Z-axis */ void GetWorldAngle(f32& worldAngleX, f32& worldAngleY, f32& worldAngleZ); public: /*! :private @brief Gets the world matrix. @return World matrix */ virtual nn::math::MTX44 GetWorldMatrix(void) const; public: /*! :private @brief Performs rendering using the rendering data. */ virtual void Draw(void); protected: virtual void DrawElements(void); public: /*! :private @brief Enables vertex attributes. */ virtual void EnableVertexAttributes(void); /*! :private @brief Sets whether to enable vertex attributes before rendering. @param[in] updateAttributesFlag true if vertex attributes are enabled before rendering, false otherwise. */ void SetEnableVertexAttributesBeforeDraw(const bool updateAttributesFlag); protected: u32 m_ShaderType; u32 m_VertexAttributes; GLenum m_TriangleType; u32 m_VerticesNum; u32 m_TrianglesNum; u32 m_PackedVerticesNum; u32 m_PackedTrianglesNum; protected: u32 m_PositionArraySize; u32 m_ColorArraySize; u32 m_TexcoordArraySize; u32 m_NormalArraySize; u32 m_TangentArraySize; u32 m_TotalArraySize; u32 m_OldTotalArraySize; u32 m_IndexArraySize; u32 m_OldIndexArraySize; protected: GLfloat* m_PositionArray; GLfloat* m_ColorArray; GLfloat* m_TexcoordArray; GLfloat* m_NormalArray; GLfloat* m_TangentArray; GLushort* m_IndexArray; protected: GLuint m_ArrayBufferId; GLuint m_ElementArrayBufferId; protected: bool m_InitArrayFlag; bool m_InitBufferFlag; bool m_UpdateBufferFlag; bool m_UpdateAttributesFlag; protected: f32 m_PositionX; f32 m_PositionY; f32 m_PositionZ; f32 m_AngleX; f32 m_AngleY; f32 m_AngleZ; }; } #endif