Geometry::Geometry(const std::string& filePath) : m_VertexData{ false }, m_Ibo{ false }, m_Vao{false} { /* ********************** load geometry from file ********************** */ //final arrays for vbo and ibo //std::vector<GLfloat> vertexData; //std::vector<GLushort> indices; try { parseObjFile(filePath, m_vertexDataRAM, m_indicesRAM); } catch (const std::exception& ex) { throw std::exception((std::string("Parsing .obj file failed: ") + ex.what()).c_str()); } //push data to gpu m_Vao.bind(); m_VertexData.pushData(m_vertexDataRAM, GL_STATIC_DRAW, true); m_Ibo.pushData(m_indicesRAM, GL_STATIC_DRAW, true); m_Vao.unbind(); m_numberOfElements = static_cast<GLsizei>(m_indicesRAM.size()); }
MeshObj::MeshObj(const char* objPath, Shader& shaderProg) { meshPath = objPath; shader = &shaderProg; parseObjFile(meshPath, vPosArr, vTexArr, vNormArr, vIdxArr, vTexIdxArr, vNormIdxArr); initMeshStructure(); #if SHOW printVertexData(); #endif } // end constructor
void Config::load(SceneRenderer * sceneRenderer, Manager * manager) { Json::Value root; // will contains the root value after parsing. Json::Reader reader; std::ifstream file; file.open(fileName); bool parsingSuccessful = reader.parse(file, root, false); file.close(); if(!parsingSuccessful) { // report to the user the failure and their locations in the document. System::Console::WriteLine("Failed to parse configuration\n"); System::Console::WriteLine(gcnew System::String(reader.getFormatedErrorMessages().c_str())); throw std::runtime_error("Failed to parse configuration file: "+fileName); } Json::Value sceneJSON = root.get("scene", NULL); Scene * scene = sceneRenderer->getScene(); Json::Value bgColorJSON = sceneJSON.get("backgroundColor", NULL); if(bgColorJSON != NULL) { scene->setBackgroundColor(jsonToColor(bgColorJSON)); } Json::Value lightsJSON = sceneJSON.get("lights", NULL); if(lightsJSON != NULL) { Json::Value::Members lightsName = lightsJSON.getMemberNames(); for(std::vector<std::string>::iterator it = lightsName.begin(); it != lightsName.end(); it++) { std::string lightName = *it; Json::Value lightJSON = lightsJSON[lightName]; RGBColor lightColor = jsonToColor(lightJSON["color"]); LightSource * light; std::string lightTypeStr = lightJSON["type"].asString(); if(lightTypeStr == "AmbientLightSource") { light = new AmbientLightSource(lightColor); } else if(lightTypeStr == "PointLightSource") { light = new PointLightSource(jsonToP3(lightJSON["position"]), lightColor); Json::Value specularCoefJSON = lightJSON.get("specularCoef", NULL); PointLightSource * pLight = static_cast<PointLightSource*>(light); if(specularCoefJSON != NULL) { pLight->setSpecularCoef(specularCoefJSON.asDouble()); } Json::Value specularExpJSON = lightJSON.get("specularExponent", NULL); if(specularExpJSON != NULL) { pLight->setSpecularExponent(specularExpJSON.asDouble()); } } Json::Value diffuseCoefJSON = lightJSON.get("diffuseCoef", NULL); if(diffuseCoefJSON != NULL) { light->setDiffuseCoef(diffuseCoefJSON.asDouble()); } //scene->addLightSource(light); manager->getLightSources()->add(lightName, light, lightTypeStr); } } Json::Value objectsJSON = sceneJSON.get("objects", NULL); if(objectsJSON != NULL) { Json::Value::Members objectsName = objectsJSON.getMemberNames(); for(std::vector<std::string>::iterator it = objectsName.begin(); it != objectsName.end(); it++) { std::string objectName = *it; Json::Value object3DJSON = objectsJSON[objectName]; Object3D * object3D; std::string oject3DTypeStr = object3DJSON["type"].asString(); if(oject3DTypeStr == "Sphere") { object3D = new Sphere(jsonToP3(object3DJSON["center"]), object3DJSON["radius"].asDouble()); } else if(oject3DTypeStr == "Plane") { if(object3DJSON.get("u", NULL) != NULL) { object3D = new Plane(jsonToP3(object3DJSON["p"]), jsonToP3(object3DJSON["u"]), jsonToP3(object3DJSON["v"])); } else { object3D = new Plane(jsonToP3(object3DJSON["p"]), jsonToP3(object3DJSON["normal"])); } } else if(oject3DTypeStr == "Triangle") { object3D = new Triangle(jsonToP3(object3DJSON["A"]), jsonToP3(object3DJSON["B"]), jsonToP3(object3DJSON["C"])); } //scene->addObject3D(object3D); manager->getObjects3D()->add(objectName, object3D, oject3DTypeStr); } } Json::Value polyhedraJSON = sceneJSON.get("polyhedra", NULL); if(polyhedraJSON != NULL) { Json::Value::Members polyhedraName = polyhedraJSON.getMemberNames(); for(std::vector<std::string>::iterator it = polyhedraName.begin(); it != polyhedraName.end(); it++) { std::string objectName = *it; Json::Value polyhedronJSON = polyhedraJSON[objectName]; Polyhedron * polyhedron; std::string polyhedronTypeStr = polyhedronJSON["type"].asString(); if(polyhedronTypeStr == "Parallelepiped") { polyhedron = new Parallelepiped(jsonToP3(polyhedronJSON["A"]), jsonToP3(polyhedronJSON["B"]), jsonToP3(polyhedronJSON["C"]), jsonToP3(polyhedronJSON["D"])); } else if(polyhedronTypeStr == "Maya") { std::vector<Triangle*> triangles; std::string filename = polyhedronJSON["FileName"].asString(); Json::Value pJSON = polyhedronJSON.get("p", NULL); P3 centerObj(0,0,0); if(pJSON != NULL) centerObj = jsonToP3(polyhedronJSON["p"]); Json::Value scaleJSON = polyhedronJSON.get("scale", NULL); double scale = 1.0; if(scaleJSON != NULL) scale = scaleJSON.asDouble(); parseObjFile(filename, &triangles, centerObj, scale); polyhedron = new Polyhedron(triangles); } else continue; /*std::vector<Triangle*> triangles = polyhedron->getTriangles(); for(std::vector<Triangle*>::iterator it = triangles.begin(); it != triangles.end(); it++) { scene->addObject3D(*it); }*/ manager->getPolyhedra()->add(objectName, polyhedron, polyhedronTypeStr); } } Json::Value texturesJSON = root.get("textures", NULL); if(texturesJSON != NULL) { Json::Value::Members texturesName = texturesJSON.getMemberNames(); for(std::vector<std::string>::iterator it = texturesName.begin(); it != texturesName.end(); it++) { std::string textureName = *it; Json::Value textureJSON = texturesJSON[textureName]; Texture * texture = new Texture(textureJSON["FileName"].asString()); manager->getTextures()->add(textureName, texture); } } Json::Value sceneRendererJSON = root.get("sceneRenderer", NULL); if(sceneRendererJSON == NULL) throw std::runtime_error("sceneRenderer not defined in "+fileName); Json::Value cameraJSON = sceneRendererJSON["camera"]; Camera * camera = new Camera(jsonToP3(cameraJSON["position"]), jsonToP3(cameraJSON["direction"]), cameraJSON.get("rotation", 0).asDouble()); sceneRenderer->setCamera(camera); sceneRenderer->setCameraScreenDist(sceneRendererJSON.get("cameraScreenDist", 200).asDouble()); manager->getCameras()->add("camera1", camera); std::string methodType = sceneRendererJSON["method"].get("type", "OrthographicProjection").asString(); RenderingMethod * renderingMethod = NULL; if(methodType == "OrthographicProjection") { renderingMethod = new OrthographicProjection(); } else if(methodType == "RayCasting") { renderingMethod = new RayCasting(); } else if(methodType == "RayTracing") { renderingMethod = new RayTracing(); } if(renderingMethod != NULL) sceneRenderer->setRenderingMethod(renderingMethod); Json::Value modelsJSON = sceneRendererJSON.get("models", NULL); if(modelsJSON != NULL) { Json::Value::Members modelsName = modelsJSON.getMemberNames(); for(std::vector<std::string>::iterator it = modelsName.begin(); it != modelsName.end(); it++) { std::string modelName = *it; Json::Value modelJSON = modelsJSON[modelName]; Json::Value modelTypeJSON = modelJSON.get("type", NULL); if(modelTypeJSON != NULL) { std::string modelTypeStr = modelTypeJSON.asString(); Model * model; if(modelTypeStr == "SphereModel") { model = new SphereModel(); } else if( modelTypeStr == "PlaneModel" || modelTypeStr == "TriangleModel") { model = new PlaneModel(); } else if(modelTypeStr == "PolyhedronModel" || modelTypeStr == "ParallelepipedModel" || modelTypeStr == "MayaModel") { model = new PolyhedronModel(); } Json::Value textureJSON = modelJSON.get("texture", NULL); if(textureJSON != NULL) { model->setTexture(manager->getTextures()->get(textureJSON.asString())); } Json::Value colorJSON = modelJSON.get("color", NULL); if(colorJSON != NULL) { model->setColor(jsonToColor(colorJSON)); } Json::Value bumpJSON = modelJSON.get("bump", NULL); if(bumpJSON != NULL) { model->setBump(manager->getTextures()->get(bumpJSON.asString())); } Json::Value materialJSON = modelJSON.get("material", NULL); if(materialJSON != NULL) { model->setMaterial(jsonToMaterial(materialJSON)); } Json::Value textureScaleJSON = modelJSON.get("textureScale", NULL); if(textureScaleJSON != NULL) { model->setTextureScale(textureScaleJSON.asDouble()); } manager->getModels()->add(modelName, model); Json::Value modelObjectsJSON = modelJSON.get("objects", NULL); if(modelObjectsJSON != NULL) { for(unsigned int i=0; i < modelObjectsJSON.size(); i++) { std::string objectName = modelObjectsJSON[i].asString(); Object3D * object3D = manager->getObjects3D()->get(objectName); Polyhedron * polyhedron = manager->getPolyhedra()->get(objectName); if(object3D != NULL) { sceneRenderer->getObject3DRenderer(object3D)->setModel(model); } else if(polyhedron != NULL) { std::vector<Triangle*> triangles = polyhedron->getTriangles(); for(std::vector<Triangle*>::iterator it = triangles.begin(); it != triangles.end(); it++) { sceneRenderer->getObject3DRenderer(*it)->setModel(model); } } else continue; } } } } } }
Scene* parseScene(std::string filename) { Scene* s = parseObjFile(filename); filename.replace(filename.length() - 3, 3, "mtl"); parseMtlFile(filename, s); return s; }