// This software is provide under the GNU General Public License // Please visit: http://opensource.org/licenses/GPL-3.0 //############################################################################# #include <stdafx.h> // precompiled headers #ifdef SL_MEMLEAKDETECT #include <nvwa/debug_new.h> // memory leak detector #endif #include "SLMaterial.h" #include "SLSceneView.h" #include "SLShape.h" //----------------------------------------------------------------------------- SLMaterial SLMaterial::AIR = SLMaterial((SLchar*)"Air", SLCol4f(0,0,0), SLCol4f(0,0,0)); //----------------------------------------------------------------------------- SLfloat SLMaterial::PERFECT = 1000.0f; //----------------------------------------------------------------------------- SLMaterial* SLMaterial::current = 0; //----------------------------------------------------------------------------- // Default ctor SLMaterial::SLMaterial(const SLchar* name, SLCol4f amdi, SLCol4f spec, SLfloat shininess, SLfloat kr, SLfloat kt, SLfloat kn) : SLObject(name) { _ambient = _diffuse = amdi; _specular = spec;
/*! SLButton::buildBuffers creates the VAO for rendering */ void SLButton::buildBuffers() { SLVVec2f P; // vertex positions SLVCol4f C; // colors SLfloat x = _minX; SLfloat y = _minY; SLfloat w = _btnW; SLfloat h = _btnH; SLfloat mx = x + 2*_sv->dpmm()*BTN_GAP_W_MM; // center x of check mark SLfloat my = y + h*0.5f; // center y of check mark SLfloat diff1 = 0.3f; // button color difference upper to lower border SLfloat diff2 = 0.6f; // border color difference upper to lower border SLint nP = 0; SLint nC = 0; // up button P.push_back(SLVec2f(x, y+h)); // button top left corner P.push_back(SLVec2f(x, y )); // button bottom left corner P.push_back(SLVec2f(x+w, y+h)); // button top right corner P.push_back(SLVec2f(x+w, y )); // button bottom right corner C.push_back(SLCol4f(_btnCol.r+diff1, _btnCol.g+diff1, _btnCol.b+diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r-diff1, _btnCol.g-diff1, _btnCol.b-diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r+diff1, _btnCol.g+diff1, _btnCol.b+diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r-diff1, _btnCol.g-diff1, _btnCol.b-diff1, _btnAlpha)); // down button P.push_back(SLVec2f(x, y+h)); // button top left corner P.push_back(SLVec2f(x, y )); // button bottom left corner P.push_back(SLVec2f(x+w, y+h)); // button top right corner P.push_back(SLVec2f(x+w, y )); // button bottom right corner C.push_back(SLCol4f(_btnCol.r+diff1, _btnCol.g+diff1, _btnCol.b+diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r+diff1, _btnCol.g+diff1, _btnCol.b+diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r+diff1, _btnCol.g+diff1, _btnCol.b+diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r+diff1, _btnCol.g+diff1, _btnCol.b+diff1, _btnAlpha)); // pressed button P.push_back(SLVec2f(x, y+h)); // button top left corner P.push_back(SLVec2f(x, y )); // button bottom left corner P.push_back(SLVec2f(x+w, y+h)); // button top right corner P.push_back(SLVec2f(x+w, y )); // button bottom right corner C.push_back(SLCol4f(_btnCol.r-diff1, _btnCol.g-diff1, _btnCol.b-diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r+diff1, _btnCol.g+diff1, _btnCol.b+diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r-diff1, _btnCol.g-diff1, _btnCol.b-diff1, _btnAlpha)); C.push_back(SLCol4f(_btnCol.r+diff1, _btnCol.g+diff1, _btnCol.b+diff1, _btnAlpha)); // down border P.push_back(SLVec2f(x, y )); // button bottom left corner P.push_back(SLVec2f(x+w, y )); // button bottom right corner P.push_back(SLVec2f(x+w, y+h)); // button top right corner P.push_back(SLVec2f(x, y+h)); // button top left corner C.push_back(SLCol4f(_btnCol.r+diff2, _btnCol.g+diff2, _btnCol.b+diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r+diff2, _btnCol.g+diff2, _btnCol.b+diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r-diff2, _btnCol.g-diff2, _btnCol.b-diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r-diff2, _btnCol.g-diff2, _btnCol.b-diff2, 1.0f)); // up border P.push_back(SLVec2f(x, y )); // button bottom left corner P.push_back(SLVec2f(x+w, y )); // button bottom right corner P.push_back(SLVec2f(x+w, y+h)); // button top right corner P.push_back(SLVec2f(x, y+h)); // button top left corner C.push_back(SLCol4f(_btnCol.r-diff2, _btnCol.g-diff2, _btnCol.b-diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r-diff2, _btnCol.g-diff2, _btnCol.b-diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r+diff2, _btnCol.g+diff2, _btnCol.b+diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r+diff2, _btnCol.g+diff2, _btnCol.b+diff2, 1.0f)); // White check box P.push_back(SLVec2f(mx-5, my-5)); // 1st point of check box rect P.push_back(SLVec2f(mx+5, my-5)); // 2nd point of check box rect P.push_back(SLVec2f(mx+5, my+5)); // 3rd point of check box rect P.push_back(SLVec2f(mx-5, my+5)); // 4th point of check box rect C.push_back(SLCol4f(_btnCol.r-diff2, _btnCol.g-diff2, _btnCol.b-diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r-diff2, _btnCol.g-diff2, _btnCol.b-diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r-diff2, _btnCol.g-diff2, _btnCol.b-diff2, 1.0f)); C.push_back(SLCol4f(_btnCol.r-diff2, _btnCol.g-diff2, _btnCol.b-diff2, 1.0f)); // White check mark P.push_back(SLVec2f(mx-4, my+4)); // 1st point of check mark P.push_back(SLVec2f(mx+4, my-4)); // 2nd point of check mark P.push_back(SLVec2f(mx-4, my-4)); // 3rd point of check mark P.push_back(SLVec2f(mx+4, my+4)); // 4th point of check mark C.push_back(SLCol4f(1,1,1,0.8f)); C.push_back(SLCol4f(1,1,1,0.8f)); C.push_back(SLCol4f(1,1,1,0.8f)); C.push_back(SLCol4f(1,1,1,0.8f)); // create buffers on GPU SLGLProgram* sp = SLScene::current->programs(SP_colorAttribute); sp->useProgram(); _vao.setAttrib(AT_position, sp->getAttribLocation("a_position"), &P); _vao.setAttrib(AT_color, sp->getAttribLocation("a_color"), &C); _vao.generate((SLuint)P.size()); }
/*! SLAssimpImporter::loadMaterial loads the AssImp material an returns the SLMaterial. The materials and textures are added to the SLScene material and texture vectors. */ SLMaterial* SLAssimpImporter::loadMaterial(SLint index, aiMaterial *material, SLstring modelPath) { // Get the materials name aiString matName; material->Get(AI_MATKEY_NAME, matName); SLstring name = matName.data; if (name.empty()) name = "Import Material"; // Create SLMaterial instance. It is also added to the SLScene::_materials vector SLMaterial* mat = new SLMaterial(name.c_str()); // set the texture types to import into our material const SLint textureCount = 4; aiTextureType textureTypes[textureCount]; textureTypes[0] = aiTextureType_DIFFUSE; textureTypes[1] = aiTextureType_NORMALS; textureTypes[2] = aiTextureType_SPECULAR; textureTypes[3] = aiTextureType_HEIGHT; // load all the textures for this material and add it to the material vector for(SLint i = 0; i < textureCount; ++i) { if(material->GetTextureCount(textureTypes[i]) > 0) { aiString aipath; material->GetTexture(textureTypes[i], 0, &aipath, nullptr, nullptr, nullptr, nullptr, nullptr); SLTextureType texType = textureTypes[i]==aiTextureType_DIFFUSE ? TT_color : textureTypes[i]==aiTextureType_NORMALS ? TT_normal : textureTypes[i]==aiTextureType_SPECULAR ? TT_gloss : textureTypes[i]==aiTextureType_HEIGHT ? TT_height : TT_unknown; SLstring texFile = checkFilePath(modelPath, aipath.data); SLGLTexture* tex = loadTexture(texFile, texType); mat->textures().push_back(tex); } } // get color data aiColor3D ambient, diffuse, specular, emissive; SLfloat shininess, refracti, reflectivity, opacity; material->Get(AI_MATKEY_COLOR_AMBIENT, ambient); material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse); material->Get(AI_MATKEY_COLOR_SPECULAR, specular); material->Get(AI_MATKEY_COLOR_EMISSIVE, emissive); material->Get(AI_MATKEY_SHININESS, shininess); material->Get(AI_MATKEY_REFRACTI, refracti); material->Get(AI_MATKEY_REFLECTIVITY, reflectivity); material->Get(AI_MATKEY_OPACITY, opacity); // increase shininess if specular color is not low. // The material will otherwise be to bright if (specular.r > 0.5f && specular.g > 0.5f && specular.b > 0.5f && shininess < 0.01f) shininess = 10.0f; // set color data mat->ambient(SLCol4f(ambient.r, ambient.g, ambient.b)); mat->diffuse(SLCol4f(diffuse.r, diffuse.g, diffuse.b)); mat->specular(SLCol4f(specular.r, specular.g, specular.b)); mat->emission(SLCol4f(emissive.r, emissive.g, emissive.b)); mat->shininess(shininess); //mat->kr(reflectivity); //mat->kt(1.0f-opacity); //mat->kn(refracti); return mat; }