void CloudLayer::renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *mi ) { GFXTransformSaver saver; const Point3F &camPos = state->getCameraPosition(); MatrixF xfm(true); xfm.setPosition(camPos); GFX->multWorld(xfm); if ( state->isReflectPass() ) GFX->setProjectionMatrix( state->getSceneManager()->getNonClipProjection() ); GFX->setShader( mShader ); GFX->setShaderConstBuffer( mShaderConsts ); GFX->setStateBlock( mStateblock ); // Set all the shader consts... MatrixF xform(GFX->getProjectionMatrix()); xform *= GFX->getViewMatrix(); xform *= GFX->getWorldMatrix(); mShaderConsts->setSafe( mModelViewProjSC, xform ); mShaderConsts->setSafe( mEyePosWorldSC, camPos ); LightInfo *lightinfo = LIGHTMGR->getSpecialLight(LightManager::slSunLightType); const ColorF &sunlight = state->getAmbientLightColor(); Point3F ambientColor( sunlight.red, sunlight.green, sunlight.blue ); mShaderConsts->setSafe( mAmbientColorSC, ambientColor ); const ColorF &sunColor = lightinfo->getColor(); Point3F data( sunColor.red, sunColor.green, sunColor.blue ); mShaderConsts->setSafe( mSunColorSC, data ); mShaderConsts->setSafe( mSunVecSC, lightinfo->getDirection() ); for ( U32 i = 0; i < TEX_COUNT; i++ ) mShaderConsts->setSafe( mTexOffsetSC[i], mTexOffset[i] ); Point3F scale( mTexScale[0], mTexScale[1], mTexScale[2] ); mShaderConsts->setSafe( mTexScaleSC, scale ); Point3F color; color.set( mBaseColor.red, mBaseColor.green, mBaseColor.blue ); mShaderConsts->setSafe( mBaseColorSC, color ); mShaderConsts->setSafe( mCoverageSC, mCoverage ); mShaderConsts->setSafe( mExposureSC, mExposure ); GFX->setTexture( mNormalHeightMapSC->getSamplerRegister(), mTexture ); GFX->setVertexBuffer( mVB ); GFX->setPrimitiveBuffer( mPB ); GFX->drawIndexedPrimitive( GFXTriangleList, 0, 0, smVertCount, 0, smTriangleCount ); }
color3 Ray::PhongColor(Light light, Material material, Fragment fragment) const { vec3 ambient = ambientColor(light, material); vec3 diffuse = diffuseColor(light, material, fragment); vec3 specular = specularColor(light, material, fragment); vec3 color = clamp(ambient + diffuse + specular, 0.0, 1.0); return color; }
Color<real> BlinnPhongShader<real>::GetAmbient( const Light<real>& light ) { Color<real> ambientColor( 0, 0, 0, 1 ); //////////////////////////////////////////// //////////////////IFT 3355////////////////// //////////////////////////////////////////// //Ici, vous calculerez la composante //ambiante. //////////////////////////////////////////// //////////////////IFT 3355////////////////// //////////////////////////////////////////// ambientColor += light.GetAmbient() * GetMaterialSurfaceColor() * mMaterial.GetAmbient() * GetAmbientOcclusion(mRayTracer.GetNbAmbientOcclusionSamples(), mRayTracer.GetAmbientOcclusionRadius()); return ambientColor; }
void SpotLight::apply() { const math::Matrix4x4& wt = cachedWorldTransform(); math::Vector3 wrotz = math::Vector3( wt(0,2),wt(1,2),wt(2,2) ); float wrotzlen = wrotz.length(); if ( wrotzlen > Float::MIN_VALUE ) { wrotz *= 1.f / wrotzlen; float intens = intensity(); gd::LightState ls; GdUtil::setLightState( ls, gd::LightState::LIGHT_SPOT, diffuseColor()*intens, specularColor()*intens, ambientColor()*intens, wt.translation(), wrotz, m_range, m_atten[0], m_atten[1], m_atten[2], 1.f, m_inner, m_outer ); gd::GraphicsDevice* dev = Context::device(); dev->addLight( ls ); } }
void DirectLight::apply() { const Matrix4x4& wt = cachedWorldTransform(); Vector3 wrotz = Vector3( wt(0,2),wt(1,2),wt(2,2) ); float wrotzlen = wrotz.length(); if ( wrotzlen > Float::MIN_VALUE ) { wrotz *= 1.f / wrotzlen; float intens = intensity(); gd::LightState ls; GdUtil::setLightState( ls, gd::LightState::LIGHT_DIRECT, diffuseColor()*intens, specularColor()*intens, ambientColor()*intens, wt.translation(), wrotz.normalize(), MAX_RANGE, 1.f, 0.f, 0.f, 1.f, MAX_CONE_ANGLE, MAX_CONE_ANGLE ); gd::GraphicsDevice* dev = Context::device(); dev->addLight( ls ); } }
void Lighting2D::init(glm::vec2 _screenSize, GE::Camera2D* _mainCamera){ screenSize = _screenSize; mainCamera = _mainCamera; camera.init(glm::ivec2(screenSize)); camera.setPosition(glm::vec2(0)); camera.setScale(camera.getScale()); camera.update(); shader.compileShaders("shaders/lighting2D.vert", "shaders/lighting2D.frag"); shader.addAttribute("vertexPosition"); shader.addAttribute("vertexColor"); shader.linkShaders(); ColorRGBA8 ambientColor(0, 0, 0, 255); vertexArray.init(); vertexArray.addVertex(glm::vec2(-screenSize.x / 2, -screenSize.y / 2), ambientColor); vertexArray.addVertex(glm::vec2(screenSize.x / 2, -screenSize.y / 2), ambientColor); vertexArray.addVertex(glm::vec2(screenSize.x / 2, screenSize.y / 2), ambientColor); vertexArray.addVertex(glm::vec2(-screenSize.x / 2, -screenSize.y / 2), ambientColor); vertexArray.addVertex(glm::vec2(screenSize.x / 2, screenSize.y / 2), ambientColor); vertexArray.addVertex(glm::vec2(-screenSize.x / 2, screenSize.y / 2), ambientColor); shadows.init(); glGenBuffers(1, &lUBO); glBindBuffer(GL_UNIFORM_BUFFER, lUBO); glBufferData(GL_UNIFORM_BUFFER, TMPNL * sizeof(Light2D), nullptr, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); GLuint lIx = glGetUniformBlockIndex(shader.getProgramID(), "lightBlock"); glUniformBlockBinding(shader.getProgramID(), lIx, 2); glBindBufferBase(GL_UNIFORM_BUFFER, 2, lUBO); glGenBuffers(1, &ecUBO); glBindBuffer(GL_UNIFORM_BUFFER, ecUBO); glBufferData(GL_UNIFORM_BUFFER, TMPNL * TMPNS * sizeof(ExCor), nullptr, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); GLuint exIx = glGetUniformBlockIndex(shader.getProgramID(), "shadowClippingBlock"); glUniformBlockBinding(shader.getProgramID(), exIx, 3); glBindBufferBase(GL_UNIFORM_BUFFER, 3, ecUBO); glGenBuffers(1, &sdUBO); glBindBuffer(GL_UNIFORM_BUFFER, sdUBO); glBufferData(GL_UNIFORM_BUFFER, TMPNS * sizeof(ShadowData), nullptr, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); GLuint sdIx = glGetUniformBlockIndex(shader.getProgramID(), "shadowDataBlock"); glUniformBlockBinding(shader.getProgramID(), sdIx, 4); glBindBufferBase(GL_UNIFORM_BUFFER, 4, sdUBO); float size = TMPNL * sizeof(Light2D)+TMPNL * TMPNS * sizeof(ExCor)+TMPNS * sizeof(ShadowData); std::cout << size << std::endl; }
MaterialData ModelLoader::loadMaterial(unsigned int index, const aiMaterial* material) { Q_ASSERT(material != nullptr); MaterialData data = MaterialData(); data.name = "material_" + to_string(index); aiColor3D ambientColor(0.1f, 0.1f, 0.1f); aiColor3D diffuseColor(0.8f, 0.8f, 0.8f); aiColor3D specularColor(0.0f, 0.0f, 0.0f); aiColor3D emissiveColor(0.0f, 0.0f, 0.0f); int blendMode; int twoSided = 1; float opacity = 1.0f; float shininess = 0.0f; float shininessStrength = 1.0f; if(material->Get(AI_MATKEY_COLOR_AMBIENT, ambientColor) == AI_SUCCESS) { data.ambientColor.setX(ambientColor.r); data.ambientColor.setY(ambientColor.g); data.ambientColor.setZ(ambientColor.b); } if(material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor) == AI_SUCCESS) { data.diffuseColor.setX(diffuseColor.r); data.diffuseColor.setY(diffuseColor.g); data.diffuseColor.setZ(diffuseColor.b); } if(material->Get(AI_MATKEY_COLOR_SPECULAR, specularColor) == AI_SUCCESS) { data.specularColor.setX(specularColor.r); data.specularColor.setY(specularColor.g); data.specularColor.setZ(specularColor.b); } if(material->Get(AI_MATKEY_COLOR_EMISSIVE, emissiveColor) == AI_SUCCESS) { data.emissiveColor.setX(emissiveColor.r); data.emissiveColor.setY(emissiveColor.g); data.emissiveColor.setZ(emissiveColor.b); } if(material->Get(AI_MATKEY_TWOSIDED, twoSided) == AI_SUCCESS) { data.twoSided = twoSided; } if(material->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS) { data.ambientColor.setW(opacity); data.diffuseColor.setW(opacity); data.specularColor.setW(opacity); data.emissiveColor.setW(opacity); if(opacity < 1.0f) { data.alphaBlending = true; // Activate backface culling allows to avoid // cull artifacts when alpha blending is activated data.twoSided = 1; if(material->Get(AI_MATKEY_BLEND_FUNC, blendMode) == AI_SUCCESS) { if(blendMode == aiBlendMode_Additive) data.blendMode = aiBlendMode_Additive; else data.blendMode = aiBlendMode_Default; } } else { data.alphaBlending = false; data.blendMode = -1; } } if(material->Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS) { data.shininess = shininess; } if(material->Get(AI_MATKEY_SHININESS_STRENGTH, shininessStrength) == AI_SUCCESS) { data.shininessStrength = shininessStrength; } return data; }
MStatus anisotropicShaderNode::compute( const MPlug& plug, MDataBlock& block ) { if ((plug == aOutColor) || (plug.parent() == aOutColor)) { MFloatVector resultColor(0.0,0.0,0.0); MFloatVector diffuseColor( 0.0,0.0,0.0 ); MFloatVector specularColor( 0.0,0.0,0.0 ); MFloatVector ambientColor( 0.0,0.0,0.0 ); // get matrix MFloatMatrix& matrixOToW = block.inputValue( aMatrixOToW ).asFloatMatrix(); MFloatMatrix& matrixWToC = block.inputValue( aMatrixWToC ).asFloatMatrix(); // spin scratch around this vector (in object space ) MFloatVector& A = block.inputValue( aAxesVector ).asFloatVector(); A.normalize(); // spin scratch around this vector (in world space ) MFloatVector wa = A * matrixOToW; wa.normalize(); // spin scratch around this vector (in camera space ) MFloatVector ca = wa * matrixWToC; ca.normalize(); MFloatVector& surfacePoint = block.inputValue( aPointCamera ).asFloatVector(); // get sample surface shading parameters MFloatVector& N = block.inputValue( aNormalCamera ).asFloatVector(); MFloatVector& surfaceColor = block.inputValue( aColor ).asFloatVector(); float diffuseReflectivity = block.inputValue( aDiffuseReflectivity ).asFloat(); float specularCoeff = block.inputValue( aSpecularCoeff ).asFloat(); // get light list MArrayDataHandle lightData = block.inputArrayValue( aLightData ); int numLights = lightData.elementCount(); // iterate through light list and get ambient/diffuse values for( int count=0; count < numLights; count++ ) { MDataHandle currentLight = lightData.inputValue(); MFloatVector& lightIntensity = currentLight.child( aLightIntensity ).asFloatVector(); MFloatVector& lightDirection = currentLight.child( aLightDirection ).asFloatVector(); // find ambient component if( currentLight.child(aLightAmbient).asBool()) { ambientColor[0] += lightIntensity[0] * surfaceColor[0]; ambientColor[1] += lightIntensity[1] * surfaceColor[1]; ambientColor[2] += lightIntensity[2] * surfaceColor[2]; } float cosln = lightDirection * N; if( cosln > 0.0f ){ // illuminated! // find diffuse component if( currentLight.child(aLightDiffuse).asBool()) { float cosDif = cosln * diffuseReflectivity; diffuseColor[0] += lightIntensity[0] * cosDif * surfaceColor[0]; diffuseColor[1] += lightIntensity[1] * cosDif * surfaceColor[1]; diffuseColor[2] += lightIntensity[2] * cosDif * surfaceColor[2]; } // find specular component if( currentLight.child( aLightSpecular).asBool()){ MFloatVector& rayDirection = block.inputValue( aRayDirection ).asFloatVector(); MFloatVector viewDirection = -rayDirection; MFloatVector half = calcHalfVector( viewDirection, lightDirection ); // Beckmann function MFloatVector nA; if( fabs(1.0-fabs(N*ca)) <= 0.0001f ){ MFloatPoint oo( 0.0,0.0,0.0 ); MFloatPoint ow = oo * matrixOToW; MFloatPoint oc = ow * matrixWToC; MFloatVector origin( oc[0], oc[1], oc[2] ); nA = origin - surfacePoint; nA.normalize(); }else{ nA = ca; } MFloatVector x = N ^ nA; x.normalize(); MFloatVector y = N ^ x; y.normalize(); MFloatVector azimuthH = N ^ half; azimuthH = N ^ azimuthH; azimuthH.normalize(); float cos_phai = x * azimuthH; float sin_phai = 0.0; if( fabs(1 - cos_phai*cos_phai) < 0.0001 ){ sin_phai = 0.0; }else{ sin_phai = sqrtf( 1.0f - cos_phai*cos_phai ); } double co = pow( (half * N), 4.0f ); double t = tan( acos(half*N) ); t *= -t; float rough1 = block.inputValue( aRoughness1 ).asFloat(); float rough2 = block.inputValue( aRoughness2 ).asFloat(); double aaa = cos_phai / rough1; double bbb = sin_phai / rough2; t = t * ( aaa*aaa + bbb*bbb ); double D = pow( (1.0/((double)rough1*(double)rough2 * co)), t ); double aa = (2.0 * (N*half) * (N*viewDirection) ) / (viewDirection*half); double bb = (2.0 * (N*half) * (N*lightDirection) ) / (viewDirection*half); double cc = 1.0; double G = 0.0; G = MIN( aa, bb ); G = MIN( G, cc ); float s = (float) (D * G / (double)((N*lightDirection) * (N*viewDirection))); MFloatVector& specColor = block.inputValue( aSpecColor ).asFloatVector(); specularColor[0] += lightIntensity[0] * specColor[0] * s * specularCoeff; specularColor[1] += lightIntensity[1] * specColor[1] * s * specularCoeff; specularColor[2] += lightIntensity[2] * specColor[2] * s * specularCoeff; } } if( !lightData.next() ){ break; } } // result = specular + diffuse + ambient; resultColor = diffuseColor + specularColor + ambientColor; MFloatVector& transparency = block.inputValue( aInTransparency ).asFloatVector(); resultColor[0] *= ( 1.0f - transparency[0] ); resultColor[1] *= ( 1.0f - transparency[1] ); resultColor[2] *= ( 1.0f - transparency[2] ); // set ouput color attribute MDataHandle outColorHandle = block.outputValue( aOutColor ); MFloatVector& outColor = outColorHandle.asFloatVector(); outColor = resultColor; outColorHandle.setClean(); block.setClean( plug ); } else if ((plug == aOutTransparency) || (plug.parent() == aOutTransparency)) { MFloatVector& tr = block.inputValue( aInTransparency ).asFloatVector(); // set ouput color attribute MDataHandle outTransHandle = block.outputValue( aOutTransparency ); MFloatVector& outTrans = outTransHandle.asFloatVector(); outTrans = tr; block.setClean( plug ); } else return MS::kUnknownParameter; return MS::kSuccess; }
// Execute OpenGL Material void GLC_Material::glExecute(float overwriteTransparency) { GLfloat pAmbientColor[4]= {ambientColor().redF(), ambientColor().greenF(), ambientColor().blueF(), overwriteTransparency}; GLfloat pDiffuseColor[4]= {diffuseColor().redF(), diffuseColor().greenF(), diffuseColor().blueF(), overwriteTransparency}; GLfloat pSpecularColor[4]= {specularColor().redF(), specularColor().greenF(), specularColor().blueF(), overwriteTransparency}; GLfloat pLightEmission[4]= {emissiveColor().redF(), emissiveColor().greenF(), emissiveColor().blueF(), overwriteTransparency}; const bool textureIsEnable= glIsEnabled(GL_TEXTURE_2D); if (m_pTexture != NULL) { if (!textureIsEnable) glEnable(GL_TEXTURE_2D); m_pTexture->glcBindTexture(); if (GLC_State::glslUsed()) { if (GLC_Shader::hasActiveShader()) { GLC_Shader::currentShaderHandle()->programShaderHandle()->setUniformValue("tex", GLint(0)); GLC_Shader::currentShaderHandle()->programShaderHandle()->setUniformValue("useTexture", true); } } } else { if (textureIsEnable) glDisable(GL_TEXTURE_2D); if (GLC_State::glslUsed()) { if (GLC_Shader::hasActiveShader()) { GLC_Shader::currentShaderHandle()->programShaderHandle()->setUniformValue("tex", GLint(0)); GLC_Shader::currentShaderHandle()->programShaderHandle()->setUniformValue("useTexture", false); } } } glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, pAmbientColor); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pDiffuseColor); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pSpecularColor); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pLightEmission); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &m_Shininess); glColor4fv(pDiffuseColor); // OpenGL Error handler GLenum error= glGetError(); if (error != GL_NO_ERROR) { GLC_OpenGlException OpenGlException("GLC_Material::glExecute(float overwriteTransparency) ", error); throw(OpenGlException); } }
void Material::ExportMaterials(FbxScene* scene, FbxMesh* mesh, const ImporterMesh& importedMesh, const ImporterMaterial* importedMaterials) { for (int i = 0; i < importedMesh.material_count; i++) { FbxNode* node = mesh->GetNode(); FbxString materialName = importedMaterials[importedMesh.material_Id[i]].name; FbxString shadingName; FbxDouble3 diffuseColor(importedMaterials[importedMesh.material_Id[i]].diffuse[0], importedMaterials[importedMesh.material_Id[i]].diffuse[1], importedMaterials[importedMesh.material_Id[i]].diffuse[2]); FbxDouble3 ambientColor(importedMaterials[importedMesh.material_Id[i]].ambient[0], importedMaterials[importedMesh.material_Id[i]].ambient[1], importedMaterials[importedMesh.material_Id[i]].ambient[2]); FbxDouble3 emissiveColor(importedMaterials[importedMesh.material_Id[i]].incandescence[0], importedMaterials[importedMesh.material_Id[i]].incandescence[1], importedMaterials[importedMesh.material_Id[i]].incandescence[2]); FbxDouble3 transparencyColor(importedMaterials[importedMesh.material_Id[i]].transparency_color[0], importedMaterials[importedMesh.material_Id[i]].transparency_color[1], importedMaterials[importedMesh.material_Id[i]].transparency_color[2]); FbxDouble3 specularColor(importedMaterials[importedMesh.material_Id[i]].specular[0], importedMaterials[importedMesh.material_Id[i]].specular[1] , importedMaterials[importedMesh.material_Id[i]].specular[2]); const char* pathName = "C://Users/Litet/Documents/GitHub/SmallGameProject/FBX Export/FBX Export/"; // Lambert if (importedMaterials[importedMesh.material_Id[i]].mtrl_type == 0) { shadingName = "Lambert"; FbxSurfaceLambert* material = NULL; material= node->GetSrcObject<FbxSurfaceLambert>(0); material = FbxSurfaceLambert::Create(scene, materialName.Buffer()); materialName += i; material->Emissive.Set(emissiveColor); material->Ambient.Set(ambientColor); material->Diffuse.Set(diffuseColor); material->DiffuseFactor.Set(importedMaterials[importedMesh.material_Id[i]].diffuse_factor); material->TransparentColor.Set(transparencyColor); FbxNode* node = mesh->GetNode(); if (node) { node->AddMaterial(material); } // Diffuse Texture FbxFileTexture* texture = FbxFileTexture::Create(scene, "Diffuse Texture"); std::cout << "DAFUSE MAP LENGTH: " << importedMaterials[importedMesh.material_Id[i]].duffuse_map_length << std::endl; if (importedMaterials[importedMesh.material_Id[i]].duffuse_map_length > 0) { std::string tmp(pathName); tmp += importedMaterials[importedMesh.material_Id[i]].diffuse_map; texture->SetFileName(tmp.c_str()); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->Diffuse.ConnectSrcObject(texture); } // Normal Texture texture = FbxFileTexture::Create(scene, "Normal Texture"); if (importedMaterials[importedMesh.material_Id[i]].normal_map_length > 0) { texture->SetFileName(importedMaterials[importedMesh.material_Id[i]].normal_map); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->NormalMap.ConnectSrcObject(texture); } } // Phong else { shadingName = "Phong"; FbxSurfacePhong* material = NULL; material = node->GetSrcObject<FbxSurfacePhong>(0); material = FbxSurfacePhong::Create(scene, materialName.Buffer()); materialName += i; material->Emissive.Set(emissiveColor); material->Ambient.Set(ambientColor); material->Diffuse.Set(diffuseColor); material->DiffuseFactor.Set(importedMaterials[importedMesh.material_Id[i]].diffuse_factor); material->TransparentColor.Set(transparencyColor); material->Specular.Set(specularColor); // No need... super boost? //material->SpecularFactor.Set(importedMaterials[importedMesh.material_Id[i]].specular_factor * 100); material->Shininess.Set(importedMaterials[importedMesh.material_Id[i]].specular_factor); material->Reflection.Set(FbxDouble3(importedMaterials[importedMesh.material_Id[i]].reflection[0], importedMaterials[importedMesh.material_Id[i]].reflection[1], importedMaterials[importedMesh.material_Id[i]].reflection[2])); // Bugged...? material->ReflectionFactor.Set(FbxDouble(importedMaterials[importedMesh.material_Id[i]].reflection_factor)); cout << importedMaterials[importedMesh.material_Id[i]].shininess << endl; FbxNode* node = mesh->GetNode(); if (node) { node->AddMaterial(material); } // Diffuse Texture FbxFileTexture* texture = FbxFileTexture::Create(scene, "Diffuse Texture"); if (importedMaterials[importedMesh.material_Id[i]].duffuse_map_length > 0) { texture->SetFileName(importedMaterials[importedMesh.material_Id[i]].diffuse_map); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->Diffuse.ConnectSrcObject(texture); } // Specular Texture texture = FbxFileTexture::Create(scene, "Specular Texture"); if (importedMaterials[importedMesh.material_Id[i]].specular_map_length > 0) { texture->SetFileName(importedMaterials[importedMesh.material_Id[i]].specular_map); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->Specular.ConnectSrcObject(texture); } // Normal Texture texture = FbxFileTexture::Create(scene, "Normal Texture"); if (importedMaterials[importedMesh.material_Id[i]].normal_map_length > 0) { texture->SetFileName(importedMaterials[importedMesh.material_Id[i]].normal_map); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->NormalMap.ConnectSrcObject(texture); } } } }
Color trace (const Ray& ray, std::set<IShape*>& sceneShapes, std::set<Light*>& sceneLights, int depth) { Color pixelColor (0.3); float near; Color color; Vector3D normal; IShape *shape = calculateIntersect (ray, sceneShapes, &near, normal, color); if (shape) { pixelColor = color; Point intersectionPoint = ray.calculate (near); Vector3D n; Color c; pixelColor = Color (0.0f); //Calculate illumination on intersected pixel for (auto light : sceneLights) { Vector3D lightDirection = (light->position () - intersectionPoint); float lightLenght = lightDirection.normalize (); const Ray shadowRay (intersectionPoint + normal * bias, lightDirection, lightLenght); float near = INFINITY; IShape *s = calculateIntersect (shadowRay, sceneShapes, &near, n, c); if (!s) //There is no object between the intersected pixel and this light. { float diffuseCoefficient = shape->diffuse (); float specularCoefficient = shape->specular (); pixelColor += ambientColor (color); if (diffuseCoefficient > 0.0f) pixelColor += diffuseColor (lightDirection, light, normal, color, diffuseCoefficient); if (specularCoefficient > 0.0f) pixelColor += specularColor (lightDirection, normal, ray, light, specularCoefficient); } else //Intersected pixel is shadowed!!! { pixelColor = color * 0.1; break; } } //Calculate the reflected color if ((shape->reflection () > 0) && depth <= MAX_DEPTH) { Vector3D reflDir = ray.direction () - normal * 2 * ray.direction ().dot (normal); reflDir.normalize (); Ray reflectionRay (intersectionPoint + normal * bias, reflDir); Color reflectionColor = trace (reflectionRay, sceneShapes, sceneLights, depth + 1); pixelColor += reflectionColor * shape->reflection (); } } pixelColor.clamp (); return pixelColor; }