Example #1
0
void Material::Draw(PolygonGroup * group, InstanceMaterialState * instanceMaterialState)
{
	if(!RenderManager::Instance()->GetOptions()->IsOptionEnabled(RenderOptions::MATERIAL_DRAW))
	{
		return;
	}

	if(isOpaque && !RenderManager::Instance()->GetOptions()->IsOptionEnabled(RenderOptions::OPAQUE_DRAW))
	{
		return;
	}

	//Dizz: uniformFogDensity != -1 is a check if fog is inabled in shader
	if(isFogEnabled && (uniformFogDensity != -1) && !RenderManager::Instance()->GetOptions()->IsOptionEnabled(RenderOptions::FOG_ENABLE))
	{
		RebuildShader();
	}

	if(isFogEnabled && (uniformFogDensity == -1) && RenderManager::Instance()->GetOptions()->IsOptionEnabled(RenderOptions::FOG_ENABLE))
	{
		RebuildShader();
	}

	RenderManager::Instance()->SetRenderData(group->renderDataObject);

	eBlendMode oldSrc;
	eBlendMode oldDst;
	if(isAlphablend)
	{
		oldSrc = RenderManager::Instance()->GetSrcBlend();
		oldDst = RenderManager::Instance()->GetDestBlend();
	}

	PrepareRenderState();

    if (instanceMaterialState)
    {
        Camera * camera = scene->GetCurrentCamera();
        LightNode * lightNode0 = instanceMaterialState->GetLight(0);
        if (lightNode0 && camera)
        {
            if (uniformLightPosition0 != -1)
            {
                const Matrix4 & matrix = camera->GetMatrix();
                Vector3 lightPosition0InCameraSpace = lightNode0->GetPosition() * matrix;
                
                shader->SetUniformValue(uniformLightPosition0, lightPosition0InCameraSpace); 
            }
            if (uniformMaterialLightAmbientColor != -1)
            {
                shader->SetUniformValue(uniformMaterialLightAmbientColor, lightNode0->GetAmbientColor() * GetAmbientColor());
            }
            if (uniformMaterialLightDiffuseColor != -1)
            {
                shader->SetUniformValue(uniformMaterialLightDiffuseColor, lightNode0->GetDiffuseColor() * GetDiffuseColor());
            }
            if (uniformMaterialLightSpecularColor != -1)
            {
                shader->SetUniformValue(uniformMaterialLightSpecularColor, lightNode0->GetSpecularColor() * GetSpecularColor());
            }
            if (uniformMaterialSpecularShininess != -1)
            {
                shader->SetUniformValue(uniformMaterialSpecularShininess, shininess);
            }
            
            if (uniformLightIntensity0 != -1)
            {
                shader->SetUniformValue(uniformLightIntensity0, lightNode0->GetIntensity());
            }
            if (uniformLightAttenuationQ != -1)
            {
                //shader->SetUniformValue(uniformLightAttenuationQ, lightNode0->GetAttenuation());
            }
        }
    }
    
    // TODO: rethink this code
    if (group->renderDataObject->GetIndexBufferID() != 0)
	{
		RenderManager::Instance()->HWDrawElements(PRIMITIVETYPE_TRIANGLELIST, group->indexCount, EIF_16, 0);
	}else
	{
		RenderManager::Instance()->HWDrawElements(PRIMITIVETYPE_TRIANGLELIST, group->indexCount, EIF_16, group->indexArray);
	}
    
	RenderManager::Instance()->SetTexture(0, 1); 
	RenderManager::Instance()->SetState(RenderStateBlock::DEFAULT_3D_STATE);
	if(isAlphablend)
	{
		RenderManager::Instance()->SetBlendMode(oldSrc, oldDst);
	}
}
Example #2
0
void Material::PrepareRenderState(InstanceMaterialState * instanceMaterialState)
{
    ///float32 timeElapsed = SystemTimer::Instance()->FrameDelta();

    if(MATERIAL_UNLIT_TEXTURE_LIGHTMAP == type)
	{
        if (!instanceMaterialState->lightmapTexture)
        {
            SetSetupLightmap(true);
        }
		else
        {
            SetSetupLightmap(false);
            renderStateBlock.SetTexture(instanceMaterialState->lightmapTexture, 1);
        }
    }
	else if (MATERIAL_UNLIT_TEXTURE_DECAL == type || MATERIAL_UNLIT_TEXTURE_DETAIL == type)
    {
        renderStateBlock.SetTexture(textures[Material::TEXTURE_DECAL], 1);
    }


    
	renderStateBlock.shader = shader;

	if (textures[Material::TEXTURE_DIFFUSE])
	{
		renderStateBlock.SetTexture(textures[Material::TEXTURE_DIFFUSE], 0);
	}


	if(MATERIAL_PIXEL_LIT_NORMAL_DIFFUSE == type || MATERIAL_PIXEL_LIT_NORMAL_DIFFUSE_SPECULAR == type || MATERIAL_PIXEL_LIT_NORMAL_DIFFUSE_SPECULAR_MAP == type)
	{
		if (textures[Material::TEXTURE_NORMALMAP])
		{
			renderStateBlock.SetTexture(textures[Material::TEXTURE_NORMALMAP], 1);
		}
	}
	else
	{
//		if (textures[Material::TEXTURE_DECAL])
//		{
//			renderStateBlock.SetTexture(textures[Material::TEXTURE_DECAL], 1);
//		}
	}

	if (isTranslucent || isTwoSided)
	{
		renderStateBlock.state &= ~RenderStateBlock::STATE_CULL;
	}
	else
	{
		renderStateBlock.state |= RenderStateBlock::STATE_CULL;
	}


	if(isAlphablend)
	{
		renderStateBlock.state |= RenderStateBlock::STATE_BLEND;
		//Dizz: temporary solution
		renderStateBlock.state &= ~RenderStateBlock::STATE_DEPTH_WRITE;

		renderStateBlock.SetBlendMode((eBlendMode)blendSrc, (eBlendMode)blendDst);
	}
	else
	{
		//Dizz: temporary solution
		renderStateBlock.state |= RenderStateBlock::STATE_DEPTH_WRITE;
		renderStateBlock.state &= ~RenderStateBlock::STATE_BLEND;
	}

	if(isWireframe)
	{
		renderStateBlock.SetFillMode(FILLMODE_WIREFRAME);
	}
	else
	{
		renderStateBlock.SetFillMode(FILLMODE_SOLID);
	}

	// render
	RenderManager::Instance()->FlushState(&renderStateBlock);
	RenderManager::Instance()->AttachRenderData();


    if(uniformTexture0 != -1)
    {
        shader->SetUniformValue(uniformTexture0, 0);
    }
    if(uniformTexture1 != -1)
    {
        shader->SetUniformValue(uniformTexture1, 1);
    }

	if(isSetupLightmap)
	{
		int32 lightmapSizePosition = shader->FindUniformLocationByName("lightmapSize");
		if (lightmapSizePosition != -1)
		{
			shader->SetUniformValue(lightmapSizePosition, (float32)setupLightmapSize); 
		}
	}
    

	if(MATERIAL_UNLIT_TEXTURE_LIGHTMAP == type)
	{
		if (uniformUvOffset != -1)
			shader->SetUniformValue(uniformUvOffset, instanceMaterialState->uvOffset);
		if (uniformUvScale != -1)
			shader->SetUniformValue(uniformUvScale, instanceMaterialState->uvScale);
	}

	if (isFogEnabled)
	{
		DVASSERT(uniformFogDensity != -1);
        shader->SetUniformValue(uniformFogDensity, fogDensity);
		
        DVASSERT(uniformFogColor != -1)
        shader->SetUniformColor3(uniformFogColor, fogColor);
	}
    
    if (instanceMaterialState)
    {
        if (isFlatColorEnabled)
        {
            DVASSERT(uniformFlatColor != -1);
            shader->SetUniformColor4(uniformFlatColor, instanceMaterialState->flatColor);
        }
        if (isTexture0ShiftEnabled)
        {
            DVASSERT(uniformTexture0Shift != -1);
            shader->SetUniformValue(uniformTexture0Shift, instanceMaterialState->texture0Shift);
        }

        Camera * camera = scene->GetCurrentCamera();
        Light * lightNode0 = instanceMaterialState->GetLight(0);
        if (lightNode0 && camera)
        {
            if (uniformLightPosition0 != -1)
            {
                const Matrix4 & matrix = camera->GetMatrix();
                Vector3 lightPosition0InCameraSpace = lightNode0->GetPosition() * matrix;
                
                shader->SetUniformValue(uniformLightPosition0, lightPosition0InCameraSpace);
            }
            if (uniformMaterialLightAmbientColor != -1)
            {
                shader->SetUniformColor3(uniformMaterialLightAmbientColor, lightNode0->GetAmbientColor() * GetAmbientColor());
            }
            if (uniformMaterialLightDiffuseColor != -1)
            {
                shader->SetUniformColor3(uniformMaterialLightDiffuseColor, lightNode0->GetDiffuseColor() * GetDiffuseColor());
            }
            if (uniformMaterialLightSpecularColor != -1)
            {
                shader->SetUniformColor3(uniformMaterialLightSpecularColor, lightNode0->GetSpecularColor() * GetSpecularColor());
            }
            if (uniformMaterialSpecularShininess != -1)
            {
                shader->SetUniformValue(uniformMaterialSpecularShininess, shininess);
            }
            
            if (uniformLightIntensity0 != -1)
            {
                shader->SetUniformValue(uniformLightIntensity0, lightNode0->GetIntensity());
            }
            if (uniformLightAttenuationQ != -1)
            {
                //shader->SetUniformValue(uniformLightAttenuationQ, lightNode0->GetAttenuation());
            }
        }
    }

}
Example #3
0
//----------------------------------------------------------------------------
MtlLoader::MtlLoader (const string& path, const string& filename)
    :
    mCode(EC_SUCCESSFUL),
    mCurrent(-1)
{
    mLogFile = fopen("MtlLogFile.txt", "wt");
    if (!mLogFile)
    {
        assert(false);
        mCode = EC_LOGFILE_OPEN_FAILED;
        return;
    }

    string filePath = path + filename;
    ifstream inFile(filePath.c_str());
    if (!inFile)
    {
        assert(false);
        mCode = EC_FILE_OPEN_FAILED;
        fprintf(mLogFile, "%s: %s\n", msCodeString[mCode], filePath.c_str());
        fclose(mLogFile);
        return;
    }

    string line;
    vector<string> tokens;

    while (!inFile.eof())
    {
        getline(inFile, line);

        // Skip blank lines.
        if (line == "") { continue; }

        // Skip comments.
        if (line[0] == '#') { continue; }

        GetTokens(line, tokens);
        if (tokens.size() == 0)
        {
            assert(false);
            mCode = EC_NO_TOKENS;
            break;
        }

        // newmtl
        if (GetNewMaterial(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        // illum
        if (GetIlluminationModel(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        // Ka
        if (GetAmbientColor(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        // Kd
        if (GetDiffuseColor(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        // Ks
        if (GetSpecularColor(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        // Tf
        if (GetTransmissionFilter(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        // Ni
        if (GetOpticalDensity(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        // Ni
        if (GetSpecularExponent(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        // map_Kd
        if (GetDiffuseTexture(tokens)) { continue; }
        if (mCode != EC_SUCCESSFUL) { break; }

        assert(false);
        mCode = EC_UNEXPECTED_TOKEN;
        break;
    }

    if (mCode != EC_SUCCESSFUL)
    {
        fprintf(mLogFile, "%s: %s\n", msCodeString[mCode], line.c_str());
    }
    else
    {
        fprintf(mLogFile, "%s\n", msCodeString[EC_SUCCESSFUL]);
    }
    fclose(mLogFile);
    inFile.close();
}