//=====-----=======---------=========---------==========---------=========----------=============
    void ParticleSet::drawParticles( SpriteBatch& sb, const TexturePtr texture, const Color& color, const int layer, const float sizeFactor, bool doFade )
	{
		for(int i = 0; i <= mHighestLiveIndex; ++i)
		{
			if(!mParticles[i].mAlive)
			{
				continue;
			}
			
			if (mParticles[i].mLifetime == -1.0f) 
			{
                Color finalColor( color );
                finalColor *= mParticles[i].mAlphaMultiplier;
				sb.drawQuad(layer, texture, mParticles[i].mPosition + VectorTools::rotateVector(mParticles[i].mOffset, Walaber::degToRad(mParticles[i].mAngleDeg)), Walaber::degToRad(mParticles[i].mAngleDeg), mParticles[i].mSize * sizeFactor, finalColor);
			}
			else 
			{
				Color finalCol = color;
				
				if (doFade)
					finalCol = _getParticleColor(&mParticles[i], color); // color * lerp(0.0f, 1.0f, mParticles[i].mLifetime / mParticles[i].mFadeLength);
                
				sb.drawQuad(layer, texture, mParticles[i].mPosition + VectorTools::rotateVector(mParticles[i].mOffset, Walaber::degToRad(mParticles[i].mAngleDeg)), Walaber::degToRad(mParticles[i].mAngleDeg), getParticleSize(i) * sizeFactor, finalCol);
			}
		}
	}
Color<real> BlinnPhongShader<real>::Shade()
{
    Color<real> finalColor( 0, 0, 0, 1 );
    const LightVector& lights = mScene.GetLights();

    ////////////////////////////////////////////
    //////////////////IFT 3355//////////////////
    ////////////////////////////////////////////
    //Ici, vous assemblerez toutes les
    //composantes (ambient, diffus, spéculaire,
    //réflexion, réfraction). Vous ferez
    //également en sorte de déterminer
    //l'ombrage du point.
    //N.B.
    //Lisez d'abord les méthodes qui suivent avant
    //de commencer le codage.
    ////////////////////////////////////////////
    //////////////////IFT 3355//////////////////
    ////////////////////////////////////////////

    // Clamp the color to white
    for (uint i = 0; i < lights.size(); ++i) {
       finalColor += GetAmbient(*lights[i]);
       finalColor += GetDiffuseAndSpecular(*lights[i]);
    }
    if (!mMaterial.GetReflection().IsBlack())
        finalColor += GetReflection();
    if (!mMaterial.GetRefraction().IsBlack())
        finalColor += GetRefraction();

    finalColor.Clamp( Color<real>( 1, 1, 1, 1 ) );
    return finalColor;
}
Color<real> BlinnPhongShader<real>::GetDiffuseAndSpecular( const Light<real>& light )
{
    Color<real> finalColor( 0, 0, 0, 1 );

    ////////////////////////////////////////////
    //////////////////IFT 3355//////////////////
    ////////////////////////////////////////////
    //Ici, vous calculerez les composantes
    //diffuse et spéculaire en vous servant des
    //propriétés du matériau.
    ////////////////////////////////////////////
    //////////////////IFT 3355//////////////////
    ////////////////////////////////////////////

    Vector3<real> lightPos = light.GetGlobalPosition();
    Vector3<real> lightDir = (lightPos - mIntersection.GetPosition()).Normalized();
    real r2 = (mIntersection.GetPosition() - lightPos).SquaredLength();
    Vector3<real> intersectionShifted = mIntersection.GetPosition() + EPS*mIntersection.GetNormal();

    // Shadow+Diffuse
    bool lightVisible = mRayTracer.IsLightVisible(intersectionShifted, &light);
    if (!lightVisible)
        return finalColor;

    switch (light.GetLightType()) {
    case Light<real>::TypeDirectional:
    case Light<real>::TypeSpot:
    case Light<real>::TypePoint:
        finalColor += mMaterial.GetDiffuse() *
            std::max<real>(0, lightDir * mIntersection.GetNormal()) *
            GetMaterialSurfaceColor();
        break;
    }

    // Specular
    const Camera<real> *cam = mScene.GetActiveCamera();
    Vector3<real> camDir = cam->GetFrontDirection();
    Vector3<real> H = lightDir + camDir;
    H.Normalize();
    Vector3<real> N = mIntersection.GetNormal();
    real n = mMaterial.GetShininess();
    real cs = (n+2)/2;
    Color<real> ks = mMaterial.GetSpecular();
    Color<real> m = mMaterial.GetMetallic();

    finalColor += cs * ks *
        (m*GetMaterialSurfaceColor() + (Color<real>(1, 1, 1, 1) - m)) *
        pow(N * H, n);

    return finalColor * light.GetIntensity() / (r2 * PI);
}
Exemple #4
0
Tcolor TrayTracer::radianceWithExplicitLight(Tray ray,int reflectLevel)
{
    if(reflectLevel <= 0)
        return Tcolor(0,0,0);
    Tcolor finalColor(0,0,0);
    auto result = scene()->intersect(ray);
    if (result.geometry()) {
        auto material = result.geometry ()->material ();
        if(!material) return finalColor;

        Tcolor selfColor = material->sampleSelfColor ();

        //**********Emission.
        auto emissionColor = selfColor*material->emission ();

        //reflect
        Tcolor reflectColor;
        auto allLights = scene()->getLightList();
        for(int i =0;i<allLights.size ();i++)
        {
            TexplicitLight * light = allLights[i];

            if(!light->isVisible (result.pos (),scene())) continue;
            //get the irradiance from the explicit light source.
            auto lightDir = light->getDir (result.pos ());
            auto lightRadiance = light->getIrradiance (result.pos (),result.normal (),scene());

            reflectColor += lightRadiance * material->BRDF (-ray.direction (),-lightDir,result.normal ());
        }

        //ideal specular radiance from other object.
        if(material->reflectiveness () > 0)
        {

            // get the ideal specular ray.
            auto reflectVec = reflect(ray.direction (),result.normal ());
            auto reflectRay = Tray(result.pos (),reflectVec);

            auto idealSpecularRadiance = radianceWithExplicitLight(reflectRay,reflectLevel - 1);
            reflectColor +=idealSpecularRadiance * material->BRDF (-ray.direction (),reflectVec,result.normal ());
        }

        //render euqation.
        finalColor = emissionColor+ selfColor*reflectColor*material->reflectiveness ();
    }
    return finalColor;
}
    //=====-----=======---------=========---------==========---------=========----------=============
    void ParticleSet::drawParticles( SpriteBatch& sb, const std::vector<TexturePtr> textures, const Color& color, const int layer )
	{
		for(int i = 0; i <= mHighestLiveIndex; ++i)
		{
			if(!mParticles[i].mAlive)
			{
				continue;
			}
			
			if (mParticles[i].mLifetime == -1.0f) 
			{
                Color finalColor( color );
                finalColor *= mParticles[i].mAlphaMultiplier;
				sb.drawQuad(layer, textures[mParticles[i].mTextureIndex], mParticles[i].mPosition + VectorTools::rotateVector(mParticles[i].mOffset, Walaber::degToRad(mParticles[i].mAngleDeg)), Walaber::degToRad(mParticles[i].mAngleDeg), mParticles[i].mSize, finalColor);
			}
			else 
			{	
				Color fadeOut = _getParticleColor(&mParticles[i], color); // color * lerp(0.0f, 1.0f, mParticles[i].mLifetime / mParticles[i].mFadeLength);
                
				sb.drawQuad(layer, textures[mParticles[i].mTextureIndex], mParticles[i].mPosition + VectorTools::rotateVector(mParticles[i].mOffset, Walaber::degToRad(mParticles[i].mAngleDeg)), Walaber::degToRad(mParticles[i].mAngleDeg), getParticleSize(i), fadeOut);
			}
		}
	}
Exemple #6
0
bool BulletContent::Initialize(std::string& err)
{
    typedef VertexPosUV BulletVertex;
    RenderIOAttributes vertIns = BulletVertex::GetVertexAttributes();


    #pragma region Meshes

    for (unsigned int i = 0; i < B_NUMBER_OF_BULLETS; ++i)
    {
        bulletMesh.SubMeshes.push_back(MeshData(false, PT_TRIANGLE_LIST));
    }

    std::vector<BulletVertex> vertices;
    std::vector<unsigned int> indices;

    Assimp::Importer importer;
    unsigned int flags = aiProcessPreset_TargetRealtime_MaxQuality;

    for (unsigned int i = 0; i < B_NUMBER_OF_BULLETS; ++i)
    {
        //Get the file for this bullet type.
        std::string file = "Content/Game/Meshes/Bullets/";
        switch ((Bullets)i)
        {
            case B_PUNCHER:
                file += "Puncher.obj";
                break;
            case B_TERRIBLE_SHOTGUN:
                file += "Terrible Shotgun.obj";
                break;
            case B_SPRAY_N_PRAY:
                file += "Spray and Pray.obj";
                break;
            case B_CLUSTER:
                file += "Cluster.obj";
                break;

            default:
                assert(false);
        }

        const aiScene* scene = importer.ReadFile(file, flags);

        //Make sure the scene is valid.
        if (scene == 0)
        {
            err = "Error loading '" + file + "': " + importer.GetErrorString();
            return false;
        }
        if (scene->mNumMeshes != 1)
        {
            err = "Mesh '" + file + "' has " + std::to_string(scene->mNumMeshes) + " meshes in it";
            return false;
        }

        aiMesh* mesh = scene->mMeshes[0];

        //Make sure the mesh is valid.
        assert(mesh->HasFaces());
        if (!mesh->HasPositions() || !mesh->HasTextureCoords(0))
        {
            err = "Mesh '" + file + "' is missing positions or UVs!";
            return false;
        }

        //Populate the vertex/index buffer data.
        vertices.resize(mesh->mNumVertices);
        for (unsigned int j = 0; j < mesh->mNumVertices; ++j)
        {
            vertices[j].Pos = *(Vector3f*)(&mesh->mVertices[j].x);
            vertices[j].UV = *(Vector2f*)(&mesh->mTextureCoords[0][j].x);
        }
        indices.resize(mesh->mNumFaces * 3);
        for (unsigned int j = 0; j < mesh->mNumFaces; ++j)
        {
            aiFace& fce = mesh->mFaces[j];
            if (fce.mNumIndices != 3)
            {
                err = "A face in mesh '" + file + "' has a non-tri face with " +
                      std::to_string(fce.mNumIndices) + " indices!";
                return false;
            }

            indices[(j * 3)] = fce.mIndices[0];
            indices[(j * 3) + 1] = fce.mIndices[1];
            indices[(j * 3) + 2] = fce.mIndices[2];
        }

        //Create the vertex/index buffers.
        bulletMesh.SubMeshes[i].SetVertexData(vertices, MeshData::BUF_STATIC, vertIns);
        bulletMesh.SubMeshes[i].SetIndexData(indices, MeshData::BUF_STATIC);
    }

    #pragma endregion

    #pragma region Textures

    {
        Array2D<Vector4b> values(1, 1, Vector4b((unsigned char)255, 255, 255, 255));
        defaultTex.Create();
        defaultTex.SetColorData(values);
    }

    #pragma endregion

    #pragma region Materials

    {
        SerializedMaterial serMat;
        serMat.VertexInputs = vertIns;

        DataLine vIn_Pos(VertexInputNode::GetInstance(), 0),
                 vIn_UV(VertexInputNode::GetInstance(), 1);
        
        DataNode::Ptr screenPos = SpaceConverterNode::ObjPosToScreenPos(vIn_Pos, "screenPos");
        serMat.MaterialOuts.VertexPosOutput = DataLine(screenPos, 1);

        serMat.MaterialOuts.VertexOutputs.push_back(ShaderOutput("fIn_UV", vIn_UV));

        DataLine fIn_UV(FragmentInputNode::GetInstance(), 0);

        DataNode::Ptr tex(new TextureSample2DNode(fIn_UV, UNIFORM_TEXTURE, "texSample"));
        DataLine texRGB(tex, TextureSample2DNode::GetOutputIndex(CO_AllColorChannels));

        DataNode::Ptr colorParam(new ParamNode(3, UNIFORM_COLOR));

        DataNode::Ptr finalRGB(new MultiplyNode(texRGB, colorParam, "finalRGB"));
        DataNode::Ptr finalColor(new CombineVectorNode(finalRGB, 1.0f, "finalColor"));

        serMat.MaterialOuts.FragmentOutputs.push_back(ShaderOutput("fOut_Color", finalColor));

        auto genMat = ShaderGenerator::GenerateMaterial(serMat, bulletParams, BlendMode::GetOpaque());
        if (!genMat.ErrorMessage.empty())
        {
            err = "Error generating bullet material: " + genMat.ErrorMessage;
            return false;
        }
        bulletMat = genMat.Mat;
    }

    #pragma endregion

    return true;
}