kick
|
00001 // 00002 // MeshData.h 00003 // KickCPP 00004 // 00005 // Created by Morten Nobel-Jørgensen on 10/20/13. 00006 // Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved. 00007 // 00008 00009 #pragma once 00010 00011 #include "kick/core/project_asset.h" 00012 #include "kick/math/bounds3.h" 00013 #include "kick/core/kickgl.h" 00014 #include <vector> 00015 #include <unordered_map> 00016 #include <string> 00017 00018 #include <glm/glm.hpp> 00019 #include <exception> 00020 00021 namespace kick { 00022 enum class MeshType { 00023 Triangles = GL_TRIANGLES, 00024 TriangleFan = GL_TRIANGLE_FAN, 00025 TriangleStrip = GL_TRIANGLE_STRIP, 00026 Points = GL_POINTS, 00027 Lines = GL_LINES, 00028 LineLoop = GL_LINE_LOOP, 00029 LineStrip = GL_LINE_STRIP, 00030 }; 00031 00032 enum class MeshUsage { 00033 00034 StreamDraw = GL_STREAM_DRAW, 00035 #ifndef GL_ES_VERSION_2_0 00036 StreamRead = GL_STREAM_READ, 00037 StreamCopy = GL_STREAM_COPY, 00038 #endif 00039 StaticDraw = GL_STATIC_DRAW, 00040 #ifndef GL_ES_VERSION_2_0 00041 StaticRead = GL_STATIC_READ, 00042 StaticCopy = GL_STATIC_COPY, 00043 #endif 00044 DynamicDraw = GL_DYNAMIC_DRAW, 00045 #ifndef GL_ES_VERSION_2_0 00046 DynamicRead = GL_DYNAMIC_READ, 00047 DynamicCopy = GL_DYNAMIC_COPY 00048 #endif 00049 }; 00050 00051 enum class VertexAttributeSemantic { 00052 Position, 00053 Normal, 00054 Uv1, 00055 Uv2, 00056 Tangent, 00057 Color, 00058 Unknown 00059 }; 00060 00061 VertexAttributeSemantic to_semantic(std::string name); 00062 std::string to_string(VertexAttributeSemantic semantic); 00063 00064 struct InterleavedRecord { 00065 const VertexAttributeSemantic semantic; 00066 const GLvoid * offset; // offset pointer 00067 const int size; // number of (float/int) elements 00068 const bool normalized; // should be normalized or not 00069 const GLenum type; // {GL_FLOAT or GL_INT} 00070 GLsizei stride; 00071 }; 00072 00073 struct SubMeshData { 00074 const GLsizei indexCount; 00075 const GLvoid *dataOffset; 00076 const GLenum mode; 00077 const GLenum type; 00078 }; 00079 00083 class MeshData : public ProjectAsset { 00084 public: 00085 MeshData(); 00086 void setBounds(const Bounds3 &mBounds); 00087 const Bounds3 &bounds(); 00088 00089 GLenum meshUsageVal(); 00090 00091 void setPosition(const std::vector<glm::vec3> &p); 00092 const std::vector<glm::vec3>&position(); 00093 void setNormal(const std::vector<glm::vec3> &n); 00094 const std::vector<glm::vec3>&normal(); 00095 void setTexCoord0(const std::vector<glm::vec2> &u1); 00096 const std::vector<glm::vec2>&texCoord0(); 00097 void setTexCoord1(const std::vector<glm::vec2> &u2); 00098 const std::vector<glm::vec2>&texCoord1(); 00099 void setTangent(const std::vector<glm::vec3> &t); 00100 const std::vector<glm::vec3>&tangent(); 00101 void setColor(const std::vector<glm::vec4> &c); 00102 const std::vector<glm::vec4>&color(); 00103 00104 // Note indices of size 0 means draw all vertices sequentially 00105 void setSubmesh(unsigned int index, const std::vector<GLushort> &indices, MeshType meshType); 00106 const std::vector<GLushort>&submeshIndices(unsigned int index) const; 00107 00108 const MeshType submeshType(unsigned int index) const; 00109 00110 unsigned int submeshesCount(); 00111 GLsizei submeshSize(unsigned int index); 00112 00113 std::vector<float> interleavedData(); 00114 std::vector<InterleavedRecord> interleavedFormat(); 00115 std::vector<GLushort> indicesConcat(); 00116 std::vector<SubMeshData> indicesFormat(); 00117 00118 // recomputing normals (using angle weighted normals) 00119 void recomputeNormals(); 00120 00121 void recomputeBounds(); 00122 MeshUsage meshUsage() const; 00123 void setMeshUsage(MeshUsage meshUsage); 00124 private: 00125 Bounds3 mBounds; 00126 MeshUsage mMeshUsage = MeshUsage::StaticDraw; 00127 std::vector<glm::vec3> mPosition; 00128 std::vector<glm::vec3> mNormal; 00129 std::vector<glm::vec2> mTexCoord0; 00130 std::vector<glm::vec2> mTexCoord1; 00131 std::vector<glm::vec3> mTangent; 00132 std::vector<glm::vec4> mColor; 00133 00134 struct SubMeshInternal { 00135 std::vector<GLushort> indices; 00136 MeshType meshType; 00137 }; 00138 00139 std::vector<SubMeshInternal> subMeshes; 00140 size_t computeInterleavedDataSize(); 00141 }; 00142 }