diff --git a/code/graphics/material.cpp b/code/graphics/material.cpp index 5a57d0359e6..d770af7b6c1 100644 --- a/code/graphics/material.cpp +++ b/code/graphics/material.cpp @@ -301,6 +301,15 @@ int material::get_texture_map(int tex_type) const return Texture_maps[tex_type]; } +int material::get_specular_map(bool* is_spec_gloss) const +{ + const auto reflect_map = get_texture_map(TM_SPEC_GLOSS_TYPE); + if (is_spec_gloss != nullptr) { + *is_spec_gloss = reflect_map >= 0; + } + return reflect_map >= 0 ? reflect_map : get_texture_map(TM_SPECULAR_TYPE); +} + bool material::is_textured() const { return @@ -787,7 +796,7 @@ int model_material::get_shader_runtime_flags() const { flags |= MODEL_SDR_FLAG_DIFFUSE; if (get_texture_map(TM_GLOW_TYPE) >= 0) flags |= MODEL_SDR_FLAG_GLOW; - if (get_texture_map(TM_SPECULAR_TYPE) >= 0 || get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) + if (get_specular_map() >= 0) flags |= MODEL_SDR_FLAG_SPEC; if (get_texture_map(TM_NORMAL_TYPE) >= 0) flags |= MODEL_SDR_FLAG_NORMAL; diff --git a/code/graphics/material.h b/code/graphics/material.h index 3c67ff821a3..3e42be4a513 100644 --- a/code/graphics/material.h +++ b/code/graphics/material.h @@ -100,6 +100,11 @@ class material void set_texture_map(int tex_type, int texture_num); int get_texture_map(int tex_type) const; + + // Returns the -reflect map if present; otherwise returns the -shine slot value (-1 when absent). + // If supplied, is_spec_gloss reports whether the chosen texture uses specular/gloss encoding. + int get_specular_map(bool* is_spec_gloss = nullptr) const; + bool is_textured() const; void set_texture_type(texture_type t_type); diff --git a/code/graphics/opengl/gropengltnl.cpp b/code/graphics/opengl/gropengltnl.cpp index 2c8397c10b7..31c8c28681f 100644 --- a/code/graphics/opengl/gropengltnl.cpp +++ b/code/graphics/opengl/gropengltnl.cpp @@ -1004,23 +1004,14 @@ void opengl_tnl_set_model_material(model_material *material_info) 1); } - if (material_info->get_texture_map(TM_SPECULAR_TYPE) >= 0 || - material_info->get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) { - if (material_info->get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) { - gr_opengl_tcache_set(material_info->get_texture_map(TM_SPEC_GLOSS_TYPE), - TCACHE_TYPE_NORMAL, - &u_scale, - &v_scale, - &array_index, - 2); - } else { - gr_opengl_tcache_set(material_info->get_texture_map(TM_SPECULAR_TYPE), - TCACHE_TYPE_NORMAL, - &u_scale, - &v_scale, - &array_index, - 2); - } + const auto specular_map = material_info->get_specular_map(); + if (specular_map >= 0) { + gr_opengl_tcache_set(specular_map, + TCACHE_TYPE_NORMAL, + &u_scale, + &v_scale, + &array_index, + 2); } if (material_info->get_texture_map(TM_NORMAL_TYPE) >= 0) { diff --git a/code/graphics/uniforms.cpp b/code/graphics/uniforms.cpp index ea36b05dfad..edf65a0a218 100644 --- a/code/graphics/uniforms.cpp +++ b/code/graphics/uniforms.cpp @@ -145,19 +145,12 @@ void convert_model_material(model_uniform_data* data_out, } - if (material.get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) { - data_out->sSpecmapIndex = bm_get_array_index(material.get_texture_map(TM_SPEC_GLOSS_TYPE)); - - data_out->gammaSpec = 1; - data_out->alphaGloss = 1; - - } - - if (material.get_texture_map(TM_SPECULAR_TYPE) >= 0) { - data_out->sSpecmapIndex = bm_get_array_index(material.get_texture_map(TM_SPECULAR_TYPE)); - - data_out->gammaSpec = 0; - data_out->alphaGloss = 0; + bool is_spec_gloss; + const auto specular_map = material.get_specular_map(&is_spec_gloss); + if (specular_map >= 0) { + data_out->sSpecmapIndex = bm_get_array_index(specular_map); + data_out->gammaSpec = is_spec_gloss ? 1 : 0; + data_out->alphaGloss = is_spec_gloss ? 1 : 0; } if (material.get_texture_map(TM_NORMAL_TYPE) >= 0) { diff --git a/code/graphics/vulkan/VulkanDraw.cpp b/code/graphics/vulkan/VulkanDraw.cpp index 8a5eeaecd68..5d741571f13 100644 --- a/code/graphics/vulkan/VulkanDraw.cpp +++ b/code/graphics/vulkan/VulkanDraw.cpp @@ -1304,12 +1304,7 @@ bool VulkanDrawManager::bindMaterialTextures(material* mat, DescriptorWriter* wr setTexture(mat->get_texture_map(TM_BASE_TYPE), TextureSlot::BaseMap, true); setTexture(mat->get_texture_map(TM_GLOW_TYPE), TextureSlot::GlowMap); - // Specular - prefer spec_gloss if available - int specMap = mat->get_texture_map(TM_SPEC_GLOSS_TYPE); - if (specMap < 0) { - specMap = mat->get_texture_map(TM_SPECULAR_TYPE); - } - setTexture(specMap, TextureSlot::SpecMap); + setTexture(mat->get_specular_map(), TextureSlot::SpecMap); setTexture(mat->get_texture_map(TM_NORMAL_TYPE), TextureSlot::NormalMap); setTexture(mat->get_texture_map(TM_HEIGHT_TYPE), TextureSlot::HeightMap); @@ -1453,11 +1448,7 @@ bool VulkanDrawManager::applyMaterial(material* mat, primitive_type prim_type, v MaterialSetInputs matInputs; matInputs.texHandles[0] = mat->get_texture_map(TM_BASE_TYPE); matInputs.texHandles[1] = mat->get_texture_map(TM_GLOW_TYPE); - int specMap = mat->get_texture_map(TM_SPEC_GLOSS_TYPE); - if (specMap < 0) { - specMap = mat->get_texture_map(TM_SPECULAR_TYPE); - } - matInputs.texHandles[2] = specMap; + matInputs.texHandles[2] = mat->get_specular_map(); matInputs.texHandles[3] = mat->get_texture_map(TM_NORMAL_TYPE); matInputs.texHandles[4] = mat->get_texture_map(TM_HEIGHT_TYPE); matInputs.texHandles[5] = mat->get_texture_map(TM_AMBIENT_TYPE); diff --git a/test/src/graphics/test_material.cpp b/test/src/graphics/test_material.cpp index eec20b2cc5c..7267adc1ae2 100644 --- a/test/src/graphics/test_material.cpp +++ b/test/src/graphics/test_material.cpp @@ -14,3 +14,29 @@ TEST(DecalMaterial, SceneNormalFlagSetOnlyWithoutDecalNormalMap) with_normal.set_texture_map(TM_NORMAL_TYPE, 1); EXPECT_EQ(with_normal.get_shader_flags(), 0u); } + +TEST(ModelMaterial, SpecularSelectionPrefersReflectAndReportsEncoding) +{ + model_material mat; + bool is_spec_gloss = true; + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), -1); + EXPECT_FALSE(is_spec_gloss); + + mat.set_texture_map(TM_SPECULAR_TYPE, 0); + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 0); + EXPECT_FALSE(is_spec_gloss); + + mat.set_texture_map(TM_SPEC_GLOSS_TYPE, 1); + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 1); + EXPECT_TRUE(is_spec_gloss); + EXPECT_EQ(mat.get_specular_map(), 1); + + mat.set_texture_map(TM_SPECULAR_TYPE, -1); + mat.set_texture_map(TM_SPEC_GLOSS_TYPE, 0); + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 0); + EXPECT_TRUE(is_spec_gloss); + + mat.set_texture_map(TM_SPEC_GLOSS_TYPE, -1); + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), -1); + EXPECT_FALSE(is_spec_gloss); +}