The interaction of materials, textures and light

I wrote a simple 3D model viewer (using OpenGL 3.2 and GLSL) that can load and display U3D models (stand-alone or inside PDF). It can basically display all the different cells, draw them either by vertex color, texture or material and lighten them accordingly.

However, I did not quite understand what the "expected method" of combining materials and textures is. I saw teaching materials that have light objects with materials (with diffuse / mirror / ambient components and an opacity and gloss factor) and other textbooks that use textures and illuminate them, but I did not find any good resources for what was expected to happen when they are combined (i.e. the mesh has material and one or more texture layers).

Also, in accordance with the specification for the U3D file format in the description of “LitTextureShader”, I support 4 different blending functions that determine how one texture layer is combined with the result of the previous ones:

// Multiply     
blended = current * previous;  
// Add  
blended = clamp(current + previous, 0.0, 1.0);  
// Replace  
blended = current;  
// Blend  
blended = mix( previous , current, current.a );

Initially, I thought it just applied between texture levels, but when I look at models in Adobe Reader, these blending operations seem to apply also between texture and material sometimes, but the result will never be what I expect this (and very very different from my result), so I assumed that there is some basic part that I did not fully understand.

For my tests, I created a grid of one triangle, red material and two checkerboard textures:
CheckerBoard 1and Checkerboard 2

( , emmissive light):

// uniforms, lights etc
// ...

// material input
flat in vec4   matDiffuse;
flat in float  matOpacity;
// ...

// globals
vec3 accumulatedLight = vec3(0.0);  
vec4 currAmbientColor = vec4(0.0);
vec4 currDiffuseColor = vec4(0.0);
float currOpacity = 1.0;
// ...

// lighting
void AddLightOutput()
{
    // add ambient
    vec3 ambientTerm = vec3(0.0);
    if( Lights.enableAmbient == 1 )
    {
        ambientTerm = (vec3(currAmbientColor) * vec3(Lights.ambientIntensity));
    }

    // add diffuse and specular
    vec3 diffuseTerm = vec3(0.0);
    for( int index = 0; index < NUMBER_OF_LIGHTS; index++ )
    {
        // calculate ...
        diffuseTerm += vec3(lgt.diffuse) * lightDot * attenuation;

        // specular (disregard for now)
        // ...
    }

    // add up results results
    accumulatedLight = ambientTerm + (diffuseTerm * vec3(currDiffuseColor));
}

void main()
{
    // set up colors and opacity
    currDiffuseColor = texture(texSampler0, vertTexCoord0) * vec4( vec3(texIntensity0), 1.0 );
    currOpacity = matOpacity;

    // apply material-texture interaction ?
    // ??

    // iterate all lights
    AddLightOutput();

    // set result color
    resultColor = vec4(accumulatedLight, currOpacity);
}  

[ : , ( /) - , ( , , , )?]

, , : , (- ) , "multiply" - "" ( , )

:

// add base color info
currDiffuseColor = texture(texSampler0, vertTexCoord0) * vec4( vec3(texIntensity0), 1.0 );

// modify color according to blend factors
currOpacity = matOpacity * currDiffuseColor.a;
currDiffuseColor = matDiffuse * currDiffuseColor;

Valid XHTML

, , , , "" :

// add base color info
currDiffuseColor = texture(texSampler0, vertTexCoord0) * vec4( vec3(texIntensity0), 1.0 );

// modify color according to blend factors
currOpacity = matOpacity;
currDiffuseColor = vec4( clamp(matDiffuse + currDiffuseColor, 0.0, 1.0).rgb, currDiffuseColor.a );

:

Valid XHTML

, . ( , btw), :

// set up colors and opacity
currDiffuseColor = texture(texSampler0, vertTexCoord0) * vec4( vec3(texIntensity0), 1.0 );
vec4 texLayer1 = texture(texSampler1, vertTexCoord1) * vec4( vec3(texIntensity1), 1.0 );
currDiffuseColor = vec4( clamp(currDiffuseColor + texLayer1, 0.0, 1.0).rgb, texLayer1.a );

Result comparison

, Adobe Reader, , 0.

, , U3D, ( ), Adobe :

Valid XHTML Adobe, , , .

, , , "" . , , Adobe, , -, , , , " " .

, ? , /// ? ? ()? , Adobe , .

PDF, zip: https://s3.amazonaws.com/drabeck/stackoverflow/TestPDF3Ds.zip - , app, (IDTF), U3D ( PDF).

+3

All Articles