kick
|
00001 // 00002 // mesh_factory.cpp 00003 // KickCPP 00004 // 00005 // Created by Morten Nobel-Jørgensen on 05/12/13. 00006 // Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved. 00007 // 00008 00009 #include "kick/mesh/mesh_factory.h" 00010 #include"kick/core/cpp_ext.h" 00011 #include"kick/core/project.h" 00012 #include "glm/gtx/string_cast.hpp" 00013 #include <glm/gtc/constants.hpp> 00014 00015 using namespace std; 00016 using namespace glm; 00017 00018 namespace kick { 00019 shared_ptr<MeshData> MeshFactory::createPointData(){ 00020 auto meshData = make_shared<MeshData>(); 00021 meshData->setPosition({vec3{0,0,0}}); 00022 meshData->setSubmesh(0, {0}, MeshType::Points); 00023 meshData->recomputeBounds(); 00024 return meshData; 00025 } 00026 00027 shared_ptr<MeshData> MeshFactory::createTriangleData(){ 00028 auto meshData = make_shared<MeshData>(); 00029 float sqrt75 = glm::sqrt(0.75f); 00030 meshData->setPosition({ 00031 vec3{0,1,0}, 00032 vec3{-sqrt75,-0.5f,0}, 00033 vec3{sqrt75,-0.5f,0} 00034 }); 00035 meshData->setTexCoord0({ 00036 vec2{0.0f,1}, 00037 vec2{0.125f,0.25f}, 00038 vec2{1- 0.125f, 0.25f} 00039 }); 00040 meshData->setNormal({ 00041 vec3{0,0,1}, 00042 vec3{0,0,1}, 00043 vec3{0,0,1} 00044 }); 00045 meshData->setColor({ 00046 vec4{1,0,0,1}, 00047 vec4{0,1,0,1}, 00048 vec4{0,0,1,1} 00049 }); 00050 meshData->setSubmesh(0, {0,1,2}, MeshType::Triangles); 00051 meshData->recomputeBounds(); 00052 return meshData; 00053 } 00054 00055 shared_ptr<MeshData> MeshFactory::createDiscData(unsigned short slices){ 00056 auto meshData = make_shared<MeshData>(); 00057 size_t vertexCount = slices+1; 00058 vector<vec3> vertices{vertexCount}; 00059 vector<vec2> uvs{vertexCount}; 00060 vector<vec3> normals{vertexCount}; 00061 vector<vec4> colors{vertexCount}; 00062 vector<GLushort> indices(vertexCount); 00063 for (unsigned short i = 0; i <= slices; i++) { 00064 auto fraction = 2 * glm::pi<float>() * i / slices; 00065 vertices[i] = vec3{sin(fraction), 00066 -cos(fraction), 00067 0}; 00068 uvs[i] = vec2{sin(fraction) * 0.5 + 0.5 , -cos(fraction) * 0.5 + 0.5}; 00069 colors[i] = vec4{sin(fraction) * 0.5 + 0.5 , -cos(fraction) * 0.5 + 0.5,0,1}; 00070 normals[i] = vec3{0}; 00071 indices[i] = i; 00072 } 00073 meshData->setPosition(vertices); 00074 meshData->setTexCoord0(uvs); 00075 meshData->setNormal(normals); 00076 meshData->setColor(colors); 00077 meshData->setSubmesh(0, indices, MeshType::TriangleFan); 00078 meshData->recomputeBounds(); 00079 return meshData; 00080 } 00081 00082 shared_ptr<MeshData> MeshFactory::createPlaneData(){ 00083 auto meshData = make_shared<MeshData>(); 00084 00085 meshData->setPosition({ 00086 vec3{1, -1, 0}, 00087 vec3{1, 1, 0}, 00088 vec3{-1, -1, 0}, 00089 vec3{-1, 1, 0} 00090 }); 00091 meshData->setTexCoord0({ 00092 vec2{1, 0}, 00093 vec2{1, 1}, 00094 vec2{0, 0}, 00095 vec2{0, 1} 00096 }); 00097 meshData->setNormal({ 00098 vec3{0, 0, 1}, 00099 vec3{0, 0, 1}, 00100 vec3{0, 0, 1}, 00101 vec3{0, 0, 1} 00102 }); 00103 meshData->setColor({ 00104 vec4{1, 0, 0, 1}, 00105 vec4{0, 1, 0, 1}, 00106 vec4{0, 0, 1, 1}, 00107 vec4{1, 1, 1, 1} 00108 }); 00109 meshData->setSubmesh(0, {0, 1, 2, 2, 1, 3}, MeshType::Triangles); 00110 meshData->recomputeBounds(); 00111 00112 return meshData; 00113 } 00114 00115 shared_ptr<MeshData> MeshFactory::createUVSphereData(unsigned short slices, unsigned short stacks, float radius){ 00116 auto meshData = make_shared<MeshData>(); 00117 size_t vertexCount = (stacks+1) * (slices + 1); 00118 vector<vec3> vertices{vertexCount}; 00119 vector<vec2> uvs{vertexCount}; 00120 vector<vec3> normals{vertexCount}; 00121 vector<vec4> colors{vertexCount}; 00122 vector<GLushort> indices; 00123 00124 int index = 0; 00125 // create vertices 00126 for (unsigned short j = 0; j <= stacks; j++) { 00127 float latitude1 = (glm::pi<float>() / stacks) * j - (glm::pi<float>() / 2); 00128 float sinLat1 = sin(latitude1); 00129 float cosLat1 = cos(latitude1); 00130 for (int i = 0; i <= slices; i++) { 00131 float longitude = ((glm::pi<float>() * 2) / slices) * i; 00132 float sinLong = sin(longitude); 00133 float cosLong = cos(longitude); 00134 vec3 normal{cosLong * cosLat1, 00135 sinLat1, 00136 sinLong * cosLat1}; 00137 normal = normalize(normal); 00138 normals[index] = normal; 00139 uvs[index] = vec2{1 - i /(float) slices, j /(float) stacks}; 00140 vertices[index] = normal * radius; 00141 colors[index] = vec4(normal*0.5f+vec3(0.5f), 1.0f); 00142 index++; 00143 } 00144 } 00145 // create indices 00146 for (int j = 0; j < stacks; j++) { 00147 if (j > 0) { 00148 indices.push_back(j * (slices + 1)); // make degenerate 00149 } 00150 for (int i = 0; i <= slices; i++) { 00151 index = j * (slices + 1) + i; 00152 indices.push_back(index); 00153 indices.push_back(index + slices + 1); 00154 } 00155 if (j + 1 < stacks) { 00156 indices.push_back(index + slices + 1); // make degenerate 00157 } 00158 } 00159 00160 meshData->setPosition(vertices); 00161 meshData->setTexCoord0(uvs); 00162 meshData->setNormal(normals); 00163 meshData->setColor(colors); 00164 meshData->setSubmesh(0, indices, MeshType::TriangleStrip); 00165 meshData->recomputeBounds(); 00166 return meshData; 00167 } 00168 00169 shared_ptr<MeshData> MeshFactory::createCubeData(float length){ 00170 auto meshData = make_shared<MeshData>(); 00171 // v6----- v5 00172 // /| /| 00173 // v1------v0| 00174 // | | | | 00175 // | |v7---|-|v4 00176 // |/ |/ 00177 // v2------v3 00178 meshData->setPosition({ 00179 vec3{length, length, length}, 00180 vec3{-length, length, length}, 00181 vec3{-length, -length, length}, 00182 vec3{length, -length, length}, // v0-v1-v2-v3 00183 vec3{length, length, length}, 00184 vec3{length, -length, length}, 00185 vec3{length, -length, -length}, 00186 vec3{length, length, -length}, // v0-v3-v4-v5 00187 vec3{length, length, length}, 00188 vec3{length, length, -length}, 00189 vec3{-length, length, -length}, 00190 vec3{-length, length, length}, // v0-v5-v6-v1 00191 vec3{-length, length, length}, 00192 vec3{-length, length, -length}, 00193 vec3{-length, -length, -length}, 00194 vec3{-length, -length, length}, // v1-v6-v7-v2 00195 vec3{-length, -length, -length}, 00196 vec3{length, -length, -length}, 00197 vec3{length, -length, length}, 00198 vec3{-length, -length, length}, // v7-v4-v3-v2 00199 vec3{length, -length, -length}, 00200 vec3{-length, -length, -length}, 00201 vec3{-length, length, -length}, 00202 vec3{length, length, -length} // v4-v7-v6-v5 00203 }); 00204 meshData->setTexCoord0({ 00205 vec2{1, 1}, 00206 vec2{0, 1}, 00207 vec2{0, 0}, 00208 vec2{1, 0}, // v0-v1-v2-v3 00209 vec2{0, 1}, 00210 vec2{0, 0}, 00211 vec2{1, 0}, 00212 vec2{1, 1}, // v0-v3-v4-v5 00213 vec2{1, 0}, 00214 vec2{1, 1}, 00215 vec2{0, 1}, 00216 vec2{0, 0}, // v0-v5-v6-v1 (top) 00217 vec2{1, 1}, 00218 vec2{0, 1}, 00219 vec2{0, 0}, 00220 vec2{1, 0}, // v1-v6-v7-v2 00221 vec2{1, 1}, 00222 vec2{0, 1}, 00223 vec2{0, 0}, 00224 vec2{1, 0}, // v7-v4-v3-v2 (bottom) 00225 vec2{0, 0}, 00226 vec2{1, 0}, 00227 vec2{1, 1}, 00228 vec2{0, 1} // v4-v7-v6-v5 00229 }); 00230 meshData->setNormal({ 00231 vec3{0, 0, 1}, 00232 vec3{0, 0, 1}, 00233 vec3{0, 0, 1}, 00234 vec3{0, 0, 1}, // v0-v1-v2-v3 00235 vec3{1, 0, 0}, 00236 vec3{1, 0, 0}, 00237 vec3{1, 0, 0}, 00238 vec3{1, 0, 0}, // v0-v3-v4-v5 00239 vec3{0, 1, 0}, 00240 vec3{0, 1, 0}, 00241 vec3{0, 1, 0}, 00242 vec3{0, 1, 0}, // v0-v5-v6-v1 00243 vec3{-1, 0, 0}, 00244 vec3{-1, 0, 0}, 00245 vec3{-1, 0, 0}, 00246 vec3{-1, 0, 0}, // v1-v6-v7-v2 00247 vec3{0, -1, 0}, 00248 vec3{0, -1, 0}, 00249 vec3{0, -1, 0}, 00250 vec3{0, -1, 0}, // v7-v4-v3-v2 00251 vec3{0, 0, -1}, 00252 vec3{0, 0, -1}, 00253 vec3{0, 0, -1}, 00254 vec3{0, 0, -1} // v4-v7-v6-v5 00255 }); 00256 meshData->setColor({ 00257 vec4{1, 1, 1, 1}, 00258 vec4{1, 1, 0, 1}, 00259 vec4{1, 0, 0, 1}, 00260 vec4{1, 0, 1, 1}, // v0-v1-v2-v3 00261 vec4{1, 1, 1, 1}, 00262 vec4{1, 0, 1, 1}, 00263 vec4{0, 0, 1, 1}, 00264 vec4{0, 1, 1, 1}, // v0-v3-v4-v5 00265 vec4{1, 1, 1, 1}, 00266 vec4{0, 1, 1, 1}, 00267 vec4{0, 1, 0, 1}, 00268 vec4{1, 1, 0, 1}, // v0-v5-v6-v1 00269 vec4{1, 1, 0, 1}, 00270 vec4{0, 1, 0, 1}, 00271 vec4{0, 0, 0, 1}, 00272 vec4{1, 0, 0, 1}, // v1-v6-v7-v2 00273 vec4{0, 0, 0, 1}, 00274 vec4{0, 0, 1, 1}, 00275 vec4{1, 0, 1, 1}, 00276 vec4{1, 0, 0, 1}, // v7-v4-v3-v2 00277 vec4{0, 0, 1, 1}, 00278 vec4{0, 0, 0, 1}, 00279 vec4{0, 1, 0, 1}, 00280 vec4{0, 1, 1, 1} // v4-v7-v6-v5 00281 }); 00282 meshData->setSubmesh(0, { 00283 0, 1, 2, 00284 0, 2, 3, 00285 4, 5, 6, 00286 4, 6, 7, 00287 8, 9, 10, 00288 8, 10, 11, 00289 12, 13, 14, 00290 12, 14, 15, 00291 16, 17, 18, 00292 16, 18, 19, 00293 20, 21, 22, 00294 20, 22, 23}, 00295 MeshType::Triangles); 00296 meshData->recomputeBounds(); 00297 return meshData; 00298 } 00299 }