BOOLEAN loadAddon (const char *addon) { uio_DirHandle *addonsDir, *addonDir; int numLoaded; addonsDir = uio_openDirRelative (contentDir, "addons", 0); if (addonsDir == NULL) { // No addon dir found. log_add (log_Warning, "Warning: There's no 'addons' " "directory in the 'content' directory;\n\t'--addon' " "options are ignored."); return FALSE; } addonDir = uio_openDirRelative (addonsDir, addon, 0); if (addonDir == NULL) { log_add (log_Warning, "Warning: Addon '%s' not found", addon); uio_closeDir (addonsDir); return FALSE; } numLoaded = loadIndices (addonDir); uio_closeDir (addonDir); uio_closeDir (addonsDir); return (numLoaded > 0); }
Mesh* Mesh::create(std::vector<char> vertexData, size_t vertexCount, std::vector<VertexElement> vertexLayout, std::vector<uint32_t> indices, PrimitiveType primitiveType) { auto mesh = new Mesh(); mesh->setPrimitiveType(primitiveType); mesh->loadVertices(std::move(vertexData), vertexCount, std::move(vertexLayout)); mesh->loadIndices(std::move(indices)); return mesh; }
/** * @brief Define a unitary sphere geometry * * Sphere is created by starting with an octahedron and subdividing triangles * for a nice reference see: https://sites.google.com/site/dlampetest/python/triangulating-a-sphere-recursively * No need to set normal, just use the position in shader since it is the same */ void createGeometry (int subdivisions = 4) { vector< Eigen::Vector4f > vert; vector< GLuint > faces; vert.push_back ( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) ); vert.push_back ( Eigen::Vector4f(-1.0, 0.0, 0.0, 1.0) ); vert.push_back ( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) ); vert.push_back ( Eigen::Vector4f( 0.0,-1.0, 0.0, 1.0) ); vert.push_back ( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) ); vert.push_back ( Eigen::Vector4f( 0.0, 0.0,-1.0, 1.0) ); int a[24] = { 0, 4, 2, 2, 4, 1, 1, 4, 3, 3, 4, 0, 0, 2, 5, 2, 1, 5, 1, 3, 5, 3, 0, 5 }; faces.insert(faces.end(), a, a+24); // now subdivide, divide each triangle into 4 for (int s = 0; s < subdivisions; ++s) { vector< GLuint > sub_faces; for (int i = 0; i < (int)faces.size(); i=i+3) { Eigen::Vector4f p0 = vert[faces[i+0]]; Eigen::Vector4f p1 = vert[faces[i+1]]; Eigen::Vector4f p2 = vert[faces[i+2]]; Eigen::Vector4f p3 = (p0 + p1)*0.5; Eigen::Vector4f p4 = (p0 + p2)*0.5; Eigen::Vector4f p5 = (p1 + p2)*0.5; p3.head(3).normalize(); p4.head(3).normalize(); p5.head(3).normalize(); vert.push_back(p3); vert.push_back(p4); vert.push_back(p5); int ind = vert.size()-3; // new faces are: (p0, p4, p3), (p4, p1, p5), (p3, p4, p5), (p3, p5, p2) int b[12] = {(int)faces[i+0], ind, ind+1, ind+1, ind+2, (int)faces[i+2], ind, ind+2, ind+1, ind, (int)faces[i+1], ind+2}; sub_faces.insert(sub_faces.end(), b, b+12); } faces.clear(); faces = sub_faces; } loadVertices(vert); loadIndices(faces); setDefaultAttribLocations(); }
GRAPHICS::RESOURCES::Mesh* AssetImporter::loadMeshes(ID3D11Device* d3dDevice, const aiScene* scene) { if (!scene->HasMeshes()) return nullptr; GRAPHICS::RESOURCES::Mesh* meshArr = new GRAPHICS::RESOURCES::Mesh[scene->mNumMeshes]; for (uint_fast32_t i = 0; i < scene->mNumMeshes; ++i) { GRAPHICS::RESOURCES::Vertex* vertexArr = nullptr; unsigned int geomStreamLength = 0; loadVertices(scene->mMeshes[i], vertexArr, geomStreamLength); GRAPHICS::D3D11Utility::createBuffer<GRAPHICS::RESOURCES::Vertex>(d3dDevice, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_IMMUTABLE, vertexArr, geomStreamLength, meshArr[i].geomBuffer); GRAPHICS::RESOURCES::Index* indexArr = nullptr; unsigned int indexArrLength = 0; D3D_PRIMITIVE_TOPOLOGY primitiveTopology; loadIndices(scene->mMeshes[i], indexArr, indexArrLength, primitiveTopology); meshArr[i].primitiveTopology = primitiveTopology; GRAPHICS::D3D11Utility::createBuffer<GRAPHICS::RESOURCES::Index>(d3dDevice, D3D11_BIND_INDEX_BUFFER, D3D11_USAGE_IMMUTABLE, indexArr, indexArrLength, meshArr[i].indexBuffer); } return meshArr; }
void CitySceneGenerator::generate(Scene* scene) { auto renderer = scene->renderer(); auto material = std::make_shared<PhongMaterial>(renderer); material->setShader(renderer->shaderManager()->getGlslProgram("phong")); PhongMaterialData materialData = { glm::vec4(0.0f, 0.1f, 0.0f, 1.0f), glm::vec4(0.8f, 0.3f, 0.1f, 1.0f), glm::vec4(0.3f, 0.3f, 0.3f, 1.0f), 5.0f }; material->properties()->setData(materialData); material->properties()->flushData(); auto mesh = std::make_shared<Mesh>(); mesh->setPrimitiveType(PrimitiveType::TriangleList); size_t buildingVerticesCount = sizeof(buildingVertices) / sizeof(*buildingVertices); std::vector<char> vertices(reinterpret_cast<const char*>(buildingVertices), reinterpret_cast<const char*>(buildingVertices) + sizeof(buildingVertices)); std::vector<VertexElement> layout = { VertexElement(3, VertexElementType::Float), VertexElement(3, VertexElementType::Float) }; mesh->loadVertices(vertices, buildingVerticesCount, layout); size_t buildingIndicesCount = sizeof(buildingIndices) / sizeof(*buildingIndices); std::vector<uint32_t> indices(reinterpret_cast<const unsigned*>(buildingIndices), reinterpret_cast<const unsigned*>(buildingIndices) + buildingIndicesCount); mesh->loadIndices(indices); size_t numBuildings = 1000; float citySize = 500.0f; float minBuildingSize = 10.0f; float maxBuildingSize = 60.0f; float minHeightToWidthRatio = 8.0f; float maxHeightToWidthRatio = 16.0f; std::uniform_real_distribution<float> angleDist(0.0f, 360.0f); std::uniform_real_distribution<float> positionDist(-citySize, citySize); std::uniform_real_distribution<float> canonicalDist; std::vector<std::shared_ptr<BaseSceneObject>> buildings; for (size_t i = 0; i < numBuildings; i++) { auto building = std::make_shared<Building>(mesh, material); // set random position glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(positionDist(m_rng), positionDist(m_rng), 0.0f) ); // rotate around z with random angle model = glm::rotate(model, angleDist(m_rng), glm::vec3(0.0f, 0.0f, 1.0f)); glm::vec3 scale; // multiplying uniform distribution will generate beta distribution scale.x = canonicalDist(m_rng) * canonicalDist(m_rng) * canonicalDist(m_rng) * canonicalDist(m_rng) * (maxBuildingSize - minBuildingSize) + minBuildingSize; scale.y = scale.x; scale.z = canonicalDist(m_rng) * canonicalDist(m_rng) * canonicalDist(m_rng) * scale.x * (maxHeightToWidthRatio - minHeightToWidthRatio) + minHeightToWidthRatio; model = glm::scale(model, scale); building->setModelMatrix(model); building->calculateBBox(); buildings.push_back(building); } scene->setStaticGeometry(std::move(buildings)); }
std::vector<Index> *Table::getIndices() { loadIndices(); return &indicesM; }
BasicMesh* MeshManager::loadMesh(const std::string& fileName, const std::string meshName) { shared_ptr<GameFile> file = GameFile::openFile(fileName, "r"); std::string readLine; BasicMesh* result = new BasicMesh(); std::string collisionLine; while(!(file->eof())) { readLine = file->readLine(); if(readLine.size() == 0 || readLine == "\n") { continue; } if(readLine.find(NAME_SECTION) == 0) { loadName(result, readLine); continue; } else if(readLine.find(VERTICES_SECTION) == 0) { loadVertices(result, readLine, file.get()); continue; } else if(readLine.find(UVS_SECTION) == 0) { loadUvs(result, readLine, file.get()); continue; } else if(readLine.find(NORMALS_SECTION) == 0) { loadNormals(result, readLine, file.get()); continue; } else if(readLine.find(INDICES_SECTION) == 0) { loadIndices(result, readLine, file.get()); continue; } else if(readLine.find(COLLISION_SECTION) == 0) { collisionLine = readLine; // it has to be calculated after all the geometry continue; } else { Log("Skipping unknown line in mesh file - %s", readLine.c_str()); } } if(collisionLine.size() != 0) { setCollision(result, collisionLine); } else { // by default create box collision result->setCollisionType(CS_BOX); result->m_collisionShape = createCollisionShape(result); } return result; }