@@ -249,21 +249,21 @@ GLSL_DEFINE(materialEmissionColor, GLSL_LTCTXA(NV_IGRAPH_XF_LTCTXA_CM_COL) ".xyz
249249 alpha_source = alpha_source_specular ;
250250 }
251251
252- if ( state -> ambient_src == MATERIAL_COLOR_SRC_MATERIAL ) {
253- mstring_append_fmt ( body , "oD0 = vec4(sceneAmbientColor, %s);\n" , alpha_source );
254- } else if ( state -> ambient_src == MATERIAL_COLOR_SRC_DIFFUSE ) {
255- mstring_append_fmt ( body , "oD0 = vec4(diffuse.rgb, %s);\n" , alpha_source ) ;
256- } else if ( state -> ambient_src == MATERIAL_COLOR_SRC_SPECULAR ) {
257- mstring_append_fmt ( body , "oD0 = vec4(specular.rgb , %s);\n" , alpha_source );
258- }
259-
260- mstring_append ( body , "oD0.rgb *= materialEmissionColor.rgb;\n" );
261- if ( state -> emission_src == MATERIAL_COLOR_SRC_MATERIAL ) {
262- mstring_append ( body , "oD0.rgb += sceneAmbientColor;\n" );
263- } else if (state -> emission_src == MATERIAL_COLOR_SRC_DIFFUSE ) {
264- mstring_append (body , "oD0.rgb += diffuse.rgb;\n" );
265- } else if (state -> emission_src == MATERIAL_COLOR_SRC_SPECULAR ) {
266- mstring_append (body , "oD0.rgb += specular.rgb;\n" );
252+ // If emission and ambient sources are taken from a vertex color, the scene ambient color is reconstructed from
253+ // the vertex colors.
254+ bool use_scene_ambient =
255+ state -> emission_src == MATERIAL_COLOR_SRC_MATERIAL || state -> ambient_src == MATERIAL_COLOR_SRC_MATERIAL ;
256+ mstring_append_fmt ( body ,
257+ "oD0 = vec4(%s , %s);\n" ,
258+ use_scene_ambient ? "sceneAmbientColor" :
259+ ( state -> emission_src == MATERIAL_COLOR_SRC_DIFFUSE ? "diffuse.rgb" : "specular.rgb" ),
260+ alpha_source
261+ );
262+
263+ if (state -> emission_src == MATERIAL_COLOR_SRC_DIFFUSE || state -> ambient_src == MATERIAL_COLOR_SRC_DIFFUSE ) {
264+ mstring_append (body , "oD0.rgb += diffuse.rgb * materialEmissionColor ;\n" );
265+ } else if (state -> emission_src == MATERIAL_COLOR_SRC_SPECULAR || state -> ambient_src == MATERIAL_COLOR_SRC_SPECULAR ) {
266+ mstring_append (body , "oD0.rgb += specular.rgb * materialEmissionColor ;\n" );
267267 }
268268
269269 mstring_append (body , "oD1 = vec4(0.0, 0.0, 0.0, specular.a);\n" );
@@ -368,8 +368,20 @@ GLSL_DEFINE(materialEmissionColor, GLSL_LTCTXA(NV_IGRAPH_XF_LTCTXA_CM_COL) ".xyz
368368 " vec3 lightSpecular = lightSpecularColor(%d) * attenuation * pf;\n" ,
369369 state -> specular_power , i , i , i );
370370
371- mstring_append (body ,
372- " oD0.xyz += lightAmbient;\n" );
371+ switch (state -> ambient_src ) {
372+ case MATERIAL_COLOR_SRC_MATERIAL :
373+ mstring_append (body ,
374+ " oD0.rgb += lightAmbient;\n" );
375+ break ;
376+ case MATERIAL_COLOR_SRC_DIFFUSE :
377+ mstring_append (body ,
378+ " oD0.rgb += diffuse.rgb * lightAmbient;\n" );
379+ break ;
380+ case MATERIAL_COLOR_SRC_SPECULAR :
381+ mstring_append (body ,
382+ " oD0.rgb += specular.rgb * lightAmbient;\n" );
383+ break ;
384+ }
373385
374386 switch (state -> diffuse_src ) {
375387 case MATERIAL_COLOR_SRC_MATERIAL :
0 commit comments