int scene::loadMaterial(string materialid){ int id = atoi(materialid.c_str()); if(id!=materials.size()){ cout << "ERROR: MATERIAL ID does not match expected number of materials" << endl; return -1; }else{ cout << "Loading Material " << id << "..." << endl; material newMaterial; //load static properties for(int i=0; i<12; i++){ string line; utilityCore::safeGetline(fp_in,line); vector<string> tokens = utilityCore::tokenizeString(line); if(strcmp(tokens[0].c_str(), "RGB")==0){ glm::vec3 color( atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()) ); newMaterial.color = color; }else if(strcmp(tokens[0].c_str(), "SPECEX")==0){ newMaterial.specularExponent = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "SPECRGB")==0){ glm::vec3 specColor( atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()) ); newMaterial.specularColor = specColor; }else if(strcmp(tokens[0].c_str(), "REFL")==0){ newMaterial.hasReflective = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "REFR")==0){ newMaterial.hasRefractive = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "REFRIOR")==0){ newMaterial.indexOfRefraction = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "SCATTER")==0){ newMaterial.hasScatter = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "ABSCOEFF")==0){ glm::vec3 abscoeff( atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()) ); newMaterial.absorptionCoefficient = abscoeff; }else if(strcmp(tokens[0].c_str(), "RSCTCOEFF")==0){ newMaterial.reducedScatterCoefficient = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "EMITTANCE")==0){ newMaterial.emittance = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "DIFFCOEFF")==0){ newMaterial.diffuseCoefficient = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "SPECCOEFF")==0){ newMaterial.specularCoefficient = atof(tokens[1].c_str()); } } materials.push_back(newMaterial); return 1; } }
int Scene::loadMaterial(string materialid) { int id = atoi(materialid.c_str()); if (id != materials.size()) { cout << "ERROR: MATERIAL ID does not match expected number of materials" << endl; return -1; } else { cout << "Loading Material " << id << "..." << endl; Material newMaterial; //load static properties for (int i = 0; i < 7; i++) { string line; utilityCore::safeGetline(fp_in, line); vector<string> tokens = utilityCore::tokenizeString(line); if (strcmp(tokens[0].c_str(), "RGB") == 0) { glm::vec3 color( atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()) ); newMaterial.color = color; } else if (strcmp(tokens[0].c_str(), "SPECEX") == 0) { newMaterial.specular.exponent = atof(tokens[1].c_str()); } else if (strcmp(tokens[0].c_str(), "SPECRGB") == 0) { glm::vec3 specColor(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str())); newMaterial.specular.color = specColor; } else if (strcmp(tokens[0].c_str(), "REFL") == 0) { newMaterial.hasReflective = atof(tokens[1].c_str()); } else if (strcmp(tokens[0].c_str(), "REFR") == 0) { newMaterial.hasRefractive = atof(tokens[1].c_str()); } else if (strcmp(tokens[0].c_str(), "REFRIOR") == 0) { newMaterial.indexOfRefraction = atof(tokens[1].c_str()); } else if (strcmp(tokens[0].c_str(), "EMITTANCE") == 0) { newMaterial.emittance = atof(tokens[1].c_str()); } } string line; utilityCore::safeGetline(fp_in, line); while (!line.empty() && fp_in.good()) { vector<string> tokens = utilityCore::tokenizeString(line); if (strcmp(tokens[0].c_str(), "SSS") == 0) { newMaterial.hasSSS = atof(tokens[1].c_str()); } utilityCore::safeGetline(fp_in, line); } materials.push_back(newMaterial); return 1; } }
Color bkgl::calculateSpecularColor(cml:: vector4f p, cml::vector4f normal) { Color specColor(0,0,0,0); for (int i=0; i<8; i++) { Light l = gllights[i]; if (l.isEnabled()) { if (cml::dot(normal, (l.getPosition()-p).normalize()) > 0) { cml::vector4f halfway = ((l.getPosition()-p).normalize() + cml::vector4f(0,0,1,0)).normalize(); specColor += max(0.0, (double)pow(cml::dot(normal, halfway), curShininess)) * elementwise_mult(curShineColor, l.getSpecular()); } } } return specColor; }
void CVK::Node::load(std::string path) { // load a scene with ASSIMP Assimp::Importer importer; const aiScene* scene = importer.ReadFile(path, aiProcess_GenSmoothNormals | aiProcess_Triangulate); if(scene) printf("SUCCESS: Loaded Model from Path: \"%s\"\n", path.c_str()); else { printf("ERROR: Loading failed from Path: \"%s\"\n", path.c_str()); return; } std::vector<CVK::Material*> materials; // load all materials in this file for(unsigned int i=0; i < scene->mNumMaterials; i++) { aiMaterial* mat = scene->mMaterials[i]; aiColor3D diffColor (0.f,0.f,0.f); mat->Get(AI_MATKEY_COLOR_DIFFUSE, diffColor); aiColor3D specColor (0.f,0.f,0.f); mat->Get(AI_MATKEY_COLOR_SPECULAR, specColor); float shininess = 0.0f; mat->Get(AI_MATKEY_SHININESS, shininess); glm::vec3 diffuseColor(diffColor.r, diffColor.g, diffColor.b); glm::vec3 specularColor(specColor.r, specColor.g, specColor.b); aiString fileName; // filename mat->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), fileName); std::string s = path.substr(0, path.rfind("/")) + "/"; s += fileName.data; //materials.push_back(new CVK::Material(glm::vec3(0.0,1.0,0.0), glm::vec3(0.0,0.0,1.0), 10)); if (fileName.length>0) materials.push_back(new CVK::Material( const_cast<char*> ( s.c_str() ), 1.f, 1.f, specularColor, shininess)); //should set kd and ks!! else materials.push_back(new CVK::Material(diffuseColor, specularColor, shininess)); } // load all meshes in this file for(unsigned int i=0; i < scene->mNumMeshes; i++) { aiMesh* mesh = scene->mMeshes[i]; CVK::Geometry* geometry = new CVK::Geometry(); // load geometry information of the current mesh for(unsigned int vCount = 0; vCount < mesh->mNumVertices; vCount++) { // vertices aiVector3D v = mesh->mVertices[vCount]; geometry->getVertices()->push_back( glm::vec4(v.x, v.y, v.z, 1.0f)); // normals if (mesh->HasNormals()) { v = mesh->mNormals[vCount]; geometry->getNormals()->push_back( glm::vec3(v.x, v.y, v.z)); } // texture coordinates if (mesh->HasTextureCoords(0)) { v = mesh->mTextureCoords[0][vCount]; geometry->getUVs()->push_back( glm::vec2(v.x, v.y)); } } for(unsigned int fCount = 0; fCount < mesh->mNumFaces; fCount++) { aiFace f = mesh->mFaces[fCount]; // index numbers for(unsigned int iCount = 0; iCount < f.mNumIndices; iCount++) { geometry->getIndex()->push_back(f.mIndices[iCount]); } } geometry->createBuffers(); // new child node with the geometry and a connection to material CVK::Node* child = new CVK::Node(); child->setGeometry(geometry); child->setMaterial(materials[mesh->mMaterialIndex]); addChild(child); } }
int scene::loadMaterial(string materialid){ int id = atoi(materialid.c_str()); if(id!=materials.size()){ cout << "ERROR: MATERIAL ID does not match expected number of materials" << endl; return -1; }else{ cout << "Loading Material " << id << "..." << endl; material newMaterial; newMaterial.hasTexture = false; newMaterial.Texture.texelHeight = 0; newMaterial.Texture.texelWidth = 0; newMaterial.hasNormalMap = false; newMaterial.NormalMap.texelHeight = 0; newMaterial.NormalMap.texelWidth = 0; //load static properties for(int i=0; i<10; i++){ string line; utilityCore::safeGetline(fp_in,line); vector<string> tokens = utilityCore::tokenizeString(line); if(strcmp(tokens[0].c_str(), "RGB")==0){ glm::vec3 color( atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()) ); newMaterial.color = color; }else if(strcmp(tokens[0].c_str(), "SPECEX")==0){ newMaterial.specularExponent = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "SPECRGB")==0){ glm::vec3 specColor( atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()) ); newMaterial.specularColor = specColor; }else if(strcmp(tokens[0].c_str(), "REFL")==0){ newMaterial.hasReflective = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "REFR")==0){ newMaterial.hasRefractive = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "REFRIOR")==0){ newMaterial.indexOfRefraction = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "SCATTER")==0){ newMaterial.hasScatter = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "ABSCOEFF")==0){ glm::vec3 abscoeff( atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()) ); newMaterial.absorptionCoefficient = abscoeff; }else if(strcmp(tokens[0].c_str(), "RSCTCOEFF")==0){ newMaterial.reducedScatterCoefficient = atof(tokens[1].c_str()); }else if(strcmp(tokens[0].c_str(), "EMITTANCE")==0){ newMaterial.emittance = atof(tokens[1].c_str()); } else if (strcmp(tokens[0].c_str(), "TEXTURE")==0) { int nComps; unsigned char *bytes = stbi_load(tokens [1].c_str (), &newMaterial.Texture.texelWidth, &newMaterial.Texture.texelHeight, &nComps, 3); if (bytes) { newMaterial.hasTexture = true; newMaterial.Texture.texels = new glm::vec3 [newMaterial.Texture.texelWidth * newMaterial.Texture.texelHeight]; for (int i = 0; i < (newMaterial.Texture.texelWidth * newMaterial.Texture.texelHeight); i ++) { newMaterial.Texture.texels [i].r = bytes [3*i] / 255.0; newMaterial.Texture.texels [i].g = bytes [3*i + 1] / 255.0; newMaterial.Texture.texels [i].b = bytes [3*i + 2] / 255.0; } } } else if (strcmp(tokens[0].c_str(), "NMAP")==0) { int nComps; unsigned char *bytes = stbi_load(tokens [1].c_str (), &newMaterial.NormalMap.texelWidth, &newMaterial.NormalMap.texelHeight, &nComps, 3); if (bytes) { newMaterial.hasNormalMap = true; newMaterial.NormalMap.texels = new glm::vec3 [newMaterial.NormalMap.texelWidth * newMaterial.NormalMap.texelHeight]; for (int i = 0; i < (newMaterial.Texture.texelWidth * newMaterial.NormalMap.texelHeight); i ++) { newMaterial.NormalMap.texels [i].r = bytes [3*i] / 255.0; newMaterial.NormalMap.texels [i].g = bytes [3*i + 1] / 255.0; newMaterial.NormalMap.texels [i].b = bytes [3*i + 2] / 255.0; } } } } materials.push_back(newMaterial); return 1; } }