1 #ifndef _SurfaceCtrl_Mesh_h_ 2 #define _SurfaceCtrl_Mesh_h_ 3 4 namespace Upp{ 5 6 struct Material{ 7 public: MaterialMaterial8 Material(){} MaterialMaterial9 Material(const glm::vec3& diffuse_, const glm::vec3& speculare_, float shininess_){Diffuse = diffuse_;Specular = speculare_;Shininess = shininess_;} 10 GetDiffuseMaterial11 glm::vec3 GetDiffuse()const noexcept{return Diffuse;} GetSpecularMaterial12 glm::vec3 GetSpecular()const noexcept{return Specular;} GetShininessMaterial13 float GetShininess()const noexcept{return Shininess;} 14 SetDiffuseMaterial15 Material& SetDiffuse(const glm::vec3& diffuse_)noexcept{Diffuse = diffuse_; update = true; return *this;} SetSpecularMaterial16 Material& SetSpecular(const glm::vec3& speculare_)noexcept{Specular = speculare_; update = true; return *this;} SetShininessMaterial17 Material& SetShininess(float shininess_)noexcept{Shininess = shininess_; update = true; return *this;} 18 ShouldBeUpdatedMaterial19 bool ShouldBeUpdated()const noexcept{return update;} HaveBeenUpdatedMaterial20 void HaveBeenUpdated()noexcept{update = false;} 21 private: 22 glm::vec3 Diffuse = glm::vec3(0.30f, 0.30f, 0.30f); 23 glm::vec3 Specular = glm::vec3(0.1f, 0.1f, 0.1f); 24 float Shininess = 12.0f; 25 bool update = true; 26 }; 27 28 //Mesh contain vertex and how it's read 29 class Mesh : public Moveable<Mesh>{ 30 public: Mesh()31 Mesh(){} Mesh(Mesh && obj)32 Mesh(Mesh&& obj){*this = pick(obj);} 33 Mesh& operator=(Mesh&& obj){ 34 name = obj.name; 35 loaded = obj.loaded; 36 37 material = obj.material; 38 39 vertices = pick(obj.vertices); 40 normals = pick(obj.normals); 41 colors = pick(obj.colors); 42 texCoords = pick(obj.texCoords); 43 44 vao = obj.vao; 45 verticesVBO = obj.verticesVBO; 46 normalsVBO = obj.normalsVBO; 47 colorsVBO = obj.colorsVBO; 48 texCoordsVBO = obj.texCoordsVBO; 49 50 obj.vertices.Clear(); 51 obj.normals.Clear(); 52 obj.colors.Clear(); 53 obj.texCoords.Clear(); 54 return *this; 55 } 56 57 //copy also VBO and VAO , wich mean when object is destroyed , every copy will 58 //lost their 3D representation in OpenGL Mesh(const Mesh & obj)59 Mesh(const Mesh& obj){*this = obj;} 60 Mesh& operator=(const Mesh& obj){ 61 name = obj.name; 62 loaded = obj.loaded; 63 64 material = obj.material; 65 vertices.Append(obj.vertices); 66 normals.Append(obj.normals); 67 colors.Append(obj.colors); 68 texCoords.Append(obj.texCoords); 69 70 vao = obj.vao; 71 verticesVBO = obj.verticesVBO; 72 normalsVBO = obj.normalsVBO; 73 colorsVBO = obj.colorsVBO; 74 texCoordsVBO = obj.texCoordsVBO; 75 return *this; 76 } ~Mesh()77 ~Mesh(){ 78 if(vertices.GetCount()){ 79 Clear(true); 80 } 81 } 82 SetTextureIndice(unsigned int indice)83 Mesh& SetTextureIndice(unsigned int indice){textureIndice = indice; return *this;} GetTextureIndice()84 unsigned int GetTextureIndice(){return textureIndice;} 85 GetMaterial()86 Material& GetMaterial(){return material;} 87 SetName(const String & n)88 void SetName(const String& n){name = n;} GetName()89 String GetName()const noexcept{return name;} 90 91 /*** Retireve all vector, to fille data ***/ GetVertices()92 Vector<float>& GetVertices(){return vertices;} GetNormals()93 Vector<float>& GetNormals(){return normals;} GetColors()94 Vector<float>& GetColors(){return colors;} GetTexCoords()95 Vector<float>& GetTexCoords(){return texCoords;} 96 GetVertices()97 const Vector<float>& GetVertices()const noexcept{return vertices;} GetNormals()98 const Vector<float>& GetNormals()const noexcept{return normals;} GetColors()99 const Vector<float>& GetColors()const noexcept{return colors;} GetTexCoords()100 const Vector<float>& GetTexCoords()const noexcept{return texCoords;} 101 GetVAO()102 GLuint GetVAO()const noexcept{return vao;} GetVerticesVBO()103 GLuint GetVerticesVBO()const noexcept{return verticesVBO;} GetNormalsVBO()104 GLuint GetNormalsVBO()const noexcept{return normalsVBO;} GetColorsVBO()105 GLuint GetColorsVBO()const noexcept{return colorsVBO;} GetTexCoordsVBO()106 GLuint GetTexCoordsVBO()const noexcept{return texCoordsVBO;} 107 Init()108 void Init()noexcept{ 109 if(!loaded){ 110 glGenVertexArrays(1, &vao); 111 glGenBuffers(1,&verticesVBO); 112 glGenBuffers(1,&normalsVBO); 113 glGenBuffers(1,&colorsVBO); 114 glGenBuffers(1,&texCoordsVBO); 115 116 glBindVertexArray(vao); 117 glBindBuffer(GL_ARRAY_BUFFER,verticesVBO); 118 glBufferData(GL_ARRAY_BUFFER,vertices.GetCount() * sizeof(float),(vertices.GetCount() > 0)? &(vertices[0]): NULL,GL_DYNAMIC_READ); 119 120 glBindBuffer(GL_ARRAY_BUFFER,normalsVBO); 121 glBufferData(GL_ARRAY_BUFFER,normals.GetCount() * sizeof(float),(normals.GetCount() > 0)? &(normals[0]): NULL,GL_DYNAMIC_READ); 122 123 glBindBuffer(GL_ARRAY_BUFFER,colorsVBO); 124 glBufferData(GL_ARRAY_BUFFER,colors.GetCount() * sizeof(float),(colors.GetCount() > 0)? &(colors[0]): NULL,GL_DYNAMIC_READ); 125 126 glBindBuffer(GL_ARRAY_BUFFER,texCoordsVBO); 127 glBufferData(GL_ARRAY_BUFFER,texCoords.GetCount() * sizeof(float),(texCoords.GetCount() > 0)? &(texCoords[0]): NULL ,GL_DYNAMIC_READ); 128 129 if(vertices.GetCount()){ 130 glBindBuffer(GL_ARRAY_BUFFER,verticesVBO); 131 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); 132 glEnableVertexAttribArray(0); 133 } 134 if(normals.GetCount()){ 135 glBindBuffer(GL_ARRAY_BUFFER,normalsVBO); 136 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); 137 glEnableVertexAttribArray(1); 138 } 139 if(colors.GetCount()){ 140 glBindBuffer(GL_ARRAY_BUFFER,colorsVBO); 141 glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); 142 glEnableVertexAttribArray(2); 143 } 144 if(texCoords.GetCount()){ 145 glBindBuffer(GL_ARRAY_BUFFER,texCoordsVBO); 146 glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); 147 glEnableVertexAttribArray(3); 148 } 149 150 loaded = true; 151 } 152 } 153 154 155 void Clear(bool ClearVector = false)noexcept{ //Clear openGL data 156 if(vao > 0) glDeleteVertexArrays(1,&vao); 157 if(verticesVBO > 0)glDeleteBuffers(1,&verticesVBO); 158 if(normalsVBO > 0)glDeleteBuffers(1,&normalsVBO); 159 if(colorsVBO > 0)glDeleteBuffers(1,&colorsVBO); 160 if(texCoordsVBO > 0)glDeleteBuffers(1,&texCoordsVBO); 161 vao = 0; 162 verticesVBO = 0; 163 normalsVBO = 0; 164 colorsVBO = 0; 165 texCoordsVBO = 0; 166 if(ClearVector){ 167 vertices.Clear(); 168 normals.Clear(); 169 colors.Clear(); 170 texCoords.Clear(); 171 } 172 loaded = false; 173 } 174 175 private: 176 String name =""; //Possible name of Mesh 177 bool loaded = false; 178 179 Vector<GLfloat> vertices; 180 Vector<GLfloat> normals; 181 Vector<GLfloat> colors; 182 Vector<GLfloat> texCoords; 183 184 GLuint vao = 0; 185 GLuint verticesVBO = 0; 186 GLuint normalsVBO = 0; 187 GLuint colorsVBO = 0; 188 GLuint texCoordsVBO = 0; 189 190 Material material; //The material object is a representation of material property of the object (it change how light affect it) 191 192 unsigned int textureIndice = 0; 193 }; 194 195 } 196 197 198 199 #endif 200